xref: /csrg-svn/lib/libc/net/ns_addr.c (revision 32297)
1 /*
2  * Copyright (c) 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  * Includes material written at Cornell University, by J. Q. Johnson.
7  * Used by permission.
8  */
9 
10 #if defined(LIBC_SCCS) && !defined(lint)
11 static char sccsid[] = "@(#)ns_addr.c	6.3 (Berkeley) 09/30/87";
12 #endif LIBC_SCCS and not lint
13 
14 #include <sys/types.h>
15 #include <netns/ns.h>
16 
17 static struct ns_addr addr, zero_addr;
18 
19 struct ns_addr
20 ns_addr(name)
21 	char *name;
22 {
23 	char separator = '.';
24 	char *hostname, *socketname, *cp;
25 	char buf[50];
26 	extern char *index();
27 
28 	addr = zero_addr;
29 	(void)strncpy(buf, name, 49);
30 
31 	/*
32 	 * First, figure out what he intends as a field separtor.
33 	 * Despite the way this routine is written, the prefered
34 	 * form  2-272.AA001234H.01777, i.e. XDE standard.
35 	 * Great efforts are made to insure backward compatability.
36 	 */
37 	if (hostname = index(buf, '#'))
38 		separator = '#';
39 	else {
40 		hostname = index(buf, '.');
41 		if ((cp = index(buf, ':')) &&
42 		    ( (hostname && cp < hostname) || (hostname == 0))) {
43 			hostname = cp;
44 			separator = ':';
45 		}
46 	}
47 	if (hostname)
48 		*hostname++ = 0;
49 	Field(buf, addr.x_net.c_net, 4);
50 	if (hostname == 0)
51 		return (addr);  /* No separator means net only */
52 
53 	socketname = index(hostname, separator);
54 	if (socketname) {
55 		*socketname++ = 0;
56 		Field(socketname, (u_char *)&addr.x_port, 2);
57 	}
58 
59 	Field(hostname, addr.x_host.c_host, 6);
60 
61 	return (addr);
62 }
63 
64 static
65 Field(buf, out, len)
66 char *buf;
67 u_char *out;
68 int len;
69 {
70 	register char *bp = buf;
71 	int i, ibase, base16 = 0, base10 = 0, clen = 0;
72 	int hb[6], *hp;
73 	char *fmt;
74 
75 	/*
76 	 * first try 2-273#2-852-151-014#socket
77 	 */
78 	if ((*buf != '-') &&
79 	    (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
80 			&hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
81 		cvtbase(1000L, 256, hb, i, out, len);
82 		return;
83 	}
84 	/*
85 	 * try form 8E1#0.0.AA.0.5E.E6#socket
86 	 */
87 	if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
88 			&hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
89 		cvtbase(256L, 256, hb, i, out, len);
90 		return;
91 	}
92 	/*
93 	 * try form 8E1#0:0:AA:0:5E:E6#socket
94 	 */
95 	if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
96 			&hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
97 		cvtbase(256L, 256, hb, i, out, len);
98 		return;
99 	}
100 	/*
101 	 * This is REALLY stretching it but there was a
102 	 * comma notation separting shorts -- definitely non standard
103 	 */
104 	if (1 < (i = sscanf(buf,"%x,%x,%x",
105 			&hb[0], &hb[1], &hb[2]))) {
106 		hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
107 		hb[2] = htons(hb[2]);
108 		cvtbase(65536L, 256, hb, i, out, len);
109 		return;
110 	}
111 
112 	/* Need to decide if base 10, 16 or 8 */
113 	while (*bp) switch (*bp++) {
114 
115 	case '0': case '1': case '2': case '3': case '4': case '5':
116 	case '6': case '7': case '-':
117 		break;
118 
119 	case '8': case '9':
120 		base10 = 1;
121 		break;
122 
123 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
124 	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
125 		base16 = 1;
126 		break;
127 
128 	case 'x': case 'X':
129 		*--bp = '0';
130 		base16 = 1;
131 		break;
132 
133 	case 'h': case 'H':
134 		base16 = 1;
135 		/* fall into */
136 
137 	default:
138 		*--bp = 0; /* Ends Loop */
139 	}
140 	if (base16) {
141 		fmt = "%3x";
142 		ibase = 4096;
143 	} else if (base10 == 0 && *buf == '0') {
144 		fmt = "%3o";
145 		ibase = 512;
146 	} else {
147 		fmt = "%3d";
148 		ibase = 1000;
149 	}
150 
151 	for (bp = buf; *bp++; ) clen++;
152 	if (clen == 0) clen++;
153 	if (clen > 18) clen = 18;
154 	i = ((clen - 1) / 3) + 1;
155 	bp = clen + buf - 3;
156 	hp = hb + i - 1;
157 
158 	while (hp > hb) {
159 		(void)sscanf(bp, fmt, hp);
160 		bp[0] = 0;
161 		hp--;
162 		bp -= 3;
163 	}
164 	(void)sscanf(buf, fmt, hp);
165 	cvtbase((long)ibase, 256, hb, i, out, len);
166 }
167 
168 static
169 cvtbase(oldbase,newbase,input,inlen,result,reslen)
170 	long oldbase;
171 	int newbase;
172 	int input[];
173 	int inlen;
174 	unsigned char result[];
175 	int reslen;
176 {
177 	int d, e;
178 	long sum;
179 
180 	e = 1;
181 	while (e > 0 && reslen > 0) {
182 		d = 0; e = 0; sum = 0;
183 		/* long division: input=input/newbase */
184 		while (d < inlen) {
185 			sum = sum*oldbase + (long) input[d];
186 			e += (sum > 0);
187 			input[d++] = sum / newbase;
188 			sum %= newbase;
189 		}
190 		result[--reslen] = sum;	/* accumulate remainder */
191 	}
192 	for (d=0; d < reslen; d++)
193 		result[d] = 0;
194 }
195