1 /*
2  *  $Id: arp.c,v 1.1.1.1 1999/05/18 15:33:43 dugsong Exp $
3  *
4  *  libnet
5  *  arp.c - Build an ARP packet
6  *
7  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
8  *                           route|daemon9 <route@infonexus.com>
9  *  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #if (HAVE_CONFIG_H)
35 #include "../../include/config.h"
36 #endif
37 #include "../libnet_test.h"
38 
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42     int  c;
43     char errbuf[256];
44     char *device = NULL;
45     struct link_int *l;
46 
47     while ((c = getopt(argc, argv, "i:")) != EOF)
48     {
49         switch (c)
50         {
51             case 'i':
52                 device = optarg;
53                 break;
54             default:
55                 exit(EXIT_FAILURE);
56         }
57     }
58 
59     if (!device)
60     {
61         fprintf(stderr, "Specify a device\n");
62         exit(EXIT_FAILURE);
63     }
64 
65     l = libnet_open_link_interface(device, errbuf);
66     if (!l)
67     {
68         fprintf(stderr, "libnet_open_link_interface: %s\n", errbuf);
69         exit(EXIT_FAILURE);
70     }
71     c = send_arp(l, device);
72 
73     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
74 }
75 
76 
77 int
send_arp(struct link_int * l,u_char * device)78 send_arp(struct link_int *l, u_char *device)
79 {
80     int n;
81     u_char *buf;
82 
83     if (libnet_init_packet(ARP_H + ETH_H, &buf) == -1)
84     {
85         perror("libnet_init_packet memory:");
86         exit(EXIT_FAILURE);
87     }
88 
89     /*
90      *  Ethernet header
91      */
92     libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_ARP, NULL, 0, buf);
93 
94     /*
95      *  ARP header
96      */
97     libnet_build_arp(ARPHRD_ETHER,
98         ETHERTYPE_IP,
99         6,
100         4,
101         ARPOP_REQUEST,
102         enet_src,
103         ip_src,
104         enet_dst,
105         ip_dst,
106         NULL,
107         0,
108         buf + ETH_H);
109 
110     n = libnet_write_link_layer(l, device, buf, ARP_H + ETH_H);
111 
112     printf("Wrote %d byte ARP packet through linktype %d\n", n, l->linktype);
113 
114     libnet_destroy_packet(&buf);
115     return (n);
116 }
117 
118 /* EOF */
119