Annotation of embedaddon/libnet/sample/arp.c, revision 1.1.1.1
1.1 misho 1: /*
2: * $Id: arp.c,v 1.6 2004/03/01 20:26:12 mike Exp $
3: *
4: * libnet 1.1
5: * Build an 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: #if ((_WIN32) && !(__CYGWIN__))
35: #include "../include/win32/config.h"
36: #else
37: #include "../include/config.h"
38: #endif
39: #endif
40: #include "./libnet_test.h"
41:
42: int
43: main(int argc, char *argv[])
44: {
45: int c;
46: u_int32_t i;
47: libnet_t *l;
48: libnet_ptag_t t;
49: char *device = NULL;
50: u_int8_t *packet;
51: u_int32_t packet_s;
52: char errbuf[LIBNET_ERRBUF_SIZE];
53:
54: printf("libnet 1.1 packet shaping: ARP[link -- autobuilding ethernet]\n");
55:
56: if (argc > 1)
57: {
58: device = argv[1];
59: }
60:
61: l = libnet_init(
62: LIBNET_LINK_ADV, /* injection type */
63: device, /* network interface */
64: errbuf); /* errbuf */
65:
66: if (l == NULL)
67: {
68: fprintf(stderr, "%s", errbuf);
69: exit(EXIT_FAILURE);
70: }
71: else
72:
73: /*
74: * Build the packet, remmebering that order IS important. We must
75: * build the packet from lowest protocol type on up as it would
76: * appear on the wire. So for our ARP packet:
77: *
78: * -------------------------------------------
79: * | Ethernet | ARP |
80: * -------------------------------------------
81: * ^ ^
82: * |------------------ |
83: * libnet_build_ethernet()--| |
84: * |
85: * libnet_build_arp()-----------|
86: */
87:
88: i = libnet_get_ipaddr4(l);
89:
90: t = libnet_build_arp(
91: ARPHRD_ETHER, /* hardware addr */
92: ETHERTYPE_IP, /* protocol addr */
93: 6, /* hardware addr size */
94: 4, /* protocol addr size */
95: ARPOP_REPLY, /* operation type */
96: enet_src, /* sender hardware addr */
97: (u_int8_t *)&i, /* sender protocol addr */
98: enet_dst, /* target hardware addr */
99: (u_int8_t *)&i, /* target protocol addr */
100: NULL, /* payload */
101: 0, /* payload size */
102: l, /* libnet context */
103: 0); /* libnet id */
104: if (t == -1)
105: {
106: fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
107: goto bad;
108: }
109:
110: t = libnet_autobuild_ethernet(
111: enet_dst, /* ethernet destination */
112: ETHERTYPE_ARP, /* protocol type */
113: l); /* libnet handle */
114: if (t == -1)
115: {
116: fprintf(stderr, "Can't build ethernet header: %s\n",
117: libnet_geterror(l));
118: goto bad;
119: }
120:
121:
122: if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
123: {
124: fprintf(stderr, "%s", libnet_geterror(l));
125: }
126: else
127: {
128: fprintf(stderr, "packet size: %d\n", packet_s);
129: libnet_adv_free_packet(l, packet);
130: }
131:
132: c = libnet_write(l);
133:
134: if (c == -1)
135: {
136: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
137: goto bad;
138: }
139: else
140: {
141: fprintf(stderr, "Wrote %d byte ARP packet from context \"%s\"; "
142: "check the wire.\n", c, libnet_cq_getlabel(l));
143: }
144: libnet_destroy(l);
145: return (EXIT_SUCCESS);
146: bad:
147: libnet_destroy(l);
148: return (EXIT_FAILURE);
149: }
150:
151: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>