2 * $Id: icmp_echo.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * icmp_echo.c - ICMP_ECHO Packet assembly tester
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)
41 int sock, n, c, p_num;
42 struct libnet_arena arena, *arena_p;
44 u_long src_ip, dst_ip;
46 printf("ICMP_ECHO / Arena allocator test\n");
50 while((c = getopt(argc, argv, "d:s:")) != EOF)
55 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
57 fprintf(stderr, "Bad destination IP address: %s\n", optarg);
62 if (!(src_ip = libnet_name_resolve(optarg, 1)))
64 fprintf(stderr, "Bad source IP address: %s\n", optarg);
70 if (!src_ip || !dst_ip)
78 if (libnet_init_packet_arena(&arena_p, p_num, LIBNET_ICMP_ECHO_H +
81 fprintf(stderr, "libnet_init_packet_arena failed\n");
86 printf("Allocated an arena of %ld bytes..\n",
87 LIBNET_GET_ARENA_SIZE(arena));
91 * Open our raw IP socket and set IP_HDRINCL.
93 sock = libnet_open_raw_sock(IPPROTO_RAW);
100 for (n = 0; n < p_num; n++)
102 printf("%ld bytes remaining in arena\n",
103 LIBNET_GET_ARENA_REMAINING_BYTES(arena));
104 packets[n] = libnet_next_packet_from_arena(&arena_p,
105 LIBNET_ICMP_ECHO_H + LIBNET_IP_H);
108 fprintf(stderr, "Arena is empty\n");
113 * Build the IP header (shown exploded for commenting).
115 libnet_build_ip(LIBNET_ICMP_ECHO_H, /* Size of the payload */
116 IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* IP tos */
120 IPPROTO_ICMP, /* Transport protocol */
121 src_ip, /* Source IP */
122 dst_ip, /* Destination IP */
123 NULL, /* Pointer to payload (none) */
125 packets[n]); /* Packet header memory */
128 * Build the ICMP header.
130 libnet_build_icmp_echo(ICMP_ECHO, /* type */
134 NULL, /* pointer to payload */
135 0, /* size of payload */
136 packets[n] + LIBNET_IP_H); /* packet header memory */
138 if (libnet_do_checksum(packets[n], IPPROTO_ICMP, LIBNET_ICMP_ECHO_H)
141 fprintf(stderr, "Can't do checksum!\n");
145 * Write the packet to the network.
147 c = libnet_write_ip(sock, packets[n], LIBNET_ICMP_ECHO_H + LIBNET_IP_H);
148 if (c < LIBNET_ICMP_ECHO_H + LIBNET_IP_H)
150 fprintf(stderr, "write_ip\n");
152 printf("Completed %d of %d, wrote %d bytes\n", n + 1, p_num, c);
155 libnet_destroy_packet_arena(&arena_p);
157 return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
164 fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name);