docs: authors: add Doug as minor contr. (thanks)
[netsniff-ng.git] / src / mausezahn.c
blob5d7a71db2a4e4d408983e8f5f7aae3bf356bbfea
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"
33 #include "list.h"
35 int verbose_level = 0;
37 static const char *short_options = "46hqvVSxra:A:b:B:c:d:E:f:F:p:P:t:T:M:Q:X:";
39 static void signal_handler(int number)
41 clean_up(number);
44 void clean_up(int sig)
46 int i;
47 struct arp_table_struct *cur, *next;
49 if (!quiet) fprintf(stderr, "\nMausezahn cleans up...\n");
51 if (fp != NULL) {
52 verbose_l1(" close files (1) ...\n");
54 fflush(fp);
55 fclose(fp);
58 if (fp2!=NULL) {
59 if (verbose) fprintf(stderr, " close files (2) ...\n");
60 (void) fflush(fp2);
61 (void) fclose(fp2);
64 // interactive mode?
65 if (mz_port) {
66 if (verbose) fprintf(stderr, " clear mops list...\n");
67 mops_cleanup(mp_head);
68 if (verbose) fprintf(stderr, " clear automops list...\n");
69 automops_cleanup(amp_head);
70 if (verbose) fprintf(stderr, " clear packet sequences...\n");
71 mz_ll_delete_list(packet_sequences);
74 for (i=0; i<device_list_entries; i++) {
75 if (device_list[i].p_arp!=NULL) {
76 pcap_close(device_list[i].p_arp);
77 fprintf(stderr, " stopped ARP process for device %s\n", device_list[i].dev);
79 if (device_list[i].arprx_thread!=0) {
80 pthread_cancel(device_list[i].arprx_thread);
81 if (verbose)
82 fprintf(stderr, " (ARP thread for device %s done)\n", device_list[i].dev);
85 if (device_list[i].arp_table!=NULL) {
86 cur=device_list[i].arp_table;
87 while (cur!=NULL) {
88 next = cur->next;
89 if (cur!=NULL) free(cur);
90 cur=next;
94 // close packet sockets
95 if (device_list[i].ps>=0) {
96 close(device_list[i].ps);
101 if (verbose) fprintf(stderr, "finished.\n");
102 exit(sig);
106 static void help(void)
108 printf("\nmausezahn %s, a fast versatile traffic generator\n", VERSION_STRING);
109 puts("http://www.netsniff-ng.org\n\n"
110 "Usage: mausezahn [options] [interface] <keyword>|<arg-string>|<hex-string>\n"
111 "Options:\n"
112 " -x <port> Interactive mode with telnet CLI, default port: 25542\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> 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) 2012 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, a fast versatile traffic generator\n", VERSION_STRING);
176 puts("http://www.netsniff-ng.org\n\n"
177 "Please report bugs to <bugs@netsniff-ng.org>\n"
178 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
179 "Copyright (C) 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
180 "Swiss federal institute of technology (ETH Zurich)\n"
181 "License: GNU GPL version 2.0\n"
182 "This is free software: you are free to change and redistribute it.\n"
183 "There is NO WARRANTY, to the extent permitted by law.\n");
184 die();
187 int reset()
189 int i;
190 time_t t;
192 // Determine platform type sizes:
193 MZ_SIZE_LONG_INT = sizeof(long int);
195 mz_default_config_path[0] = 0x00;
196 mz_default_log_path[0] = 0x00;
198 // Reset globals:
199 quiet = 0;
200 ipv6_mode = 0;
201 verbose = 0;
202 simulate = 0;
203 filename[0] = '\0';
204 path[0] = '\0';
205 gind=0;
206 gind_max = TIME_COUNT;
207 fp = NULL;
208 fp2 = NULL;
209 mz_port = 0;
210 mz_rand = 0;
211 mp_head = NULL;
213 for (i=0;i<TIME_COUNT_MAX;i++) jitter[i] = 0;
215 time0_flag = 0; // If set then time0 has valid data
216 sqnr0_flag = 0; // If set then sqnr_last and sqnr_next has valid data
217 rtp_log = 0;
218 mz_ssrc[0]=0; mz_ssrc[1]=0; mz_ssrc[2]=0; mz_ssrc[3]=0;
220 // Reset mgmt parameters of TX:
221 tx.packet_mode = 1; // assume we don't care about L2
222 tx.count = 1;
223 tx.delay = DEFAULT_DELAY;
224 tx.arg_string[0] = '\0';
226 // Reset Ethernet parameters of TX:
227 tx.eth_params_already_set = 0;
228 for (i=0; i<6; i++) tx.eth_dst[i] = 0xff;
229 for (i=0; i<6; i++) tx.eth_src[i] = 0; // TODO: Get own MAC !!!
230 tx.eth_dst_txt[0] = '\0';
231 tx.eth_src_txt[0] = '\0';
232 tx.eth_dst_rand = 0;
233 tx.eth_src_rand = 0;
235 tx.eth_type = 0x800;
236 tx.eth_len = 0;
237 tx.eth_payload[0] = '\0';
238 tx.eth_payload_s = 0;
239 tx.padding = 0;
241 // Reset CDP parameters for TX:
242 tx.cdp_sum = 0;
243 tx.cdp_version = 0;
244 tx.cdp_ttl = 0;
245 tx.cdp_payload[0] = '\0';
246 tx.cdp_payload_s = 0;
247 tx.cdp_tlv_id[0] = '\0';
248 tx.cdp_tlv_id_len = 0;
250 // Reset 802.1Q parameters of TX:
251 tx.dot1Q=0;
252 tx.dot1Q_txt[0] = '\0';
254 // ASCII Payload:
255 tx.ascii = 0; // 1 if specified
256 tx.ascii_payload[0]= '\0';
258 // HEX Payload:
259 tx.hex_payload_s = 0;
261 // Reset MPLS parameters of TX:
262 tx.mpls = 0;
263 tx.mpls_txt[0] = '\0';
264 tx.mpls_label = 0;
265 tx.mpls_exp = 0;
266 tx.mpls_bos = 1;
267 tx.mpls_ttl = 255;
268 tx.mpls_verbose_string[0] = '\0';
270 // Reset IP parameters of TX:
271 tx.ip_src_txt[0] = '\0';
272 tx.ip_src_rand = 0;
273 tx.ip_dst_txt[0] = '\0';
274 tx.ip_src_isrange = 0;
275 tx.ip_src_start = 0;
276 tx.ip_src_stop = 0;
278 tx.ip_dst_start = 0;
279 tx.ip_dst_stop = 0;
280 tx.ip_dst_isrange = 0;
282 tx.ip_len = 0;
283 tx.ip_payload[0]= '\0';
284 tx.ip_payload_s = 0;
285 tx.ip_option[0]= '\0';
286 tx.ip_option_s = 0;
288 // Reset ICMP parameters:
289 tx.icmp_type=0;
290 tx.icmp_code=0;
291 tx.icmp_chksum=0; // 0=autofill
292 tx.icmp_ident=0x42;
293 tx.icmp_sqnr=0x1;
294 tx.icmp_payload_s=0;
296 // Reset general L4 parameters:
297 tx.sp = 0;
298 tx.dp = 0;
299 tx.sp_start = 0;
300 tx.sp_stop = 0;
301 tx.dp_start = 0;
302 tx.dp_stop = 0;
303 tx.sp_isrange = 0;
304 tx.dp_isrange = 0;
306 // Reset UDP parameters of TX:
308 tx.udp_len = 0; // If set to zero then create_udp_packet will calculate it
309 tx.udp_sum = 0;
310 tx.udp_payload[0] = '\0';
311 tx.udp_payload_s = 0;
313 // Reset TCP parameters of TX:
315 tx.tcp_seq = 42;
316 tx.tcp_seq_stop = 42;
317 tx.tcp_seq_delta = 0; // also used as 'isrange' meaning
318 tx.tcp_ack = 42;
319 tx.tcp_control = 0;
320 tx.tcp_win = 10000;
321 tx.tcp_sum = 0;
322 tx.tcp_urg = 0;
323 tx.tcp_len = 20; // Least size (TCP header only)
324 tx.tcp_payload[0] = '\0';
325 tx.tcp_payload_s = 0;
327 // Reset RTP parameters of TX:
328 tx.rtp_sqnr = 0;
329 tx.rtp_stmp = 0;
331 // Initialize random generator
332 time(&t);
333 srand((unsigned int)t);
335 // Reset device_list
336 for (i=0; i<MZ_MAX_DEVICES; i++) {
337 device_list[i].arprx_thread = 0;
338 device_list[i].p_arp = NULL;
339 device_list[i].arp_table = NULL;
340 device_list[i].ps=-1;
341 device_list[i].cli=0;
342 device_list[i].mgmt_only=0;
345 return 0;
350 // Purpose: Properly handle arguments and configure global structs (tx)
351 int getopts (int argc, char *argv[])
353 int i, c, rargs, RX=0, count_set=0, delay_set=0;
354 unsigned int time_factor;
355 char *packet_type=NULL, *mops_type=NULL;
356 char *dum;
357 unsigned char *dum1, *dum2;
359 libnet_t *l;
360 char err_buf[LIBNET_ERRBUF_SIZE];
361 struct libnet_ether_addr *mymac;
363 FILE *afp;
364 char hexpld[MAX_PAYLOAD_SIZE*2];
365 int hexpld_specified=0;
367 opterr = 1; // let getopt print error message if necessary
370 while ((c = getopt(argc, argv, short_options)) != -1)
371 switch (c) {
372 case '4':
373 tx.eth_type = 0x0800;
374 ipv6_mode=0;
375 break;
376 case '6':
377 tx.eth_type = 0x86dd;
378 ipv6_mode=1;
379 break;
380 case 'h':
381 help();
382 break;
383 case 'q':
384 quiet=1;
385 break;
386 case 'v':
387 version();
388 break;
389 case 'V':
390 verbose++;
391 break;
392 case 'S':
393 simulate=1;
394 break;
395 case 'x':
396 mz_port = MZ_DEFAULT_PORT;
397 break;
398 case 'a':
399 strncpy (tx.eth_src_txt, optarg, 32);
400 tx.packet_mode = 0;
401 break;
402 case 'A':
403 strncpy (tx.ip_src_txt, optarg, sizeof(tx.ip_src_txt));
404 break;
405 case 'b':
406 strncpy (tx.eth_dst_txt, optarg, 32);
407 tx.packet_mode = 0;
408 break;
409 case 'B':
410 strncpy (tx.ip_dst_txt, optarg, sizeof(tx.ip_dst_txt));
411 break;
412 case 'c':
413 errno=0;
414 tx.count = strtol(optarg, (char **)NULL, 10);
415 if ((errno == ERANGE && (tx.count == LONG_MAX || tx.count == LONG_MIN))
416 || (errno != 0 && tx.count == 0)) {
417 perror("strtol");
418 return (-1);
420 if (tx.count<0) tx.count=1; //TODO: Allow count=0 which means infinity (need to update all send_functions)
421 count_set=1;
422 break;
423 case 'd':
424 errno=0;
425 // determine whether seconds or msecs are used
426 // default is usec!!!
427 time_factor=1;
428 if (exists(optarg,"s") || exists(optarg,"sec")) time_factor=1000000;
429 if (exists(optarg,"m") || exists(optarg,"msec")) time_factor=1000;
430 dum = strtok(optarg,"ms");
431 tx.delay = strtol(dum, (char **)NULL, 10) * time_factor;
432 if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN))
433 || (errno != 0 && tx.delay == 0)) {
434 perror("strtol");
435 return (-1);
437 if (tx.delay<0) tx.delay=0; // no delay
438 delay_set=1;
439 break;
440 case 'p':
441 errno=0;
442 tx.padding = strtol(optarg, (char **)NULL, 10);
443 if ((errno == ERANGE && (tx.padding == LONG_MAX || tx.padding == LONG_MIN))
444 || (errno != 0 && tx.padding == 0)) {
445 perror("strtol");
446 return (-1);
448 if (tx.padding>10000) {
449 fprintf(stderr, " Warning: Padding must not exceed 10000!\n");
450 return -1;
452 break;
453 case 't':
454 packet_type = optarg; // analyzed below
455 break;
456 case 'X':
457 mops_type = optarg; // MOPS TRANSITION STRATEGY -- analyzed below
458 break;
459 case 'T':
460 packet_type = optarg;
461 RX = 1;
462 break;
463 case 'r':
464 mz_rand = 1;
465 break;
466 case 'M':
467 if (strncmp(optarg,"help",4)==0) {
468 (void) get_mpls_params("help ");
470 else {
471 strncpy (tx.mpls_txt, optarg, 128);
472 tx.eth_type = ETHERTYPE_MPLS;
473 tx.packet_mode = 0;
474 tx.mpls=1;
476 break;
477 case 'P': // ASCII payload
478 strncpy((char*)tx.ascii_payload, optarg, MAX_PAYLOAD_SIZE);
479 tx.ascii = 1;
480 break;
481 case 'f': // ASCII payload in FILE
482 afp = fopen(optarg, "r");
483 if (fgets((char*)tx.ascii_payload, MAX_PAYLOAD_SIZE, afp) == NULL)
484 fprintf(stderr, " mz/getopts: File empty?\n");
485 fclose(afp);
486 tx.ascii = 1;
487 break;
488 case 'F': // HEX payload in FILE
489 afp = fopen(optarg, "r");
490 i=0;
491 while ( (hexpld[i]=fgetc(afp))!=EOF ) {
492 if (isspace(hexpld[i])) {
493 hexpld[i]=':';
495 i++;
497 hexpld[i]='\0';
498 fclose(afp);
499 hexpld_specified=1;
500 break;
501 case 'Q': // VLAN TAG
502 if (strncmp(optarg,"help",4)==0) {
503 print_dot1Q_help(); // ugly but most simple and safe solution
505 else {
506 strncpy (tx.dot1Q_txt, optarg, 32);
507 tx.dot1Q=1;
508 // determine number of VLAN tags
509 for (i=0; i<strlen(tx.dot1Q_txt); i++) {
510 if (tx.dot1Q_txt[i]==',') tx.dot1Q++;
512 tx.packet_mode = 0;
514 break;
515 case '?':
516 if ((optopt == 'a') || (optopt == 'b') || (optopt = 'c') ||
517 (optopt == 'd') || (optopt == 'f') || (optopt = 'p') ||
518 (optopt == 't') || (optopt == 'm'))
519 fprintf (stderr, " mz/getopts: Option -%c requires an argument.\n", optopt);
520 else if (isprint (optopt))
521 fprintf (stderr, " mz/getopts: Unknown option -%c'.\n", optopt);
522 else
523 fprintf (stderr, " mz/getopts: Unknown option character \\x%x'.\n", optopt);
524 return 1;
525 default:
526 fprintf (stderr," mz/getopts: Could not handle arguments properly!\n");
527 return 1;
530 // ********************************************
531 // Handle additional arguments
532 // ********************************************
534 // Greeting text
535 if (verbose) {
536 fprintf(stderr,"\n"
537 MAUSEZAHN_VERSION
538 "\n"
539 "Use at your own risk and responsibility!\n"
540 "-- Verbose mode --\n"
541 "\n");
544 if (argc<2) {
545 help();
548 if ((rargs=argc-optind)>2) { // number of remaining arguments
549 fprintf(stderr," mz/getopts: Too many arguments!\n");
550 return -1;
554 // There can be 0-2 additional arguments
555 switch (rargs) {
556 case 0:
557 if (lookupdev()) { // no device found
558 if (verbose) fprintf(stderr, " mz: no active interfaces found!\n");
559 strcpy(tx.device, "lo");
561 if (verbose) // device found
562 fprintf(stderr," mz: device not given, will use %s\n",tx.device);
563 break;
564 case 1: // arg_string OR device given => find out!
565 if ( (strncmp(argv[optind],"eth",3)==0)
566 || (strncmp(argv[optind],"ath",3)==0)
567 || ((strncmp(argv[optind],"lo",2)==0)&&(strncmp(argv[optind],"log",3)!=0))
568 || (strncmp(argv[optind],"vmnet",5)==0)
569 || (strncmp(argv[optind],"wifi",4)==0) ) {
570 // device has been specified!
571 strncpy (tx.device, argv[optind], 16);
573 else { /// arg_string given => no device has been specified -- let's find one!
574 strncpy (tx.arg_string, argv[optind], MAX_PAYLOAD_SIZE);
575 if (lookupdev()) { // no device found
576 if (verbose) fprintf(stderr, " mz: no active interfaces found!\n");
577 strcpy(tx.device, "lo");
579 if (verbose)
580 fprintf(stderr," mz: device not given, will use %s\n",tx.device);
582 break;
583 case 2: // both device and arg_string given
584 strncpy (tx.device, argv[optind], 16);
585 strncpy (tx.arg_string, argv[optind+1], MAX_PAYLOAD_SIZE);
586 break;
587 default:
588 fprintf(stderr," mz/getopts: Unknown argument problem!\n");
589 return 1;
592 if (hexpld_specified) {
593 strcat(tx.arg_string, ",p=");
594 strcat(tx.arg_string, hexpld);
598 //////////////////////////////////////////////////////////////////////////
600 // Initialize MAC and IP Addresses.
602 // - tx.eth_src = own interface MAC
603 // - tx.ip_src = own interface IP or user specified
604 // - tx.ip_dst = 255.255.255.255 or user specified (can be a range)
605 // - tx.ip_src_rand ... is set if needed.
608 // Get own device MAC address:
609 // Don't open context if only a help text is requested
610 if (getarg(tx.arg_string,"help", NULL)!=1) {
611 l = libnet_init (LIBNET_LINK_ADV, tx.device, err_buf );
612 if (l == NULL) {
613 fprintf(stderr, " mz/getopts: libnet_init() failed (%s)", err_buf);
614 return -1;
616 mymac = libnet_get_hwaddr(l);
617 for (i=0; i<6; i++) {
618 tx.eth_src[i] = mymac->ether_addr_octet[i];
619 tx.eth_mac_own[i] = mymac->ether_addr_octet[i];
622 // Set source IP address:
623 if (strlen(tx.ip_src_txt)) { // option -A has been specified
624 if (mz_strcmp(tx.ip_src_txt, "bcast", 2)==0) {
625 tx.ip_src = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
626 } else if (strcmp(tx.ip_src_txt, "rand") == 0) {
627 tx.ip_src_rand = 1;
628 tx.ip_src_h = (u_int32_t) ( ((float) rand()/RAND_MAX)*0xE0000000); //this is 224.0.0.0
630 else if (get_ip_range_src(tx.ip_src_txt)) { // returns 1 when no range has been specified
631 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
632 if (ipv6_mode)
633 tx.ip6_src = libnet_name2addr6 (l, tx.ip_src_txt, LIBNET_RESOLVE);
634 else
635 tx.ip_src = libnet_name2addr4 (l, tx.ip_src_txt, LIBNET_RESOLVE);
638 else { // no source IP specified: by default use own IP address
639 if (ipv6_mode) {
640 tx.ip6_src = libnet_get_ipaddr6(l);
641 if (strncmp((char*)&tx.ip6_src,(char*)&in6addr_error,sizeof(in6addr_error))==0)
642 printf("Failed to set source IPv6 address: %s", l->err_buf);
644 else
645 tx.ip_src = libnet_get_ipaddr4(l);
648 // Set destination IP address:
649 if (strlen(tx.ip_dst_txt)) { // option -B has been specified
650 if (mz_strcmp(tx.ip_dst_txt, "rand", 2)==0) {
651 fprintf(stderr, "Option -B does not support random destination IP addresses currently.\n");
652 return 1;
655 if (mz_strcmp(tx.ip_dst_txt, "bcast", 2)==0) {
656 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
657 } else if (get_ip_range_dst(tx.ip_dst_txt)) { // returns 1 when no range has been specified
658 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
659 if (ipv6_mode)
660 tx.ip6_dst = libnet_name2addr6 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
661 else
662 tx.ip_dst = libnet_name2addr4 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
665 else { // no destination IP specified: by default use broadcast
666 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
669 // Initialize tx.ip_src_h and tx.ip_dst_h which are used by 'print_frame_details()'
670 // in verbose mode. See 'modifications.c'.
672 if (tx.ip_src_rand) { // ip_src_h already given, convert to ip_src
673 dum1 = (unsigned char*) &tx.ip_src_h;
674 dum2 = (unsigned char*) &tx.ip_src;
676 else { // ip_src already given, convert to ip_src_h
677 dum1 = (unsigned char*) &tx.ip_src;
678 dum2 = (unsigned char*) &tx.ip_src_h;
681 *dum2 = *(dum1+3);
682 dum2++;
683 *dum2 = *(dum1+2);
684 dum2++;
685 *dum2 = *(dum1+1);
686 dum2++;
687 *dum2 = *dum1;
689 dum1 = (unsigned char*) &tx.ip_dst;
690 dum2 = (unsigned char*) &tx.ip_dst_h;
692 *dum2 = *(dum1+3);
693 dum2++;
694 *dum2 = *(dum1+2);
695 dum2++;
696 *dum2 = *(dum1+1);
697 dum2++;
698 *dum2 = *dum1;
700 libnet_destroy(l);
704 // END OF ADDRESS INITIALIZATION
706 //////////////////////////////////////////////////////////////////////////
709 ////// retrieve interface parameters ///////
711 for (i=0; i<device_list_entries; i++) {
712 get_dev_params(device_list[i].dev);
716 //////////////////////////////////////////////////////////////////////////
718 // Mausezahn CLI desired?
719 if (mz_port) {
720 // has port number been specified?
721 if (strlen(tx.arg_string)) {
722 mz_port = (int) str2int (tx.arg_string);
725 if (!quiet) {
726 fprintf(stderr, "Mausezahn accepts incoming Telnet connections on port %i.\n", mz_port);
729 mz_cli_init();
730 cli();
733 //////////////////////////////////////////////////////////////////////////
735 // Mode decision
737 // Consider -t and -m option (used exclusively)
738 // -t => special packet types, stateless
740 // If -t not present then evaluate arg_string which must
741 // contain a byte-string in hexadecimal notation.
745 // ***** NEW: MOPS TRANSITION STRATEGY *****
746 if (mops_type != NULL) {
748 if (mz_strcmp(mops_type,"lldp",4)==0) {
749 mops_direct(tx.device, MOPS_LLDP, tx.arg_string);
754 if (packet_type == NULL) { // raw hex string given
755 mode = BYTE_STREAM;
757 else if (strcmp(packet_type,"arp")==0) {
758 mode = ARP;
760 else if (strcmp(packet_type,"bpdu")==0) {
761 mode = BPDU;
763 else if (strcmp(packet_type,"ip")==0) {
764 mode = IP;
766 else if (strcmp(packet_type,"udp")==0) {
767 mode = UDP;
769 else if (strcmp(packet_type,"icmp")==0) {
770 mode = ICMP;
772 else if (strcmp(packet_type,"icmp6")==0) {
773 mode = ICMP6;
775 else if (strcmp(packet_type,"tcp")==0) {
776 mode = TCP;
778 else if (strcmp(packet_type,"dns")==0) {
779 mode = DNS;
781 else if (strcmp(packet_type,"cdp")==0) {
782 mode = CDP;
784 else if (strcmp(packet_type,"syslog")==0) {
785 mode = SYSLOG;
787 else if (strcmp(packet_type,"lldp")==0) {
788 mode = LLDP;
789 tx.packet_mode=0; // create whole frame by ourself
791 else if (strcmp(packet_type,"rtp")==0) {
792 if (RX) {
793 mode = RX_RTP;
795 else {
796 mode = RTP;
797 if (!count_set) tx.count = 0;
798 if (!delay_set) tx.delay = 20000; // 20 msec inter-packet delay for RTP
801 else if (strcmp(packet_type,"help")==0) {
802 fprintf(stderr, "\n"
803 MAUSEZAHN_VERSION
804 "\n"
805 "| The following packet types are currently implemented:\n"
806 "|\n"
807 "| arp ... sends ARP packets\n"
808 "| bpdu ... sends BPDU packets (STP or PVST+)\n"
809 "| cdp ... sends CDP messages\n"
810 "| ip ... sends IPv4 packets\n"
811 "| udp ... sends UDP datagrams\n"
812 "| tcp ... sends TCP segments\n"
813 "| icmp ... sends ICMP messages\n"
814 "| dns ... sends DNS messages\n"
815 "| rtp ... sends RTP datagrams\n"
816 "| syslog ... sends Syslog messages\n"
817 "|\n"
818 "| Of course you can build any other packet type 'manually' using the direct layer 2 mode.\n"
819 "| FYI: The interactive mode supports additional protocols. (Try mz -x <port>)\n"
820 "\n"
822 exit(1);
824 else {
825 fprintf(stderr, " mz: you must specify a valid packet type!\n");
829 //////////////////////////////////////////////////////////////////////////
831 // TODO: Implement macro support
832 // Check macro types here
834 return 0;
837 int main(int argc, char **argv)
839 // These handles are only used when creating L3 and above packets.
840 libnet_t *l; // the context
841 libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers
842 double cpu_time_used;
844 reset();
846 if ( getopts(argc, argv) )
848 (void) fprintf(stderr, " Invalid command line parameters!\n");
849 help();
852 // Check whether hires timers are supported or not:
853 (void) check_timer();
855 signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT
857 switch (mode)
859 case BYTE_STREAM:
860 send_eth();
861 break;
863 case ARP:
864 (void) send_arp();
865 break;
867 case BPDU:
868 (void) send_bpdu();
869 break;
871 case CDP:
872 (void) send_cdp();
873 break;
875 case IP: // From now on a new much more modular method is used:
876 l = get_link_context();
877 t3 = create_ip_packet(l); // t3 can be used for later header changes
878 if (!quiet) complexity();
879 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
880 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
881 else
882 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
883 break;
885 case ICMP:
886 tx.ip_proto = 1;
887 l = get_link_context();
888 t4 = create_icmp_packet(l); // t4 can be used for later header changes
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 ICMP6:
898 tx.ip_proto = 58;
899 l = get_link_context();
900 t4 = create_icmp6_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 (ipv6_mode)
903 update_ISUM(l, t4);
904 if (!quiet) complexity();
905 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
906 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
907 else
908 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
909 break;
911 case UDP:
912 tx.ip_proto = 17;
913 l = get_link_context();
914 t4 = create_udp_packet(l); // t4 can be used for later header changes
915 t3 = create_ip_packet(l); // t3 can be used for later header changes
916 if (ipv6_mode)
917 update_USUM(l, t4);
918 if (!quiet) complexity();
919 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
920 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
921 else
922 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
923 break;
925 case TCP:
926 tx.ip_proto = 6;
927 l = get_link_context();
928 t4 = create_tcp_packet(l); // t4 can be used for later header changes
929 t3 = create_ip_packet(l); // t3 can be used for later header changes
930 if (ipv6_mode)
931 update_TSUM(l, t4);
932 if (!quiet) complexity();
933 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
934 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
935 else
936 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
937 break;
939 case DNS:
940 tx.ip_proto = 17;
941 l = get_link_context();
942 (void) create_dns_packet();
943 t4 = create_udp_packet(l); // t4 can be used for later header changes
944 t3 = create_ip_packet(l); // t3 can be used for later header changes
945 if (!quiet) complexity();
946 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
947 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
948 else
949 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
950 break;
952 case RTP:
953 tx.ip_proto = 17;
954 l = get_link_context();
955 if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay);
956 (void) create_rtp_packet();
957 t4 = create_udp_packet(l); // t4 can be used for later header changes
958 t3 = create_ip_packet(l); // t3 can be used for later header changes
959 if (!quiet) complexity();
960 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
961 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
962 else
963 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
964 break;
966 case RX_RTP: // Receive RTP packets
967 rcv_rtp_init();
968 rcv_rtp();
969 break;
971 case SYSLOG:
972 tx.ip_proto = 17;
973 l = get_link_context();
974 (void) create_syslog_packet();
975 t4 = create_udp_packet(l); // t4 can be used for later header changes
976 t3 = create_ip_packet(l); // t3 can be used for later header changes
977 if (!quiet) complexity();
979 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
980 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
981 else
982 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
983 break;
985 case LLDP: // start with a new concept here
986 //l = get_link_context();
987 //(void) create_lldp_packet();
988 // // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
989 fprintf(stderr, "LLDP is currently only supported via the interactive mode\n");
990 exit(1);
991 break;
994 default:
995 (void) fprintf(stderr," mz/main: unknown mode! Stop.\n");
996 return (1);
999 if (!quiet)
1001 mz_stop = clock();
1002 cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC;
1003 if (cpu_time_used > 0)
1005 total_d /= cpu_time_used;
1006 fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d);
1008 else
1010 fprintf(stderr, "\n");
1014 return(0);