Annotation of 43BSDReno/lib/libc/net/ns_addr.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1986 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * This code is derived from software contributed to Berkeley by
        !             6:  * J.Q. Johnson.
        !             7:  *
        !             8:  * Redistribution and use in source and binary forms are permitted provided
        !             9:  * that: (1) source distributions retain this entire copyright notice and
        !            10:  * comment, and (2) distributions including binaries display the following
        !            11:  * acknowledgement:  ``This product includes software developed by the
        !            12:  * University of California, Berkeley and its contributors'' in the
        !            13:  * documentation or other materials provided with the distribution and in
        !            14:  * all advertising materials mentioning features or use of this software.
        !            15:  * Neither the name of the University nor the names of its contributors may
        !            16:  * be used to endorse or promote products derived from this software without
        !            17:  * specific prior written permission.
        !            18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            21:  */
        !            22: 
        !            23: #if defined(LIBC_SCCS) && !defined(lint)
        !            24: static char sccsid[] = "@(#)ns_addr.c  6.6 (Berkeley) 6/6/90";
        !            25: #endif /* LIBC_SCCS and not lint */
        !            26: 
        !            27: #include <sys/types.h>
        !            28: #include <netns/ns.h>
        !            29: 
        !            30: static struct ns_addr addr, zero_addr;
        !            31: 
        !            32: struct ns_addr 
        !            33: ns_addr(name)
        !            34:        char *name;
        !            35: {
        !            36:        char separator = '.';
        !            37:        char *hostname, *socketname, *cp;
        !            38:        char buf[50];
        !            39:        extern char *index();
        !            40: 
        !            41:        addr = zero_addr;
        !            42:        (void)strncpy(buf, name, 49);
        !            43: 
        !            44:        /*
        !            45:         * First, figure out what he intends as a field separtor.
        !            46:         * Despite the way this routine is written, the prefered
        !            47:         * form  2-272.AA001234H.01777, i.e. XDE standard.
        !            48:         * Great efforts are made to insure backward compatability.
        !            49:         */
        !            50:        if (hostname = index(buf, '#'))
        !            51:                separator = '#';
        !            52:        else {
        !            53:                hostname = index(buf, '.');
        !            54:                if ((cp = index(buf, ':')) &&
        !            55:                    ( (hostname && cp < hostname) || (hostname == 0))) {
        !            56:                        hostname = cp;
        !            57:                        separator = ':';
        !            58:                }
        !            59:        }
        !            60:        if (hostname)
        !            61:                *hostname++ = 0;
        !            62:        Field(buf, addr.x_net.c_net, 4);
        !            63:        if (hostname == 0)
        !            64:                return (addr);  /* No separator means net only */
        !            65: 
        !            66:        socketname = index(hostname, separator);
        !            67:        if (socketname) {
        !            68:                *socketname++ = 0;
        !            69:                Field(socketname, (u_char *)&addr.x_port, 2);
        !            70:        }
        !            71: 
        !            72:        Field(hostname, addr.x_host.c_host, 6);
        !            73: 
        !            74:        return (addr);
        !            75: }
        !            76: 
        !            77: static
        !            78: Field(buf, out, len)
        !            79: char *buf;
        !            80: u_char *out;
        !            81: int len;
        !            82: {
        !            83:        register char *bp = buf;
        !            84:        int i, ibase, base16 = 0, base10 = 0, clen = 0;
        !            85:        int hb[6], *hp;
        !            86:        char *fmt;
        !            87: 
        !            88:        /*
        !            89:         * first try 2-273#2-852-151-014#socket
        !            90:         */
        !            91:        if ((*buf != '-') &&
        !            92:            (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
        !            93:                        &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
        !            94:                cvtbase(1000L, 256, hb, i, out, len);
        !            95:                return;
        !            96:        }
        !            97:        /*
        !            98:         * try form 8E1#0.0.AA.0.5E.E6#socket
        !            99:         */
        !           100:        if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
        !           101:                        &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
        !           102:                cvtbase(256L, 256, hb, i, out, len);
        !           103:                return;
        !           104:        }
        !           105:        /*
        !           106:         * try form 8E1#0:0:AA:0:5E:E6#socket
        !           107:         */
        !           108:        if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
        !           109:                        &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
        !           110:                cvtbase(256L, 256, hb, i, out, len);
        !           111:                return;
        !           112:        }
        !           113:        /*
        !           114:         * This is REALLY stretching it but there was a
        !           115:         * comma notation separting shorts -- definitely non standard
        !           116:         */
        !           117:        if (1 < (i = sscanf(buf,"%x,%x,%x",
        !           118:                        &hb[0], &hb[1], &hb[2]))) {
        !           119:                hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
        !           120:                hb[2] = htons(hb[2]);
        !           121:                cvtbase(65536L, 256, hb, i, out, len);
        !           122:                return;
        !           123:        }
        !           124: 
        !           125:        /* Need to decide if base 10, 16 or 8 */
        !           126:        while (*bp) switch (*bp++) {
        !           127: 
        !           128:        case '0': case '1': case '2': case '3': case '4': case '5':
        !           129:        case '6': case '7': case '-':
        !           130:                break;
        !           131: 
        !           132:        case '8': case '9':
        !           133:                base10 = 1;
        !           134:                break;
        !           135: 
        !           136:        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        !           137:        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        !           138:                base16 = 1;
        !           139:                break;
        !           140:        
        !           141:        case 'x': case 'X':
        !           142:                *--bp = '0';
        !           143:                base16 = 1;
        !           144:                break;
        !           145: 
        !           146:        case 'h': case 'H':
        !           147:                base16 = 1;
        !           148:                /* fall into */
        !           149: 
        !           150:        default:
        !           151:                *--bp = 0; /* Ends Loop */
        !           152:        }
        !           153:        if (base16) {
        !           154:                fmt = "%3x";
        !           155:                ibase = 4096;
        !           156:        } else if (base10 == 0 && *buf == '0') {
        !           157:                fmt = "%3o";
        !           158:                ibase = 512;
        !           159:        } else {
        !           160:                fmt = "%3d";
        !           161:                ibase = 1000;
        !           162:        }
        !           163: 
        !           164:        for (bp = buf; *bp++; ) clen++;
        !           165:        if (clen == 0) clen++;
        !           166:        if (clen > 18) clen = 18;
        !           167:        i = ((clen - 1) / 3) + 1;
        !           168:        bp = clen + buf - 3;
        !           169:        hp = hb + i - 1;
        !           170: 
        !           171:        while (hp > hb) {
        !           172:                (void)sscanf(bp, fmt, hp);
        !           173:                bp[0] = 0;
        !           174:                hp--;
        !           175:                bp -= 3;
        !           176:        }
        !           177:        (void)sscanf(buf, fmt, hp);
        !           178:        cvtbase((long)ibase, 256, hb, i, out, len);
        !           179: }
        !           180: 
        !           181: static
        !           182: cvtbase(oldbase,newbase,input,inlen,result,reslen)
        !           183:        long oldbase;
        !           184:        int newbase;
        !           185:        int input[];
        !           186:        int inlen;
        !           187:        unsigned char result[];
        !           188:        int reslen;
        !           189: {
        !           190:        int d, e;
        !           191:        long sum;
        !           192: 
        !           193:        e = 1;
        !           194:        while (e > 0 && reslen > 0) {
        !           195:                d = 0; e = 0; sum = 0;
        !           196:                /* long division: input=input/newbase */
        !           197:                while (d < inlen) {
        !           198:                        sum = sum*oldbase + (long) input[d];
        !           199:                        e += (sum > 0);
        !           200:                        input[d++] = sum / newbase;
        !           201:                        sum %= newbase;
        !           202:                }
        !           203:                result[--reslen] = sum; /* accumulate remainder */
        !           204:        }
        !           205:        for (d=0; d < reslen; d++)
        !           206:                result[d] = 0;
        !           207: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.