inet: first few bits of the libc code we need
authorAlan Cox <alan@linux.intel.com>
Thu, 25 Feb 2016 23:53:27 +0000 (23:53 +0000)
committerAlan Cox <alan@linux.intel.com>
Thu, 25 Feb 2016 23:53:27 +0000 (23:53 +0000)
Lots more needed yet including gethostby*, resolver etc

Library/include/arpa/inet.h [new file with mode: 0644]
Library/include/netinet/in.h [new file with mode: 0644]
Library/include/sys/socket.h [new file with mode: 0644]
Library/libs/inet_addr.c [new file with mode: 0644]
Library/libs/inet_aton.c [new file with mode: 0644]
Library/libs/inet_network.c [new file with mode: 0644]
Library/libs/inet_ntoa.c [new file with mode: 0644]
Library/libs/inet_ntop.c [new file with mode: 0644]
Library/libs/inet_pton.c [new file with mode: 0644]

diff --git a/Library/include/arpa/inet.h b/Library/include/arpa/inet.h
new file mode 100644 (file)
index 0000000..2c52f33
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef _ARPA_INET_H
+#define _ARPA_INET_H
+
+/* All our big endian platforms use gcc, and gcc gets the define right
+   so trust it for big endian detection */
+
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+
+#define htonl(x)       (x)
+#define htons(x)       (x)
+
+#else
+
+extern uint32_t htonl(uint32_t __hostlong);
+extern uint16_t htons(uint16_t __hostshort);
+
+#endif
+
+#define ntohl(a)       htonl(a)
+#define ntohs(a)       htons(a)
+
+/* Legacy BSD API - avoid if possible */
+extern int inet_aton(const char *__cp, struct in_addr *__inp);
+extern in_addr_t inet_addr(const char *__cp);
+extern in_addr_t inet_network(const char *__cp);
+/* Awkward - struct argument .. may need hacks for 6502 ?? */
+extern char *inet_ntoa(struct in_addr __in);
+
+/* Modern APIs */
+extern const char *inet_ntop(int __af, const void *__src, char *__dst,
+                                                       socklen_t __size);
+extern int inet_pton(int __af, const char *__src, void *__dst);
+
+#if 0
+/* These are obsolete to the point we don't bother */
+extern struct in_addr inet_makeaddr(in_addr_t net, ip_addr_t host);
+extern in_addr_t inet_lnaof(struct in_addr in);
+extern in_addr_t inet_netof(struct in_addr in);
+#endif
+
+#endif
diff --git a/Library/include/netinet/in.h b/Library/include/netinet/in.h
new file mode 100644 (file)
index 0000000..20885c2
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef _NETINET_IN_H
+#define _NETINET_IN_H
+
+typedef uint16_t in_port_t;
+typedef uint32_t in_addr_t;
+
+struct in_addr {
+  uint32_t s_addr;
+};
+
+struct sockaddr_in {
+  sa_family_t sin_family;
+  in_port_t sin_port;
+  struct in_addr sin_addr;
+  uint8_t sin_zero[8];
+};
+
+/* This one is used internally to deal with many argumented net
+   functions */
+struct __fuzix_sockio {
+  uint16_t sio_flags;          /* Keep the order so we can use partial */
+  uint16_t sio_addr_len;       /* structs for simple cases */
+  struct sockaddr_in sio_addr;
+};
+
+#define INADDR_ANY             0L
+#define INADDR_BROADCAST       0xFFFFFFFFUL
+#define INADDR_LOOPBACK                0x7F000001UL
+#define INADDR_NONE    ((uint32_t)(-1))
+
+#define IPPROTO_ICMP   1
+#define IPPROTO_TCP    6
+#define IPPROTO_UDP    17
+#define IPPROTO_RAW    255
+
+#endif
diff --git a/Library/include/sys/socket.h b/Library/include/sys/socket.h
new file mode 100644 (file)
index 0000000..610a84d
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef _SYS_SOCKET_H
+#define _SYS_SOCKET_H
+
+#define AF_INET                1
+
+#define SOCK_RAW       1
+#define SOCK_DGAM      2
+#define SOCK_STREAM    3
+
+
+typedef int socklen_t;
+typedef uint16_t sa_family_t;
+
+
+#endif
\ No newline at end of file
diff --git a/Library/libs/inet_addr.c b/Library/libs/inet_addr.c
new file mode 100644 (file)
index 0000000..33b2013
--- /dev/null
@@ -0,0 +1,75 @@
+/*     $OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $    */
+
+/*
+ * ++Copyright++ 1983, 1990, 1993
+ * -
+ * Copyright (c) 1983, 1990, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ * 
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+/*
+ * Ascii internet address interpretation routine.
+ * The value returned is in network order.
+ *
+ * Obsolete
+ */
+in_addr_t inet_addr(const char *cp)
+{
+       struct in_addr val;
+
+       if (inet_aton(cp, &val))
+               return (val.s_addr);
+       return (INADDR_NONE);
+}
diff --git a/Library/libs/inet_aton.c b/Library/libs/inet_aton.c
new file mode 100644 (file)
index 0000000..285abb7
--- /dev/null
@@ -0,0 +1,163 @@
+/*     $OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $    */
+
+/*
+ * ++Copyright++ 1983, 1990, 1993
+ * -
+ * Copyright (c) 1983, 1990, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ * 
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+
+/* 
+ * Check whether "cp" is a valid ascii representation
+ * of an Internet address and convert to a binary address.
+ * Returns 1 if the address is valid, 0 if not.
+ * This replaces inet_addr, the return value from which
+ * cannot distinguish between failure and a local broadcast address.
+ */
+int inet_aton(const char *cp, struct in_addr *addr)
+{
+       u_int32_t val;
+       int base, n;
+       char c;
+       unsigned int parts[4];
+       unsigned int *pp = parts;
+
+       c = *cp;
+       for (;;) {
+               /*
+                * Collect number up to ``.''.
+                * Values are specified as for C:
+                * 0x=hex, 0=octal, isdigit=decimal.
+                */
+               if (!isdigit(c))
+                       return 0;
+               val = 0;
+               base = 10;
+               if (c == '0') {
+                       c = *++cp;
+                       if (c == 'x' || c == 'X')
+                               base = 16, c = *++cp;
+                       else
+                               base = 8;
+               }
+               for (;;) {
+                       if (isascii(c) && isdigit(c)) {
+                               val = (val * base) + (c - '0');
+                               c = *++cp;
+                       } else if (base == 16 && isascii(c) && isxdigit(c)) {
+                               val = (val << 4) |
+                                   (c + 10 - (islower(c) ? 'a' : 'A'));
+                               c = *++cp;
+                       } else
+                               break;
+               }
+               if (c == '.') {
+                       /*
+                        * Internet format:
+                        *      a.b.c.d
+                        *      a.b.c   (with c treated as 16 bits)
+                        *      a.b     (with b treated as 24 bits)
+                        */
+                       if (pp >= parts + 3)
+                               return (0);
+                       *pp++ = val;
+                       c = *++cp;
+               } else
+                       break;
+       }
+       /*
+        * Check for trailing characters.
+        */
+       if (c != '\0' && (!isascii(c) || !isspace(c)))
+               return 0;
+       /*
+        * Concoct the address according to
+        * the number of parts specified.
+        */
+       n = pp - parts + 1;
+       switch (n) {
+
+       case 0:
+               return (0);     /* initial nondigit */
+
+       case 1:         /* a -- 32 bits */
+               break;
+
+       case 2:         /* a.b -- 8.24 bits */
+               if ((val > 0xffffff) || (parts[0] > 0xff))
+                       return (0);
+               val |= parts[0] << 24;
+               break;
+
+       case 3:         /* a.b.c -- 8.8.16 bits */
+               if ((val > 0xffff) || (parts[0] > 0xff)
+                   || (parts[1] > 0xff))
+                       return (0);
+               val |= (parts[0] << 24) | (parts[1] << 16);
+               break;
+
+       case 4:         /* a.b.c.d -- 8.8.8.8 bits */
+               if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff)
+                   || (parts[2] > 0xff))
+                       return (0);
+               val |=
+                   (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+               break;
+       }
+       if (addr)
+               addr->s_addr = htonl(val);
+       return 1;
+}
diff --git a/Library/libs/inet_network.c b/Library/libs/inet_network.c
new file mode 100644 (file)
index 0000000..e2fff19
--- /dev/null
@@ -0,0 +1,83 @@
+/*     $OpenBSD: inet_network.c,v 1.10 2005/08/06 20:30:03 espie Exp $ */
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+
+/*
+ * Internet network address interpretation routine.
+ * The library routines call this routine to interpret
+ * network numbers.
+ */
+in_addr_t inet_network(const char *cp)
+{
+       in_addr_t val, base, n;
+       char c;
+       in_addr_t parts[4], *pp = parts;
+       int i;
+
+again:
+       val = 0; base = 10;
+       if (*cp == '0')
+               base = 8, cp++;
+       if (*cp == 'x' || *cp == 'X')
+               base = 16, cp++;
+       while ((c = *cp)) {
+               if (isdigit(c)) {
+                       val = (val * base) + (c - '0');
+                       cp++;
+                       continue;
+               }
+               if (base == 16 && isxdigit(c)) {
+                       val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
+                       cp++;
+                       continue;
+               }
+               break;
+       }
+       if (*cp == '.') {
+               if (pp >= parts + 3)
+                       return INADDR_NONE;
+               *pp++ = val, cp++;
+               goto again;
+       }
+       if (*cp && !isspace(*cp))
+               return INADDR_NONE;
+       *pp++ = val;
+       n = pp - parts;
+       for (val = 0, i = 0; i < 4; i++) {
+               val <<= 8;
+               if (i < n)
+                       val |= parts[i] & 0xff;
+       }
+       return val;
+}
diff --git a/Library/libs/inet_ntoa.c b/Library/libs/inet_ntoa.c
new file mode 100644 (file)
index 0000000..dc01fc2
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ *     Avoid the use of snprintf the way BSD does it
+ */
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+
+char *inet_ntoa(struct in_addr in)
+{
+  static char b[18];
+  uint8_t *p = (uint8_t)&in;
+  uint8_t *o = b;
+  
+  while(p != ((uint8_t *)&in) + 3)
+    strcpy(o, _itoa(*p++));
+    o += strlen(o);
+    *o++ = '.';
+  }
+  o[-1] = 0;
+  return b;
+}
+
+  
+  
\ No newline at end of file
diff --git a/Library/libs/inet_ntop.c b/Library/libs/inet_ntop.c
new file mode 100644 (file)
index 0000000..f9b7062
--- /dev/null
@@ -0,0 +1,20 @@
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arap/inet.h>
+#include <errno.h>
+
+const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
+{
+       if (af != AF_INET) {
+               errno = EAFNOSUPPORT;
+               return NULL;
+       }
+       if (size < 18) {
+               errno = ENOSPC;
+               return NULL;
+       }
+       /* This isn't strictly correct because it means we are not
+          re-entrant. Really we need to rework _itoa() to have a re-entrant
+          base form, then rework inet_ntoa to use inet_ntop FIXME */
+       return strcpy(dst, inet_ntoa(*(uint32_t *)src);
+}
diff --git a/Library/libs/inet_pton.c b/Library/libs/inet_pton.c
new file mode 100644 (file)
index 0000000..96b24da
--- /dev/null
@@ -0,0 +1,56 @@
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+
+/* Keep this as tight as possible. It's the routine people should be using
+   for address printing and it's not got all the legacy crap in it so can
+   be small and clean */
+
+/* Yes the error path for this is slower than it needs to be. If you are
+   optimzing for the error path you are doing something wrong. Instead it's
+   optimized for size and the normal path */
+
+static const char *quad(const char *p, uint8_t *r)
+{
+       uint16_t val = 0;
+       uint8_t valid = 0;
+
+       if (p == NULL)
+               return NULL;
+
+       while(*p >= '0' && *p <= '9') {
+               val = (val * 10) + *p++ - '0';
+               valid++;
+               if (valid > 3)
+                       return NULL;
+       }
+       if (val > 255)
+               return NULL;
+       *r = val;
+       if (*p == '.')
+               return p + 1;
+       if (*p == 0)
+               return p;
+       return NULL;
+}
+               
+int inet_pton(int af, const char *src, void *dst)
+{
+       uint8_t *p = dst;
+
+       if (af != AF_INET) {
+               errno = EAFNOSUPPORT;
+               return 0;
+       }
+
+       /* Unlike the legacy interfaces this one requires decimal and four
+          dotted quads */
+       src = quad(src, *p++);
+       src = quad(src, *p++);
+       src = quad(src, *p++);
+       src = quad(src, *p);
+       if (src == NULL || *src)
+               return 0;
+       return 1;
+}