OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / test / ICMP / icmp_echo.c
1 /*
2  *  $Id: icmp_echo.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  icmp_echo.c - ICMP_ECHO Packet assembly tester
6  *
7  *  Copyright (c) 1998, 1999, 2000 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 sock, n, c, p_num;
42     struct libnet_arena arena, *arena_p;
43     u_char *packets[10];
44     u_long src_ip, dst_ip;
45     
46     printf("ICMP_ECHO / Arena allocator test\n");
47
48     src_ip = 0;
49     dst_ip = 0;
50     while((c = getopt(argc, argv, "d:s:")) != EOF)
51     {
52         switch (c)
53         {
54             case 'd':
55                 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
56                 {
57                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
58                     exit(1);
59                 }
60                 break;
61             case 's':
62                 if (!(src_ip = libnet_name_resolve(optarg, 1)))
63                 {
64                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
65                     exit(1);
66                 }
67                 break;
68         }
69     }
70     if (!src_ip || !dst_ip)
71     {
72         usage(argv[0]);
73         exit(EXIT_FAILURE);
74     }
75
76     arena_p = &arena;
77     p_num = 10;
78     if (libnet_init_packet_arena(&arena_p, p_num, LIBNET_ICMP_ECHO_H +
79                 LIBNET_IP_H) == -1)
80     {
81         fprintf(stderr, "libnet_init_packet_arena failed\n");
82         exit(EXIT_FAILURE);
83     }
84     else
85     {
86         printf("Allocated an arena of %ld bytes..\n",
87             LIBNET_GET_ARENA_SIZE(arena));
88     }
89
90     /*
91      *  Open our raw IP socket and set IP_HDRINCL.
92      */
93     sock = libnet_open_raw_sock(IPPROTO_RAW);
94     if (sock == -1)
95     {
96         perror("No socket");
97         exit(EXIT_FAILURE);
98     }
99     
100     for (n = 0; n < p_num; n++)
101     {
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);
106         if (!packets[n])
107         {
108             fprintf(stderr, "Arena is empty\n");
109             continue;
110         }
111
112         /*
113          *  Build the IP header (shown exploded for commenting).
114          */
115         libnet_build_ip(LIBNET_ICMP_ECHO_H,     /* Size of the payload */
116                 IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* IP tos */
117                 242,                            /* IP ID */
118                 0,                              /* Frag stuff */
119                 48,                             /* TTL */
120                 IPPROTO_ICMP,                   /* Transport protocol */
121                 src_ip,                         /* Source IP */
122                 dst_ip,                         /* Destination IP */
123                 NULL,                           /* Pointer to payload (none) */
124                 0,
125                 packets[n]);                    /* Packet header memory */
126
127         /*
128          *  Build the ICMP header.
129          */
130         libnet_build_icmp_echo(ICMP_ECHO,       /* type */
131                 0,                              /* code */
132                 242,                            /* id */
133                 1,                              /* seq */
134                 NULL,                           /* pointer to payload */
135                 0,                              /* size of payload */
136                 packets[n] + LIBNET_IP_H);      /* packet header memory */
137
138         if (libnet_do_checksum(packets[n], IPPROTO_ICMP, LIBNET_ICMP_ECHO_H)
139                     == -1)
140         {
141             fprintf(stderr, "Can't do checksum!\n");
142         }
143
144         /*
145          *  Write the packet to the network.
146          */
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)
149         {
150             fprintf(stderr, "write_ip\n");
151         }
152         printf("Completed %d of %d, wrote %d bytes\n", n + 1, p_num, c);
153     }
154
155     libnet_destroy_packet_arena(&arena_p);
156     /* Blah. */
157     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
158 }
159
160
161 void
162 usage(u_char *name)
163 {
164     fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name);
165 }
166
167 /* EOF */