build: Don't include <config.h> in native programs when cross-compiling.
[bpt/guile.git] / libguile / socket.c
index d0cc0f8..5b17a74 100644 (file)
@@ -1,43 +1,31 @@
-/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2004 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 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
 
 \f
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
 #include <errno.h>
-#include <gmp.h>
-
-#include "libguile/_scm.h"
-#include "libguile/unif.h"
-#include "libguile/feature.h"
-#include "libguile/fports.h"
-#include "libguile/strings.h"
-#include "libguile/vectors.h"
-
-#include "libguile/validate.h"
-#include "libguile/socket.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>
-#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))
-#endif
-
-/* we are not currently using socklen_t.  it's not defined on all systems,
-   so would need to be checked by configure.  in the meantime, plain
-   int is the best alternative.  */
-
-\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
-{
-  unsigned short c_in;
-
-  SCM_VALIDATE_INUM_COPY (1, value, c_in);
-  if (c_in != SCM_INUM (value))
-    SCM_OUT_OF_RANGE (1, value);
+#include <gmp.h>
 
-  return SCM_MAKINUM (htons (c_in));
-}
-#undef FUNC_NAME
+#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"
 
-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
-{
-  unsigned short c_in;
+#include "libguile/validate.h"
+#include "libguile/socket.h"
 
-  SCM_VALIDATE_INUM_COPY (1, value, c_in);
-  if (c_in != SCM_INUM (value))
-    SCM_OUT_OF_RANGE (1, value);
+#if SCM_ENABLE_DEPRECATED == 1
+# include "libguile/deprecation.h"
+#endif
 
-  return SCM_MAKINUM (ntohs (c_in));
-}
-#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
-{
-  scm_t_uint32 c_in = SCM_NUM2ULONG (1, value);
 
-  return scm_ulong2num (htonl (c_in));
-}
-#undef FUNC_NAME
+#if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
+#define SUN_LEN(ptr) (offsetof (struct sockaddr_un, sun_path) \
+                     + strlen ((ptr)->sun_path))
+#endif
 
-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
+/* The largest possible socket address.  Wrapping it in a union guarantees
+   that the compiler will make it suitably aligned.  */
+typedef union
 {
-  scm_t_uint32 c_in = SCM_NUM2ULONG (1, value);
-
-  return scm_ulong2num (ntohl (c_in));
-}
-#undef FUNC_NAME
+  struct sockaddr     sockaddr;
+  struct sockaddr_in  sockaddr_in;
 
-#ifndef HAVE_INET_ATON
-/* for our definition in inet_aton.c, not usually needed.  */
-extern int inet_aton ();
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
+  struct sockaddr_un  sockaddr_un;
 #endif
+#ifdef HAVE_IPV6
+  struct sockaddr_in6 sockaddr_in6;
+#endif
+} scm_t_max_sockaddr;
 
-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;
 
-  SCM_VALIDATE_STRING (1, address);
-  if (inet_aton (SCM_STRING_CHARS (address), &soka) == 0)
-    SCM_MISC_ERROR ("bad address", SCM_EOL);
-  return scm_ulong2num (ntohl (soka.s_addr));
-}
-#undef FUNC_NAME
+/* Maximum size of a socket address.  */
+#define MAX_ADDR_SIZE   (sizeof (scm_t_max_sockaddr))
 
 
-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_mem2string (s, strlen (s));
-  return answer;
-}
-#undef FUNC_NAME
+\f
 
 #ifdef HAVE_INET_NETOF
 SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0, 
@@ -187,7 +102,7 @@ SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0,
 {
   struct in_addr addr;
   addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
-  return scm_ulong2num ((unsigned long) inet_netof (addr));
+  return scm_from_ulong (inet_netof (addr));
 }
 #undef FUNC_NAME
 #endif
@@ -205,7 +120,7 @@ SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0,
 {
   struct in_addr addr;
   addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
-  return scm_ulong2num ((unsigned long) inet_lnaof (addr));
+  return scm_from_ulong (inet_lnaof (addr));
 }
 #undef FUNC_NAME
 #endif
@@ -228,7 +143,7 @@ SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
   netnum = SCM_NUM2ULONG (1, net);
   lnanum = SCM_NUM2ULONG (2, lna);
   addr = inet_makeaddr (netnum, lnanum);
-  return scm_ulong2num (ntohl (addr.s_addr));
+  return scm_from_ulong (ntohl (addr.s_addr));
 }
 #undef FUNC_NAME
 #endif
@@ -282,76 +197,30 @@ SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
 
 /* convert a 128 bit IPv6 address in network order to a host ordered
    SCM integer.  */
-static SCM ipv6_net_to_num (const scm_t_uint8 *src)
+static SCM
+scm_from_ipv6 (const scm_t_uint8 *src)
 {
-  int i = 0;
-  const scm_t_uint8 *ptr = src;
-  int num_zero_bytes = 0;
-  scm_t_uint8 addr[16];
-
-  /* count leading zeros (since we know it's bigendian, they'll be first) */ 
-  while (i < 16)
-    {
-      if (*ptr) break;
-      num_zero_bytes++;
-      i++;
-    }
-
-  if (SCM_SIZEOF_UNSIGNED_LONG_LONG != 0) /* compiler should optimize this */
-    {
-      if ((16 - num_zero_bytes) <= sizeof (unsigned long long))
-        {
-          /* it fits */
-          unsigned long long x;
-          
-          FLIPCPY_NET_HOST_128(addr, src);
-#ifdef WORDS_BIGENDIAN
-          memcpy (&x, addr + (16 - sizeof (x)), sizeof (x));
-#else
-          memcpy (&x, addr, sizeof (x));
-#endif
-          return scm_ulong_long2num (x);
-        }
-    }
-  else
-    {
-      if ((16 - num_zero_bytes) <= sizeof (unsigned long))
-        {
-          /* this is just so that we use INUM where possible.  */
-          unsigned long x;
-
-          FLIPCPY_NET_HOST_128(addr, src);          
-#ifdef WORDS_BIGENDIAN
-          memcpy (&x, addr + (16 - sizeof (x)), sizeof (x));
-#else
-          memcpy (&x, addr, sizeof (x));
-#endif
-          return scm_ulong2num (x);
-        }
-    }
-  /* otherwise get the big hammer */
-  {
-    SCM result = scm_i_mkbig ();
-    
-    mpz_import (SCM_I_BIG_MPZ (result),
-                1, /* chunk */
-                1, /* big-endian chunk ordering */
-                16, /* chunks are 16 bytes long */
-                1, /* big-endian byte ordering */
-                0, /* "nails" -- leading unused bits per chunk */
-                src);
-    return scm_i_normbig (result);
-  }
-}  
+  SCM result = scm_i_mkbig ();
+  mpz_import (SCM_I_BIG_MPZ (result),
+              1,  /* chunk */
+              1,  /* big-endian chunk ordering */
+              16, /* chunks are 16 bytes long */
+              1,  /* big-endian byte ordering */
+              0,  /* "nails" -- leading unused bits per chunk */
+              src);
+  return scm_i_normbig (result);
+}
 
 /* convert a host ordered SCM integer to a 128 bit IPv6 address in
    network order.  */
-static void ipv6_num_to_net (SCM src, scm_t_uint8 *dst)
+static void
+scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
 {
-  /* This code presumes that src has already been checked for range. */
-  if (SCM_INUMP (src))
+  if (SCM_I_INUMP (src))
     {
-      scm_t_signed_bits n = SCM_INUM (src);
+      scm_t_signed_bits n = SCM_I_INUM (src);
+      if (n < 0)
+       scm_out_of_range (NULL, src);
 #ifdef WORDS_BIGENDIAN
       memset (dst, 0, 16 - sizeof (scm_t_signed_bits));
       memcpy (dst + (16 - sizeof (scm_t_signed_bits)),
@@ -367,10 +236,14 @@ static void ipv6_num_to_net (SCM src, scm_t_uint8 *dst)
       FLIP_NET_HOST_128 (dst);
 #endif
     }
-  else
+  else if (SCM_BIGP (src))
     {
-      /* Presumes src has already been checked for fit -- see above. */
       size_t count;
+      
+      if ((mpz_sgn (SCM_I_BIG_MPZ (src)) < 0)
+         || mpz_sizeinbase (SCM_I_BIG_MPZ (src), 2) > 128)
+       scm_out_of_range (NULL, src);
+      
       memset (dst, 0, 16);
       mpz_export (dst,
                   &count,
@@ -381,38 +254,68 @@ static void ipv6_num_to_net (SCM src, scm_t_uint8 *dst)
                   SCM_I_BIG_MPZ (src));
       scm_remember_upto_here_1 (src);
     }
+  else
+    scm_wrong_type_arg_msg ("scm_to_ipv6", 0, src, "integer");
 }
 
-static int
-bignum_in_ipv6_range_p (SCM address)
+#endif  /* HAVE_IPV6 */
+
+\f
+
+SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
+            (SCM family, SCM address),
+           "Convert a network address into a printable string.\n"
+           "Note that unlike the C version of this function,\n"
+           "the input 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-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\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
 {
-  int result;
-  int sgn = mpz_sgn (SCM_I_BIG_MPZ (address));
-  
-  if (sgn < 0)
-    result = 0;
-  else
+  int af;
+#ifdef INET6_ADDRSTRLEN
+  char dst[INET6_ADDRSTRLEN];
+#else
+  char dst[46];
+#endif
+  const char *result;
+
+  af = scm_to_int (family);
+  SCM_ASSERT_RANGE (1, family,
+                   af == AF_INET
+#ifdef HAVE_IPV6
+                   || af == AF_INET6
+#endif
+                   );
+  if (af == AF_INET)
+    {
+      scm_t_uint32 addr4;
+
+      addr4 = htonl (SCM_NUM2ULONG (2, address));
+      result = inet_ntop (af, &addr4, dst, sizeof (dst));
+    }
+#ifdef HAVE_IPV6
+  else if (af == AF_INET6)
     {
-      int size = mpz_sizeinbase (SCM_I_BIG_MPZ (address), 2);
-      if (size > 128) result = 0;
-      else result = 1;
+      char addr6[16];
+
+      scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
+      result = inet_ntop (af, &addr6, dst, sizeof (dst));
     }
-  scm_remember_upto_here_1 (address);
-  return result;
+#endif
+  else
+    SCM_MISC_ERROR ("unsupported address family", family);
+
+  if (result == NULL)
+    SCM_SYSERROR;
+
+  return scm_from_locale_string (dst);
 }
+#undef FUNC_NAME
 
-/* check that an SCM variable contains an IPv6 integer address.  */
-#define VALIDATE_INET6(which_arg, address)\
-   if (SCM_INUMP (address))\
-      SCM_ASSERT_RANGE (which_arg, address, SCM_INUM (address) >= 0);\
-   else\
-   {\
-      SCM_VALIDATE_BIGINT (which_arg, address);\
-      SCM_ASSERT_RANGE (which_arg, address,    \
-                        bignum_in_ipv6_range_p (address));      \
-   }
-
-#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"
@@ -428,65 +331,39 @@ SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
 {
   int af;
   char *src;
-  char dst[16];
-  int rv;
+  scm_t_uint32 dst[4];
+  int rv, eno;
 
-  SCM_VALIDATE_INUM_COPY (1, family, af);
-  SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
-  SCM_VALIDATE_STRING_COPY (2, address, src);
+  af = scm_to_int (family);
+  SCM_ASSERT_RANGE (1, family,
+                   af == AF_INET
+#ifdef HAVE_IPV6
+                   || af == AF_INET6
+#endif
+                   );
+
+  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_ulong2num (ntohl (*(scm_t_uint32 *) dst));
-  else
-    return ipv6_net_to_num ((char *) dst);
-}
-#undef FUNC_NAME
-#endif
-
-#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"
-           "Note that unlike the C version of this function,\n"
-           "the input 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-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"
-           "@end lisp")
-#define FUNC_NAME s_scm_inet_ntop
-{
-  int af;
-#ifdef INET6_ADDRSTRLEN
-  char dst[INET6_ADDRSTRLEN];
-#else
-  char dst[46];
+    return scm_from_ulong (ntohl (*dst));
+#ifdef HAVE_IPV6
+  else if (af == AF_INET6)
+    return scm_from_ipv6 ((scm_t_uint8 *) dst);
 #endif
-  char addr6[16];
-
-  SCM_VALIDATE_INUM_COPY (1, family, af);
-  SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
-  if (af == AF_INET)
-    *(scm_t_uint32 *) addr6 = htonl (SCM_NUM2ULONG (2, address));
   else
-    {
-      VALIDATE_INET6 (2, address);
-      ipv6_num_to_net (address, addr6);
-    }
-  if (inet_ntop (af, &addr6, dst, sizeof dst) == NULL)
-    SCM_SYSERROR;
-  return scm_makfrom0str (dst);
+    SCM_MISC_ERROR ("unsupported address family", family);
 }
 #undef FUNC_NAME
-#endif
-
-#endif  /* HAVE_IPV6 */
 
+\f
 SCM_SYMBOL (sym_socket, "socket");
 
 #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
@@ -508,10 +385,9 @@ SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
 {
   int fd;
 
-  SCM_VALIDATE_INUM (1, family);
-  SCM_VALIDATE_INUM (2, style);
-  SCM_VALIDATE_INUM (3, proto);
-  fd = socket (SCM_INUM (family), SCM_INUM (style), SCM_INUM (proto));
+  fd = socket (scm_to_int (family),
+              scm_to_int (style),
+              scm_to_int (proto));
   if (fd == -1)
     SCM_SYSERROR;
   return SCM_SOCK_FD_TO_PORT (fd);
@@ -531,13 +407,9 @@ SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
   int fam;
   int fd[2];
 
-  SCM_VALIDATE_INUM (1, family);
-  SCM_VALIDATE_INUM (2, style);
-  SCM_VALIDATE_INUM (3, proto);
+  fam = scm_to_int (family);
 
-  fam = SCM_INUM (family);
-
-  if (socketpair (fam, SCM_INUM (style), SCM_INUM (proto), fd) == -1)
+  if (socketpair (fam, scm_to_int (style), scm_to_int (proto), fd) == -1)
     SCM_SYSERROR;
 
   return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
@@ -545,37 +417,80 @@ SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
 #undef FUNC_NAME
 #endif
 
+/* Possible results for `getsockopt ()'.  Wrapping it into a union guarantees
+   suitable alignment.  */
+typedef union
+{
+#ifdef HAVE_STRUCT_LINGER
+  struct linger linger;
+#endif
+  size_t size;
+  int    integer;
+} scm_t_getsockopt_result;
+
 SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
             (SCM sock, SCM level, SCM optname),
-           "Return the value of a particular socket option for the socket\n"
-           "port @var{sock}.  @var{level} is an integer code for type of\n"
-           "option being requested, e.g., @code{SOL_SOCKET} for\n"
-           "socket-level options.  @var{optname} is an integer code for the\n"
-           "option required and should be specified using one of the\n"
-           "symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n"
-           "The returned value is typically an integer but @code{SO_LINGER}\n"
-           "returns a pair of integers.")
+           "Return an option value from socket port @var{sock}.\n"
+           "\n"
+           "@var{level} is an integer specifying a protocol layer, either\n"
+           "@code{SOL_SOCKET} for socket level options, or a protocol\n"
+           "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
+           "(@pxref{Network Databases}).\n"
+           "\n"
+           "@defvar SOL_SOCKET\n"
+           "@defvarx IPPROTO_IP\n"
+           "@defvarx IPPROTO_TCP\n"
+           "@defvarx IPPROTO_UDP\n"
+           "@end defvar\n"
+           "\n"
+           "@var{optname} is an integer specifying an option within the\n"
+           "protocol layer.\n"
+           "\n"
+           "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
+           "defined (when provided by the system).  For their meaning see\n"
+           "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
+           "Manual}, or @command{man 7 socket}.\n"
+           "\n"
+           "@defvar SO_DEBUG\n"
+           "@defvarx SO_REUSEADDR\n"
+           "@defvarx SO_STYLE\n"
+           "@defvarx SO_TYPE\n"
+           "@defvarx SO_ERROR\n"
+           "@defvarx SO_DONTROUTE\n"
+           "@defvarx SO_BROADCAST\n"
+           "@defvarx SO_SNDBUF\n"
+           "@defvarx SO_RCVBUF\n"
+           "@defvarx SO_KEEPALIVE\n"
+           "@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 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"
+           "pair.\n"
+           "@end defvar")
 #define FUNC_NAME s_scm_getsockopt
 {
   int fd;
   /* size of optval is the largest supported option.  */
-#ifdef HAVE_STRUCT_LINGER
-  char optval[sizeof (struct linger)];
-  int optlen = sizeof (struct linger);
-#else
-  char optval[sizeof (size_t)];
-  int optlen = sizeof (size_t);
-#endif
+  scm_t_getsockopt_result optval;
+  socklen_t optlen = sizeof (optval);
   int ilevel;
   int ioptname;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_INUM_COPY (2, level, ilevel);
-  SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
+  ilevel = scm_to_int (level);
+  ioptname = scm_to_int (optname);
 
   fd = SCM_FPORT_FDES (sock);
-  if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
+  if (getsockopt (fd, ilevel, ioptname, (void *) &optval, &optlen) == -1)
     SCM_SYSERROR;
 
   if (ilevel == SOL_SOCKET)
@@ -584,13 +499,13 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
       if (ioptname == SO_LINGER)
        {
 #ifdef HAVE_STRUCT_LINGER
-         struct linger *ling = (struct linger *) optval;
+         struct linger *ling = (struct linger *) &optval;
 
-         return scm_cons (scm_long2num (ling->l_onoff),
-                          scm_long2num (ling->l_linger));
+         return scm_cons (scm_from_long (ling->l_onoff),
+                          scm_from_long (ling->l_linger));
 #else
-         return scm_cons (scm_long2num (*(int *) optval),
-                          SCM_MAKINUM (0));
+         return scm_cons (scm_from_long (*(int *) &optval),
+                          scm_from_int (0));
 #endif
        }
       else
@@ -604,77 +519,134 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
 #endif
            )
          {
-           return scm_long2num (*(size_t *) optval);
+           return scm_from_size_t (*(size_t *) &optval);
          }
     }
-  return scm_long2num (*(int *) optval);
+  return scm_from_int (*(int *) &optval);
 }
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
             (SCM sock, SCM level, SCM optname, SCM value),
-           "Set the value of a particular socket option for the socket\n"
-           "port @var{sock}.  @var{level} is an integer code for type of option\n"
-           "being set, e.g., @code{SOL_SOCKET} for socket-level options.\n"
-           "@var{optname} is an\n"
-           "integer code for the option to set and should be specified using one of\n"
-           "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n"
-           "@var{value} is the value to which the option should be set.  For\n"
-           "most options this must be an integer, but for @code{SO_LINGER} it must\n"
-           "be a pair.\n\n"
-           "The return value is unspecified.")
+           "Set an option on socket port @var{sock}.  The return value is\n"
+           "unspecified.\n"
+           "\n"
+           "@var{level} is an integer specifying a protocol layer, either\n"
+           "@code{SOL_SOCKET} for socket level options, or a protocol\n"
+           "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
+           "(@pxref{Network Databases}).\n"
+           "\n"
+           "@defvar SOL_SOCKET\n"
+           "@defvarx IPPROTO_IP\n"
+           "@defvarx IPPROTO_TCP\n"
+           "@defvarx IPPROTO_UDP\n"
+           "@end defvar\n"
+           "\n"
+           "@var{optname} is an integer specifying an option within the\n"
+           "protocol layer.\n"
+           "\n"
+           "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
+           "defined (when provided by the system).  For their meaning see\n"
+           "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
+           "Manual}, or @command{man 7 socket}.\n"
+           "\n"
+           "@defvar SO_DEBUG\n"
+           "@defvarx SO_REUSEADDR\n"
+           "@defvarx SO_STYLE\n"
+           "@defvarx SO_TYPE\n"
+           "@defvarx SO_ERROR\n"
+           "@defvarx SO_DONTROUTE\n"
+           "@defvarx SO_BROADCAST\n"
+           "@defvarx SO_SNDBUF\n"
+           "@defvarx SO_RCVBUF\n"
+           "@defvarx SO_KEEPALIVE\n"
+           "@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"
+           "@defvar SO_LINGER\n"
+           "@var{value} is a pair of integers @code{(@var{ENABLE}\n"
+           ". @var{TIMEOUT})}.  On old systems without timeout support\n"
+           "(ie.@: without @code{struct linger}), only @var{ENABLE} has an\n"
+           "effect but the value in Guile is always a pair.\n"
+           "@end defvar\n"
+           "\n"
+           "@c  Note that we refer only to ``man ip'' here.  On GNU/Linux it's\n"
+           "@c  ``man 7 ip'' but on NetBSD it's ``man 4 ip''.\n"
+           "@c \n"
+           "For IP level (@code{IPPROTO_IP}) the following @var{optname}s\n"
+           "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"
+           "@code{getsockopt}.  @var{value} is a pair\n"
+           "@code{(@var{MULTIADDR} . @var{INTERFACEADDR})} of IPv4\n"
+           "addresses (@pxref{Network Address Conversion}).\n"
+           "@var{MULTIADDR} is a multicast address to be added to or\n"
+           "dropped from the interface @var{INTERFACEADDR}.\n"
+           "@var{INTERFACEADDR} can be @code{INADDR_ANY} to have the system\n"
+           "select the interface.  @var{INTERFACEADDR} can also be an\n"
+           "interface index number, on systems supporting that.\n"
+           "@end defvar")
 #define FUNC_NAME s_scm_setsockopt
 {
   int fd;
-  int optlen = -1;
-  /* size of optval is the largest supported option.  */
+
+  int opt_int;
 #ifdef HAVE_STRUCT_LINGER
-  char optval[sizeof (struct linger)];
-#else
-  char optval[sizeof (size_t)];
+  struct linger opt_linger;
 #endif
+
+#ifdef HAVE_STRUCT_IP_MREQ
+  struct ip_mreq opt_mreq;
+#endif
+
+  const void *optval = NULL;
+  socklen_t optlen = 0;
+
   int ilevel, ioptname;
 
   sock = SCM_COERCE_OUTPORT (sock);
 
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_INUM_COPY (2, level, ilevel);
-  SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
+  ilevel = scm_to_int (level);
+  ioptname = scm_to_int (optname);
 
   fd = SCM_FPORT_FDES (sock);
-
+  
   if (ilevel == SOL_SOCKET)
     {
 #ifdef SO_LINGER
       if (ioptname == SO_LINGER)
        {
 #ifdef HAVE_STRUCT_LINGER
-         struct linger ling;
-         long lv;
-
-         SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
-         lv = SCM_NUM2LONG (4, SCM_CAR (value));
-         ling.l_onoff = (int) lv;
-         SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_onoff == lv);
-         lv = SCM_NUM2LONG (4, SCM_CDR (value));
-         ling.l_linger = (int) lv;
-         SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_linger == lv);
-         optlen = (int) sizeof (struct linger);
-         memcpy (optval, (void *) &ling, optlen);
+         SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
+         opt_linger.l_onoff = scm_to_int (SCM_CAR (value));
+         opt_linger.l_linger = scm_to_int (SCM_CDR (value));
+         optlen = sizeof (struct linger);
+         optval = &opt_linger;
 #else
-         int ling;
-         long lv;
-
-         SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
+         SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
+         opt_int = scm_to_int (SCM_CAR (value));
          /* timeout is ignored, but may as well validate it.  */
-         lv = SCM_NUM2LONG (4, SCM_CDR (value));
-         ling = (int) lv;
-         SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv);
-         lv = SCM_NUM2LONG (4, SCM_CAR (value));
-         ling = (int) lv;
-         SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv);
-         optlen = (int) sizeof (int);
-         (*(int *) optval) = ling;
+         scm_to_int (SCM_CDR (value));
+         optlen = sizeof (int);
+         optval = &opt_int;
 #endif
        }
       else
@@ -688,28 +660,44 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
 #endif
            )
          {
-           long lv = SCM_NUM2LONG (4, value);
-
-           optlen = (int) sizeof (size_t);
-           (*(size_t *) optval) = (size_t) lv;
+           opt_int = scm_to_int (value);
+           optlen = sizeof (size_t);
+           optval = &opt_int;
          }
     }
-  if (optlen == -1)
+
+#ifdef HAVE_STRUCT_IP_MREQ
+  if (ilevel == IPPROTO_IP &&
+      (ioptname == IP_ADD_MEMBERSHIP || ioptname == IP_DROP_MEMBERSHIP))
     {
-      /* Most options take an int.  */
-      long lv = SCM_NUM2LONG (4, value);
-      int val = (int) lv;
+      /* Fourth argument must be a pair of addresses. */
+      SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
+      opt_mreq.imr_multiaddr.s_addr = htonl (scm_to_ulong (SCM_CAR (value)));
+      opt_mreq.imr_interface.s_addr = htonl (scm_to_ulong (SCM_CDR (value)));
+      optlen = sizeof (opt_mreq);
+      optval = &opt_mreq;
+    }
+#endif
 
-      SCM_ASSERT_RANGE (SCM_ARG4, value, val == lv);
-      optlen = (int) sizeof (int);
-      (*(int *) optval) = val;
+  if (optval == NULL)
+    {
+      /* Most options take an int.  */
+      opt_int = scm_to_int (value);
+      optlen = sizeof (int);
+      optval = &opt_int;
     }
-  if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1)
+
+  if (setsockopt (fd, ilevel, ioptname, optval, optlen) == -1)
     SCM_SYSERROR;
   return SCM_UNSPECIFIED;
 }
 #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"
@@ -732,10 +720,8 @@ SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
   int fd;
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_INUM (2, how);
-  SCM_ASSERT_RANGE(2, how,0 <= SCM_INUM (how) && 2 >= SCM_INUM (how));
   fd = SCM_FPORT_FDES (sock);
-  if (shutdown (fd, SCM_INUM (how)) == -1)
+  if (shutdown (fd, scm_to_signed_integer (how, 0, 2)) == -1)
     SCM_SYSERROR;
   return SCM_UNSPECIFIED;
 }
@@ -750,7 +736,7 @@ SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
 
 static struct sockaddr *
 scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
-                  const char *proc, int *size)
+                  const char *proc, size_t *size)
 #define FUNC_NAME proc
 {
   switch (fam)
@@ -763,14 +749,12 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
 
        SCM_VALIDATE_ULONG_COPY (which_arg, address, addr);
        SCM_VALIDATE_CONS (which_arg + 1, *args);
-       SCM_VALIDATE_INUM_COPY (which_arg + 1, SCM_CAR (*args), port);
+       port = scm_to_int (SCM_CAR (*args));
        *args = SCM_CDR (*args);
        soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in));
-       if (!soka)
-         scm_memory_error (proc);
-       /* 4.4BSD-style interface includes sin_len member and defines SIN_LEN,
-          4.3BSD  does not.  */
-#ifdef SIN_LEN
+        memset (soka, '\0', sizeof (struct sockaddr_in));
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
        soka->sin_len = sizeof (struct sockaddr_in);
 #endif
        soka->sin_family = AF_INET;
@@ -788,15 +772,14 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
        unsigned long flowinfo = 0;
        unsigned long scope_id = 0;
 
-       VALIDATE_INET6 (which_arg, address);
        SCM_VALIDATE_CONS (which_arg + 1, *args);
-       SCM_VALIDATE_INUM_COPY (which_arg + 1, SCM_CAR (*args), port);
+       port = scm_to_int (SCM_CAR (*args));
        *args = SCM_CDR (*args);
-       if (SCM_CONSP (*args))
+       if (scm_is_pair (*args))
          {
            SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo);
            *args = SCM_CDR (*args);
-           if (SCM_CONSP (*args))
+           if (scm_is_pair (*args))
              {
                SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args),
                                         scope_id);
@@ -804,13 +787,12 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
              }
          }
        soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));
-       if (!soka)
-         scm_memory_error (proc);
-#ifdef SIN_LEN6
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
        soka->sin6_len = sizeof (struct sockaddr_in6);
 #endif
        soka->sin6_family = AF_INET6;
-       ipv6_num_to_net (address, soka->sin6_addr.s6_addr);
+       scm_to_ipv6 (soka->sin6_addr.s6_addr, address);
        soka->sin6_port = htons (port);
        soka->sin6_flowinfo = flowinfo;
 #ifdef HAVE_SIN6_SCOPE_ID
@@ -825,8 +807,13 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
       {
        struct sockaddr_un *soka;
        int addr_size;
+       char *c_address;
+
+       scm_dynwind_begin (0);
+
+       c_address = scm_to_locale_string (address);
+       scm_dynwind_free (c_address);
 
-       SCM_ASSERT (SCM_STRINGP (address), address, which_arg, proc);
        /* the static buffer size in sockaddr_un seems to be arbitrary
           and not necessarily a hard limit.  e.g., the glibc manual
           suggests it may be possible to declare it size 0.  let's
@@ -834,26 +821,25 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
           connect/bind etc., to fail.  sun_path is always the last
           member of the structure.  */
        addr_size = sizeof (struct sockaddr_un)
-         + max (0, SCM_STRING_LENGTH (address) + 1 - (sizeof soka->sun_path));
+         + max (0, strlen (c_address) + 1 - (sizeof soka->sun_path));
        soka = (struct sockaddr_un *) scm_malloc (addr_size);
-       if (!soka)
-         scm_memory_error (proc);
        memset (soka, 0, addr_size);  /* for sun_len: see sin_len above. */
        soka->sun_family = AF_UNIX;
-       memcpy (soka->sun_path, SCM_STRING_CHARS (address),
-               SCM_STRING_LENGTH (address));
+       strcpy (soka->sun_path, c_address);
        *size = SUN_LEN (soka);
+
+       scm_dynwind_end ();
        return (struct sockaddr *) soka;
       }
 #endif
     default:
-      scm_out_of_range (proc, SCM_MAKINUM (fam));
+      scm_out_of_range (proc, scm_from_int (fam));
     }
 }
 #undef FUNC_NAME
-  
-SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
-            (SCM sock, SCM fam, SCM address, SCM args),
+
+SCM_DEFINE (scm_connect, "connect", 2, 1, 1,
+            (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
            "Initiate a connection from a socket using a specified address\n"
            "family to the address\n"
            "specified by @var{address} and possibly @var{args}.\n"
@@ -870,23 +856,32 @@ SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
            "@var{args} may be up to three integers:\n"
            "port [flowinfo] [scope_id],\n"
            "where flowinfo and scope_id default to zero.\n\n"
+           "Alternatively, the second argument can be a socket address object "
+           "as returned by @code{make-socket-address}, in which case the "
+           "no additional arguments should be passed.\n\n"
            "The return value is unspecified.")
 #define FUNC_NAME s_scm_connect
 {
   int fd;
   struct sockaddr *soka;
-  int size;
+  size_t size;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_INUM (2, fam);
   fd = SCM_FPORT_FDES (sock);
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
-                           &size);
+
+  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);
+  else
+    soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
+                             &args, 3, FUNC_NAME, &size);
+
   if (connect (fd, soka, size) == -1)
     {
       int save_errno = errno;
-      
+
       free (soka);
       errno = save_errno;
       SCM_SYSERROR;
@@ -896,8 +891,8 @@ SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
-            (SCM sock, SCM fam, SCM address, SCM args),
+SCM_DEFINE (scm_bind, "bind", 2, 1, 1,
+            (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
            "Assign an address to the socket port @var{sock}.\n"
            "Generally this only needs to be done for server sockets,\n"
            "so they know where to look for incoming connections.  A socket\n"
@@ -930,23 +925,33 @@ SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
            "may be up to three integers:\n"
            "port [flowinfo] [scope_id],\n"
            "where flowinfo and scope_id default to zero.\n\n"
+           "Alternatively, the second argument can be a socket address object "
+           "as returned by @code{make-socket-address}, in which case the "
+           "no additional arguments should be passed.\n\n"
            "The return value is unspecified.")
 #define FUNC_NAME s_scm_bind
 {
   struct sockaddr *soka;
-  int size;
+  size_t size;
   int fd;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_INUM (2, fam);
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
-                           &size);
   fd = SCM_FPORT_FDES (sock);
+
+  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);
+  else
+    soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
+                             &args, 3, FUNC_NAME, &size);
+
+
   if (bind (fd, soka, size) == -1)
   {
     int save_errno = errno;
-      
+
     free (soka);
     errno = save_errno;
     SCM_SYSERROR;
@@ -970,22 +975,20 @@ SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
   int fd;
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_INUM (2, backlog);
   fd = SCM_FPORT_FDES (sock);
-  if (listen (fd, SCM_INUM (backlog)) == -1)
+  if (listen (fd, scm_to_int (backlog)) == -1)
     SCM_SYSERROR;
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
 /* Put the components of a sockaddr into a new SCM vector.  */
-static SCM
-scm_addr_vector (const struct sockaddr *address, int addr_size, 
-                const char *proc)
+static SCM_C_INLINE_KEYWORD SCM
+_scm_from_sockaddr (const scm_t_max_sockaddr *address, unsigned addr_size,
+                   const char *proc)
 {
-  short int fam = address->sa_family;
-  SCM result =SCM_EOL;
-
+  SCM result = SCM_EOL;
+  short int fam = ((struct sockaddr *) address)->sa_family;
 
   switch (fam)
     {
@@ -995,9 +998,12 @@ scm_addr_vector (const struct sockaddr *address, int addr_size,
 
        result = scm_c_make_vector (3, SCM_UNSPECIFIED);
 
-       SCM_VECTOR_SET(result, 0, scm_ulong2num ((unsigned long) fam));
-       SCM_VECTOR_SET(result, 1, scm_ulong2num (ntohl (nad->sin_addr.s_addr)));
-       SCM_VECTOR_SET(result, 2, scm_ulong2num ((unsigned long) ntohs (nad->sin_port)));
+       SCM_SIMPLE_VECTOR_SET(result, 0,
+                             scm_from_short (fam));
+       SCM_SIMPLE_VECTOR_SET(result, 1,
+                             scm_from_ulong (ntohl (nad->sin_addr.s_addr)));
+       SCM_SIMPLE_VECTOR_SET(result, 2,
+                             scm_from_ushort (ntohs (nad->sin_port)));
       }
       break;
 #ifdef HAVE_IPV6
@@ -1006,14 +1012,14 @@ scm_addr_vector (const struct sockaddr *address, int addr_size,
        const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;
 
        result = scm_c_make_vector (5, SCM_UNSPECIFIED);
-       SCM_VECTOR_SET(result, 0, scm_ulong2num ((unsigned long) fam));
-       SCM_VECTOR_SET(result, 1, ipv6_net_to_num (nad->sin6_addr.s6_addr));
-       SCM_VECTOR_SET(result, 2, scm_ulong2num ((unsigned long) ntohs (nad->sin6_port)));
-       SCM_VECTOR_SET(result, 3, scm_ulong2num ((unsigned long) nad->sin6_flowinfo));
+       SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
+       SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_ipv6 (nad->sin6_addr.s6_addr));
+       SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ushort (ntohs (nad->sin6_port)));
+       SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_uint32 (nad->sin6_flowinfo));
 #ifdef HAVE_SIN6_SCOPE_ID
-       SCM_VECTOR_SET(result, 4, scm_ulong2num ((unsigned long) nad->sin6_scope_id));
+       SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_ulong (nad->sin6_scope_id));
 #else
-       SCM_VECTOR_SET(result, 4, SCM_INUM0);
+       SCM_SIMPLE_VECTOR_SET(result, 4, SCM_INUM0);
 #endif
       }
       break;
@@ -1025,43 +1031,234 @@ scm_addr_vector (const struct sockaddr *address, int addr_size,
 
        result = scm_c_make_vector (2, SCM_UNSPECIFIED);
 
-       SCM_VECTOR_SET(result, 0, scm_ulong2num ((unsigned long) fam));
+       SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
        /* When addr_size is not enough to cover sun_path, do not try
           to access it. */
        if (addr_size <= offsetof (struct sockaddr_un, sun_path))
-         SCM_VECTOR_SET(result, 1, SCM_BOOL_F);
+         SCM_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F);
        else
-         SCM_VECTOR_SET(result, 1, scm_mem2string (nad->sun_path,
-                                                   strlen (nad->sun_path)));
+         SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (nad->sun_path));
       }
       break;
 #endif
     default:
-      scm_misc_error (proc, "Unrecognised address family: ~A",
-                     scm_list_1 (SCM_MAKINUM (fam)));
+      result = SCM_UNSPECIFIED;
+      scm_misc_error (proc, "unrecognised address family: ~A",
+                     scm_list_1 (scm_from_int (fam)));
+
     }
   return result;
 }
 
-/* calculate the size of a buffer large enough to hold any supported
-   sockaddr type.  if the buffer isn't large enough, certain system
-   calls will return a truncated address.  */
+/* The publicly-visible function.  Return a Scheme object representing
+   ADDRESS, an address of ADDR_SIZE bytes.  */
+SCM
+scm_from_sockaddr (const struct sockaddr *address, unsigned addr_size)
+{
+  return (_scm_from_sockaddr ((scm_t_max_sockaddr *) address,
+                             addr_size, "scm_from_sockaddr"));
+}
 
-#if defined (HAVE_UNIX_DOMAIN_SOCKETS)
-#define MAX_SIZE_UN sizeof (struct sockaddr_un)
-#else
-#define MAX_SIZE_UN 0
+/* Convert ADDRESS, an address object returned by either
+   `scm_from_sockaddr ()' or `scm_make_socket_address ()', into its C
+   representation.  On success, a non-NULL pointer is returned and
+   ADDRESS_SIZE is updated to the actual size (in bytes) of the returned
+   address.  The result must eventually be freed using `free ()'.  */
+struct sockaddr *
+scm_to_sockaddr (SCM address, size_t *address_size)
+#define FUNC_NAME "scm_to_sockaddr"
+{
+  short int family;
+  struct sockaddr *c_address = NULL;
+
+  SCM_VALIDATE_VECTOR (1, address);
+
+  *address_size = 0;
+  family = scm_to_short (SCM_SIMPLE_VECTOR_REF (address, 0));
+
+  switch (family)
+    {
+    case AF_INET:
+      {
+       if (SCM_SIMPLE_VECTOR_LENGTH (address) != 3)
+         scm_misc_error (FUNC_NAME,
+                         "invalid inet address representation: ~A",
+                         scm_list_1 (address));
+       else
+         {
+           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 =
+             htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
+           c_inet.sin_family = AF_INET;
+
+           *address_size = sizeof (c_inet);
+           c_address = scm_malloc (sizeof (c_inet));
+           memcpy (c_address, &c_inet, sizeof (c_inet));
+         }
+
+       break;
+      }
+
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      {
+       if (SCM_SIMPLE_VECTOR_LENGTH (address) != 5)
+         scm_misc_error (FUNC_NAME, "invalid inet6 address representation: ~A",
+                         scm_list_1 (address));
+       else
+         {
+           struct sockaddr_in6 c_inet6;
+
+           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 =
+             scm_to_uint32 (SCM_SIMPLE_VECTOR_REF (address, 3));
+#ifdef HAVE_SIN6_SCOPE_ID
+           c_inet6.sin6_scope_id =
+             scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 4));
 #endif
 
-#if defined (HAVE_IPV6)
-#define MAX_SIZE_IN6 sizeof (struct sockaddr_in6)
+           c_inet6.sin6_family = AF_INET6;
+
+           *address_size = sizeof (c_inet6);
+           c_address = scm_malloc (sizeof (c_inet6));
+           memcpy (c_address, &c_inet6, sizeof (c_inet6));
+         }
+
+       break;
+      }
+#endif
+
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
+    case AF_UNIX:
+      {
+       if (SCM_SIMPLE_VECTOR_LENGTH (address) != 2)
+         scm_misc_error (FUNC_NAME, "invalid unix address representation: ~A",
+                         scm_list_1 (address));
+       else
+         {
+           SCM path;
+           size_t path_len = 0;
+
+           path = SCM_SIMPLE_VECTOR_REF (address, 1);
+           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 (scm_is_false (path))
+                 path_len = 0;
+               else
+                 path_len = scm_c_string_length (path);
+
+#ifdef UNIX_PATH_MAX
+               if (path_len >= UNIX_PATH_MAX)
 #else
-#define MAX_SIZE_IN6 0
+/* We can hope that this limit will eventually vanish, at least on GNU.
+   However, currently, while glibc doesn't define `UNIX_PATH_MAX', it
+   documents it has being limited to 108 bytes.  */
+               if (path_len >= sizeof (c_unix.sun_path))
+#endif
+                 scm_misc_error (FUNC_NAME, "unix address path "
+                                 "too long: ~A", scm_list_1 (path));
+               else
+                 {
+                   if (path_len)
+                     {
+                       scm_to_locale_stringbuf (path, c_unix.sun_path,
+#ifdef UNIX_PATH_MAX
+                                                UNIX_PATH_MAX);
+#else
+                                                sizeof (c_unix.sun_path));
+#endif
+                       c_unix.sun_path[path_len] = '\0';
+
+                       /* Sanity check.  */
+                       if (strlen (c_unix.sun_path) != path_len)
+                         scm_misc_error (FUNC_NAME, "unix address path "
+                                         "contains nul characters: ~A",
+                                         scm_list_1 (path));
+                     }
+                   else
+                     c_unix.sun_path[0] = '\0';
+
+                   c_unix.sun_family = AF_UNIX;
+
+                   *address_size = SUN_LEN (&c_unix);
+                   c_address = scm_malloc (sizeof (c_unix));
+                   memcpy (c_address, &c_unix, sizeof (c_unix));
+                 }
+             }
+         }
+
+       break;
+      }
 #endif
 
-#define MAX_ADDR_SIZE max (max (sizeof (struct sockaddr_in), MAX_SIZE_IN6),\
-                           MAX_SIZE_UN)
+    default:
+      scm_misc_error (FUNC_NAME, "unrecognised address family: ~A",
+                     scm_list_1 (scm_from_ushort (family)));
+    }
+
+  return c_address;
+}
+#undef FUNC_NAME
+
+
+/* Return a newly-allocated `sockaddr' structure that reflects ADDRESS, being
+   an address of family FAMILY, with the family-specific parameters ARGS (see
+   the description of `connect' for details).  The returned structure may be
+   freed using `free ()'.  */
+struct sockaddr *
+scm_c_make_socket_address (SCM family, SCM address, SCM args,
+                          size_t *address_size)
+{
+  struct sockaddr *soka;
+
+  soka = scm_fill_sockaddr (scm_to_ushort (family), address, &args, 1,
+                           "scm_c_make_socket_address", address_size);
+
+  return soka;
+}
+
+SCM_DEFINE (scm_make_socket_address, "make-socket-address", 2, 0, 1,
+           (SCM family, SCM address, SCM args),
+           "Return a Scheme address object that reflects @var{address}, "
+           "being an address of family @var{family}, with the "
+           "family-specific parameters @var{args} (see the description of "
+           "@code{connect} for details).")
+#define FUNC_NAME s_scm_make_socket_address
+{
+  SCM result = SCM_BOOL_F;
+  struct sockaddr *c_address;
+  size_t c_address_size;
+
+  c_address = scm_c_make_socket_address (family, address, args,
+                                        &c_address_size);
+  if (c_address != NULL)
+    {
+      result = scm_from_sockaddr (c_address, c_address_size);
+      free (c_address);
+    }
+
+  return result;
+}
+#undef FUNC_NAME
 
+\f
 SCM_DEFINE (scm_accept, "accept", 1, 0, 0, 
             (SCM sock),
            "Accept a connection on a bound, listening socket.\n"
@@ -1082,18 +1279,19 @@ SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
   int newfd;
   SCM address;
   SCM newsock;
-  int addr_size = MAX_ADDR_SIZE;
-  char max_addr[MAX_ADDR_SIZE];
-  struct sockaddr *addr = (struct sockaddr *) max_addr;
+  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);
-  newfd = accept (fd, 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);
-  address = scm_addr_vector (addr, addr_size, FUNC_NAME);
+  address = _scm_from_sockaddr (&addr, addr_size,
+                               FUNC_NAME);
+
   return scm_cons (newsock, address);
 }
 #undef FUNC_NAME
@@ -1106,16 +1304,16 @@ SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
 #define FUNC_NAME s_scm_getsockname
 {
   int fd;
-  int addr_size = MAX_ADDR_SIZE;
-  char max_addr[MAX_ADDR_SIZE];
-  struct sockaddr *addr = (struct sockaddr *) max_addr;
+  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);
-  if (getsockname (fd, addr, &addr_size) == -1)
+  if (getsockname (fd, (struct sockaddr *) &addr, &addr_size) == -1)
     SCM_SYSERROR;
-  return scm_addr_vector (addr, addr_size, FUNC_NAME);
+
+  return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
 }
 #undef FUNC_NAME
 
@@ -1128,16 +1326,16 @@ SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
 #define FUNC_NAME s_scm_getpeername
 {
   int fd;
-  int addr_size = MAX_ADDR_SIZE;
-  char max_addr[MAX_ADDR_SIZE];
-  struct sockaddr *addr = (struct sockaddr *) max_addr;
+  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);
-  if (getpeername (fd, addr, &addr_size) == -1)
+  if (getpeername (fd, (struct sockaddr *) &addr, &addr_size) == -1)
     SCM_SYSERROR;
-  return scm_addr_vector (addr, addr_size, FUNC_NAME);
+
+  return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
 }
 #undef FUNC_NAME
 
@@ -1146,7 +1344,7 @@ 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"
@@ -1162,26 +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;
+  int rv, fd, flg;
 
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_STRING (2, buf);
-  SCM_VALIDATE_INUM_DEF_COPY (3, flags,0, flg);
+
+  if (SCM_UNBNDP (flags))
+    flg = 0;
+  else
+    flg = scm_to_int (flags);
   fd = SCM_FPORT_FDES (sock);
 
-  SCM_SYSCALL (rv = recv (fd, SCM_STRING_CHARS (buf), SCM_STRING_LENGTH (buf), flg));
-  if (rv == -1)
+#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);
+
+      SCM_SYSCALL (rv = recv (fd,
+                             SCM_BYTEVECTOR_CONTENTS (buf),
+                             SCM_BYTEVECTOR_LENGTH (buf),
+                             flg));
+    }
+
+  if (SCM_UNLIKELY (rv == -1))
     SCM_SYSERROR;
 
-  return SCM_MAKINUM (rv);
+  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"
@@ -1192,93 +1419,179 @@ SCM_DEFINE (scm_send, "send", 2, 1, 0,
            "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
            "Note that the data is written directly to the socket\n"
            "file descriptor:\n"
-           "any unflushed buffered port data is ignored.")
+           "any unflushed buffered port data is ignored.\n\n"
+           "This operation is defined only for strings containing codepoints\n"
+           "zero to 255.")
 #define FUNC_NAME s_scm_send
 {
-  int rv;
-  int fd;
-  int flg;
+  int rv, fd, flg;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
-  SCM_VALIDATE_STRING (2, message);
-  SCM_VALIDATE_INUM_DEF_COPY (3, flags,0, flg);
+
+  if (SCM_UNBNDP (flags))
+    flg = 0;
+  else
+    flg = scm_to_int (flags);
+
   fd = SCM_FPORT_FDES (sock);
 
-  SCM_SYSCALL (rv = send (fd, SCM_STRING_CHARS (message), SCM_STRING_LENGTH (message), flg));
+#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;
-  return SCM_MAKINUM (rv);
+
+  scm_remember_upto_here_1 (message);
+  return scm_from_int (rv);
 }
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
-            (SCM sock, SCM str, SCM flags, SCM start, SCM end),
-           "Return data from the socket port @var{sock} and also\n"
-           "information about where the data was received from.\n"
-           "@var{sock} must already be bound to the address from which\n"
-           "data is to be received.  @code{str}, is a string into which the\n"
-           "data will be written.  The size of @var{str} limits the amount\n"
-           "of data which can be received: in the case of packet protocols,\n"
-           "if a packet larger than this limit is encountered then some\n"
-           "data will be irrevocably lost.\n\n"
-           "The optional @var{flags} argument is a value or bitwise OR of\n"
-           "@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.\n\n"
-           "The value returned is a pair: the @emph{car} is the number of\n"
-           "bytes read from the socket and the @emph{cdr} an address object\n"
-           "in the same form as returned by @code{accept}.  The address\n"
-           "will given as @code{#f} if not available, as is usually the\n"
-           "case for stream sockets.\n\n"
-           "The @var{start} and @var{end} arguments specify a substring of\n"
-           "@var{str} to which the data should be written.\n\n"
-           "Note that the data is read directly from the socket file\n"
-           "descriptor: any unread buffered port data is ignored.")
+            (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 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 came from, or @code{#f} if the origin is\n"
+           "unknown.\n"
+           "\n"
+           "The optional @var{flags} argument is a or bitwise OR\n"
+           "(@code{logior}) of @code{MSG_OOB}, @code{MSG_PEEK},\n"
+           "@code{MSG_DONTROUTE} etc.\n"
+           "\n"
+           "Data is read directly from the socket file descriptor, any\n"
+           "buffered port data is ignored.\n"
+           "\n"
+           "On a GNU/Linux system @code{recvfrom!} is not multi-threading,\n"
+           "all threads stop while a @code{recvfrom!} call is in progress.\n"
+           "An application may need to use @code{select}, @code{O_NONBLOCK}\n"
+           "or @code{MSG_DONTWAIT} to avoid this.")
 #define FUNC_NAME s_scm_recvfrom
 {
-  int rv;
-  int fd;
-  int flg;
-  char *buf;
-  int offset;
-  int cend;
+  int rv, fd, flg;
   SCM address;
-  int addr_size = MAX_ADDR_SIZE;
-  char max_addr[MAX_ADDR_SIZE];
-  struct sockaddr *addr = (struct sockaddr *) max_addr;
+  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_SUBSTRING_SPEC_COPY (2, str, buf, 4, start, offset,
-                                   5, 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.  */
-  addr->sa_family = AF_UNSPEC;
-  SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
-                             cend - offset, flg,
-                             addr, &addr_size));
+  ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
+
+#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;
-  if (addr->sa_family != AF_UNSPEC)
-    address = scm_addr_vector (addr, addr_size, FUNC_NAME);
+
+  /* `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;
 
-  return scm_cons (SCM_MAKINUM (rv), address);
+  scm_remember_upto_here_1 (buf);
+
+  return scm_cons (scm_from_int (rv), address);
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
-            (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags),
-           "Transmit the string @var{message} on the socket port\n"
+SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
+            (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SCM args_and_flags),
+           "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, in a similar way to the\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"
            "@code{connect} procedure.  @var{args_and_flags} contains\n"
            "the usual connection arguments optionally followed by\n"
            "a flags argument, which is a value or\n"
@@ -1290,32 +1603,70 @@ SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
            "set to be non-blocking.\n"
            "Note that the data is written directly to the socket\n"
            "file descriptor:\n"
-           "any unflushed buffered port data is ignored.")
+           "any unflushed buffered port data is ignored.\n"
+           "This operation is defined only for strings containing codepoints\n"
+           "zero to 255.")
 #define FUNC_NAME s_scm_sendto
 {
-  int rv;
-  int fd;
-  int flg;
+  int rv, fd, flg;
   struct sockaddr *soka;
-  int size;
+  size_t size;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_FPORT (1, sock);
-  SCM_VALIDATE_STRING (2, message);
-  SCM_VALIDATE_INUM (3, fam);
   fd = SCM_FPORT_FDES (sock);
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args_and_flags, 4,
-                           FUNC_NAME, &size);
-  if (SCM_NULLP (args_and_flags))
+
+  if (!scm_is_number (fam_or_sockaddr))
+    {
+      /* FAM_OR_SOCKADDR must actually be a `socket address' object.  This
+        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 (!scm_is_eq (address, SCM_UNDEFINED))
+       args_and_flags = scm_cons (address, args_and_flags);
+    }
+  else
+    soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
+                             &args_and_flags, 3, FUNC_NAME, &size);
+
+  if (scm_is_null (args_and_flags))
     flg = 0;
   else
     {
       SCM_VALIDATE_CONS (5, args_and_flags);
       flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
     }
-  SCM_SYSCALL (rv = sendto (fd, SCM_STRING_CHARS (message),
-                           SCM_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;
@@ -1324,7 +1675,9 @@ SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
       SCM_SYSERROR;
     }
   free (soka);
-  return SCM_MAKINUM (rv);
+
+  scm_remember_upto_here_1 (message);
+  return scm_from_int (rv);
 }
 #undef FUNC_NAME
 \f
@@ -1335,127 +1688,159 @@ scm_init_socket ()
 {
   /* protocol families.  */
 #ifdef AF_UNSPEC
-  scm_c_define ("AF_UNSPEC", SCM_MAKINUM (AF_UNSPEC));
+  scm_c_define ("AF_UNSPEC", scm_from_int (AF_UNSPEC));
 #endif
-#ifdef AF_UNIX
-  scm_c_define ("AF_UNIX", SCM_MAKINUM (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
-  scm_c_define ("AF_INET", SCM_MAKINUM (AF_INET));
+  scm_c_define ("AF_INET", scm_from_int (AF_INET));
 #endif
 #ifdef AF_INET6
-  scm_c_define ("AF_INET6", SCM_MAKINUM (AF_INET6));
+  scm_c_define ("AF_INET6", scm_from_int (AF_INET6));
 #endif
 
 #ifdef PF_UNSPEC
-  scm_c_define ("PF_UNSPEC", SCM_MAKINUM (PF_UNSPEC));
+  scm_c_define ("PF_UNSPEC", scm_from_int (PF_UNSPEC));
 #endif
 #ifdef PF_UNIX
-  scm_c_define ("PF_UNIX", SCM_MAKINUM (PF_UNIX));
+  scm_c_define ("PF_UNIX", scm_from_int (PF_UNIX));
 #endif
 #ifdef PF_INET
-  scm_c_define ("PF_INET", SCM_MAKINUM (PF_INET));
+  scm_c_define ("PF_INET", scm_from_int (PF_INET));
 #endif
 #ifdef PF_INET6
-  scm_c_define ("PF_INET6", SCM_MAKINUM (PF_INET6));
+  scm_c_define ("PF_INET6", scm_from_int (PF_INET6));
 #endif
 
   /* standard addresses.  */
 #ifdef INADDR_ANY
-  scm_c_define ("INADDR_ANY", scm_ulong2num (INADDR_ANY));
+  scm_c_define ("INADDR_ANY", scm_from_ulong (INADDR_ANY));
 #endif
 #ifdef INADDR_BROADCAST
-  scm_c_define ("INADDR_BROADCAST", scm_ulong2num (INADDR_BROADCAST));
+  scm_c_define ("INADDR_BROADCAST", scm_from_ulong (INADDR_BROADCAST));
 #endif
 #ifdef INADDR_NONE
-  scm_c_define ("INADDR_NONE", scm_ulong2num (INADDR_NONE));
+  scm_c_define ("INADDR_NONE", scm_from_ulong (INADDR_NONE));
 #endif
 #ifdef INADDR_LOOPBACK
-  scm_c_define ("INADDR_LOOPBACK", scm_ulong2num (INADDR_LOOPBACK));
+  scm_c_define ("INADDR_LOOPBACK", scm_from_ulong (INADDR_LOOPBACK));
 #endif
 
-  /* socket types.  */
+  /* socket types.
+
+     SOCK_PACKET is deliberately omitted, the GNU/Linux socket(2) and
+     packet(7) advise that it's obsolete and strongly deprecated.  */
+
 #ifdef SOCK_STREAM
-  scm_c_define ("SOCK_STREAM", SCM_MAKINUM (SOCK_STREAM));
+  scm_c_define ("SOCK_STREAM", scm_from_int (SOCK_STREAM));
 #endif
 #ifdef SOCK_DGRAM
-  scm_c_define ("SOCK_DGRAM", SCM_MAKINUM (SOCK_DGRAM));
+  scm_c_define ("SOCK_DGRAM", scm_from_int (SOCK_DGRAM));
+#endif
+#ifdef SOCK_SEQPACKET
+  scm_c_define ("SOCK_SEQPACKET", scm_from_int (SOCK_SEQPACKET));
 #endif
 #ifdef SOCK_RAW
-  scm_c_define ("SOCK_RAW", SCM_MAKINUM (SOCK_RAW));
+  scm_c_define ("SOCK_RAW", scm_from_int (SOCK_RAW));
+#endif
+#ifdef SOCK_RDM
+  scm_c_define ("SOCK_RDM", scm_from_int (SOCK_RDM));
 #endif
 
-  /* setsockopt level.  */
+  /* setsockopt level.
+
+     SOL_IP, SOL_TCP and SOL_UDP are defined on gnu/linux, but not on for
+     instance NetBSD.  We define IPPROTOs because that's what the posix spec
+     shows in its example at
+
+     http://www.opengroup.org/onlinepubs/007904975/functions/getsockopt.html
+  */
 #ifdef SOL_SOCKET
-  scm_c_define ("SOL_SOCKET", SCM_MAKINUM (SOL_SOCKET));
+  scm_c_define ("SOL_SOCKET", scm_from_int (SOL_SOCKET));
 #endif
-#ifdef SOL_IP
-  scm_c_define ("SOL_IP", SCM_MAKINUM (SOL_IP));
+#ifdef IPPROTO_IP
+  scm_c_define ("IPPROTO_IP", scm_from_int (IPPROTO_IP));
 #endif
-#ifdef SOL_TCP
-  scm_c_define ("SOL_TCP", SCM_MAKINUM (SOL_TCP));
+#ifdef IPPROTO_TCP
+  scm_c_define ("IPPROTO_TCP", scm_from_int (IPPROTO_TCP));
 #endif
-#ifdef SOL_UDP
-  scm_c_define ("SOL_UDP", SCM_MAKINUM (SOL_UDP));
+#ifdef IPPROTO_UDP
+  scm_c_define ("IPPROTO_UDP", scm_from_int (IPPROTO_UDP));
 #endif
 
   /* setsockopt names.  */
 #ifdef SO_DEBUG
-  scm_c_define ("SO_DEBUG", SCM_MAKINUM (SO_DEBUG));
+  scm_c_define ("SO_DEBUG", scm_from_int (SO_DEBUG));
 #endif
 #ifdef SO_REUSEADDR
-  scm_c_define ("SO_REUSEADDR", SCM_MAKINUM (SO_REUSEADDR));
+  scm_c_define ("SO_REUSEADDR", scm_from_int (SO_REUSEADDR));
 #endif
 #ifdef SO_STYLE
-  scm_c_define ("SO_STYLE", SCM_MAKINUM (SO_STYLE));
+  scm_c_define ("SO_STYLE", scm_from_int (SO_STYLE));
 #endif
 #ifdef SO_TYPE
-  scm_c_define ("SO_TYPE", SCM_MAKINUM (SO_TYPE));
+  scm_c_define ("SO_TYPE", scm_from_int (SO_TYPE));
 #endif
 #ifdef SO_ERROR
-  scm_c_define ("SO_ERROR", SCM_MAKINUM (SO_ERROR));
+  scm_c_define ("SO_ERROR", scm_from_int (SO_ERROR));
 #endif
 #ifdef SO_DONTROUTE
-  scm_c_define ("SO_DONTROUTE", SCM_MAKINUM (SO_DONTROUTE));
+  scm_c_define ("SO_DONTROUTE", scm_from_int (SO_DONTROUTE));
 #endif
 #ifdef SO_BROADCAST
-  scm_c_define ("SO_BROADCAST", SCM_MAKINUM (SO_BROADCAST));
+  scm_c_define ("SO_BROADCAST", scm_from_int (SO_BROADCAST));
 #endif
 #ifdef SO_SNDBUF
-  scm_c_define ("SO_SNDBUF", SCM_MAKINUM (SO_SNDBUF));
+  scm_c_define ("SO_SNDBUF", scm_from_int (SO_SNDBUF));
 #endif
 #ifdef SO_RCVBUF
-  scm_c_define ("SO_RCVBUF", SCM_MAKINUM (SO_RCVBUF));
+  scm_c_define ("SO_RCVBUF", scm_from_int (SO_RCVBUF));
 #endif
 #ifdef SO_KEEPALIVE
-  scm_c_define ("SO_KEEPALIVE", SCM_MAKINUM (SO_KEEPALIVE));
+  scm_c_define ("SO_KEEPALIVE", scm_from_int (SO_KEEPALIVE));
 #endif
 #ifdef SO_OOBINLINE
-  scm_c_define ("SO_OOBINLINE", SCM_MAKINUM (SO_OOBINLINE));
+  scm_c_define ("SO_OOBINLINE", scm_from_int (SO_OOBINLINE));
 #endif
 #ifdef SO_NO_CHECK
-  scm_c_define ("SO_NO_CHECK", SCM_MAKINUM (SO_NO_CHECK));
+  scm_c_define ("SO_NO_CHECK", scm_from_int (SO_NO_CHECK));
 #endif
 #ifdef SO_PRIORITY
-  scm_c_define ("SO_PRIORITY", SCM_MAKINUM (SO_PRIORITY));
+  scm_c_define ("SO_PRIORITY", scm_from_int (SO_PRIORITY));
 #endif
 #ifdef SO_LINGER
-  scm_c_define ("SO_LINGER", SCM_MAKINUM (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
+  scm_c_define ("MSG_DONTWAIT", scm_from_int (MSG_DONTWAIT));
+#endif
 #ifdef MSG_OOB
-  scm_c_define ("MSG_OOB", SCM_MAKINUM (MSG_OOB));
+  scm_c_define ("MSG_OOB", scm_from_int (MSG_OOB));
 #endif
 #ifdef MSG_PEEK
-  scm_c_define ("MSG_PEEK", SCM_MAKINUM (MSG_PEEK));
+  scm_c_define ("MSG_PEEK", scm_from_int (MSG_PEEK));
 #endif
 #ifdef MSG_DONTROUTE
-  scm_c_define ("MSG_DONTROUTE", SCM_MAKINUM (MSG_DONTROUTE));
+  scm_c_define ("MSG_DONTROUTE", scm_from_int (MSG_DONTROUTE));
+#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 __MINGW32__
-  scm_i_init_socket_Win32 ();
+#ifdef IP_MULTICAST_IF 
+  scm_c_define ("IP_MULTICAST_IF", scm_from_int ( IP_MULTICAST_IF));
 #endif
 
   scm_add_feature ("socket");