2 * $Id: libnet-example-1.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * example 1: raw socket api / TCP 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
38 main(int argc, char **argv)
40 int network, packet_size, c;
41 u_long src_ip, dst_ip;
42 u_short src_prt, dst_prt;
45 printf("libnet example code:\tmodule 1\n\n");
46 printf("packet injection interface:\traw socket\n");
47 printf("packet type:\t\t\tTCP [no payload]\n");
54 while((c = getopt(argc, argv, "d:s:")) != EOF)
59 * We expect the input to be of the form `ip.ip.ip.ip.port`. We
60 * point cp to the last dot of the IP address/port string and
61 * then seperate them with a NULL byte. The optarg now points to
62 * just the IP address, and cp points to the port.
65 if (!(cp = strrchr(optarg, '.')))
70 dst_prt = (u_short)atoi(cp);
71 if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
73 libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);
77 if (!(cp = strrchr(optarg, '.')))
82 src_prt = (u_short)atoi(cp);
83 if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
85 libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);
90 if (!src_ip || !src_prt || !dst_ip || !dst_prt)
97 * We're just going to build a TCP packet with no payload using the
98 * raw sockets API, so we only need memory for a TCP header and an IP
101 packet_size = LIBNET_IP_H + LIBNET_TCP_H;
104 * Step 1: Memory initialization (interchangable with step 2).
106 libnet_init_packet(packet_size, &packet);
109 libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n");
113 * Step 2: Network initialization (interchangable with step 1).
115 network = libnet_open_raw_sock(IPPROTO_RAW);
118 libnet_error(LIBNET_ERR_FATAL, "Can't open network.\n");
123 * Step 3: Packet construction (IP header).
125 libnet_build_ip(LIBNET_TCP_H, /* size of the packet sans IP header */
126 IPTOS_LOWDELAY, /* IP tos */
130 IPPROTO_TCP, /* transport protocol */
131 src_ip, /* source IP */
132 dst_ip, /* destination IP */
133 NULL, /* payload (none) */
134 0, /* payload length */
135 packet); /* packet header memory */
139 * Step 3: Packet construction (TCP header).
141 libnet_build_tcp(src_prt, /* source TCP port */
142 dst_prt, /* destination TCP port */
143 0xa1d95, /* sequence number */
144 0x53, /* acknowledgement number */
145 TH_SYN, /* control flags */
146 1024, /* window size */
147 0, /* urgent pointer */
148 NULL, /* payload (none) */
149 0, /* payload length */
150 packet + LIBNET_IP_H); /* packet header memory */
154 * Step 4: Packet checksums (TCP header only).
156 if (libnet_do_checksum(packet, IPPROTO_TCP, LIBNET_TCP_H) == -1)
158 libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
163 * Step 5: Packet injection.
165 c = libnet_write_ip(network, packet, packet_size);
168 libnet_error(LN_ERR_WARNING, "libnet_write_ip only wrote %d bytes\n", c);
172 printf("construction and injection completed, wrote all %d bytes\n", c);
176 * Shut down the interface.
178 if (libnet_close_raw_sock(network) == -1)
180 libnet_error(LN_ERR_WARNING, "libnet_close_raw_sock couldn't close the interface");
185 * Free packet memory.
187 libnet_destroy_packet(&packet);
189 return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
196 fprintf(stderr, "usage: %s -s s_ip.s_port -d d_ip.d_port\n", name);