From c981679bbf78df6e4686ce5f3b87ad95f7cf0fa3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joan=20Lled=C3=B3?= <jlledom@member.fsf.org>
Date: Sun, 21 May 2017 10:59:50 +0200
Subject: [PATCH] Sockets API: use OS's sys/socket.h instead of lwip/sock

---
 src/include/lwip/ip.h      |   6 ++
 src/include/lwip/sockets.h | 213 ++++++++++++++++++++++++++++-----------------
 2 files changed, 137 insertions(+), 82 deletions(-)

diff --git a/src/include/lwip/ip.h b/src/include/lwip/ip.h
index 3c89d5b..a04c931 100644
--- a/src/include/lwip/ip.h
+++ b/src/include/lwip/ip.h
@@ -96,9 +96,15 @@ struct ip_pcb {
 /*
  * Option flags per-socket. These are the same like SO_XXX in sockets.h
  */
+#ifndef LWIP_SYS_SOCKET
 #define SOF_REUSEADDR     0x04U  /* allow local address reuse */
 #define SOF_KEEPALIVE     0x08U  /* keep connections alive */
 #define SOF_BROADCAST     0x20U  /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
+#else
+#define SOF_REUSEADDR     SO_REUSEADDR  /* allow local address reuse */
+#define SOF_KEEPALIVE     SO_KEEPALIVE  /* keep connections alive */
+#define SOF_BROADCAST     SO_BROADCAST  /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
+#endif
 
 /* These flags are inherited (e.g. from a listen-pcb to a connection-pcb): */
 #define SOF_INHERITED   (SOF_REUSEADDR|SOF_KEEPALIVE)
diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
index 4f9913a..805b435 100644
--- a/src/include/lwip/sockets.h
+++ b/src/include/lwip/sockets.h
@@ -53,39 +53,19 @@
 extern "C" {
 #endif
 
+#ifndef LWIP_SYS_SOCKET
+
+/* If your port already typedef's socklen_t, define SOCKLEN_T_DEFINED
+   to prevent this code from redefining it. */
+#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
+typedef u32_t socklen_t;
+#endif
+
 /* If your port already typedef's sa_family_t, define SA_FAMILY_T_DEFINED
    to prevent this code from redefining it. */
 #if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
 typedef u8_t sa_family_t;
 #endif
-/* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED
-   to prevent this code from redefining it. */
-#if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
-typedef u16_t in_port_t;
-#endif
-
-#if LWIP_IPV4
-/* members are in network byte order */
-struct sockaddr_in {
-  u8_t            sin_len;
-  sa_family_t     sin_family;
-  in_port_t       sin_port;
-  struct in_addr  sin_addr;
-#define SIN_ZERO_LEN 8
-  char            sin_zero[SIN_ZERO_LEN];
-};
-#endif /* LWIP_IPV4 */
-
-#if LWIP_IPV6
-struct sockaddr_in6 {
-  u8_t            sin6_len;      /* length of this structure    */
-  sa_family_t     sin6_family;   /* AF_INET6                    */
-  in_port_t       sin6_port;     /* Transport layer port #      */
-  u32_t           sin6_flowinfo; /* IPv6 flow information       */
-  struct in6_addr sin6_addr;     /* IPv6 address                */
-  u32_t           sin6_scope_id; /* Set of interfaces for scope */
-};
-#endif /* LWIP_IPV6 */
 
 struct sockaddr {
   u8_t        sa_len;
@@ -103,18 +83,6 @@ struct sockaddr_storage {
 #endif /* LWIP_IPV6 */
 };
 
-/* If your port already typedef's socklen_t, define SOCKLEN_T_DEFINED
-   to prevent this code from redefining it. */
-#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
-typedef u32_t socklen_t;
-#endif
-
-#if !defined IOV_MAX
-#define IOV_MAX 0xFFFF
-#elif IOV_MAX > 0xFFFF
-#error "IOV_MAX larger than supported by LwIP"
-#endif /* IOV_MAX */
-
 #if !defined(iovec)
 struct iovec {
   void  *iov_base;
@@ -132,10 +100,6 @@ struct msghdr {
   int           msg_flags;
 };
 
-/* struct msghdr->msg_flags bit field values */
-#define MSG_TRUNC   0x04
-#define MSG_CTRUNC  0x08
-
 /* RFC 3542, Section 20: Ancillary Data */
 struct cmsghdr {
   socklen_t  cmsg_len;   /* number of bytes, including header */
@@ -170,16 +134,12 @@ will need to increase long long */
 #define CMSG_DATA(cmsg) ((void*)((u8_t *)(cmsg) + \
                          ALIGN_D(sizeof(struct cmsghdr))))
 
-#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
-                            ALIGN_H(length))
-
-#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
-                           length)
-
-/* Set socket options argument */
-#define IFNAMSIZ NETIF_NAMESIZE
-struct ifreq {
-  char ifr_name[IFNAMSIZ]; /* Interface name */
+/*
+ * Structure used for manipulating linger option.
+ */
+struct linger {
+  int l_onoff;                /* option on/off */
+  int l_linger;               /* linger time in seconds */
 };
 
 /* Socket protocol types (TCP/UDP/RAW) */
@@ -188,6 +148,11 @@ struct ifreq {
 #define SOCK_RAW        3
 
 /*
+ * Level number for (get/set)sockopt() to apply to socket itself.
+ */
+#define  SOL_SOCKET  0xfff    /* options for socket level */
+
+/*
  * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
  */
 #define SO_REUSEADDR   0x0004 /* Allow local address reuse */
@@ -201,11 +166,9 @@ struct ifreq {
 #define SO_DEBUG        0x0001 /* Unimplemented: turn on debugging info recording */
 #define SO_ACCEPTCONN   0x0002 /* socket has had listen() */
 #define SO_DONTROUTE    0x0010 /* Unimplemented: just use interface addresses */
-#define SO_USELOOPBACK  0x0040 /* Unimplemented: bypass hardware when possible */
+
 #define SO_LINGER       0x0080 /* linger on close if data present */
-#define SO_DONTLINGER   ((int)(~SO_LINGER))
 #define SO_OOBINLINE    0x0100 /* Unimplemented: leave received OOB data in line */
-#define SO_REUSEPORT    0x0200 /* Unimplemented: allow local address & port reuse */
 #define SO_SNDBUF       0x1001 /* Unimplemented: send buffer size */
 #define SO_RCVBUF       0x1002 /* receive buffer size */
 #define SO_SNDLOWAT     0x1003 /* Unimplemented: send low-water mark */
@@ -214,23 +177,14 @@ struct ifreq {
 #define SO_RCVTIMEO     0x1006 /* receive timeout */
 #define SO_ERROR        0x1007 /* get error status and clear */
 #define SO_TYPE         0x1008 /* get socket type */
-#define SO_CONTIMEO     0x1009 /* Unimplemented: connect timeout */
-#define SO_NO_CHECK     0x100a /* don't create UDP checksum */
-#define SO_BINDTODEVICE 0x100b /* bind to device */
-
-/*
- * Structure used for manipulating linger option.
- */
-struct linger {
-  int l_onoff;                /* option on/off */
-  int l_linger;               /* linger time in seconds */
-};
-
-/*
- * Level number for (get/set)sockopt() to apply to socket itself.
- */
-#define  SOL_SOCKET  0xfff    /* options for socket level */
 
+/* struct msghdr->msg_flags bit field values */
+#define MSG_TRUNC   0x04
+#define MSG_CTRUNC  0x08
+/* Flags we can use with send and recv. */
+#define MSG_PEEK       0x01    /* Peeks at an incoming message */
+#define MSG_WAITALL    0x02    /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
+#define MSG_OOB        0x04    /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
 
 #define AF_UNSPEC       0
 #define AF_INET         2
@@ -239,9 +193,99 @@ struct linger {
 #else /* LWIP_IPV6 */
 #define AF_INET6        AF_UNSPEC
 #endif /* LWIP_IPV6 */
+
+#ifndef SHUT_RD
+  #define SHUT_RD   0
+  #define SHUT_WR   1
+  #define SHUT_RDWR 2
+#endif
+
+#endif /* LWIP_SYS_SOCKET */
+
+
+/* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED
+   to prevent this code from redefining it. */
+#if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
+typedef u16_t in_port_t;
+#endif
+
+#if LWIP_IPV4
+/* members are in network byte order */
+struct sockaddr_in {
+  u8_t            sin_len;
+  sa_family_t     sin_family;
+  in_port_t       sin_port;
+  struct in_addr  sin_addr;
+#define SIN_ZERO_LEN 8
+  char            sin_zero[SIN_ZERO_LEN];
+};
+#endif /* LWIP_IPV4 */
+
+#if LWIP_IPV6
+struct sockaddr_in6 {
+  u8_t            sin6_len;      /* length of this structure    */
+  sa_family_t     sin6_family;   /* AF_INET6                    */
+  in_port_t       sin6_port;     /* Transport layer port #      */
+  u32_t           sin6_flowinfo; /* IPv6 flow information       */
+  struct in6_addr sin6_addr;     /* IPv6 address                */
+  u32_t           sin6_scope_id; /* Set of interfaces for scope */
+};
+#endif /* LWIP_IPV6 */
+
+#if !defined IOV_MAX
+#define IOV_MAX 0xFFFF
+#elif IOV_MAX > 0xFFFF
+#error "IOV_MAX larger than supported by LwIP"
+#endif /* IOV_MAX */
+
+#ifndef CMSG_SPACE
+#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
+                            ALIGN_H(length))
+#endif
+
+#ifndef CMSG_LEN
+#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
+                           length)
+#endif
+
+/* Set socket options argument */
+#define IFNAMSIZ NETIF_NAMESIZE
+struct ifreq {
+  char ifr_name[IFNAMSIZ]; /* Interface name */
+};
+
+
+/*
+ * Additional options, not kept in so_options.
+ */
+#ifndef SO_USELOOPBACK
+#define SO_USELOOPBACK  0x0040 /* Unimplemented: bypass hardware when possible */
+#endif
+#ifndef SO_DONTLINGER
+#define SO_DONTLINGER   ((int)(~SO_LINGER))
+#endif
+#ifndef SO_REUSEPORT
+#define SO_REUSEPORT    0x0200 /* Unimplemented: allow local address & port reuse */
+#endif
+#ifndef SO_CONTIMEO
+#define SO_CONTIMEO     0x1009 /* Unimplemented: connect timeout */
+#endif
+#ifndef SO_NO_CHECK
+#define SO_NO_CHECK     0x100a /* don't create UDP checksum */
+#endif
+#ifndef SO_BINDTODEVICE
+#define SO_BINDTODEVICE 0x100b /* bind to device */
+#endif
+
+#ifndef PF_INET
 #define PF_INET         AF_INET
+#endif
+#ifndef PF_INET6
 #define PF_INET6        AF_INET6
+#endif
+#ifndef PF_UNSPEC
 #define PF_UNSPEC       AF_UNSPEC
+#endif
 
 #define IPPROTO_IP      0
 #define IPPROTO_ICMP    1
@@ -255,12 +299,15 @@ struct linger {
 #define IPPROTO_RAW     255
 
 /* Flags we can use with send and recv. */
-#define MSG_PEEK       0x01    /* Peeks at an incoming message */
-#define MSG_WAITALL    0x02    /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
-#define MSG_OOB        0x04    /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
+#ifndef MSG_DONTWAIT
 #define MSG_DONTWAIT   0x08    /* Nonblocking i/o for this operation only */
+#endif
+#ifndef MSG_MORE
 #define MSG_MORE       0x10    /* Sender will send more */
+#endif
+#ifndef MSG_NOSIGNAL
 #define MSG_NOSIGNAL   0x20    /* Uninmplemented: Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. */
+#endif
 
 
 /*
@@ -407,9 +454,17 @@ struct in_pktinfo {
 /* Socket I/O Controls: unimplemented */
 #ifndef SIOCSHIWAT
 #define SIOCSHIWAT  _IOW('s',  0, unsigned long)  /* set high watermark */
+#endif
+#ifndef SIOCGHIWAT
 #define SIOCGHIWAT  _IOR('s',  1, unsigned long)  /* get high watermark */
+#endif
+#ifndef SIOCSLOWAT
 #define SIOCSLOWAT  _IOW('s',  2, unsigned long)  /* set low watermark */
+#endif
+#ifndef SIOCGLOWAT
 #define SIOCGLOWAT  _IOR('s',  3, unsigned long)  /* get low watermark */
+#endif
+#ifndef SIOCATMARK
 #define SIOCATMARK  _IOR('s',  7, unsigned long)  /* at oob mark? */
 #endif
 
@@ -439,12 +494,6 @@ struct in_pktinfo {
 #define O_RDWR      (O_RDONLY|O_WRONLY)
 #endif
 
-#ifndef SHUT_RD
-  #define SHUT_RD   0
-  #define SHUT_WR   1
-  #define SHUT_RDWR 2
-#endif
-
 /* FD_SET used for lwip_select */
 #ifndef FD_SET
 #undef  FD_SETSIZE
-- 
2.1.4

