]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/ns_addr.c
Fix send to properly wait while long transmits are in progress
[mirror_edk2.git] / StdLib / BsdSocketLib / ns_addr.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1986, 1993\r
3 * The Regents of the University of California. All rights reserved.\r
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
11 * \r
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
15 * \r
16 * 1. Redistributions of source code must retain the above copyright\r
17 * notice, this list of conditions and the following disclaimer.\r
18 * \r
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
22 * \r
23 * 3. All advertising materials mentioning features or use of this software\r
24 * must display the following acknowledgement:\r
25 * \r
26 * This product includes software developed by the University of\r
27 * California, Berkeley, Intel Corporation, and its contributors.\r
28 * \r
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
32 * \r
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
48static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";\r
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
56static struct ns_addr addr, zero_addr;\r
57\r
58static void Field(), cvtbase();\r
59\r
60struct ns_addr\r
61ns_addr(\r
62 const char *name\r
63 )\r
64{\r
65 char separator;\r
66 char *hostname, *socketname, *cp;\r
67 char buf[50];\r
68\r
69 (void)strncpy(buf, name, sizeof(buf) - 1);\r
70 buf[sizeof(buf) - 1] = '\0';\r
71\r
72 /*\r
73 * First, figure out what he intends as a field separtor.\r
74 * Despite the way this routine is written, the prefered\r
75 * form 2-272.AA001234H.01777, i.e. XDE standard.\r
76 * Great efforts are made to insure backward compatability.\r
77 */\r
78 if ((hostname = strchr(buf, '#')) != NULL)\r
79 separator = '#';\r
80 else {\r
81 hostname = strchr(buf, '.');\r
82 if ((cp = strchr(buf, ':')) &&\r
83 ((hostname && cp < hostname) || (hostname == 0))) {\r
84 hostname = cp;\r
85 separator = ':';\r
86 } else\r
87 separator = '.';\r
88 }\r
89 if (hostname)\r
90 *hostname++ = 0;\r
91\r
92 addr = zero_addr;\r
93 Field(buf, addr.x_net.c_net, 4);\r
94 if (hostname == 0)\r
95 return (addr); /* No separator means net only */\r
96\r
97 socketname = strchr(hostname, separator);\r
98 if (socketname) {\r
99 *socketname++ = 0;\r
100 Field(socketname, (u_char *)&addr.x_port, 2);\r
101 }\r
102\r
103 Field(hostname, addr.x_host.c_host, 6);\r
104\r
105 return (addr);\r
106}\r
107\r
108static void\r
109Field(\r
110 char *buf,\r
111 u_char *out,\r
112 int len\r
113 )\r
114{\r
115 register char *bp = buf;\r
116 int i, ibase, base16 = 0, base10 = 0, clen = 0;\r
117 int hb[6], *hp;\r
118 char *fmt;\r
119\r
120 /*\r
121 * first try 2-273#2-852-151-014#socket\r
122 */\r
123 if ((*buf != '-') &&\r
124 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",\r
125 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {\r
126 cvtbase(1000L, 256, hb, i, out, len);\r
127 return;\r
128 }\r
129 /*\r
130 * try form 8E1#0.0.AA.0.5E.E6#socket\r
131 */\r
132 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",\r
133 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {\r
134 cvtbase(256L, 256, hb, i, out, len);\r
135 return;\r
136 }\r
137 /*\r
138 * try form 8E1#0:0:AA:0:5E:E6#socket\r
139 */\r
140 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",\r
141 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {\r
142 cvtbase(256L, 256, hb, i, out, len);\r
143 return;\r
144 }\r
145 /*\r
146 * This is REALLY stretching it but there was a\r
147 * comma notation separting shorts -- definitely non standard\r
148 */\r
149 if (1 < (i = sscanf(buf,"%x,%x,%x",\r
150 &hb[0], &hb[1], &hb[2]))) {\r
151 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);\r
152 hb[2] = htons(hb[2]);\r
153 cvtbase(65536L, 256, hb, i, out, len);\r
154 return;\r
155 }\r
156\r
157 /* Need to decide if base 10, 16 or 8 */\r
158 while (*bp) switch (*bp++) {\r
159\r
160 case '0': case '1': case '2': case '3': case '4': case '5':\r
161 case '6': case '7': case '-':\r
162 break;\r
163\r
164 case '8': case '9':\r
165 base10 = 1;\r
166 break;\r
167\r
168 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\r
169 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\r
170 base16 = 1;\r
171 break;\r
172\r
173 case 'x': case 'X':\r
174 *--bp = '0';\r
175 base16 = 1;\r
176 break;\r
177\r
178 case 'h': case 'H':\r
179 base16 = 1;\r
180 /* fall into */\r
181\r
182 default:\r
183 *--bp = 0; /* Ends Loop */\r
184 }\r
185 if (base16) {\r
186 fmt = "%3x";\r
187 ibase = 4096;\r
188 } else if (base10 == 0 && *buf == '0') {\r
189 fmt = "%3o";\r
190 ibase = 512;\r
191 } else {\r
192 fmt = "%3d";\r
193 ibase = 1000;\r
194 }\r
195\r
196 for (bp = buf; *bp++; ) clen++;\r
197 if (clen == 0) clen++;\r
198 if (clen > 18) clen = 18;\r
199 i = ((clen - 1) / 3) + 1;\r
200 bp = clen + buf - 3;\r
201 hp = hb + i - 1;\r
202\r
203 while (hp > hb) {\r
204 (void)sscanf(bp, fmt, hp);\r
205 bp[0] = 0;\r
206 hp--;\r
207 bp -= 3;\r
208 }\r
209 (void)sscanf(buf, fmt, hp);\r
210 cvtbase((long)ibase, 256, hb, i, out, len);\r
211}\r
212\r
213static void\r
214cvtbase(\r
215 long oldbase,\r
216 int newbase,\r
217 int input[],\r
218 int inlen,\r
219 unsigned char result[],\r
220 int reslen\r
221 )\r
222{\r
223 int d, e;\r
224 long sum;\r
225\r
226 e = 1;\r
227 while (e > 0 && reslen > 0) {\r
228 d = 0; e = 0; sum = 0;\r
229 /* long division: input=input/newbase */\r
230 while (d < inlen) {\r
231 sum = sum*oldbase + (long) input[d];\r
232 e += (sum > 0);\r
233 input[d++] = sum / newbase;\r
234 sum %= newbase;\r
235 }\r
236 result[--reslen] = (u_char)sum; /* accumulate remainder */\r
237 }\r
238 for (d=0; d < reslen; d++)\r
239 result[d] = 0;\r
240}\r