OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / test / ICMP / silvertongue.c
1 /*
2  *  $Id: silvertongue.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  silvertongue.c - ICMP_REDIRECT Packet assembly tester
6  *
7  *  Written by Nicholas Brawn with some modifications by Mike D. Schiffman.
8  *  Copyright (c) 1999, 2000 Nicholas Brawn <nick@feralmonkey.org>
9  *  Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
10  *  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34
35 #if (HAVE_CONFIG_H)
36 #include "../../include/config.h"
37 #endif
38 #include "../libnet_test.h"
39
40 int
41 main(int argc, char **argv)
42 {
43     int sockfd, c;
44     u_char *buf;
45     u_long src, dst, gateway;
46
47     if (argc < 4)
48     {
49         fprintf(stderr,
50                 "usage: %s <old_router> <target> <new_gateway>\n",
51                 argv[0]);
52         exit(EXIT_FAILURE);
53     }
54
55     if (!(src = libnet_name_resolve(argv[1], 1)))
56     {
57         perror("Error resolving source host");
58         exit(EXIT_FAILURE);
59     }
60
61     if (!(dst = libnet_name_resolve(argv[2], 1)))
62     {
63         perror("Error resolving destination host");
64         exit(EXIT_FAILURE);
65     }
66
67     if (!(gateway = libnet_name_resolve(argv[3], 1)))
68     {
69         perror("Error resolving gateway host");
70         exit(EXIT_FAILURE);
71     }
72
73     if (libnet_init_packet(IP_MAXPACKET, &buf) == -1)
74     {
75         perror("Couldn't allocate memory for header");
76         exit(EXIT_FAILURE);
77     }
78
79     if ((sockfd = libnet_open_raw_sock(IPPROTO_RAW)) == -1)
80     {
81         perror("Couldn't open raw socket");
82         exit(EXIT_FAILURE);
83     }
84
85     libnet_build_ip(LIBNET_ICMP_REDIRECT_H + LIBNET_IP_H,
86                 IPTOS_LOWDELAY | IPTOS_THROUGHPUT,
87                 242,
88                 0,
89                 48,
90                 IPPROTO_ICMP,
91                 src,
92                 dst,
93                 NULL,
94                 0,
95                 buf);
96
97     libnet_build_icmp_redirect(
98                 ICMP_REDIRECT,
99                 ICMP_UNREACH_HOST,
100                 gateway,
101                 0,                                  /* just an ip header */
102                 IPTOS_LOWDELAY | IPTOS_THROUGHPUT,  /* IP tos */
103                 424,                                /* IP ID */
104                 0,                                  /* Frag stuff */
105                 64,                                 /* TTL */
106                 IPPROTO_ICMP,                       /* Transport protocol */
107                 dst,                                /* Source IP */
108                 src,                                /* Destination IP */
109                 NULL,                               /* pointer to payload */
110                 0,                                  /* size of payload */
111                 buf + LIBNET_IP_H);                 /* packet header memory */
112
113
114
115     libnet_do_checksum(buf, IPPROTO_ICMP, LIBNET_ICMP_REDIRECT_H +
116                 LIBNET_IP_H);
117  
118     c = libnet_write_ip(sockfd, buf, LIBNET_ICMP_REDIRECT_H + 2 *
119                 LIBNET_IP_H);
120     if (c != LIBNET_ICMP_REDIRECT_H + 2 * LIBNET_IP_H)
121     {
122         fprintf(stderr, "Error writing to socket, only wrote %d bytes\n", c);
123         exit(EXIT_FAILURE);
124     }
125     printf("Completed, wrote %d bytes\n", c);
126     libnet_destroy_packet(&buf);
127
128     exit(EXIT_SUCCESS);
129 }