1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LPOSIX/ARPA.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,261 @@
1.4 +/* ARPA.C
1.5 + *
1.6 + * Portions Copyright (c) 1997-1999 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Implementation of the ARPA/INET.H functions, based on FreeBSD.
1.12 + */
1.13 +
1.14 +/*
1.15 + * Copyright (c) 1983, 1993
1.16 + * The Regents of the University of California. All rights reserved.
1.17 + *
1.18 + * Redistribution and use in source and binary forms, with or without
1.19 + * modification, are permitted provided that the following conditions
1.20 + * are met:
1.21 + * 1. Redistributions of source code must retain the above copyright
1.22 + * notice, this list of conditions and the following disclaimer.
1.23 + * 2. Redistributions in binary form must reproduce the above copyright
1.24 + * notice, this list of conditions and the following disclaimer in the
1.25 + * documentation and/or other materials provided with the distribution.
1.26 + * 3. All advertising materials mentioning features or use of this software
1.27 + * must display the following acknowledgement:
1.28 + * This product includes software developed by the University of
1.29 + * California, Berkeley and its contributors.
1.30 + * 4. Neither the name of the University nor the names of its contributors
1.31 + * may be used to endorse or promote products derived from this software
1.32 + * without specific prior written permission.
1.33 + *
1.34 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1.35 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.36 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.37 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1.38 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.39 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.40 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.41 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.42 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.43 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.44 + * SUCH DAMAGE.
1.45 + * Portions Copyright (c) 1993 by Digital Equipment Corporation.
1.46 + *
1.47 + * Permission to use, copy, modify, and distribute this software for any
1.48 + * purpose with or without fee is hereby granted, provided that the above
1.49 + * copyright notice and this permission notice appear in all copies, and that
1.50 + * the name of Digital Equipment Corporation not be used in advertising or
1.51 + * publicity pertaining to distribution of the document or software without
1.52 + * specific, written prior permission.
1.53 + *
1.54 + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
1.55 + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
1.56 + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
1.57 + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1.58 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1.59 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1.60 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1.61 + * SOFTWARE.
1.62 + * -
1.63 + * --Copyright--
1.64 + */
1.65 +
1.66 +#include <stdio_r.h>
1.67 +#include <netdb_r.h>
1.68 +#include <ctype.h>
1.69 +#include <sys/types.h>
1.70 +#include <libc/netinet/in.h>
1.71 +#include <libc/arpa/inet.h>
1.72 +
1.73 +
1.74 +/**
1.75 +Ascii internet address interpretation routine.
1.76 +@return The value returned is in network order.
1.77 +@param cp ascii representation of an Internet address
1.78 +*/
1.79 +EXPORT_C unsigned long inet_addr(const char *cp)
1.80 +{
1.81 + struct in_addr val;
1.82 +
1.83 + if (inet_aton(cp, &val))
1.84 + return (val.s_addr);
1.85 + return (INADDR_NONE);
1.86 +}
1.87 +
1.88 +/**
1.89 +Check whether "cp" is a valid ascii representation
1.90 +of an Internet address and convert to a binary address.
1.91 +This replaces inet_addr, the return value from which
1.92 +cannot distinguish between failure and a local broadcast address.
1.93 +@return 1 if the address is valid, 0 if not.
1.94 +@param cp ascii representation of an Internet address
1.95 +@param addr internet address
1.96 +*/
1.97 +EXPORT_C int inet_aton(const char* cp, struct in_addr* addr)
1.98 +{
1.99 + unsigned long val;
1.100 + int base, n;
1.101 + char c;
1.102 + unsigned int parts[4];
1.103 + unsigned int *pp = parts;
1.104 +
1.105 + c = *cp;
1.106 + for (;;) {
1.107 + /*
1.108 + * Collect number up to ``.''.
1.109 + * Values are specified as for C:
1.110 + * 0x=hex, 0=octal, isdigit=decimal.
1.111 + */
1.112 + if (!isdigit(c))
1.113 + return (0);
1.114 + val = 0; base = 10;
1.115 + if (c == '0') {
1.116 + c = *++cp;
1.117 + if (c == 'x' || c == 'X')
1.118 + base = 16, c = *++cp;
1.119 + else
1.120 + base = 8;
1.121 + }
1.122 + for (;;) {
1.123 + if (isascii(c) && isdigit(c)) {
1.124 + val = (val * base) + (c - '0');
1.125 + c = *++cp;
1.126 + } else if (base == 16 && isascii(c) && isxdigit(c)) {
1.127 + val = (val << 4) |
1.128 + (c + 10 - (islower(c) ? 'a' : 'A'));
1.129 + c = *++cp;
1.130 + } else
1.131 + break;
1.132 + }
1.133 + if (c == '.') {
1.134 + /*
1.135 + * Internet format:
1.136 + * a.b.c.d
1.137 + * a.b.c (with c treated as 16 bits)
1.138 + * a.b (with b treated as 24 bits)
1.139 + */
1.140 + if (pp >= parts + 3)
1.141 + return (0);
1.142 + if (val > 0xff) /* Check 8-bit a, b, c */
1.143 + return (0);
1.144 + *pp++ = val;
1.145 + c = *++cp;
1.146 + } else
1.147 + break;
1.148 + }
1.149 + /*
1.150 + * Check for trailing characters.
1.151 + */
1.152 + if (c != '\0' && (!isascii(c) || !isspace(c)))
1.153 + return (0);
1.154 + /*
1.155 + * Concoct the address according to
1.156 + * the number of parts specified.
1.157 + */
1.158 + n = pp - parts + 1;
1.159 + switch (n) {
1.160 +
1.161 + case 0:
1.162 + return (0); /* initial nondigit */
1.163 +
1.164 + case 1: /* a -- 32 bits */
1.165 + break;
1.166 +
1.167 + case 2: /* a.b -- 8.24 bits */
1.168 + if (val > 0xffffff)
1.169 + return (0);
1.170 + val |= parts[0] << 24;
1.171 + break;
1.172 +
1.173 + case 3: /* a.b.c -- 8.8.16 bits */
1.174 + if (val > 0xffff)
1.175 + return (0);
1.176 + val |= (parts[0] << 24) | (parts[1] << 16);
1.177 + break;
1.178 +
1.179 + case 4: /* a.b.c.d -- 8.8.8.8 bits */
1.180 + if (val > 0xff)
1.181 + return (0);
1.182 + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
1.183 + break;
1.184 + }
1.185 + if (addr)
1.186 + addr->s_addr = htonl(val);
1.187 + return (1);
1.188 +}
1.189 +
1.190 +/*
1.191 + * Convert network-format internet address
1.192 + * to base 256 d.d.d.d representation.
1.193 + */
1.194 +EXPORT_C char* _inet_ntoa_r(struct _reent* rp, struct in_addr in)
1.195 +{
1.196 + unsigned char* p = (unsigned char *)∈
1.197 + _sprintf_r(rp, rp->_tmpnam, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
1.198 + return rp->_tmpnam;
1.199 +}
1.200 +
1.201 +#ifndef _REENT_ONLY
1.202 +
1.203 +EXPORT_C char* inet_ntoa(struct in_addr in)
1.204 +{
1.205 + return _inet_ntoa_r(_REENT,in);
1.206 +}
1.207 +
1.208 +#endif /* _REENT_ONLY */
1.209 +
1.210 +/**
1.211 +Return the local network address portion of an
1.212 +internet address; handles class a/b/c network
1.213 +number formats.
1.214 +@return the local network address portion of an
1.215 +internet address
1.216 +@param in
1.217 +*/
1.218 +EXPORT_C unsigned long inet_lnaof(struct in_addr in)
1.219 +{
1.220 + register unsigned long i = ntohl(in.s_addr);
1.221 +
1.222 + if (IN_CLASSA(i))
1.223 + return ((i)&IN_CLASSA_HOST);
1.224 + else if (IN_CLASSB(i))
1.225 + return ((i)&IN_CLASSB_HOST);
1.226 + else
1.227 + return ((i)&IN_CLASSC_HOST);
1.228 +}
1.229 +
1.230 +/**
1.231 +@return the network number from an internet
1.232 +address; handles class a/b/c network #'s.
1.233 +@param in
1.234 +*/
1.235 +EXPORT_C unsigned long inet_netof(struct in_addr in)
1.236 +{
1.237 + unsigned long i = ntohl(in.s_addr);
1.238 +
1.239 + if (IN_CLASSA(i))
1.240 + return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
1.241 + else if (IN_CLASSB(i))
1.242 + return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
1.243 + else
1.244 + return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
1.245 +}
1.246 +
1.247 +/** Formulate an Internet address from network + host.
1.248 +*/
1.249 +EXPORT_C struct in_addr inet_makeaddr(unsigned long net, unsigned long host)
1.250 +{
1.251 + unsigned long addr;
1.252 +
1.253 + if (net < IN_CLASSA_MAX)
1.254 + addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
1.255 + else if (net < IN_CLASSB_MAX)
1.256 + addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
1.257 + else if (net < IN_CLASSC_MAX)
1.258 + addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
1.259 + else
1.260 + addr = net | host;
1.261 + addr = htonl(addr);
1.262 + return (*(struct in_addr *)&addr);
1.263 +}
1.264 +