OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / example / libnet-example-2.c
1 /*
2  *  $Id: libnet-example-2.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet example code
5  *  example 2:  link-layer api / ICMP hostmask packet
6  *
7  *  Copyright (c) 1998 - 2001 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 #include <libnet.h>
34
35 void usage(char *);
36
37 u_char enet_src[6] = {0x0d, 0x0e, 0x0a, 0x0d, 0x00, 0x00};
38 u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
39
40 int
41 main(int argc, char *argv[])
42 {
43     int packet_size,                    /* size of our packet */
44         c;                              /* misc */
45     u_long src_ip, dst_ip;              /* source ip, dest ip */
46     u_char *packet;                     /* pointer to our packet buffer */
47     char err_buf[LIBNET_ERRBUF_SIZE];   /* error buffer */
48     u_char *device;                     /* pointer to the device to use */
49     struct libnet_link_int *network;    /* pointer to link interface struct */
50
51     printf("libnet example code:\tmodule 2\n\n");
52     printf("packet injection interface:\tlink layer\n");
53     printf("packet type:\t\t\tICMP net mask [no payload]\n");
54
55     device = NULL;
56     src_ip  = 0;
57     dst_ip  = 0;
58
59     while ((c = getopt(argc, argv, "i:d:s:")) != EOF)
60     {
61         switch (c)
62         {
63             case 'd':
64                 if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
65                 {
66                     libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);
67
68                 }
69                 break;
70             case 'i':
71                 device = optarg;
72                 break;
73             case 's':
74                 if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
75                 {
76                     libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);
77                 }
78                 break;
79             default:
80                 exit(EXIT_FAILURE);
81         }
82     }
83
84     if (!src_ip || !dst_ip)
85     {
86         usage(argv[0]);
87         exit(EXIT_FAILURE);
88     }
89
90
91     /*
92      *  Step 1: Network Initialization (interchangable with step 2).
93      */
94     if (device == NULL)
95     {
96         struct sockaddr_in sin;
97         /*
98          *  Try to locate a device.
99          */
100         if (libnet_select_device(&sin, &device, err_buf) == -1)
101         {
102             libnet_error(LIBNET_ERR_FATAL, "libnet_select_device failed: %s\n", err_buf);
103         }
104         printf("device:\t\t\t\t%s\n", device);
105     }
106     if ((network = libnet_open_link_interface(device, err_buf)) == NULL)
107     {
108         libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", err_buf);
109     }
110
111
112     /*
113      *  We're going to build an ICMP packet with no payload using the
114      *  link-layer API, so this time we need memory for a ethernet header
115      *  as well as memory for the ICMP and IP headers.
116      */
117     packet_size = LIBNET_IP_H + LIBNET_ETH_H + LIBNET_ICMP_MASK_H;
118
119
120     /*
121      *  Step 2: Memory Initialization (interchangable with step 1).
122      */
123     if (libnet_init_packet(packet_size, &packet) == -1)
124     {
125         libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n");
126     }
127
128
129     /*
130      *  Step 3: Packet construction (ethernet header).
131      */
132     libnet_build_ethernet(enet_dst,
133             enet_src,
134             ETHERTYPE_IP,
135             NULL,
136             0,
137             packet);
138
139     /*
140      *  Step 3: Packet construction (IP header).
141      */
142     libnet_build_ip(ICMP_MASK_H,
143             0,                      /* IP tos */
144             242,                    /* IP ID */
145             0,                      /* Frag */
146             64,                     /* TTL */
147             IPPROTO_ICMP,           /* Transport protocol */
148             src_ip,                 /* Source IP */
149             dst_ip,                 /* Destination IP */
150             NULL,                   /* Pointer to payload (none) */
151             0,
152             packet + LIBNET_ETH_H); /* Packet header memory */
153
154
155     /*
156      *  Step 3: Packet construction (ICMP header).
157      */
158     libnet_build_icmp_mask(ICMP_MASKREPLY,  /* type */
159             0,                      /* code */ 
160             242,                    /* id */ 
161             0,                      /* seq */ 
162             0xffffffff,             /* mask */
163             NULL,                   /* payload */ 
164             0,                      /* payload_s */ 
165             packet + LIBNET_ETH_H + LIBNET_IP_H);
166
167     /*
168      *  Step 4: Packet checksums (ICMP header *AND* IP header).
169      */
170     if (libnet_do_checksum(packet + ETH_H, IPPROTO_ICMP, LIBNET_ICMP_MASK_H) == -1)
171     {
172         libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
173     }
174     if (libnet_do_checksum(packet + ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1)
175     {
176         libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
177     }
178
179
180     /*
181      *  Step 5: Packet injection.
182      */
183     c = libnet_write_link_layer(network, device, packet, packet_size);
184     if (c < packet_size)
185     {
186         libnet_error(LN_ERR_WARNING, "libnet_write_link_layer only wrote %d bytes\n", c);
187     }
188     else
189     {
190         printf("construction and injection completed, wrote all %d bytes\n", c);
191     }
192
193
194     /*
195      *  Shut down the interface.
196      */
197     if (libnet_close_link_interface(network) == -1)
198     {   
199         libnet_error(LN_ERR_WARNING, "libnet_close_link_interface couldn't close the interface");
200     }
201
202
203     /*
204      *  Free packet memory.
205      */
206     libnet_destroy_packet(&packet);
207
208     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
209 }
210
211
212 void
213 usage(char *name)
214 {
215     fprintf(stderr, "usage: %s [-i interface] -s s_ip -d d_ip\n", name);
216 }
217
218 /* EOF */