Annotation of embedaddon/libnet/sample/ieee.c, revision 1.1.1.2
1.1 misho 1: /*
2: * $Id: ieee.c,v 1.2 2004/01/03 20:31:01 mike Exp $
3: *
4: * libnet 1.1
5: * Build an ieee 802.1q/ARP packet
6: *
7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8: * All rights reserved.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: *
31: */
32:
33: #if (HAVE_CONFIG_H)
34: #include "../include/config.h"
35: #endif
36: #include "./libnet_test.h"
37:
38: int
39: main(int argc, char *argv[])
40: {
41: int c, len, do_802_2;
42: u_long i;
43: libnet_t *l;
44: libnet_ptag_t t;
45: u_char *dst, *src, oui[3];
46: char *device = NULL;
47: char errbuf[LIBNET_ERRBUF_SIZE];
48:
49: printf("libnet 1.1 packet shaping: ieee[802.1q / 802.2 / ARP]\n");
50:
51: do_802_2 = 0;
52: device = NULL;
53: src = dst = NULL;
54: while ((c = getopt(argc, argv, "8d:i:s:")) != EOF)
55: {
56: switch (c)
57: {
58: case '8':
59: do_802_2 = 1;
60: break;
61: case 'd':
62: dst = libnet_hex_aton(optarg, &len);
63: break;
64: case 'i':
65: device = optarg;
66: break;
67: case 's':
68: src = libnet_hex_aton(optarg, &len);
69: break;
70: }
71: }
72:
73: if (src == NULL || dst == NULL)
74: {
75: fprintf(stderr, "usage %s -d dst -s src [-8 ] [-i interface]\n",
76: argv[0]);
77: exit(EXIT_FAILURE);
78: }
79:
80: /*
81: * Initialize the library. Root priviledges are required.
82: */
83: l = libnet_init(
84: LIBNET_LINK, /* injection type */
85: device, /* network interface */
86: errbuf); /* errbuf */
87:
88: if (l == NULL)
89: {
90: fprintf(stderr, "libnet_init() failed: %s", errbuf);
91: exit(EXIT_FAILURE);
92: }
93:
94: i = libnet_get_ipaddr4(l);
95:
96: t = libnet_build_arp(
97: ARPHRD_ETHER, /* hardware addr */
98: ETHERTYPE_IP, /* protocol addr */
99: 6, /* hardware addr size */
100: 4, /* protocol addr size */
101: ARPOP_REPLY, /* operation type */
102: src, /* sender hardware addr */
103: (u_char *)&i, /* sender protocol addr */
104: dst, /* target hardware addr */
105: (u_char *)&i, /* target protocol addr */
106: NULL, /* payload */
107: 0, /* payload size */
108: l, /* libnet handle */
109: 0); /* libnet id */
110: if (t == -1)
111: {
112: fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
113: goto bad;
114: }
115:
116: if (do_802_2)
117: {
118: memset(&oui, 0, 3);
119: t = libnet_build_802_2snap(
120: 0xaa, /* SNAP DSAP */
121: 0xaa, /* SNAP SSAP */
122: 0, /* control */
123: oui, /* oui */
124: ETHERTYPE_ARP, /* ARP header follows */
125: NULL, /* payload */
126: 0, /* payload size */
127: l, /* libnet handle */
128: 0); /* libnet id */
129: if (t == -1)
130: {
131: fprintf(stderr, "Can't build 802.2 header: %s\n", libnet_geterror(l));
132: goto bad;
133: }
134: }
135: t = libnet_build_802_1q(
136: dst, /* dest mac */
137: src, /* source mac */
138: ETHERTYPE_VLAN, /* TPI */
139: 0x006, /* priority (0 - 7) */
140: 0x001, /* CFI flag */
141: 0x100, /* vid (0 - 4095) */
142: do_802_2 ? LIBNET_802_2SNAP_H + LIBNET_ARP_ETH_IP_H : 0x0806,
143: NULL, /* payload */
144: 0, /* payload size */
145: l, /* libnet handle */
146: 0); /* libnet id */
147: if (t == -1)
148: {
149: fprintf(stderr, "Can't build 802.1q header: %s\n", libnet_geterror(l));
150: goto bad;
151: }
152: /*
153: * Write it to the wire.
154: */
155: c = libnet_write(l);
156:
157: if (c == -1)
158: {
159: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
160: goto bad;
161: }
162: else
163: {
164: fprintf(stderr, "Wrote %d byte 802.1q packet; check the wire.\n", c);
165: }
166: free(dst);
167: free(src);
168: libnet_destroy(l);
169: return (EXIT_SUCCESS);
170: bad:
171: free(dst);
172: free(src);
173: libnet_destroy(l);
174: return (EXIT_FAILURE);
175: }
176:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>