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