Home | History | Annotate | Download | only in BsdSocketLib
      1 /*
      2  * Copyright (c) 1986, 1993
      3  *  The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * J.Q. Johnson.
      7  *
      8  * Portions copyright (c) 1999, 2000
      9  * Intel Corporation.
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  *
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  *
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *
     26  *    This product includes software developed by the University of
     27  *    California, Berkeley, Intel Corporation, and its contributors.
     28  *
     29  * 4. Neither the name of University, Intel Corporation, or their respective
     30  *    contributors may be used to endorse or promote products derived from
     31  *    this software without specific prior written permission.
     32  *
     33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
     34  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
     35  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     36  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS,
     37  * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     38  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     40  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     41  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44  *
     45  */
     46 
     47 #if defined(LIBC_SCCS) && !defined(lint)
     48 static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";
     49 #endif /* LIBC_SCCS and not lint */
     50 
     51 #include <sys/param.h>
     52 #include <netns/ns.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 
     56 static struct ns_addr addr, zero_addr;
     57 
     58 static void Field   (char *buf, u_char *out, int len);
     59 static void cvtbase (long oldbase, int newbase, int input[], int inlen, unsigned char result[], int reslen);
     60 
     61 struct ns_addr
     62 ns_addr(
     63   const char *name
     64   )
     65 {
     66   char separator;
     67   char *hostname, *socketname, *cp;
     68   char buf[50];
     69 
     70   (void)strncpy(buf, name, sizeof(buf) - 1);
     71   buf[sizeof(buf) - 1] = '\0';
     72 
     73   /*
     74    * First, figure out what he intends as a field separtor.
     75    * Despite the way this routine is written, the prefered
     76    * form  2-272.AA001234H.01777, i.e. XDE standard.
     77    * Great efforts are made to insure backward compatability.
     78    */
     79   if ((hostname = strchr(buf, '#')) != NULL)
     80     separator = '#';
     81   else {
     82     hostname = strchr(buf, '.');
     83     if ((cp = strchr(buf, ':')) &&
     84         ((hostname && cp < hostname) || (hostname == 0))) {
     85       hostname = cp;
     86       separator = ':';
     87     } else
     88       separator = '.';
     89   }
     90   if (hostname)
     91     *hostname++ = 0;
     92 
     93   addr = zero_addr;
     94   Field(buf, addr.x_net.c_net, 4);
     95   if (hostname == 0)
     96     return (addr);  /* No separator means net only */
     97 
     98   socketname = strchr(hostname, separator);
     99   if (socketname) {
    100     *socketname++ = 0;
    101     Field(socketname, (u_char *)&addr.x_port, 2);
    102   }
    103 
    104   Field(hostname, addr.x_host.c_host, 6);
    105 
    106   return (addr);
    107 }
    108 
    109 static void
    110 Field(
    111   char *buf,
    112   u_char *out,
    113   int len
    114   )
    115 {
    116   register char *bp = buf;
    117   int i, ibase, base16 = 0, base10 = 0, clen = 0;
    118   int hb[6], *hp;
    119   char *fmt;
    120 
    121   /*
    122    * first try 2-273#2-852-151-014#socket
    123    */
    124   if ((*buf != '-') &&
    125       (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
    126       &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
    127     cvtbase(1000L, 256, hb, i, out, len);
    128     return;
    129   }
    130   /*
    131    * try form 8E1#0.0.AA.0.5E.E6#socket
    132    */
    133   if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
    134       &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
    135     cvtbase(256L, 256, hb, i, out, len);
    136     return;
    137   }
    138   /*
    139    * try form 8E1#0:0:AA:0:5E:E6#socket
    140    */
    141   if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
    142       &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
    143     cvtbase(256L, 256, hb, i, out, len);
    144     return;
    145   }
    146   /*
    147    * This is REALLY stretching it but there was a
    148    * comma notation separting shorts -- definitely non standard
    149    */
    150   if (1 < (i = sscanf(buf,"%x,%x,%x",
    151       &hb[0], &hb[1], &hb[2]))) {
    152     hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
    153     hb[2] = htons(hb[2]);
    154     cvtbase(65536L, 256, hb, i, out, len);
    155     return;
    156   }
    157 
    158   /* Need to decide if base 10, 16 or 8 */
    159   while (*bp) switch (*bp++) {
    160 
    161   case '0': case '1': case '2': case '3': case '4': case '5':
    162   case '6': case '7': case '-':
    163     break;
    164 
    165   case '8': case '9':
    166     base10 = 1;
    167     break;
    168 
    169   case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
    170   case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
    171     base16 = 1;
    172     break;
    173 
    174   case 'x': case 'X':
    175     *--bp = '0';
    176     base16 = 1;
    177     break;
    178 
    179   case 'h': case 'H':
    180     base16 = 1;
    181     /* fall into */
    182 
    183   default:
    184     *--bp = 0; /* Ends Loop */
    185   }
    186   if (base16) {
    187     fmt = "%3x";
    188     ibase = 4096;
    189   } else if (base10 == 0 && *buf == '0') {
    190     fmt = "%3o";
    191     ibase = 512;
    192   } else {
    193     fmt = "%3d";
    194     ibase = 1000;
    195   }
    196 
    197   for (bp = buf; *bp++; ) clen++;
    198   if (clen == 0) clen++;
    199   if (clen > 18) clen = 18;
    200   i = ((clen - 1) / 3) + 1;
    201   bp = clen + buf - 3;
    202   hp = hb + i - 1;
    203 
    204   while (hp > hb) {
    205     (void)sscanf(bp, fmt, hp);
    206     bp[0] = 0;
    207     hp--;
    208     bp -= 3;
    209   }
    210   (void)sscanf(buf, fmt, hp);
    211   cvtbase((long)ibase, 256, hb, i, out, len);
    212 }
    213 
    214 static void
    215 cvtbase(
    216   long oldbase,
    217   int newbase,
    218   int input[],
    219   int inlen,
    220   unsigned char result[],
    221   int reslen
    222   )
    223 {
    224   int d, e;
    225   long sum;
    226 
    227   e = 1;
    228   while (e > 0 && reslen > 0) {
    229     d = 0; e = 0; sum = 0;
    230     /* long division: input=input/newbase */
    231     while (d < inlen) {
    232       sum = sum*oldbase + (long) input[d];
    233       e += (sum > 0);
    234       input[d++] = sum / newbase;
    235       sum %= newbase;
    236     }
    237     result[--reslen] = (u_char)sum; /* accumulate remainder */
    238   }
    239   for (d=0; d < reslen; d++)
    240     result[d] = 0;
    241 }
    242