home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / ftp / BFTP.312 / hostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-29  |  3.2 KB  |  163 lines

  1. /*
  2.  *    hostname.c
  3.  *
  4.  *      Written at USC Information Sciences Institute
  5.  */
  6.  
  7. #include <sys/types.h> 
  8. #include <sys/param.h>
  9. #include <stdio.h>
  10.  
  11. #ifdef ISI4_2
  12. #include <netdb.h-bind>
  13. #else
  14. #include <netdb.h>
  15. #endif
  16.  
  17. #include <ctype.h>
  18.  
  19. #ifndef FALSE
  20. #define FALSE    0    /* logical false */
  21. #define TRUE    !FALSE    /* logical true */
  22. #endif
  23.  
  24. typedef int boolean;
  25.  
  26. /* dotted10
  27.    Checks that a string is a legal (4-part dotted decimal) internet address. 
  28.    Returns the address in network byte order if successful, otherwise, -1.
  29. */
  30.  
  31. u_long
  32. dotted10(cp)
  33.    register char *cp;
  34. {
  35.    register u_long val;
  36.    register char c;
  37.    u_long parts[4], *pp = parts;
  38.    int i;
  39.  
  40. again:
  41.    /* Collect number up to ``.''.  */
  42.    val = 0;
  43.    while (c = *cp) {
  44.       if (isdigit(c)) {
  45.          val = (val * 10) + (c - '0');
  46.          cp++;
  47.          continue;
  48.          }
  49.       break;
  50.       }
  51.    if (*cp == '.') {
  52.       /* Internet format: a.b.c.d  */
  53.       if (pp >= parts + 4)
  54.          return (-1);
  55.       *pp++ = val, cp++;
  56.       goto again;
  57.       }
  58.    /* Check for trailing characters. */
  59.    if (*cp && !isspace(*cp))
  60.       return (-1);
  61.    *pp++ = val;
  62.    
  63.    /*  Address must have four parts. */
  64.    if ((pp - parts) != 4)
  65.       return (-1);
  66.       
  67.    /* a.b.c.d -- 8.8.8.8 bits */
  68.    for (i=0;i<4;i++) if (parts[i] > 0xff) return(-1);
  69.    val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  70.             ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
  71.    val = htonl(val);
  72.    return (val);
  73. } /* dotted10 */
  74.  
  75. boolean
  76. isbroadcast(ipaddr)
  77.    u_long *ipaddr;
  78. {
  79.    u_long class, host, mask;
  80.    
  81.    class= (*ipaddr >> 28) & 0xd;
  82.    mask = (class == 0)? (0xffffff):     /* class a */
  83.          (class == 0x8)? (0xffff):     /* class b */
  84.       (0xff);            /* class c */
  85.    /* *** what about subnet mask? *** */
  86.    host = (*ipaddr & mask);
  87.  
  88.    return((host==0) || (host==mask)); 
  89. } /* isbroadcast */
  90.  
  91. /*
  92. *   resolve_name(namep, adrlist, length)
  93. *
  94. *   Resolves host name given in specified server structure, to
  95. *   fill in list of IP addresses and host name.  Returns number of
  96. *   addresses if OK, else zero.
  97. */
  98.                   
  99. int
  100. resolve_name( namep, adrlist, listlen)
  101.    char *namep;
  102.    u_long adrlist[];
  103.    int listlen;
  104. {
  105.    register struct hostent *hp;
  106.    u_long ipaddr;
  107.    
  108. #ifdef BSD4_3   
  109.    register int i, count;
  110.    char **ap;
  111. #endif
  112.    
  113.    bzero((char *) adrlist, listlen * sizeof(u_long));
  114.  
  115.   if ((ipaddr = dotted10(namep)) != -1) {
  116.      /* Dotted-decimal... */
  117.      adrlist[0] = ipaddr;
  118.      if (!isbroadcast(&adrlist[0]))
  119.         return(1);
  120.      }
  121.   else    if (hp = gethostbyname(namep)) {
  122. #ifndef BSD4_3   
  123.      adrlist[0] = *(u_long *) (hp->h_addr);
  124.      if (!isbroadcast(&adrlist[0]))
  125.           return(1);
  126. #else
  127.      count = 0;
  128.      ap = hp->h_addr_list;
  129.      for (i=0;i<listlen && *ap ;i++) {
  130.     count++;
  131.     adrlist[i] = * (u_long *) *ap++;
  132.     if (isbroadcast(&adrlist[i]))
  133.        adrlist[i--] = 0;
  134.         }
  135.      return(count);
  136. #endif
  137.      }
  138.  
  139.   adrlist[0] = 0;
  140.   return(0);
  141. } /* resolve_name */
  142.  
  143.  
  144. /* Error check a string that contains a port number:
  145.     if okay, return(port)
  146.     if bad, return(0) and print to stderr
  147. */
  148.  
  149. int
  150. check_port(arg)
  151.    char *arg;
  152. {
  153.    int port;
  154.    char *cp;
  155.     
  156.    for (cp = arg; *cp; cp++) 
  157.       if (! isdigit(*cp))
  158.      return(-1);
  159.    if (((port = atoi(arg)) < 1024) || (port > 0xffff))
  160.       return(-1);
  161.    return(port);
  162. } /* check_port */
  163.