revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / amiga_libcalls.c
blobafc9e74485157abf52d6dc0e33f4f8b837f64947
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 Neil Cafferkey
6 * Copyright (C) 2005 Pavel Fedin
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
25 * Copyright (c) 1983, 1990 Regents of the University of California.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
57 #include <conf.h>
59 #include <aros/libcall.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/socketvar.h>
64 #include <sys/kernel.h>
65 #include <sys/ioctl.h>
66 #include <sys/protosw.h>
67 #include <sys/malloc.h>
68 #include <sys/synch.h>
69 #include <sys/socket.h>
70 #include <sys/errno.h>
72 #include <netinet/in.h>
74 #include <kern/amiga_includes.h>
76 #include <api/amiga_api.h>
77 #include <api/amiga_libcallentry.h>
78 #include <api/allocdatabuffer.h>
80 #include <ctype.h>
82 #include <proto/bsdsocket.h>
85 * Functions which are defined in link library in unix systems
88 /* from inet_ntoa.c */
90 * Convert network-format internet address
91 * to base 256 d.d.d.d representation.
93 char * __Inet_NtoA(ULONG s_addr, struct SocketBase *libPtr)
95 NTOHL(s_addr);
97 CHECK_TASK2();
98 sprintf(libPtr->inet_ntoa,
99 "%ld.%ld.%ld.%ld",
100 (long)(s_addr>>24) & 0xff,
101 (long)(s_addr>>16) & 0xff,
102 (long)(s_addr>>8) & 0xff,
103 (long)s_addr & 0xff);
104 return ((char *)libPtr->inet_ntoa);
106 AROS_LH1(char *, Inet_NtoA,
107 AROS_LHA(ULONG, s_addr, D0),
108 struct SocketBase *, libPtr, 29, UL)
110 AROS_LIBFUNC_INIT
111 D(__log(LOG_DEBUG,"Inet_NtoA(0x%08lx) called",s_addr);)
112 return __Inet_NtoA(s_addr, libPtr);
113 AROS_LIBFUNC_EXIT
116 /* from inet_addr.c */
118 * Check whether "cp" is a valid ascii representation
119 * of an Internet address and convert to a binary address.
120 * Returns 1 if the address is valid, 0 if not.
121 * This replaces inet_addr, the return value from which
122 * cannot distinguish between failure and a local broadcast address.
125 LONG __inet_aton(CONST_STRPTR cp, struct in_addr * addr)
127 register u_long val, base, n;
128 register char c;
129 u_long parts[4], *pp = parts;
131 for (;;) {
133 * Collect number up to ``.''.
134 * Values are specified as for C:
135 * 0x=hex, 0=octal, other=decimal.
137 val = 0; base = 10;
138 if (*cp == '0') {
139 if (*++cp == 'x' || *cp == 'X')
140 base = 16, cp++;
141 else
142 base = 8;
144 while ((c = *cp) != '\0') {
145 if (isascii(c) && isdigit(c)) {
146 val = (val * base) + (c - '0');
147 cp++;
148 continue;
150 if (base == 16 && isascii(c) && isxdigit(c)) {
151 val = (val << 4) +
152 (c + 10 - (islower(c) ? 'a' : 'A'));
153 cp++;
154 continue;
156 break;
158 if (*cp == '.') {
160 * Internet format:
161 * a.b.c.d
162 * a.b.c (with c treated as 16-bits)
163 * a.b (with b treated as 24 bits)
165 if (pp >= parts + 3 || val > 0xff)
166 return (0);
167 *pp++ = val, cp++;
168 } else
169 break;
172 * Check for trailing characters.
174 if (*cp && (!isascii(*cp) || !isspace(*cp)))
175 return (0);
177 * Concoct the address according to
178 * the number of parts specified.
180 n = pp - parts + 1;
181 switch (n) {
183 case 1: /* a -- 32 bits */
184 break;
186 case 2: /* a.b -- 8.24 bits */
187 if (val > 0xffffff)
188 return (0);
189 val |= parts[0] << 24;
190 break;
192 case 3: /* a.b.c -- 8.8.16 bits */
193 if (val > 0xffff)
194 return (0);
195 val |= (parts[0] << 24) | (parts[1] << 16);
196 break;
198 case 4: /* a.b.c.d -- 8.8.8.8 bits */
199 if (val > 0xff)
200 return (0);
201 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
202 break;
204 if (addr)
205 addr->s_addr = htonl(val);
206 return (1);
209 #if defined(__CONFIG_ROADSHOW__)
210 AROS_LH2(LONG, inet_aton,
211 AROS_LHA(STRPTR, cp, A0),
212 AROS_LHA(struct in_addr *, addr, A1),
213 struct SocketBase *, libPtr, 99, UL)
215 AROS_LIBFUNC_INIT
217 return __inet_aton(cp, addr);
219 AROS_LIBFUNC_EXIT
222 #endif
225 * Ascii internet address interpretation routine.
226 * The value returned is in network order.
229 /*ULONG SAVEDS inet_addr(
230 REG(a0, const char *cp),
231 REG(a6, struct SocketBase *libPtr))*/
232 AROS_LH1(ULONG, inet_addr,
233 AROS_LHA(const char *, cp, A0),
234 struct SocketBase *, libPtr, 30, UL)
236 AROS_LIBFUNC_INIT
237 struct in_addr val;
239 if (__inet_aton(cp, &val))
240 return (val.s_addr);
241 return (INADDR_NONE);
242 AROS_LIBFUNC_EXIT
245 /* from inet_lnaof.c */
247 * Return the local network address portion of an
248 * internet address; handles class a/b/c network
249 * number formats.
251 /*ULONG SAVEDS Inet_LnaOf(
252 REG(d0, ULONG s_addr),
253 REG(a6, struct SocketBase *libPtr))*/
254 AROS_LH1(ULONG, Inet_LnaOf,
255 AROS_LHA(ULONG, s_addr, D0),
256 struct SocketBase *, libPtr, 31, UL)
258 AROS_LIBFUNC_INIT
259 NTOHL(s_addr);
261 if (IN_CLASSA(s_addr))
262 return ((s_addr)&IN_CLASSA_HOST);
263 else if (IN_CLASSB(s_addr))
264 return ((s_addr)&IN_CLASSB_HOST);
265 else
266 return ((s_addr)&IN_CLASSC_HOST);
267 AROS_LIBFUNC_EXIT
270 /* from inet_netof.c */
272 * Return the network number from an internet
273 * address; handles class a/b/c network #'s.
275 /*ULONG SAVEDS Inet_NetOf(
276 REG(d0, ULONG s_addr),
277 REG(a6, struct SocketBase *libPtr))*/
278 AROS_LH1(ULONG, Inet_NetOf,
279 AROS_LHA(ULONG, s_addr, D0),
280 struct SocketBase *, libPtr, 32, UL)
282 AROS_LIBFUNC_INIT
283 NTOHL(s_addr);
285 if (IN_CLASSA(s_addr))
286 return (((s_addr)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
287 else if (IN_CLASSB(s_addr))
288 return (((s_addr)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
289 else
290 return (((s_addr)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
291 AROS_LIBFUNC_EXIT
294 /* from inet_makeaddr.c */
296 * Formulate an Internet address from network + host. Used in
297 * building addresses stored in the ifnet structure.
299 /*ULONG SAVEDS Inet_MakeAddr(
300 REG(d0, ULONG net),
301 REG(d1, ULONG host),
302 REG(a6, struct SocketBase *libPtr))*/
303 AROS_LH2(ULONG, Inet_MakeAddr,
304 AROS_LHA(ULONG, net, D0),
305 AROS_LHA(ULONG, host, D1),
306 struct SocketBase *, libPtr, 33, UL)
308 AROS_LIBFUNC_INIT
309 u_long addr;
311 if (net < 128)
312 addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
313 else if (net < 65536)
314 addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
315 else if (net < 16777216L)
316 addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
317 else
318 addr = net | host;
320 return htonl(addr);
321 AROS_LIBFUNC_EXIT
324 /* from inet_network.c */
326 * Internet network address interpretation routine.
327 * The library routines call this routine to interpret
328 * network numbers.
330 /*ULONG SAVEDS inet_network(
331 REG(a0, const char *cp),
332 REG(a6, struct SocketBase *libPtr))*/
333 AROS_LH1(ULONG, inet_network,
334 AROS_LHA(const char *, cp, A0),
335 struct SocketBase *, libPtr, 34, UL)
337 AROS_LIBFUNC_INIT
338 register u_long val, base, n;
339 register char c;
340 u_long parts[4], *pp = parts;
341 register int i;
343 again:
344 val = 0; base = 10;
345 if (*cp == '0')
346 base = 8, cp++;
347 if (*cp == 'x' || *cp == 'X')
348 base = 16, cp++;
349 while (c = *cp) {
350 if (isdigit(c)) {
351 val = (val * base) + (c - '0');
352 cp++;
353 continue;
355 if (base == 16 && isxdigit(c)) {
356 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
357 cp++;
358 continue;
360 break;
362 if (*cp == '.') {
363 if (pp >= parts + 4)
364 return (INADDR_NONE);
365 *pp++ = val, cp++;
366 goto again;
368 if (*cp && !isspace(*cp))
369 return (INADDR_NONE);
370 *pp++ = val;
371 n = pp - parts;
372 if (n > 4)
373 return (INADDR_NONE);
374 for (val = 0, i = 0; i < n; i++) {
375 val <<= 8;
376 val |= parts[i] & 0xff;
378 return (val);
379 AROS_LIBFUNC_EXIT