2 * $Id: libnet-example-4.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * example 4: link-layer api / UDP packet using port list chaining
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
35 #define MAX_PAYLOAD_SIZE 1024
39 u_char enet_src[6] = {0x0d, 0x0e, 0x0a, 0x0d, 0x00, 0x00};
40 u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
43 main(int argc, char *argv[])
45 int packet_size, /* size of our packet */
46 payload_size, /* size of our packet */
48 u_long src_ip, dst_ip; /* source ip, dest ip */
49 u_short bport, eport; /* beginning and end ports */
50 u_short cport; /* current port */
51 u_char payload[MAX_PAYLOAD_SIZE]; /* packet payload */
52 u_char *packet; /* pointer to our packet buffer */
53 char err_buf[LIBNET_ERRBUF_SIZE]; /* error buffer */
54 u_char *device; /* pointer to the device to use */
55 struct libnet_link_int *network; /* pointer to link interface struct */
56 struct libnet_plist_chain plist; /* plist chain */
57 struct libnet_plist_chain *plist_p; /* plist chain pointer */
59 printf("libnet example code:\tmodule 4\n\n");
60 printf("packet injection interface:\tlink layer\n");
61 printf("packet type:\t\t\tUDP [with payload] using port list chaining\n");
68 while ((c = getopt(argc, argv, "i:d:s:p:")) != EOF)
73 if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
75 libnet_error(LIBNET_ERR_FATAL,
76 "Bad destination IP address: %s\n", optarg);
84 if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
86 libnet_error(LIBNET_ERR_FATAL,
87 "Bad source IP address: %s\n", optarg);
92 if (libnet_plist_chain_new(&plist_p, optarg) == -1)
94 libnet_error(LIBNET_ERR_FATAL, "Could not build port list\n");
103 if (!src_ip || !dst_ip || !plist_p)
115 memset(payload, 0, sizeof(payload));
116 strncpy(payload, argv[optind], strlen(argv[optind]));
120 * Step 1: Network Initialization (interchangable with step 2).
124 struct sockaddr_in sin;
126 * Try to locate a device.
128 if (libnet_select_device(&sin, &device, err_buf) == -1)
130 libnet_error(LIBNET_ERR_FATAL, "libnet_select_device failed: %s\n", err_buf);
132 printf("device:\t\t\t\t%s\n", device);
134 if ((network = libnet_open_link_interface(device, err_buf)) == NULL)
136 libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", err_buf);
140 * Get the payload from the user. Hrm. This might fail on a Sparc
141 * if byte alignment is off...
143 payload_size = strlen(payload);
146 * We're going to build a UDP packet with a payload using the
147 * link-layer API, so this time we need memory for a ethernet header
148 * as well as memory for the ICMP and IP headers and our payload.
150 packet_size = LIBNET_IP_H + LIBNET_ETH_H + LIBNET_UDP_H + payload_size;
153 * Step 2: Memory Initialization (interchangable with step 1).
155 if (libnet_init_packet(packet_size, &packet) == -1)
157 libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n");
162 * Step 3: Packet construction (ethernet header).
164 libnet_build_ethernet(enet_dst,
172 * Step 3: Packet construction (IP header).
174 libnet_build_ip(LIBNET_UDP_H + payload_size,
179 IPPROTO_UDP, /* Transport protocol */
180 src_ip, /* Source IP */
181 dst_ip, /* Destination IP */
182 NULL, /* Pointer to payload (none) */
184 packet + LIBNET_ETH_H); /* Packet header memory */
187 while (libnet_plist_chain_next_pair(plist_p, &bport, &eport))
189 while (!(bport > eport) && bport != 0)
193 * Step 3: Packet construction (UDP header).
195 libnet_build_udp(242, /* source port */
196 cport, /* dest. port */
197 payload, /* payload */
198 payload_size, /* payload length */
199 packet + LIBNET_ETH_H + LIBNET_IP_H);
202 * Step 4: Packet checksums (ICMP header *AND* IP header).
204 if (libnet_do_checksum(packet + ETH_H, IPPROTO_UDP, LIBNET_UDP_H + payload_size) == -1)
206 libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
208 if (libnet_do_checksum(packet + ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1)
210 libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
214 * Step 5: Packet injection.
216 c = libnet_write_link_layer(network, device, packet, packet_size);
219 libnet_error(LN_ERR_WARNING, "libnet_write_link_layer only wrote %d bytes\n", c);
223 printf("construction and injection completed, wrote all %d bytes, port %d\n", c, cport);
228 * Shut down the interface.
230 if (libnet_close_link_interface(network) == -1)
232 libnet_error(LN_ERR_WARNING, "libnet_close_link_interface couldn't close the interface");
237 * Free packet memory.
239 libnet_destroy_packet(&packet);
241 return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
248 fprintf(stderr, "usage: %s [-i interface] -s s_ip -d d_ip -p port list payload\n", name);