Annotation of embedaddon/libnet/sample/hsrp.c, revision 1.1.1.2
1.1 misho 1: /*
2: *
3: * libnet 1.1.2
4: * Build a HSRP packet
5: *
6: * Copyright (c) 2004 David Barroso Berrueta <tomac@wasahero.org>
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28: * SUCH DAMAGE.
29: *
30: */
31:
32: #include "./libnet_test.h"
33:
34:
35: int
36: main(int argc, char *argv[])
37: {
38: uint8_t version = LIBNET_HSRP_VERSION;
39: uint8_t opcode = LIBNET_HSRP_TYPE_HELLO;
40: uint8_t state = LIBNET_HSRP_STATE_ACTIVE;
41: uint8_t hello_time = 3;
42: uint8_t hold_time = 10;
43: uint8_t priority = 1;
44: uint8_t group = 1;
45: uint8_t reserved = 0;
46: uint8_t authdata[8];
47:
48: libnet_t *l;
49: char *device = NULL;
50: char src[] = "1.1.1.1";
51: char *eth_dst = "DE:AD:00:00:BE:EF";
52: char dst[] = "224.0.0.2";
53: int port = 1985;
54: int c;
55: u_long src_ip, dst_ip;
56: char errbuf[LIBNET_ERRBUF_SIZE];
57: libnet_ptag_t ptag = 0;
58:
59: printf("libnet 1.1.2 packet shaping: HSRP[link]\n");
60:
61: /*
62: * Initialize the library. Root priviledges are required.
63: */
64: l = libnet_init(
65: LIBNET_LINK_ADV, /* injection type */
66: device, /* network interface */
67: errbuf); /* error buffer */
68:
69: if (l == NULL)
70: {
71: fprintf(stderr, "libnet_init() failed: %s", errbuf);
72: exit(EXIT_FAILURE);
73: }
74:
75: printf("Using device %s\n", l->device);
76:
77: if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == (u_long)-1)
78: {
79: fprintf(stderr, "Bad destination IP address: %s\n", dst);
80: exit(EXIT_FAILURE);
81: }
82:
83: if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == (u_long)-1)
84: {
85: fprintf(stderr, "Bad source IP address: %s\n", src);
86: exit(EXIT_FAILURE);
87: }
88:
89: memset(authdata, 0, 8);
1.1.1.2 ! misho 90: strncpy((char *)authdata, "cisco", 5);
1.1 misho 91:
92:
93: ptag = libnet_build_hsrp(
94: version,
95: opcode,
96: state,
97: hello_time, /* Recommended hello time */
98: hold_time, /* Recommended hold time */
99: priority, /* Priority */
100: group, /* Group */
101: reserved, /* Reserved */
102: authdata, /* Password */
103: src_ip, /* Virtual IP */
104: NULL,
105: 0,
106: l,
107: 0
108: );
109:
110: if (ptag == -1)
111: {
112: fprintf(stderr, "Can't build HSRP header: %s\n", libnet_geterror(l));
113: goto bad;
114: }
115:
116: ptag = libnet_build_udp(
117: port, /* source port */
118: port, /* destination port */
119: LIBNET_UDP_H + LIBNET_HSRP_H , /* packet length */
120: 0, /* checksum */
121: NULL, /* payload */
122: 0, /* payload size */
123: l, /* libnet handle */
124: 0); /* libnet id */
125:
126: if (ptag == -1)
127: {
128: fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
129: goto bad;
130: }
131:
132: ptag = libnet_build_ipv4(
133: LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_HSRP_H,/* length */
134: 0, /* TOS */
135: 666, /* IP ID */
136: 0, /* IP Frag */
137: 1 , /* TTL */
138: IPPROTO_UDP, /* protocol */
139: 0, /* checksum */
140: src_ip, /* source IP */
141: dst_ip, /* destination IP */
142: NULL, /* payload */
143: 0, /* payload size */
144: l, /* libnet handle */
145: 0); /* libnet id */
146:
147: if (ptag == -1)
148: {
149: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
150: exit(EXIT_FAILURE);
151: }
152:
153:
1.1.1.2 ! misho 154: eth_dst = (char *)libnet_hex_aton(eth_dst, &c);
1.1 misho 155: ptag = libnet_autobuild_ethernet(
1.1.1.2 ! misho 156: (uint8_t *)eth_dst, /* ethernet destination */
1.1 misho 157: ETHERTYPE_IP, /* protocol type */
158: l); /* libnet handle */
159:
160: free(eth_dst);
161: if (ptag == -1)
162: {
163: fprintf(stderr, "Can't build ethernet header: %s\n",
164: libnet_geterror(l));
165: goto bad;
166: }
167:
168:
169: /*
170: * write to the wire
171: */
172: c = libnet_write(l);
173: if (c == -1)
174: {
175: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
176: goto bad;
177: }
178: else
179: {
180: fprintf(stderr, "Wrote %d byte HSRP packet; check the wire.\n", c);
181: }
182: libnet_destroy(l);
183: return (EXIT_SUCCESS);
184: bad:
185: libnet_destroy(l);
186: return (EXIT_FAILURE);
187:
188: return 0;
189: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>