trafgen: remove unused cmdline param
[netsniff-ng.git] / src / mausezahn.c
blobb8dddd4568dcb9472525ec7358533c4defdebc64
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Mausezahn, a fast versatile traffic generator
4 * Copyright 2008, 2009, 2010 Herbert Haas.
5 * Copyright 2012 Daniel Borkmann.
6 * Subject to the GPL, version 2.
7 */
9 #define _GNU_SOURCE
10 #include <libnet.h>
11 #include <pcap/pcap.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <sys/time.h>
19 #include <time.h>
20 #include <signal.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
26 #include <stdarg.h>
28 #include "mz.h"
29 #include "cli.h"
30 #include "mops.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: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 " -4 IPv4 mode (default)\n"
113 " -6 IPv6 mode\n"
114 " -c <count> Send packet count times, default:1, infinite:0\n"
115 " -d <delay> Apply delay between transmissions. The delay value can be\n"
116 " specified in usec (default, no additional unit needed), or in\n"
117 " msec (e.g. 100m or 100msec), or in seconds (e.g. 100s or 100sec)\n"
118 " -r Multiplies the specified delay with a random value\n"
119 " -p <length> Pad the raw frame to specified length (using random bytes)\n"
120 " -a <srcmac|keyword> Use specified source mac address, no matter what has\n"
121 " been specified with other arguments; keywords see below,\n"
122 " Default is own interface\n"
123 " -b <dstmac|keyword> Same with destination mac address; keywords:\n"
124 " rand Use a random MAC address\n"
125 " bc Use a broadcast MAC address\n"
126 " own Use own interface MAC address (default for source MAC)\n"
127 " stp Use IEEE 802.1d STP multicast address\n"
128 " cisco Use Cisco multicast address as used for CDP, VTP, or PVST+\n"
129 " -A <srcip> Use specified source IP address (default is own interface IP)\n"
130 " -B <dstip|dnsname> Send packet to specified destination IP or domain name\n"
131 " -P <ascii payload> Use the specified ASCII payload\n"
132 " -f <filename> Read the ASCII payload from a file\n"
133 " -F <filename> Read the hexadecimal payload from a file\n"
134 " -Q <[CoS:]vlan> Specify 802.1Q VLAN tag and optional Class of Service, you can\n"
135 " specify multiple 802.1Q VLAN tags (QinQ...) by separating them\n"
136 " via a comma or a period (e.g. '5:10,20,2:30')\n"
137 " -t <packet-type> Specify packet type for autobuild (you don't need to care for\n"
138 " encapsulations in lower layers, most packet types allow/require\n"
139 " additional packet-specific arguments in an <arg-string>;\n"
140 " Currently supported types: arp, bpdu, cdp, ip, icmp, udp, tcp,\n"
141 " dns, rtp, syslog, lldp and more;\n"
142 " For context-help use 'help' as <arg-string>!\n"
143 " -T <packet-type> Specify packet type for server mode, currently only rtp is supported;\n"
144 " Enter -T help or -T rtp help for further information\n"
145 " -M <MPLS-label> Insert a MPLS label, enter '-M help' for a syntax description\n"
146 " -V|VV|... Verbose and more verbose mode\n"
147 " -q Quiet mode, even omit 'important' standard short messages\n"
148 " -S Simulation mode: DOES NOT put anything on the wire, this is\n"
149 " typically combined with one of the verbose modes (v or V)\n"
150 " -v Show version\n"
151 " -h Print this help\n\n"
152 "Examples:\n"
153 " mausezahn -x 99\n"
154 " mausezahn -c 0 -d 2s -t bpdu conf\n"
155 " mausezahn -t cdp change -c 0\n"
156 " mausezahn -t syslog sev=3 -P \"You have been mausezahned.\" -A 10.1.1.109 -B 192.168.7.7\n"
157 " mausezahn eth0 -A rand -B 1.1.1.1 -c 0 -t tcp \"dp=1-1023, flags=syn\"\n\n"
158 "Note:\n"
159 " This tool is targeted for network developers! You should\n"
160 " be aware of what you are doing and what these options above\n"
161 " mean! Only use this tool in an isolated LAN that you own!\n\n"
162 "Please report bugs to <bugs@netsniff-ng.org>\n"
163 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
164 "Copyright (C) 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
165 "Swiss federal institute of technology (ETH Zurich)\n"
166 "License: GNU GPL version 2.0\n"
167 "This is free software: you are free to change and redistribute it.\n"
168 "There is NO WARRANTY, to the extent permitted by law.\n");
169 die();
172 static void version(void)
174 printf("\nmausezahn %s, a fast versatile traffic generator\n", VERSION_STRING);
175 puts("http://www.netsniff-ng.org\n\n"
176 "Please report bugs to <bugs@netsniff-ng.org>\n"
177 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
178 "Copyright (C) 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
179 "Swiss federal institute of technology (ETH Zurich)\n"
180 "License: GNU GPL version 2.0\n"
181 "This is free software: you are free to change and redistribute it.\n"
182 "There is NO WARRANTY, to the extent permitted by law.\n");
183 die();
186 int reset()
188 int i;
189 time_t t;
191 // Determine platform type sizes:
192 MZ_SIZE_LONG_INT = sizeof(long int);
194 mz_default_config_path[0] = 0x00;
195 mz_default_log_path[0] = 0x00;
197 // Reset globals:
198 quiet = 0;
199 ipv6_mode = 0;
200 verbose = 0;
201 simulate = 0;
202 filename[0] = '\0';
203 path[0] = '\0';
204 gind=0;
205 gind_max = TIME_COUNT;
206 fp = NULL;
207 fp2 = NULL;
208 mz_port = 0;
209 mz_rand = 0;
210 mp_head = NULL;
212 for (i=0;i<TIME_COUNT_MAX;i++) jitter[i] = 0;
214 time0_flag = 0; // If set then time0 has valid data
215 sqnr0_flag = 0; // If set then sqnr_last and sqnr_next has valid data
216 rtp_log = 0;
217 mz_ssrc[0]=0; mz_ssrc[1]=0; mz_ssrc[2]=0; mz_ssrc[3]=0;
219 // Reset mgmt parameters of TX:
220 tx.packet_mode = 1; // assume we don't care about L2
221 tx.count = 1;
222 tx.delay = DEFAULT_DELAY;
223 tx.arg_string[0] = '\0';
225 // Reset Ethernet parameters of TX:
226 tx.eth_params_already_set = 0;
227 for (i=0; i<6; i++) tx.eth_dst[i] = 0xff;
228 for (i=0; i<6; i++) tx.eth_src[i] = 0; // TODO: Get own MAC !!!
229 tx.eth_dst_txt[0] = '\0';
230 tx.eth_src_txt[0] = '\0';
231 tx.eth_dst_rand = 0;
232 tx.eth_src_rand = 0;
234 tx.eth_type = 0x800;
235 tx.eth_len = 0;
236 tx.eth_payload[0] = '\0';
237 tx.eth_payload_s = 0;
238 tx.padding = 0;
240 // Reset CDP parameters for TX:
241 tx.cdp_sum = 0;
242 tx.cdp_version = 0;
243 tx.cdp_ttl = 0;
244 tx.cdp_payload[0] = '\0';
245 tx.cdp_payload_s = 0;
246 tx.cdp_tlv_id[0] = '\0';
247 tx.cdp_tlv_id_len = 0;
249 // Reset 802.1Q parameters of TX:
250 tx.dot1Q=0;
251 tx.dot1Q_txt[0] = '\0';
253 // ASCII Payload:
254 tx.ascii = 0; // 1 if specified
255 tx.ascii_payload[0]= '\0';
257 // HEX Payload:
258 tx.hex_payload_s = 0;
260 // Reset MPLS parameters of TX:
261 tx.mpls = 0;
262 tx.mpls_txt[0] = '\0';
263 tx.mpls_label = 0;
264 tx.mpls_exp = 0;
265 tx.mpls_bos = 1;
266 tx.mpls_ttl = 255;
267 tx.mpls_verbose_string[0] = '\0';
269 // Reset IP parameters of TX:
270 tx.ip_src_txt[0] = '\0';
271 tx.ip_src_rand = 0;
272 tx.ip_dst_txt[0] = '\0';
273 tx.ip_src_isrange = 0;
274 tx.ip_src_start = 0;
275 tx.ip_src_stop = 0;
277 tx.ip_dst_start = 0;
278 tx.ip_dst_stop = 0;
279 tx.ip_dst_isrange = 0;
281 tx.ip_len = 0;
282 tx.ip_payload[0]= '\0';
283 tx.ip_payload_s = 0;
284 tx.ip_option[0]= '\0';
285 tx.ip_option_s = 0;
287 // Reset ICMP parameters:
288 tx.icmp_type=0;
289 tx.icmp_code=0;
290 tx.icmp_chksum=0; // 0=autofill
291 tx.icmp_ident=0x42;
292 tx.icmp_sqnr=0x1;
293 tx.icmp_payload_s=0;
295 // Reset general L4 parameters:
296 tx.sp = 0;
297 tx.dp = 0;
298 tx.sp_start = 0;
299 tx.sp_stop = 0;
300 tx.dp_start = 0;
301 tx.dp_stop = 0;
302 tx.sp_isrange = 0;
303 tx.dp_isrange = 0;
305 // Reset UDP parameters of TX:
307 tx.udp_len = 0; // If set to zero then create_udp_packet will calculate it
308 tx.udp_sum = 0;
309 tx.udp_payload[0] = '\0';
310 tx.udp_payload_s = 0;
312 // Reset TCP parameters of TX:
314 tx.tcp_seq = 42;
315 tx.tcp_seq_stop = 42;
316 tx.tcp_seq_delta = 0; // also used as 'isrange' meaning
317 tx.tcp_ack = 42;
318 tx.tcp_control = 0;
319 tx.tcp_win = 10000;
320 tx.tcp_sum = 0;
321 tx.tcp_urg = 0;
322 tx.tcp_len = 20; // Least size (TCP header only)
323 tx.tcp_payload[0] = '\0';
324 tx.tcp_payload_s = 0;
326 // Reset RTP parameters of TX:
327 tx.rtp_sqnr = 0;
328 tx.rtp_stmp = 0;
330 // Initialize random generator
331 time(&t);
332 srand((unsigned int)t);
334 // Reset device_list
335 for (i=0; i<MZ_MAX_DEVICES; i++) {
336 device_list[i].arprx_thread = 0;
337 device_list[i].p_arp = NULL;
338 device_list[i].arp_table = NULL;
339 device_list[i].ps=-1;
340 device_list[i].cli=0;
341 device_list[i].mgmt_only=0;
344 return 0;
349 // Purpose: Properly handle arguments and configure global structs (tx)
350 int getopts (int argc, char *argv[])
352 int i, c, rargs, RX=0, count_set=0, delay_set=0;
353 unsigned int time_factor;
354 char *packet_type=NULL, *mops_type=NULL;
355 char *dum;
356 unsigned char *dum1, *dum2;
358 libnet_t *l;
359 char err_buf[LIBNET_ERRBUF_SIZE];
360 struct libnet_ether_addr *mymac;
362 FILE *afp;
363 char hexpld[MAX_PAYLOAD_SIZE*2];
364 int hexpld_specified=0;
366 opterr = 1; // let getopt print error message if necessary
369 while ((c = getopt(argc, argv, short_options)) != -1)
370 switch (c) {
371 case '4':
372 tx.eth_type = 0x0800;
373 ipv6_mode=0;
374 break;
375 case '6':
376 tx.eth_type = 0x86dd;
377 ipv6_mode=1;
378 break;
379 case 'h':
380 help();
381 break;
382 case 'q':
383 quiet=1;
384 break;
385 case 'v':
386 version();
387 break;
388 case 'V':
389 verbose++;
390 break;
391 case 'S':
392 simulate=1;
393 break;
394 case 'x':
395 mz_port = MZ_DEFAULT_PORT;
396 break;
397 case 'a':
398 strncpy (tx.eth_src_txt, optarg, 32);
399 tx.packet_mode = 0;
400 break;
401 case 'A':
402 strncpy (tx.ip_src_txt, optarg, sizeof(tx.ip_src_txt));
403 break;
404 case 'b':
405 strncpy (tx.eth_dst_txt, optarg, 32);
406 tx.packet_mode = 0;
407 break;
408 case 'B':
409 strncpy (tx.ip_dst_txt, optarg, sizeof(tx.ip_dst_txt));
410 break;
411 case 'c':
412 errno=0;
413 tx.count = strtol(optarg, (char **)NULL, 10);
414 if ((errno == ERANGE && (tx.count == LONG_MAX || tx.count == LONG_MIN))
415 || (errno != 0 && tx.count == 0)) {
416 perror("strtol");
417 return (-1);
419 if (tx.count<0) tx.count=1; //TODO: Allow count=0 which means infinity (need to update all send_functions)
420 count_set=1;
421 break;
422 case 'd':
423 errno=0;
424 // determine whether seconds or msecs are used
425 // default is usec!!!
426 time_factor=1;
427 if (exists(optarg,"s") || exists(optarg,"sec")) time_factor=1000000;
428 if (exists(optarg,"m") || exists(optarg,"msec")) time_factor=1000;
429 dum = strtok(optarg,"ms");
430 tx.delay = strtol(dum, (char **)NULL, 10) * time_factor;
431 if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN))
432 || (errno != 0 && tx.delay == 0)) {
433 perror("strtol");
434 return (-1);
436 if (tx.delay<0) tx.delay=0; // no delay
437 delay_set=1;
438 break;
439 case 'p':
440 errno=0;
441 tx.padding = strtol(optarg, (char **)NULL, 10);
442 if ((errno == ERANGE && (tx.padding == LONG_MAX || tx.padding == LONG_MIN))
443 || (errno != 0 && tx.padding == 0)) {
444 perror("strtol");
445 return (-1);
447 if (tx.padding>10000) {
448 fprintf(stderr, " Warning: Padding must not exceed 10000!\n");
449 return -1;
451 break;
452 case 't':
453 packet_type = optarg; // analyzed below
454 break;
455 case 'X':
456 mops_type = optarg; // MOPS TRANSITION STRATEGY -- analyzed below
457 break;
458 case 'T':
459 packet_type = optarg;
460 RX = 1;
461 break;
462 case 'r':
463 mz_rand = 1;
464 break;
465 case 'M':
466 if (strncmp(optarg,"help",4)==0) {
467 (void) get_mpls_params("help ");
469 else {
470 strncpy (tx.mpls_txt, optarg, 128);
471 tx.eth_type = ETHERTYPE_MPLS;
472 tx.packet_mode = 0;
473 tx.mpls=1;
475 break;
476 case 'P': // ASCII payload
477 strncpy((char*)tx.ascii_payload, optarg, MAX_PAYLOAD_SIZE);
478 tx.ascii = 1;
479 break;
480 case 'f': // ASCII payload in FILE
481 afp = fopen(optarg, "r");
482 if (fgets((char*)tx.ascii_payload, MAX_PAYLOAD_SIZE, afp) == NULL)
483 fprintf(stderr, " mz/getopts: File empty?\n");
484 fclose(afp);
485 tx.ascii = 1;
486 break;
487 case 'F': // HEX payload in FILE
488 afp = fopen(optarg, "r");
489 i=0;
490 while ( (hexpld[i]=fgetc(afp))!=EOF ) {
491 if (isspace(hexpld[i])) {
492 hexpld[i]=':';
494 i++;
496 hexpld[i]='\0';
497 fclose(afp);
498 hexpld_specified=1;
499 break;
500 case 'Q': // VLAN TAG
501 if (strncmp(optarg,"help",4)==0) {
502 print_dot1Q_help(); // ugly but most simple and safe solution
504 else {
505 strncpy (tx.dot1Q_txt, optarg, 32);
506 tx.dot1Q=1;
507 // determine number of VLAN tags
508 for (i=0; i<strlen(tx.dot1Q_txt); i++) {
509 if (tx.dot1Q_txt[i]==',') tx.dot1Q++;
511 tx.packet_mode = 0;
513 break;
514 case '?':
515 if ((optopt == 'a') || (optopt == 'b') || (optopt = 'c') ||
516 (optopt == 'd') || (optopt == 'f') || (optopt = 'p') ||
517 (optopt == 't') || (optopt == 'm'))
518 fprintf (stderr, " mz/getopts: Option -%c requires an argument.\n", optopt);
519 else if (isprint (optopt))
520 fprintf (stderr, " mz/getopts: Unknown option -%c'.\n", optopt);
521 else
522 fprintf (stderr, " mz/getopts: Unknown option character \\x%x'.\n", optopt);
523 return 1;
524 default:
525 fprintf (stderr," mz/getopts: Could not handle arguments properly!\n");
526 return 1;
529 // ********************************************
530 // Handle additional arguments
531 // ********************************************
533 // Greeting text
534 if (verbose) {
535 fprintf(stderr,"\n"
536 MAUSEZAHN_VERSION
537 "\n"
538 "Use at your own risk and responsibility!\n"
539 "-- Verbose mode --\n"
540 "\n");
543 if (argc<2) {
544 help();
547 if ((rargs=argc-optind)>2) { // number of remaining arguments
548 fprintf(stderr," mz/getopts: Too many arguments!\n");
549 return -1;
553 // There can be 0-2 additional arguments
554 switch (rargs) {
555 case 0:
556 if (lookupdev()) { // no device found
557 if (verbose) fprintf(stderr, " mz: no active interfaces found!\n");
558 strcpy(tx.device, "lo");
560 if (verbose) // device found
561 fprintf(stderr," mz: device not given, will use %s\n",tx.device);
562 break;
563 case 1: // arg_string OR device given => find out!
564 if ( (strncmp(argv[optind],"eth",3)==0)
565 || (strncmp(argv[optind],"ath",3)==0)
566 || ((strncmp(argv[optind],"lo",2)==0)&&(strncmp(argv[optind],"log",3)!=0))
567 || (strncmp(argv[optind],"vmnet",5)==0)
568 || (strncmp(argv[optind],"wifi",4)==0) ) {
569 // device has been specified!
570 strncpy (tx.device, argv[optind], 16);
572 else { /// arg_string given => no device has been specified -- let's find one!
573 strncpy (tx.arg_string, argv[optind], MAX_PAYLOAD_SIZE);
574 if (lookupdev()) { // no device found
575 if (verbose) fprintf(stderr, " mz: no active interfaces found!\n");
576 strcpy(tx.device, "lo");
578 if (verbose)
579 fprintf(stderr," mz: device not given, will use %s\n",tx.device);
581 break;
582 case 2: // both device and arg_string given
583 strncpy (tx.device, argv[optind], 16);
584 strncpy (tx.arg_string, argv[optind+1], MAX_PAYLOAD_SIZE);
585 break;
586 default:
587 fprintf(stderr," mz/getopts: Unknown argument problem!\n");
588 return 1;
591 if (hexpld_specified) {
592 strcat(tx.arg_string, ",p=");
593 strcat(tx.arg_string, hexpld);
597 //////////////////////////////////////////////////////////////////////////
599 // Initialize MAC and IP Addresses.
601 // - tx.eth_src = own interface MAC
602 // - tx.ip_src = own interface IP or user specified
603 // - tx.ip_dst = 255.255.255.255 or user specified (can be a range)
604 // - tx.ip_src_rand ... is set if needed.
607 // Get own device MAC address:
608 // Don't open context if only a help text is requested
609 if (getarg(tx.arg_string,"help", NULL)!=1) {
610 l = libnet_init (LIBNET_LINK_ADV, tx.device, err_buf );
611 if (l == NULL) {
612 fprintf(stderr, " mz/getopts: libnet_init() failed (%s)", err_buf);
613 return -1;
615 mymac = libnet_get_hwaddr(l);
616 for (i=0; i<6; i++) {
617 tx.eth_src[i] = mymac->ether_addr_octet[i];
618 tx.eth_mac_own[i] = mymac->ether_addr_octet[i];
621 // Set source IP address:
622 if (strlen(tx.ip_src_txt)) { // option -A has been specified
623 if (mz_strcmp(tx.ip_src_txt, "bcast", 2)==0) {
624 tx.ip_src = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
625 } else if (strcmp(tx.ip_src_txt, "rand") == 0) {
626 tx.ip_src_rand = 1;
627 tx.ip_src_h = (u_int32_t) ( ((float) rand()/RAND_MAX)*0xE0000000); //this is 224.0.0.0
629 else if (get_ip_range_src(tx.ip_src_txt)) { // returns 1 when no range has been specified
630 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
631 if (ipv6_mode)
632 tx.ip6_src = libnet_name2addr6 (l, tx.ip_src_txt, LIBNET_RESOLVE);
633 else
634 tx.ip_src = libnet_name2addr4 (l, tx.ip_src_txt, LIBNET_RESOLVE);
637 else { // no source IP specified: by default use own IP address
638 if (ipv6_mode) {
639 tx.ip6_src = libnet_get_ipaddr6(l);
640 if (strncmp((char*)&tx.ip6_src,(char*)&in6addr_error,sizeof(in6addr_error))==0)
641 printf("Failed to set source IPv6 address: %s", l->err_buf);
643 else
644 tx.ip_src = libnet_get_ipaddr4(l);
647 // Set destination IP address:
648 if (strlen(tx.ip_dst_txt)) { // option -B has been specified
649 if (mz_strcmp(tx.ip_dst_txt, "rand", 2)==0) {
650 fprintf(stderr, "Option -B does not support random destination IP addresses currently.\n");
651 return 1;
654 if (mz_strcmp(tx.ip_dst_txt, "bcast", 2)==0) {
655 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
656 } else if (get_ip_range_dst(tx.ip_dst_txt)) { // returns 1 when no range has been specified
657 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
658 if (ipv6_mode)
659 tx.ip6_dst = libnet_name2addr6 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
660 else
661 tx.ip_dst = libnet_name2addr4 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
664 else { // no destination IP specified: by default use broadcast
665 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
668 // Initialize tx.ip_src_h and tx.ip_dst_h which are used by 'print_frame_details()'
669 // in verbose mode. See 'modifications.c'.
671 if (tx.ip_src_rand) { // ip_src_h already given, convert to ip_src
672 dum1 = (unsigned char*) &tx.ip_src_h;
673 dum2 = (unsigned char*) &tx.ip_src;
675 else { // ip_src already given, convert to ip_src_h
676 dum1 = (unsigned char*) &tx.ip_src;
677 dum2 = (unsigned char*) &tx.ip_src_h;
680 *dum2 = *(dum1+3);
681 dum2++;
682 *dum2 = *(dum1+2);
683 dum2++;
684 *dum2 = *(dum1+1);
685 dum2++;
686 *dum2 = *dum1;
688 dum1 = (unsigned char*) &tx.ip_dst;
689 dum2 = (unsigned char*) &tx.ip_dst_h;
691 *dum2 = *(dum1+3);
692 dum2++;
693 *dum2 = *(dum1+2);
694 dum2++;
695 *dum2 = *(dum1+1);
696 dum2++;
697 *dum2 = *dum1;
699 libnet_destroy(l);
703 // END OF ADDRESS INITIALIZATION
705 //////////////////////////////////////////////////////////////////////////
708 ////// retrieve interface parameters ///////
710 for (i=0; i<device_list_entries; i++) {
711 get_dev_params(device_list[i].dev);
715 //////////////////////////////////////////////////////////////////////////
717 // Mausezahn CLI desired?
718 if (mz_port) {
719 // has port number been specified?
720 if (strlen(tx.arg_string)) {
721 mz_port = (int) str2int (tx.arg_string);
724 if (!quiet) {
725 fprintf(stderr, "Mausezahn accepts incoming Telnet connections on port %i.\n", mz_port);
728 mz_cli_init();
729 cli();
732 //////////////////////////////////////////////////////////////////////////
734 // Mode decision
736 // Consider -t and -m option (used exclusively)
737 // -t => special packet types, stateless
739 // If -t not present then evaluate arg_string which must
740 // contain a byte-string in hexadecimal notation.
744 // ***** NEW: MOPS TRANSITION STRATEGY *****
745 if (mops_type != NULL) {
747 if (mz_strcmp(mops_type,"lldp",4)==0) {
748 mops_direct(tx.device, MOPS_LLDP, tx.arg_string);
753 if (packet_type == NULL) { // raw hex string given
754 mode = BYTE_STREAM;
756 else if (strcmp(packet_type,"arp")==0) {
757 mode = ARP;
759 else if (strcmp(packet_type,"bpdu")==0) {
760 mode = BPDU;
762 else if (strcmp(packet_type,"ip")==0) {
763 mode = IP;
765 else if (strcmp(packet_type,"udp")==0) {
766 mode = UDP;
768 else if (strcmp(packet_type,"icmp")==0) {
769 mode = ICMP;
771 else if (strcmp(packet_type,"icmp6")==0) {
772 mode = ICMP6;
774 else if (strcmp(packet_type,"tcp")==0) {
775 mode = TCP;
777 else if (strcmp(packet_type,"dns")==0) {
778 mode = DNS;
780 else if (strcmp(packet_type,"cdp")==0) {
781 mode = CDP;
783 else if (strcmp(packet_type,"syslog")==0) {
784 mode = SYSLOG;
786 else if (strcmp(packet_type,"lldp")==0) {
787 mode = LLDP;
788 tx.packet_mode=0; // create whole frame by ourself
790 else if (strcmp(packet_type,"rtp")==0) {
791 if (RX) {
792 mode = RX_RTP;
794 else {
795 mode = RTP;
796 if (!count_set) tx.count = 0;
797 if (!delay_set) tx.delay = 20000; // 20 msec inter-packet delay for RTP
800 else if (strcmp(packet_type,"help")==0) {
801 fprintf(stderr, "\n"
802 MAUSEZAHN_VERSION
803 "\n"
804 "| The following packet types are currently implemented:\n"
805 "|\n"
806 "| arp ... sends ARP packets\n"
807 "| bpdu ... sends BPDU packets (STP or PVST+)\n"
808 "| cdp ... sends CDP messages\n"
809 "| ip ... sends IPv4 packets\n"
810 "| udp ... sends UDP datagrams\n"
811 "| tcp ... sends TCP segments\n"
812 "| icmp ... sends ICMP messages\n"
813 "| dns ... sends DNS messages\n"
814 "| rtp ... sends RTP datagrams\n"
815 "| syslog ... sends Syslog messages\n"
816 "|\n"
817 "| Of course you can build any other packet type 'manually' using the direct layer 2 mode.\n"
818 "| FYI: The interactive mode supports additional protocols. (Try mz -x <port>)\n"
819 "\n"
821 exit(1);
823 else {
824 fprintf(stderr, " mz: you must specify a valid packet type!\n");
828 //////////////////////////////////////////////////////////////////////////
830 // TODO: Implement macro support
831 // Check macro types here
833 return 0;
836 int main(int argc, char **argv)
838 // These handles are only used when creating L3 and above packets.
839 libnet_t *l; // the context
840 libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers
841 double cpu_time_used;
843 reset();
845 if ( getopts(argc, argv) )
847 (void) fprintf(stderr, " Invalid command line parameters!\n");
848 help();
851 // Check whether hires timers are supported or not:
852 (void) check_timer();
854 signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT
856 switch (mode)
858 case BYTE_STREAM:
859 send_eth();
860 break;
862 case ARP:
863 (void) send_arp();
864 break;
866 case BPDU:
867 (void) send_bpdu();
868 break;
870 case CDP:
871 (void) send_cdp();
872 break;
874 case IP: // From now on a new much more modular method is used:
875 l = get_link_context();
876 t3 = create_ip_packet(l); // t3 can be used for later header changes
877 if (!quiet) complexity();
878 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
879 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
880 else
881 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
882 break;
884 case ICMP:
885 tx.ip_proto = 1;
886 l = get_link_context();
887 t4 = create_icmp_packet(l); // t4 can be used for later header changes
888 t3 = create_ip_packet(l); // t3 can be used for later header changes
889 if (!quiet) complexity();
890 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
891 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
892 else
893 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
894 break;
896 case ICMP6:
897 tx.ip_proto = 58;
898 l = get_link_context();
899 t4 = create_icmp6_packet(l); // t4 can be used for later header changes
900 t3 = create_ip_packet(l); // t3 can be used for later header changes
901 if (ipv6_mode)
902 update_ISUM(l, t4);
903 if (!quiet) complexity();
904 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
905 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
906 else
907 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
908 break;
910 case UDP:
911 tx.ip_proto = 17;
912 l = get_link_context();
913 t4 = create_udp_packet(l); // t4 can be used for later header changes
914 t3 = create_ip_packet(l); // t3 can be used for later header changes
915 if (ipv6_mode)
916 update_USUM(l, t4);
917 if (!quiet) complexity();
918 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
919 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
920 else
921 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
922 break;
924 case TCP:
925 tx.ip_proto = 6;
926 l = get_link_context();
927 t4 = create_tcp_packet(l); // t4 can be used for later header changes
928 t3 = create_ip_packet(l); // t3 can be used for later header changes
929 if (ipv6_mode)
930 update_TSUM(l, t4);
931 if (!quiet) complexity();
932 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
933 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
934 else
935 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
936 break;
938 case DNS:
939 tx.ip_proto = 17;
940 l = get_link_context();
941 (void) create_dns_packet();
942 t4 = create_udp_packet(l); // t4 can be used for later header changes
943 t3 = create_ip_packet(l); // t3 can be used for later header changes
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 RTP:
952 tx.ip_proto = 17;
953 l = get_link_context();
954 if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay);
955 (void) create_rtp_packet();
956 t4 = create_udp_packet(l); // t4 can be used for later header changes
957 t3 = create_ip_packet(l); // t3 can be used for later header changes
958 if (!quiet) complexity();
959 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
960 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
961 else
962 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
963 break;
965 case RX_RTP: // Receive RTP packets
966 rcv_rtp_init();
967 rcv_rtp();
968 break;
970 case SYSLOG:
971 tx.ip_proto = 17;
972 l = get_link_context();
973 (void) create_syslog_packet();
974 t4 = create_udp_packet(l); // t4 can be used for later header changes
975 t3 = create_ip_packet(l); // t3 can be used for later header changes
976 if (!quiet) complexity();
978 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
979 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
980 else
981 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
982 break;
984 case LLDP: // start with a new concept here
985 //l = get_link_context();
986 //(void) create_lldp_packet();
987 // // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
988 fprintf(stderr, "LLDP is currently only supported via the interactive mode\n");
989 exit(1);
990 break;
993 default:
994 (void) fprintf(stderr," mz/main: unknown mode! Stop.\n");
995 return (1);
998 if (!quiet)
1000 mz_stop = clock();
1001 cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC;
1002 if (cpu_time_used > 0)
1004 total_d /= cpu_time_used;
1005 fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d);
1007 else
1009 fprintf(stderr, "\n");
1013 return(0);