home *** CD-ROM | disk | FTP | other *** search
/ Steganos Hacker Tools / SHT151.iso / programme / scanner / nmapNTsp1 / Win_2000.exe / nmapNT-src / services.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-26  |  5.6 KB  |  196 lines

  1. #include "services.h"
  2.  
  3. extern struct ops o;
  4. static int services_initialized = 0;
  5. static int numtcpports = 0;
  6. static int numudpports = 0;
  7. static struct service_list *service_table[SERVICE_TABLE_SIZE];
  8.  
  9. static int nmap_services_init() {
  10.   char filename[512];
  11.   FILE *fp;
  12.   char servicename[128], proto[16];
  13.   unsigned short portno;
  14.   char *p;
  15.   char line[1024];
  16.   int lineno = 0;
  17.   struct service_list *current, *previous;
  18.   int res;
  19.  
  20.   if (nmap_fetchfile(filename, sizeof(filename), "nmap-services") == -1) {
  21.     error("Unable to find nmap-services!  Resorting to /etc/services");
  22.     strcpy(filename, "/etc/services");
  23.   }
  24.  
  25.   fp = fopen(filename, "r");
  26.   if (!fp) {
  27.     fatal("Unable to open %s for reading service information", filename);
  28.   }
  29.  
  30.   bzero(service_table, sizeof(service_table));
  31.   
  32.   while(fgets(line, sizeof(line), fp)) {
  33.     lineno++;
  34.     p = line;
  35.     res = sscanf(line, "%s %hu/%s", servicename, &portno, proto);
  36.     if (res !=3)
  37.       continue;
  38.     portno = htons(portno);
  39.  
  40.     /* Now we make sure our services doesn't have duplicates */
  41.     for(current = service_table[portno % SERVICE_TABLE_SIZE], previous = NULL;
  42.     current; current = current->next) {
  43.       if (portno == current->servent->s_port &&
  44.       strcasecmp(proto, current->servent->s_proto) == 0) {
  45.     if (o.debugging) {
  46.       error("Port %d proto %s is duplicated in services file %s", ntohs(portno), proto, filename);
  47.     }
  48.     break;
  49.       }
  50.       previous = current;
  51.     }
  52.     if (current)
  53.       continue;
  54.  
  55.     if (strncasecmp(proto, "tcp", 3) == 0) {
  56.       numtcpports++;
  57.     } else if (strncasecmp(proto, "udp", 3) == 0) {
  58.       numudpports++;
  59.     } else if (strncasecmp(proto, "ddp", 3) == 0) {
  60.       /* ddp is some apple thing...we don't "do" that */
  61.     } else if (strncasecmp(proto, "divert", 6) == 0) {
  62.       /* divert sockets are for freebsd's natd */
  63.     } else if (strncasecmp(proto, "#", 1) == 0) {
  64.       /* possibly misplaced comment, but who cares? */
  65.     } else {
  66.       if (o.debugging)
  67.     error("Unknown protocol (%s) on line %d of services file %s.", proto, lineno, filename);
  68.       continue;
  69.     }
  70.  
  71.     current = (struct service_list *) cp_alloc(sizeof(struct service_list));
  72.     current->servent = (struct servent *) cp_alloc(sizeof(struct servent));
  73.     current->next = NULL;
  74.     if (previous == NULL) {
  75.       service_table[portno % SERVICE_TABLE_SIZE] = current;
  76.     } else {
  77.       previous->next = current;
  78.     }
  79.     current->servent->s_name = cp_strdup(servicename);
  80.     current->servent->s_port = portno;
  81.     current->servent->s_proto = cp_strdup(proto);
  82.     current->servent->s_aliases = NULL;
  83.   }
  84.   fclose(fp);
  85.   services_initialized = 1;
  86.   return 0;
  87. }
  88.  
  89.  
  90. struct servent *nmap_getservbyport(int port, const char *proto) {
  91.   struct service_list *current;
  92.  
  93.   if (!services_initialized)
  94.     if (nmap_services_init() == -1)
  95.       return NULL;
  96.  
  97.   for(current = service_table[port % SERVICE_TABLE_SIZE];
  98.       current; current = current->next) {
  99.     if (htons(port) == htons(current->servent->s_port) &&
  100.     strcmp(proto, current->servent->s_proto) == 0)
  101.       return current->servent;
  102.   }
  103.  
  104.   /* Couldn't find it ... oh well. */
  105.   return NULL;
  106.   
  107. }
  108.  
  109. /* Be default we do all ports 1-1024 as well as any higher ports
  110.    that are in /etc/services. */
  111. unsigned short *getdefaultports(int tcpscan, int udpscan) {
  112.   int portindex = 0;
  113.   unsigned short *ports;
  114.   char usedports[65536];
  115.   struct service_list *current;
  116.   int bucket;
  117.   int portsneeded = 1; /* the 1 is for the terminating 0 */
  118.  
  119.   if (!services_initialized)
  120.     if (nmap_services_init() == -1)
  121.       fatal("Getfastports: Coudn't get port numbers");
  122.   
  123.   bzero(usedports, sizeof(usedports));
  124.   for(bucket = 1; bucket < 1025; bucket++) {  
  125.     usedports[bucket] = 1;
  126.     portsneeded++;
  127.   }
  128.  
  129.   for(bucket = 0; bucket < SERVICE_TABLE_SIZE; bucket++) {  
  130.     for(current = service_table[bucket % SERVICE_TABLE_SIZE];
  131.     current; current = current->next) {
  132.       if (!usedports[ntohs(current->servent->s_port)] &&
  133.       ((tcpscan && !strncmp(current->servent->s_proto, "tcp", 3)) ||
  134.        (udpscan && !strncmp(current->servent->s_proto, "udp", 3)))) {      
  135.     usedports[ntohs(current->servent->s_port)] = 1;
  136.     portsneeded++;
  137.       }
  138.     }
  139.   }
  140.  
  141.   ports = (unsigned short *) cp_alloc(portsneeded * sizeof(unsigned short));
  142.   o.numports = portsneeded - 1;
  143.  
  144.   for(bucket = 1; bucket < 65536; bucket++) {
  145.     if (usedports[bucket])
  146.       ports[portindex++] = bucket;
  147.   }
  148.   ports[portindex] = 0;
  149.  
  150. return ports;
  151.  
  152. }
  153.  
  154. unsigned short *getfastports(int tcpscan, int udpscan) {
  155.   int portindex = 0;
  156.   unsigned short *ports;
  157.   char usedports[65536];
  158.   struct service_list *current;
  159.   int bucket;
  160.   int portsneeded = 1; /* the 1 is for the terminating 0 */
  161.  
  162.   if (!services_initialized)
  163.     if (nmap_services_init() == -1)
  164.       fatal("Getfastports: Coudn't get port numbers");
  165.   
  166.   bzero(usedports, sizeof(usedports));
  167.  
  168.   for(bucket = 0; bucket < SERVICE_TABLE_SIZE; bucket++) {  
  169.     for(current = service_table[bucket % SERVICE_TABLE_SIZE];
  170.     current; current = current->next) {
  171.       if (!usedports[ntohs(current->servent->s_port)] &&
  172.       ((tcpscan && !strncmp(current->servent->s_proto, "tcp", 3)) ||
  173.        (udpscan && !strncmp(current->servent->s_proto, "udp", 3)))) {      
  174.     usedports[ntohs(current->servent->s_port)] = 1;
  175.     portsneeded++;
  176.       }
  177.     }
  178.   }
  179.  
  180.   ports = (unsigned short *) cp_alloc(portsneeded * sizeof(unsigned short));
  181.   o.numports = portsneeded - 1;
  182.  
  183.   for(bucket = 1; bucket < 65536; bucket++) {
  184.     if (usedports[bucket])
  185.       ports[portindex++] = bucket;
  186.   }
  187.   ports[portindex] = 0;
  188.  
  189. return ports;
  190. }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.