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