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