build: Don't include <config.h> in native programs when cross-compiling.
[bpt/guile.git] / libguile / socket.c
index 3a81ed9..5b17a74 100644 (file)
@@ -1,5 +1,6 @@
-/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
- * 
+/* Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
+ *   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
  * as published by the Free Software Foundation; either version 3 of
 #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"
-
-#include "libguile/iselect.h"
-
-#ifdef __MINGW32__
-#include "win32-socket.h"
-#endif
+#include <verify.h>
 
 #ifdef HAVE_STDINT_H
 #include <stdint.h>
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
-#ifdef HAVE_UNISTD_H
 #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) \
+#define SUN_LEN(ptr) (offsetof (struct sockaddr_un, sun_path) \
                      + strlen ((ptr)->sun_path))
 #endif
 
@@ -93,97 +90,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
-
-#ifndef HAVE_INET_ATON
-/* for our definition in inet_aton.c, not usually needed.  */
-extern int inet_aton ();
-#endif
-
-SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0, 
-            (SCM address),
-           "Convert an IPv4 Internet address from printable string\n"
-           "(dotted decimal notation) to an integer.  E.g.,\n\n"
-           "@lisp\n"
-           "(inet-aton \"127.0.0.1\") @result{} 2130706433\n"
-           "@end lisp")
-#define FUNC_NAME s_scm_inet_aton
-{
-  struct in_addr soka;
-  char *c_address;
-  int rv;
-
-  c_address = scm_to_locale_string (address);
-  rv = inet_aton (c_address, &soka);
-  free (c_address);
-  if (rv == 0)
-    SCM_MISC_ERROR ("bad address", SCM_EOL);
-  return scm_from_ulong (ntohl (soka.s_addr));
-}
-#undef FUNC_NAME
-
-
-SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0, 
-            (SCM inetid),
-           "Convert an IPv4 Internet address to a printable\n"
-           "(dotted decimal notation) string.  E.g.,\n\n"
-           "@lisp\n"
-           "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n"
-           "@end lisp")
-#define FUNC_NAME s_scm_inet_ntoa
-{
-  struct in_addr addr;
-  char *s;
-  SCM answer;
-  addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid));
-  s = inet_ntoa (addr);
-  answer = scm_from_locale_string (s);
-  return answer;
-}
-#undef FUNC_NAME
-
 #ifdef HAVE_INET_NETOF
 SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0, 
             (SCM address),
@@ -349,48 +255,13 @@ scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
       scm_remember_upto_here_1 (src);
     }
   else
-    scm_wrong_type_arg (NULL, 0, src);
+    scm_wrong_type_arg_msg ("scm_to_ipv6", 0, src, "integer");
 }
 
-#ifdef HAVE_INET_PTON
-SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
-            (SCM family, SCM address),
-           "Convert a string containing a printable network address to\n"
-           "an integer address.  Note that unlike the C version of this\n"
-           "function,\n"
-           "the result is an integer with normal host byte ordering.\n"
-           "@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,\n\n"
-           "@lisp\n"
-           "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n"
-           "(inet-pton AF_INET6 \"::1\") @result{} 1\n"
-           "@end lisp")
-#define FUNC_NAME s_scm_inet_pton
-{
-  int af;
-  char *src;
-  scm_t_uint32 dst[4];
-  int rv, eno;
+#endif  /* HAVE_IPV6 */
 
-  af = scm_to_int (family);
-  SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
-  src = scm_to_locale_string (address);
-  rv = inet_pton (af, src, dst);
-  eno = errno;
-  free (src);
-  errno = eno;
-  if (rv == -1)
-    SCM_SYSERROR;
-  else if (rv == 0)
-    SCM_MISC_ERROR ("Bad address", SCM_EOL);
-  if (af == AF_INET)
-    return scm_from_ulong (ntohl (*dst));
-  else
-    return scm_from_ipv6 ((scm_t_uint8 *) dst);
-}
-#undef FUNC_NAME
-#endif
+\f
 
-#ifdef HAVE_INET_NTOP
 SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
             (SCM family, SCM address),
            "Convert a network address into a printable string.\n"
@@ -399,8 +270,8 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
            "@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,\n\n"
            "@lisp\n"
            "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n"
-           "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n"
-           "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"
+           "(inet-ntop AF_INET6 (- (expt 2 128) 1))\n"
+           "  @result{} \"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"\n"
            "@end lisp")
 #define FUNC_NAME s_scm_inet_ntop
 {
@@ -413,7 +284,12 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
   const char *result;
 
   af = scm_to_int (family);
-  SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
+  SCM_ASSERT_RANGE (1, family,
+                   af == AF_INET
+#ifdef HAVE_IPV6
+                   || af == AF_INET6
+#endif
+                   );
   if (af == AF_INET)
     {
       scm_t_uint32 addr4;
@@ -421,13 +297,17 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
       addr4 = htonl (SCM_NUM2ULONG (2, address));
       result = inet_ntop (af, &addr4, dst, sizeof (dst));
     }
-  else
+#ifdef HAVE_IPV6
+  else if (af == AF_INET6)
     {
       char addr6[16];
 
       scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
       result = inet_ntop (af, &addr6, dst, sizeof (dst));
     }
+#endif
+  else
+    SCM_MISC_ERROR ("unsupported address family", family);
 
   if (result == NULL)
     SCM_SYSERROR;
@@ -435,10 +315,55 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
   return scm_from_locale_string (dst);
 }
 #undef FUNC_NAME
+
+SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
+            (SCM family, SCM address),
+           "Convert a string containing a printable network address to\n"
+           "an integer address.  Note that unlike the C version of this\n"
+           "function,\n"
+           "the result is an integer with normal host byte ordering.\n"
+           "@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,\n\n"
+           "@lisp\n"
+           "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n"
+           "(inet-pton AF_INET6 \"::1\") @result{} 1\n"
+           "@end lisp")
+#define FUNC_NAME s_scm_inet_pton
+{
+  int af;
+  char *src;
+  scm_t_uint32 dst[4];
+  int rv, eno;
+
+  af = scm_to_int (family);
+  SCM_ASSERT_RANGE (1, family,
+                   af == AF_INET
+#ifdef HAVE_IPV6
+                   || af == AF_INET6
 #endif
+                   );
 
-#endif  /* HAVE_IPV6 */
+  src = scm_to_locale_string (address);
+  rv = inet_pton (af, src, dst);
+  eno = errno;
+  free (src);
+  errno = eno;
+
+  if (rv == -1)
+    SCM_SYSERROR;
+  else if (rv == 0)
+    SCM_MISC_ERROR ("Bad address", SCM_EOL);
+  if (af == AF_INET)
+    return scm_from_ulong (ntohl (*dst));
+#ifdef HAVE_IPV6
+  else if (af == AF_INET6)
+    return scm_from_ipv6 ((scm_t_uint8 *) dst);
+#endif
+  else
+    SCM_MISC_ERROR ("unsupported address family", family);
+}
+#undef FUNC_NAME
 
+\f
 SCM_SYMBOL (sym_socket, "socket");
 
 #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
@@ -539,14 +464,15 @@ 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"
            "@defvar SO_LINGER\n"
-           "The @var{value} returned is a pair of integers\n"
-           "@code{(@var{ENABLE} . @var{TIMEOUT})}.  On old systems without\n"
+           "The value returned is a pair of integers\n"
+           "@code{(@var{enable} . @var{timeout})}.  On old systems without\n"
            "timeout support (ie.@: without @code{struct linger}), only\n"
-           "@var{ENABLE} has an effect but the value in Guile is always a\n"
+           "@var{enable} has an effect but the value in Guile is always a\n"
            "pair.\n"
            "@end defvar")
 #define FUNC_NAME s_scm_getsockopt
@@ -637,6 +563,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"
@@ -654,6 +581,16 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
            "are defined (when provided by the system).  See @command{man\n"
            "ip} for what they mean.\n"
            "\n"
+           "@defvar IP_MULTICAST_IF\n"
+            "This sets the source interface used by multicast traffic.\n"
+           "@end defvar\n"
+           "\n"
+           "@defvar IP_MULTICAST_TTL\n"
+            "This sets the default TTL for multicast traffic. This defaults \n"
+            "to 1 and should be increased to allow traffic to pass beyond the\n"
+            "local network.\n"
+           "@end defvar\n"
+           "\n"
            "@defvar IP_ADD_MEMBERSHIP\n"
            "@defvarx IP_DROP_MEMBERSHIP\n"
            "These can be used only with @code{setsockopt}, not\n"
@@ -675,7 +612,7 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
   struct linger opt_linger;
 #endif
 
-#if HAVE_STRUCT_IP_MREQ
+#ifdef HAVE_STRUCT_IP_MREQ
   struct ip_mreq opt_mreq;
 #endif
 
@@ -729,7 +666,7 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
          }
     }
 
-#if HAVE_STRUCT_IP_MREQ
+#ifdef HAVE_STRUCT_IP_MREQ
   if (ilevel == IPPROTO_IP &&
       (ioptname == IP_ADD_MEMBERSHIP || ioptname == IP_DROP_MEMBERSHIP))
     {
@@ -756,6 +693,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"
@@ -810,8 +752,9 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
        port = scm_to_int (SCM_CAR (*args));
        *args = SCM_CDR (*args);
        soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in));
+        memset (soka, '\0', sizeof (struct sockaddr_in));
 
-#if HAVE_STRUCT_SOCKADDR_SIN_LEN
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
        soka->sin_len = sizeof (struct sockaddr_in);
 #endif
        soka->sin_family = AF_INET;
@@ -845,7 +788,7 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
          }
        soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));
 
-#if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
+#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
        soka->sin6_len = sizeof (struct sockaddr_in6);
 #endif
        soka->sin6_family = AF_INET6;
@@ -927,7 +870,7 @@ SCM_DEFINE (scm_connect, "connect", 2, 1, 1,
   SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
 
-  if (address == SCM_UNDEFINED)
+  if (scm_is_eq (address, SCM_UNDEFINED))
     /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
        `socket address' object.  */
     soka = scm_to_sockaddr (fam_or_sockaddr, &size);
@@ -996,7 +939,7 @@ SCM_DEFINE (scm_bind, "bind", 2, 1, 1,
   SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
 
-  if (address == SCM_UNDEFINED)
+  if (scm_is_eq (address, SCM_UNDEFINED))
     /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
        `socket address' object.  */
     soka = scm_to_sockaddr (fam_or_sockaddr, &size);
@@ -1145,6 +1088,12 @@ scm_to_sockaddr (SCM address, size_t *address_size)
          {
            struct sockaddr_in c_inet;
 
+            memset (&c_inet, '\0', sizeof (struct sockaddr_in));
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
+            c_inet.sin_len = sizeof (struct sockaddr_in);
+#endif
+
            c_inet.sin_addr.s_addr =
              htonl (scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 1)));
            c_inet.sin_port =
@@ -1169,7 +1118,8 @@ scm_to_sockaddr (SCM address, size_t *address_size)
          {
            struct sockaddr_in6 c_inet6;
 
-           scm_to_ipv6 (c_inet6.sin6_addr.s6_addr, address);
+           scm_to_ipv6 (c_inet6.sin6_addr.s6_addr,
+                        SCM_SIMPLE_VECTOR_REF (address, 1));
            c_inet6.sin6_port =
              htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
            c_inet6.sin6_flowinfo =
@@ -1202,14 +1152,14 @@ scm_to_sockaddr (SCM address, size_t *address_size)
            size_t path_len = 0;
 
            path = SCM_SIMPLE_VECTOR_REF (address, 1);
-           if ((!scm_is_string (path)) && (path != SCM_BOOL_F))
+           if (!scm_is_string (path) && !scm_is_false (path))
              scm_misc_error (FUNC_NAME, "invalid unix address "
                              "path: ~A", scm_list_1 (path));
            else
              {
                struct sockaddr_un c_unix;
 
-               if (path == SCM_BOOL_F)
+               if (scm_is_false (path))
                  path_len = 0;
                else
                  path_len = scm_c_string_length (path);
@@ -1325,31 +1275,17 @@ SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
            "connection and will continue to accept new requests.")
 #define FUNC_NAME s_scm_accept
 {
-  int fd, selected;
+  int fd;
   int newfd;
   SCM address;
   SCM newsock;
-  SELECT_TYPE readfds, exceptfds;
   socklen_t addr_size = MAX_ADDR_SIZE;
   scm_t_max_sockaddr addr;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
-
-  FD_ZERO (&readfds);
-  FD_ZERO (&exceptfds);
-  FD_SET (fd, &readfds);
-  FD_SET (fd, &exceptfds);
-
-  /* Block until something happens on FD, leaving guile mode while
-     waiting.  */
-  selected = scm_std_select (fd + 1, &readfds, NULL, &exceptfds,
-                            NULL);
-  if (selected < 0)
-    SCM_SYSERROR;
-
-  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);
@@ -1408,15 +1344,13 @@ SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
            "Receive data from a socket port.\n"
            "@var{sock} must already\n"
            "be bound to the address from which data is to be received.\n"
-           "@var{buf} is a string into which\n"
+           "@var{buf} is a bytevector into which\n"
            "the data will be written.  The size of @var{buf} limits\n"
            "the amount of\n"
            "data which can be received: in the case of packet\n"
            "protocols, if a packet larger than this limit is encountered\n"
            "then some data\n"
            "will be irrevocably lost.\n\n"
-           "The data is assumed to be binary, and there is no decoding of\n"
-           "of locale-encoded strings.\n\n"
            "The optional @var{flags} argument is a value or\n"
            "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
            "The value returned is the number of bytes read from the\n"
@@ -1426,38 +1360,55 @@ SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
            "any unread buffered port data is ignored.")
 #define FUNC_NAME s_scm_recv
 {
-  int rv;
-  int fd;
-  int flg;
-  char *dest;
-  size_t len;
-  SCM msg;
+  int rv, fd, flg;
 
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_STRING (2, buf);
+
   if (SCM_UNBNDP (flags))
     flg = 0;
   else
     flg = scm_to_int (flags);
   fd = SCM_FPORT_FDES (sock);
 
-  len = scm_i_string_length (buf);
-  msg = scm_i_make_string (len, &dest);
-  SCM_SYSCALL (rv = recv (fd, dest, len, flg));
-  scm_string_copy_x (buf, scm_from_int (0), 
-                    msg, scm_from_int (0), scm_from_size_t (len));
+#if SCM_ENABLE_DEPRECATED == 1
+  if (SCM_UNLIKELY (scm_is_string (buf)))
+    {
+      SCM msg;
+      char *dest;
+      size_t len;
+
+      scm_c_issue_deprecation_warning
+       ("Passing a string to `recv!' is deprecated, "
+        "use a bytevector instead.");
+
+      len = scm_i_string_length (buf);
+      msg = scm_i_make_string (len, &dest, 0);
+      SCM_SYSCALL (rv = recv (fd, dest, len, flg));
+      scm_string_copy_x (buf, scm_from_int (0),
+                        msg, scm_from_int (0), scm_from_size_t (len));
+    }
+  else
+#endif
+    {
+      SCM_VALIDATE_BYTEVECTOR (1, buf);
 
-  if (rv == -1)
+      SCM_SYSCALL (rv = recv (fd,
+                             SCM_BYTEVECTOR_CONTENTS (buf),
+                             SCM_BYTEVECTOR_LENGTH (buf),
+                             flg));
+    }
+
+  if (SCM_UNLIKELY (rv == -1))
     SCM_SYSERROR;
 
-  scm_remember_upto_here_2 (buf, msg);
+  scm_remember_upto_here (buf);
   return scm_from_int (rv);
 }
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_send, "send", 2, 1, 0,
             (SCM sock, SCM message, SCM flags),
-           "Transmit the string @var{message} on a socket port @var{sock}.\n"
+           "Transmit bytevector @var{message} on socket port @var{sock}.\n"
            "@var{sock} must already be bound to a destination address.  The\n"
            "value returned is the number of bytes transmitted --\n"
            "it's possible for\n"
@@ -1473,34 +1424,47 @@ SCM_DEFINE (scm_send, "send", 2, 1, 0,
            "zero to 255.")
 #define FUNC_NAME s_scm_send
 {
-  int rv;
-  int fd;
-  int flg;
-  char *src;
-  size_t len;
+  int rv, fd, flg;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_STRING (2, message);
-  
-  /* If the string is wide, see if it can be coerced into
-     a narrow string.  */
-  if (!scm_i_is_narrow_string (message)
-      || scm_i_try_narrow_string (message))
-    SCM_MISC_ERROR ("the message string is not 8-bit: ~s", 
-                        scm_list_1 (message));
 
   if (SCM_UNBNDP (flags))
     flg = 0;
   else
     flg = scm_to_int (flags);
+
   fd = SCM_FPORT_FDES (sock);
 
-  len = scm_i_string_length (message);
-  message = scm_i_string_start_writing (message);
-  src = scm_i_string_writable_chars (message);
-  SCM_SYSCALL (rv = send (fd, src, len, flg));
-  scm_i_string_stop_writing ();
+#if SCM_ENABLE_DEPRECATED == 1
+  if (SCM_UNLIKELY (scm_is_string (message)))
+    {
+      scm_c_issue_deprecation_warning
+       ("Passing a string to `send' is deprecated, "
+        "use a bytevector instead.");
+
+      /* If the string is wide, see if it can be coerced into a narrow
+        string.  */
+      if (!scm_i_is_narrow_string (message)
+         || !scm_i_try_narrow_string (message))
+       SCM_MISC_ERROR ("the message string is not 8-bit: ~s",
+                        scm_list_1 (message));
+
+      SCM_SYSCALL (rv = send (fd,
+                             scm_i_string_chars (message),
+                             scm_i_string_length (message),
+                             flg));
+    }
+  else
+#endif
+    {
+      SCM_VALIDATE_BYTEVECTOR (1, message);
+
+      SCM_SYSCALL (rv = send (fd,
+                             SCM_BYTEVECTOR_CONTENTS (message),
+                             SCM_BYTEVECTOR_LENGTH (message),
+                             flg));
+    }
 
   if (rv == -1)
     SCM_SYSERROR;
@@ -1511,22 +1475,22 @@ SCM_DEFINE (scm_send, "send", 2, 1, 0,
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
-            (SCM sock, SCM str, SCM flags, SCM start, SCM end),
+            (SCM sock, SCM buf, SCM flags, SCM start, SCM end),
            "Receive data from socket port @var{sock} (which must be already\n"
            "bound), returning the originating address as well as the data.\n"
            "This is usually for use on datagram sockets, but can be used on\n"
            "stream-oriented sockets too.\n"
            "\n"
-           "The data received is stored in the given @var{str}, using\n"
-           "either the whole string or just the region between the optional\n"
-           "@var{start} and @var{end} positions.  The size of @var{str}\n"
-           "limits the amount of data which can be received.  For datagram\n"
+           "The data received is stored in bytevector @var{buf}, using\n"
+           "either the whole bytevector or just the region between the optional\n"
+           "@var{start} and @var{end} positions.  The size of @var{buf}\n"
+           "limits the amount of data that can be received.  For datagram\n"
            "protocols, if a packet larger than this is received then excess\n"
            "bytes are irrevocably lost.\n"
            "\n"
            "The return value is a pair.  The @code{car} is the number of\n"
            "bytes read.  The @code{cdr} is a socket address object which is\n"
-           "where the data come from, or @code{#f} if the origin is\n"
+           "where the data came from, or @code{#f} if the origin is\n"
            "unknown.\n"
            "\n"
            "The optional @var{flags} argument is a or bitwise OR\n"
@@ -1542,46 +1506,79 @@ SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
            "or @code{MSG_DONTWAIT} to avoid this.")
 #define FUNC_NAME s_scm_recvfrom
 {
-  int rv;
-  int fd;
-  int flg;
-  char *buf;
-  size_t offset;
-  size_t cend;
+  int rv, fd, flg;
   SCM address;
+  size_t offset, cend;
   socklen_t addr_size = MAX_ADDR_SIZE;
   scm_t_max_sockaddr addr;
 
   SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
-  
-  SCM_VALIDATE_STRING (2, str);
-  scm_i_get_substring_spec (scm_i_string_length (str),
-                           start, &offset, end, &cend);
 
   if (SCM_UNBNDP (flags))
     flg = 0;
   else
     SCM_VALIDATE_ULONG_COPY (3, flags, flg);
 
-  /* recvfrom will not necessarily return an address.  usually nothing
-     is returned for stream sockets.  */
-  str = scm_i_string_start_writing (str);
-  buf = scm_i_string_writable_chars (str);
   ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
-  SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
-                             cend - offset, flg,
-                             (struct sockaddr *) &addr, &addr_size));
-  scm_i_string_stop_writing ();
+
+#if SCM_ENABLE_DEPRECATED == 1
+  if (SCM_UNLIKELY (scm_is_string (buf)))
+    {
+      char *cbuf;
+
+      scm_c_issue_deprecation_warning
+       ("Passing a string to `recvfrom!' is deprecated, "
+        "use a bytevector instead.");
+
+      scm_i_get_substring_spec (scm_i_string_length (buf),
+                               start, &offset, end, &cend);
+
+      buf = scm_i_string_start_writing (buf);
+      cbuf = scm_i_string_writable_chars (buf);
+
+      SCM_SYSCALL (rv = recvfrom (fd, cbuf + offset,
+                                 cend - offset, flg,
+                                 (struct sockaddr *) &addr, &addr_size));
+      scm_i_string_stop_writing ();
+    }
+  else
+#endif
+    {
+      SCM_VALIDATE_BYTEVECTOR (1, buf);
+
+      if (SCM_UNBNDP (start))
+       offset = 0;
+      else
+       offset = scm_to_size_t (start);
+
+      if (SCM_UNBNDP (end))
+       cend = SCM_BYTEVECTOR_LENGTH (buf);
+      else
+       {
+         cend = scm_to_size_t (end);
+         if (SCM_UNLIKELY (cend >= SCM_BYTEVECTOR_LENGTH (buf)
+                           || cend < offset))
+           scm_out_of_range (FUNC_NAME, end);
+       }
+
+      SCM_SYSCALL (rv = recvfrom (fd,
+                                 SCM_BYTEVECTOR_CONTENTS (buf) + offset,
+                                 cend - offset, flg,
+                                 (struct sockaddr *) &addr, &addr_size));
+    }
 
   if (rv == -1)
     SCM_SYSERROR;
+
+  /* `recvfrom' does not necessarily return an address.  Usually nothing
+     is returned for stream sockets.  */
   if (((struct sockaddr *) &addr)->sa_family != AF_UNSPEC)
     address = _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
   else
     address = SCM_BOOL_F;
 
-  scm_remember_upto_here_1 (str);
+  scm_remember_upto_here_1 (buf);
 
   return scm_cons (scm_from_int (rv), address);
 }
@@ -1589,9 +1586,9 @@ SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
 
 SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
             (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SCM args_and_flags),
-           "Transmit the string @var{message} on the socket port\n"
+           "Transmit bytevector @var{message} on socket port\n"
            "@var{sock}.  The\n"
-           "destination address is specified using the @var{fam},\n"
+           "destination address is specified using the @var{fam_or_sockaddr},\n"
            "@var{address} and\n"
            "@var{args_and_flags} arguments, or just a socket address object "
            "returned by @code{make-socket-address}, in a similar way to the\n"
@@ -1611,15 +1608,12 @@ SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
            "zero to 255.")
 #define FUNC_NAME s_scm_sendto
 {
-  int rv;
-  int fd;
-  int flg;
+  int rv, fd, flg;
   struct sockaddr *soka;
   size_t size;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_FPORT (1, sock);
-  SCM_VALIDATE_STRING (2, message);
   fd = SCM_FPORT_FDES (sock);
 
   if (!scm_is_number (fam_or_sockaddr))
@@ -1628,7 +1622,7 @@ SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
         means that the following arguments, i.e. ADDRESS and those listed in
         ARGS_AND_FLAGS, are the `MSG_' flags.  */
       soka = scm_to_sockaddr (fam_or_sockaddr, &size);
-      if (address != SCM_UNDEFINED)
+      if (!scm_is_eq (address, SCM_UNDEFINED))
        args_and_flags = scm_cons (address, args_and_flags);
     }
   else
@@ -1642,10 +1636,37 @@ SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
       SCM_VALIDATE_CONS (5, args_and_flags);
       flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
     }
-  SCM_SYSCALL (rv = sendto (fd,
-                           scm_i_string_chars (message),
-                           scm_i_string_length (message),
-                           flg, soka, size));
+
+#if SCM_ENABLE_DEPRECATED == 1
+  if (SCM_UNLIKELY (scm_is_string (message)))
+    {
+      scm_c_issue_deprecation_warning
+       ("Passing a string to `sendto' is deprecated, "
+        "use a bytevector instead.");
+
+      /* If the string is wide, see if it can be coerced into a narrow
+        string.  */
+      if (!scm_i_is_narrow_string (message)
+         || !scm_i_try_narrow_string (message))
+       SCM_MISC_ERROR ("the message string is not 8-bit: ~s",
+                        scm_list_1 (message));
+
+      SCM_SYSCALL (rv = sendto (fd,
+                               scm_i_string_chars (message),
+                               scm_i_string_length (message),
+                               flg, soka, size));
+    }
+  else
+#endif
+    {
+      SCM_VALIDATE_BYTEVECTOR (1, message);
+
+      SCM_SYSCALL (rv = sendto (fd,
+                               SCM_BYTEVECTOR_CONTENTS (message),
+                               SCM_BYTEVECTOR_LENGTH (message),
+                               flg, soka, size));
+    }
+
   if (rv == -1)
     {
       int save_errno = errno;
@@ -1669,7 +1690,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
@@ -1791,6 +1812,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
@@ -1806,15 +1830,19 @@ 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));
 #endif
 
+#ifdef IP_MULTICAST_TTL 
+  scm_c_define ("IP_MULTICAST_TTL", scm_from_int ( IP_MULTICAST_TTL));
+#endif
+
+#ifdef IP_MULTICAST_IF 
+  scm_c_define ("IP_MULTICAST_IF", scm_from_int ( IP_MULTICAST_IF));
+#endif
+
   scm_add_feature ("socket");
 
 #include "libguile/socket.x"