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