2 * $Id: libnet-example-2.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * example 2: link-layer api / ICMP hostmask packet
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 u_char enet_src[6] = {0x0d, 0x0e, 0x0a, 0x0d, 0x00, 0x00};
38 u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
41 main(int argc, char *argv[])
43 int packet_size, /* size of our packet */
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 */
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");
59 while ((c = getopt(argc, argv, "i:d:s:")) != EOF)
64 if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
66 libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);
74 if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
76 libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);
84 if (!src_ip || !dst_ip)
92 * Step 1: Network Initialization (interchangable with step 2).
96 struct sockaddr_in sin;
98 * Try to locate a device.
100 if (libnet_select_device(&sin, &device, err_buf) == -1)
102 libnet_error(LIBNET_ERR_FATAL, "libnet_select_device failed: %s\n", err_buf);
104 printf("device:\t\t\t\t%s\n", device);
106 if ((network = libnet_open_link_interface(device, err_buf)) == NULL)
108 libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", err_buf);
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.
117 packet_size = LIBNET_IP_H + LIBNET_ETH_H + LIBNET_ICMP_MASK_H;
121 * Step 2: Memory Initialization (interchangable with step 1).
123 if (libnet_init_packet(packet_size, &packet) == -1)
125 libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n");
130 * Step 3: Packet construction (ethernet header).
132 libnet_build_ethernet(enet_dst,
140 * Step 3: Packet construction (IP header).
142 libnet_build_ip(ICMP_MASK_H,
147 IPPROTO_ICMP, /* Transport protocol */
148 src_ip, /* Source IP */
149 dst_ip, /* Destination IP */
150 NULL, /* Pointer to payload (none) */
152 packet + LIBNET_ETH_H); /* Packet header memory */
156 * Step 3: Packet construction (ICMP header).
158 libnet_build_icmp_mask(ICMP_MASKREPLY, /* type */
162 0xffffffff, /* mask */
165 packet + LIBNET_ETH_H + LIBNET_IP_H);
168 * Step 4: Packet checksums (ICMP header *AND* IP header).
170 if (libnet_do_checksum(packet + ETH_H, IPPROTO_ICMP, LIBNET_ICMP_MASK_H) == -1)
172 libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
174 if (libnet_do_checksum(packet + ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1)
176 libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
181 * Step 5: Packet injection.
183 c = libnet_write_link_layer(network, device, packet, packet_size);
186 libnet_error(LN_ERR_WARNING, "libnet_write_link_layer only wrote %d bytes\n", c);
190 printf("construction and injection completed, wrote all %d bytes\n", c);
195 * Shut down the interface.
197 if (libnet_close_link_interface(network) == -1)
199 libnet_error(LN_ERR_WARNING, "libnet_close_link_interface couldn't close the interface");
204 * Free packet memory.
206 libnet_destroy_packet(&packet);
208 return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
215 fprintf(stderr, "usage: %s [-i interface] -s s_ip -d d_ip\n", name);