Annotation of embedaddon/libnet/sample/tcp2.c, revision 1.1.1.1
1.1 misho 1: /*
2: * $Id: tcp2.c,v 1.3 2004/01/28 19:45:00 mike Exp $
3: *
4: * libnet 1.1
5: * raw_tcp.c - Build a TCP packet
6: *
7: * Copyright (c) 1998 - 2004 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: #if (HAVE_CONFIG_H)
34: #include "../include/config.h"
35: #endif
36: #include "./libnet_test.h"
37:
38: int
39: main(int argc, char **argv)
40: {
41: int c;
42: u_char *cp;
43: libnet_t *l;
44: libnet_ptag_t t;
45: char *payload;
46: u_short payload_s;
47: u_long src_ip, dst_ip;
48: u_short src_prt, dst_prt;
49: char errbuf[LIBNET_ERRBUF_SIZE];
50:
51: printf("libnet 1.1 packet shaping: TCP[raw]\n");
52:
53: /*
54: * Initialize the library. Root priviledges are required.
55: */
56: l = libnet_init(
57: LIBNET_RAW4, /* injection type */
58: NULL, /* network interface */
59: errbuf); /* errbuf */
60: if (l == NULL)
61: {
62: fprintf(stderr, "libnet_init() failed: %s", errbuf);
63: exit(EXIT_FAILURE);
64: }
65:
66: src_ip = 0;
67: dst_ip = 0;
68: src_prt = 0;
69: dst_prt = 0;
70: payload = NULL;
71: payload_s = 0;
72: while((c = getopt(argc, argv, "d:s:p:")) != EOF)
73: {
74: switch (c)
75: {
76: /*
77: * We expect the input to be of the form `ip.ip.ip.ip.port`. We
78: * point cp to the last dot of the IP address/port string and
79: * then seperate them with a NULL byte. The optarg now points to
80: * just the IP address, and cp points to the port.
81: */
82: case 'd':
83: if (!(cp = strrchr(optarg, '.')))
84: {
85: usage(argv[0]);
86: }
87: *cp++ = 0;
88: dst_prt = (u_short)atoi(cp);
89: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
90: {
91: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
92: exit(EXIT_FAILURE);
93: }
94: break;
95: break;
96: case 'p':
97: payload = optarg;
98: payload_s = strlen(payload);
99: break;
100: case 's':
101: if (!(cp = strrchr(optarg, '.')))
102: {
103: usage(argv[0]);
104: }
105: *cp++ = 0;
106: src_prt = (u_short)atoi(cp);
107: if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
108: {
109: fprintf(stderr, "Bad source IP address: %s\n", optarg);
110: exit(EXIT_FAILURE);
111: }
112: break;
113: }
114: }
115: if (!src_ip || !src_prt || !dst_ip || !dst_prt)
116: {
117: usage(argv[0]);
118: exit(EXIT_FAILURE);
119: }
120:
121: t = libnet_build_tcp(
122: src_prt, /* source port */
123: dst_prt, /* destination port */
124: 0x01010101, /* sequence number */
125: 0x02020202, /* acknowledgement num */
126: TH_SYN, /* control flags */
127: 32767, /* window size */
128: 0, /* checksum */
129: 10, /* urgent pointer */
130: LIBNET_TCP_H + payload_s, /* TCP packet size */
131: payload, /* payload */
132: payload_s, /* payload size */
133: l, /* libnet handle */
134: 0); /* libnet id */
135: if (t == -1)
136: {
137: fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
138: goto bad;
139: }
140:
141: t = libnet_build_ipv4(
142: LIBNET_IPV4_H + LIBNET_TCP_H + payload_s, /* length */
143: 0, /* TOS */
144: 242, /* IP ID */
145: 0, /* IP Frag */
146: 64, /* TTL */
147: IPPROTO_TCP, /* protocol */
148: 0, /* checksum */
149: src_ip, /* source IP */
150: dst_ip, /* destination IP */
151: NULL, /* payload */
152: 0, /* payload size */
153: l, /* libnet handle */
154: 0); /* libnet id */
155: if (t == -1)
156: {
157: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
158: goto bad;
159: }
160:
161: /*
162: * Write it to the wire.
163: */
164:
165: c = libnet_write(l);
166: if (c == -1)
167: {
168: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
169: goto bad;
170: }
171: else
172: {
173: fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
174: }
175: libnet_destroy(l);
176: return (EXIT_SUCCESS);
177: bad:
178: libnet_destroy(l);
179: return (EXIT_FAILURE);
180: }
181:
182:
183: void
184: usage(char *name)
185: {
186: fprintf(stderr,
187: "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
188: " [-p payload]\n",
189: name);
190: }
191:
192: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>