home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / inet_network.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  7KB  |  275 lines

  1. /*
  2.  * Copyright (c) 1983, 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)inet_addr.c 5.10 (Berkeley) 2/24/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/param.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <ctype.h>
  42. #include <stdio.h>
  43.  
  44. /*
  45.  * Check whether "cp" is a valid ascii representation
  46.  * of an Internet address and convert to a binary address.
  47.  * Returns 1 if the address is valid, 0 if not.
  48.  * This replaces inet_addr, the return value from which
  49.  * cannot distinguish between failure and a local broadcast address.
  50.  */
  51.  
  52. int
  53. inet_aton(cp, addr)
  54.     const char *cp;
  55.     struct in_addr *addr;
  56. {
  57.     register u_long val, base, n;
  58.     register char c;
  59.     u_long parts[4], *pp = parts;
  60.  
  61.     for (;;) {
  62.         /*
  63.          * Collect number up to ``.''.
  64.          * Values are specified as for C:
  65.          * 0x=hex, 0=octal, other=decimal.
  66.          */
  67.         val = 0; base = 10;
  68.         if (*cp == '0') {
  69.             if (*++cp == 'x' || *cp == 'X')
  70.                 base = 16, cp++;
  71.             else
  72.                 base = 8;
  73.         }
  74.         while ((c = *cp) != '\0') {
  75.             if (isascii(c) && isdigit(c)) {
  76.                 val = (val * base) + (c - '0');
  77.                 cp++;
  78.                 continue;
  79.             }
  80.             if (base == 16 && isascii(c) && isxdigit(c)) {
  81.                 val = (val << 4) +
  82.                     (c + 10 - (islower(c) ? 'a' : 'A'));
  83.                 cp++;
  84.                 continue;
  85.             }
  86.             break;
  87.         }
  88.         if (*cp == '.') {
  89.             /*
  90.              * Internet format:
  91.              *    a.b.c.d
  92.              *    a.b.c    (with c treated as 16-bits)
  93.              *    a.b    (with b treated as 24 bits)
  94.              */
  95.             if (pp >= parts + 3 || val > 0xff)
  96.                 return (0);
  97.             *pp++ = val, cp++;
  98.         } else
  99.             break;
  100.     }
  101.     /*
  102.      * Check for trailing characters.
  103.      */
  104.     if (*cp && (!isascii(*cp) || !isspace(*cp)))
  105.         return (0);
  106.     /*
  107.      * Concoct the address according to
  108.      * the number of parts specified.
  109.      */
  110.     n = pp - parts + 1;
  111.     switch (n) {
  112.  
  113.     case 1:             /* a -- 32 bits */
  114.         break;
  115.  
  116.     case 2:             /* a.b -- 8.24 bits */
  117.         if (val > 0xffffff)
  118.             return (0);
  119.         val |= parts[0] << 24;
  120.         break;
  121.  
  122.     case 3:             /* a.b.c -- 8.8.16 bits */
  123.         if (val > 0xffff)
  124.             return (0);
  125.         val |= (parts[0] << 24) | (parts[1] << 16);
  126.         break;
  127.  
  128.     case 4:             /* a.b.c.d -- 8.8.8.8 bits */
  129.         if (val > 0xff)
  130.             return (0);
  131.         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  132.         break;
  133.     }
  134.     if (addr)
  135.         addr->s_addr = htonl(val);
  136.     return (1);
  137. }
  138.  
  139. /*
  140.  * Ascii internet address interpretation routine.
  141.  * The value returned is in network order.
  142.  */
  143. u_long
  144. inet_addr(cp)
  145.     const char *cp;
  146. {
  147.     struct in_addr val;
  148.  
  149.     if (inet_aton(cp, &val))
  150.         return (val.s_addr);
  151.     return (u_long)-1;
  152. }
  153.  
  154. /*
  155.  * Return the local network address portion of an
  156.  * internet address; handles class a/b/c network
  157.  * number formats.
  158.  */
  159. u_long
  160. inet_lnaof(in)
  161.     struct in_addr in;
  162. {
  163.     register u_long i = ntohl(in.s_addr);
  164.  
  165.     if (IN_CLASSA(i))
  166.         return ((i)&IN_CLASSA_HOST);
  167.     else if (IN_CLASSB(i))
  168.         return ((i)&IN_CLASSB_HOST);
  169.     else
  170.         return ((i)&IN_CLASSC_HOST);
  171. }
  172.  
  173. /*
  174.  * Formulate an Internet address from network + host.  Used in
  175.  * building addresses stored in the ifnet structure.
  176.  */
  177. struct in_addr
  178. inet_makeaddr(net, host)
  179.     u_long net, host;
  180. {
  181.     u_long addr;
  182.  
  183.     if (net < 128)
  184.         addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
  185.     else if (net < 65536)
  186.         addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
  187.     else if (net < 16777216L)
  188.         addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
  189.     else
  190.         addr = net | host;
  191.     addr = htonl(addr);
  192.     return (*(struct in_addr *)&addr);
  193. }
  194.  
  195. /*
  196.  * Return the network number from an internet
  197.  * address; handles class a/b/c network #'s.
  198.  */
  199. u_long
  200. inet_netof(in)
  201.     struct in_addr in;
  202. {
  203.     register u_long i = ntohl(in.s_addr);
  204.  
  205.     if (IN_CLASSA(i))
  206.         return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
  207.     else if (IN_CLASSB(i))
  208.         return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
  209.     else
  210.         return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
  211. }
  212. /*
  213.  * Internet network address interpretation routine.
  214.  * The library routines call this routine to interpret
  215.  * network numbers.
  216.  */
  217. u_long
  218. inet_network(cp)
  219.     const char *cp;
  220. {
  221.     register u_long val, base, n;
  222.     register char c;
  223.     u_long parts[4], *pp = parts;
  224.     register int i;
  225.  
  226. again:
  227.     val = 0; base = 10;
  228.     if (*cp == '0')
  229.         base = 8, cp++;
  230.     if (*cp == 'x' || *cp == 'X')
  231.         base = 16, cp++;
  232.     while ((c = *cp)) {
  233.         if (isdigit(c)) {
  234.             val = (val * base) + (c - '0');
  235.             cp++;
  236.             continue;
  237.         }
  238.         if (base == 16 && isxdigit(c)) {
  239.             val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
  240.             cp++;
  241.             continue;
  242.         }
  243.         break;
  244.     }
  245.     if (*cp == '.') {
  246.         if (pp >= parts + 4)
  247.             return (u_long)-1;
  248.         *pp++ = val, cp++;
  249.         goto again;
  250.     }
  251.     if (*cp && !isspace(*cp))
  252.         return (u_long)-1;
  253.     *pp++ = val;
  254.     n = pp - parts;
  255.     if (n > 4)
  256.         return (u_long)-1;
  257.     for (val = 0, i = 0; i < n; i++) {
  258.         val <<= 8;
  259.         val |= parts[i] & 0xff;
  260.     }
  261.     return (val);
  262. }
  263. char *
  264. inet_ntoa(in)
  265.     struct in_addr in;
  266. {
  267.     char *p = (char *)∈
  268.  
  269. #define UC(b)   (((int)b)&0xff)
  270.  
  271.     snprintf(u.u_ntoa_buf, sizeof(u.u_ntoa_buf),
  272.         "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
  273.     return u.u_ntoa_buf;
  274. }
  275.