src/send.c
iterator_t *send_init(void)
{
	// generate a new primitive root and starting position
	iterator_t *it;
	uint32_t num_subshards =
	    (uint32_t)zconf.senders * (uint32_t)zconf.total_shards;
	if (num_subshards > blacklist_count_allowed()) {
		log_fatal("send", "senders * shards > allowed probes");
	}
	if (zsend.max_targets && (num_subshards > zsend.max_targets)) {
		log_fatal("send", "senders * shards > max targets");
	}
	it = iterator_init(zconf.senders, zconf.shard_num, zconf.total_shards);
	// process the dotted-notation addresses passed to ZMAP and determine
	// the source addresses from which we'll send packets;
	srcip_first = inet_addr(zconf.source_ip_first);
	if (srcip_first == INADDR_NONE) {
		log_fatal("send", "invalid begin source ip address: `%s'",
			  zconf.source_ip_first);
	}
	srcip_last = inet_addr(zconf.source_ip_last);
	if (srcip_last == INADDR_NONE) {
		log_fatal("send", "invalid end source ip address: `%s'",
			  zconf.source_ip_last);
	}
	log_debug("send", "srcip_first: %u", srcip_first);
	log_debug("send", "srcip_last: %u", srcip_last);
	if (srcip_first == srcip_last) {
		srcip_offset = 0;
		num_src_addrs = 1;
	} else {
		uint32_t ip_first = ntohl(srcip_first);
		uint32_t ip_last = ntohl(srcip_last);
		assert(ip_first && ip_last);
		assert(ip_last > ip_first);
		uint32_t offset =
		    (uint32_t)(aesrand_getword(zconf.aes) & 0xFFFFFFFF);
		srcip_offset = offset % (srcip_last - srcip_first);
		num_src_addrs = ip_last - ip_first + 1;
	}

	// process the source port range that ZMap is allowed to use
	num_src_ports = zconf.source_port_last - zconf.source_port_first + 1;
	log_debug("send", "will send from %i address%s on %u source ports",
		  num_src_addrs, ((num_src_addrs == 1) ? "" : "es"),
		  num_src_ports);
	// global initialization for send module
	assert(zconf.probe_module);
	if (zconf.probe_module->global_initialize) {
		if (zconf.probe_module->global_initialize(&zconf)) {
			log_fatal(
			    "send",
			    "global initialization for probe module failed.");
		}
	}
	// convert specified bandwidth to packet rate
	if (zconf.bandwidth > 0) {
		size_t pkt_len = zconf.probe_module->packet_length;
		pkt_len *= 8;
		// 7 byte MAC preamble, 1 byte Start frame, 4 byte CRC, 12 byte inter-frame gap
		pkt_len += 8 * 24;
		// adjust calculated length if less than the minimum size of an ethernet frame
		if (pkt_len < 84 * 8) {
			pkt_len = 84 * 8;
		}
		// rate is a uint32_t so, don't overflow
		if (zconf.bandwidth / pkt_len > 0xFFFFFFFFu) {
			zconf.rate = 0;
		} else {
			zconf.rate = zconf.bandwidth / pkt_len;
			if (zconf.rate == 0) {
				log_warn(
				    "send",
				    "bandwidth %lu bit/s is slower than 1 pkt/s, "
				    "setting rate to 1 pkt/s",
				    zconf.bandwidth);
				zconf.rate = 1;
			}
		}
		log_debug("send",
			  "using bandwidth %lu bits/s for %zu byte probe, rate set to %d pkt/s",
			  zconf.bandwidth, pkt_len/8, zconf.rate);
	}
	// Get the source hardware address, and give it to the probe
	// module
	if (!zconf.hw_mac_set) {
		if (get_iface_hw_addr(zconf.iface, zconf.hw_mac)) {
			log_fatal("send",
				  "could not retrieve hardware address for "
				  "interface: %s",
				  zconf.iface);
			return NULL;
		}
		log_debug(
		    "send",
		    "no source MAC provided. "
		    "automatically detected %02x:%02x:%02x:%02x:%02x:%02x as hw "
		    "interface for %s",
		    zconf.hw_mac[0], zconf.hw_mac[1], zconf.hw_mac[2],
		    zconf.hw_mac[3], zconf.hw_mac[4], zconf.hw_mac[5],
		    zconf.iface);
	}
	log_debug("send", "source MAC address %02x:%02x:%02x:%02x:%02x:%02x",
		  zconf.hw_mac[0], zconf.hw_mac[1], zconf.hw_mac[2],
		  zconf.hw_mac[3], zconf.hw_mac[4], zconf.hw_mac[5]);

	if (zconf.dryrun) {
		log_info("send", "dryrun mode -- won't actually send packets");
	}
	// initialize random validation key
	validate_init();
	// setup signal handlers for changing scan speed
	signal(SIGUSR1, sig_handler_increase_speed);
	signal(SIGUSR2, sig_handler_decrease_speed);

	zsend.start = now();
	return it;
}
// global sender initialize (not thread specific)
global sender initialize (not thread specific)
global sender initialize ( not thread specific )
iterator_t *send_init(void)
{
	iterator_t *it;
	uint32_t num_subshards =
	    (uint32_t)zconf.senders * (uint32_t)zconf.total_shards;
	if (num_subshards > blacklist_count_allowed()) {
		log_fatal("send", "senders * shards > allowed probes");
	}
	if (zsend.max_targets && (num_subshards > zsend.max_targets)) {
		log_fatal("send", "senders * shards > max targets");
	}
	it = iterator_init(zconf.senders, zconf.shard_num, zconf.total_shards);
	srcip_first = inet_addr(zconf.source_ip_first);
	if (srcip_first == INADDR_NONE) {
		log_fatal("send", "invalid begin source ip address: `%s'",
			  zconf.source_ip_first);
	}
	srcip_last = inet_addr(zconf.source_ip_last);
	if (srcip_last == INADDR_NONE) {
		log_fatal("send", "invalid end source ip address: `%s'",
			  zconf.source_ip_last);
	}
	log_debug("send", "srcip_first: %u", srcip_first);
	log_debug("send", "srcip_last: %u", srcip_last);
	if (srcip_first == srcip_last) {
		srcip_offset = 0;
		num_src_addrs = 1;
	} else {
		uint32_t ip_first = ntohl(srcip_first);
		uint32_t ip_last = ntohl(srcip_last);
		assert(ip_first && ip_last);
		assert(ip_last > ip_first);
		uint32_t offset =
		    (uint32_t)(aesrand_getword(zconf.aes) & 0xFFFFFFFF);
		srcip_offset = offset % (srcip_last - srcip_first);
		num_src_addrs = ip_last - ip_first + 1;
	}
	num_src_ports = zconf.source_port_last - zconf.source_port_first + 1;
	log_debug("send", "will send from %i address%s on %u source ports",
		  num_src_addrs, ((num_src_addrs == 1) ? "" : "es"),
		  num_src_ports);
	assert(zconf.probe_module);
	if (zconf.probe_module->global_initialize) {
		if (zconf.probe_module->global_initialize(&zconf)) {
			log_fatal(
			    "send",
			    "global initialization for probe module failed.");
		}
	}
	if (zconf.bandwidth > 0) {
		size_t pkt_len = zconf.probe_module->packet_length;
		pkt_len *= 8;
		pkt_len += 8 * 24;
		if (pkt_len < 84 * 8) {
			pkt_len = 84 * 8;
		}
		if (zconf.bandwidth / pkt_len > 0xFFFFFFFFu) {
			zconf.rate = 0;
		} else {
			zconf.rate = zconf.bandwidth / pkt_len;
			if (zconf.rate == 0) {
				log_warn(
				    "send",
				    "bandwidth %lu bit/s is slower than 1 pkt/s, "
				    "setting rate to 1 pkt/s",
				    zconf.bandwidth);
				zconf.rate = 1;
			}
		}
		log_debug("send",
			  "using bandwidth %lu bits/s for %zu byte probe, rate set to %d pkt/s",
			  zconf.bandwidth, pkt_len/8, zconf.rate);
	}
	if (!zconf.hw_mac_set) {
		if (get_iface_hw_addr(zconf.iface, zconf.hw_mac)) {
			log_fatal("send",
				  "could not retrieve hardware address for "
				  "interface: %s",
				  zconf.iface);
			return NULL;
		}
		log_debug(
		    "send",
		    "no source MAC provided. "
		    "automatically detected %02x:%02x:%02x:%02x:%02x:%02x as hw "
		    "interface for %s",
		    zconf.hw_mac[0], zconf.hw_mac[1], zconf.hw_mac[2],
		    zconf.hw_mac[3], zconf.hw_mac[4], zconf.hw_mac[5],
		    zconf.iface);
	}
	log_debug("send", "source MAC address %02x:%02x:%02x:%02x:%02x:%02x",
		  zconf.hw_mac[0], zconf.hw_mac[1], zconf.hw_mac[2],
		  zconf.hw_mac[3], zconf.hw_mac[4], zconf.hw_mac[5]);
	if (zconf.dryrun) {
		log_info("send", "dryrun mode -- won't actually send packets");
	}
	validate_init();
	signal(SIGUSR1, sig_handler_increase_speed);
	signal(SIGUSR2, sig_handler_decrease_speed);
	zsend.start = now();
	return it;
}
iterator_t * send_init ( void ) { iterator_t * it ; uint32_t num_subshards = ( uint32_t ) zconf . senders * ( uint32_t ) zconf . total_shards ; if ( num_subshards > blacklist_count_allowed ( ) ) { log_fatal ( " " , " " ) ; } if ( zsend . max_targets && ( num_subshards > zsend . max_targets ) ) { log_fatal ( " " , " " ) ; } it = iterator_init ( zconf . senders , zconf . shard_num , zconf . total_shards ) ; srcip_first = inet_addr ( zconf . source_ip_first ) ; if ( srcip_first == INADDR_NONE ) { log_fatal ( " " , " " , zconf . source_ip_first ) ; } srcip_last = inet_addr ( zconf . source_ip_last ) ; if ( srcip_last == INADDR_NONE ) { log_fatal ( " " , " " , zconf . source_ip_last ) ; } log_debug ( " " , " " , srcip_first ) ; log_debug ( " " , " " , srcip_last ) ; if ( srcip_first == srcip_last ) { srcip_offset = 0 ; num_src_addrs = 1 ; } else { uint32_t ip_first = ntohl ( srcip_first ) ; uint32_t ip_last = ntohl ( srcip_last ) ; assert ( ip_first && ip_last ) ; assert ( ip_last > ip_first ) ; uint32_t offset = ( uint32_t ) ( aesrand_getword ( zconf . aes ) & 0xFFFFFFFF ) ; srcip_offset = offset % ( srcip_last - srcip_first ) ; num_src_addrs = ip_last - ip_first + 1 ; } num_src_ports = zconf . source_port_last - zconf . source_port_first + 1 ; log_debug ( " " , " " , num_src_addrs , ( ( num_src_addrs == 1 ) ? " " : " " ) , num_src_ports ) ; assert ( zconf . probe_module ) ; if ( zconf . probe_module -> global_initialize ) { if ( zconf . probe_module -> global_initialize ( & zconf ) ) { log_fatal ( " " , " " ) ; } } if ( zconf . bandwidth > 0 ) { size_t pkt_len = zconf . probe_module -> packet_length ; pkt_len *= 8 ; pkt_len += 8 * 24 ; if ( pkt_len < 84 * 8 ) { pkt_len = 84 * 8 ; } if ( zconf . bandwidth / pkt_len > 0xFFFFFFFFu ) { zconf . rate = 0 ; } else { zconf . rate = zconf . bandwidth / pkt_len ; if ( zconf . rate == 0 ) { log_warn ( " " , " " " " , zconf . bandwidth ) ; zconf . rate = 1 ; } } log_debug ( " " , " " , zconf . bandwidth , pkt_len / 8 , zconf . rate ) ; } if ( ! zconf . hw_mac_set ) { if ( get_iface_hw_addr ( zconf . iface , zconf . hw_mac ) ) { log_fatal ( " " , " " " " , zconf . iface ) ; return NULL ; } log_debug ( " " , " " " " " " , zconf . hw_mac [ 0 ] , zconf . hw_mac [ 1 ] , zconf . hw_mac [ 2 ] , zconf . hw_mac [ 3 ] , zconf . hw_mac [ 4 ] , zconf . hw_mac [ 5 ] , zconf . iface ) ; } log_debug ( " " , " " , zconf . hw_mac [ 0 ] , zconf . hw_mac [ 1 ] , zconf . hw_mac [ 2 ] , zconf . hw_mac [ 3 ] , zconf . hw_mac [ 4 ] , zconf . hw_mac [ 5 ] ) ; if ( zconf . dryrun ) { log_info ( " " , " " ) ; } validate_init ( ) ; signal ( SIGUSR1 , sig_handler_increase_speed ) ; signal ( SIGUSR2 , sig_handler_decrease_speed ) ; zsend . start = now ( ) ; return it ; }
global sender initialize (not thread specific)
global sender initialize ( not thread specific )
// generate a new primitive root and starting position // process the dotted-notation addresses passed to ZMAP and determine // the source addresses from which we'll send packets; // process the source port range that ZMap is allowed to use // global initialization for send module // convert specified bandwidth to packet rate // 7 byte MAC preamble, 1 byte Start frame, 4 byte CRC, 12 byte inter-frame gap // adjust calculated length if less than the minimum size of an ethernet frame // rate is a uint32_t so, don't overflow // Get the source hardware address, and give it to the probe // module // initialize random validation key // setup signal handlers for changing scan speed