2 * $Id: tcp+data+ipopt.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * TCP Packet assembly tester (with payload and IP options)
7 * Copyright (c) 1998, 1999, 2000 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
34 #include "../../include/config.h"
36 #include "../libnet_test.h"
39 main(int argc, char **argv)
42 u_long src_ip, dst_ip;
43 u_short src_prt, dst_prt;
45 u_char *payload = "hello world";
46 u_char *options = " This here is a 40-byte IP option list.";
47 int payload_s = strlen(payload);
48 int option_s = strlen(options);
49 struct ipoption ipopt;
55 printf("IP + options and TCP + payload packet building/writing test\n");
57 while((c = getopt(argc, argv, "d:s:")) != EOF)
62 * We expect the input to be of the form `ip.ip.ip.ip.port`. We
63 * point cp to the last dot of the IP address/port string and
64 * then seperate them with a NULL byte. The optarg now points to
65 * just the IP address, and cp points to the port.
68 if (!(cp = strrchr(optarg, '.')))
73 dst_prt = (u_short)atoi(cp);
74 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
76 fprintf(stderr, "Bad destination IP address: %s\n", optarg);
81 if (!(cp = strrchr(optarg, '.')))
86 src_prt = (u_short)atoi(cp);
87 if (!(src_ip = libnet_name_resolve(optarg, 1)))
89 fprintf(stderr, "Bad source IP address: %s\n", optarg);
96 if (!src_ip || !src_prt || !dst_ip || !dst_prt)
103 * Get packet memory. Let's get plenty.
105 buf = malloc(IP_MAXPACKET);
108 perror("No memory for packet");
113 * Open our raw IP socket and set IP_HDRINCL.
115 sock = libnet_open_raw_sock(IPPROTO_RAW);
123 * Build the IP header (shown exploded for commenting).
125 libnet_build_ip(LIBNET_TCP_H + payload_s,/* Size of the payload */
130 IPPROTO_TCP, /* Transport protocol */
131 src_ip, /* Source IP */
132 dst_ip, /* Destination IP */
133 NULL, /* Pointer to payload (none) */
135 buf); /* Packet header memory */
138 * Build the TCP header.
140 libnet_build_tcp(src_prt, /* Source TCP port */
141 dst_prt, /* Destination TCP port */
142 11111, /* Sequence number */
143 99999, /* Acknowledgement number */
144 TH_SYN|TH_ACK, /* Control flags */
145 1024, /* Window size */
146 0, /* Urgent pointer */
147 payload, /* Pointer to payload */
149 buf + LIBNET_IP_H); /* Packet header memory */
152 * Calculate the TCP header checksum (IP header checksum is *always* done
155 libnet_do_checksum(buf, IPPROTO_TCP, LIBNET_TCP_H + payload_s);
157 memcpy(ipopt.ipopt_list, options, option_s);
158 *(ipopt.ipopt_list) = IPOPT_SECURITY;
159 *(ipopt.ipopt_list + 1) = 1;
162 * Insert the IP options.
164 c = libnet_insert_ipo(&ipopt, /* pointer to ipopt struct */
165 option_s, /* Length of option list */
166 buf); /* Packet header memory */
170 fprintf(stderr, "Can't add options, discarding them.\n");
174 * Write the packet to the network.
176 c = libnet_write_ip(sock, buf, LIBNET_TCP_H + LIBNET_IP_H + payload_s
178 if (c < LIBNET_TCP_H + LIBNET_IP_H + payload_s + option_s)
180 fprintf(stderr, "libnet_write_ip: %s\n", strerror(errno));
182 printf("Completed, wrote %d bytes\n", c);
185 return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
193 "usage: %s -s source_ip.source_port -d destination_ip.destination_port\n",