OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / hping / main.c
1 /* 
2  * $smu-mark$ 
3  * $name: main.c$ 
4  * $author: Salvatore Sanfilippo <antirez@invece.org>$ 
5  * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$ 
6  * $license: This software is under GPL version 2 of license$ 
7  * $date: Fri Nov  5 11:55:48 MET 1999$ 
8  * $rev: 8$ 
9  */ 
10
11 /*
12  * hping official page at http://www.kyuzz.org/antirez
13  * Covered by GPL version 2, Read the COPYING file for more information
14  */
15
16 /* $Id: main.c,v 1.4 2004/06/18 09:53:11 antirez Exp $ */
17
18 #include <unistd.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <signal.h>
26 #include <time.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <pcap.h>
30
31 #include "hping2.h"
32
33 /* globals */
34 unsigned int
35         tcp_th_flags = 0,
36         linkhdr_size,                           /* physical layer header size */
37         ip_tos = 0,
38         set_seqnum = FALSE,
39         tcp_seqnum = FALSE,
40         set_ack,
41         h_if_mtu,
42         virtual_mtu     = DEFAULT_VIRTUAL_MTU,
43         ip_frag_offset  = 0,
44         signlen,
45         lsr_length = 0,
46         ssr_length = 0,
47         tcp_ack;
48
49
50 unsigned short int
51         data_size = 0;
52
53 float
54         rtt_min = 0,
55         rtt_max = 0,
56         rtt_avg = 0;
57
58 int
59         sockpacket,
60         sockraw,
61         sent_pkt = 0,
62         recv_pkt = 0,
63         out_of_sequence_pkt = 0,
64         sending_wait = DEFAULT_SENDINGWAIT,     /* see DEFAULT_SENDINGWAIT */
65         opt_rawipmode   = FALSE,
66         opt_icmpmode    = FALSE,
67         opt_udpmode     = FALSE,
68         opt_scanmode    = FALSE,
69         opt_listenmode  = FALSE,
70         opt_waitinusec  = FALSE,
71         opt_numeric     = FALSE,
72         opt_gethost     = TRUE,
73         opt_quiet       = FALSE,
74         opt_relid       = FALSE,
75         opt_fragment    = FALSE,
76         opt_df          = FALSE,
77         opt_mf          = FALSE,
78         opt_debug       = FALSE,
79         opt_verbose     = FALSE,
80         opt_winid_order = FALSE,
81         opt_keepstill   = FALSE,
82         opt_datafromfile= FALSE,
83         opt_hexdump     = FALSE,
84         opt_contdump    = FALSE,
85         opt_sign        = FALSE,
86         opt_safe        = FALSE,
87         opt_end         = FALSE,
88         opt_traceroute  = FALSE,
89         opt_seqnum      = FALSE,
90         opt_incdport    = FALSE,
91         opt_force_incdport = FALSE,
92         opt_icmptype    = DEFAULT_ICMP_TYPE,
93         opt_icmpcode    = DEFAULT_ICMP_CODE,
94         opt_rroute      = FALSE,
95         opt_tcpexitcode = FALSE,
96         opt_badcksum    = FALSE,
97         opt_tr_keep_ttl = FALSE,
98         opt_tcp_timestamp = FALSE,
99         opt_tr_stop     = FALSE,
100         opt_tr_no_rtt   = FALSE,
101         opt_rand_dest   = FALSE,
102         opt_rand_source = FALSE,
103         opt_lsrr        = FALSE,
104         opt_ssrr        = FALSE,
105         opt_cplt_rte    = FALSE,
106         opt_beep        = FALSE,
107         opt_flood       = FALSE,
108         tcp_exitcode    = 0,
109         src_ttl         = DEFAULT_TTL,
110         src_id          = -1, /* random */
111         base_dst_port   = DEFAULT_DPORT,
112         dst_port        = DEFAULT_DPORT,
113         src_port,
114         sequence        = 0,
115         initsport       = DEFAULT_INITSPORT,
116         src_winsize     = DEFAULT_SRCWINSIZE,
117         src_thoff       = (TCPHDR_SIZE >> 2),
118         count           = DEFAULT_COUNT,
119         ctrlzbind       = DEFAULT_BIND,
120         delaytable_index= 0,
121         eof_reached     = FALSE,
122         icmp_ip_version = DEFAULT_ICMP_IP_VERSION,
123         icmp_ip_ihl     = DEFAULT_ICMP_IP_IHL,
124         icmp_ip_tos     = DEFAULT_ICMP_IP_TOS,
125         icmp_ip_tot_len = DEFAULT_ICMP_IP_TOT_LEN,
126         icmp_ip_id      = DEFAULT_ICMP_IP_ID,
127         icmp_ip_protocol= DEFAULT_ICMP_IP_PROTOCOL,
128         icmp_ip_srcport = DEFAULT_DPORT,
129         icmp_ip_dstport = DEFAULT_DPORT,
130         opt_force_icmp  = FALSE,
131         icmp_cksum      = DEFAULT_ICMP_CKSUM,
132         raw_ip_protocol = DEFAULT_RAW_IP_PROTOCOL;
133
134 char
135         datafilename    [1024],
136         targetname      [1024],
137         targetstraddr   [1024],
138         ifname          [1024] = {'\0'},
139         ifstraddr       [1024],
140         spoofaddr       [1024],
141         icmp_ip_srcip   [1024],
142         icmp_ip_dstip   [1024],
143         icmp_gwip       [1024],
144         sign            [1024],
145         rsign           [1024], /* reverse sign (hping -> gniph) */
146         ip_opt          [40],
147         *opt_scanports = "";
148
149 unsigned char
150         lsr             [255] = {0},
151         ssr             [255] = {0};
152
153 unsigned
154         ip_optlen       = 0;
155
156 struct sockaddr_in
157         icmp_ip_src,
158         icmp_ip_dst,
159         icmp_gw,
160         local,
161         remote;
162
163 struct itimerval usec_delay;
164 volatile struct delaytable_element delaytable[TABLESIZE];
165
166 struct hcmphdr *hcmphdr_p; /* global pointer used by send_hcmp to transfer
167                               hcmp headers to data_handler */
168
169 pcap_t *pcapfp;
170 char errbuf[PCAP_ERRBUF_SIZE];
171 struct pcap_pkthdr hdr;
172
173 /* main */
174 int main(int argc, char **argv)
175 {
176         char setflags[1024] = {'\0'};
177         int c, hdr_size;
178
179         /* Check for the scripting mode */
180         if (argc == 1 || (argc > 1 && !strcmp(argv[1], "exec"))) {
181 #ifdef USE_TCL
182                 if (argc != 1) {
183                         argv++;
184                         argc--;
185                 }
186                 hping_script(argc, argv);
187                 exit(0); /* unreached */
188 #else
189                 fprintf(stderr, "Sorry, this hping binary was compiled "
190                                 "without TCL scripting support\n");
191                 exit(1);
192 #endif
193         }
194
195         if (parse_options(argc, argv) == -1) {
196                 printf("hping2: missing host argument\n"
197                         "Try `hping2 --help' for more information.\n");
198                 exit(1);
199         }
200
201         /* reverse sign */
202         if (opt_sign || opt_listenmode) {
203                 char *src = sign+strlen(sign)-1; /* last char before '\0' */
204                 char *dst = rsign;
205
206                 while(src>=sign)
207                         *dst++ = *src--;
208                 *dst = '\0';
209                 if (opt_debug)
210                         printf("DEBUG: reverse sign: %s\n", rsign);
211         }
212
213         /* get target address before interface processing */
214         if ((!opt_listenmode && !opt_safe) && !opt_rand_dest)
215                 resolve((struct sockaddr*)&remote, targetname);
216
217         if (opt_rand_dest) {
218                 strlcpy(targetstraddr, targetname, sizeof(targetstraddr));
219         } else {
220                 strlcpy(targetstraddr, inet_ntoa(remote.sin_addr),
221                         sizeof(targetstraddr));
222         }
223
224         /* get interface's name and address */
225         if ( get_if_name() == -1 ) {
226                 printf("[main] no such device\n");
227                 exit(1);
228         }
229
230         if (opt_verbose || opt_debug) {
231                 printf("using %s, addr: %s, MTU: %d\n",
232                         ifname, ifstraddr, h_if_mtu);
233         }
234
235         /* open raw socket */
236         sockraw = open_sockraw();
237         if (sockraw == -1) {
238                 printf("[main] can't open raw socket\n");
239                 exit(1);
240         }
241
242         /* set SO_BROADCAST option */
243         socket_broadcast(sockraw);
244         /* set SO_IPHDRINCL option */
245         socket_iphdrincl(sockraw);
246
247         /* open sock packet or libpcap socket */
248         if (open_pcap() == -1) {
249                 printf("[main] open_pcap failed\n");
250                 exit(1);
251         }
252
253         /* get physical layer header size */
254         if ( get_linkhdr_size(ifname) == -1 ) {
255                 printf("[main] physical layer header size unknown\n");
256                 exit(1);
257         }
258
259         if (spoofaddr[0] == '\0')
260                 resolve((struct sockaddr*)&local, ifstraddr);
261         else
262                 resolve((struct sockaddr*)&local, spoofaddr);
263
264         if (icmp_ip_srcip[0] == '\0')
265                 resolve((struct sockaddr*)&icmp_ip_src, "1.2.3.4");
266         else
267                 resolve((struct sockaddr*)&icmp_ip_src, icmp_ip_srcip);
268
269         if (icmp_ip_dstip[0] == '\0')
270                 resolve((struct sockaddr*)&icmp_ip_dst, "5.6.7.8");
271         else
272                 resolve((struct sockaddr*)&icmp_ip_dst, icmp_ip_dstip);
273
274         if (icmp_gwip[0] == '\0')
275                 resolve((struct sockaddr*)&icmp_gw, "0.0.0.0");
276         else
277                 resolve((struct sockaddr*)&icmp_gw, icmp_gwip);
278
279         srand(time(NULL));
280
281         /* set initial source port */
282         if (initsport == -1)
283                 initsport = src_port = 1024 + (rand() % 2000);
284         else
285                 src_port = initsport;
286
287         for (c = 0; c < TABLESIZE; c++)
288                 delaytable[c].seq = -1;
289
290         /* use SIGALRM to send packets like ping do */
291         Signal(SIGALRM, send_packet);
292
293         /* binding */
294         if (ctrlzbind != BIND_NONE) Signal(SIGTSTP, inc_destparm);
295         Signal(SIGINT, print_statistics);
296         Signal(SIGTERM, print_statistics);
297
298         /* if we are in listemode enter in listenmain() else  */
299         /* print HPING... bla bla bla and enter in wait_packet() */
300         if (opt_listenmode) {
301                 fprintf(stderr, "hping2 listen mode\n");
302
303                 /* memory protection */
304                 if (memlockall() == -1) {
305                         perror("[main] memlockall()");
306                         fprintf(stderr, "Warning: can't disable memory paging!\n");
307                 } else if (opt_verbose || opt_debug) {
308                         printf("Memory paging disabled\n");
309                 }
310                 listenmain();
311                 /* UNREACHED */
312         }
313
314         /* Scan mode */
315         if (opt_scanmode) {
316                 fprintf(stderr, "Scanning %s (%s), port %s\n",
317                                 targetname, targetstraddr, opt_scanports);
318                 scanmain();
319                 /* UNREACHED */
320         }
321
322         if (opt_rawipmode) {
323                 strcat(setflags, "raw IP mode");
324                 hdr_size = IPHDR_SIZE;
325         } else if (opt_icmpmode) {
326                 strcat(setflags, "icmp mode");
327                 hdr_size = IPHDR_SIZE + ICMPHDR_SIZE;
328         } else if (opt_udpmode) {
329                 strcat(setflags, "udp mode");
330                 hdr_size = IPHDR_SIZE + UDPHDR_SIZE;
331         } else {
332                 if (tcp_th_flags & TH_RST)  strcat(setflags, "R");
333                 if (tcp_th_flags & TH_SYN)  strcat(setflags, "S");
334                 if (tcp_th_flags & TH_ACK)  strcat(setflags, "A");
335                 if (tcp_th_flags & TH_FIN)  strcat(setflags, "F");
336                 if (tcp_th_flags & TH_PUSH) strcat(setflags, "P");
337                 if (tcp_th_flags & TH_URG)  strcat(setflags, "U");
338                 if (tcp_th_flags & TH_X)    strcat(setflags, "X");
339                 if (tcp_th_flags & TH_Y)    strcat(setflags, "Y");
340                 if (setflags[0] == '\0')    strcat(setflags, "NO FLAGS are");
341                 hdr_size = IPHDR_SIZE + TCPHDR_SIZE;
342         }
343         
344         printf("HPING %s (%s %s): %s set, %d headers + %d data bytes\n",
345                 targetname,
346                 ifname,
347                 targetstraddr,
348                 setflags,
349                 hdr_size,
350                 data_size);
351
352         /* memory protection */
353         if (opt_datafromfile || opt_sign) {
354                 if (memlockall() == -1) {
355                         perror("[main] memlockall()");
356                         fprintf(stderr,
357                                 "Warning: can't disable memory paging!\n");
358                 } else if (opt_verbose || opt_debug) {
359                         printf("Memory paging disabled\n");
360                 }
361         }
362
363         /* start packet sending */
364         kill(getpid(), SIGALRM);
365
366         /* flood mode? */
367         if (opt_flood) {
368                 fprintf(stderr,
369                         "hping in flood mode, no replies will be shown\n");
370                 while (1) {
371                         send_packet(0);
372                 }
373         }
374
375         /* main loop */
376         while(1)
377                 wait_packet();
378
379         return 0;
380 }