@@ -396,34 +396,46 @@ int main(int argc, char *argv[])
396396 }
397397 }
398398
399- rawsock_eth_settings (source_mac , router_mac );
400- rawsock_ip_settings (source_addr , ttl );
401-
402399 // Handle --source-port: auto-detection, reservation, errors
403- if (r == 0 ) {
404- const bool mandatory = banners && ip_type == IP_TYPE_TCP ;
405- const bool useful = mandatory || (banners && ip_type == IP_TYPE_UDP );
400+ const bool port_mandatory = banners && ip_type == IP_TYPE_TCP ;
401+ if (r == 0 && rawsock_islocal (source_addr ) == 0 ) {
402+ // We're using an unassigned IP, pick any random port. No need to
403+ // reserve it or care about the OS.
404+ if (port_mandatory && source_port == -1 ) {
405+ source_port = 25000 + rand () % 40000 ;
406+ log_raw ("Using random source port: %d" , source_port );
407+ }
408+ } else if (r == 0 ) {
409+ const bool port_useful = banners && ip_type == IP_TYPE_UDP ;
406410 bool auto_failed = false;
407411
408- if (mandatory || useful ) {
412+ if (port_mandatory || port_useful ) {
409413 int tmp = rawsock_reserve_port (source_addr , ip_type , source_port == -1 ? 0 : source_port );
410414 if (tmp >= 0 ) {
411- log_debug ("reserved source port: %d" , tmp );
412415 source_port = tmp ;
416+ if (port_mandatory )
417+ log_raw ("Using reserved source port: %d" , source_port );
418+ else
419+ log_debug ("Using reserved source port: %d" , source_port );
413420 } else {
414421 auto_failed = tmp == -1 ;
415422 }
416423 }
417424
418425 assert (source_port != 0 );
419- if (mandatory && source_port == -1 ) {
426+ if (port_mandatory && source_port == -1 ) {
420427 log_raw ("A source port is required but was not given%s." ,
421428 auto_failed ? " (automatic reservation failed)" : "" );
422429 r = 1 ;
430+ } else if (auto_failed ) {
431+ // assume the user knows what he's doing
432+ log_debug ("automatic port reservation failed" );
423433 }
424434 }
425435
426436 if (r == 0 ) {
437+ rawsock_eth_settings (source_mac , router_mac );
438+ rawsock_ip_settings (source_addr , ttl );
427439 scan_set_general (& ports , max_rate , show_closed , banners );
428440 scan_set_network (source_addr , source_port , ip_type );
429441 scan_set_output (outfile , outdef );
0 commit comments