Annotation of embedaddon/libnet/sample/tring_tcp1.c, revision 1.1.1.1
1.1 misho 1: /*
2: * libnet 1.1
3: * Build a TCP packet to ride on Token Ring
4: *
5: * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: *
29: */
30:
31: #if (HAVE_CONFIG_H)
32: #include "../include/config.h"
33: #endif
34: #include "./libnet_test.h"
35:
36: int
37: main(int argc, char *argv[])
38: {
39: int c;
40: u_char *cp;
41: libnet_t *l;
42: libnet_ptag_t t;
43: char *payload;
44: u_short payload_s;
45: u_long src_ip, dst_ip;
46: u_short src_prt, dst_prt;
47: char errbuf[LIBNET_ERRBUF_SIZE];
48:
49: printf("libnet 1.1 packet shaping: TCP (over Token Ring) [link]\n");
50:
51: /*
52: * Initialize the library. Root priviledges are required.
53: *
54: * Currently hard-coded for tr0.
55: */
56: l = libnet_init(
57: LIBNET_LINK, /* injection type */
58: "tr0", /* network interface */
59: errbuf); /* error buffer */
60:
61: if (l == NULL)
62: {
63: fprintf(stderr, "libnet_init() failed: %s", errbuf);
64: exit(EXIT_FAILURE);
65: }
66:
67: src_ip = 0;
68: dst_ip = 0;
69: src_prt = 0;
70: dst_prt = 0;
71: payload = NULL;
72: payload_s = 0;
73: while ((c = getopt(argc, argv, "d:s:p:")) != EOF)
74: {
75: switch (c)
76: {
77: /*
78: * We expect the input to be of the form `ip.ip.ip.ip.port`. We
79: * point cp to the last dot of the IP address/port string and
80: * then seperate them with a NULL byte. The optarg now points to
81: * just the IP address, and cp points to the port.
82: */
83: case 'd':
84: if (!(cp = strrchr(optarg, '.')))
85: {
86: usage(argv[0]);
87: }
88: *cp++ = 0;
89: dst_prt = (u_short)atoi(cp);
90: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
91: {
92: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
93: exit(EXIT_FAILURE);
94: }
95: break;
96: case 's':
97: if (!(cp = strrchr(optarg, '.')))
98: {
99: usage(argv[0]);
100: }
101: *cp++ = 0;
102: src_prt = (u_short)atoi(cp);
103: if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
104: {
105: fprintf(stderr, "Bad source IP address: %s\n", optarg);
106: exit(EXIT_FAILURE);
107: }
108: break;
109: case 'p':
110: payload = optarg;
111: payload_s = strlen(payload);
112: break;
113: default:
114: exit(EXIT_FAILURE);
115: }
116: }
117:
118: if (!src_ip || !src_prt || !dst_ip || !dst_prt)
119: {
120: usage(argv[0]);
121: exit(EXIT_FAILURE);
122: }
123:
124:
125: t = libnet_build_tcp(
126: src_prt, /* source port */
127: dst_prt, /* destination port */
128: 0x01010101, /* sequence number */
129: 0x02020202, /* acknowledgement num */
130: TH_SYN, /* control flags */
131: 32767, /* window size */
132: 0, /* checksum */
133: 0, /* urgent pointer */
134: LIBNET_TCP_H + payload_s, /* TCP packet size */
135: payload, /* payload */
136: payload_s, /* payload size */
137: l, /* libnet handle */
138: 0); /* libnet id */
139: if (t == -1)
140: {
141: fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
142: goto bad;
143: }
144:
145: t = libnet_build_ipv4(
146: LIBNET_IPV4_H + LIBNET_TCP_H + payload_s, /* length */
147: 0, /* TOS */
148: 242, /* IP ID */
149: 0, /* IP Frag */
150: 64, /* TTL */
151: IPPROTO_TCP, /* protocol */
152: 0, /* checksum */
153: src_ip, /* source IP */
154: dst_ip, /* destination IP */
155: NULL, /* payload */
156: 0, /* payload size */
157: l, /* libnet handle */
158: 0); /* libnet id */
159: if (t == -1)
160: {
161: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
162: goto bad;
163: }
164:
165: t = libnet_build_token_ring(
166: LIBNET_TOKEN_RING_FRAME,
167: LIBNET_TOKEN_RING_LLC_FRAME, /* LLC - Normal buffer */
168: tr_dst, /* token ring destination */
169: tr_src, /* token ring source */
170: LIBNET_SAP_SNAP, /* DSAP -> SNAP encap */
171: LIBNET_SAP_SNAP, /* SSAP -> SNAP encap */
172: 0x03, /* Unnumbered info/frame */
173: org_code, /* Organization Code */
174: TOKEN_RING_TYPE_IP, /* protocol type */
175: NULL, /* payload */
176: 0, /* payload size */
177: l, /* libnet handle */
178: 0); /* libnet id */
179: if (t == -1)
180: {
181: fprintf(stderr, "Can't build token ring header: %s\n", libnet_geterror(l));
182: goto bad;
183: }
184:
185: /*
186: * Write it to the wire.
187: */
188: c = libnet_write(l);
189: if (c == -1)
190: {
191: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
192: goto bad;
193: }
194: else
195: {
196: fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
197: }
198: libnet_destroy(l);
199: return (EXIT_SUCCESS);
200: bad:
201: libnet_destroy(l);
202: return (EXIT_FAILURE);
203: }
204:
205: void
206: usage(char *name)
207: {
208: fprintf(stderr,
209: "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
210: " [-p payload]\n",
211: name);
212: }
213:
214: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>