mausezahn: Add error handling for mismatched address families
[netsniff-ng.git] / staging / mausezahn.c
blob7acd6bced31006901479477eefe22e9c35e2f62b
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"
33 #include "dev.h"
35 int verbose_level = 0;
37 static const char *short_options = "46hqvVSxra:A:b:B:c:d:E:f:F:l: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 " -l <ip> Listen address to bind to when in interactive mode, default: 0.0.0.0\n"
114 " -4 IPv4 mode (default)\n"
115 " -6 IPv6 mode\n"
116 " -c <count> Send packet count times, default:1, infinite:0\n"
117 " -d <delay> Apply delay between transmissions. The delay value can be\n"
118 " specified in usec (default, no additional unit needed), or in\n"
119 " msec (e.g. 100m or 100msec), or in seconds (e.g. 100s or 100sec)\n"
120 " -r Multiplies the specified delay with a random value\n"
121 " -p <length> Pad the raw frame to specified length (using random bytes)\n"
122 " -a <srcmac|keyword> Use specified source mac address, no matter what has\n"
123 " been specified with other arguments; keywords see below,\n"
124 " Default is own interface\n"
125 " -b <dstmac|keyword> Same with destination mac address; keywords:\n"
126 " rand Use a random MAC address\n"
127 " bc Use a broadcast MAC address\n"
128 " own Use own interface MAC address (default for source MAC)\n"
129 " stp Use IEEE 802.1d STP multicast address\n"
130 " cisco Use Cisco multicast address as used for CDP, VTP, or PVST+\n"
131 " -A <srcip> Use specified source IP address (default is own interface IP)\n"
132 " -B <dstip|dnsname> Send packet to specified destination IP or domain name\n"
133 " -P <ascii payload> Use the specified ASCII payload\n"
134 " -f <filename> Read the ASCII payload from a file\n"
135 " -F <filename> Read the hexadecimal payload from a file\n"
136 " -Q <[CoS:]vlan> Specify 802.1Q VLAN tag and optional Class of Service, you can\n"
137 " specify multiple 802.1Q VLAN tags (QinQ...) by separating them\n"
138 " via a comma or a period (e.g. '5:10,20,2:30')\n"
139 " -t <packet-type|help> Specify packet type for autobuild (you don't need to care for\n"
140 " encapsulations in lower layers, most packet types allow/require\n"
141 " additional packet-specific arguments in an <arg-string>;\n"
142 " Currently supported types: arp, bpdu, cdp, ip, icmp, udp, tcp,\n"
143 " dns, rtp, syslog, lldp and more;\n"
144 " For context-help use 'help' as <arg-string>!\n"
145 " -T <packet-type> Specify packet type for server mode, currently only rtp is supported;\n"
146 " Enter -T help or -T rtp help for further information\n"
147 " -M <MPLS-label> Insert a MPLS label, enter '-M help' for a syntax description\n"
148 " -V|VV|... Verbose and more verbose mode\n"
149 " -q Quiet mode, even omit 'important' standard short messages\n"
150 " -S Simulation mode: DOES NOT put anything on the wire, this is\n"
151 " typically combined with one of the verbose modes (v or V)\n"
152 " -v Show version\n"
153 " -h Print this help\n\n"
154 "Examples:\n"
155 " mausezahn -x 99\n"
156 " mausezahn -c 0 -d 2s -t bpdu conf\n"
157 " mausezahn -t cdp change -c 0\n"
158 " mausezahn -t syslog sev=3 -P \"You have been mausezahned.\" -A 10.1.1.109 -B 192.168.7.7\n"
159 " mausezahn eth0 -A rand -B 1.1.1.1 -c 0 -t tcp \"dp=1-1023, flags=syn\"\n\n"
160 "Note:\n"
161 " This tool is targeted for network developers! You should\n"
162 " be aware of what you are doing and what these options above\n"
163 " mean! Only use this tool in an isolated LAN that you own!\n\n"
164 "Please report bugs to <bugs@netsniff-ng.org>\n"
165 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
166 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
167 "Swiss federal institute of technology (ETH Zurich)\n"
168 "License: GNU GPL version 2.0\n"
169 "This is free software: you are free to change and redistribute it.\n"
170 "There is NO WARRANTY, to the extent permitted by law.\n");
171 die();
174 static void version(void)
176 printf("\nmausezahn %s, Git id: %s\n", VERSION_LONG, GITVERSION);
177 puts("a fast versatile traffic generator\n"
178 "http://www.netsniff-ng.org\n\n"
179 "Please report bugs to <bugs@netsniff-ng.org>\n"
180 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
181 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
182 "Swiss federal institute of technology (ETH Zurich)\n"
183 "License: GNU GPL version 2.0\n"
184 "This is free software: you are free to change and redistribute it.\n"
185 "There is NO WARRANTY, to the extent permitted by law.\n");
186 die();
189 int reset(void)
191 int i;
192 time_t t;
194 // Determine platform type sizes:
195 MZ_SIZE_LONG_INT = sizeof(long int);
197 mz_default_config_path[0] = 0x00;
198 mz_default_log_path[0] = 0x00;
200 // Reset globals:
201 quiet = 0;
202 ipv6_mode = 0;
203 verbose = 0;
204 simulate = 0;
205 filename[0] = '\0';
206 path[0] = '\0';
207 gind=0;
208 gind_max = TIME_COUNT;
209 fp = NULL;
210 fp2 = NULL;
211 mz_port = 0;
212 mz_rand = 0;
213 char mz_listen_addr[16] = "0.0.0.0";
214 mp_head = NULL;
216 for (i=0;i<TIME_COUNT_MAX;i++) jitter[i] = 0;
218 time0_flag = 0; // If set then time0 has valid data
219 sqnr0_flag = 0; // If set then sqnr_last and sqnr_next has valid data
220 rtp_log = 0;
221 mz_ssrc[0]=0; mz_ssrc[1]=0; mz_ssrc[2]=0; mz_ssrc[3]=0;
223 // Reset mgmt parameters of TX:
224 tx.packet_mode = 1; // assume we don't care about L2
225 tx.count = 1;
226 tx.delay = DEFAULT_DELAY;
227 tx.arg_string[0] = '\0';
229 // Reset Ethernet parameters of TX:
230 tx.eth_params_already_set = 0;
231 for (i=0; i<6; i++) tx.eth_dst[i] = 0xff;
232 for (i=0; i<6; i++) tx.eth_src[i] = 0; // TODO: Get own MAC !!!
233 tx.eth_dst_txt[0] = '\0';
234 tx.eth_src_txt[0] = '\0';
235 tx.eth_dst_rand = 0;
236 tx.eth_src_rand = 0;
238 tx.eth_type = 0x800;
239 tx.eth_len = 0;
240 tx.eth_payload[0] = '\0';
241 tx.eth_payload_s = 0;
242 tx.padding = 0;
244 // Reset CDP parameters for TX:
245 tx.cdp_sum = 0;
246 tx.cdp_version = 0;
247 tx.cdp_ttl = 0;
248 tx.cdp_payload[0] = '\0';
249 tx.cdp_payload_s = 0;
250 tx.cdp_tlv_id[0] = '\0';
251 tx.cdp_tlv_id_len = 0;
253 // Reset 802.1Q parameters of TX:
254 tx.dot1Q=0;
255 tx.dot1Q_txt[0] = '\0';
257 // ASCII Payload:
258 tx.ascii = 0; // 1 if specified
259 tx.ascii_payload[0]= '\0';
261 // HEX Payload:
262 tx.hex_payload_s = 0;
264 // Reset MPLS parameters of TX:
265 tx.mpls = 0;
266 tx.mpls_txt[0] = '\0';
267 tx.mpls_label = 0;
268 tx.mpls_exp = 0;
269 tx.mpls_bos = 1;
270 tx.mpls_ttl = 255;
271 tx.mpls_verbose_string[0] = '\0';
273 // Reset IP parameters of TX:
274 tx.ip_src_txt[0] = '\0';
275 tx.ip_src_rand = 0;
276 tx.ip_dst_txt[0] = '\0';
277 tx.ip_src_isrange = 0;
278 tx.ip_src_start = 0;
279 tx.ip_src_stop = 0;
280 memset(&tx.ip6_src_start, 0, sizeof(tx.ip6_src_start));
281 memset(&tx.ip6_src_stop, 0, sizeof(tx.ip6_src_stop));
283 tx.ip_dst_start = 0;
284 tx.ip_dst_stop = 0;
285 memset(&tx.ip6_dst_start, 0, sizeof(tx.ip6_dst_start));
286 memset(&tx.ip6_dst_stop, 0, sizeof(tx.ip6_dst_stop));
287 tx.ip_dst_isrange = 0;
289 tx.ip_ttl = 0;
290 tx.ip_len = 0;
291 tx.ip_payload[0]= '\0';
292 tx.ip_payload_s = 0;
293 tx.ip_option[0]= '\0';
294 tx.ip_option_s = 0;
296 // Reset ICMP parameters:
297 tx.icmp_type=0;
298 tx.icmp_code=0;
299 tx.icmp_chksum=0; // 0=autofill
300 tx.icmp_ident=0x42;
301 tx.icmp_sqnr=0x1;
302 tx.icmp_payload_s=0;
304 // Reset general L4 parameters:
305 tx.sp = 0;
306 tx.dp = 0;
307 tx.sp_start = 0;
308 tx.sp_stop = 0;
309 tx.dp_start = 0;
310 tx.dp_stop = 0;
311 tx.sp_isrange = 0;
312 tx.dp_isrange = 0;
314 // Reset UDP parameters of TX:
316 tx.udp_len = 0; // If set to zero then create_udp_packet will calculate it
317 tx.udp_sum = 0;
318 tx.udp_payload[0] = '\0';
319 tx.udp_payload_s = 0;
321 // Reset TCP parameters of TX:
323 tx.tcp_seq = 42;
324 tx.tcp_seq_stop = 42;
325 tx.tcp_seq_delta = 0; // also used as 'isrange' meaning
326 tx.tcp_ack = 42;
327 tx.tcp_control = 0;
328 tx.tcp_win = 10000;
329 tx.tcp_sum = 0;
330 tx.tcp_urg = 0;
331 tx.tcp_len = 20; // Least size (TCP header only)
332 tx.tcp_payload[0] = '\0';
333 tx.tcp_payload_s = 0;
335 // Reset RTP parameters of TX:
336 tx.rtp_sqnr = 0;
337 tx.rtp_stmp = 0;
339 // Initialize random generator
340 time(&t);
341 srand((unsigned int)t);
343 // Reset device_list
344 for (i=0; i<MZ_MAX_DEVICES; i++) {
345 device_list[i].arprx_thread = 0;
346 device_list[i].p_arp = NULL;
347 device_list[i].arp_table = NULL;
348 device_list[i].ps=-1;
349 device_list[i].cli=0;
350 device_list[i].mgmt_only=0;
353 return 0;
356 static void print_packet_types(void)
358 fprintf(stderr, "\n"
359 MAUSEZAHN_VERSION
360 "\n"
361 "| The following packet types are currently implemented:\n"
362 "|\n"
363 "| arp ... sends ARP packets\n"
364 "| bpdu ... sends BPDU packets (STP or PVST+)\n"
365 "| cdp ... sends CDP messages\n"
366 "| ip ... sends IPv4 packets\n"
367 "| udp ... sends UDP datagrams\n"
368 "| tcp ... sends TCP segments\n"
369 "| icmp ... sends ICMP messages\n"
370 "| igmp ... sends IGMP messages\n"
371 "| dns ... sends DNS messages\n"
372 "| rtp ... sends RTP datagrams\n"
373 "| syslog ... sends Syslog messages\n"
374 "|\n"
375 "| Of course you can build any other packet type 'manually' using the direct layer 2 mode.\n"
376 "| FYI: The interactive mode supports additional protocols. (Try mz -x <port>)\n"
377 "\n");
379 die();
382 // Purpose: Properly handle arguments and configure global structs (tx)
383 int getopts (int argc, char *argv[])
385 int i, c, rargs, RX=0, count_set=0, delay_set=0;
386 unsigned int time_factor;
387 char *packet_type=NULL, *mops_type=NULL;
388 char *dum;
389 unsigned char *dum1, *dum2;
390 bool do_help = false;
392 libnet_t *l;
393 char err_buf[LIBNET_ERRBUF_SIZE];
394 struct libnet_ether_addr *mymac;
396 FILE *afp;
397 char hexpld[MAX_PAYLOAD_SIZE*2];
398 int hexpld_specified=0;
400 opterr = 1; // let getopt print error message if necessary
403 while ((c = getopt(argc, argv, short_options)) != -1)
404 switch (c) {
405 case '4':
406 tx.eth_type = 0x0800;
407 ipv6_mode=0;
408 break;
409 case '6':
410 tx.eth_type = 0x86dd;
411 ipv6_mode=1;
412 break;
413 case 'h':
414 help();
415 break;
416 case 'q':
417 quiet=1;
418 break;
419 case 'v':
420 version();
421 break;
422 case 'V':
423 verbose++;
424 break;
425 case 'S':
426 simulate=1;
427 break;
428 case 'x':
429 mz_port = MZ_DEFAULT_PORT;
430 break;
431 case 'l':
432 strncpy (mz_listen_addr, optarg, sizeof(mz_listen_addr));
433 break;
434 case 'a':
435 strncpy (tx.eth_src_txt, optarg, 32);
436 tx.packet_mode = 0;
437 break;
438 case 'A':
439 strncpy (tx.ip_src_txt, optarg, sizeof(tx.ip_src_txt));
440 break;
441 case 'b':
442 strncpy (tx.eth_dst_txt, optarg, 32);
443 tx.packet_mode = 0;
444 break;
445 case 'B':
446 strncpy (tx.ip_dst_txt, optarg, sizeof(tx.ip_dst_txt));
447 break;
448 case 'c':
449 errno=0;
450 tx.count = strtol(optarg, (char **)NULL, 10);
451 if ((errno == ERANGE && (tx.count == LONG_MAX || tx.count == LONG_MIN))
452 || (errno != 0 && tx.count == 0)) {
453 perror("strtol");
454 return (-1);
456 if (tx.count<0) tx.count=1; //TODO: Allow count=0 which means infinity (need to update all send_functions)
457 count_set=1;
458 break;
459 case 'd':
460 errno=0;
461 // determine whether seconds or msecs are used
462 // default is usec!!!
463 time_factor=1;
464 if (exists(optarg,"s") || exists(optarg,"sec")) time_factor=1000000;
465 if (exists(optarg,"m") || exists(optarg,"msec")) time_factor=1000;
466 dum = strtok(optarg,"ms");
467 tx.delay = strtol(dum, (char **)NULL, 10) * time_factor;
468 if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN))
469 || (errno != 0 && tx.delay == 0)) {
470 perror("strtol");
471 return (-1);
473 if (tx.delay<0) tx.delay=0; // no delay
474 delay_set=1;
475 break;
476 case 'p':
477 errno=0;
478 tx.padding = strtol(optarg, (char **)NULL, 10);
479 if ((errno == ERANGE && (tx.padding == LONG_MAX || tx.padding == LONG_MIN))
480 || (errno != 0 && tx.padding == 0)) {
481 perror("strtol");
482 return (-1);
484 if (tx.padding>10000) {
485 fprintf(stderr, " Warning: Padding must not exceed 10000!\n");
486 return -1;
488 break;
489 case 't':
490 packet_type = optarg; // analyzed below
491 if (strcmp(packet_type,"help") == 0)
492 print_packet_types();
493 break;
494 case 'X':
495 mops_type = optarg; // MOPS TRANSITION STRATEGY -- analyzed below
496 break;
497 case 'T':
498 packet_type = optarg;
499 RX = 1;
500 break;
501 case 'r':
502 mz_rand = 1;
503 break;
504 case 'M':
505 if (strncmp(optarg,"help",4)==0) {
506 (void) get_mpls_params("help ");
508 else {
509 strncpy (tx.mpls_txt, optarg, 128);
510 tx.eth_type = ETHERTYPE_MPLS;
511 tx.packet_mode = 0;
512 tx.mpls=1;
514 break;
515 case 'P': // ASCII payload
516 strncpy((char*)tx.ascii_payload, optarg, MAX_PAYLOAD_SIZE);
517 tx.ascii = 1;
518 break;
519 case 'f': // ASCII payload in FILE
520 afp = fopen(optarg, "r");
521 if (fgets((char*)tx.ascii_payload, MAX_PAYLOAD_SIZE, afp) == NULL)
522 fprintf(stderr, " mz/getopts: File empty?\n");
523 fclose(afp);
524 tx.ascii = 1;
525 break;
526 case 'F': // HEX payload in FILE
527 afp = fopen(optarg, "r");
528 i=0;
529 while ( (hexpld[i]=fgetc(afp))!=EOF ) {
530 if (isspace(hexpld[i])) {
531 hexpld[i]=':';
533 i++;
535 hexpld[i]='\0';
536 fclose(afp);
537 hexpld_specified=1;
538 break;
539 case 'Q': // VLAN TAG
540 if (strncmp(optarg,"help",4)==0) {
541 print_dot1Q_help(); // ugly but most simple and safe solution
543 else {
544 strncpy (tx.dot1Q_txt, optarg, 32);
545 tx.dot1Q=1;
546 // determine number of VLAN tags
547 for (i=0; i<strlen(tx.dot1Q_txt); i++) {
548 if (tx.dot1Q_txt[i]==',') tx.dot1Q++;
550 tx.packet_mode = 0;
552 break;
553 case '?':
554 if ((optopt == 'a') || (optopt == 'b') || (optopt = 'c') ||
555 (optopt == 'd') || (optopt == 'f') || (optopt = 'p') ||
556 (optopt == 't') || (optopt == 'm'))
557 fprintf (stderr, " mz/getopts: Option -%c requires an argument.\n", optopt);
558 else if (isprint (optopt))
559 fprintf (stderr, " mz/getopts: Unknown option -%c'.\n", optopt);
560 else
561 fprintf (stderr, " mz/getopts: Unknown option character \\x%x'.\n", optopt);
562 return 1;
563 default:
564 fprintf (stderr," mz/getopts: Could not handle arguments properly!\n");
565 return 1;
568 // ********************************************
569 // Handle additional arguments
570 // ********************************************
572 // Greeting text
573 if (verbose) {
574 fprintf(stderr,"\n"
575 MAUSEZAHN_VERSION
576 "\n"
577 "Use at your own risk and responsibility!\n"
578 "-- Verbose mode --\n"
579 "\n");
582 if (argc<2) {
583 help();
586 if ((rargs=argc-optind)>2) { // number of remaining arguments
587 fprintf(stderr," mz/getopts: Too many arguments!\n");
588 return -1;
592 // There can be 0-2 additional arguments
593 switch (rargs) {
594 case 0:
595 if (lookupdev()) { // no device found
596 if (verbose) fprintf(stderr, " mz: no active interfaces found!\n");
597 strcpy(tx.device, "lo");
599 break;
600 case 1: // arg_string OR device given => find out!
601 if (__device_ifindex(argv[optind]) > 0) {
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 if (ipv6_mode) {
662 fprintf(stderr, "Option -A does not support 'bcast' when in IPv6 mode.\n");
663 return 1;
665 tx.ip_src = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
666 } else if (strcmp(tx.ip_src_txt, "rand") == 0) {
667 if (ipv6_mode) {
668 fprintf(stderr, "Option -A does not support 'rand' when in IPv6 mode.\n");
669 return 1;
671 tx.ip_src_rand = 1;
672 tx.ip_src_h = (u_int32_t) ( ((float) rand()/RAND_MAX)*0xE0000000); //this is 224.0.0.0
674 else if (ipv6_mode && (strstr(tx.ip_src_txt, ".") || strstr(tx.ip_dst_txt, "."))) {
675 fprintf(stderr, "You are using IPv6 mode but are providing IPv4 src/dst addresses.\n");
676 return 1;
678 else if (!ipv6_mode && (strstr(tx.ip_src_txt, ":") || strstr(tx.ip_dst_txt, ":"))) {
679 fprintf(stderr, "You are using IPv4 mode (the default) but are providing IPv6 src/dst addresses.\n");
680 fprintf(stderr, "Please use the -6 option for IPv6 mode.\n");
681 return 1;
683 else if (
684 (ipv6_mode && get_ip6_range_src(tx.ip_src_txt, l)) || // returns 1 when no range has been specified
685 (!ipv6_mode && get_ip_range_src(tx.ip_src_txt))
688 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
689 if (ipv6_mode)
690 tx.ip6_src = libnet_name2addr6 (l, tx.ip_src_txt, LIBNET_RESOLVE);
691 else
692 tx.ip_src = libnet_name2addr4 (l, tx.ip_src_txt, LIBNET_RESOLVE);
695 else { // no source IP specified: by default use own IP address
696 if (ipv6_mode) {
697 tx.ip6_src = libnet_get_ipaddr6(l);
698 if (strncmp((char*)&tx.ip6_src,(char*)&in6addr_error,sizeof(in6addr_error))==0)
699 printf("Failed to set source IPv6 address: %s", l->err_buf);
701 else
702 tx.ip_src = libnet_get_ipaddr4(l);
705 // Set destination IP address:
706 if (strlen(tx.ip_dst_txt)) { // option -B has been specified
707 if (mz_strcmp(tx.ip_dst_txt, "rand", 2)==0) {
708 fprintf(stderr, "Option -B does not support random destination IP addresses currently.\n");
709 return 1;
712 if (mz_strcmp(tx.ip_dst_txt, "bcast", 2)==0) {
713 if (ipv6_mode) {
714 fprintf(stderr, "Option -B does not support 'bcast' when in IPv6 mode.\n");
715 return 1;
717 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
718 } else if (
719 (ipv6_mode && get_ip6_range_dst(tx.ip_dst_txt, l)) || // returns 1 when no range has been specified
720 (!ipv6_mode && get_ip_range_dst(tx.ip_dst_txt)))
722 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
723 if (ipv6_mode)
724 tx.ip6_dst = libnet_name2addr6 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
725 else
726 tx.ip_dst = libnet_name2addr4 (l, tx.ip_dst_txt, LIBNET_RESOLVE);
729 else { // no destination IP specified: by default use broadcast
730 if (ipv6_mode) {
731 tx.ip6_dst = libnet_name2addr6 (l, "ff02::1", LIBNET_DONT_RESOLVE);
732 } else {
733 tx.ip_dst = libnet_name2addr4 (l, "255.255.255.255", LIBNET_DONT_RESOLVE);
737 // Initialize tx.ip_src_h and tx.ip_dst_h which are used by 'print_frame_details()'
738 // in verbose mode. See 'modifications.c'.
740 if (tx.ip_src_rand) { // ip_src_h already given, convert to ip_src
741 dum1 = (unsigned char*) &tx.ip_src_h;
742 dum2 = (unsigned char*) &tx.ip_src;
744 else { // ip_src already given, convert to ip_src_h
745 if (ipv6_mode) {
746 if (tx.ip_src_isrange) {
747 tx.ip6_src = tx.ip6_src_start;
749 } else {
750 dum1 = (unsigned char*) &tx.ip_src;
751 dum2 = (unsigned char*) &tx.ip_src_h;
755 if (ipv6_mode) {
756 if (tx.ip_dst_isrange) {
757 tx.ip6_dst = tx.ip6_dst_start;
759 } else {
760 *dum2 = *(dum1+3);
761 dum2++;
762 *dum2 = *(dum1+2);
763 dum2++;
764 *dum2 = *(dum1+1);
765 dum2++;
766 *dum2 = *dum1;
768 dum1 = (unsigned char*) &tx.ip_dst;
769 dum2 = (unsigned char*) &tx.ip_dst_h;
771 *dum2 = *(dum1+3);
772 dum2++;
773 *dum2 = *(dum1+2);
774 dum2++;
775 *dum2 = *(dum1+1);
776 dum2++;
777 *dum2 = *dum1;
780 libnet_destroy(l);
784 // END OF ADDRESS INITIALIZATION
786 //////////////////////////////////////////////////////////////////////////
789 ////// retrieve interface parameters ///////
791 for (i=0; i<device_list_entries; i++) {
792 get_dev_params(device_list[i].dev);
796 //////////////////////////////////////////////////////////////////////////
798 // Mausezahn CLI desired?
799 if (mz_port) {
800 // has port number been specified?
801 if (strlen(tx.arg_string)) {
802 mz_port = (int) str2int (tx.arg_string);
805 mz_cli_init();
806 cli();
809 //////////////////////////////////////////////////////////////////////////
811 // Mode decision
813 // Consider -t and -m option (used exclusively)
814 // -t => special packet types, stateless
816 // If -t not present then evaluate arg_string which must
817 // contain a byte-string in hexadecimal notation.
821 // ***** NEW: MOPS TRANSITION STRATEGY *****
822 if (mops_type != NULL) {
824 if (mz_strcmp(mops_type,"lldp",4)==0) {
825 mops_direct(tx.device, MOPS_LLDP, tx.arg_string);
830 if (packet_type == NULL) { // raw hex string given
831 mode = BYTE_STREAM;
833 else if (strcmp(packet_type,"arp")==0) {
834 mode = ARP;
836 else if (strcmp(packet_type,"bpdu")==0) {
837 mode = BPDU;
839 else if (strcmp(packet_type,"ip")==0) {
840 mode = IP;
842 else if (strcmp(packet_type,"udp")==0) {
843 mode = UDP;
845 else if (strcmp(packet_type,"icmp")==0) {
846 mode = ICMP;
848 else if (strcmp(packet_type,"icmp6")==0) {
849 mode = ICMP6;
851 else if (strcmp(packet_type,"tcp")==0) {
852 mode = TCP;
854 else if (strcmp(packet_type,"dns")==0) {
855 mode = DNS;
857 else if (strcmp(packet_type,"cdp")==0) {
858 mode = CDP;
860 else if (strcmp(packet_type,"syslog")==0) {
861 mode = SYSLOG;
863 else if (strcmp(packet_type, "igmp") == 0) {
864 mode = IGMP;
866 else if (strcmp(packet_type,"lldp")==0) {
867 mode = LLDP;
868 tx.packet_mode=0; // create whole frame by ourself
870 else if (strcmp(packet_type,"rtp")==0) {
871 if (RX) {
872 mode = RX_RTP;
874 else {
875 mode = RTP;
876 if (!count_set) tx.count = 0;
877 if (!delay_set) tx.delay = 20000; // 20 msec inter-packet delay for RTP
880 else {
881 fprintf(stderr, " mz: you must specify a valid packet type!\n");
885 //////////////////////////////////////////////////////////////////////////
887 // TODO: Implement macro support
888 // Check macro types here
890 return 0;
893 int main(int argc, char **argv)
895 // These handles are only used when creating L3 and above packets.
896 libnet_t *l; // the context
897 libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers
898 double cpu_time_used;
900 reset();
902 if ( getopts(argc, argv) )
904 (void) fprintf(stderr, "Invalid command line parameters!\n");
905 help();
908 // Check whether hires timers are supported or not:
909 (void) check_timer();
911 signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT
913 switch (mode)
915 case BYTE_STREAM:
916 send_eth();
917 break;
919 case ARP:
920 (void) send_arp();
921 break;
923 case BPDU:
924 (void) send_bpdu();
925 break;
927 case CDP:
928 (void) send_cdp();
929 break;
931 case IP: // From now on a new much more modular method is used:
932 l = get_link_context();
933 t3 = create_ip_packet(l); // t3 can be used for later header changes
934 if (!quiet) complexity();
935 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
936 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
937 else
938 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
939 break;
941 case ICMP:
942 tx.ip_proto = 1;
943 l = get_link_context();
944 t4 = create_icmp_packet(l); // t4 can be used for later header changes
945 t3 = create_ip_packet(l); // t3 can be used for later header changes
946 if (!quiet) complexity();
947 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
948 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
949 else
950 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
951 break;
953 case ICMP6:
954 tx.ip_proto = 58;
955 l = get_link_context();
956 t4 = create_icmp6_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 UDP:
966 tx.ip_proto = 17;
967 l = get_link_context();
968 t4 = create_udp_packet(l); // t4 can be used for later header changes
969 t3 = create_ip_packet(l); // t3 can be used for later header changes
970 if (!quiet) complexity();
971 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
972 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
973 else
974 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
975 break;
977 case TCP:
978 tx.ip_proto = 6;
979 l = get_link_context();
980 t4 = create_tcp_packet(l); // t4 can be used for later header changes
981 t3 = create_ip_packet(l); // t3 can be used for later header changes
982 if (!quiet) complexity();
983 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
984 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
985 else
986 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
987 break;
989 case IGMP:
990 tx.ip_proto = 2;
991 l = get_link_context();
992 t4 = create_igmp_packet(l);
993 /* t3 can be used for later header changes */
994 t3 = create_ip_packet(l);
995 if (!quiet)
996 complexity();
998 /* Ethernet manipulation features does NOT use ARP to determine eth_dst
999 * */
1000 if (tx.packet_mode == 0)
1001 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
1002 else
1003 send_frame(l, t3, t4); // NOTE: send_frame also destroys context finaly
1004 break;
1006 case DNS:
1007 tx.ip_proto = 17;
1008 l = get_link_context();
1009 (void) create_dns_packet();
1010 t4 = create_udp_packet(l); // t4 can be used for later header changes
1011 t3 = create_ip_packet(l); // t3 can be used for later header changes
1012 if (!quiet) complexity();
1013 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1014 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
1015 else
1016 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
1017 break;
1019 case RTP:
1020 tx.ip_proto = 17;
1021 l = get_link_context();
1022 if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay);
1023 (void) create_rtp_packet();
1024 t4 = create_udp_packet(l); // t4 can be used for later header changes
1025 t3 = create_ip_packet(l); // t3 can be used for later header changes
1026 if (!quiet) complexity();
1027 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1028 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
1029 else
1030 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
1031 break;
1033 case RX_RTP: // Receive RTP packets
1034 rcv_rtp_init();
1035 rcv_rtp();
1036 break;
1038 case SYSLOG:
1039 tx.ip_proto = 17;
1040 l = get_link_context();
1041 (void) create_syslog_packet();
1042 t4 = create_udp_packet(l); // t4 can be used for later header changes
1043 t3 = create_ip_packet(l); // t3 can be used for later header changes
1044 if (!quiet) complexity();
1046 if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1047 t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
1048 else
1049 send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
1050 break;
1052 case LLDP: // start with a new concept here
1053 //l = get_link_context();
1054 //(void) create_lldp_packet();
1055 // // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
1056 fprintf(stderr, "LLDP is currently only supported via the interactive mode\n");
1057 exit(1);
1058 break;
1061 default:
1062 (void) fprintf(stderr," mz/main: unknown mode! Stop.\n");
1063 return (1);
1066 if (!quiet)
1068 mz_stop = clock();
1069 cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC;
1070 if (cpu_time_used > 0)
1072 total_d /= cpu_time_used;
1073 fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d);
1075 else
1077 fprintf(stderr, "\n");
1081 return(0);