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.
10 #include <pcap/pcap.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>
35 int verbose_level
= 0;
37 static const char *short_options
= "46hqvVSxra:A:b:B:c:d:E:f:F:l:p:P:R:t:T:M:Q:X:";
39 static void signal_handler(int number
)
44 void clean_up(int sig
)
47 struct arp_table_struct
*cur
, *next
;
49 if (!quiet
) fprintf(stderr
, "\nMausezahn cleans up...\n");
52 verbose_l1(" close files (1) ...\n");
59 if (verbose
) fprintf(stderr
, " close files (2) ...\n");
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
);
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
;
89 if (cur
!=NULL
) free(cur
);
94 // close packet sockets
95 if (device_list
[i
].ps
>=0) {
96 close(device_list
[i
].ps
);
101 if (verbose
) fprintf(stderr
, "finished.\n");
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"
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"
116 " -R <PRIO> Set socket priority\n"
117 " -c <count> Send packet count times, default:1, infinite:0\n"
118 " -d <delay> Apply delay between transmissions. The delay value can be\n"
119 " specified in usec (default, no additional unit needed), or in\n"
120 " msec (e.g. 100m or 100msec), or in seconds (e.g. 100s or 100sec)\n"
121 " -r Multiplies the specified delay with a random value\n"
122 " -p <length> Pad the raw frame to specified length (using random bytes)\n"
123 " -a <srcmac|keyword> Use specified source mac address, no matter what has\n"
124 " been specified with other arguments; keywords see below,\n"
125 " Default is own interface\n"
126 " -b <dstmac|keyword> Same with destination mac address; keywords:\n"
127 " rand Use a random MAC address\n"
128 " bc Use a broadcast MAC address\n"
129 " own Use own interface MAC address (default for source MAC)\n"
130 " stp Use IEEE 802.1d STP multicast address\n"
131 " cisco Use Cisco multicast address as used for CDP, VTP, or PVST+\n"
132 " -A <srcip> Use specified source IP address (default is own interface IP)\n"
133 " -B <dstip|dnsname> Send packet to specified destination IP or domain name\n"
134 " -P <ascii payload> Use the specified ASCII payload\n"
135 " -f <filename> Read the ASCII payload from a file\n"
136 " -F <filename> Read the hexadecimal payload from a file\n"
137 " -Q <[CoS:]vlan> Specify 802.1Q VLAN tag and optional Class of Service, you can\n"
138 " specify multiple 802.1Q VLAN tags (QinQ...) by separating them\n"
139 " via a comma or a period (e.g. '5:10,20,2:30')\n"
140 " -t <packet-type|help> Specify packet type for autobuild (you don't need to care for\n"
141 " encapsulations in lower layers, most packet types allow/require\n"
142 " additional packet-specific arguments in an <arg-string>;\n"
143 " Currently supported types: arp, bpdu, cdp, ip, icmp, udp, tcp,\n"
144 " dns, rtp, syslog, lldp and more;\n"
145 " For context-help use 'help' as <arg-string>!\n"
146 " -T <packet-type> Specify packet type for server mode, currently only rtp is supported;\n"
147 " Enter -T help or -T rtp help for further information\n"
148 " -M <MPLS-label> Insert a MPLS label, enter '-M help' for a syntax description\n"
149 " -V|VV|... Verbose and more verbose mode\n"
150 " -q Quiet mode, even omit 'important' standard short messages\n"
151 " -S Simulation mode: DOES NOT put anything on the wire, this is\n"
152 " typically combined with one of the verbose modes (v or V)\n"
154 " -h Print this help\n\n"
157 " mausezahn -c 0 -d 2s -t bpdu conf\n"
158 " mausezahn -t cdp change -c 0\n"
159 " mausezahn -t syslog sev=3 -P \"You have been mausezahned.\" -A 10.1.1.109 -B 192.168.7.7\n"
160 " mausezahn eth0 -A rand -B 1.1.1.1 -c 0 -t tcp \"dp=1-1023, flags=syn\"\n\n"
162 " This tool is targeted for network developers! You should\n"
163 " be aware of what you are doing and what these options above\n"
164 " mean! Only use this tool in an isolated LAN that you own!\n\n"
165 "Please report bugs to <bugs@netsniff-ng.org>\n"
166 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
167 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
168 "Swiss federal institute of technology (ETH Zurich)\n"
169 "License: GNU GPL version 2.0\n"
170 "This is free software: you are free to change and redistribute it.\n"
171 "There is NO WARRANTY, to the extent permitted by law.\n");
175 static void version(void)
177 printf("\nmausezahn %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
178 puts("a fast versatile traffic generator\n"
179 "http://www.netsniff-ng.org\n\n"
180 "Please report bugs to <bugs@netsniff-ng.org>\n"
181 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
182 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
183 "Swiss federal institute of technology (ETH Zurich)\n"
184 "License: GNU GPL version 2.0\n"
185 "This is free software: you are free to change and redistribute it.\n"
186 "There is NO WARRANTY, to the extent permitted by law.\n");
195 // Determine platform type sizes:
196 MZ_SIZE_LONG_INT
= sizeof(long int);
198 mz_default_config_path
[0] = 0x00;
199 mz_default_log_path
[0] = 0x00;
209 gind_max
= TIME_COUNT
;
214 char mz_listen_addr
[16] = "0.0.0.0";
217 for (i
=0;i
<TIME_COUNT_MAX
;i
++) jitter
[i
] = 0;
219 time0_flag
= 0; // If set then time0 has valid data
220 sqnr0_flag
= 0; // If set then sqnr_last and sqnr_next has valid data
222 mz_ssrc
[0]=0; mz_ssrc
[1]=0; mz_ssrc
[2]=0; mz_ssrc
[3]=0;
224 // Reset mgmt parameters of TX:
225 tx
.packet_mode
= 1; // assume we don't care about L2
227 tx
.delay
= DEFAULT_DELAY
;
229 tx
.arg_string
[0] = '\0';
231 // Reset Ethernet parameters of TX:
232 tx
.eth_params_already_set
= 0;
233 for (i
=0; i
<6; i
++) tx
.eth_dst
[i
] = 0xff;
234 for (i
=0; i
<6; i
++) tx
.eth_src
[i
] = 0; // TODO: Get own MAC !!!
235 tx
.eth_dst_txt
[0] = '\0';
236 tx
.eth_src_txt
[0] = '\0';
242 tx
.eth_payload
[0] = '\0';
243 tx
.eth_payload_s
= 0;
246 // Reset CDP parameters for TX:
250 tx
.cdp_payload
[0] = '\0';
251 tx
.cdp_payload_s
= 0;
252 tx
.cdp_tlv_id
[0] = '\0';
253 tx
.cdp_tlv_id_len
= 0;
255 // Reset 802.1Q parameters of TX:
257 tx
.dot1Q_txt
[0] = '\0';
260 tx
.ascii
= 0; // 1 if specified
261 tx
.ascii_payload
[0]= '\0';
264 tx
.hex_payload_s
= 0;
266 // Reset MPLS parameters of TX:
268 tx
.mpls_txt
[0] = '\0';
273 tx
.mpls_verbose_string
[0] = '\0';
275 // Reset IP parameters of TX:
276 tx
.ip_src_txt
[0] = '\0';
278 tx
.ip_dst_txt
[0] = '\0';
279 tx
.ip_src_isrange
= 0;
282 memset(&tx
.ip6_src_start
, 0, sizeof(tx
.ip6_src_start
));
283 memset(&tx
.ip6_src_stop
, 0, sizeof(tx
.ip6_src_stop
));
287 memset(&tx
.ip6_dst_start
, 0, sizeof(tx
.ip6_dst_start
));
288 memset(&tx
.ip6_dst_stop
, 0, sizeof(tx
.ip6_dst_stop
));
289 tx
.ip_dst_isrange
= 0;
293 tx
.ip_payload
[0]= '\0';
295 tx
.ip_option
[0]= '\0';
298 // Reset ICMP parameters:
301 tx
.icmp_chksum
=0; // 0=autofill
306 // Reset general L4 parameters:
316 // Reset UDP parameters of TX:
318 tx
.udp_len
= 0; // If set to zero then create_udp_packet will calculate it
320 tx
.udp_payload
[0] = '\0';
321 tx
.udp_payload_s
= 0;
323 // Reset TCP parameters of TX:
326 tx
.tcp_seq_stop
= 42;
327 tx
.tcp_seq_delta
= 0; // also used as 'isrange' meaning
333 tx
.tcp_len
= 20; // Least size (TCP header only)
334 tx
.tcp_payload
[0] = '\0';
335 tx
.tcp_payload_s
= 0;
337 // Reset RTP parameters of TX:
341 // Initialize random generator
343 srand((unsigned int)t
);
347 for (i
=0; i
<MZ_MAX_DEVICES
; i
++) {
348 device_list
[i
].arprx_thread
= 0;
349 device_list
[i
].p_arp
= NULL
;
350 device_list
[i
].arp_table
= NULL
;
351 device_list
[i
].ps
=-1;
352 device_list
[i
].cli
=0;
353 device_list
[i
].mgmt_only
=0;
359 static void print_packet_types(void)
364 "| The following packet types are currently implemented:\n"
366 "| arp ... sends ARP packets\n"
367 "| bpdu ... sends BPDU packets (STP or PVST+)\n"
368 "| cdp ... sends CDP messages\n"
369 "| ip ... sends IPv4 packets\n"
370 "| udp ... sends UDP datagrams\n"
371 "| tcp ... sends TCP segments\n"
372 "| icmp ... sends ICMP messages\n"
373 "| igmp ... sends IGMP messages\n"
374 "| dns ... sends DNS messages\n"
375 "| rtp ... sends RTP datagrams\n"
376 "| syslog ... sends Syslog messages\n"
378 "| Of course you can build any other packet type 'manually' using the direct layer 2 mode.\n"
379 "| FYI: The interactive mode supports additional protocols. (Try mz -x <port>)\n"
385 // Purpose: Properly handle arguments and configure global structs (tx)
386 int getopts (int argc
, char *argv
[])
388 int i
, c
, rargs
, RX
=0, count_set
=0, delay_set
=0;
389 unsigned int time_factor
;
390 char *packet_type
=NULL
, *mops_type
=NULL
;
392 unsigned char *dum1
, *dum2
;
393 bool do_help
= false;
396 char err_buf
[LIBNET_ERRBUF_SIZE
];
397 struct libnet_ether_addr
*mymac
;
400 char hexpld
[MAX_PAYLOAD_SIZE
*2];
401 int hexpld_specified
=0;
406 opterr
= 1; // let getopt print error message if necessary
409 while ((c
= getopt(argc
, argv
, short_options
)) != -1)
412 tx
.eth_type
= 0x0800;
416 tx
.eth_type
= 0x86dd;
421 prio
= strtol(optarg
, NULL
, 0);
423 perror("Couldn't parse priority");
426 if (prio
< 0 || prio
> 0xffffffff) {
427 perror("Invalid priority value");
448 mz_port
= MZ_DEFAULT_PORT
;
451 strncpy (mz_listen_addr
, optarg
, sizeof(mz_listen_addr
));
454 strncpy (tx
.eth_src_txt
, optarg
, 32);
458 strncpy (tx
.ip_src_txt
, optarg
, sizeof(tx
.ip_src_txt
));
461 strncpy (tx
.eth_dst_txt
, optarg
, 32);
465 strncpy (tx
.ip_dst_txt
, optarg
, sizeof(tx
.ip_dst_txt
));
469 tx
.count
= strtol(optarg
, (char **)NULL
, 10);
470 if ((errno
== ERANGE
&& (tx
.count
== LONG_MAX
|| tx
.count
== LONG_MIN
))
471 || (errno
!= 0 && tx
.count
== 0)) {
475 if (tx
.count
<0) tx
.count
=1; //TODO: Allow count=0 which means infinity (need to update all send_functions)
482 unit
='u'; // default is usecs
483 if (sscanf(optarg
, "%ld%c", &delay
, &unit
) == EOF
) {
488 fprintf(stderr
, " Incorrect delay format\n");
491 if (unit
== 's') time_factor
=1000000; // seconds
492 else if (unit
== 'm') time_factor
=1000; // msecs
493 else if (unit
== 'u') time_factor
=1; // usecs
495 fprintf(stderr
, " Incorrect delay format\n");
498 tx
.delay
= delay
* time_factor
;
499 if ((errno
== ERANGE
&& (tx
.delay
== LONG_MAX
|| tx
.delay
== LONG_MIN
))
500 || (errno
!= 0 && tx
.delay
== 0)) {
504 if (tx
.delay
<0) tx
.delay
=0; // no delay
509 tx
.padding
= strtol(optarg
, (char **)NULL
, 10);
510 if ((errno
== ERANGE
&& (tx
.padding
== LONG_MAX
|| tx
.padding
== LONG_MIN
))
511 || (errno
!= 0 && tx
.padding
== 0)) {
515 if (tx
.padding
>10000) {
516 fprintf(stderr
, " Warning: Padding must not exceed 10000!\n");
521 packet_type
= optarg
; // analyzed below
522 if (strcmp(packet_type
,"help") == 0)
523 print_packet_types();
526 mops_type
= optarg
; // MOPS TRANSITION STRATEGY -- analyzed below
529 packet_type
= optarg
;
536 if (strncmp(optarg
,"help",4)==0) {
537 (void) get_mpls_params("help ");
540 strncpy (tx
.mpls_txt
, optarg
, 128);
541 tx
.eth_type
= ETHERTYPE_MPLS
;
546 case 'P': // ASCII payload
547 strncpy((char*)tx
.ascii_payload
, optarg
, MAX_PAYLOAD_SIZE
);
550 case 'f': // ASCII payload in FILE
551 afp
= fopen(optarg
, "r");
553 fprintf(stderr
, " mz/getopts: Can not open file %s. %s!\n", optarg
, strerror(errno
));
556 if (fgets((char*)tx
.ascii_payload
, MAX_PAYLOAD_SIZE
, afp
) == NULL
)
557 fprintf(stderr
, " mz/getopts: File empty?\n");
561 case 'F': // HEX payload in FILE
562 afp
= fopen(optarg
, "r");
564 fprintf(stderr
, " mz/getopts: Can not open file %s. %s!\n", optarg
, strerror(errno
));
568 while ( (hexpld
[i
]=fgetc(afp
))!=EOF
) {
569 if (isspace(hexpld
[i
])) {
578 case 'Q': // VLAN TAG
579 if (strncmp(optarg
,"help",4)==0) {
580 print_dot1Q_help(); // ugly but most simple and safe solution
583 strncpy (tx
.dot1Q_txt
, optarg
, 32);
585 // determine number of VLAN tags
586 for (i
=0; i
<strlen(tx
.dot1Q_txt
); i
++) {
587 if (tx
.dot1Q_txt
[i
]==',') tx
.dot1Q
++;
593 if ((optopt
== 'a') || (optopt
== 'b') || (optopt
= 'c') ||
594 (optopt
== 'd') || (optopt
== 'f') || (optopt
= 'p') ||
595 (optopt
== 't') || (optopt
== 'm'))
596 fprintf (stderr
, " mz/getopts: Option -%c requires an argument.\n", optopt
);
597 else if (isprint (optopt
))
598 fprintf (stderr
, " mz/getopts: Unknown option -%c'.\n", optopt
);
600 fprintf (stderr
, " mz/getopts: Unknown option character \\x%x'.\n", optopt
);
603 fprintf (stderr
," mz/getopts: Could not handle arguments properly!\n");
607 // ********************************************
608 // Handle additional arguments
609 // ********************************************
616 "Use at your own risk and responsibility!\n"
617 "-- Verbose mode --\n"
625 if ((rargs
=argc
-optind
)>2) { // number of remaining arguments
626 fprintf(stderr
," mz/getopts: Too many arguments!\n");
631 // There can be 0-2 additional arguments
634 if (lookupdev()) { // no device found
635 if (verbose
) fprintf(stderr
, " mz: no active interfaces found!\n");
636 strcpy(tx
.device
, "lo");
639 case 1: // arg_string OR device given => find out!
640 if (__device_ifindex(argv
[optind
]) > 0) {
641 strncpy(tx
.device
, argv
[optind
], 16);
643 else { /// arg_string given => no device has been specified -- let's find one!
644 strncpy (tx
.arg_string
, argv
[optind
], MAX_PAYLOAD_SIZE
);
645 do_help
= !!getarg(tx
.arg_string
,"help", NULL
);
648 /* no device found */
650 fprintf(stderr
, " mz: no active interfaces found!\n");
651 strcpy(tx
.device
, "lo");
654 fprintf(stderr
," mz: device not given, will use %s\n",tx
.device
);
658 case 2: // both device and arg_string given
659 strncpy (tx
.device
, argv
[optind
], 16);
660 strncpy (tx
.arg_string
, argv
[optind
+1], MAX_PAYLOAD_SIZE
);
663 fprintf(stderr
," mz/getopts: Unknown argument problem!\n");
667 if (hexpld_specified
) {
668 strcat(tx
.arg_string
, ",p=");
669 strcat(tx
.arg_string
, hexpld
);
673 //////////////////////////////////////////////////////////////////////////
675 // Initialize MAC and IP Addresses.
677 // - tx.eth_src = own interface MAC
678 // - tx.ip_src = own interface IP or user specified
679 // - tx.ip_dst = 255.255.255.255 or user specified (can be a range)
680 // - tx.ip_src_rand ... is set if needed.
683 // Get own device MAC address:
684 // Don't open context if only a help text is requested
685 if (!do_help
&& getarg(tx
.arg_string
,"help", NULL
) !=1) {
686 l
= libnet_init (LIBNET_LINK_ADV
, tx
.device
, err_buf
);
688 fprintf(stderr
, " mz/getopts: libnet_init() failed (%s)", err_buf
);
691 mymac
= libnet_get_hwaddr(l
);
692 for (i
=0; i
<6; i
++) {
693 tx
.eth_src
[i
] = mymac
->ether_addr_octet
[i
];
694 tx
.eth_mac_own
[i
] = mymac
->ether_addr_octet
[i
];
697 // Set source IP address:
698 if (strlen(tx
.ip_src_txt
)) { // option -A has been specified
699 if (mz_strcmp(tx
.ip_src_txt
, "bcast", 2)==0) {
701 fprintf(stderr
, "Option -A does not support 'bcast' when in IPv6 mode.\n");
704 tx
.ip_src
= libnet_name2addr4 (l
, "255.255.255.255", LIBNET_DONT_RESOLVE
);
705 } else if (strcmp(tx
.ip_src_txt
, "rand") == 0) {
707 fprintf(stderr
, "Option -A does not support 'rand' when in IPv6 mode.\n");
711 tx
.ip_src_h
= (u_int32_t
) ( ((float) rand()/RAND_MAX
)*0xE0000000); //this is 224.0.0.0
714 (ipv6_mode
&& get_ip6_range_src(tx
.ip_src_txt
, l
)) || // returns 1 when no range has been specified
715 (!ipv6_mode
&& get_ip_range_src(tx
.ip_src_txt
))
718 // name2addr{4,6} accepts a valid IP address or a FQDN:
720 tx
.ip6_src
= libnet_name2addr6(l
, tx
.ip_src_txt
, LIBNET_RESOLVE
);
721 if (libnet_in6_is_error(tx
.ip6_src
)) {
722 /* libnet_in6_is_error returns 1 for the valid IPv6 address
723 * ffff:ffff:ffff:ffff:ffff:ffff. Use an additional inet_pton()
724 * check to cover cases where this address is specified
727 struct in6_addr src_check
;
728 if (inet_pton(AF_INET6
, tx
.ip_src_txt
, &src_check
) != 1) {
729 fprintf(stderr
, "Failed to set source"
730 " IPv6 address. Please check if"
731 " source is set to a valid IPv6 address.\n");
736 tx
.ip_src
= libnet_name2addr4(l
, tx
.ip_src_txt
, LIBNET_RESOLVE
);
737 if (tx
.ip_src
== -1) {
738 /* libnet_name2addr4() returns -1 for the valid address 255.255.255.255.
739 * Use an additional inet_pton() check to cover case where this address
740 * is specified as source
742 struct in_addr src_check
;
743 if (inet_pton(AF_INET
, tx
.ip_src_txt
, &src_check
) != 1) {
744 fprintf(stderr
, "Failed to set source"
745 " IPv4 address. Please check if"
746 " source is set to a valid IPv4 address.\n");
753 // no source IP specified: by default use own IP address
755 tx
.ip6_src
= libnet_get_ipaddr6(l
);
756 if (strncmp((char*)&tx
.ip6_src
,(char*)&in6addr_error
, sizeof(in6addr_error
))==0)
757 printf("Failed to set source IPv6 address: %s", l
->err_buf
);
760 tx
.ip_src
= libnet_get_ipaddr4(l
);
763 // Set destination IP address:
764 if (strlen(tx
.ip_dst_txt
)) { // option -B has been specified
765 if (mz_strcmp(tx
.ip_dst_txt
, "rand", 2)==0) {
766 fprintf(stderr
, "Option -B does not support random destination IP addresses currently.\n");
770 if (mz_strcmp(tx
.ip_dst_txt
, "bcast", 2)==0) {
772 fprintf(stderr
, "Option -B does not support 'bcast' when in IPv6 mode.\n");
775 tx
.ip_dst
= libnet_name2addr4 (l
, "255.255.255.255", LIBNET_DONT_RESOLVE
);
777 (ipv6_mode
&& get_ip6_range_dst(tx
.ip_dst_txt
, l
)) || // returns 1 when no range has been specified
778 (!ipv6_mode
&& get_ip_range_dst(tx
.ip_dst_txt
)))
780 // name2addr{4, 6} accepts a valid IP address or a FQDN:
782 tx
.ip6_dst
= libnet_name2addr6(l
, tx
.ip_dst_txt
, LIBNET_RESOLVE
);
783 if (libnet_in6_is_error(tx
.ip6_dst
)) {
784 /* libnet_in6_is_error returns 1 for the valid IPv6 address
785 * ffff:ffff:ffff:ffff:ffff:ffff. Use an additional inet_pton()
786 * check to cover cases where this address is specified
789 struct in6_addr dst_check
;
790 if (inet_pton(AF_INET6
, tx
.ip_dst_txt
, &dst_check
) != 1) {
791 fprintf(stderr
, "Failed to set destination"
792 " IPv6 address. Please check if"
793 " source is set to a valid IPv6 address.\n");
798 tx
.ip_dst
= libnet_name2addr4(l
, tx
.ip_dst_txt
, LIBNET_RESOLVE
);
799 if (tx
.ip_dst
== -1) {
800 /* libnet_name2addr4() returns -1 for the valid address 255.255.255.255.
801 * Use an additional inet_pton() check to cover case where this address
802 * is specified as destination
804 struct in_addr dst_check
;
805 if (inet_pton(AF_INET
, tx
.ip_dst_txt
, &dst_check
) != 1) {
806 fprintf(stderr
, "Failed to set destination"
807 " IPv4 address. Please check if"
808 " destination is set to a valid IPv4 address.\n");
816 else { // no destination IP specified: by default use broadcast
818 tx
.ip6_dst
= libnet_name2addr6 (l
, "ff02::1", LIBNET_DONT_RESOLVE
);
820 tx
.ip_dst
= libnet_name2addr4 (l
, "255.255.255.255", LIBNET_DONT_RESOLVE
);
824 // Initialize tx.ip_src_h and tx.ip_dst_h which are used by 'print_frame_details()'
825 // in verbose mode. See 'modifications.c'.
827 if (tx
.ip_src_rand
) { // ip_src_h already given, convert to ip_src
828 dum1
= (unsigned char*) &tx
.ip_src_h
;
829 dum2
= (unsigned char*) &tx
.ip_src
;
831 else { // ip_src already given, convert to ip_src_h
833 if (tx
.ip_src_isrange
) {
834 tx
.ip6_src
= tx
.ip6_src_start
;
837 dum1
= (unsigned char*) &tx
.ip_src
;
838 dum2
= (unsigned char*) &tx
.ip_src_h
;
843 if (tx
.ip_dst_isrange
) {
844 tx
.ip6_dst
= tx
.ip6_dst_start
;
855 dum1
= (unsigned char*) &tx
.ip_dst
;
856 dum2
= (unsigned char*) &tx
.ip_dst_h
;
871 // END OF ADDRESS INITIALIZATION
873 //////////////////////////////////////////////////////////////////////////
876 ////// retrieve interface parameters ///////
878 for (i
=0; i
<device_list_entries
; i
++) {
879 get_dev_params(device_list
[i
].dev
);
883 //////////////////////////////////////////////////////////////////////////
885 // Mausezahn CLI desired?
887 // has port number been specified?
888 if (strlen(tx
.arg_string
)) {
889 mz_port
= (int) str2int (tx
.arg_string
);
896 //////////////////////////////////////////////////////////////////////////
900 // Consider -t and -m option (used exclusively)
901 // -t => special packet types, stateless
903 // If -t not present then evaluate arg_string which must
904 // contain a byte-string in hexadecimal notation.
908 // ***** NEW: MOPS TRANSITION STRATEGY *****
909 if (mops_type
!= NULL
) {
911 if (mz_strcmp(mops_type
,"lldp",4)==0) {
912 mops_direct(tx
.device
, MOPS_LLDP
, tx
.arg_string
);
917 if (packet_type
== NULL
) { // raw hex string given
920 else if (strcmp(packet_type
,"arp")==0) {
923 else if (strcmp(packet_type
,"bpdu")==0) {
926 else if (strcmp(packet_type
,"ip")==0) {
929 else if (strcmp(packet_type
,"udp")==0) {
932 else if (strcmp(packet_type
,"icmp")==0) {
935 else if (strcmp(packet_type
,"icmp6")==0) {
938 else if (strcmp(packet_type
,"tcp")==0) {
941 else if (strcmp(packet_type
,"dns")==0) {
944 else if (strcmp(packet_type
,"cdp")==0) {
947 else if (strcmp(packet_type
,"syslog")==0) {
950 else if (strcmp(packet_type
, "igmp") == 0) {
953 else if (strcmp(packet_type
,"lldp")==0) {
955 tx
.packet_mode
=0; // create whole frame by ourself
957 else if (strcmp(packet_type
,"rtp")==0) {
963 if (!count_set
) tx
.count
= 0;
964 if (!delay_set
) tx
.delay
= 20000; // 20 msec inter-packet delay for RTP
968 fprintf(stderr
, " mz: you must specify a valid packet type!\n");
972 //////////////////////////////////////////////////////////////////////////
974 // TODO: Implement macro support
975 // Check macro types here
980 int main(int argc
, char **argv
)
982 // These handles are only used when creating L3 and above packets.
983 libnet_t
*l
; // the context
984 libnet_ptag_t t2
=0, t3
=0, t4
=0; // handles to layers
985 double cpu_time_used
;
989 if ( getopts(argc
, argv
) )
991 (void) fprintf(stderr
, " Invalid command line parameters!\n");
995 // Check whether hires timers are supported or not:
996 (void) check_timer();
998 signal(SIGINT
, signal_handler
); // to close all file pointers etc upon SIGINT
1018 case IP
: // From now on a new much more modular method is used:
1019 l
= get_link_context();
1020 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1021 if (!quiet
) complexity();
1022 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1023 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1025 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1030 l
= get_link_context();
1031 t4
= create_icmp_packet(l
); // t4 can be used for later header changes
1032 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1033 if (!quiet
) complexity();
1034 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1035 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1037 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1042 l
= get_link_context();
1043 t4
= create_icmp6_packet(l
); // t4 can be used for later header changes
1044 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1045 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
1049 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1054 l
= get_link_context();
1055 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1056 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1057 if (!quiet
) complexity();
1058 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1059 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1061 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1066 l
= get_link_context();
1067 t4
= create_tcp_packet(l
); // t4 can be used for later header changes
1068 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1069 if (!quiet
) complexity();
1070 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1071 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1073 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1078 l
= get_link_context();
1079 t4
= create_igmp_packet(l
);
1080 /* t3 can be used for later header changes */
1081 t3
= create_ip_packet(l
);
1085 /* Ethernet manipulation features does NOT use ARP to determine eth_dst
1087 if (tx
.packet_mode
== 0)
1088 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1090 send_frame(l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1095 l
= get_link_context();
1096 (void) create_dns_packet();
1097 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1098 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1099 if (!quiet
) complexity();
1100 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1101 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1103 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1108 l
= get_link_context();
1109 if (!quiet
) fprintf(stderr
, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx
.count
, tx
.delay
);
1110 (void) create_rtp_packet();
1111 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1112 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1113 if (!quiet
) complexity();
1114 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1115 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1117 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1120 case RX_RTP
: // Receive RTP packets
1127 l
= get_link_context();
1128 (void) create_syslog_packet();
1129 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1130 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1131 if (!quiet
) complexity();
1133 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1134 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1136 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1139 case LLDP
: // start with a new concept here
1140 //l = get_link_context();
1141 //(void) create_lldp_packet();
1142 // // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
1143 fprintf(stderr
, "LLDP is currently only supported via the interactive mode\n");
1149 (void) fprintf(stderr
," mz/main: unknown mode! Stop.\n");
1156 cpu_time_used
= ((double) (mz_stop
- mz_start
)) / CLOCKS_PER_SEC
;
1157 if (cpu_time_used
> 0)
1159 total_d
/= cpu_time_used
;
1160 fprintf(stderr
, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used
,total_d
);
1164 fprintf(stderr
, "\n");