home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / targets.c < prev    next >
C/C++ Source or Header  |  2001-08-10  |  46KB  |  1,356 lines

  1.  
  2. /***********************************************************************/
  3. /* targets.c -- Functions relating to "ping scanning" as well as       */
  4. /* determining the exact IPs to hit based on CIDR and other input      */
  5. /* formats.                                                            */
  6. /*                                                                     */
  7. /***********************************************************************/
  8. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  9. /*  program is free software; you can redistribute it and/or modify    */
  10. /*  it under the terms of the GNU General Public License as published  */
  11. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  12. /*  right to use, modify, and redistribute this software under certain */
  13. /*  conditions.  If this license is unacceptable to you, we may be     */
  14. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  15. /*                                                                     */
  16. /*  If you received these files with a written license agreement       */
  17. /*  stating terms other than the (GPL) terms above, then that          */
  18. /*  alternative license agreement takes precendence over this comment. */
  19. /*                                                                     */
  20. /*  Source is provided to this software because we believe users have  */
  21. /*  a right to know exactly what a program is going to do before they  */
  22. /*  run it.  This also allows you to audit the software for security   */
  23. /*  holes (none have been found so far).                               */
  24. /*                                                                     */
  25. /*  Source code also allows you to port Nmap to new platforms, fix     */
  26. /*  bugs, and add new features.  You are highly encouraged to send     */
  27. /*  your changes to fyodor@insecure.org for possible incorporation     */
  28. /*  into the main distribution.  By sending these changes to Fyodor or */
  29. /*  one the insecure.org development mailing lists, it is assumed that */
  30. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  31. /*  reuse, modify, and relicense the code.  This is important because  */
  32. /*  the inability to relicense code has caused devastating problems    */
  33. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  34. /*  will always be available Open Source.  If you wish to specify      */
  35. /*  special license conditions of your contributions, just say so      */
  36. /*  when you send them.                                                */
  37. /*                                                                     */
  38. /*  This program is distributed in the hope that it will be useful,    */
  39. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  40. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  41. /*  General Public License for more details (                          */
  42. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  43. /*                                                                     */
  44. /***********************************************************************/
  45.  
  46. /* $Id: targets.c,v 1.76 2001/08/10 05:53:08 fyodor Exp $ */
  47.  
  48.  
  49. #include "targets.h"
  50. #include "timing.h"
  51. #include "osscan.h"
  52.  
  53. extern struct ops o;
  54.  
  55. /*  predefined filters -- I need to kill these globals at some pont. */
  56. extern unsigned long flt_dsthost, flt_srchost, flt_baseport;
  57.  
  58. /* Fills up the hostgroup_state structure passed in (which must point
  59.    to valid memory).  Lookahead is the number of hosts that can be
  60.    checked (such as ping scanned) in advance.  Randomize causes each
  61.    group of up to lookahead hosts to be internally shuffled around.
  62.    The target_expressions array must remail valid in memory as long as
  63.    this hostgroup_state structure is used -- the array is NOT copied.
  64.    Also, REMEMBER TO CALL hostgroup_state_destroy() when you are done
  65.    with the hostgroup_state (the latter function only frees internal
  66.    resources -- you still have to free the alocated memory (if any)
  67.    for the struct hostgroup_state itself.  */
  68. int hostgroup_state_init(struct hostgroup_state *hs, int lookahead,
  69.              int randomize, char *target_expressions[],
  70.              int num_expressions) {
  71.   bzero(hs, sizeof(struct hostgroup_state));
  72.   assert(lookahead > 0);
  73.   hs->hostbatch = (struct hoststruct *) safe_malloc(lookahead * sizeof(struct hoststruct));
  74.   hs->max_batch_sz = lookahead;
  75.   hs->current_batch_sz = 0;
  76.   hs->next_batch_no = 0;
  77.   hs->randomize = randomize;
  78.   hs->target_expressions = target_expressions;
  79.   hs->num_expressions = num_expressions;
  80.   hs->next_expression = 0;
  81.   hs->current_expression.nleft = 0; 
  82.   return 0;
  83. }
  84.  
  85. /* Free the *internal state* of a hostgroup_state structure -- it is
  86.    important to note that this does not free the actual memory
  87.    allocated for the "struct hostgroup_state" you pass in.  It only
  88.    frees internal stuff -- after all, your hostgroup_state could be on
  89.    the stack */
  90. void hostgroup_state_destroy(struct hostgroup_state *hs) {
  91.   if (!hs) fatal("NULL hostgroup_state passed to hostgroup_state_destroy()!");
  92.   if (!hs->hostbatch) fatal("hostgroup_state passed to hostgroup_state_destroy() contains NULL hostbatch!");
  93.   free(hs->hostbatch);
  94. }
  95.  
  96.  
  97. /* If there is at least one IP address left in t, one is pulled out and placed
  98.    in sin and then zero is returned and state information in t is updated
  99.    to reflect that the IP was pulled out.  If t is empty, -1 is returned */
  100. int target_struct_get(struct targets *t, struct in_addr *sin) {
  101.   int octet;
  102.  
  103.   startover: /* to hande nmap --resume where I have already
  104.         scanned many of the IPs */  
  105.  
  106.   if (t->nleft <= 0)
  107.     return -1;
  108.   
  109.   if (t->maskformat) {
  110.     if (t->currentaddr.s_addr <= t->end.s_addr) {
  111.       sin->s_addr = htonl(t->currentaddr.s_addr++);
  112.     } else {
  113.       error("Bogus target structure passed to target_struct_get");
  114.       t->nleft = 0;
  115.       sin->s_addr = 0;
  116.       return -1;
  117.     }
  118.   }
  119.   else {
  120.     if (o.debugging > 2) {
  121.       log_write(LOG_STDOUT, "doing %d.%d.%d.%d = %d.%d.%d.%d\n", t->current[0], t->current[1], t->current[2], t->current[3], t->addresses[0][t->current[0]],t->addresses[1][t->current[1]],t->addresses[2][t->current[2]],t->addresses[3][t->current[3]]);
  122.     }
  123.     /* Set the IP to the current value of everything */
  124.     sin->s_addr = htonl(t->addresses[0][t->current[0]] << 24 | 
  125.             t->addresses[1][t->current[1]] << 16 |
  126.             t->addresses[2][t->current[2]] << 8 | 
  127.             t->addresses[3][t->current[3]]);
  128.     
  129.     /* Now we nudge up to the next IP */
  130.     for(octet = 3; octet >= 0; octet--) {
  131.       if (t->current[octet] < t->last[octet]) {
  132.     /* OK, this is the column I have room to nudge upwards */
  133.     t->current[octet]++;
  134.     break;
  135.       } else {
  136.     /* This octet is finished so I reset it to the beginning */
  137.     t->current[octet] = 0;
  138.       }
  139.     }
  140.     if (octet == -1) {
  141.       /* It didn't find anything to bump up, I muast have taken the last IP */
  142.       assert(t->nleft == 1);
  143.       /* So I set current to last with the very final octet up one ... */
  144.       /* Note that this may make t->current[3] == 256 */
  145.       t->current[0] = t->last[0]; t->current[1] = t->last[1];
  146.       t->current[2] = t->last[2]; t->current[3] = t->last[3] + 1;
  147.     } else {
  148.       assert(t->nleft > 1); /* There must be at least one more IP left */
  149.     }
  150.   }
  151.   t->nleft--;
  152.   assert(t->nleft >= 0);
  153.   
  154.   /* If we are resuming from a previous scan, we have already finished
  155.      scans up to o.resume_ip.  */
  156.   if (o.resume_ip.s_addr) {
  157.     if (o.resume_ip.s_addr == sin->s_addr)
  158.       o.resume_ip.s_addr = 0; /* So that we will KEEP the next one */
  159.     goto startover; /* Try again */
  160.   }
  161.  
  162.   return 1;
  163. }
  164.  
  165. /* Undoes the previous target_struct_get operation */
  166. void target_struct_return(struct targets *t) {
  167.   int octet;
  168.   t->nleft++;
  169.   if (t->maskformat) {
  170.     assert(t->currentaddr.s_addr > t->start.s_addr);
  171.     t->currentaddr.s_addr--;
  172.   }
  173.   else {
  174.     for(octet = 3; octet >= 0; octet--) {
  175.       if (t->current[octet] > 0) {
  176.     /* OK, this is the column I have room to nudge downwards */
  177.     t->current[octet]--;
  178.     break;
  179.       } else {
  180.     /* This octet is already at the beginning, so I set it to the end */
  181.     t->current[octet] = t->last[octet];
  182.       }
  183.     }
  184.     assert(octet != -1);
  185.   }
  186. }
  187.  
  188. void hoststructfry(struct hoststruct *hostbatch, int nelem) {
  189.   genfry((unsigned char *)hostbatch, sizeof(struct hoststruct), nelem);
  190.   return;
  191. }
  192.  
  193. /* REMEMBER TO CALL hoststruct_free() on the hoststruct when you are done
  194.    with it!!! */
  195. struct hoststruct *nexthost(struct hostgroup_state *hs, 
  196.                 struct scan_lists *ports) {
  197. int hidx;
  198. char *device;
  199. int i;
  200.  
  201. if (hs->next_batch_no < hs->current_batch_sz) {
  202.   /* Woop!  This is easy -- we just pass back the next host struct */
  203.   return &hs->hostbatch[hs->next_batch_no++];
  204. }
  205. /* Doh, we need to refresh our array */
  206. bzero(hs->hostbatch, hs->max_batch_sz * sizeof(struct hoststruct));
  207. hs->current_batch_sz = hs->next_batch_no = 0;
  208. do {
  209.   /* Grab anything we have in our current_expression */
  210.   while (hs->current_batch_sz < hs->max_batch_sz && 
  211.      target_struct_get(&hs->current_expression, 
  212.        &(hs->hostbatch[hs->current_batch_sz].host)) != -1)
  213.     {
  214.       hidx = hs->current_batch_sz;
  215.  
  216.       /* Lets figure out what device this IP uses ... */
  217.       if (o.source) {
  218.     memcpy((char *)&hs->hostbatch[hidx].source_ip,(char *) o.source, 
  219.            sizeof(struct in_addr));
  220.     strcpy(hs->hostbatch[hidx].device, o.device);
  221.       } else {
  222.     /* We figure out the source IP/device IFF
  223.        1) We are r00t AND
  224.        2) We are doing tcp pingscan OR
  225.        3) We are doing NO scan AND we are doing a raw-mode portscan or 
  226.        osscan */
  227.     if (o.isr00t && 
  228.         ((o.pingtype & PINGTYPE_TCP) || 
  229.          (o.pingtype == PINGTYPE_NONE && 
  230.           (o.synscan || o.finscan || o.xmasscan || o.nullscan || o.ipprotscan ||
  231.            o.maimonscan || o.idlescan || o.ackscan || o.udpscan || o.osscan || o.windowscan)))) {
  232.      device = routethrough(&(hs->hostbatch[hidx].host), &(hs->hostbatch[hidx].source_ip));
  233.      if (!device) {
  234.        if (o.pingtype == PINGTYPE_NONE) {
  235.          fatal("Could not determine what interface to route packets through, run again with -e <device>");
  236.        } else {
  237.          error("WARNING:  Could not determine what interface to route packets through to %s, changing ping scantype to ICMP only", inet_ntoa(hs->hostbatch[hidx].host));
  238.          o.pingtype = PINGTYPE_ICMP;
  239.        }
  240.      } else {
  241.        strcpy(hs->hostbatch[hidx].device, device);
  242.      }
  243.     }  
  244.       }
  245.  
  246.       /* In some cases, we can only allow hosts that use the same device
  247.      in a group. */
  248.       if (o.isr00t && hidx > 0 && *hs->hostbatch[hidx].device && hs->hostbatch[hidx].source_ip.s_addr != hs->hostbatch[0].source_ip.s_addr) {
  249.     /* Cancel everything!  This guy must go in the next group and we are
  250.        outtof here */
  251.     target_struct_return(&(hs->current_expression));
  252.     goto batchfull;
  253.       }
  254.  
  255.       hs->current_batch_sz++;
  256.     }
  257.  
  258.   if (hs->current_batch_sz < hs->max_batch_sz &&
  259.       hs->next_expression < hs->num_expressions) {
  260.     /* We are going to have to plop in another expression. */
  261.     while (!parse_targets(&(hs->current_expression), hs->target_expressions[hs->next_expression++])) {
  262.       if (hs->next_expression >= hs->num_expressions)
  263.     break;
  264.     }     
  265.   } else break;
  266. } while(1);
  267.  batchfull:
  268.  
  269. if (hs->current_batch_sz == 0)
  270.   return NULL;
  271.  
  272. /* OK, now we have our complete batch of entries.  The next step is to
  273.    randomize them (if requested) */
  274. if (hs->randomize) {
  275.   hoststructfry(hs->hostbatch, hs->current_batch_sz);
  276. }
  277.  
  278. /* Finally we do the mass ping (if required) */
  279.  if ((o.pingtype == PINGTYPE_ICMP) || ((hs->hostbatch[0].host.s_addr || !o.isr00t) && (o.pingtype != PINGTYPE_NONE))) 
  280.    massping(hs->hostbatch, hs->current_batch_sz, ports);
  281.  else for(i=0; i < hs->current_batch_sz; i++)  {
  282.    hs->hostbatch[i].to.srtt = -1;
  283.    hs->hostbatch[i].to.rttvar = -1;
  284.    hs->hostbatch[i].to.timeout = o.initial_rtt_timeout * 1000;
  285.    hs->hostbatch[i].flags |= HOST_UP; /*hostbatch[i].up = 1;*/
  286.  }
  287.  return &hs->hostbatch[hs->next_batch_no++];
  288. }
  289.  
  290. /* Frees the *INTERNAL STRUCTURES* inside a hoststruct -- does not
  291.    free the actual memory allocated to the hoststruct itself (for all
  292.    this function knows, you could have declared it on the stack */
  293. void hoststruct_free(struct hoststruct *currenths) {
  294.   int i;
  295.  
  296.   /* Free the DNS name if we resolved one */
  297.   if (currenths->name && *currenths->name)
  298.     free(currenths->name);
  299.  
  300.   /* Free OS fingerprints of OS scanning was done */
  301.   for(i=0; i < currenths->numFPs; i++) {
  302.     freeFingerPrint(currenths->FPs[i]);
  303.     currenths->FPs[i] = NULL;
  304.   }
  305.   currenths->numFPs = 0;
  306.  
  307.   /* Free the port lists */
  308.   resetportlist(¤ths->ports);
  309.  
  310. }
  311.  
  312.  
  313.  
  314. int parse_targets(struct targets *targets, char *h) {
  315. int i=0,j=0,k=0;
  316. int start, end;
  317. char *r,*s, *target_net;
  318. char *addy[5];
  319. char *hostexp = strdup(h);
  320. struct hostent *target;
  321. unsigned long longtmp;
  322. int namedhost = 0;
  323.  
  324. bzero(targets, sizeof(struct targets));
  325. targets->nleft = 0;
  326. /*struct in_addr current_in;*/
  327. addy[0] = addy[1] = addy[2] = addy[3] = addy[4] = NULL;
  328. addy[0] = r = hostexp;
  329. /* First we break the expression up into the four parts of the IP address
  330.    + the optional '/mask' */
  331. target_net = strtok(hostexp, "/");
  332. s = strtok(NULL, "");    /* find the end of the token from hostexp */
  333. targets->netmask  = ( s ) ? atoi(s) : 32;
  334. if ((int) targets->netmask < 0 || targets->netmask > 32) {
  335.   fprintf(stderr, "Illegal netmask value (%d), must be /0 - /32 .  Assuming /32 (one host)\n", targets->netmask);
  336.   targets->netmask = 32;
  337. }
  338. for(i=0; *(hostexp + i); i++) 
  339.   if (isupper((int) *(hostexp +i)) || islower((int) *(hostexp +i))) {
  340.   namedhost = 1;
  341.   break;
  342. }
  343. if (targets->netmask != 32 || namedhost) {
  344.   targets->maskformat = 1;
  345.  if (!inet_aton(target_net, &(targets->start))) {
  346.     if ((target = gethostbyname(target_net)))
  347.       memcpy(&(targets->start), target->h_addr_list[0], sizeof(struct in_addr));
  348.     else {
  349.       fprintf(stderr, "Failed to resolve given hostname/IP: %s.  Note that you can't use '/mask' AND '[1-4,7,100-]' style IP ranges\n", target_net);
  350.       free(hostexp);
  351.       return 0;
  352.     }
  353.  } 
  354.  longtmp = ntohl(targets->start.s_addr);
  355.  targets->start.s_addr = longtmp & (unsigned long) (0 - (1<<(32 - targets->netmask)));
  356.  targets->end.s_addr = longtmp | (unsigned long)  ((1<<(32 - targets->netmask)) - 1);
  357.  targets->currentaddr = targets->start;
  358.  if (targets->start.s_addr <= targets->end.s_addr) { 
  359.    targets->nleft = targets->end.s_addr - targets->start.s_addr + 1;
  360.    free(hostexp); 
  361.    return 1; 
  362.  }
  363.  fprintf(stderr, "Host specification invalid");
  364.  free(hostexp);
  365.  return 0;
  366. }
  367. else {
  368.   i=0;
  369.   targets->maskformat = 0;
  370.   while(*++r) {
  371.     if (*r == '.' && ++i < 4) {
  372.       *r = '\0';
  373.       addy[i] = r + 1;
  374.     }
  375.     else if (*r == '[') {
  376.       *r = '\0';
  377.       addy[i]++;
  378.     }
  379.     else if (*r == ']') *r = '\0';
  380.     /*else if ((*r == '/' || *r == '\\') && i == 3) {
  381.      *r = '\0';
  382.      addy[4] = r + 1;
  383.      }*/
  384.     else if (*r != '*' && *r != ',' && *r != '-' && !isdigit((int)*r)) fatal("Invalid character in  host specification.");
  385.   }
  386.   if (i != 3) fatal("Target host specification is illegal.");
  387.   
  388.   for(i=0; i < 4; i++) {
  389.     j=0;
  390.     while((s = strchr(addy[i],','))) {
  391.       *s = '\0';
  392.       if (*addy[i] == '*') { start = 0; end = 255; } 
  393.       else if (*addy[i] == '-') {
  394.     start = 0;
  395.     if (!addy[i] + 1) end = 255;
  396.     else end = atoi(addy[i]+ 1);
  397.       }
  398.       else {
  399.     start = end = atoi(addy[i]);
  400.     if ((r = strchr(addy[i],'-')) && *(r+1) ) end = atoi(r + 1);
  401.     else if (r && !*(r+1)) end = 255;
  402.       }
  403.       if (o.debugging)
  404.     log_write(LOG_STDOUT, "The first host is %d, and the last one is %d\n", start, end);
  405.       if (start < 0 || start > end) fatal("Your host specifications are illegal!");
  406.       for(k=start; k <= end; k++)
  407.     targets->addresses[i][j++] = k;
  408.       addy[i] = s + 1;
  409.     }
  410.     if (*addy[i] == '*') { start = 0; end = 255; } 
  411.     else if (*addy[i] == '-') {
  412.       start = 0;
  413.       if (!addy[i] + 1) end = 255;
  414.       else end = atoi(addy[i]+ 1);
  415.     }
  416.     else {
  417.       start = end = atoi(addy[i]);
  418.       if ((r =  strchr(addy[i],'-')) && *(r+1) ) end = atoi(r+1);
  419.       else if (r && !*(r+1)) end = 255;
  420.     }
  421.     if (o.debugging)
  422.       log_write(LOG_STDOUT, "The first host is %d, and the last one is %d\n", start, end);
  423.     if (start < 0 || start > end) fatal("Your host specifications are illegal!");
  424.     if (j + (end - start) > 255) fatal("Your host specifications are illegal!");
  425.     for(k=start; k <= end; k++) 
  426.       targets->addresses[i][j++] = k;
  427.     targets->last[i] = j - 1;
  428.     
  429.   }
  430. }
  431.   bzero((char *)targets->current, 4);
  432.   targets->nleft = (targets->last[0] + 1) * (targets->last[1] + 1) *
  433.     (targets->last[2] + 1) * (targets->last[3] + 1);
  434.   free(hostexp);
  435.   return 1;
  436. }
  437.  
  438.  
  439. void massping(struct hoststruct *hostbatch, int num_hosts, 
  440.               struct scan_lists *ports) {
  441. static struct timeout_info to = { 0,0,0};
  442. static int gsize = LOOKAHEAD;
  443. int hostnum;
  444. struct pingtune pt;
  445. struct scanstats ss;
  446. struct timeval begin_select;
  447. struct pingtech ptech;
  448. struct tcpqueryinfo tqi;
  449. int max_block_size = 40;
  450. struct ppkt {
  451.   unsigned char type;
  452.   unsigned char code;
  453.   unsigned short checksum;
  454.   unsigned short id;
  455.   unsigned short seq;
  456. };
  457. int elapsed_time;
  458. int blockinc;
  459. int sd_blocking = 1;
  460. struct sockaddr_in sock;
  461. short seq = 0;
  462. int sd = -1, rawsd = -1, rawpingsd = -1;
  463. struct timeval *time;
  464. struct timeval start, end, t2;
  465. unsigned short id;
  466. pcap_t *pd = NULL;
  467. char filter[512];
  468. unsigned short sportbase;
  469. int max_width = 0;
  470.  
  471. bzero((char *)&ptech, sizeof(struct pingtech));
  472.  
  473. bzero((char *) &pt, sizeof(struct pingtune)); 
  474.  
  475. pt.up_this_block = 0;
  476. pt.block_unaccounted = LOOKAHEAD;
  477. pt.discardtimesbefore = 0;
  478. pt.down_this_block = 0;
  479. pt.num_responses = 0;
  480. pt.max_tries = 5; /* Maximum number of tries for a block */
  481. pt.group_size = (o.max_parallelism)? MIN(o.max_parallelism, gsize) : gsize;
  482. pt.group_start = 0;
  483. pt.block_tries = 0; /* How many tries this block has gone through */
  484.  
  485. /* What port should we send from? */
  486. if (o.magic_port_set) sportbase = o.magic_port;
  487. else sportbase = o.magic_port + 20;
  488.  
  489. /* What kind of scans are we doing? */
  490. if ((o.pingtype & PINGTYPE_ICMP) &&  hostbatch[0].source_ip.s_addr) 
  491.   ptech.rawicmpscan = 1;
  492. else if (o.pingtype & PINGTYPE_ICMP) 
  493.   ptech.icmpscan = 1;
  494. if (o.pingtype & PINGTYPE_TCP) {
  495.   if (o.isr00t)
  496.     ptech.rawtcpscan = 1;
  497.   else ptech.connecttcpscan = 1;
  498. }
  499.  
  500. time = (struct timeval *) safe_malloc(sizeof(struct timeval) * ((pt.max_tries) * num_hosts));
  501. bzero(time, sizeof(struct timeval) * pt.max_tries * num_hosts);
  502. id = (unsigned short) get_random_uint();
  503.  
  504. if (ptech.connecttcpscan)  {
  505.   max_width = (o.max_parallelism)? o.max_parallelism : MAX(1, max_sd() - 4);
  506.   max_block_size = MIN(50, max_width);
  507. }
  508.  
  509.  
  510. bzero((char *)&tqi, sizeof(tqi));
  511. if (ptech.connecttcpscan) {
  512.   tqi.sockets = (int *) safe_malloc(sizeof(int) * (pt.max_tries) * num_hosts);
  513.   memset(tqi.sockets, 255, sizeof(int) * (pt.max_tries) * num_hosts);
  514.   FD_ZERO(&(tqi.fds_r));
  515.   FD_ZERO(&(tqi.fds_w));
  516.   FD_ZERO(&(tqi.fds_x));
  517.   tqi.sockets_out = 0;
  518.   tqi.maxsd = 0;
  519. }
  520.  
  521. if (ptech.icmpscan) {
  522.   sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  523.   if (sd < 0) pfatal("Socket trouble in massping"); 
  524.   unblock_socket(sd);
  525.   sd_blocking = 0;
  526.   if (num_hosts > 10)
  527.     max_rcvbuf(sd);
  528.   broadcast_socket(sd);
  529. } else sd = -1;
  530.  
  531.  
  532. /* if to timeout structure hasn't been initialized yet */
  533. if (!to.srtt && !to.rttvar && !to.timeout) {
  534.   /*  to.srtt = 800000;
  535.       to.rttvar = 500000; */ /* we will init these when we get real data */
  536.   to.timeout = o.initial_rtt_timeout * 1000;
  537.   to.srtt = -1;
  538.   to.rttvar = -1;
  539.  
  540. /* Init our raw socket */
  541. if (o.numdecoys > 1 || ptech.rawtcpscan || ptech.rawicmpscan) {
  542.   if ((rawsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 )
  543.     pfatal("socket trobles in massping");
  544.   broadcast_socket(rawsd);
  545.  
  546.   
  547.   if ((rawpingsd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 )
  548.     pfatal("socket trobles in massping");
  549.   broadcast_socket(rawpingsd);
  550. }
  551.  else { rawsd = -1; rawpingsd = -1; }
  552.  
  553. if (ptech.rawicmpscan || ptech.rawtcpscan) {
  554.   /* we need a pcap descript0r! */
  555.   /* MAX snaplen needed = 
  556.      24 bytes max link_layer header
  557.      64 bytes max IPhdr
  558.      16 bytes of the TCP header
  559.      ---
  560.    = 104 byte snaplen */
  561.   pd = my_pcap_open_live(hostbatch[0].device, 104, o.spoofsource, 20);
  562.  
  563.   flt_dsthost = hostbatch[0].source_ip.s_addr;
  564.   flt_baseport = sportbase;
  565.  
  566.   snprintf(filter, sizeof(filter), "(icmp and dst host %s) or (tcp and dst host %s and ( dst port %d or dst port %d or dst port %d or dst port %d or dst port %d))", 
  567.       inet_ntoa(hostbatch[0].source_ip),inet_ntoa(hostbatch[0].source_ip),
  568.       sportbase , sportbase + 1, sportbase + 2, sportbase + 3, 
  569.       sportbase + 4);
  570.  
  571.   set_pcap_filter(hostbatch, pd, flt_icmptcp_5port, filter); 
  572. }
  573.  
  574.  if (ptech.rawicmpscan + ptech.icmpscan + ptech.connecttcpscan +
  575.      ptech.rawtcpscan == 1)
  576.    blockinc = 8;
  577.  else blockinc = 5;
  578.  
  579. bzero((char *)&sock,sizeof(struct sockaddr_in));
  580. gettimeofday(&start, NULL);
  581.  
  582.  pt.group_end = MIN(pt.group_start + pt.group_size -1, num_hosts -1);
  583.  
  584.  while(pt.group_start < num_hosts) { /* while we have hosts left to scan */
  585.    do { /* one block */
  586.      pt.discardtimesbefore = -1;
  587.      pt.up_this_block = 0;
  588.      pt.down_this_block = 0;
  589.      pt.block_unaccounted = 0;
  590.      for(hostnum=pt.group_start; hostnum <= pt.group_end; hostnum++) {      
  591.        /* If (we don't know whether the host is up yet) ... */
  592.        if (!(hostbatch[hostnum].flags & HOST_UP) && !hostbatch[hostnum].wierd_responses && !(hostbatch[hostnum].flags & HOST_DOWN)) {  
  593.      /* Send a ping packet to it */
  594.      seq = hostnum * pt.max_tries + pt.block_tries;
  595.      if (ptech.icmpscan && !sd_blocking) { 
  596.        block_socket(sd); sd_blocking = 1; 
  597.      }
  598.      if (o.scan_delay) enforce_scan_delay(NULL);
  599.      if (ptech.icmpscan || ptech.rawicmpscan)
  600.        sendpingquery(sd, rawpingsd, &hostbatch[hostnum],  
  601.              seq, id, &ss, time, ptech);
  602.        
  603.      if (ptech.rawtcpscan) {
  604.        sendrawtcppingquery(rawsd, &hostbatch[hostnum],  seq, time, &pt);
  605.      }
  606.      else if (ptech.connecttcpscan) {
  607.        sendconnecttcpquery(hostbatch, &tqi, &hostbatch[hostnum], seq, time, &pt, &to, max_width);
  608.      }
  609.      pt.block_unaccounted++;
  610.      gettimeofday(&t2, NULL);
  611.      if (TIMEVAL_SUBTRACT(t2,time[seq]) > 1000000) {
  612.        pt.discardtimesbefore = hostnum;
  613.        if (o.debugging) 
  614.          log_write(LOG_STDOUT, "Huge send delay: %lu microseconds\n", (unsigned long) TIMEVAL_SUBTRACT(t2,time[seq]));
  615.      }
  616.        }
  617.      } /* for() loop */
  618.      /* OK, we have sent our ping packets ... now we wait for responses */
  619.      gettimeofday(&begin_select, NULL);
  620.      do {
  621.        if (ptech.icmpscan && sd_blocking ) { 
  622.      unblock_socket(sd); sd_blocking = 0; 
  623.        }
  624.        if(ptech.icmpscan || ptech.rawicmpscan || ptech.rawtcpscan) {       
  625.      get_ping_results(sd, pd, hostbatch, time, &pt, &to, id, &ptech, 
  626.               ports);
  627.        }
  628.        if (ptech.connecttcpscan) {
  629.      get_connecttcpscan_results(&tqi, hostbatch, time, &pt, &to);
  630.        }
  631.        gettimeofday(&end, NULL);
  632.        elapsed_time = TIMEVAL_SUBTRACT(end, begin_select);
  633.      } while( elapsed_time < to.timeout);
  634.      /* try again if a new box was found but some are still unaccounted for and
  635.     we haven't run out of retries.  Also retry if the block is extremely
  636.         small.
  637.      */
  638.      pt.dropthistry = 0;
  639.      pt.block_tries++;
  640.    } while ((pt.up_this_block > 0 || pt.group_end - pt.group_start <= 3) && pt.block_unaccounted > 0 && pt.block_tries < pt.max_tries);
  641.  
  642.    if (o.debugging)
  643.      log_write(LOG_STDOUT, "Finished block: srtt: %d rttvar: %d timeout: %d block_tries: %d up_this_block: %d down_this_block: %d group_sz: %d\n", to.srtt, to.rttvar, to.timeout, pt.block_tries, pt.up_this_block, pt.down_this_block, pt.group_end - pt.group_start + 1);
  644.  
  645.    if ((pt.block_tries == 1) || (pt.block_tries == 2 && pt.up_this_block == 0 && pt.down_this_block == 0)) 
  646.      /* Then it did not miss any hosts (that we know of)*/
  647.        pt.group_size = MIN(pt.group_size + blockinc, max_block_size);
  648.    
  649.    /* Move to next block */
  650.    pt.block_tries = 0;
  651.    pt.group_start = pt.group_end +1;
  652.    pt.group_end = MIN(pt.group_start + pt.group_size -1, num_hosts -1);
  653.    /*   pt.block_unaccounted = pt.group_end - pt.group_start + 1;   */
  654.  }
  655.  
  656.  close(sd);
  657.  if (ptech.connecttcpscan) free(tqi.sockets);
  658.  if (sd >= 0) close(sd);
  659.  if (rawsd >= 0) close(rawsd);
  660.  if (rawpingsd >= 0) close(rawpingsd);
  661.  free(time);
  662.  if (pd) pcap_close(pd);
  663.  if (o.debugging) 
  664.    log_write(LOG_STDOUT, "massping done:  num_hosts: %d  num_responses: %d\n", num_hosts, pt.num_responses);
  665.  gsize = pt.group_size;
  666.  return;
  667. }
  668.  
  669. int sendconnecttcpquery(struct hoststruct *hostbatch, struct tcpqueryinfo *tqi,
  670.             struct hoststruct *target, int seq, 
  671.             struct timeval *time, struct pingtune *pt, 
  672.             struct timeout_info *to, int max_width) {
  673.  
  674.   int res,i;
  675.   int tmpsd;
  676.   int hostnum, trynum;
  677.   struct sockaddr_in sock;
  678.   int sockaddr_in_len = sizeof(struct sockaddr_in);
  679.   
  680.   trynum = seq % pt->max_tries;
  681.   hostnum = seq / pt->max_tries;
  682.  
  683.   assert(tqi->sockets_out <= max_width);
  684.   if (tqi->sockets_out == max_width) {
  685.     /* We've got to free one! */
  686.     for(i=0; i < trynum; i++) {
  687.       tmpsd = hostnum * pt->max_tries + i;
  688.       if (tqi->sockets[tmpsd] >= 0) {
  689.     if (o.debugging) 
  690.       log_write(LOG_STDOUT, "sendconnecttcpquery: Scavenging a free socket due to serious shortage\n");
  691.     close(tqi->sockets[tmpsd]);
  692.     tqi->sockets[tmpsd] = -1;
  693.     tqi->sockets_out--;
  694.     break;
  695.       }
  696.     }
  697.     if (i == trynum)
  698.       fatal("sendconnecttcpquery: Could not scavenge a free socket!");
  699.   }
  700.     
  701.   /* Since we know we now have a free s0cket, lets take it */
  702.  
  703.   assert(tqi->sockets[seq] == -1);
  704.   tqi->sockets[seq] =  socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  705.   if (tqi->sockets[seq] == -1) 
  706.     fatal("Socket creation in sendconnecttcpquery");
  707.   tqi->maxsd = MAX(tqi->maxsd, tqi->sockets[seq]);
  708.   tqi->sockets_out++;
  709.   unblock_socket(tqi->sockets[seq]);
  710.   init_socket(tqi->sockets[seq]);
  711.  
  712.   bzero(&sock, sockaddr_in_len);
  713.   sock.sin_family = AF_INET;
  714.   sock.sin_port = htons(o.tcp_probe_port);
  715.   sock.sin_addr.s_addr = target->host.s_addr;
  716.   
  717.   res = connect(tqi->sockets[seq],(struct sockaddr *)&sock,sizeof(struct sockaddr));
  718.   gettimeofday(&time[seq], NULL);
  719.  
  720.   if ((res != -1 || errno == ECONNREFUSED)) {
  721.     /* This can happen on localhost, successful/failing connection immediately
  722.        in non-blocking mode */
  723.       hostupdate(hostbatch, target, HOST_UP, 1, trynum, to, 
  724.          &time[seq], pt, tqi, PINGTYPE_CONNECTTCP);
  725.     if (tqi->maxsd == tqi->sockets[seq]) tqi->maxsd--;
  726.   }
  727.   else if (errno == ENETUNREACH) {
  728.     if (o.debugging) 
  729.       error("Got ENETUNREACH from sendconnecttcpquery connect()");
  730.     hostupdate(hostbatch, target, HOST_DOWN, 1, trynum, to, 
  731.            &time[seq], pt, tqi, PINGTYPE_CONNECTTCP);
  732.   }
  733.   else {
  734.     /* We'll need to select() and wait it out */
  735.     FD_SET(tqi->sockets[seq], &(tqi->fds_r));
  736.     FD_SET(tqi->sockets[seq], &(tqi->fds_w));
  737.     FD_SET(tqi->sockets[seq], &(tqi->fds_x));
  738.   }
  739. return 0;
  740. }
  741.  
  742. int sendrawtcppingquery(int rawsd, struct hoststruct *target, int seq,
  743.             struct timeval *time, struct pingtune *pt) {
  744. int trynum;
  745. int myseq;
  746. unsigned short sportbase;
  747. unsigned long myack = get_random_uint();
  748.  
  749. if (o.magic_port_set) sportbase = o.magic_port;
  750. else sportbase = o.magic_port + 20;
  751. trynum = seq % pt->max_tries;
  752.  
  753.  myseq = (get_random_uint() << 19) + (seq << 3) + 3; /* Response better end in 011 or 100 */
  754.  memcpy((char *)&(o.decoys[o.decoyturn]), (char *)&target->source_ip, sizeof(struct in_addr));
  755.  if (o.pingtype & PINGTYPE_TCP_USE_SYN) {   
  756.    send_tcp_raw_decoys( rawsd, &(target->host), sportbase + trynum, o.tcp_probe_port, myseq, myack, TH_SYN, 0, NULL, 0, o.extra_payload, 
  757.             o.extra_payload_length);
  758.  } else {
  759.    send_tcp_raw_decoys( rawsd, &(target->host), sportbase + trynum, o.tcp_probe_port, myseq, myack, TH_ACK, 0, NULL, 0, o.extra_payload, 
  760.             o.extra_payload_length);
  761.  }
  762.  
  763.  gettimeofday(&time[seq], NULL);
  764.  return 0;
  765. }
  766.  
  767.  
  768. int sendpingquery(int sd, int rawsd, struct hoststruct *target,  
  769.           int seq, unsigned short id, struct scanstats *ss, 
  770.           struct timeval *time, struct pingtech ptech) {
  771.   
  772. struct ppkt {
  773.   unsigned char type;
  774.   unsigned char code;
  775.   unsigned short checksum;
  776.   unsigned short id;
  777.   unsigned short seq;
  778. } pingpkt;
  779. int decoy;
  780. int res;
  781. struct sockaddr_in sock;
  782. char *ping = (char *) &pingpkt;
  783.  
  784. /* Fill out the ping packet */
  785. pingpkt.type = 8;
  786. pingpkt.code = 0;
  787. pingpkt.id = id;
  788. pingpkt.seq = seq;
  789. pingpkt.checksum = 0;
  790. pingpkt.checksum = in_cksum((unsigned short *)ping, 8);
  791.  
  792. /* Now for our sock */
  793. if (ptech.icmpscan) {
  794.   bzero((char *)&sock, sizeof(struct sockaddr_in));
  795.   sock.sin_family= AF_INET;
  796.   sock.sin_addr = target->host;
  797.   
  798.   if (sizeof(struct ppkt) != 8) 
  799.     fatal("Your native data type sizes are too screwed up for this to work.");
  800. } else {
  801.     memcpy((char *) &(o.decoys[o.decoyturn]), (char *)&target->source_ip, sizeof(struct in_addr));
  802. }
  803.  
  804.  for (decoy = 0; decoy < o.numdecoys; decoy++) {
  805.    if (ptech.icmpscan && decoy == o.decoyturn) {
  806.      /* FIXME: If EHOSTUNREACH (Windows does that) then we were
  807.     probably unable to obtain an arp response from the machine.
  808.     We should just considering the host down rather than ignoring
  809.     the error */
  810.      if ((res = sendto(sd,(char *) ping,8,0,(struct sockaddr *)&sock,
  811.                sizeof(struct sockaddr))) != 8 && errno != EHOSTUNREACH ) {
  812.        fprintf(stderr, "sendto in sendpingquery returned %d (should be 8)!\n", res);
  813.        perror("sendto");
  814.      }
  815.    } else {
  816.      send_ip_raw( rawsd, &o.decoys[decoy], &(target->host), IPPROTO_ICMP, ping, 8);
  817.    }
  818.  }
  819.  gettimeofday(&time[seq], NULL);
  820.  return 0;
  821. }
  822.  
  823. int get_connecttcpscan_results(struct tcpqueryinfo *tqi, 
  824.                    struct hoststruct *hostbatch, 
  825.                    struct timeval *time, struct pingtune *pt, 
  826.                    struct timeout_info *to) {
  827.  
  828. int res, res2;
  829. int tm;
  830. struct timeval myto, start, end;
  831. int hostindex;
  832. int trynum, newstate = HOST_DOWN;
  833. int seq;
  834. char buf[256];
  835. int foundsomething = 0;
  836. fd_set myfds_r,myfds_w,myfds_x;
  837. gettimeofday(&start, NULL);
  838.  
  839. while(pt->block_unaccounted) {
  840.  
  841.   /* OK so there is a little fudge factor, SUE ME! */
  842.   myto.tv_sec  = to->timeout / 1000000; 
  843.   myto.tv_usec = to->timeout % 1000000;
  844.   foundsomething = 0;
  845.   myfds_r = tqi->fds_r;
  846.   myfds_w = tqi->fds_w;
  847.   myfds_x = tqi->fds_x;
  848.   res = select(tqi->maxsd + 1, &myfds_r, &myfds_w, &myfds_x, &myto);
  849.   if (res > 0) {
  850.     for(hostindex = pt->group_start; hostindex <= pt->group_end; hostindex++) {
  851.       for(trynum=0; trynum <= pt->block_tries; trynum++) {
  852.     seq = hostindex * pt->max_tries + trynum;
  853.     if (tqi->sockets[seq] >= 0) {
  854.       if (o.debugging > 1) {
  855.         if (FD_ISSET(tqi->sockets[seq], &(myfds_r))) {
  856.           log_write(LOG_STDOUT, "WRITE selected for machine %s\n", inet_ntoa(hostbatch[hostindex].host));  
  857.         }
  858.         if ( FD_ISSET(tqi->sockets[seq], &myfds_w)) {
  859.           log_write(LOG_STDOUT, "READ selected for machine %s\n", inet_ntoa(hostbatch[hostindex].host)); 
  860.         }
  861.         if  ( FD_ISSET(tqi->sockets[seq], &myfds_x)) {
  862.           log_write(LOG_STDOUT, "EXC selected for machine %s\n", inet_ntoa(hostbatch[hostindex].host));
  863.         }
  864.       }
  865.       if (FD_ISSET(tqi->sockets[seq], &myfds_r) || FD_ISSET(tqi->sockets[seq], &myfds_w) ||  FD_ISSET(tqi->sockets[seq], &myfds_x)) {
  866.         foundsomething = 0;
  867.         res2 = read(tqi->sockets[seq], buf, sizeof(buf));
  868.         if (res2 == -1) {
  869.           switch(errno) {
  870.           case ECONNREFUSED:
  871.           case EAGAIN:
  872. #ifdef WIN32
  873. //          case WSAENOTCONN:    //    needed?  this fails around here on my system
  874. #endif
  875.         if (errno == EAGAIN && o.verbose) {
  876.           log_write(LOG_STDOUT, "Machine %s MIGHT actually be listening on probe port %d\n", inet_ntoa(hostbatch[hostindex].host), o.tcp_probe_port);
  877.         }
  878.         foundsomething = 1;
  879.         newstate = HOST_UP;    
  880.         break;
  881.           case ENETDOWN:
  882.           case ENETUNREACH:
  883.           case ENETRESET:
  884.           case ECONNABORTED:
  885.           case ETIMEDOUT:
  886.           case EHOSTDOWN:
  887.           case EHOSTUNREACH:
  888.         foundsomething = 1;
  889.         newstate = HOST_DOWN;
  890.         break;
  891.           default:
  892.         snprintf (buf, sizeof(buf), "Strange read error from %s", inet_ntoa(hostbatch[hostindex].host));
  893.         perror(buf);
  894.         break;
  895.           }
  896.         } else { 
  897.           foundsomething = 1;
  898.           newstate = HOST_UP;
  899.           if (o.verbose) {          
  900.         buf[res2] = '\0';
  901.         if (res2 == 0)
  902.           log_write(LOG_STDOUT, "Machine %s is actually LISTENING on probe port %d\n",
  903.              inet_ntoa(hostbatch[hostindex].host), 
  904.              o.tcp_probe_port);
  905.         else 
  906.           log_write(LOG_STDOUT, "Machine %s is actually LISTENING on probe port %d, banner: %s\n",
  907.              inet_ntoa(hostbatch[hostindex].host), 
  908.              o.tcp_probe_port, buf);
  909.           }
  910.         }
  911.         if (foundsomething) {
  912.           hostupdate(hostbatch, &hostbatch[hostindex], newstate, 1, trynum,
  913.              to,  &time[seq], pt, tqi, PINGTYPE_CONNECTTCP);
  914.           /*          break;*/
  915.         }
  916.       }
  917.     }
  918.       }
  919.     }
  920.   }
  921.   gettimeofday(&end, NULL);
  922.   tm = TIMEVAL_SUBTRACT(end,start);  
  923.   if (tm > (30 * to->timeout)) {
  924.     error("WARNING: getconnecttcpscanresults is taking way too long, skipping");
  925.     break;
  926.   }
  927.   if (res == 0 &&  tm > to->timeout) break; 
  928. }
  929.  
  930. /* OK, now we have to kill all outstanding queries to make room for
  931.    the next group :( I'll miss these little guys. */
  932.  for(hostindex = pt->group_start; hostindex <= pt->group_end; hostindex++) { 
  933.       for(trynum=0; trynum <= pt->block_tries; trynum++) {
  934.     seq = hostindex * pt->max_tries + trynum;
  935.     if ( tqi->sockets[seq] >= 0) {
  936.       tqi->sockets_out--;
  937.       close(tqi->sockets[seq]);
  938.       tqi->sockets[seq] = -1;
  939.     }
  940.       }
  941.  }
  942.  tqi->maxsd = 0;
  943.  assert(tqi->sockets_out == 0);
  944.  FD_ZERO(&(tqi->fds_r));
  945.  FD_ZERO(&(tqi->fds_w));
  946.  FD_ZERO(&(tqi->fds_x));
  947.      
  948. return 0;
  949. }
  950.  
  951.  
  952. int get_ping_results(int sd, pcap_t *pd, struct hoststruct *hostbatch, struct timeval *time,  struct pingtune *pt, struct timeout_info *to, int id, struct pingtech *ptech, struct scan_lists *ports) {
  953. fd_set fd_r, fd_x;
  954. struct timeval myto, tmpto, start, end;
  955. unsigned int bytes;
  956. int res;
  957. struct ppkt {
  958.   unsigned char type;
  959.   unsigned char code;
  960.   unsigned short checksum;
  961.   unsigned short id;
  962.   unsigned short seq;
  963. } *ping = NULL, *ping2 = NULL;
  964. char response[16536]; 
  965. struct tcphdr *tcp;
  966. struct ip *ip, *ip2;
  967. int hostnum = -99999; /* This ought to crash us if it is used uninitialized */
  968. int tm;
  969. int dotimeout = 1;
  970. int newstate = HOST_DOWN;
  971. int foundsomething;
  972. unsigned short newport;
  973. int newportstate; /* Hack so that in some specific cases we can determine the 
  974.              state of a port and even skip the real scan */
  975. int trynum = -999999;
  976. int pingtype = -999999;
  977. int timeout = 0;
  978. unsigned short sequence = 65534;
  979. unsigned long tmpl;
  980. unsigned short sportbase;
  981.  
  982.  
  983. FD_ZERO(&fd_r);
  984. FD_ZERO(&fd_x);
  985.  
  986. /* Decide on the timeout, based on whether we need to also watch for TCP stuff */
  987. if (ptech->icmpscan && !ptech->rawtcpscan) {
  988.   /* We only need to worry about pings, so we set timeout for the whole she-bang! */
  989.   myto.tv_sec  = to->timeout / 1000000;
  990.   myto.tv_usec = to->timeout % 1000000;
  991. } else {
  992.   myto.tv_sec = 0;
  993.   myto.tv_usec = 20000;
  994. }
  995.  
  996. if (o.magic_port_set) sportbase = o.magic_port;
  997. else sportbase = o.magic_port + 20;
  998.  
  999. gettimeofday(&start, NULL);
  1000. newport = 0;
  1001. newportstate = PORT_UNKNOWN;
  1002.  
  1003. while(pt->block_unaccounted > 0 && !timeout) {
  1004.   tmpto = myto;
  1005.  
  1006.   if (pd) {
  1007.     ip = (struct ip *) readip_pcap(pd, &bytes, to->timeout);
  1008.   } else {    
  1009.     FD_SET(sd, &fd_r);
  1010.     FD_SET(sd, &fd_x);
  1011.     res = select(sd+1, &fd_r, NULL, &fd_x, &tmpto);
  1012.     if (res == 0) break;
  1013.     bytes = read(sd,&response,sizeof(response));
  1014.     ip = (struct ip *) &(response);
  1015.   }
  1016.  
  1017.   gettimeofday(&end, NULL);
  1018.   tm = TIMEVAL_SUBTRACT(end,start);  
  1019.   if (tm > (MAX(400000,3 * to->timeout)))
  1020.     timeout = 1;
  1021.   if (bytes == 0 &&  tm > to->timeout) {  
  1022.     timeout = 1;
  1023.   }
  1024.   if (bytes == 0)
  1025.     continue;
  1026.  
  1027.   if (bytes > 0 && bytes <= 20) {  
  1028.     error("%d byte micro packet received in get_ping_results");
  1029.     continue;
  1030.   }  
  1031.  
  1032.   foundsomething = 0;
  1033.   dotimeout = 0;
  1034.   
  1035.   /* First check if it is ICMP or TCP */
  1036.   if (ip->ip_p == IPPROTO_ICMP) {    
  1037.     /* if it is our response */
  1038.     ping = (struct ppkt *) ((ip->ip_hl * 4) + (char *) ip);
  1039.     if (bytes < ip->ip_hl * 4 + 8U) {
  1040.       error("Supposed ping packet is only %d bytes long!", bytes);
  1041.       continue;
  1042.     }
  1043.     if  ( !ping->type && !ping->code && ping->id == id) {
  1044.       hostnum = ping->seq / pt->max_tries;
  1045.       if (hostnum > pt->group_end) {
  1046.     if (o.debugging) 
  1047.       error("Ping sequence %d leads to hostnum %d which is beyond the end of this group (%d)", ping->seq, hostnum, pt->group_end);
  1048.     continue;
  1049.       }
  1050.       if (!hostbatch[hostnum].source_ip.s_addr)
  1051.     hostbatch[hostnum].source_ip.s_addr = ip->ip_dst.s_addr;
  1052.       if (o.debugging) 
  1053.     log_write(LOG_STDOUT, "We got a ping packet back from %s: id = %d seq = %d checksum = %d\n", inet_ntoa(ip->ip_src), ping->id, ping->seq, ping->checksum);
  1054.       if (hostbatch[hostnum].host.s_addr == ip->ip_src.s_addr) {
  1055.     foundsomething = 1;
  1056.     pingtype = PINGTYPE_ICMP;
  1057.     sequence = ping->seq;
  1058.     newstate = HOST_UP;
  1059.     trynum = sequence % pt->max_tries;
  1060.     if (pt->discardtimesbefore < ping->seq)
  1061.       dotimeout = 1;
  1062.     else dotimeout = 0;
  1063.       }
  1064.       else hostbatch[hostnum].wierd_responses++;
  1065.     }
  1066.     else if (ping->type == 3 || ping->type == 11 || ping->type == 4 || 
  1067.          o.debugging) {
  1068.       if (bytes <  ip->ip_hl * 4 + 28U) {
  1069.     if (o.debugging)
  1070.       error("ICMP type %d code %d packet is only %d bytes\n", ping->type, ping->code, bytes);
  1071.     continue;
  1072.       }
  1073.  
  1074.       ip2 = (struct ip *) ((char *)ip + ip->ip_hl * 4 + 8);
  1075.       if (bytes < ip->ip_hl * 4 + 8U + ip2->ip_hl * 4 + 8U) {
  1076.     if (o.debugging)
  1077.       error("ICMP type %d code %d packet is only %d bytes\n", ping->type, ping->code, bytes);
  1078.     continue;
  1079.       }
  1080.       
  1081.       if (ip2->ip_p == IPPROTO_ICMP) {
  1082.     /* The response was based on a ping packet we sent */
  1083.     if (!ptech->icmpscan) {
  1084.       if (o.debugging)
  1085.         error("Got ICMP error referring to ICMP msg which we did not send");
  1086.       continue;
  1087.     }
  1088.     ping2 = (struct ppkt *) ((char *)ip2 + ip2->ip_hl * 4);
  1089.     if (ping2->id != id) {
  1090.       if (o.debugging) {    
  1091.         error("Illegal id %d found, should be %d (icmp type/code %d/%d)", ping2->id, id, ping->type, ping->code);
  1092.         if (o.debugging > 1)
  1093.           lamont_hdump((unsigned char *)ip, bytes);
  1094.       }
  1095.       continue;
  1096.     }
  1097.     sequence = ping2->seq;
  1098.     hostnum = sequence / pt->max_tries;
  1099.     trynum = sequence % pt->max_tries;
  1100.  
  1101.       } else if (ip2->ip_p == IPPROTO_TCP) {
  1102.     /* The response was based our TCP probe */
  1103.     if (!ptech->rawtcpscan) {
  1104.       if (o.debugging)
  1105.         error("Got ICMP error referring to TCP msg which we did not send");
  1106.       continue;
  1107.     }
  1108.     tcp = (struct tcphdr *) (((char *) ip2) + 4 * ip2->ip_hl);
  1109.     /* No need to check size here, the "+8" check a ways up takes care 
  1110.        of it */
  1111.     newport = ntohs(tcp->th_dport);
  1112.     
  1113.     trynum = ntohs(tcp->th_sport) - sportbase;
  1114.     if (trynum >= pt->max_tries) {
  1115.       if (o.debugging)
  1116.         error("Bogus trynum %d", trynum);
  1117.       continue;
  1118.     }
  1119.  
  1120.     /* Grab the sequence nr */
  1121.     tmpl = ntohl(tcp->th_seq);
  1122.     
  1123.     if ((tmpl & 7) == 3) {
  1124.       sequence = (tmpl >> 3) & 0xffff;
  1125.       hostnum = sequence / pt->max_tries;
  1126.       trynum = sequence % pt->max_tries;
  1127.     } else {
  1128.       if (o.debugging) {
  1129.         error("Whacked seq number from %s", inet_ntoa(ip->ip_src));
  1130.       }
  1131.       continue;    
  1132.     }    
  1133.       } else {
  1134.     if (o.debugging)
  1135.       error("Got ICMP response to a packet which was not ICMP or TCP");
  1136.     continue;
  1137.       }
  1138.  
  1139.       if (hostnum > pt->group_end) {
  1140.     if (o.debugging)
  1141.       error("Bogus ping sequence: %d leads to bogus hostnum %d (icmp type/code %d/%d", sequence, hostnum, ping->type, ping->code);
  1142.     continue;
  1143.       }
  1144.         
  1145.       if (ping->type == 3) {
  1146.     if (o.debugging) 
  1147.       log_write(LOG_STDOUT, "Got destination unreachable for %s\n", inet_ntoa(hostbatch[hostnum].host));
  1148.     /* Since this gives an idea of how long it takes to get an answer,
  1149.        we add it into our times */
  1150.     if (pt->discardtimesbefore < sequence)
  1151.       dotimeout = 1;    
  1152.     foundsomething = 1;
  1153.     pingtype = PINGTYPE_ICMP;
  1154.     newstate = HOST_DOWN;
  1155.     newportstate = PORT_FIREWALLED;
  1156.       } else if (ping->type == 11) {
  1157.     if (o.debugging) 
  1158.       log_write(LOG_STDOUT, "Got Time Exceeded for %s\n", inet_ntoa(hostbatch[hostnum].host));
  1159.     dotimeout = 0; /* I don't want anything to do with timing this */
  1160.     foundsomething = 1;
  1161.     pingtype = PINGTYPE_ICMP;
  1162.     newstate = HOST_DOWN;
  1163.       }
  1164.       else if (ping->type == 4) {      
  1165.     if (o.debugging) log_write(LOG_STDOUT, "Got ICMP source quench\n");
  1166.     usleep(50000);
  1167.       }  
  1168.       else if (o.debugging > 0) {
  1169.     log_write(LOG_STDOUT, "Got ICMP message type %d code %d\n", ping->type, ping->code);
  1170.       }
  1171.     }
  1172.   } else if (ip->ip_p == IPPROTO_TCP) 
  1173.     {
  1174.       if (!ptech->rawtcpscan) {
  1175.     continue;
  1176.       }
  1177.       tcp = (struct tcphdr *) (((char *) ip) + 4 * ip->ip_hl);
  1178.       if (!(tcp->th_flags & TH_RST) && ((tcp->th_flags & (TH_SYN|TH_ACK)) != (TH_SYN|TH_ACK)))
  1179.     continue;
  1180.       newport = ntohs(tcp->th_sport);
  1181.       tmpl = ntohl(tcp->th_ack);
  1182.       /* Grab the sequence nr */
  1183.       if (o.pingtype & PINGTYPE_TCP_USE_SYN) {      
  1184.     if ((tmpl & 7) == 4 || (tmpl & 7) == 3) {
  1185.       sequence = (tmpl >> 3) & 0xffff;
  1186.       hostnum = sequence / pt->max_tries;
  1187.       trynum = sequence % pt->max_tries;
  1188.     } else {
  1189.       if (o.debugging) {
  1190.         error("Whacked ACK number from %s", inet_ntoa(ip->ip_src));
  1191.       }
  1192.       continue;    
  1193.     }
  1194.       } else {
  1195.     trynum = ntohs(tcp->th_dport) - sportbase;
  1196.     if (trynum >= pt->max_tries) {
  1197.       if (o.debugging)
  1198.         error("Bogus trynum %d", trynum);
  1199.       continue;
  1200.     }
  1201.     /* FUDGE!  This ACK scan is cool but we don't get sequence numbers
  1202.        back! We'll have to brute force lookup to find the hostnum */
  1203.     for(hostnum = pt->group_end; hostnum >= 0; hostnum--) {
  1204.       if (hostbatch[hostnum].host.s_addr == ip->ip_src.s_addr)
  1205.         break;
  1206.     }
  1207.     if (hostnum < 0) {    
  1208.       if (o.debugging > 1) 
  1209.         error("Warning, unexpacted packet from machine %s", inet_ntoa(ip->ip_src));
  1210.       continue;
  1211.     }    
  1212.     sequence = hostnum * pt->max_tries + trynum;
  1213.       }
  1214.       if (hostnum > pt->group_end) {
  1215.     if (o.debugging) {
  1216.       error("Response from host beyond group_end");
  1217.     }
  1218.     continue;
  1219.       }
  1220.       if (o.debugging) 
  1221.     log_write(LOG_STDOUT, "We got a TCP ping packet back from %s (hostnum = %d trynum = %d\n", inet_ntoa(ip->ip_src), hostnum, trynum);
  1222.       pingtype = PINGTYPE_RAWTCP;
  1223.       foundsomething = 1;
  1224.       if (pt->discardtimesbefore < sequence)
  1225.     dotimeout = 1;
  1226.       newstate = HOST_UP;
  1227.  
  1228.       if (o.pingtype & PINGTYPE_TCP_USE_SYN) {
  1229.     if (tcp->th_flags & TH_RST) {
  1230.       newportstate = PORT_CLOSED;
  1231.     } else if ((tcp->th_flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
  1232.       newportstate = PORT_OPEN;
  1233.     }
  1234.       }
  1235.     } else if (o.debugging) {
  1236.       error("Found whacked packet protocol %d in get_ping_results", ip->ip_p);
  1237.     }
  1238.     if (foundsomething) {  
  1239.       hostupdate(hostbatch, &hostbatch[hostnum], newstate, dotimeout, 
  1240.          trynum, to, &time[sequence], pt, NULL,pingtype);
  1241.     }
  1242.     if (newport && newportstate != PORT_UNKNOWN) {
  1243.       /* OK, we can add it, but that is only appropriate if this is one
  1244.      of the ports the user ASKED for */
  1245.       if (ports && ports->tcp_count == 1 && ports->tcp_ports[0] == newport)
  1246.     addport(&(hostbatch[hostnum].ports), newport, IPPROTO_TCP, NULL, 
  1247.         newportstate);
  1248.     }
  1249. }
  1250. return 0;
  1251. }
  1252.  
  1253. int hostupdate(struct hoststruct *hostbatch, struct hoststruct *target, 
  1254.            int newstate, int dotimeout, int trynum, 
  1255.            struct timeout_info *to, struct timeval *sent, 
  1256.            struct pingtune *pt, struct tcpqueryinfo *tqi, int pingtype) {
  1257.  
  1258. int hostnum = target - hostbatch;
  1259. int i;
  1260. int seq;
  1261. int tmpsd;
  1262. struct timeval tv;
  1263.  
  1264. if (o.debugging)  {
  1265.   gettimeofday(&tv, NULL);
  1266.   log_write(LOG_STDOUT, "Hostupdate called for machine %s state %s -> %s (trynum %d, dotimeadj: %s time: %ld)\n", inet_ntoa(target->host), readhoststate(target->flags), readhoststate(newstate), trynum, (dotimeout)? "yes" : "no", (long) TIMEVAL_SUBTRACT(tv, *sent));
  1267. }
  1268. assert(hostnum <= pt->group_end);
  1269.  
  1270. if (dotimeout) {
  1271.   adjust_timeouts(*sent, to);
  1272. }
  1273.  
  1274. /* If this is a tcp connect() pingscan, close all sockets */
  1275.  
  1276. if (pingtype == PINGTYPE_CONNECTTCP) {
  1277.   seq = (target - hostbatch) * pt->max_tries + trynum;
  1278.   assert(tqi->sockets[seq] >= 0);
  1279.   for(i=0; i <= pt->block_tries; i++) {  
  1280.     seq = (target - hostbatch) * pt->max_tries + i;
  1281.     tmpsd = tqi->sockets[seq];
  1282.     if (tmpsd >= 0) {
  1283.       assert(tqi->sockets_out > 0);
  1284.       tqi->sockets_out--;
  1285.       close(tmpsd);
  1286.       if (tmpsd == tqi->maxsd) tqi->maxsd--;
  1287.       FD_CLR(tmpsd, &(tqi->fds_r));
  1288.       FD_CLR(tmpsd, &(tqi->fds_w));
  1289.       FD_CLR(tmpsd, &(tqi->fds_x));
  1290.       tqi->sockets[seq] = -1;
  1291.     }
  1292.   }
  1293. }
  1294.  
  1295.  
  1296. target->to = *to;
  1297.  
  1298. if (target->flags & HOST_UP) {
  1299.   /* The target is already up and that takes precedence over HOST_DOWN
  1300.      or HOST_FIREWALLED, so we just return. */
  1301.   return 0;
  1302. }
  1303.  
  1304. if (trynum > 0 && !(pt->dropthistry)) {
  1305.   pt->dropthistry = 1;
  1306.   if (o.debugging) 
  1307.     log_write(LOG_STDOUT, "Decreasing massping group size from %d to ", pt->group_size);
  1308.   pt->group_size = MAX((int) (pt->group_size * 0.75), 10);
  1309.   if (o.debugging) 
  1310.     log_write(LOG_STDOUT, "%d\n", pt->group_size);
  1311. }
  1312.  
  1313. if (newstate == HOST_DOWN && (target->flags & HOST_DOWN)) {
  1314.   /* I see nothing to do here */
  1315. } else if (newstate == HOST_UP && (target->flags & HOST_DOWN)) {
  1316.   /* We give host_up precedence */
  1317.   target->flags &= ~HOST_DOWN; /* Kill the host_down flag */
  1318.   target->flags |= HOST_UP;
  1319.   if (hostnum >= pt->group_start) {  
  1320.     assert(pt->down_this_block > 0);
  1321.     pt->down_this_block--;
  1322.     pt->up_this_block++;
  1323.   }
  1324. } else if (newstate == HOST_DOWN) {
  1325.   target->flags |= HOST_DOWN;
  1326.   pt->down_this_block++;
  1327.   pt->block_unaccounted--;
  1328.   pt->num_responses++;
  1329. } else {
  1330.   assert(newstate == HOST_UP);
  1331.   target->flags |= HOST_UP;
  1332.   pt->up_this_block++;
  1333.   pt->block_unaccounted--;
  1334.   pt->num_responses++;
  1335. }
  1336. return 0;
  1337. }
  1338.  
  1339. char *readhoststate(int state) {
  1340.   switch(state) {
  1341.   case HOST_UP:
  1342.     return "HOST_UP";
  1343.   case HOST_DOWN:
  1344.     return "HOST_DOWN";
  1345.   case HOST_FIREWALLED:
  1346.     return "HOST_FIREWALLED";
  1347.   default:
  1348.     return "UNKNOWN/COMBO";
  1349.   }
  1350.   return NULL;
  1351. }
  1352.  
  1353.  
  1354.  
  1355.