*** empty log message ***
[bpt/guile.git] / libguile / socket.c
index 935f315..09af232 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1996,1997 Free Software Foundation, Inc.
+/*     Copyright (C) 1996,1997,1998 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
  * If you write modifications of your own for GUILE, it is your choice
  * whether to permit this exception to apply to your modifications.
  * If you do not wish that, delete this exception notice.  */
+
+/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
+   gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
+
 \f
 
 #include <stdio.h>
@@ -47,6 +51,7 @@
 #include "feature.h"
 #include "fports.h"
 
+#include "validate.h"
 #include "socket.h"
 
 #ifdef HAVE_STRING_H
@@ -57,7 +62,7 @@
 #endif
 #include <sys/types.h>
 #include <sys/socket.h>
-#ifdef UNIX_DOMAIN_SOCKETS
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
 #include <sys/un.h>
 #endif
 #include <netinet/in.h>
 
 \f
 
-static SCM scm_sock_fd_to_port SCM_P ((int fd, char *proc));
+SCM_DEFINE (scm_htons, "htons", 1, 0, 0, 
+            (SCM in),
+           "Returns a new integer from @var{value} by converting from host to\n"
+           "network order. @var{value} must be within the range of a C unsigned\n"
+           "short integer.")
+#define FUNC_NAME s_scm_htons
+{
+  unsigned short c_in;
+
+  SCM_VALIDATE_INUM_COPY (1,in,c_in);
+  if (c_in != SCM_INUM (in))
+    SCM_OUT_OF_RANGE (1,in);
+
+  return SCM_MAKINUM (htons (c_in));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0, 
+            (SCM in),
+           "Returns a new integer from @var{value} by converting from network to\n"
+           "host order.  @var{value} must be within the range of a C unsigned short\n"
+           "integer.")
+#define FUNC_NAME s_scm_ntohs
+{
+  unsigned short c_in;
+
+  SCM_VALIDATE_INUM_COPY (1,in,c_in);
+  if (c_in != SCM_INUM (in))
+    SCM_OUT_OF_RANGE (1,in);
+
+  return SCM_MAKINUM (ntohs (c_in));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0, 
+            (SCM in),
+           "Returns a new integer from @var{value} by converting from host to\n"
+           "network order. @var{value} must be within the range of a C unsigned\n"
+           "long integer.")
+#define FUNC_NAME s_scm_htonl
+{
+  unsigned long c_in = SCM_NUM2ULONG (1,in);
+  return scm_ulong2num (htonl (c_in));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0, 
+            (SCM in),
+           "Returns a new integer from @var{value} by converting from network to\n"
+           "host order. @var{value} must be within the range of a C unsigned\n"
+           "long integer.")
+#define FUNC_NAME s_scm_ntohl
+{
+  unsigned long c_in = SCM_NUM2ULONG (1,in);
+  return scm_ulong2num (ntohl (c_in));
+}
+#undef FUNC_NAME
+
+SCM_SYMBOL (sym_socket, "socket");
 
 static SCM
-scm_sock_fd_to_port (fd, proc)
-     int fd;
-     char *proc;
+scm_sock_fd_to_port (int fd, const char *proc)
 {
   SCM result;
-  FILE *f;
-
   if (fd == -1)
     scm_syserror (proc);
-  f = fdopen (fd, "r+");
-  if (!f)
-    {
-      SCM_SYSCALL (close (fd));
-      scm_syserror (proc);
-    }
-  SCM_NEWCELL (result);
-  {
-    struct scm_port_table *pt = scm_add_to_port_table (result);
-
-    SCM_SETPTAB_ENTRY (result, pt);
-  }
-  SCM_SETCAR (result, scm_tc16_fport | scm_mode_bits ("r+0"));
-  SCM_SETSTREAM (result, (SCM)f);
-  scm_setbuf0 (result);
+  result = scm_fdes_to_port (fd, "r+0", sym_socket);
   return result;
 }
 
-SCM_PROC (s_socket, "socket", 3, 0, 0, scm_socket);
 
-SCM 
-scm_socket (family, style, proto)
-     SCM family;
-     SCM style;
-     SCM proto;
+#define SCM_SOCK_FD_TO_PORT(fd) (scm_sock_fd_to_port((fd),FUNC_NAME))
+
+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}\n"
+           "and @var{protocol}.  All three parameters are integers.  Typical values\n"
+           "for @var{family} are the values of @code{AF_UNIX}\n"
+           "and @code{AF_INET}.  Typical values for @var{style} are\n"
+           "the values of @code{SOCK_STREAM}, @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\n"
+           "zero specifies the default protocol, which is usually right.\n\n"
+           "A single socket port cannot by used for communication until\n"
+           "it has been connected to another socket.")
+#define FUNC_NAME s_scm_socket
 {
   int fd;
   SCM result;
 
-  SCM_ASSERT (SCM_INUMP (family), family, SCM_ARG1, s_socket);
-  SCM_ASSERT (SCM_INUMP (style), style, SCM_ARG2, s_socket);
-  SCM_ASSERT (SCM_INUMP (proto), proto, SCM_ARG3, s_socket);
-  SCM_DEFER_INTS;
+  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, s_socket);
-  SCM_ALLOW_INTS;
+  result = SCM_SOCK_FD_TO_PORT (fd);
   return result;
 }
+#undef FUNC_NAME
 
 
 
 #ifdef HAVE_SOCKETPAIR
-SCM_PROC (s_socketpair, "socketpair", 3, 0, 0, scm_socketpair);
-
-SCM 
-scm_socketpair (family, style, proto)
-     SCM family;
-     SCM style;
-     SCM proto;
+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\n"
+           "by @var{family}, @var{style} and @var{protocol}.\n"
+           "Many systems support only\n"
+           "socket pairs of the @code{AF_UNIX} family.  Zero is likely to be\n"
+           "the only meaningful value for @var{protocol}.")
+#define FUNC_NAME s_scm_socketpair
 {
   int fam;
   int fd[2];
   SCM a;
   SCM b;
 
-  SCM_ASSERT (SCM_INUMP (family), family, SCM_ARG1, s_socketpair);
-  SCM_ASSERT (SCM_INUMP (style), style, SCM_ARG2, s_socketpair);
-  SCM_ASSERT (SCM_INUMP (proto), proto, SCM_ARG3, s_socketpair);
+  SCM_VALIDATE_INUM (1,family);
+  SCM_VALIDATE_INUM (2,style);
+  SCM_VALIDATE_INUM (3,proto);
 
   fam = SCM_INUM (family);
 
-  SCM_DEFER_INTS;
   if (socketpair (fam, SCM_INUM (style), SCM_INUM (proto), fd) == -1)
-    scm_syserror (s_socketpair);
+    SCM_SYSERROR;
 
-  a = scm_sock_fd_to_port (fd[0], s_socketpair);
-  b = scm_sock_fd_to_port (fd[1], s_socketpair);
-  SCM_ALLOW_INTS;
+  a = SCM_SOCK_FD_TO_PORT(fd[0]);
+  b = SCM_SOCK_FD_TO_PORT(fd[1]);
   return scm_cons (a, b);
 }
-#endif
-
-SCM_PROC (s_getsockopt, "getsockopt", 3, 0, 0, scm_getsockopt);
-
-SCM
-scm_getsockopt (sock, level, optname)
-     SCM sock;
-     SCM level;
-     SCM optname;
+#undef FUNC_NAME
+#endif
+
+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\n"
+           "port @var{socket}.  @var{level} is an integer code for type of option\n"
+           "being requested, e.g., @code{SOL_SOCKET} for socket-level options.\n"
+           "@var{optname} is an\n"
+           "integer code for the option required and should be specified using one of\n"
+           "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n"
+           "The returned value is typically an integer but @code{SO_LINGER} returns a\n"
+           "pair of integers.")
+#define FUNC_NAME s_scm_getsockopt
 {
   int fd;
   int optlen;
@@ -174,16 +231,14 @@ scm_getsockopt (sock, level, optname)
   optlen = (int) sizeof (scm_sizet);
 #endif
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1,
-             s_getsockopt);
-  SCM_ASSERT (SCM_INUMP (level), level, SCM_ARG2, s_getsockopt);
-  SCM_ASSERT (SCM_INUMP (optname), optname, SCM_ARG3, s_getsockopt);
+  sock = SCM_COERCE_OUTPORT (sock);
+  SCM_VALIDATE_OPFPORT (1,sock);
+  SCM_VALIDATE_INUM_COPY (2,level,ilevel);
+  SCM_VALIDATE_INUM_COPY (3,optname,ioptname);
 
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  ilevel = SCM_INUM (level);
-  ioptname = SCM_INUM (optname);
+  fd = SCM_FPORT_FDES (sock);
   if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
-    scm_syserror (s_getsockopt);
+    SCM_SYSERROR;
 
 #ifdef SO_LINGER
   if (ilevel == SOL_SOCKET && ioptname == SO_LINGER)
@@ -215,15 +270,21 @@ scm_getsockopt (sock, level, optname)
 #endif
   return SCM_MAKINUM (*(int *) optval);
 }
-
-SCM_PROC (s_setsockopt, "setsockopt", 4, 0, 0, scm_setsockopt);
-
-SCM
-scm_setsockopt (sock, level, optname, value)
-     SCM sock;
-     SCM level;
-     SCM optname;
-     SCM value;
+#undef FUNC_NAME
+
+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\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;
@@ -233,33 +294,31 @@ scm_setsockopt (sock, level, optname, value)
   char optval[sizeof (scm_sizet)];
 #endif
   int ilevel, ioptname;
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1,
-             s_setsockopt);
-  SCM_ASSERT (SCM_INUMP (level), level, SCM_ARG2, s_setsockopt);
-  SCM_ASSERT (SCM_INUMP (optname), optname, SCM_ARG3, s_setsockopt);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  ilevel = SCM_INUM (level);
-  ioptname = SCM_INUM (optname);
+  sock = SCM_COERCE_OUTPORT (sock);
+  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)
     {
 #ifdef HAVE_STRUCT_LINGER
       struct linger ling;
-      SCM_ASSERT (SCM_NIMP (value) && SCM_CONSP (value)
+      SCM_ASSERT (SCM_CONSP (value)
                  && SCM_INUMP (SCM_CAR (value))
                  &&  SCM_INUMP (SCM_CDR (value)),
-                 value, SCM_ARG4, s_setsockopt);
+                 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);
 #else
       scm_sizet ling;
-      SCM_ASSERT (SCM_NIMP (value) && SCM_CONSP (value)
+      SCM_ASSERT (SCM_CONSP (value)
                  && SCM_INUMP (SCM_CAR (value))
                  &&  SCM_INUMP (SCM_CDR (value)),
-                 value, SCM_ARG4, s_setsockopt);
+                 value, SCM_ARG4, FUNC_NAME);
       ling = SCM_INUM (SCM_CAR (value));
       optlen = (int) sizeof (scm_sizet);
       (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
@@ -269,7 +328,7 @@ scm_setsockopt (sock, level, optname, value)
 #ifdef SO_SNDBUF
   else if (ilevel == SOL_SOCKET && ioptname == SO_SNDBUF)
     {
-      SCM_ASSERT (SCM_INUMP (value), value, SCM_ARG4, s_setsockopt);
+      SCM_VALIDATE_INUM (4,value);
       optlen = (int) sizeof (scm_sizet);
       (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
     }
@@ -277,7 +336,7 @@ scm_setsockopt (sock, level, optname, value)
 #ifdef SO_RCVBUF
   else if (ilevel == SOL_SOCKET && ioptname == SO_RCVBUF)
     {
-      SCM_ASSERT (SCM_INUMP (value), value, SCM_ARG4, s_setsockopt);
+      SCM_VALIDATE_INUM (4,value);
       optlen = (int) sizeof (scm_sizet);
       (*(scm_sizet *) optval) = (scm_sizet) SCM_INUM (value);
     }
@@ -285,32 +344,46 @@ scm_setsockopt (sock, level, optname, value)
   else
     {
       /* Most options just take an int.  */
-      SCM_ASSERT (SCM_INUMP (value), value, SCM_ARG4, s_setsockopt);
+      SCM_VALIDATE_INUM (4,value);
       optlen = (int) sizeof (int);
       (*(int *) optval) = (int) SCM_INUM (value);
     }
   if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1)
-    scm_syserror (s_setsockopt);
+    SCM_SYSERROR;
   return SCM_UNSPECIFIED;
 }
-
-SCM_PROC (s_shutdown, "shutdown", 2, 0, 0, scm_shutdown);
-
-SCM 
-scm_shutdown (sock, how)
-     SCM sock;
-     SCM how;
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
+          (SCM sock, SCM how),
+           "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;
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1,
-             s_shutdown);
-  SCM_ASSERT (SCM_INUMP (how) && 0 <= SCM_INUM (how) && 2 >= SCM_INUM (how),
-         how, SCM_ARG2, s_shutdown);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
+  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)
-    scm_syserror (s_shutdown);
+    SCM_SYSERROR;
   return SCM_UNSPECIFIED;
 }
+#undef FUNC_NAME
 
 /* convert fam/address/args into a sockaddr of the appropriate type.
    args is modified by removing the arguments actually used.
@@ -320,16 +393,8 @@ scm_shutdown (sock, how)
    size returns the size of the structure allocated.  */
 
 
-static struct sockaddr * scm_fill_sockaddr SCM_P ((int fam, SCM address, SCM *args, int which_arg, char *proc, scm_sizet *size));
-
 static struct sockaddr *
-scm_fill_sockaddr (fam, address, args, which_arg, proc, size)
-     int fam;
-     SCM address;
-     SCM *args;
-     int which_arg;
-     char *proc;
-     scm_sizet *size;
+scm_fill_sockaddr (int fam,SCM address,SCM *args,int which_arg,const char *proc,scm_sizet *size)
 {
   switch (fam)
     {
@@ -340,10 +405,12 @@ scm_fill_sockaddr (fam, address, args, which_arg, proc, size)
 
        soka = (struct sockaddr_in *)
          scm_must_malloc (sizeof (struct sockaddr_in), 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_NIMP (*args) && SCM_CONSP (*args), *args, 
+       SCM_ASSERT (SCM_CONSP (*args), *args, 
                    which_arg + 1, proc);
        isport = SCM_CAR (*args);
        *args = SCM_CDR (*args);
@@ -352,15 +419,16 @@ scm_fill_sockaddr (fam, address, args, which_arg, proc, size)
        *size = sizeof (struct sockaddr_in);
        return (struct sockaddr *) soka;
       }
-#ifdef UNIX_DOMAIN_SOCKETS
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
     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));
        soka->sun_family = AF_UNIX;
-       SCM_ASSERT (SCM_NIMP (address) && SCM_ROSTRINGP (address), address,
+       SCM_ASSERT (SCM_ROSTRINGP (address), address,
                    which_arg, proc);
        memcpy (soka->sun_path, SCM_ROCHARS (address),
                1 + SCM_ROLENGTH (address));
@@ -373,89 +441,120 @@ scm_fill_sockaddr (fam, address, args, which_arg, proc, size)
     }
 }
   
-SCM_PROC (s_connect, "connect", 3, 0, 1, scm_connect);
-
-SCM 
-scm_connect (sock, fam, address, args)
-
-     SCM sock;
-     SCM fam;
-     SCM address;
-     SCM args;
+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\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;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_connect);
-  SCM_ASSERT (SCM_INUMP (fam), fam, SCM_ARG2, s_connect);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  SCM_DEFER_INTS;
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, s_connect, &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 (connect (fd, soka, size) == -1)
-    scm_syserror (s_connect);
+    SCM_SYSERROR;
   scm_must_free ((char *) soka);
-  SCM_ALLOW_INTS;
   return SCM_UNSPECIFIED;
 }
-
-SCM_PROC (s_bind, "bind", 3, 0, 1, scm_bind);
-
-SCM 
-scm_bind (sock, fam, address, args)
-     SCM sock;
-     SCM fam;
-     SCM address;
-     SCM args;
+#undef FUNC_NAME
+
+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}.\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 fd;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_bind);
-  SCM_ASSERT (SCM_INUMP (fam), fam, SCM_ARG2, s_bind);
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, s_bind, &size);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
+  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);
   rv = bind (fd, soka, size);
   if (rv == -1)
-    scm_syserror (s_bind);
+    SCM_SYSERROR;
+  scm_must_free ((char *) soka);
   return SCM_UNSPECIFIED;
 }
-
-SCM_PROC (s_listen, "listen", 2, 0, 0, scm_listen);
-
-SCM 
-scm_listen (sock, backlog)
-     SCM sock;
-     SCM backlog;
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
+            (SCM sock, SCM backlog),
+           "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;
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_listen);
-  SCM_ASSERT (SCM_INUMP (backlog), backlog, SCM_ARG2, s_listen);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
+  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)
-    scm_syserror (s_listen);
+    SCM_SYSERROR;
   return SCM_UNSPECIFIED;
 }
+#undef FUNC_NAME
 
 /* Put the components of a sockaddr into a new SCM vector.  */
 
-static SCM scm_addr_vector SCM_P ((struct sockaddr *address, char *proc));
-
 static SCM
-scm_addr_vector (address, proc)
-     struct sockaddr *address;
-     char *proc;
+scm_addr_vector (struct sockaddr *address,const char *proc)
 {
   short int fam = address->sa_family;
   SCM result;
   SCM *ve;
-#ifdef UNIX_DOMAIN_SOCKETS
+#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, SCM_BOOL_F);
+      result = scm_make_vector (SCM_MAKINUM (2), SCM_UNSPECIFIED);
       ve = SCM_VELTS (result);
       ve[0] = scm_ulong2num ((unsigned long) fam);
       ve[1] = scm_makfromstr (nad->sun_path,
@@ -466,14 +565,14 @@ scm_addr_vector (address, proc)
   if (fam == AF_INET)
     {
       struct sockaddr_in *nad = (struct sockaddr_in *) address;
-      result = scm_make_vector (SCM_MAKINUM (3), SCM_UNSPECIFIED, SCM_BOOL_F);
+      result = scm_make_vector (SCM_MAKINUM (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_misc_error (proc, "Unrecognised address family: ~A",
                    scm_listify (SCM_MAKINUM (fam), SCM_UNDEFINED));
 
   return result;
@@ -483,13 +582,11 @@ scm_addr_vector (address, proc)
 static char *scm_addr_buffer;
 static int scm_addr_buffer_size;
 
-static void scm_init_addr_buffer SCM_P ((void));
-
 static void
-scm_init_addr_buffer ()
+scm_init_addr_buffer (void)
 {
   scm_addr_buffer_size =
-#ifdef UNIX_DOMAIN_SOCKETS
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
   (int) sizeof (struct sockaddr_un)
 #else
   0
@@ -500,11 +597,20 @@ scm_init_addr_buffer ()
   scm_addr_buffer = scm_must_malloc (scm_addr_buffer_size, "address buffer");
 }
 
-SCM_PROC (s_accept, "accept", 1, 0, 0, scm_accept);
-
-SCM 
-scm_accept (sock)
-     SCM sock;
+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;
@@ -512,131 +618,151 @@ scm_accept (sock)
   SCM newsock;
 
   int tmp_size;
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_accept);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  SCM_DEFER_INTS;
+  sock = SCM_COERCE_OUTPORT (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, s_accept);
+  newsock = scm_sock_fd_to_port (newfd, FUNC_NAME);
   if (tmp_size > 0)
-    address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, s_accept);
+    address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
   else
     address = SCM_BOOL_F;
   
-  SCM_ALLOW_INTS;
   return scm_cons (newsock, address);
 }
-
-SCM_PROC (s_getsockname, "getsockname", 1, 0, 0, scm_getsockname);
-
-SCM 
-scm_getsockname (sock)
-     SCM sock;
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0, 
+            (SCM sock),
+           "Returns the address of @var{socket}, in the same form as the object\n"
+           "returned by @code{accept}.  On many systems the address of a socket\n"
+           "in the @code{AF_FILE} namespace cannot be read.")
+#define FUNC_NAME s_scm_getsockname
 {
   int tmp_size;
   int fd;
   SCM result;
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_getsockname);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  SCM_DEFER_INTS;
+  sock = SCM_COERCE_OUTPORT (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)
-    scm_syserror (s_getsockname);
+    SCM_SYSERROR;
   if (tmp_size > 0)
-    result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, s_getsockname);
+    result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
   else
     result = SCM_BOOL_F;
-  SCM_ALLOW_INTS;
   return result;
 }
-
-SCM_PROC (s_getpeername, "getpeername", 1, 0, 0, scm_getpeername);
-
-SCM 
-scm_getpeername (sock)
-     SCM sock;
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0, 
+            (SCM sock),
+           "Returns the address of the socket that the socket @var{socket} is connected to,\n"
+           "in the same form as the object\n"
+           "returned by @code{accept}.  On many systems the address of a socket\n"
+           "in the @code{AF_FILE} namespace cannot be read.")
+#define FUNC_NAME s_scm_getpeername
 {
   int tmp_size;
   int fd;
   SCM result;
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_getpeername);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  SCM_DEFER_INTS;
+  sock = SCM_COERCE_OUTPORT (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)
-    scm_syserror (s_getpeername);
+    SCM_SYSERROR;
   if (tmp_size > 0)
-    result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, s_getpeername);
+    result = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
   else
     result = SCM_BOOL_F;
-  SCM_ALLOW_INTS;
   return result;
 }
-
-SCM_PROC (s_recv, "recv!", 2, 1, 0, scm_recv);
-
-SCM
-scm_recv (sock, buf, flags)
-     SCM sock;
-     SCM buf;
-     SCM flags;
+#undef FUNC_NAME
+
+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\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:any unread buffered port data is ignored.")
+#define FUNC_NAME s_scm_recv
 {
   int rv;
   int fd;
   int flg;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_recv);
-  SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf), buf, SCM_ARG2, s_recv);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-
-  if (SCM_UNBNDP (flags))
-    flg = 0;
-  else
-    flg = scm_num2ulong (flags, (char *) SCM_ARG3, s_recv);
+  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));
   if (rv == -1)
-    scm_syserror (s_recv);
+    SCM_SYSERROR;
 
   return SCM_MAKINUM (rv);
 }
-
-SCM_PROC (s_send, "send", 2, 1, 0, scm_send);
-
-SCM
-scm_send (sock, message, flags)
-     SCM sock;
-     SCM message;
-     SCM flags;
+#undef FUNC_NAME
+
+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}. \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;
   int fd;
   int flg;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_send);
-  SCM_ASSERT (SCM_NIMP (message) && SCM_ROSTRINGP (message), message, SCM_ARG2, s_send);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-
-  if (SCM_UNBNDP (flags))
-    flg = 0;
-  else
-    flg = scm_num2ulong (flags, (char *) SCM_ARG3, s_send);
+  sock = SCM_COERCE_OUTPORT (sock);
+  SCM_VALIDATE_OPFPORT (1,sock);
+  SCM_VALIDATE_ROSTRING (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));
   if (rv == -1)
-    scm_syserror (s_send);
+    SCM_SYSERROR;
   return SCM_MAKINUM (rv);
 }
-
-SCM_PROC (s_recvfrom, "recvfrom!", 2, 3, 0, scm_recvfrom);
-
-SCM
-scm_recvfrom (sock, buf, flags, start, end)
-     SCM sock;
-     SCM buf;
-     SCM flags;
-     SCM start;
-     SCM end;
+#undef FUNC_NAME
+
+SCM_DEFINE (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\n"
+           "where the data was received from.  @var{socket} must already\n"
+           "be bound to the address from which data is to be received.\n"
+           "@code{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 a pair: the CAR is the number of bytes read from\n"
+           "the socket and the CDR an address object in the same form as returned by\n"
+           "@code{accept}.\n\n"
+           "The @var{start} and @var{end} arguments specify a substring of @var{buf}\n"
+           "to which the data should be written.\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_recvfrom
 {
   int rv;
   int fd;
@@ -646,39 +772,36 @@ scm_recvfrom (sock, buf, flags, start, end)
   int tmp_size;
   SCM address;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1,
-             s_recvfrom);
-  SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf), buf, SCM_ARG2, s_recvfrom);
+  SCM_VALIDATE_OPFPORT (1,sock);
+  SCM_VALIDATE_STRING (2,buf);
   cend = SCM_LENGTH (buf);
   
   if (SCM_UNBNDP (flags))
     flg = 0;
   else
     {
-      flg = scm_num2ulong (flags, (char *) SCM_ARG3, s_recvfrom);
+      flg = SCM_NUM2ULONG (3,flags);
 
       if (!SCM_UNBNDP (start))
        {
-         offset = (int) scm_num2long (start,
-                                      (char *) SCM_ARG4, s_recvfrom);
+         offset = (int) SCM_NUM2LONG (4,start);
          
          if (offset < 0 || offset >= cend)
-           scm_out_of_range (s_recvfrom, start);
+           SCM_OUT_OF_RANGE (4, start);
 
          if (!SCM_UNBNDP (end))
            {
-             int tend = (int) scm_num2long (end,
-                                            (char *) SCM_ARG5, s_recvfrom);
+             int tend = (int) SCM_NUM2LONG (5,end);
       
              if (tend <= offset || tend > cend)
-               scm_out_of_range (s_recvfrom, end);
+               SCM_OUT_OF_RANGE (5, end);
 
              cend = tend;
            }
        }
     }
 
-  fd = fileno ((FILE *)SCM_STREAM (sock));
+  fd = SCM_FPORT_FDES (sock);
 
   tmp_size = scm_addr_buffer_size;
   SCM_SYSCALL (rv = recvfrom (fd, SCM_CHARS (buf) + offset,
@@ -686,55 +809,61 @@ scm_recvfrom (sock, buf, flags, start, end)
                              (struct sockaddr *) scm_addr_buffer,
                              &tmp_size));
   if (rv == -1)
-    scm_syserror (s_recvfrom);
+    SCM_SYSERROR;
   if (tmp_size > 0)
-    address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, s_recvfrom);
+    address = scm_addr_vector ((struct sockaddr *) scm_addr_buffer, FUNC_NAME);
   else
     address = SCM_BOOL_F;
 
   return scm_cons (SCM_MAKINUM (rv), address);
 }
-
-SCM_PROC (s_sendto, "sendto", 4, 0, 1, scm_sendto);
-
-SCM
-scm_sendto (sock, message, fam, address, args_and_flags)
-     SCM sock;
-     SCM message;
-     SCM fam;
-     SCM address;
-     SCM args_and_flags;
+#undef FUNC_NAME
+
+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\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;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_sendto);
-  SCM_ASSERT (SCM_NIMP (message) && SCM_ROSTRINGP (message), message,
-             SCM_ARG2, s_sendto);
-  SCM_ASSERT (SCM_INUMP (fam), fam, SCM_ARG3, s_sendto);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
-  SCM_DEFER_INTS;
+  sock = SCM_COERCE_OUTPORT (sock);
+  SCM_VALIDATE_FPORT (1,sock);
+  SCM_VALIDATE_ROSTRING (2,message);
+  SCM_VALIDATE_INUM (3,fam);
+  fd = SCM_FPORT_FDES (sock);
   soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args_and_flags, 4,
-                           s_sendto, &size);
+                           FUNC_NAME, &size);
   if (SCM_NULLP (args_and_flags))
     flg = 0;
   else
     {
-      SCM_ASSERT (SCM_NIMP (args_and_flags) && SCM_CONSP (args_and_flags),
-             args_and_flags, SCM_ARG5, s_sendto);
-      flg = scm_num2ulong (SCM_CAR (args_and_flags), (char *) SCM_ARG5, s_sendto);
+      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),
                            flg, soka, size));
-  if (rv == -1)
-    scm_syserror (s_sendto);
+  save_err = errno;
   scm_must_free ((char *) soka);
-  SCM_ALLOW_INTS;
+  errno = save_err;
+  if (rv == -1)
+    SCM_SYSERROR;
   return SCM_MAKINUM (rv);
 }
+#undef FUNC_NAME
 \f