Merge commit 'b9b88351ea2c4b43a6f90938554dc5693b17e328'
[bpt/guile.git] / libguile / socket.c
index 149ec00..0516e52 100644 (file)
@@ -1,5 +1,5 @@
 /* Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
- *   2006, 2007, 2009, 2011 Free Software Foundation, Inc.
+ *   2006, 2007, 2009, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
 #endif
 
 #include <errno.h>
-#include <gmp.h>
-
-#include "libguile/_scm.h"
-#include "libguile/arrays.h"
-#include "libguile/feature.h"
-#include "libguile/fports.h"
-#include "libguile/strings.h"
-#include "libguile/vectors.h"
-#include "libguile/dynwind.h"
-#include "libguile/srfi-13.h"
-
-#include "libguile/validate.h"
-#include "libguile/socket.h"
-
-#ifdef __MINGW32__
-#include "win32-socket.h"
-#include <netdb.h>
-#endif
+#include <verify.h>
 
 #ifdef HAVE_STDINT_H
 #include <stdint.h>
@@ -54,9 +37,6 @@
 #include <unistd.h>
 #endif
 #include <sys/types.h>
-#ifdef HAVE_WINSOCK2_H
-#include <winsock2.h>
-#else
 #include <sys/socket.h>
 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <netdb.h>
 #include <arpa/inet.h>
+
+#include <gmp.h>
+
+#include "libguile/_scm.h"
+#include "libguile/arrays.h"
+#include "libguile/feature.h"
+#include "libguile/fports.h"
+#include "libguile/strings.h"
+#include "libguile/vectors.h"
+#include "libguile/dynwind.h"
+#include "libguile/srfi-13.h"
+
+#include "libguile/validate.h"
+#include "libguile/socket.h"
+
+#if SCM_ENABLE_DEPRECATED == 1
+# include "libguile/deprecation.h"
 #endif
 
+
+
 #if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
                      + strlen ((ptr)->sun_path))
@@ -93,50 +92,6 @@ typedef union
 
 \f
 
-SCM_DEFINE (scm_htons, "htons", 1, 0, 0, 
-            (SCM value),
-           "Convert a 16 bit quantity from host to network byte ordering.\n"
-           "@var{value} is packed into 2 bytes, which are then converted\n"
-           "and returned as a new integer.")
-#define FUNC_NAME s_scm_htons
-{
-  return scm_from_ushort (htons (scm_to_ushort (value)));
-}
-#undef FUNC_NAME
-
-SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0, 
-            (SCM value),
-           "Convert a 16 bit quantity from network to host byte ordering.\n"
-           "@var{value} is packed into 2 bytes, which are then converted\n"
-           "and returned as a new integer.")
-#define FUNC_NAME s_scm_ntohs
-{
-  return scm_from_ushort (ntohs (scm_to_ushort (value)));
-}
-#undef FUNC_NAME
-
-SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0, 
-            (SCM value),
-           "Convert a 32 bit quantity from host to network byte ordering.\n"
-           "@var{value} is packed into 4 bytes, which are then converted\n"
-           "and returned as a new integer.")
-#define FUNC_NAME s_scm_htonl
-{
-  return scm_from_ulong (htonl (scm_to_uint32 (value)));
-}
-#undef FUNC_NAME
-
-SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0, 
-            (SCM value),
-           "Convert a 32 bit quantity from network to host byte ordering.\n"
-           "@var{value} is packed into 4 bytes, which are then converted\n"
-           "and returned as a new integer.")
-#define FUNC_NAME s_scm_ntohl
-{
-  return scm_from_ulong (ntohl (scm_to_uint32 (value)));
-}
-#undef FUNC_NAME
-
 #ifdef HAVE_INET_NETOF
 SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0, 
             (SCM address),
@@ -511,6 +466,7 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
            "@defvarx SO_OOBINLINE\n"
            "@defvarx SO_NO_CHECK\n"
            "@defvarx SO_PRIORITY\n"
+           "@defvarx SO_REUSEPORT\n"
            "The value returned is an integer.\n"
            "@end defvar\n"
            "\n"
@@ -609,6 +565,7 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
            "@defvarx SO_OOBINLINE\n"
            "@defvarx SO_NO_CHECK\n"
            "@defvarx SO_PRIORITY\n"
+           "@defvarx SO_REUSEPORT\n"
            "@var{value} is an integer.\n"
            "@end defvar\n"
            "\n"
@@ -738,6 +695,11 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
 }
 #undef FUNC_NAME
 
+/* Our documentation hard-codes this mapping, so make sure it holds.  */
+verify (SHUT_RD == 0);
+verify (SHUT_WR == 1);
+verify (SHUT_RDWR == 2);
+
 SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
           (SCM sock, SCM how),
            "Sockets can be closed simply by using @code{close-port}. The\n"
@@ -1325,7 +1287,7 @@ SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
-  newfd = accept (fd, (struct sockaddr *) &addr, &addr_size);
+  SCM_SYSCALL (newfd = accept (fd, (struct sockaddr *) &addr, &addr_size));
   if (newfd == -1)
     SCM_SYSERROR;
   newsock = SCM_SOCK_FD_TO_PORT (newfd);
@@ -1639,7 +1601,7 @@ scm_init_socket ()
 #ifdef AF_UNSPEC
   scm_c_define ("AF_UNSPEC", scm_from_int (AF_UNSPEC));
 #endif
-#ifdef AF_UNIX
+#if defined HAVE_UNIX_DOMAIN_SOCKETS && defined AF_UNIX
   scm_c_define ("AF_UNIX", scm_from_int (AF_UNIX));
 #endif
 #ifdef AF_INET
@@ -1761,6 +1723,9 @@ scm_init_socket ()
 #ifdef SO_LINGER
   scm_c_define ("SO_LINGER", scm_from_int (SO_LINGER));
 #endif
+#ifdef SO_REUSEPORT                              /* new in Linux 3.9 */
+  scm_c_define ("SO_REUSEPORT", scm_from_int (SO_REUSEPORT));
+#endif
 
   /* recv/send options.  */
 #ifdef MSG_DONTWAIT
@@ -1776,10 +1741,6 @@ scm_init_socket ()
   scm_c_define ("MSG_DONTROUTE", scm_from_int (MSG_DONTROUTE));
 #endif
 
-#ifdef __MINGW32__
-  scm_i_init_socket_Win32 ();
-#endif
-
 #ifdef IP_ADD_MEMBERSHIP
   scm_c_define ("IP_ADD_MEMBERSHIP", scm_from_int (IP_ADD_MEMBERSHIP));
   scm_c_define ("IP_DROP_MEMBERSHIP", scm_from_int (IP_DROP_MEMBERSHIP));