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 enum operating_modes mode
;
38 int quiet
; // don't even print 'important standard short messages'
39 int verbose
; // report character
40 int simulate
; // if 1 then don't really send frames
44 FILE *fp
, *fp2
; // global multipurpose file pointer
47 clock_t mz_start
, mz_stop
;
53 jitter
[TIME_COUNT_MAX
];
57 time0_flag
, // If set then time0 has valid data
61 mz_ssrc
[4]; // holds RTP stream identifier for rcv_rtp()
69 gind
, // a global index to run through deltaRX, deltaTX, and jitter
70 gind_max
; // the amount of entries used in the (ugly oversized) arrays; per default set to TIME_COUNT
72 struct tx_struct tx
; // NOTE: tx elements are considered as default values for MOPS
74 struct device_struct device_list
[MZ_MAX_DEVICES
];
76 int device_list_entries
;
78 int verbose_level
= 0;
80 char mz_default_config_path
[256];
81 char mz_default_log_path
[256];
83 static const char *short_options
= "46hqvVSxra:A:b:B:c:d:E:f:F:l:p:P:R:t:T:M:Q:X:";
85 static void signal_handler(int number
)
90 void clean_up(int sig
)
93 struct arp_table_struct
*cur
, *next
;
95 if (!quiet
) fprintf(stderr
, "\nMausezahn cleans up...\n");
98 verbose_l1(" close files (1) ...\n");
105 if (verbose
) fprintf(stderr
, " close files (2) ...\n");
112 if (verbose
) fprintf(stderr
, " clear mops list...\n");
113 mops_cleanup(mp_head
);
114 if (verbose
) fprintf(stderr
, " clear automops list...\n");
115 automops_cleanup(amp_head
);
116 if (verbose
) fprintf(stderr
, " clear packet sequences...\n");
117 mz_ll_delete_list(packet_sequences
);
120 for (i
=0; i
<device_list_entries
; i
++) {
121 if (device_list
[i
].p_arp
!=NULL
) {
122 pcap_close(device_list
[i
].p_arp
);
123 fprintf(stderr
, " stopped ARP process for device %s\n", device_list
[i
].dev
);
125 if (device_list
[i
].arprx_thread
!=0) {
126 pthread_cancel(device_list
[i
].arprx_thread
);
128 fprintf(stderr
, " (ARP thread for device %s done)\n", device_list
[i
].dev
);
131 if (device_list
[i
].arp_table
!=NULL
) {
132 cur
=device_list
[i
].arp_table
;
135 if (cur
!=NULL
) free(cur
);
140 // close packet sockets
141 if (device_list
[i
].ps
>=0) {
142 close(device_list
[i
].ps
);
147 if (verbose
) fprintf(stderr
, "finished.\n");
152 static void help(void)
154 printf("\nmausezahn %s, a fast versatile traffic generator\n", VERSION_STRING
);
155 puts("http://www.netsniff-ng.org\n\n"
156 "Usage: mausezahn [options] [interface] <keyword>|<arg-string>|<hex-string>\n"
158 " -x <port> Interactive mode with telnet CLI, default port: 25542\n"
159 " -l <ip> Listen address to bind to when in interactive mode, default: 0.0.0.0\n"
160 " -4 IPv4 mode (default)\n"
162 " -R <PRIO> Set socket priority\n"
163 " -c <count> Send packet count times, default:1, infinite:0\n"
164 " -d <delay> Apply delay between transmissions. The delay value can be\n"
165 " specified in usec (default, no additional unit needed), or in\n"
166 " msec (e.g. 100m or 100msec), or in seconds (e.g. 100s or 100sec)\n"
167 " -r Multiplies the specified delay with a random value\n"
168 " -p <length> Pad the raw frame to specified length (using random bytes)\n"
169 " -a <srcmac|keyword> Use specified source mac address, no matter what has\n"
170 " been specified with other arguments; keywords see below,\n"
171 " Default is own interface\n"
172 " -b <dstmac|keyword> Same with destination mac address; keywords:\n"
173 " rand Use a random MAC address\n"
174 " bc Use a broadcast MAC address\n"
175 " own Use own interface MAC address (default for source MAC)\n"
176 " stp Use IEEE 802.1d STP multicast address\n"
177 " cisco Use Cisco multicast address as used for CDP, VTP, or PVST+\n"
178 " -A <srcip> Use specified source IP address (default is own interface IP)\n"
179 " -B <dstip|dnsname> Send packet to specified destination IP or domain name\n"
180 " -P <ascii payload> Use the specified ASCII payload\n"
181 " -f <filename> Read the ASCII payload from a file\n"
182 " -F <filename> Read the hexadecimal payload from a file\n"
183 " -Q <[CoS:]vlan> Specify 802.1Q VLAN tag and optional Class of Service, you can\n"
184 " specify multiple 802.1Q VLAN tags (QinQ...) by separating them\n"
185 " via a comma or a period (e.g. '5:10,20,2:30')\n"
186 " -t <packet-type|help> Specify packet type for autobuild (you don't need to care for\n"
187 " encapsulations in lower layers, most packet types allow/require\n"
188 " additional packet-specific arguments in an <arg-string>;\n"
189 " Currently supported types: arp, bpdu, cdp, ip, icmp, udp, tcp,\n"
190 " dns, rtp, syslog, lldp and more;\n"
191 " For context-help use 'help' as <arg-string>!\n"
192 " -T <packet-type> Specify packet type for server mode, currently only rtp is supported;\n"
193 " Enter -T help or -T rtp help for further information\n"
194 " -M <MPLS-label> Insert a MPLS label, enter '-M help' for a syntax description\n"
195 " -V|VV|... Verbose and more verbose mode\n"
196 " -q Quiet mode, even omit 'important' standard short messages\n"
197 " -S Simulation mode: DOES NOT put anything on the wire, this is\n"
198 " typically combined with one of the verbose modes (v or V)\n"
200 " -h Print this help\n\n"
203 " mausezahn -c 0 -d 2s -t bpdu conf\n"
204 " mausezahn -t cdp change -c 0\n"
205 " mausezahn -t syslog sev=3 -P \"You have been mausezahned.\" -A 10.1.1.109 -B 192.168.7.7\n"
206 " mausezahn eth0 -A rand -B 1.1.1.1 -c 0 -t tcp \"dp=1-1023, flags=syn\"\n\n"
208 " This tool is targeted for network developers! You should\n"
209 " be aware of what you are doing and what these options above\n"
210 " mean! Only use this tool in an isolated LAN that you own!\n\n"
211 "Please report bugs to <bugs@netsniff-ng.org>\n"
212 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
213 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
214 "Swiss federal institute of technology (ETH Zurich)\n"
215 "License: GNU GPL version 2.0\n"
216 "This is free software: you are free to change and redistribute it.\n"
217 "There is NO WARRANTY, to the extent permitted by law.\n");
221 static void version(void)
223 printf("\nmausezahn %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
224 puts("a fast versatile traffic generator\n"
225 "http://www.netsniff-ng.org\n\n"
226 "Please report bugs to <bugs@netsniff-ng.org>\n"
227 "Copyright (C) 2008-2010 Herbert Haas <herbert@perihel.at>,\n"
228 "Copyright (C) 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
229 "Swiss federal institute of technology (ETH Zurich)\n"
230 "License: GNU GPL version 2.0\n"
231 "This is free software: you are free to change and redistribute it.\n"
232 "There is NO WARRANTY, to the extent permitted by law.\n");
241 mz_default_config_path
[0] = 0x00;
242 mz_default_log_path
[0] = 0x00;
252 gind_max
= TIME_COUNT
;
257 char mz_listen_addr
[16] = "0.0.0.0";
260 for (i
=0;i
<TIME_COUNT_MAX
;i
++) jitter
[i
] = 0;
262 time0_flag
= 0; // If set then time0 has valid data
263 sqnr0_flag
= 0; // If set then sqnr_last and sqnr_next has valid data
265 mz_ssrc
[0]=0; mz_ssrc
[1]=0; mz_ssrc
[2]=0; mz_ssrc
[3]=0;
267 // Reset mgmt parameters of TX:
268 tx
.packet_mode
= 1; // assume we don't care about L2
270 tx
.delay
= DEFAULT_DELAY
;
272 tx
.arg_string
[0] = '\0';
274 // Reset Ethernet parameters of TX:
275 tx
.eth_params_already_set
= 0;
276 for (i
=0; i
<6; i
++) tx
.eth_dst
[i
] = 0xff;
277 for (i
=0; i
<6; i
++) tx
.eth_src
[i
] = 0; // TODO: Get own MAC !!!
278 tx
.eth_dst_txt
[0] = '\0';
279 tx
.eth_src_txt
[0] = '\0';
285 tx
.eth_payload
[0] = '\0';
286 tx
.eth_payload_s
= 0;
289 // Reset CDP parameters for TX:
293 tx
.cdp_payload
[0] = '\0';
294 tx
.cdp_payload_s
= 0;
295 tx
.cdp_tlv_id
[0] = '\0';
296 tx
.cdp_tlv_id_len
= 0;
298 // Reset 802.1Q parameters of TX:
300 tx
.dot1Q_txt
[0] = '\0';
303 tx
.ascii
= 0; // 1 if specified
304 tx
.ascii_payload
[0]= '\0';
307 tx
.hex_payload_s
= 0;
309 // Reset MPLS parameters of TX:
311 tx
.mpls_txt
[0] = '\0';
316 tx
.mpls_verbose_string
[0] = '\0';
318 // Reset IP parameters of TX:
319 tx
.ip_src_txt
[0] = '\0';
321 tx
.ip_dst_txt
[0] = '\0';
322 tx
.ip_src_isrange
= 0;
325 memset(&tx
.ip6_src_start
, 0, sizeof(tx
.ip6_src_start
));
326 memset(&tx
.ip6_src_stop
, 0, sizeof(tx
.ip6_src_stop
));
330 memset(&tx
.ip6_dst_start
, 0, sizeof(tx
.ip6_dst_start
));
331 memset(&tx
.ip6_dst_stop
, 0, sizeof(tx
.ip6_dst_stop
));
332 tx
.ip_dst_isrange
= 0;
336 tx
.ip_payload
[0]= '\0';
338 tx
.ip_option
[0]= '\0';
341 // Reset ICMP parameters:
344 tx
.icmp_chksum
=0; // 0=autofill
349 // Reset general L4 parameters:
359 // Reset UDP parameters of TX:
361 tx
.udp_len
= 0; // If set to zero then create_udp_packet will calculate it
363 tx
.udp_payload
[0] = '\0';
364 tx
.udp_payload_s
= 0;
366 // Reset TCP parameters of TX:
369 tx
.tcp_seq_stop
= 42;
370 tx
.tcp_seq_delta
= 0; // also used as 'isrange' meaning
376 tx
.tcp_len
= 20; // Least size (TCP header only)
377 tx
.tcp_payload
[0] = '\0';
378 tx
.tcp_payload_s
= 0;
380 // Reset RTP parameters of TX:
384 // Initialize random generator
386 srand((unsigned int)t
);
390 for (i
=0; i
<MZ_MAX_DEVICES
; i
++) {
391 device_list
[i
].arprx_thread
= 0;
392 device_list
[i
].p_arp
= NULL
;
393 device_list
[i
].arp_table
= NULL
;
394 device_list
[i
].ps
=-1;
395 device_list
[i
].cli
=0;
396 device_list
[i
].mgmt_only
=0;
402 static void print_packet_types(void)
407 "| The following packet types are currently implemented:\n"
409 "| arp ... sends ARP packets\n"
410 "| bpdu ... sends BPDU packets (STP or PVST+)\n"
411 "| cdp ... sends CDP messages\n"
412 "| ip ... sends IPv4 packets\n"
413 "| udp ... sends UDP datagrams\n"
414 "| tcp ... sends TCP segments\n"
415 "| icmp ... sends ICMP messages\n"
416 "| igmp ... sends IGMP messages\n"
417 "| dns ... sends DNS messages\n"
418 "| rtp ... sends RTP datagrams\n"
419 "| syslog ... sends Syslog messages\n"
421 "| Of course you can build any other packet type 'manually' using the direct layer 2 mode.\n"
422 "| FYI: The interactive mode supports additional protocols. (Try mz -x <port>)\n"
428 // Purpose: Properly handle arguments and configure global structs (tx)
429 int getopts (int argc
, char *argv
[])
431 int i
, c
, rargs
, RX
=0, count_set
=0, delay_set
=0;
432 unsigned int time_factor
;
433 char *packet_type
=NULL
, *mops_type
=NULL
;
435 unsigned char *dum1
, *dum2
;
436 bool do_help
= false;
439 char err_buf
[LIBNET_ERRBUF_SIZE
];
440 struct libnet_ether_addr
*mymac
;
443 char hexpld
[MAX_PAYLOAD_SIZE
*2];
444 int hexpld_specified
=0;
449 opterr
= 1; // let getopt print error message if necessary
452 while ((c
= getopt(argc
, argv
, short_options
)) != -1)
455 tx
.eth_type
= 0x0800;
459 tx
.eth_type
= 0x86dd;
464 prio
= strtol(optarg
, NULL
, 0);
466 perror("Couldn't parse priority");
469 if (prio
< 0 || prio
> 0xffffffff) {
470 perror("Invalid priority value");
491 mz_port
= MZ_DEFAULT_PORT
;
494 strncpy (mz_listen_addr
, optarg
, sizeof(mz_listen_addr
));
497 strncpy (tx
.eth_src_txt
, optarg
, 32);
501 strncpy (tx
.ip_src_txt
, optarg
, sizeof(tx
.ip_src_txt
));
504 strncpy (tx
.eth_dst_txt
, optarg
, 32);
508 strncpy (tx
.ip_dst_txt
, optarg
, sizeof(tx
.ip_dst_txt
));
512 tx
.count
= strtol(optarg
, (char **)NULL
, 10);
513 if ((errno
== ERANGE
&& (tx
.count
== LONG_MAX
|| tx
.count
== LONG_MIN
))
514 || (errno
!= 0 && tx
.count
== 0)) {
518 if (tx
.count
<0) tx
.count
=1; //TODO: Allow count=0 which means infinity (need to update all send_functions)
525 unit
='u'; // default is usecs
526 if (sscanf(optarg
, "%ld%c", &delay
, &unit
) == EOF
) {
531 fprintf(stderr
, " Incorrect delay format\n");
534 if (unit
== 's') time_factor
=1000000; // seconds
535 else if (unit
== 'm') time_factor
=1000; // msecs
536 else if (unit
== 'u') time_factor
=1; // usecs
538 fprintf(stderr
, " Incorrect delay format\n");
541 tx
.delay
= delay
* time_factor
;
542 if ((errno
== ERANGE
&& (tx
.delay
== LONG_MAX
|| tx
.delay
== LONG_MIN
))
543 || (errno
!= 0 && tx
.delay
== 0)) {
547 if (tx
.delay
<0) tx
.delay
=0; // no delay
552 tx
.padding
= strtol(optarg
, (char **)NULL
, 10);
553 if ((errno
== ERANGE
&& (tx
.padding
== LONG_MAX
|| tx
.padding
== LONG_MIN
))
554 || (errno
!= 0 && tx
.padding
== 0)) {
558 if (tx
.padding
>10000) {
559 fprintf(stderr
, " Warning: Padding must not exceed 10000!\n");
564 packet_type
= optarg
; // analyzed below
565 if (strcmp(packet_type
,"help") == 0)
566 print_packet_types();
569 mops_type
= optarg
; // MOPS TRANSITION STRATEGY -- analyzed below
572 packet_type
= optarg
;
579 if (strncmp(optarg
,"help",4)==0) {
580 (void) get_mpls_params("help ");
583 strncpy (tx
.mpls_txt
, optarg
, 128);
584 tx
.eth_type
= ETHERTYPE_MPLS
;
589 case 'P': // ASCII payload
590 strncpy((char*)tx
.ascii_payload
, optarg
, MAX_PAYLOAD_SIZE
);
593 case 'f': // ASCII payload in FILE
594 afp
= fopen(optarg
, "r");
596 fprintf(stderr
, " mz/getopts: Can not open file %s. %s!\n", optarg
, strerror(errno
));
599 if (fgets((char*)tx
.ascii_payload
, MAX_PAYLOAD_SIZE
, afp
) == NULL
)
600 fprintf(stderr
, " mz/getopts: File empty?\n");
604 case 'F': // HEX payload in FILE
605 afp
= fopen(optarg
, "r");
607 fprintf(stderr
, " mz/getopts: Can not open file %s. %s!\n", optarg
, strerror(errno
));
611 while ( (hexpld
[i
]=fgetc(afp
))!=EOF
) {
612 if (isspace(hexpld
[i
])) {
621 case 'Q': // VLAN TAG
622 if (strncmp(optarg
,"help",4)==0) {
623 print_dot1Q_help(); // ugly but most simple and safe solution
626 strncpy (tx
.dot1Q_txt
, optarg
, 32);
628 // determine number of VLAN tags
629 for (i
=0; i
<strlen(tx
.dot1Q_txt
); i
++) {
630 if (tx
.dot1Q_txt
[i
]==',') tx
.dot1Q
++;
636 if ((optopt
== 'a') || (optopt
== 'b') || (optopt
== 'c') ||
637 (optopt
== 'd') || (optopt
== 'f') || (optopt
== 'p') ||
638 (optopt
== 't') || (optopt
== 'm'))
639 fprintf (stderr
, " mz/getopts: Option -%c requires an argument.\n", optopt
);
640 else if (isprint (optopt
))
641 fprintf (stderr
, " mz/getopts: Unknown option -%c'.\n", optopt
);
643 fprintf (stderr
, " mz/getopts: Unknown option character \\x%x'.\n", optopt
);
646 fprintf (stderr
," mz/getopts: Could not handle arguments properly!\n");
650 // ********************************************
651 // Handle additional arguments
652 // ********************************************
659 "Use at your own risk and responsibility!\n"
660 "-- Verbose mode --\n"
668 if ((rargs
=argc
-optind
)>2) { // number of remaining arguments
669 fprintf(stderr
," mz/getopts: Too many arguments!\n");
674 // There can be 0-2 additional arguments
677 if (lookupdev()) { // no device found
678 if (verbose
) fprintf(stderr
, " mz: no active interfaces found!\n");
679 strcpy(tx
.device
, "lo");
682 case 1: // arg_string OR device given => find out!
683 if (__device_ifindex(argv
[optind
]) > 0) {
684 strncpy(tx
.device
, argv
[optind
], 16);
686 else { /// arg_string given => no device has been specified -- let's find one!
687 strncpy (tx
.arg_string
, argv
[optind
], MAX_PAYLOAD_SIZE
);
688 do_help
= !!getarg(tx
.arg_string
,"help", NULL
);
691 /* no device found */
693 fprintf(stderr
, " mz: no active interfaces found!\n");
694 strcpy(tx
.device
, "lo");
697 fprintf(stderr
," mz: device not given, will use %s\n",tx
.device
);
701 case 2: // both device and arg_string given
702 strncpy (tx
.device
, argv
[optind
], 16);
703 strncpy (tx
.arg_string
, argv
[optind
+1], MAX_PAYLOAD_SIZE
);
706 fprintf(stderr
," mz/getopts: Unknown argument problem!\n");
710 if (hexpld_specified
) {
711 strcat(tx
.arg_string
, ",p=");
712 strcat(tx
.arg_string
, hexpld
);
716 //////////////////////////////////////////////////////////////////////////
718 // Initialize MAC and IP Addresses.
720 // - tx.eth_src = own interface MAC
721 // - tx.ip_src = own interface IP or user specified
722 // - tx.ip_dst = 255.255.255.255 or user specified (can be a range)
723 // - tx.ip_src_rand ... is set if needed.
726 // Get own device MAC address:
727 // Don't open context if only a help text is requested
728 if (!do_help
&& getarg(tx
.arg_string
,"help", NULL
) !=1) {
729 l
= libnet_init (LIBNET_LINK_ADV
, tx
.device
, err_buf
);
731 fprintf(stderr
, " mz/getopts: libnet_init() failed (%s)", err_buf
);
734 mymac
= libnet_get_hwaddr(l
);
735 for (i
=0; i
<6; i
++) {
736 tx
.eth_src
[i
] = mymac
->ether_addr_octet
[i
];
737 tx
.eth_mac_own
[i
] = mymac
->ether_addr_octet
[i
];
740 // Set source IP address:
741 if (strlen(tx
.ip_src_txt
)) { // option -A has been specified
742 if (mz_strcmp(tx
.ip_src_txt
, "bcast", 2)==0) {
744 fprintf(stderr
, "Option -A does not support 'bcast' when in IPv6 mode.\n");
747 tx
.ip_src
= libnet_name2addr4 (l
, "255.255.255.255", LIBNET_DONT_RESOLVE
);
748 } else if (strcmp(tx
.ip_src_txt
, "rand") == 0) {
750 fprintf(stderr
, "Option -A does not support 'rand' when in IPv6 mode.\n");
754 tx
.ip_src_h
= (u_int32_t
) ( ((float) rand()/RAND_MAX
)*0xE0000000); //this is 224.0.0.0
757 (ipv6_mode
&& get_ip6_range_src(tx
.ip_src_txt
, l
)) || // returns 1 when no range has been specified
758 (!ipv6_mode
&& get_ip_range_src(tx
.ip_src_txt
))
761 // name2addr{4,6} accepts a valid IP address or a FQDN:
763 tx
.ip6_src
= libnet_name2addr6(l
, tx
.ip_src_txt
, LIBNET_RESOLVE
);
764 if (libnet_in6_is_error(tx
.ip6_src
)) {
765 /* libnet_in6_is_error returns 1 for the valid IPv6 address
766 * ffff:ffff:ffff:ffff:ffff:ffff. Use an additional inet_pton()
767 * check to cover cases where this address is specified
770 struct in6_addr src_check
;
771 if (inet_pton(AF_INET6
, tx
.ip_src_txt
, &src_check
) != 1) {
772 fprintf(stderr
, "Failed to set source"
773 " IPv6 address. Please check if"
774 " source is set to a valid IPv6 address.\n");
779 tx
.ip_src
= libnet_name2addr4(l
, tx
.ip_src_txt
, LIBNET_RESOLVE
);
780 if (tx
.ip_src
== -1) {
781 /* libnet_name2addr4() returns -1 for the valid address 255.255.255.255.
782 * Use an additional inet_pton() check to cover case where this address
783 * is specified as source
785 struct in_addr src_check
;
786 if (inet_pton(AF_INET
, tx
.ip_src_txt
, &src_check
) != 1) {
787 fprintf(stderr
, "Failed to set source"
788 " IPv4 address. Please check if"
789 " source is set to a valid IPv4 address.\n");
796 // no source IP specified: by default use own IP address
798 tx
.ip6_src
= libnet_get_ipaddr6(l
);
799 if (strncmp((char*)&tx
.ip6_src
,(char*)&in6addr_error
, sizeof(in6addr_error
))==0)
800 printf("Failed to set source IPv6 address: %s", l
->err_buf
);
803 tx
.ip_src
= libnet_get_ipaddr4(l
);
806 // Set destination IP address:
807 if (strlen(tx
.ip_dst_txt
)) { // option -B has been specified
808 if (mz_strcmp(tx
.ip_dst_txt
, "rand", 2)==0) {
809 fprintf(stderr
, "Option -B does not support random destination IP addresses currently.\n");
813 if (mz_strcmp(tx
.ip_dst_txt
, "bcast", 2)==0) {
815 fprintf(stderr
, "Option -B does not support 'bcast' when in IPv6 mode.\n");
818 tx
.ip_dst
= libnet_name2addr4 (l
, "255.255.255.255", LIBNET_DONT_RESOLVE
);
820 (ipv6_mode
&& get_ip6_range_dst(tx
.ip_dst_txt
, l
)) || // returns 1 when no range has been specified
821 (!ipv6_mode
&& get_ip_range_dst(tx
.ip_dst_txt
)))
823 // name2addr{4, 6} accepts a valid IP address or a FQDN:
825 tx
.ip6_dst
= libnet_name2addr6(l
, tx
.ip_dst_txt
, LIBNET_RESOLVE
);
826 if (libnet_in6_is_error(tx
.ip6_dst
)) {
827 /* libnet_in6_is_error returns 1 for the valid IPv6 address
828 * ffff:ffff:ffff:ffff:ffff:ffff. Use an additional inet_pton()
829 * check to cover cases where this address is specified
832 struct in6_addr dst_check
;
833 if (inet_pton(AF_INET6
, tx
.ip_dst_txt
, &dst_check
) != 1) {
834 fprintf(stderr
, "Failed to set destination"
835 " IPv6 address. Please check if"
836 " source is set to a valid IPv6 address.\n");
841 tx
.ip_dst
= libnet_name2addr4(l
, tx
.ip_dst_txt
, LIBNET_RESOLVE
);
842 if (tx
.ip_dst
== -1) {
843 /* libnet_name2addr4() returns -1 for the valid address 255.255.255.255.
844 * Use an additional inet_pton() check to cover case where this address
845 * is specified as destination
847 struct in_addr dst_check
;
848 if (inet_pton(AF_INET
, tx
.ip_dst_txt
, &dst_check
) != 1) {
849 fprintf(stderr
, "Failed to set destination"
850 " IPv4 address. Please check if"
851 " destination is set to a valid IPv4 address.\n");
859 else { // no destination IP specified: by default use broadcast
861 tx
.ip6_dst
= libnet_name2addr6 (l
, "ff02::1", LIBNET_DONT_RESOLVE
);
863 tx
.ip_dst
= libnet_name2addr4 (l
, "255.255.255.255", LIBNET_DONT_RESOLVE
);
867 // Initialize tx.ip_src_h and tx.ip_dst_h which are used by 'print_frame_details()'
868 // in verbose mode. See 'modifications.c'.
870 if (tx
.ip_src_rand
) { // ip_src_h already given, convert to ip_src
871 dum1
= (unsigned char*) &tx
.ip_src_h
;
872 dum2
= (unsigned char*) &tx
.ip_src
;
874 else { // ip_src already given, convert to ip_src_h
876 if (tx
.ip_src_isrange
) {
877 tx
.ip6_src
= tx
.ip6_src_start
;
880 dum1
= (unsigned char*) &tx
.ip_src
;
881 dum2
= (unsigned char*) &tx
.ip_src_h
;
886 if (tx
.ip_dst_isrange
) {
887 tx
.ip6_dst
= tx
.ip6_dst_start
;
898 dum1
= (unsigned char*) &tx
.ip_dst
;
899 dum2
= (unsigned char*) &tx
.ip_dst_h
;
914 // END OF ADDRESS INITIALIZATION
916 //////////////////////////////////////////////////////////////////////////
919 ////// retrieve interface parameters ///////
921 for (i
=0; i
<device_list_entries
; i
++) {
922 get_dev_params(device_list
[i
].dev
);
926 //////////////////////////////////////////////////////////////////////////
928 // Mausezahn CLI desired?
930 // has port number been specified?
931 if (strlen(tx
.arg_string
)) {
932 mz_port
= (int) str2int (tx
.arg_string
);
939 //////////////////////////////////////////////////////////////////////////
943 // Consider -t and -m option (used exclusively)
944 // -t => special packet types, stateless
946 // If -t not present then evaluate arg_string which must
947 // contain a byte-string in hexadecimal notation.
951 // ***** NEW: MOPS TRANSITION STRATEGY *****
952 if (mops_type
!= NULL
) {
954 if (mz_strcmp(mops_type
,"lldp",4)==0) {
955 mops_direct(tx
.device
, MOPS_LLDP
, tx
.arg_string
);
960 if (packet_type
== NULL
) { // raw hex string given
963 else if (strcmp(packet_type
,"arp")==0) {
966 else if (strcmp(packet_type
,"bpdu")==0) {
969 else if (strcmp(packet_type
,"ip")==0) {
972 else if (strcmp(packet_type
,"udp")==0) {
975 else if (strcmp(packet_type
,"icmp")==0) {
978 else if (strcmp(packet_type
,"icmp6")==0) {
981 else if (strcmp(packet_type
,"tcp")==0) {
984 else if (strcmp(packet_type
,"dns")==0) {
987 else if (strcmp(packet_type
,"cdp")==0) {
990 else if (strcmp(packet_type
,"syslog")==0) {
993 else if (strcmp(packet_type
, "igmp") == 0) {
996 else if (strcmp(packet_type
,"lldp")==0) {
998 tx
.packet_mode
=0; // create whole frame by ourself
1000 else if (strcmp(packet_type
,"rtp")==0) {
1006 if (!count_set
) tx
.count
= 0;
1007 if (!delay_set
) tx
.delay
= 20000; // 20 msec inter-packet delay for RTP
1011 fprintf(stderr
, " mz: you must specify a valid packet type!\n");
1015 //////////////////////////////////////////////////////////////////////////
1017 // TODO: Implement macro support
1018 // Check macro types here
1023 int main(int argc
, char **argv
)
1025 // These handles are only used when creating L3 and above packets.
1026 libnet_t
*l
; // the context
1027 libnet_ptag_t t2
=0, t3
=0, t4
=0; // handles to layers
1028 double cpu_time_used
;
1032 if ( getopts(argc
, argv
) )
1034 (void) fprintf(stderr
, " Invalid command line parameters!\n");
1038 // Check whether hires timers are supported or not:
1039 (void) check_timer();
1041 signal(SIGINT
, signal_handler
); // to close all file pointers etc upon SIGINT
1061 case IP
: // From now on a new much more modular method is used:
1062 l
= get_link_context();
1063 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1064 if (!quiet
) complexity();
1065 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1066 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1068 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1073 l
= get_link_context();
1074 t4
= create_icmp_packet(l
); // t4 can be used for later header changes
1075 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1076 if (!quiet
) complexity();
1077 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1078 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1080 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1085 l
= get_link_context();
1086 t4
= create_icmp6_packet(l
); // t4 can be used for later header changes
1087 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1088 if (!quiet
) complexity();
1089 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1090 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1092 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1097 l
= get_link_context();
1098 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1099 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1100 if (!quiet
) complexity();
1101 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1102 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1104 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1109 l
= get_link_context();
1110 t4
= create_tcp_packet(l
); // t4 can be used for later header changes
1111 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1112 if (!quiet
) complexity();
1113 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1114 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1116 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1121 l
= get_link_context();
1122 t4
= create_igmp_packet(l
);
1123 /* t3 can be used for later header changes */
1124 t3
= create_ip_packet(l
);
1128 /* Ethernet manipulation features does NOT use ARP to determine eth_dst
1130 if (tx
.packet_mode
== 0)
1131 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1133 send_frame(l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1138 l
= get_link_context();
1139 (void) create_dns_packet();
1140 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1141 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1142 if (!quiet
) complexity();
1143 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1144 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1146 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1151 l
= get_link_context();
1152 if (!quiet
) fprintf(stderr
, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx
.count
, tx
.delay
);
1153 (void) create_rtp_packet();
1154 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1155 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1156 if (!quiet
) complexity();
1157 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1158 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1160 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1163 case RX_RTP
: // Receive RTP packets
1170 l
= get_link_context();
1171 (void) create_syslog_packet();
1172 t4
= create_udp_packet(l
); // t4 can be used for later header changes
1173 t3
= create_ip_packet(l
); // t3 can be used for later header changes
1174 if (!quiet
) complexity();
1176 if (tx
.packet_mode
==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
1177 t2
= create_eth_frame(l
, t3
, t4
); // t2 can be used for later header changes
1179 send_frame (l
, t3
, t4
); // NOTE: send_frame also destroys context finaly
1182 case LLDP
: // start with a new concept here
1183 //l = get_link_context();
1184 //(void) create_lldp_packet();
1185 // // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
1186 fprintf(stderr
, "LLDP is currently only supported via the interactive mode\n");
1192 (void) fprintf(stderr
," mz/main: unknown mode! Stop.\n");
1199 cpu_time_used
= ((double) (mz_stop
- mz_start
)) / CLOCKS_PER_SEC
;
1200 if (cpu_time_used
> 0)
1202 total_d
/= cpu_time_used
;
1203 fprintf(stderr
, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used
,total_d
);
1207 fprintf(stderr
, "\n");