1: /*
2: * $Id: icmp_echo_cq.c,v 1.1.1.1 2012/02/21 22:14:23 misho Exp $
3: *
4: * libnet 1.1
5: * Build ICMP_ECHO packets using the context queue interface.
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: #ifdef __WIN32__
38: #include "../include/win32/getopt.h"
39: #endif
40:
41: void usage(char *);
42:
43:
44: int
45: main(int argc, char **argv)
46: {
47: libnet_t *l = NULL;
48: u_long src_ip = 0, dst_ip = 0;
49: u_long count = 10;
50: int i, c;
51: libnet_ptag_t t;
52: char *payload = NULL;
53: u_short payload_s = 0;
54:
55: char *device = NULL;
56: char *pDst = NULL, *pSrc = NULL;
57: char errbuf[LIBNET_ERRBUF_SIZE];
58: char label[LIBNET_LABEL_SIZE];
59:
60: printf("libnet 1.1 packet shaping: ICMP[RAW using context queue]\n");
61:
62: while((c = getopt(argc, argv, "d:s:i:c:p:")) != EOF)
63: {
64: switch (c)
65: {
66: case 'd':
67: pDst = optarg;
68: break;
69: case 's':
70: pSrc = optarg;
71: break;
72: case 'i':
73: device = optarg;
74: break;
75: case 'c':
76: count = strtoul(optarg, 0, 10);
77: break;
78: case 'p':
79: payload = optarg;
80: payload_s = strlen(payload);
81: break;
82: }
83: }
84:
85: if (!pSrc || !pDst)
86: {
87: usage(argv[0]);
88: exit(EXIT_FAILURE);
89: }
90:
91: /*
92: * Fill the context queue with "count" packets, each with their own
93: * context.
94: */
95: for (i = 0; i < count; i++)
96: {
97: l = libnet_init(
98: LIBNET_RAW4, /* injection type */
99: device, /* network interface */
100: errbuf); /* errbuf */
101:
102: if (l == NULL)
103: {
104: /* we should run through the queue and free any stragglers */
105: fprintf(stderr, "libnet_init() failed: %s", errbuf);
106: exit(EXIT_FAILURE);
107: }
108: /*
109: * Since we need a libnet context for address resolution it is
110: * necessary to put this inside the loop.
111: */
112: if (!dst_ip && (dst_ip = libnet_name2addr4(l, pDst,
113: LIBNET_RESOLVE)) == -1)
114: {
115: fprintf(stderr, "Bad destination IP address: %s\n", pDst);
116: exit(1);
117: }
118: if (!src_ip && (src_ip = libnet_name2addr4(l, pSrc,
119: LIBNET_RESOLVE)) == -1)
120: {
121: fprintf(stderr, "Bad source IP address: %s\n", pSrc);
122: exit(1);
123: }
124:
125: t = libnet_build_icmpv4_echo(
126: ICMP_ECHO, /* type */
127: 0, /* code */
128: 0, /* checksum */
129: 0x42, /* id */
130: 0x42, /* sequence number */
131: NULL, /* payload */
132: 0, /* payload size */
133: l, /* libnet handle */
134: 0);
135: if (t == -1)
136: {
137: fprintf(stderr, "Can't build ICMP header: %s\n",
138: libnet_geterror(l));
139: goto bad;
140: }
141:
142: t = libnet_build_ipv4(
143: LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H + payload_s, /* length */
144: 0, /* TOS */
145: 0x42, /* IP ID */
146: 0, /* IP Frag */
147: 64, /* TTL */
148: IPPROTO_ICMP, /* protocol */
149: 0, /* checksum */
150: src_ip, /* source IP */
151: dst_ip, /* destination IP */
152: payload, /* payload */
153: payload_s, /* payload size */
154: l, /* libnet handle */
155: 0);
156: if (t == -1)
157: {
158: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
159: goto bad;
160: }
161:
162: /* and finally, put it in the context queue */
163: snprintf(label, sizeof(label)-1, "echo %d", i);
164: if (libnet_cq_add(l, label) == -1)
165: {
166: fprintf(stderr, "add error: %s\n", libnet_geterror(l));
167: goto bad;
168: }
169: }
170:
171: for_each_context_in_cq(l)
172: {
173: c = libnet_write(l);
174: if (c == -1)
175: {
176: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
177: goto bad;
178: }
179: else
180: {
181: fprintf(stderr, "Wrote %d byte ICMP packet from context \"%s\"; "
182: "check the wire.\n", c, libnet_cq_getlabel(l));
183: }
184: }
185:
186: libnet_cq_destroy();
187: return (EXIT_SUCCESS);
188: bad:
189: libnet_cq_destroy();
190: libnet_destroy(l);
191: return (EXIT_FAILURE);
192: }
193:
194: void
195: usage(char *name)
196: {
197: fprintf(stderr, "usage: %s -s source_ip -d destination_ip"
198: " [-i iface] [-c count = 10]\n ", name);
199: }
200:
201: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>