home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nmap254b.zip / protocols.c < prev    next >
C/C++ Source or Header  |  2001-10-14  |  7KB  |  209 lines

  1.  
  2. /***********************************************************************/
  3. /* protocols.c -- Functions relating to the protocol scan and mapping  */
  4. /* between IPproto Number <-> name.                                    */
  5. /*                                                                     */
  6. /***********************************************************************/
  7. /*  The Nmap Security Scanner is (C) 1995-2001 Insecure.Com LLC. This  */
  8. /*  program is free software; you can redistribute it and/or modify    */
  9. /*  it under the terms of the GNU General Public License as published  */
  10. /*  by the Free Software Foundation; Version 2.  This guarantees your  */
  11. /*  right to use, modify, and redistribute this software under certain */
  12. /*  conditions.  If this license is unacceptable to you, we may be     */
  13. /*  willing to sell alternative licenses (contact sales@insecure.com). */
  14. /*                                                                     */
  15. /*  If you received these files with a written license agreement       */
  16. /*  stating terms other than the (GPL) terms above, then that          */
  17. /*  alternative license agreement takes precendence over this comment. */
  18. /*                                                                     */
  19. /*  Source is provided to this software because we believe users have  */
  20. /*  a right to know exactly what a program is going to do before they  */
  21. /*  run it.  This also allows you to audit the software for security   */
  22. /*  holes (none have been found so far).                               */
  23. /*                                                                     */
  24. /*  Source code also allows you to port Nmap to new platforms, fix     */
  25. /*  bugs, and add new features.  You are highly encouraged to send     */
  26. /*  your changes to fyodor@insecure.org for possible incorporation     */
  27. /*  into the main distribution.  By sending these changes to Fyodor or */
  28. /*  one the insecure.org development mailing lists, it is assumed that */
  29. /*  you are offering Fyodor the unlimited, non-exclusive right to      */
  30. /*  reuse, modify, and relicense the code.  This is important because  */
  31. /*  the inability to relicense code has caused devastating problems    */
  32. /*  for other Free Software projects (such as KDE and NASM).  Nmap     */
  33. /*  will always be available Open Source.  If you wish to specify      */
  34. /*  special license conditions of your contributions, just say so      */
  35. /*  when you send them.                                                */
  36. /*                                                                     */
  37. /*  This program is distributed in the hope that it will be useful,    */
  38. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of     */
  39. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  */
  40. /*  General Public License for more details (                          */
  41. /*  http://www.gnu.org/copyleft/gpl.html ).                            */
  42. /*                                                                     */
  43. /***********************************************************************/
  44.  
  45. /* $Id: protocols.c,v 1.7 2001/10/14 09:38:26 fyodor Exp $ */
  46.  
  47.  
  48. #include "protocols.h"
  49.  
  50. extern struct ops o;
  51. static int protocols_initialized = 0;
  52. static int numipprots = 0;
  53. static struct protocol_list *protocol_table[PROTOCOL_TABLE_SIZE];
  54.  
  55. static int nmap_protocols_init() {
  56.   char filename[512];
  57.   FILE *fp;
  58.   char protocolname[128], proto[16];
  59.   unsigned short protno;
  60.   char *p;
  61.   char line[1024];
  62.   int lineno = 0;
  63.   struct protocol_list *current, *previous;
  64.   int res;
  65.  
  66.   if (nmap_fetchfile(filename, sizeof(filename), "nmap-protocols") == -1) {
  67.     error("Unable to find nmap-protocols!  Resorting to /etc/protocol");
  68.     strcpy(filename, "/etc/protocols");
  69.   }
  70.  
  71.   fp = fopen(filename, "r");
  72.   if (!fp) {
  73.     fatal("Unable to open %s for reading protocol information", filename);
  74.   }
  75.  
  76.   bzero(protocol_table, sizeof(protocol_table));
  77.   
  78.   while(fgets(line, sizeof(line), fp)) {
  79.     lineno++;
  80.     p = line;
  81.     while(*p && isspace((int) *p))
  82.       p++;
  83.     if (*p == '#')
  84.       continue;
  85.     res = sscanf(line, "%s %hu", protocolname, &protno);
  86.     if (res !=2)
  87.       continue;
  88.     protno = htons(protno);
  89.  
  90.     /* Now we make sure our protocols don't have duplicates */
  91.     for(current = protocol_table[0], previous = NULL;
  92.     current; current = current->next) {
  93.       if (protno == current->protoent->p_proto) {
  94.     if (o.debugging) {
  95.       error("Protocol %d is duplicated in protocols file %s", ntohs(protno), proto, filename);
  96.     }
  97.     break;
  98.       }
  99.       previous = current;
  100.     }
  101.     if (current)
  102.       continue;
  103.  
  104.     numipprots++;
  105.  
  106.     current = (struct protocol_list *) cp_alloc(sizeof(struct protocol_list));
  107.     current->protoent = (struct protoent *) cp_alloc(sizeof(struct protoent));
  108.     current->next = NULL;
  109.     if (previous == NULL) {
  110.       protocol_table[protno] = current;
  111.     } else {
  112.       previous->next = current;
  113.     }
  114.     current->protoent->p_name = cp_strdup(protocolname);
  115.     current->protoent->p_proto = protno;
  116.     current->protoent->p_aliases = NULL;
  117.   }
  118.   fclose(fp);
  119.   protocols_initialized = 1;
  120.   return 0;
  121. }
  122.  
  123.  
  124. struct protoent *nmap_getprotbynum(int num) {
  125.   struct protocol_list *current;
  126.  
  127.   if (!protocols_initialized)
  128.     if (nmap_protocols_init() == -1)
  129.       return NULL;
  130.  
  131.   for(current = protocol_table[num % PROTOCOL_TABLE_SIZE];
  132.       current; current = current->next) {
  133.     if (num == current->protoent->p_proto)
  134.       return current->protoent;
  135.   }
  136.  
  137.   /* Couldn't find it ... oh well. */
  138.   return NULL;
  139.   
  140. }
  141.  
  142. /* Be default we do all prots 0-255. */
  143. struct scan_lists *getdefaultprots(void) {
  144.   int protindex = 0;
  145.   struct scan_lists *scanlist;
  146.   /*struct protocol_list *current;*/
  147.   int bucket;
  148.   int protsneeded = 255;
  149.  
  150.   if (!protocols_initialized)
  151.     if (nmap_protocols_init() == -1)
  152.       fatal("getdefaultprots(): Couldn't get protocol numbers");
  153.   
  154.   scanlist = (struct scan_lists *) cp_alloc(sizeof(struct scan_lists));
  155.   bzero(scanlist, sizeof(scanlist));
  156.   scanlist->prots = (unsigned short *) cp_alloc((protsneeded +1) * sizeof(unsigned short));
  157.   scanlist->prot_count = protsneeded;
  158.  
  159.   for(bucket = 1; bucket < protsneeded; bucket++) {
  160.     scanlist->prots[protindex++] = bucket;
  161.   }
  162.   scanlist->prots[protindex] = 0;
  163.   return scanlist;
  164. }
  165.  
  166. struct scan_lists *getfastprots(void) {
  167.   int protindex = 0;
  168.   struct scan_lists *scanlist;
  169.   char usedprots[256];
  170.   struct protocol_list *current;
  171.   int bucket;
  172.   int protsneeded = 0;
  173.  
  174.   if (!protocols_initialized)
  175.     if (nmap_protocols_init() == -1)
  176.       fatal("Getfastprots: Couldn't get protocol numbers");
  177.   
  178.   bzero(usedprots, sizeof(usedprots));
  179.  
  180.   for(bucket = 0; bucket < PROTOCOL_TABLE_SIZE; bucket++) {  
  181.     for(current = protocol_table[bucket % PROTOCOL_TABLE_SIZE];
  182.     current; current = current->next) {
  183.       if (!usedprots[ntohs(current->protoent->p_proto)])
  184.     usedprots[ntohs(current->protoent->p_proto)] = 1;
  185.     protsneeded++;
  186.     }
  187.   }
  188.  
  189.   scanlist = (struct scan_lists *) cp_alloc(sizeof(struct scan_lists));
  190.   bzero(scanlist, sizeof(scanlist));
  191.   scanlist->prots = (unsigned short *) cp_alloc((protsneeded +1) * sizeof(unsigned short));
  192.   scanlist->prot_count = protsneeded;
  193.  
  194.   for(bucket = 1; bucket < 256; bucket++) {
  195.     if (usedprots[bucket])
  196.       scanlist->prots[protindex++] = bucket;
  197.   }
  198.   scanlist->prots[protindex] = 0;
  199.  
  200.   return scanlist;
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.