Correct, update, improve and clean up a lot of docstrings in order to make
[bpt/guile.git] / libguile / socket.c
index 22a6012..f6b404b 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1996,1997,1998 Free Software Foundation, Inc.
+/*     Copyright (C) 1996,1997,1998, 2000, 2001 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 \f
 
-#include <stdio.h>
+#include <errno.h>
 
-#include "_scm.h"
-#include "unif.h"
-#include "feature.h"
-#include "fports.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 "scm_validate.h"
-#include "socket.h"
+#include "libguile/validate.h"
+#include "libguile/socket.h"
 
 #ifdef HAVE_STRING_H
 #include <string.h>
 #include <netdb.h>
 #include <arpa/inet.h>
 
+#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
 
-GUILE_PROC (scm_htons, "htons", 1, 0, 0, 
+SCM_DEFINE (scm_htons, "htons", 1, 0, 0, 
             (SCM in),
-"Returns a new integer from @var{value} by converting from host to
-network order. @var{value} must be within the range of a C unsigned
-short integer.")
+           "Return a new integer from @var{value} by converting from host\n"
+           "to network order. @var{value} must be within the range of a C\n"
+           "unsigned short integer.")
 #define FUNC_NAME s_scm_htons
 {
   unsigned short c_in;
 
-  SCM_VALIDATE_INT_COPY(1,in,c_in);
+  SCM_VALIDATE_INUM_COPY (1,in,c_in);
   if (c_in != SCM_INUM (in))
     SCM_OUT_OF_RANGE (1,in);
 
@@ -88,16 +99,16 @@ short integer.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_ntohs, "ntohs", 1, 0, 0, 
+SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0, 
             (SCM in),
-"Returns a new integer from @var{value} by converting from network to
-host order.  @var{value} must be within the range of a C unsigned short
-integer.")
+           "Return a new integer from @var{value} by converting from\n"
+           "network to host order.  @var{value} must be within the range of\n"
+           "a C unsigned short integer.")
 #define FUNC_NAME s_scm_ntohs
 {
   unsigned short c_in;
 
-  SCM_VALIDATE_INT_COPY(1,in,c_in);
+  SCM_VALIDATE_INUM_COPY (1,in,c_in);
   if (c_in != SCM_INUM (in))
     SCM_OUT_OF_RANGE (1,in);
 
@@ -105,11 +116,11 @@ integer.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_htonl, "htonl", 1, 0, 0, 
+SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0, 
             (SCM in),
-"Returns a new integer from @var{value} by converting from host to
-network order. @var{value} must be within the range of a C unsigned
-long integer.")
+           "Return a new integer from @var{value} by converting from host\n"
+           "to network order. @var{value} must be within the range of a C\n"
+           "unsigned long integer.")
 #define FUNC_NAME s_scm_htonl
 {
   unsigned long c_in = SCM_NUM2ULONG (1,in);
@@ -117,11 +128,11 @@ long integer.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_ntohl, "ntohl", 1, 0, 0, 
+SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0, 
             (SCM in),
-"Returns a new integer from @var{value} by converting from network to
-host order. @var{value} must be within the range of a C unsigned
-long integer.")
+           "Return a new integer from @var{value} by converting from\n"
+           "network to host order. @var{value} must be within the range of\n"
+           "a C unsigned long integer.")
 #define FUNC_NAME s_scm_ntohl
 {
   unsigned long c_in = SCM_NUM2ULONG (1,in);
@@ -131,226 +142,223 @@ long integer.")
 
 SCM_SYMBOL (sym_socket, "socket");
 
-static SCM
-scm_sock_fd_to_port (int fd, const char *proc)
-{
-  SCM result;
-  if (fd == -1)
-    scm_syserror (proc);
-  result = scm_fdes_to_port (fd, "r+0", sym_socket);
-  return result;
-}
-
+#define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
 
-#define SCM_SOCK_FD_TO_PORT(fd) (scm_sock_fd_to_port((fd),FUNC_NAME))
-
-GUILE_PROC (scm_socket, "socket", 3, 0, 0,
+SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
             (SCM family, SCM style, SCM proto),
-"Returns a new socket port of the type specified by @var{family}, @var{style}
-and @var{protocol}.  All three parameters are integers.  Typical values
-for @var{family} are the values of @code{AF_UNIX}
-and @code{AF_INET}.  Typical values for @var{style} are
-the values of @code{SOCK_STREAM}, @code{SOCK_DGRAM} and @code{SOCK_RAW}.
-
-@var{protocol} can be obtained from a protocol name using
-@code{getprotobyname}.  A value of
-zero specifies the default protocol, which is usually right.
-
-A single socket port cannot by used for communication until
-it has been connected to another socket.")
+           "Return a new socket port of the type specified by @var{family},\n"
+           "@var{style} and @var{protocol}.  All three parameters are\n"
+           "integers.  Typical values for @var{family} are the values of\n"
+           "@code{AF_UNIX} and @code{AF_INET}.  Typical values for\n"
+           "@var{style} are the values of @code{SOCK_STREAM},\n"
+           "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n"
+           "\n"
+           "@var{protocol} can be obtained from a protocol name using\n"
+           "@code{getprotobyname}.  A value of zero specifies the default\n"
+           "protocol, which is usually right.\n"
+           "\n"
+           "A single socket port cannot by used for communication until it\n"
+           "has been connected to another socket.")
 #define FUNC_NAME s_scm_socket
 {
   int fd;
-  SCM result;
 
-  SCM_VALIDATE_INT(1,family);
-  SCM_VALIDATE_INT(2,style);
-  SCM_VALIDATE_INT(3,proto);
+  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));
-  result = SCM_SOCK_FD_TO_PORT (fd);
-  return result;
+  if (fd == -1)
+    SCM_SYSERROR;
+  return SCM_SOCK_FD_TO_PORT (fd);
 }
 #undef FUNC_NAME
 
-
-
 #ifdef HAVE_SOCKETPAIR
-GUILE_PROC (scm_socketpair, "socketpair", 3, 0, 0,
+SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
             (SCM family, SCM style, SCM proto),
-"Returns a pair of connected (but unnamed) socket ports of the type specified
-by @var{family}, @var{style} and @var{protocol}.
-Many systems support only
-socket pairs of the @code{AF_UNIX} family.  Zero is likely to be
-the only meaningful value for @var{protocol}.")
+           "Return a pair of connected (but unnamed) socket ports of the\n"
+           "type specified by @var{family}, @var{style} and @var{protocol}.\n"
+           "Many systems support only socket pairs of the @code{AF_UNIX}\n"
+           "family.  Zero is likely to be the only meaningful value for\n"
+           "@var{protocol}.")
 #define FUNC_NAME s_scm_socketpair
 {
   int fam;
   int fd[2];
-  SCM a;
-  SCM b;
 
-  SCM_VALIDATE_INT(1,family);
-  SCM_VALIDATE_INT(2,style);
-  SCM_VALIDATE_INT(3,proto);
+  SCM_VALIDATE_INUM (1,family);
+  SCM_VALIDATE_INUM (2,style);
+  SCM_VALIDATE_INUM (3,proto);
 
   fam = SCM_INUM (family);
 
   if (socketpair (fam, SCM_INUM (style), SCM_INUM (proto), fd) == -1)
     SCM_SYSERROR;
 
-  a = SCM_SOCK_FD_TO_PORT(fd[0]);
-  b = SCM_SOCK_FD_TO_PORT(fd[1]);
-  return scm_cons (a, b);
+  return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
 }
 #undef FUNC_NAME
 #endif
 
-GUILE_PROC (scm_getsockopt, "getsockopt", 3, 0, 0,
+SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
             (SCM sock, SCM level, SCM optname),
-"Returns the value of a particular socket option for the socket
-port @var{socket}.  @var{level} is an integer code for type of option
-being requested, e.g., @code{SOL_SOCKET} for socket-level options.
-@var{optname} is an
-integer code for the option required and should be specified using one of
-the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.
-
-The returned value is typically an integer but @code{SO_LINGER} returns a
-pair of integers.")
+           "Return the value of a particular socket option for the socket\n"
+           "port @var{socket}.  @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.")
 #define FUNC_NAME s_scm_getsockopt
 {
   int fd;
-  int optlen;
+  /* 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 (scm_sizet)];
+  int optlen = sizeof (scm_sizet);
 #endif
   int ilevel;
   int ioptname;
 
-#ifdef HAVE_STRUCT_LINGER
-  optlen = (int) sizeof (struct linger);
-#else
-  optlen = (int) sizeof (scm_sizet);
-#endif
-
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_INT_COPY(2,level,ilevel);
-  SCM_VALIDATE_INT_COPY(3,optname,ioptname);
+  SCM_VALIDATE_OPFPORT (1, sock);
+  SCM_VALIDATE_INUM_COPY (2, level, ilevel);
+  SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
 
   fd = SCM_FPORT_FDES (sock);
   if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
     SCM_SYSERROR;
 
-#ifdef SO_LINGER
-  if (ilevel == SOL_SOCKET && ioptname == SO_LINGER)
+  if (ilevel == SOL_SOCKET)
     {
+#ifdef SO_LINGER
+      if (ioptname == SO_LINGER)
+       {
 #ifdef HAVE_STRUCT_LINGER
-      struct linger *ling = (struct linger *) optval;
-      return scm_cons (SCM_MAKINUM (ling->l_onoff),
-                      SCM_MAKINUM (ling->l_linger));
+         struct linger *ling = (struct linger *) optval;
+
+         return scm_cons (scm_long2num (ling->l_onoff),
+                          scm_long2num (ling->l_linger));
 #else
-      scm_sizet *ling = (scm_sizet *) optval;
-      return scm_cons (SCM_MAKINUM (*ling),
-                      SCM_MAKINUM (0));
+         return scm_cons (scm_long2num (*(int *) optval)
+                          SCM_MAKINUM (0));
 #endif
-    }
+       }
+      else
 #endif
+       if (0
 #ifdef SO_SNDBUF
-  if (ilevel == SOL_SOCKET && ioptname == SO_SNDBUF)
-    {
-      scm_sizet *bufsize = (scm_sizet *) optval;
-      return SCM_MAKINUM (*bufsize);
-    }
+           || ioptname == SO_SNDBUF
 #endif
 #ifdef SO_RCVBUF
-  if (ilevel == SOL_SOCKET && ioptname == SO_RCVBUF)
-    {
-      scm_sizet *bufsize = (scm_sizet *) optval;
-      return SCM_MAKINUM (*bufsize);
-    }
+           || ioptname == SO_RCVBUF
 #endif
-  return SCM_MAKINUM (*(int *) optval);
+           )
+         {
+           return scm_long2num (*(scm_sizet *) optval);
+         }
+    }
+  return scm_long2num (*(int *) optval);
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_setsockopt, "setsockopt", 4, 0, 0,
+SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
             (SCM sock, SCM level, SCM optname, SCM value),
-"Sets the value of a particular socket option for the socket
-port @var{socket}.  @var{level} is an integer code for type of option
-being set, e.g., @code{SOL_SOCKET} for socket-level options.
-@var{optname} is an
-integer code for the option to set and should be specified using one of
-the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.
-@var{value} is the value to which the option should be set.  For
-most options this must be an integer, but for @code{SO_LINGER} it must
-be a pair.
-
-The return value is unspecified.")
+           "Sets the value of a particular socket option for the socket\n"
+           "port @var{socket}.  @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.")
 #define FUNC_NAME s_scm_setsockopt
 {
   int fd;
-  int optlen;
+  int optlen = -1;
+  /* size of optval is the largest supported option.  */
 #ifdef HAVE_STRUCT_LINGER
-  char optval[sizeof (struct linger)]; /* Biggest option :-(  */
+  char optval[sizeof (struct linger)];
 #else
   char optval[sizeof (scm_sizet)];
 #endif
   int ilevel, ioptname;
+
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_INT_COPY(2,level,ilevel);
-  SCM_VALIDATE_INT_COPY(3,optname,ioptname);
+
+  SCM_VALIDATE_OPFPORT (1, sock);
+  SCM_VALIDATE_INUM_COPY (2, level, ilevel);
+  SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
+
   fd = SCM_FPORT_FDES (sock);
-  if (0);
-#ifdef SO_LINGER
-  else if (ilevel == SOL_SOCKET && ioptname == SO_LINGER)
+
+  if (ilevel == SOL_SOCKET)
     {
+#ifdef SO_LINGER
+      if (ioptname == SO_LINGER)
+       {
 #ifdef HAVE_STRUCT_LINGER
-      struct linger ling;
-      SCM_ASSERT (SCM_CONSP (value)
-                 && SCM_INUMP (SCM_CAR (value))
-                 &&  SCM_INUMP (SCM_CDR (value)),
-                 value, SCM_ARG4, FUNC_NAME);
-      ling.l_onoff = SCM_INUM (SCM_CAR (value));
-      ling.l_linger = SCM_INUM (SCM_CDR (value));
-      optlen = (int) sizeof (struct linger);
-      memcpy (optval, (void *) &ling, optlen);
+         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);
 #else
-      scm_sizet ling;
-      SCM_ASSERT (SCM_CONSP (value)
-                 && SCM_INUMP (SCM_CAR (value))
-                 &&  SCM_INUMP (SCM_CDR (value)),
-                 value, SCM_ARG4, FUNC_NAME);
-      ling = SCM_INUM (SCM_CAR (value));
-      optlen = (int) sizeof (scm_sizet);
-      (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
+         int ling;
+         long lv;
+
+         SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
+         /* 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;
 #endif
-    }
+       }
+      else
 #endif
+       if (0
 #ifdef SO_SNDBUF
-  else if (ilevel == SOL_SOCKET && ioptname == SO_SNDBUF)
-    {
-      SCM_VALIDATE_INT(4,value);
-      optlen = (int) sizeof (scm_sizet);
-      (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
-    }
+           || ioptname == SO_SNDBUF
 #endif
 #ifdef SO_RCVBUF
-  else if (ilevel == SOL_SOCKET && ioptname == SO_RCVBUF)
-    {
-      SCM_VALIDATE_INT(4,value);
-      optlen = (int) sizeof (scm_sizet);
-      (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
-    }
+           || ioptname == SO_RCVBUF
 #endif
-  else
+           )
+         {
+           long lv = SCM_NUM2LONG (4, value);
+
+           optlen = (int) sizeof (scm_sizet);
+           (*(scm_sizet *) optval) = (scm_sizet) lv;
+         }
+    }
+  if (optlen == -1)
     {
-      /* Most options just take an int.  */
-      SCM_VALIDATE_INT(4,value);
+      /* Most options take an int.  */
+      long lv = SCM_NUM2LONG (4, value);
+      int val = (int) lv;
+
+      SCM_ASSERT_RANGE (SCM_ARG4, value, val == lv);
       optlen = (int) sizeof (int);
-      (*(int *) optval) = (int) SCM_INUM (value);
+      (*(int *) optval) = val;
     }
   if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1)
     SCM_SYSERROR;
@@ -358,31 +366,29 @@ The return value is unspecified.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_shutdown, "shutdown", 2, 0, 0,
+SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
           (SCM sock, SCM how),
-"Sockets can be closed simply by using @code{close-port}. The
-@code{shutdown} procedure allows reception or tranmission on a
-connection to be shut down individually, according to the parameter
-@var{how}:
-
-@table @asis
-@item 0
-Stop receiving data for this socket.  If further data arrives,  reject it.
-@item 1
-Stop trying to transmit data from this socket.  Discard any
-data waiting to be sent.  Stop looking for acknowledgement of
-data already sent; don't retransmit it if it is lost.
-@item 2
-Stop both reception and transmission.
-@end table
-
-The return value is unspecified.")
+           "Sockets can be closed simply by using @code{close-port}. The\n"
+           "@code{shutdown} procedure allows reception or tranmission on a\n"
+           "connection to be shut down individually, according to the parameter\n"
+           "@var{how}:\n\n"
+           "@table @asis\n"
+           "@item 0\n"
+           "Stop receiving data for this socket.  If further data arrives,  reject it.\n"
+           "@item 1\n"
+           "Stop trying to transmit data from this socket.  Discard any\n"
+           "data waiting to be sent.  Stop looking for acknowledgement of\n"
+           "data already sent; don't retransmit it if it is lost.\n"
+           "@item 2\n"
+           "Stop both reception and transmission.\n"
+           "@end table\n\n"
+           "The return value is unspecified.")
 #define FUNC_NAME s_scm_shutdown
 {
   int fd;
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_INT(2,how);
+  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)
@@ -398,9 +404,9 @@ The return value is unspecified.")
    proc is the name of the original procedure.
    size returns the size of the structure allocated.  */
 
-
 static struct sockaddr *
-scm_fill_sockaddr (int fam,SCM address,SCM *args,int which_arg,const char *proc,scm_sizet *size)
+scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
+                  const char *proc, int *size)
 {
   switch (fam)
     {
@@ -409,18 +415,19 @@ scm_fill_sockaddr (int fam,SCM address,SCM *args,int which_arg,const char *proc,
        SCM isport;
        struct sockaddr_in *soka;
 
-       soka = (struct sockaddr_in *)
-         scm_must_malloc (sizeof (struct sockaddr_in), proc);
+       SCM_ASSERT (SCM_CONSP (*args), *args, 
+                   which_arg + 1, proc);
+       isport = SCM_CAR (*args);
+       SCM_ASSERT (SCM_INUMP (isport), isport, which_arg + 1, proc);
+       soka = (struct sockaddr_in *) malloc (sizeof (struct sockaddr_in));
+       if (!soka)
+         scm_memory_error (proc);
        /* e.g., for BSDs which don't like invalid sin_len.  */
        memset (soka, 0, sizeof (struct sockaddr_in));
        soka->sin_family = AF_INET;
        soka->sin_addr.s_addr =
          htonl (scm_num2ulong (address, (char *) which_arg, proc));
-       SCM_ASSERT (SCM_CONSP (*args), *args, 
-                   which_arg + 1, proc);
-       isport = SCM_CAR (*args);
        *args = SCM_CDR (*args);
-       SCM_ASSERT (SCM_INUMP (isport), isport, which_arg + 1, proc);
        soka->sin_port = htons (SCM_INUM (isport));
        *size = sizeof (struct sockaddr_in);
        return (struct sockaddr *) soka;
@@ -429,16 +436,25 @@ scm_fill_sockaddr (int fam,SCM address,SCM *args,int which_arg,const char *proc,
     case AF_UNIX:
       {
        struct sockaddr_un *soka;
-
-       soka = (struct sockaddr_un *)
-         scm_must_malloc (sizeof (struct sockaddr_un), proc);
-       memset (soka, 0, sizeof (struct sockaddr_un));
+       int addr_size;
+
+       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
+          ignore it.  if the O/S doesn't like the size it will cause
+          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));
+       soka = (struct sockaddr_un *) 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;
-       SCM_ASSERT (SCM_ROSTRINGP (address), address,
-                   which_arg, proc);
-       memcpy (soka->sun_path, SCM_ROCHARS (address),
-               1 + SCM_ROLENGTH (address));
-       *size = sizeof (struct sockaddr_un);
+       memcpy (soka->sun_path, SCM_STRING_CHARS (address),
+               SCM_STRING_LENGTH (address));
+       *size = SUN_LEN (soka);
        return (struct sockaddr *) soka;
       }
 #endif
@@ -447,113 +463,112 @@ scm_fill_sockaddr (int fam,SCM address,SCM *args,int which_arg,const char *proc,
     }
 }
   
-GUILE_PROC (scm_connect, "connect", 3, 0, 1,
+SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
             (SCM sock, SCM fam, SCM address, SCM args),
-"Initiates a connection from @var{socket} to the address
-specified by @var{address} and possibly @var{arg @dots{}}.  The format
-required for @var{address}
-and @var{arg} @dots{} depends on the family of the socket.
-
-For a socket of family @code{AF_UNIX},
-only @code{address} is specified and must be a string with the
-filename where the socket is to be created.
-
-For a socket of family @code{AF_INET},
-@code{address} must be an integer Internet host address and @var{arg} @dots{}
-must be a single integer port number.
-
-The return value is unspecified.")
+           "Initiates a connection from @var{socket} to the address\n"
+           "specified by @var{address} and possibly @var{arg @dots{}}.  The format\n"
+           "required for @var{address}\n"
+           "and @var{arg} @dots{} depends on the family of the socket.\n\n"
+           "For a socket of family @code{AF_UNIX},\n"
+           "only @code{address} is specified and must be a string with the\n"
+           "filename where the socket is to be created.\n\n"
+           "For a socket of family @code{AF_INET},\n"
+           "@code{address} must be an integer Internet host address and @var{arg} @dots{}\n"
+           "must be a single integer port number.\n\n"
+           "The return value is unspecified.")
 #define FUNC_NAME s_scm_connect
 {
   int fd;
   struct sockaddr *soka;
-  scm_sizet size;
+  int size;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_INT(2,fam);
+  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);
+  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
+                           &size);
   if (connect (fd, soka, size) == -1)
-    SCM_SYSERROR;
-  scm_must_free ((char *) soka);
+    {
+      int save_errno = errno;
+      
+      free (soka);
+      errno = save_errno;
+      SCM_SYSERROR;
+    }
+  free (soka);
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_bind, "bind", 3, 0, 1,
+SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
             (SCM sock, SCM fam, SCM address, SCM args),
-"Assigns an address to the socket port @var{socket}.
-Generally this only needs to be done for server sockets,
-so they know where to look for incoming connections.  A socket
-without an address will be assigned one automatically when it
-starts communicating.
-
-The format of @var{address} and @var{ARG} @dots{} depends on the family
-of the socket.
-
-For a socket of family @code{AF_UNIX}, only @var{address}
-is specified and must 
-be a string with the filename where the socket is to be created.
-
-For a socket of family @code{AF_INET}, @var{address} must be an integer
-Internet host address and @var{arg} @dots{} must be a single integer
-port number.
-
-The values of the following variables can also be used for @var{address}:
-
-@defvar INADDR_ANY
-Allow connections from any address.
-@end defvar
-
-@defvar INADDR_LOOPBACK
-The address of the local host using the loopback device.
-@end defvar
-
-@defvar INADDR_BROADCAST
-The broadcast address on the local network.
-@end defvar
-
-@defvar INADDR_NONE
-No address.
-@end defvar
-
-The return value is unspecified.")
+           "Assigns an address to the socket port @var{socket}.\n"
+           "Generally this only needs to be done for server sockets,\n"
+           "so they know where to look for incoming connections.  A socket\n"
+           "without an address will be assigned one automatically when it\n"
+           "starts communicating.\n\n"
+           "The format of @var{address} and @var{ARG} @dots{} depends on the family\n"
+           "of the socket.\n\n"
+           "For a socket of family @code{AF_UNIX}, only @var{address}\n"
+           "is specified and must \n"
+           "be a string with the filename where the socket is to be created.\n\n"
+           "For a socket of family @code{AF_INET}, @var{address} must be an integer\n"
+           "Internet host address and @var{arg} @dots{} must be a single integer\n"
+           "port number.\n\n"
+           "The values of the following variables can also be used for @var{address}:\n\n"
+           "@defvar INADDR_ANY\n"
+           "Allow connections from any address.\n"
+           "@end defvar\n\n"
+           "@defvar INADDR_LOOPBACK\n"
+           "The address of the local host using the loopback device.\n"
+           "@end defvar\n\n"
+           "@defvar INADDR_BROADCAST\n"
+           "The broadcast address on the local network.\n"
+           "@end defvar\n\n"
+           "@defvar INADDR_NONE\n"
+           "No address.\n"
+           "@end defvar\n\n"
+           "The return value is unspecified.")
 #define FUNC_NAME s_scm_bind
 {
-  int rv;
   struct sockaddr *soka;
-  scm_sizet size;
+  int size;
   int fd;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_INT(2,fam);
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME, &size);
+  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);
-  rv = bind (fd, soka, size);
-  if (rv == -1)
+  if (bind (fd, soka, size) == -1)
+  {
+    int save_errno = errno;
+      
+    free (soka);
+    errno = save_errno;
     SCM_SYSERROR;
-  scm_must_free ((char *) soka);
+  }
+  free (soka);
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_listen, "listen", 2, 0, 0,
+SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
             (SCM sock, SCM backlog),
-"This procedure enables @var{socket} to accept connection
-requests.  @var{backlog} is an integer specifying
-the maximum length of the queue for pending connections.
-If the queue fills, new clients will fail to connect until the
-server calls @code{accept} to accept a connection from the queue.
-
-The return value is unspecified.")
+           "This procedure enables @var{socket} to accept connection\n"
+           "requests.  @var{backlog} is an integer specifying\n"
+           "the maximum length of the queue for pending connections.\n"
+           "If the queue fills, new clients will fail to connect until the\n"
+           "server calls @code{accept} to accept a connection from the queue.\n\n"
+           "The return value is unspecified.")
 #define FUNC_NAME s_scm_listen
 {
   int fd;
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_INT(2,backlog);
+  SCM_VALIDATE_OPFPORT (1,sock);
+  SCM_VALIDATE_INUM (2,backlog);
   fd = SCM_FPORT_FDES (sock);
   if (listen (fd, SCM_INUM (backlog)) == -1)
     SCM_SYSERROR;
@@ -562,18 +577,19 @@ The return value is unspecified.")
 #undef FUNC_NAME
 
 /* Put the components of a sockaddr into a new SCM vector.  */
-
 static SCM
-scm_addr_vector (struct sockaddr *address,const char *proc)
+scm_addr_vector (struct sockaddr *address, const char *proc)
 {
   short int fam = address->sa_family;
   SCM result;
   SCM *ve;
+
 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
   if (fam == AF_UNIX)
     {
       struct sockaddr_un *nad = (struct sockaddr_un *) address;
-      result = scm_make_vector (SCM_MAKINUM (2), SCM_UNSPECIFIED);
+
+      result = scm_c_make_vector (2, SCM_UNSPECIFIED);
       ve = SCM_VELTS (result);
       ve[0] = scm_ulong2num ((unsigned long) fam);
       ve[1] = scm_makfromstr (nad->sun_path,
@@ -584,70 +600,64 @@ scm_addr_vector (struct sockaddr *address,const char *proc)
   if (fam == AF_INET)
     {
       struct sockaddr_in *nad = (struct sockaddr_in *) address;
-      result = scm_make_vector (SCM_MAKINUM (3), SCM_UNSPECIFIED);
+
+      result = scm_c_make_vector (3, SCM_UNSPECIFIED);
       ve = SCM_VELTS (result);
       ve[0] = scm_ulong2num ((unsigned long) fam);
       ve[1] = scm_ulong2num (ntohl (nad->sin_addr.s_addr));
       ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin_port));
     }
   else
-    scm_misc_error (proc, "Unrecognised address family: %s",
-                   scm_listify (SCM_MAKINUM (fam), SCM_UNDEFINED));
+    scm_misc_error (proc, "Unrecognised address family: ~A",
+                   SCM_LIST1 (SCM_MAKINUM (fam)));
 
   return result;
 }
 
-/* Allocate a buffer large enough to hold any sockaddr type.  */
-static char *scm_addr_buffer;
-static int scm_addr_buffer_size;
+/* 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.  */
 
-static void
-scm_init_addr_buffer (void)
-{
-  scm_addr_buffer_size =
-#ifdef HAVE_UNIX_DOMAIN_SOCKETS
-  (int) sizeof (struct sockaddr_un)
+#if defined (HAVE_UNIX_DOMAIN_SOCKETS)
+#define MAX_SIZE_UN sizeof (struct sockaddr_un)
 #else
-  0
+#define MAX_SIZE_UN 0
 #endif
-  ;
-  if (sizeof (struct sockaddr_in) > scm_addr_buffer_size)
-    scm_addr_buffer_size = (int) sizeof (struct sockaddr_in);
-  scm_addr_buffer = scm_must_malloc (scm_addr_buffer_size, "address buffer");
-}
-
-GUILE_PROC (scm_accept, "accept", 1, 0, 0, 
-            (SCM sock),
-"Accepts a connection on a bound, listening socket @var{socket}.  If there
-are no pending connections in the queue, it waits until
-one is available unless the non-blocking option has been set on the
-socket.
 
-The return value is a
-pair in which the CAR is a new socket port for the connection and
-the CDR is an object with address information about the client which
-initiated the connection.
+#define MAX_ADDR_SIZE max (sizeof (struct sockaddr_in), MAX_SIZE_UN)
 
-If the address is not available then the CDR will be an empty vector.
-
-@var{socket} does not become part of the
-connection and will continue to accept new requests.")
+SCM_DEFINE (scm_accept, "accept", 1, 0, 0, 
+            (SCM sock),
+           "Accepts a connection on a bound, listening socket @var{socket}.  If there\n"
+           "are no pending connections in the queue, it waits until\n"
+           "one is available unless the non-blocking option has been set on the\n"
+           "socket.\n\n"
+           "The return value is a\n"
+           "pair in which the CAR is a new socket port for the connection and\n"
+           "the CDR is an object with address information about the client which\n"
+           "initiated the connection.\n\n"
+           "If the address is not available then the CDR will be an empty vector.\n\n"
+           "@var{socket} does not become part of the\n"
+           "connection and will continue to accept new requests.")
 #define FUNC_NAME s_scm_accept
 {
   int fd;
   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;
 
-  int tmp_size;
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
+  SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
-  tmp_size = scm_addr_buffer_size;
-  newfd = accept (fd, (struct sockaddr *) scm_addr_buffer, &tmp_size);
-  newsock = scm_sock_fd_to_port (newfd, FUNC_NAME);
-  if (tmp_size > 0)
-    address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
+  newfd = accept (fd, addr, &addr_size);
+  if (newfd == -1)
+    SCM_SYSERROR;
+  newsock = SCM_SOCK_FD_TO_PORT (newfd);
+  if (addr_size > 0)
+    address = scm_addr_vector (addr, FUNC_NAME);
   else
     address = SCM_BOOL_F;
   
@@ -655,84 +665,85 @@ connection and will continue to accept new requests.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_getsockname, "getsockname", 1, 0, 0, 
+SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0, 
             (SCM sock),
-"Returns the address of @var{socket}, in the same form as the object
-returned by @code{accept}.  On many systems the address of a socket
-in the @code{AF_FILE} namespace cannot be read.")
+           "Return the address of @var{socket}, in the same form as the\n"
+           "object returned by @code{accept}.  On many systems the address\n"
+           "of a socket in the @code{AF_FILE} namespace cannot be read.")
 #define FUNC_NAME s_scm_getsockname
 {
-  int tmp_size;
   int fd;
   SCM result;
+  int addr_size = MAX_ADDR_SIZE;
+  char max_addr[MAX_ADDR_SIZE];
+  struct sockaddr *addr = (struct sockaddr *) max_addr;
+
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
+  SCM_VALIDATE_OPFPORT (1,sock);
   fd = SCM_FPORT_FDES (sock);
-  tmp_size = scm_addr_buffer_size;
-  if (getsockname (fd, (struct sockaddr *) scm_addr_buffer, &tmp_size) == -1)
+  if (getsockname (fd, addr, &addr_size) == -1)
     SCM_SYSERROR;
-  if (tmp_size > 0)
-    result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
+  if (addr_size > 0)
+    result = scm_addr_vector (addr, FUNC_NAME);
   else
     result = SCM_BOOL_F;
   return result;
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_getpeername, "getpeername", 1, 0, 0, 
+SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0, 
             (SCM sock),
-"Returns the address of the socket that the socket @var{socket} is connected to,
-in the same form as the object
-returned by @code{accept}.  On many systems the address of a socket
-in the @code{AF_FILE} namespace cannot be read.")
+           "Return the address of the socket that the socket @var{socket}\n"
+           "is connected to, in the same form as the object returned by\n"
+           "@code{accept}.  On many systems the address of a socket in the\n"
+           "@code{AF_FILE} namespace cannot be read.")
 #define FUNC_NAME s_scm_getpeername
 {
-  int tmp_size;
   int fd;
   SCM result;
+  int addr_size = MAX_ADDR_SIZE;
+  char max_addr[MAX_ADDR_SIZE];
+  struct sockaddr *addr = (struct sockaddr *) max_addr;
+
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
+  SCM_VALIDATE_OPFPORT (1,sock);
   fd = SCM_FPORT_FDES (sock);
-  tmp_size = scm_addr_buffer_size;
-  if (getpeername (fd, (struct sockaddr *) scm_addr_buffer, &tmp_size) == -1)
+  if (getpeername (fd, addr, &addr_size) == -1)
     SCM_SYSERROR;
-  if (tmp_size > 0)
-    result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
+  if (addr_size > 0)
+    result = scm_addr_vector (addr, FUNC_NAME);
   else
     result = SCM_BOOL_F;
   return result;
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_recv, "recv!", 2, 1, 0,
+SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
             (SCM sock, SCM buf, SCM flags),
-"Receives data from the socket port @var{socket}.  @var{socket} must already
-be bound to the address from which data is to be received.
-@var{buf} is a string into which
-the data will be written.  The size of @var{buf} limits the amount of
-data which can be received: in the case of packet
-protocols, if a packet larger than this limit is encountered then some data
-will be irrevocably lost.
-
-The optional @var{flags} argument is a value or
-bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
-
-The value returned is the number of bytes read from the socket.
-
-Note that the data is read directly from the socket file descriptor:
-any unread buffered port data is ignored.")
+           "Receives data from the socket port @var{socket}.  @var{socket} must already\n"
+           "be bound to the address from which data is to be received.\n"
+           "@var{buf} is a string into which\n"
+           "the data will be written.  The size of @var{buf} limits 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 then some data\n"
+           "will be irrevocably lost.\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 socket.\n\n"
+           "Note that the data is read directly from the socket file descriptor:\n"
+           "any unread buffered port data is ignored.")
 #define FUNC_NAME s_scm_recv
 {
   int rv;
   int fd;
   int flg;
 
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_STRING(2,buf);
-  SCM_VALIDATE_INT_DEF_COPY(3,flags,0,flg);
+  SCM_VALIDATE_OPFPORT (1,sock);
+  SCM_VALIDATE_STRING (2,buf);
+  SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
   fd = SCM_FPORT_FDES (sock);
 
-  SCM_SYSCALL (rv = recv (fd, SCM_CHARS (buf), SCM_LENGTH (buf), flg));
+  SCM_SYSCALL (rv = recv (fd, SCM_STRING_CHARS (buf), SCM_STRING_LENGTH (buf), flg));
   if (rv == -1)
     SCM_SYSERROR;
 
@@ -740,17 +751,16 @@ any unread buffered port data is ignored.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_send, "send", 2, 1, 0,
+SCM_DEFINE (scm_send, "send", 2, 1, 0,
             (SCM sock, SCM message, SCM flags),
-"Transmits the string @var{message} on the socket port @var{socket}. 
-@var{socket} must already be bound to a destination address.  The
-value returned is the number of bytes transmitted -- it's possible for
-this to be less than the length of @var{message} if the socket is
-set to be non-blocking.  The optional @var{flags} argument is a value or
-bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
-
-Note that the data is written directly to the socket file descriptor:
-any unflushed buffered port data is ignored.")
+           "Transmits the string @var{message} on the socket port @var{socket}. \n"
+           "@var{socket} must already be bound to a destination address.  The\n"
+           "value returned is the number of bytes transmitted -- it's possible for\n"
+           "this to be less than the length of @var{message} if the socket is\n"
+           "set to be non-blocking.  The optional @var{flags} argument is a value or\n"
+           "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
+           "Note that the data is written directly to the socket file descriptor:\n"
+           "any unflushed buffered port data is ignored.")
 #define FUNC_NAME s_scm_send
 {
   int rv;
@@ -758,91 +768,73 @@ any unflushed buffered port data is ignored.")
   int flg;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_ROSTRING(2,message);
-  SCM_VALIDATE_INT_DEF_COPY(3,flags,0,flg);
+  SCM_VALIDATE_OPFPORT (1,sock);
+  SCM_VALIDATE_STRING (2, message);
+  SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
   fd = SCM_FPORT_FDES (sock);
 
-  SCM_SYSCALL (rv = send (fd, SCM_ROCHARS (message), SCM_ROLENGTH (message), flg));
+  SCM_SYSCALL (rv = send (fd, SCM_STRING_CHARS (message), SCM_STRING_LENGTH (message), flg));
   if (rv == -1)
     SCM_SYSERROR;
   return SCM_MAKINUM (rv);
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_recvfrom, "recvfrom!", 2, 3, 0,
-            (SCM sock, SCM buf, SCM flags, SCM start, SCM end),
-"Returns data from the socket port @var{socket} and also information about
-where the data was received from.  @var{socket} must already
-be bound to the address from which data is to be received.
-@code{buf}, is a string into which
-the data will be written.  The size of @var{buf} limits the amount of
-data which can be received: in the case of packet
-protocols, if a packet larger than this limit is encountered then some data
-will be irrevocably lost.
-
-The optional @var{flags} argument is a value or
-bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
-
-The value returned is a pair: the CAR is the number of bytes read from
-the socket and the CDR an address object in the same form as returned by
-@code{accept}.
-
-The @var{start} and @var{end} arguments specify a substring of @var{buf}
-to which the data should be written.
-
-Note that the data is read directly from the socket file descriptor:
-any unread buffered port data is ignored.")
+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{socket} and also\n"
+           "information about where the data was received from.\n"
+           "@var{socket} 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}.\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.")
 #define FUNC_NAME s_scm_recvfrom
 {
   int rv;
   int fd;
   int flg;
-  int offset = 0;
+  char *buf;
+  int offset;
   int cend;
-  int tmp_size;
   SCM address;
+  int addr_size = MAX_ADDR_SIZE;
+  char max_addr[MAX_ADDR_SIZE];
+  struct sockaddr *addr = (struct sockaddr *) max_addr;
 
-  SCM_VALIDATE_OPFPORT(1,sock);
-  SCM_VALIDATE_STRING(2,buf);
-  cend = SCM_LENGTH (buf);
-  
+  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
-    {
-      flg = SCM_NUM2ULONG (3,flags);
-
-      if (!SCM_UNBNDP (start))
-       {
-         offset = (int) SCM_NUM2LONG (4,start);
-         
-         if (offset < 0 || offset >= cend)
-           SCM_OUT_OF_RANGE (4, start);
-
-         if (!SCM_UNBNDP (end))
-           {
-             int tend = (int) SCM_NUM2LONG (5,end);
-      
-             if (tend <= offset || tend > cend)
-               SCM_OUT_OF_RANGE (5, end);
-
-             cend = tend;
-           }
-       }
-    }
+    SCM_VALIDATE_ULONG_COPY (3, flags, flg);
 
-  fd = SCM_FPORT_FDES (sock);
-
-  tmp_size = scm_addr_buffer_size;
-  SCM_SYSCALL (rv = recvfrom (fd, SCM_CHARS (buf) + offset,
+  /* 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,
-                             (struct sockaddr *) scm_addr_buffer,
-                             &tmp_size));
+                             addr, &addr_size));
   if (rv == -1)
     SCM_SYSERROR;
-  if (tmp_size > 0)
-    address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
+  if (addr_size > 0 && addr->sa_family != AF_UNSPEC)
+    address = scm_addr_vector (addr, FUNC_NAME);
   else
     address = SCM_BOOL_F;
 
@@ -850,32 +842,30 @@ any unread buffered port data is ignored.")
 }
 #undef FUNC_NAME
 
-GUILE_PROC (scm_sendto, "sendto", 4, 0, 1,
+SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
             (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags),
-"Transmits the string @var{message} on the socket port @var{socket}.  The
-destination address is specified using the @var{family}, @var{address} and
-@var{arg} arguments, in a similar way to the @code{connect}
-procedure.  The
-value returned is the number of bytes transmitted -- it's possible for
-this to be less than the length of @var{message} if the socket is
-set to be non-blocking.  The optional @var{flags} argument is a value or
-bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
-
-Note that the data is written directly to the socket file descriptor:
-any unflushed buffered port data is ignored.")
+           "Transmits the string @var{message} on the socket port @var{socket}.  The\n"
+           "destination address is specified using the @var{family}, @var{address} and\n"
+           "@var{arg} arguments, in a similar way to the @code{connect}\n"
+           "procedure.  The\n"
+           "value returned is the number of bytes transmitted -- it's possible for\n"
+           "this to be less than the length of @var{message} if the socket is\n"
+           "set to be non-blocking.  The optional @var{flags} argument is a value or\n"
+           "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
+           "Note that the data is written directly to the socket file descriptor:\n"
+           "any unflushed buffered port data is ignored.")
 #define FUNC_NAME s_scm_sendto
 {
   int rv;
   int fd;
   int flg;
   struct sockaddr *soka;
-  scm_sizet size;
-  int save_err;
+  int size;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_FPORT(1,sock);
-  SCM_VALIDATE_ROSTRING(2,message);
-  SCM_VALIDATE_INT(3,fam);
+  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);
@@ -883,16 +873,20 @@ any unflushed buffered port data is ignored.")
     flg = 0;
   else
     {
-      SCM_VALIDATE_NIMCONS(5,args_and_flags);
+      SCM_VALIDATE_CONS (5,args_and_flags);
       flg = SCM_NUM2ULONG (5,SCM_CAR (args_and_flags));
     }
-  SCM_SYSCALL (rv = sendto (fd, SCM_ROCHARS (message), SCM_ROLENGTH (message),
+  SCM_SYSCALL (rv = sendto (fd, SCM_STRING_CHARS (message),
+                           SCM_STRING_LENGTH (message),
                            flg, soka, size));
-  save_err = errno;
-  scm_must_free ((char *) soka);
-  errno = save_err;
   if (rv == -1)
-    SCM_SYSERROR;
+    {
+      int save_errno = errno;
+      free (soka);
+      errno = save_errno;
+      SCM_SYSERROR;
+    }
+  free (soka);
   return SCM_MAKINUM (rv);
 }
 #undef FUNC_NAME
@@ -1004,8 +998,15 @@ scm_init_socket ()
 #endif
 
   scm_add_feature ("socket");
-  scm_init_addr_buffer ();
 
-#include "socket.x"
+#ifndef SCM_MAGIC_SNARFER
+#include "libguile/socket.x"
+#endif
 }
 
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/