trafgen: man: Add help for VLAN header function
[netsniff-ng.git] / staging / mausezahn.c
blob5051bcb750cf3ebf50d7070bda8d407e39272e7e
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Mausezahn, a fast versatile traffic generator
4 * Copyright 2008, 2009, 2010 Herbert Haas.
5 * Subject to the GPL, version 2.
6 */
8 #define _GNU_SOURCE
9 #include <libnet.h>
10 #include <pcap/pcap.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <limits.h>
17 #include <sys/time.h>
18 #include <time.h>
19 #include <signal.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <sys/ioctl.h>
24 #include <netinet/in.h>
25 #include <stdarg.h>
27 #include "mz.h"
28 #include "cli.h"
29 #include "mops.h"
30 #include "config.h"
31 #include "llist.h"
32 #include "die.h"
34 int verbose_level = 0;
36 static const char *short_options = "46hqvVSxra:A:b:B:c:d:E:f:F:l:p:P:t:T:M:Q:X:";
38 static void signal_handler(int number)
40 clean_up(number);
43 void clean_up(int sig)
45 int i;
46 struct arp_table_struct *cur, *next;
48 if (!quiet) fprintf(stderr, "\nMausezahn cleans up...\n");
50 if (fp != NULL) {
51 verbose_l1(" close files (1) ...\n");
53 fflush(fp);
54 fclose(fp);
57 if (fp2!=NULL) {
58 if (verbose) fprintf(stderr, " close files (2) ...\n");
59 (void) fflush(fp2);
60 (void) fclose(fp2);
63 // interactive mode?
64 if (mz_port) {
65 if (verbose) fprintf(stderr, " clear mops list...\n");
66 mops_cleanup(mp_head);
67 if (verbose) fprintf(stderr, " clear automops list...\n");
68 automops_cleanup(amp_head);
69 if (verbose) fprintf(stderr, " clear packet sequences...\n");
70 mz_ll_delete_list(packet_sequences);
73 for (i=0; i<device_list_entries; i++) {
74 if (device_list[i].p_arp!=NULL) {
75 pcap_close(device_list[i].p_arp);
76 fprintf(stderr, " stopped ARP process for device %s\n", device_list[i].dev);
78 if (device_list[i].arprx_thread!=0) {
79 pthread_cancel(device_list[i].arprx_thread);
80 if (verbose)
81 fprintf(stderr, " (ARP thread for device %s done)\n", device_list[i].dev);
84 if (device_list[i].arp_table!=NULL) {
85 cur=device_list[i].arp_table;
86 while (cur!=NULL) {
87 next = cur->next;
88 if (cur!=NULL) free(cur);
89 cur=next;
93 // close packet sockets
94 if (device_list[i].ps>=0) {
95 close(device_list[i].ps);
100 if (verbose) fprintf(stderr, "finished.\n");
101 exit(sig);
105 static void help(void)
107 printf("\nmausezahn %s, a fast versatile traffic generator\n", VERSION_STRING);
108 puts("http://www.netsniff-ng.org\n\n"
109 "Usage: mausezahn [options] [interface] <keyword>|<arg-string>|<hex-string>\n"
110 "Options:\n"
111 " -x <port> Interactive mode with telnet CLI, default port: 25542\n"
112 " -l <ip> Listen address to bind to when in interactive mode, default: 0.0.0.0\n"
113 " -4 IPv4 mode (default)\n"
114 " -6 IPv6 mode\n"
115 " -c <count> Send packet count times, default:1, infinite:0\n"
116 " -d <delay> Apply delay between transmissions. The delay value can be\n"
117 " specified in usec (default, no additional unit needed), or in\n"
118 " msec (e.g. 100m or 100msec), or in seconds (e.g. 100s or 100sec)\n"
119 " -r Multiplies the specified delay with a random value\n"
120 " -p <length> Pad the raw frame to specified length (using random bytes)\n"
121 " -a <srcmac|keyword> Use specified source mac address, no matter what has\n"
122 " been specified with other arguments; keywords see below,\n"
123 " Default is own interface\n"
124 " -b <dstmac|keyword> Same with destination mac address; keywords:\n"
125 " rand Use a random MAC address\n"
126 " bc Use a broadcast MAC address\n"
127 " own Use own interface MAC address (default for source MAC)\n"
128 " stp Use IEEE 802.1d STP multicast address\n"
129 " cisco Use Cisco multicast address as used for CDP, VTP, or PVST+\n"
130 " -A <srcip> Use specified source IP address (default is own interface IP)\n"
131 " -B <dstip|dnsname> Send packet to specified destination IP or domain name\n"
132 " -P <ascii payload> Use the specified ASCII payload\n"
133 " -f <filename> Read the ASCII payload from a file\n"
134 " -F <filename> Read the hexadecimal payload from a file\n"
135 " -Q <[CoS:]vlan> Specify 802.1Q VLAN tag and optional Class of Service, you can\n"
136 " specify multiple 802.1Q VLAN tags (QinQ...) by separating them\n"
137 " via a comma or a period (e.g. '5:10,20,2:30')\n"
138 " -t <packet-type|help> Specify packet type for autobuild (you don't need to care for\n"
139 " encapsulations in lower layers, most packet types allow/require\n"
140 " additional packet-specific arguments in an <arg-string>;\n"
141 " Currently supported types: arp, bpdu, cdp, ip, icmp, udp, tcp,\n"
142 " dns, rtp, syslog, lldp and more;\n"
143 " For context-help use 'help' as <arg-string>!\n"
144 " -T <packet-type> Specify packet type for server mode, currently only rtp is supported;\n"
145 " Enter -T help or -T rtp help for further information\n"
146 " -M <MPLS-label> Insert a MPLS label, enter '-M help' for a syntax description\n"
147 " -V|VV|... Verbose and more verbose mode\n"
148 " -q Quiet mode, even omit 'important' standard short messages\n"
149 " -S Simulation mode: DOES NOT put anything on the wire, this is\n"
150 " typically combined with one of the verbose modes (v or V)\n"
151 " -v Show version\n"
152 " -h Print this help\n\n"
153 "Examples:\n"
154 " mausezahn -x 99\n"
155 " mausezahn -c 0 -d 2s -t bpdu conf\n"
156 " mausezahn -t cdp change -c 0\n"
157 " mausezahn -t syslog sev=3 -P \"You have been mausezahned.\" -A 10.1.1.109 -B 192.168.7.7\n"
158 " mausezahn eth0 -A rand -B 1.1.1.1 -c 0 -t tcp \"dp=1-1023, flags=syn\"\n\n"
159 "Note:\n"
160 " This tool is targeted for network developers! You should\n"
161 " be aware of what you are doing and what these options above\n"
162 " mean! Only use this tool in an isolated LAN that you own!\n\n"
163 "Please report bugs to <bugs@netsniff-ng.org>\n"
164 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
165 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
166 "Swiss federal institute of technology (ETH Zurich)\n"
167 "License: GNU GPL version 2.0\n"
168 "This is free software: you are free to change and redistribute it.\n"
169 "There is NO WARRANTY, to the extent permitted by law.\n");
170 die();
173 static void version(void)
175 printf("\nmausezahn %s, Git id: %s\n", VERSION_LONG, GITVERSION);
176 puts("a fast versatile traffic generator\n"
177 "http://www.netsniff-ng.org\n\n"
178 "Please report bugs to <bugs@netsniff-ng.org>\n"
179 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
180 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
181 "Swiss federal institute of technology (ETH Zurich)\n"
182 "License: GNU GPL version 2.0\n"
183 "This is free software: you are free to change and redistribute it.\n"
184 "There is NO WARRANTY, to the extent permitted by law.\n");
185 die();
188 int reset(void)
190 int i;
191 time_t t;
193 // Determine platform type sizes:
194 MZ_SIZE_LONG_INT = sizeof(long int);
196 mz_default_config_path[0] = 0x00;
197 mz_default_log_path[0] = 0x00;
199 // Reset globals:
200 quiet = 0;
201 ipv6_mode = 0;
202 verbose = 0;
203 simulate = 0;
204 filename[0] = '\0';
205 path[0] = '\0';
206 gind=0;
207 gind_max = TIME_COUNT;
208 fp = NULL;
209 fp2 = NULL;
210 mz_port = 0;
211 mz_rand = 0;
212 char mz_listen_addr[16] = "0.0.0.0";
213 mp_head = NULL;
215 for (i=0;i<TIME_COUNT_MAX;i++) jitter[i] = 0;
217 time0_flag = 0; // If set then time0 has valid data
218 sqnr0_flag = 0; // If set then sqnr_last and sqnr_next has valid data
219 rtp_log = 0;
220 mz_ssrc[0]=0; mz_ssrc[1]=0; mz_ssrc[2]=0; mz_ssrc[3]=0;
222 // Reset mgmt parameters of TX:
223 tx.packet_mode = 1; // assume we don't care about L2
224 tx.count = 1;
225 tx.delay = DEFAULT_DELAY;
226 tx.arg_string[0] = '\0';
228 // Reset Ethernet parameters of TX:
229 tx.eth_params_already_set = 0;
230 for (i=0; i<6; i++) tx.eth_dst[i] = 0xff;
231 for (i=0; i<6; i++) tx.eth_src[i] = 0; // TODO: Get own MAC !!!
232 tx.eth_dst_txt[0] = '\0';
233 tx.eth_src_txt[0] = '\0';
234 tx.eth_dst_rand = 0;
235 tx.eth_src_rand = 0;
237 tx.eth_type = 0x800;
238 tx.eth_len = 0;
239 tx.eth_payload[0] = '\0';
240 tx.eth_payload_s = 0;
241 tx.padding = 0;
243 // Reset CDP parameters for TX:
244 tx.cdp_sum = 0;
245 tx.cdp_version = 0;
246 tx.cdp_ttl = 0;
247 tx.cdp_payload[0] = '\0';
248 tx.cdp_payload_s = 0;
249 tx.cdp_tlv_id[0] = '\0';
250 tx.cdp_tlv_id_len = 0;
252 // Reset 802.1Q parameters of TX:
253 tx.dot1Q=0;
254 tx.dot1Q_txt[0] = '\0';
256 // ASCII Payload:
257 tx.ascii = 0; // 1 if specified
258 tx.ascii_payload[0]= '\0';
260 // HEX Payload:
261 tx.hex_payload_s = 0;
263 // Reset MPLS parameters of TX:
264 tx.mpls = 0;
265 tx.mpls_txt[0] = '\0';
266 tx.mpls_label = 0;
267 tx.mpls_exp = 0;
268 tx.mpls_bos = 1;
269 tx.mpls_ttl = 255;
270 tx.mpls_verbose_string[0] = '\0';
272 // Reset IP parameters of TX:
273 tx.ip_src_txt[0] = '\0';
274 tx.ip_src_rand = 0;
275 tx.ip_dst_txt[0] = '\0';
276 tx.ip_src_isrange = 0;
277 tx.ip_src_start = 0;
278 tx.ip_src_stop = 0;
280 tx.ip_dst_start = 0;
281 tx.ip_dst_stop = 0;
282 tx.ip_dst_isrange = 0;
284 tx.ip_ttl = 0;
285 tx.ip_len = 0;
286 tx.ip_payload[0]= '\0';
287 tx.ip_payload_s = 0;
288 tx.ip_option[0]= '\0';
289 tx.ip_option_s = 0;
291 // Reset ICMP parameters:
292 tx.icmp_type=0;
293 tx.icmp_code=0;
294 tx.icmp_chksum=0; // 0=autofill
295 tx.icmp_ident=0x42;
296 tx.icmp_sqnr=0x1;
297 tx.icmp_payload_s=0;
299 // Reset general L4 parameters:
300 tx.sp = 0;
301 tx.dp = 0;
302 tx.sp_start = 0;
303 tx.sp_stop = 0;
304 tx.dp_start = 0;
305 tx.dp_stop = 0;
306 tx.sp_isrange = 0;
307 tx.dp_isrange = 0;
309 // Reset UDP parameters of TX:
311 tx.udp_len = 0; // If set to zero then create_udp_packet will calculate it
312 tx.udp_sum = 0;
313 tx.udp_payload[0] = '\0';
314 tx.udp_payload_s = 0;
316 // Reset TCP parameters of TX:
318 tx.tcp_seq = 42;
319 tx.tcp_seq_stop = 42;
320 tx.tcp_seq_delta = 0; // also used as 'isrange' meaning
321 tx.tcp_ack = 42;
322 tx.tcp_control = 0;
323 tx.tcp_win = 10000;
324 tx.tcp_sum = 0;
325 tx.tcp_urg = 0;
326 tx.tcp_len = 20; // Least size (TCP header only)
327 tx.tcp_payload[0] = '\0';
328 tx.tcp_payload_s = 0;
330 // Reset RTP parameters of TX:
331 tx.rtp_sqnr = 0;
332 tx.rtp_stmp = 0;
334 // Initialize random generator
335 time(&t);
336 srand((unsigned int)t);
338 // Reset device_list
339 for (i=0; i<MZ_MAX_DEVICES; i++) {
340 device_list[i].arprx_thread = 0;
341 device_list[i].p_arp = NULL;
342 device_list[i].arp_table = NULL;
343 device_list[i].ps=-1;
344 device_list[i].cli=0;
345 device_list[i].mgmt_only=0;
348 return 0;
351 static void print_packet_types(void)
353 fprintf(stderr, "\n"
354 MAUSEZAHN_VERSION
355 "\n"
356 "| The following packet types are currently implemented:\n"
357 "|\n"
358 "| arp ... sends ARP packets\n"
359 "| bpdu ... sends BPDU packets (STP or PVST+)\n"
360 "| cdp ... sends CDP messages\n"
361 "| ip ... sends IPv4 packets\n"
362 "| udp ... sends UDP datagrams\n"
363 "| tcp ... sends TCP segments\n"
364 "| icmp ... sends ICMP messages\n"
365 "| igmp ... sends IGMP messages\n"
366 "| dns ... sends DNS messages\n"
367 "| rtp ... sends RTP datagrams\n"
368 "| syslog ... sends Syslog messages\n"
369 "|\n"
370 "| Of course you can build any other packet type 'manually' using the direct layer 2 mode.\n"
371 "| FYI: The interactive mode supports additional protocols. (Try mz -x <port>)\n"
372 "\n");
374 die();
377 // Purpose: Properly handle arguments and configure global structs (tx)
378 int getopts (int argc, char *argv[])
380 int i, c, rargs, RX=0, count_set=0, delay_set=0;
381 unsigned int time_factor;
382 char *packet_type=NULL, *mops_type=NULL;
383 char *dum;
384 unsigned char *dum1, *dum2;
385 bool do_help = false;
387 libnet_t *l;
388 char err_buf[LIBNET_ERRBUF_SIZE];
389 struct libnet_ether_addr *mymac;
391 FILE *afp;
392 char hexpld[MAX_PAYLOAD_SIZE*2];
393 int hexpld_specified=0;
395 opterr = 1; // let getopt print error message if necessary
398 while ((c = getopt(argc, argv, short_options)) != -1)
399 switch (c) {
400 case '4':
401 tx.eth_type = 0x0800;
402 ipv6_mode=0;
403 break;
404 case '6':
405 tx.eth_type = 0x86dd;
406 ipv6_mode=1;
407 break;
408 case 'h':
409 help();
410 break;
411 case 'q':
412 quiet=1;
413 break;
414 case 'v':
415 version();
416 break;
417 case 'V':
418 verbose++;
419 break;
420 case 'S':
421 simulate=1;
422 break;
423 case 'x':
424 mz_port = MZ_DEFAULT_PORT;
425 break;
426 case 'l':
427 strncpy (mz_listen_addr, optarg, sizeof(mz_listen_addr));
428 break;
429 case 'a':
430 strncpy (tx.eth_src_txt, optarg, 32);
431 tx.packet_mode = 0;
432 break;
433 case 'A':
434 strncpy (tx.ip_src_txt, optarg, sizeof(tx.ip_src_txt));
435 break;
436 case 'b':
437 strncpy (tx.eth_dst_txt, optarg, 32);
438 tx.packet_mode = 0;
439 break;
440 case 'B':
441 strncpy (tx.ip_dst_txt, optarg, sizeof(tx.ip_dst_txt));
442 break;
443 case 'c':
444 errno=0;
445 tx.count = strtol(optarg, (char **)NULL, 10);
446 if ((errno == ERANGE && (tx.count == LONG_MAX || tx.count == LONG_MIN))
447 || (errno != 0 && tx.count == 0)) {
448 perror("strtol");
449 return (-1);
451 if (tx.count<0) tx.count=1; //TODO: Allow count=0 which means infinity (need to update all send_functions)
452 count_set=1;
453 break;
454 case 'd':
455 errno=0;
456 // determine whether seconds or msecs are used
457 // default is usec!!!
458 time_factor=1;
459 if (exists(optarg,"s") || exists(optarg,"sec")) time_factor=1000000;
460 if (exists(optarg,"m") || exists(optarg,"msec")) time_factor=1000;
461 dum = strtok(optarg,"ms");
462 tx.delay = strtol(dum, (char **)NULL, 10) * time_factor;
463 if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN))
464 || (errno != 0 && tx.delay == 0)) {
465 perror("strtol");
466 return (-1);
468 if (tx.delay<0) tx.delay=0; // no delay
469 delay_set=1;
470 break;
471 case 'p':
472 errno=0;
473 tx.padding = strtol(optarg, (char **)NULL, 10);
474 if ((errno == ERANGE && (tx.padding == LONG_MAX || tx.padding == LONG_MIN))
475 || (errno != 0 && tx.padding == 0)) {
476 perror("strtol");
477 return (-1);
479 if (tx.padding>10000) {
480 fprintf(stderr, " Warning: Padding must not exceed 10000!\n");
481 return -1;
483 break;
484 case 't':
485 packet_type = optarg; // analyzed below
486 if (strcmp(packet_type,"help") == 0)
487 print_packet_types();
488 break;
489 case 'X':
490 mops_type = optarg; // MOPS TRANSITION STRATEGY -- analyzed below
491 break;
492 case 'T':
493 packet_type = optarg;
494 RX = 1;
495 break;
496 case 'r':
497 mz_rand = 1;
498 break;
499 case 'M':
500 if (strncmp(optarg,"help",4)==0) {
501 (void) get_mpls_params("help ");
503 else {
504 strncpy (tx.mpls_txt, optarg, 128);
505 tx.eth_type = ETHERTYPE_MPLS;
506 tx.packet_mode = 0;
507 tx.mpls=1;
509 break;
510 case 'P': // ASCII payload
511 strncpy((char*)tx.ascii_payload, optarg, MAX_PAYLOAD_SIZE);
512 tx.ascii = 1;
513 break;
514 case 'f': // ASCII payload in FILE
515 afp = fopen(optarg, "r");
516 if (fgets((char*)tx.ascii_payload, MAX_PAYLOAD_SIZE, afp) == NULL)
517 fprintf(stderr, " mz/getopts: File empty?\n");
518 fclose(afp);
519 tx.ascii = 1;
520 break;
521 case 'F': // HEX payload in FILE
522 afp = fopen(optarg, "r");
523 i=0;
524 while ( (hexpld[i]=fgetc(afp))!=EOF ) {
525 if (isspace(hexpld[i])) {
526 hexpld[i]=':';
528 i++;
530 hexpld[i]='\0';
531 fclose(afp);
532 hexpld_specified=1;
533 break;
534 case 'Q': // VLAN TAG
535 if (strncmp(optarg,"help",4)==0) {
536 print_dot1Q_help(); // ugly but most simple and safe solution
538 else {
539 strncpy (tx.dot1Q_txt, optarg, 32);
540 tx.dot1Q=1;
541 // determine number of VLAN tags
542 for (i=0; i<strlen(tx.dot1Q_txt); i++) {
543 if (tx.dot1Q_txt[i]==',') tx.dot1Q++;
545 tx.packet_mode = 0;
547 break;
548 case '?':
549 if ((optopt == 'a') || (optopt == 'b') || (optopt = 'c') ||
550 (optopt == 'd') || (optopt == 'f') || (optopt = 'p') ||
551 (optopt == 't') || (optopt == 'm'))
552 fprintf (stderr, " mz/getopts: Option -%c requires an argument.\n", optopt);
553 else if (isprint (optopt))
554 fprintf (stderr, " mz/getopts: Unknown option -%c'.\n", optopt);
555 else
556 fprintf (stderr, " mz/getopts: Unknown option character \\x%x'.\n", optopt);
557 return 1;
558 default:
559 fprintf (stderr," mz/getopts: Could not handle arguments properly!\n");
560 return 1;
563 // ********************************************
564 // Handle additional arguments
565 // ********************************************
567 // Greeting text
568 if (verbose) {
569 fprintf(stderr,"\n"
570 MAUSEZAHN_VERSION
571 "\n"
572 "Use at your own risk and responsibility!\n"
573 "-- Verbose mode --\n"
574 "\n");
577 if (argc<2) {
578 help();
581 if ((rargs=argc-optind)>2) { // number of remaining arguments
582 fprintf(stderr," mz/getopts: Too many arguments!\n");
583 return -1;
587 // There can be 0-2 additional arguments
588 switch (rargs) {
589 case 0:
590 if (lookupdev()) { // no device found
591 if (verbose) fprintf(stderr, " mz: no active interfaces found!\n");
592 strcpy(tx.device, "lo");
594 break;
595 case 1: // arg_string OR device given => find out!
596 if ( (strncmp(argv[optind],"eth",3)==0)
597 || (strncmp(argv[optind],"ath",3)==0)
598 || ((strncmp(argv[optind],"lo",2)==0)&&(strncmp(argv[optind],"log",3)!=0))
599 || (strncmp(argv[optind],"vmnet",5)==0)
600 || (strncmp(argv[optind],"wifi",4)==0) ) {
601 // device has been specified!
602 strncpy (tx.device, argv[optind], 16);
604 else { /// arg_string given => no device has been specified -- let's find one!
605 strncpy (tx.arg_string, argv[optind], MAX_PAYLOAD_SIZE);
606 do_help = !!getarg(tx.arg_string,"help", NULL);
607 if (!do_help) {
608 if (lookupdev()) {
609 /* no device found */
610 if (verbose)
611 fprintf(stderr, " mz: no active interfaces found!\n");
612 strcpy(tx.device, "lo");
614 if (verbose)
615 fprintf(stderr," mz: device not given, will use %s\n",tx.device);
618 break;
619 case 2: // both device and arg_string given
620 strncpy (tx.device, argv[optind], 16);
621 strncpy (tx.arg_string, argv[optind+1], MAX_PAYLOAD_SIZE);
622 break;
623 default:
624 fprintf(stderr," mz/getopts: Unknown argument problem!\n");
625 return 1;
628 if (hexpld_specified) {
629 strcat(tx.arg_string, ",p=");
630 strcat(tx.arg_string, hexpld);
634 //////////////////////////////////////////////////////////////////////////
636 // Initialize MAC and IP Addresses.
638 // - tx.eth_src = own interface MAC
639 // - tx.ip_src = own interface IP or user specified
640 // - tx.ip_dst = 255.255.255.255 or user specified (can be a range)
641 // - tx.ip_src_rand ... is set if needed.
644 // Get own device MAC address:
645 // Don't open context if only a help text is requested
646 if (!do_help && getarg(tx.arg_string,"help", NULL) !=1) {
647 l = libnet_init (LIBNET_LINK_ADV, tx.device, err_buf );
648 if (l == NULL) {
649 fprintf(stderr, " mz/getopts: libnet_init() failed (%s)", err_buf);
650 return -1;
652 mymac = libnet_get_hwaddr(l);
653 for (i=0; i<6; i++) {
654 tx.eth_src[i] = mymac->ether_addr_octet[i];
655 tx.eth_mac_own[i] = mymac->ether_addr_octet[i];
658 // Set source IP address:
659 if (strlen(tx.ip_src_txt)) { // option -A has been specified
660 if (mz_strcmp(tx.ip_src_txt, "bcast", 2)==0) {
661 tx.ip_src = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
662 } else if (strcmp(tx.ip_src_txt, "rand") == 0) {
663 tx.ip_src_rand = 1;
664 tx.ip_src_h = (u_int32_t) ( ((float) rand()/RAND_MAX)*0xE0000000); //this is 224.0.0.0
666 else if (get_ip_range_src(tx.ip_src_txt)) { // returns 1 when no range has been specified
667 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
668 if (ipv6_mode)
669 tx.ip6_src = libnet_name2addr6 (l, tx.ip_src_txt, LIBNET_RESOLVE);
670 else
671 tx.ip_src = libnet_name2addr4 (l, tx.ip_src_txt, LIBNET_RESOLVE);
674 else { // no source IP specified: by default use own IP address
675 if (ipv6_mode) {
676 tx.ip6_src = libnet_get_ipaddr6(l);
677 if (strncmp((char*)&tx.ip6_src,(char*)&in6addr_error,sizeof(in6addr_error))==0)
678 printf("Failed to set source IPv6 address: %s", l->err_buf);
680 else
681 tx.ip_src = libnet_get_ipaddr4(l);
684 // Set destination IP address:
685 if (strlen(tx.ip_dst_txt)) { // option -B has been specified
686 if (mz_strcmp(tx.ip_dst_txt, "rand", 2)==0) {
687 fprintf(stderr, "Option -B does not support random destination IP addresses currently.\n");
688 return 1;
691 if (mz_strcmp(tx.ip_dst_txt, "bcast", 2)==0) {
692 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
693 } else if (get_ip_range_dst(tx.ip_dst_txt)) { // returns 1 when no range has been specified
694 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
695 if (ipv6_mode)
696 tx.ip6_dst = libnet_name2addr6 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
697 else
698 tx.ip_dst = libnet_name2addr4 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
701 else { // no destination IP specified: by default use broadcast
702 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
705 // Initialize tx.ip_src_h and tx.ip_dst_h which are used by 'print_frame_details()'
706 // in verbose mode. See 'modifications.c'.
708 if (tx.ip_src_rand) { // ip_src_h already given, convert to ip_src
709 dum1 = (unsigned char*) &tx.ip_src_h;
710 dum2 = (unsigned char*) &tx.ip_src;
712 else { // ip_src already given, convert to ip_src_h
713 dum1 = (unsigned char*) &tx.ip_src;
714 dum2 = (unsigned char*) &tx.ip_src_h;
717 *dum2 = *(dum1+3);
718 dum2++;
719 *dum2 = *(dum1+2);
720 dum2++;
721 *dum2 = *(dum1+1);
722 dum2++;
723 *dum2 = *dum1;
725 dum1 = (unsigned char*) &tx.ip_dst;
726 dum2 = (unsigned char*) &tx.ip_dst_h;
728 *dum2 = *(dum1+3);
729 dum2++;
730 *dum2 = *(dum1+2);
731 dum2++;
732 *dum2 = *(dum1+1);
733 dum2++;
734 *dum2 = *dum1;
736 libnet_destroy(l);
740 // END OF ADDRESS INITIALIZATION
742 //////////////////////////////////////////////////////////////////////////
745 ////// retrieve interface parameters ///////
747 for (i=0; i<device_list_entries; i++) {
748 get_dev_params(device_list[i].dev);
752 //////////////////////////////////////////////////////////////////////////
754 // Mausezahn CLI desired?
755 if (mz_port) {
756 // has port number been specified?
757 if (strlen(tx.arg_string)) {
758 mz_port = (int) str2int (tx.arg_string);
761 mz_cli_init();
762 cli();
765 //////////////////////////////////////////////////////////////////////////
767 // Mode decision
769 // Consider -t and -m option (used exclusively)
770 // -t => special packet types, stateless
772 // If -t not present then evaluate arg_string which must
773 // contain a byte-string in hexadecimal notation.
777 // ***** NEW: MOPS TRANSITION STRATEGY *****
778 if (mops_type != NULL) {
780 if (mz_strcmp(mops_type,"lldp",4)==0) {
781 mops_direct(tx.device, MOPS_LLDP, tx.arg_string);
786 if (packet_type == NULL) { // raw hex string given
787 mode = BYTE_STREAM;
789 else if (strcmp(packet_type,"arp")==0) {
790 mode = ARP;
792 else if (strcmp(packet_type,"bpdu")==0) {
793 mode = BPDU;
795 else if (strcmp(packet_type,"ip")==0) {
796 mode = IP;
798 else if (strcmp(packet_type,"udp")==0) {
799 mode = UDP;
801 else if (strcmp(packet_type,"icmp")==0) {
802 mode = ICMP;
804 else if (strcmp(packet_type,"icmp6")==0) {
805 mode = ICMP6;
807 else if (strcmp(packet_type,"tcp")==0) {
808 mode = TCP;
810 else if (strcmp(packet_type,"dns")==0) {
811 mode = DNS;
813 else if (strcmp(packet_type,"cdp")==0) {
814 mode = CDP;
816 else if (strcmp(packet_type,"syslog")==0) {
817 mode = SYSLOG;
819 else if (strcmp(packet_type, "igmp") == 0) {
820 mode = IGMP;
822 else if (strcmp(packet_type,"lldp")==0) {
823 mode = LLDP;
824 tx.packet_mode=0; // create whole frame by ourself
826 else if (strcmp(packet_type,"rtp")==0) {
827 if (RX) {
828 mode = RX_RTP;
830 else {
831 mode = RTP;
832 if (!count_set) tx.count = 0;
833 if (!delay_set) tx.delay = 20000; // 20 msec inter-packet delay for RTP
836 else {
837 fprintf(stderr, " mz: you must specify a valid packet type!\n");
841 //////////////////////////////////////////////////////////////////////////
843 // TODO: Implement macro support
844 // Check macro types here
846 return 0;
849 int main(int argc, char **argv)
851 // These handles are only used when creating L3 and above packets.
852 libnet_t *l; // the context
853 libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers
854 double cpu_time_used;
856 reset();
858 if ( getopts(argc, argv) )
860 (void) fprintf(stderr, " Invalid command line parameters!\n");
861 help();
864 // Check whether hires timers are supported or not:
865 (void) check_timer();
867 signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT
869 switch (mode)
871 case BYTE_STREAM:
872 send_eth();
873 break;
875 case ARP:
876 (void) send_arp();
877 break;
879 case BPDU:
880 (void) send_bpdu();
881 break;
883 case CDP:
884 (void) send_cdp();
885 break;
887 case IP: // From now on a new much more modular method is used:
888 l = get_link_context();
889 t3 = create_ip_packet(l); // t3 can be used for later header changes
890 if (!quiet) complexity();
891 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
892 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
893 else
894 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
895 break;
897 case ICMP:
898 tx.ip_proto = 1;
899 l = get_link_context();
900 t4 = create_icmp_packet(l); // t4 can be used for later header changes
901 t3 = create_ip_packet(l); // t3 can be used for later header changes
902 if (!quiet) complexity();
903 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
904 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
905 else
906 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
907 break;
909 case ICMP6:
910 tx.ip_proto = 58;
911 l = get_link_context();
912 t4 = create_icmp6_packet(l); // t4 can be used for later header changes
913 t3 = create_ip_packet(l); // t3 can be used for later header changes
914 if (ipv6_mode)
915 update_ISUM(l, t4);
916 if (!quiet) complexity();
917 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
918 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
919 else
920 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
921 break;
923 case UDP:
924 tx.ip_proto = 17;
925 l = get_link_context();
926 t4 = create_udp_packet(l); // t4 can be used for later header changes
927 t3 = create_ip_packet(l); // t3 can be used for later header changes
928 if (ipv6_mode)
929 update_USUM(l, t4);
930 if (!quiet) complexity();
931 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
932 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
933 else
934 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
935 break;
937 case TCP:
938 tx.ip_proto = 6;
939 l = get_link_context();
940 t4 = create_tcp_packet(l); // t4 can be used for later header changes
941 t3 = create_ip_packet(l); // t3 can be used for later header changes
942 if (ipv6_mode)
943 update_TSUM(l, t4);
944 if (!quiet) complexity();
945 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
946 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
947 else
948 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
949 break;
951 case IGMP:
952 tx.ip_proto = 2;
953 l = get_link_context();
954 t4 = create_igmp_packet(l);
955 /* t3 can be used for later header changes */
956 t3 = create_ip_packet(l);
957 if (!quiet)
958 complexity();
960 /* Ethernet manipulation features does NOT use ARP to determine eth_dst
961 * */
962 if (tx.packet_mode == 0)
963 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
964 else
965 send_frame(l, t3, t4); // NOTE: send_frame also destroys context finaly
966 break;
968 case DNS:
969 tx.ip_proto = 17;
970 l = get_link_context();
971 (void) create_dns_packet();
972 t4 = create_udp_packet(l); // t4 can be used for later header changes
973 t3 = create_ip_packet(l); // t3 can be used for later header changes
974 if (!quiet) complexity();
975 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
976 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
977 else
978 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
979 break;
981 case RTP:
982 tx.ip_proto = 17;
983 l = get_link_context();
984 if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay);
985 (void) create_rtp_packet();
986 t4 = create_udp_packet(l); // t4 can be used for later header changes
987 t3 = create_ip_packet(l); // t3 can be used for later header changes
988 if (!quiet) complexity();
989 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
990 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
991 else
992 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
993 break;
995 case RX_RTP: // Receive RTP packets
996 rcv_rtp_init();
997 rcv_rtp();
998 break;
1000 case SYSLOG:
1001 tx.ip_proto = 17;
1002 l = get_link_context();
1003 (void) create_syslog_packet();
1004 t4 = create_udp_packet(l); // t4 can be used for later header changes
1005 t3 = create_ip_packet(l); // t3 can be used for later header changes
1006 if (!quiet) complexity();
1008 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1009 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
1010 else
1011 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
1012 break;
1014 case LLDP: // start with a new concept here
1015 //l = get_link_context();
1016 //(void) create_lldp_packet();
1017 // // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
1018 fprintf(stderr, "LLDP is currently only supported via the interactive mode\n");
1019 exit(1);
1020 break;
1023 default:
1024 (void) fprintf(stderr," mz/main: unknown mode! Stop.\n");
1025 return (1);
1028 if (!quiet)
1030 mz_stop = clock();
1031 cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC;
1032 if (cpu_time_used > 0)
1034 total_d /= cpu_time_used;
1035 fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d);
1037 else
1039 fprintf(stderr, "\n");
1043 return(0);