home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / netlib / lib / iaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-27  |  4.4 KB  |  157 lines

  1. /*
  2.  * Adopted to Mint-Net 1994, Kay Roemer.
  3.  */
  4.  
  5. /*
  6.  * Copyright (c) 1983, 1990 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)inet_addr.c    5.11 (Berkeley) 12/9/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. #include "socklib.h"
  43. #include <sys/types.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <ctype.h>
  48.  
  49. /*
  50.  * Ascii internet address interpretation routine.
  51.  * The value returned is in network order.
  52.  */
  53. u_long
  54. inet_addr(cp)
  55.     register const char *cp;
  56. {
  57.     struct in_addr val;
  58.  
  59.     if (inet_aton(cp, &val))
  60.         return (val.s_addr);
  61.     return (INADDR_NONE);
  62. }
  63.  
  64. /* 
  65.  * Check whether "cp" is a valid ascii representation
  66.  * of an Internet address and convert to a binary address.
  67.  * Returns 1 if the address is valid, 0 if not.
  68.  * This replaces inet_addr, the return value from which
  69.  * cannot distinguish between failure and a local broadcast address.
  70.  */
  71. unsigned long
  72. inet_aton(cp, addr)
  73.     register const char *cp;
  74.     struct in_addr *addr;
  75. {
  76.     register u_long val, base, n;
  77.     register char c;
  78.     u_long parts[4], *pp = parts;
  79.  
  80.     for (;;) {
  81.         /*
  82.          * Collect number up to ``.''.
  83.          * Values are specified as for C:
  84.          * 0x=hex, 0=octal, other=decimal.
  85.          */
  86.         val = 0; base = 10;
  87.         if (*cp == '0') {
  88.             if (*++cp == 'x' || *cp == 'X')
  89.                 base = 16, cp++;
  90.             else
  91.                 base = 8;
  92.         }
  93.         while ((c = *cp) != '\0') {
  94.             if (isascii(c) && isdigit(c)) {
  95.                 val = (val * base) + (c - '0');
  96.                 cp++;
  97.                 continue;
  98.             }
  99.             if (base == 16 && isascii(c) && isxdigit(c)) {
  100.                 val = (val << 4) + 
  101.                     (c + 10 - (islower(c) ? 'a' : 'A'));
  102.                 cp++;
  103.                 continue;
  104.             }
  105.             break;
  106.         }
  107.         if (*cp == '.') {
  108.             /*
  109.              * Internet format:
  110.              *    a.b.c.d
  111.              *    a.b.c    (with c treated as 16-bits)
  112.              *    a.b    (with b treated as 24 bits)
  113.              */
  114.             if (pp >= parts + 3 || val > 0xff)
  115.                 return (0);
  116.             *pp++ = val, cp++;
  117.         } else
  118.             break;
  119.     }
  120.     /*
  121.      * Check for trailing characters.
  122.      */
  123.     if (*cp && (!isascii(*cp) || !isspace(*cp)))
  124.         return (0);
  125.     /*
  126.      * Concoct the address according to
  127.      * the number of parts specified.
  128.      */
  129.     n = pp - parts + 1;
  130.     switch (n) {
  131.  
  132.     case 1:                /* a -- 32 bits */
  133.         break;
  134.  
  135.     case 2:                /* a.b -- 8.24 bits */
  136.         if (val > 0xffffff)
  137.             return (0);
  138.         val |= parts[0] << 24;
  139.         break;
  140.  
  141.     case 3:                /* a.b.c -- 8.8.16 bits */
  142.         if (val > 0xffff)
  143.             return (0);
  144.         val |= (parts[0] << 24) | (parts[1] << 16);
  145.         break;
  146.  
  147.     case 4:                /* a.b.c.d -- 8.8.8.8 bits */
  148.         if (val > 0xff)
  149.             return (0);
  150.         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  151.         break;
  152.     }
  153.     if (addr)
  154.         addr->s_addr = htonl(val);
  155.     return (1);
  156. }
  157.