2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
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,
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
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
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>
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
)
98 sprintf(libPtr
->inet_ntoa
,
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
)
111 D(__log(LOG_DEBUG
,"Inet_NtoA(0x%08lx) called",s_addr
);)
112 return __Inet_NtoA(s_addr
, libPtr
);
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
;
129 u_long parts
[4], *pp
= parts
;
133 * Collect number up to ``.''.
134 * Values are specified as for C:
135 * 0x=hex, 0=octal, other=decimal.
139 if (*++cp
== 'x' || *cp
== 'X')
144 while ((c
= *cp
) != '\0') {
145 if (isascii(c
) && isdigit(c
)) {
146 val
= (val
* base
) + (c
- '0');
150 if (base
== 16 && isascii(c
) && isxdigit(c
)) {
152 (c
+ 10 - (islower(c
) ? 'a' : 'A'));
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)
172 * Check for trailing characters.
174 if (*cp
&& (!isascii(*cp
) || !isspace(*cp
)))
177 * Concoct the address according to
178 * the number of parts specified.
183 case 1: /* a -- 32 bits */
186 case 2: /* a.b -- 8.24 bits */
189 val
|= parts
[0] << 24;
192 case 3: /* a.b.c -- 8.8.16 bits */
195 val
|= (parts
[0] << 24) | (parts
[1] << 16);
198 case 4: /* a.b.c.d -- 8.8.8.8 bits */
201 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
205 addr
->s_addr
= htonl(val
);
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
)
217 return __inet_aton(cp
, addr
);
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
)
239 if (__inet_aton(cp
, &val
))
241 return (INADDR_NONE
);
245 /* from inet_lnaof.c */
247 * Return the local network address portion of an
248 * internet address; handles class a/b/c network
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
)
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
);
266 return ((s_addr
)&IN_CLASSC_HOST
);
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
)
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
);
290 return (((s_addr
)&IN_CLASSC_NET
) >> IN_CLASSC_NSHIFT
);
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(
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
)
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
);
324 /* from inet_network.c */
326 * Internet network address interpretation routine.
327 * The library routines call this routine to interpret
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
)
338 register u_long val
, base
, n
;
340 u_long parts
[4], *pp
= parts
;
347 if (*cp
== 'x' || *cp
== 'X')
351 val
= (val
* base
) + (c
- '0');
355 if (base
== 16 && isxdigit(c
)) {
356 val
= (val
<< 4) + (c
+ 10 - (islower(c
) ? 'a' : 'A'));
364 return (INADDR_NONE
);
368 if (*cp
&& !isspace(*cp
))
369 return (INADDR_NONE
);
373 return (INADDR_NONE
);
374 for (val
= 0, i
= 0; i
< n
; i
++) {
376 val
|= parts
[i
] & 0xff;