* hashtab.h: Bugfix: use SCM_API (WAS: extern).
[bpt/guile.git] / libguile / socket.c
index 82d03fb..c268d5e 100644 (file)
@@ -1,50 +1,29 @@
-/* Copyright (C) 1996,1997,1998,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004 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
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * This program 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 General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
+ * 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.
  *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
- *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
+ * 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.
  *
- * 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.  */
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
 
-/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
-   gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
 
 \f
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <errno.h>
+#include <gmp.h>
 
 #include "libguile/_scm.h"
 #include "libguile/unif.h"
 #include "libguile/fports.h"
 #include "libguile/strings.h"
 #include "libguile/vectors.h"
+#include "libguile/dynwind.h"
 
 #include "libguile/validate.h"
 #include "libguile/socket.h"
 
+#ifdef __MINGW32__
+#include "win32-socket.h"
+#endif
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
                      + strlen ((ptr)->sun_path))
 #endif
 
-#if !defined (HAVE_UINT32_T)
-#if SIZEOF_INT == 4
-typedef unsigned int uint32_t;
-#elif SIZEOF_LONG == 4
-typedef unsigned long uint32_t;
-#else
-#error can not define uint32_t
-#endif
-#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, 
@@ -103,13 +76,7 @@ SCM_DEFINE (scm_htons, "htons", 1, 0, 0,
            "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);
-
-  return SCM_MAKINUM (htons (c_in));
+  return scm_from_ushort (htons (scm_to_ushort (value)));
 }
 #undef FUNC_NAME
 
@@ -120,13 +87,7 @@ SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0,
            "and returned as a new integer.")
 #define FUNC_NAME s_scm_ntohs
 {
-  unsigned short c_in;
-
-  SCM_VALIDATE_INUM_COPY (1, value, c_in);
-  if (c_in != SCM_INUM (value))
-    SCM_OUT_OF_RANGE (1, value);
-
-  return SCM_MAKINUM (ntohs (c_in));
+  return scm_from_ushort (ntohs (scm_to_ushort (value)));
 }
 #undef FUNC_NAME
 
@@ -137,9 +98,9 @@ SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0,
            "and returned as a new integer.")
 #define FUNC_NAME s_scm_htonl
 {
-  uint32_t c_in = SCM_NUM2ULONG (1, value);
+  scm_t_uint32 c_in = SCM_NUM2ULONG (1, value);
 
-  return scm_ulong2num (htonl (c_in));
+  return scm_from_ulong (htonl (c_in));
 }
 #undef FUNC_NAME
 
@@ -150,9 +111,9 @@ SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0,
            "and returned as a new integer.")
 #define FUNC_NAME s_scm_ntohl
 {
-  uint32_t c_in = SCM_NUM2ULONG (1, value);
+  scm_t_uint32 c_in = SCM_NUM2ULONG (1, value);
 
-  return scm_ulong2num (ntohl (c_in));
+  return scm_from_ulong (ntohl (c_in));
 }
 #undef FUNC_NAME
 
@@ -171,12 +132,15 @@ SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0,
 #define FUNC_NAME s_scm_inet_aton
 {
   struct in_addr soka;
+  char *c_address;
+  int rv;
 
-  SCM_VALIDATE_STRING (1, address);
-  SCM_STRING_COERCE_0TERMINATION_X (address);
-  if (inet_aton (SCM_STRING_CHARS (address), &soka) == 0)
+  c_address = scm_to_locale_string (address);
+  rv = inet_aton (c_address, &soka);
+  free (c_address);
+  if (rv == 0)
     SCM_MISC_ERROR ("bad address", SCM_EOL);
-  return scm_ulong2num (ntohl (soka.s_addr));
+  return scm_from_ulong (ntohl (soka.s_addr));
 }
 #undef FUNC_NAME
 
@@ -195,7 +159,7 @@ SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0,
   SCM answer;
   addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid));
   s = inet_ntoa (addr);
-  answer = scm_mem2string (s, strlen (s));
+  answer = scm_from_locale_string (s);
   return answer;
 }
 #undef FUNC_NAME
@@ -212,7 +176,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
@@ -230,7 +194,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
@@ -253,7 +217,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
@@ -270,7 +234,7 @@ SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
   \
   for (i = 0; i < 8; i++)\
     {\
-      char c = (addr)[i];\
+      scm_t_uint8 c = (addr)[i];\
       \
       (addr)[i] = (addr)[15 - i];\
       (addr)[15 - i] = c;\
@@ -278,78 +242,96 @@ SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
 }
 #endif
 
+#ifdef WORDS_BIGENDIAN
+#define FLIPCPY_NET_HOST_128(dest, src) memcpy (dest, src, 16)
+#else
+#define FLIPCPY_NET_HOST_128(dest, src) \
+{ \
+  const scm_t_uint8 *tmp_srcp = (src) + 15; \
+  scm_t_uint8 *tmp_destp = (dest); \
+  \
+  do { \
+    *tmp_destp++ = *tmp_srcp--; \
+  } while (tmp_srcp != (src)); \
+}
+#endif
+
+
+#if (SIZEOF_SCM_T_BITS * SCM_CHAR_BIT) > 128
+#error "Assumption that scm_t_bits <= 128 bits has been violated."
+#endif
+
+#if (SIZEOF_UNSIGNED_LONG * SCM_CHAR_BIT) > 128
+#error "Assumption that unsigned long <= 128 bits has been violated."
+#endif
+
+#if (SIZEOF_UNSIGNED_LONG_LONG * SCM_CHAR_BIT) > 128
+#error "Assumption that unsigned long long <= 128 bits has been violated."
+#endif
+
 /* convert a 128 bit IPv6 address in network order to a host ordered
    SCM integer.  */
-static SCM ipv6_net_to_num (const char *src)
+static SCM
+scm_from_ipv6 (const scm_t_uint8 *src)
 {
-  int big_digits = 128 / SCM_BITSPERDIG;
-  const int bytes_per_dig = SCM_BITSPERDIG / 8;
-  char addr[16];
-  char *ptr = addr;
-  SCM result;
-
-  memcpy (addr, src, 16);
-  /* get rid of leading zeros.  */ 
-  while (big_digits > 0)
-    {
-      long test = 0;
-
-      memcpy (&test, ptr, bytes_per_dig);
-      if (test != 0)
-       break;
-      ptr += bytes_per_dig;
-      big_digits--;
-    }
-  FLIP_NET_HOST_128 (addr);
-  if (big_digits * bytes_per_dig <= sizeof (unsigned long))
-    {
-      /* this is just so that we use INUM where possible.  */
-      unsigned long l_addr;
-
-      memcpy (&l_addr, addr, sizeof (unsigned long));
-      result = scm_ulong2num (l_addr);
-    }
-  else
-    {
-      result = scm_i_mkbig (big_digits, 0);
-      memcpy (SCM_BDIGITS (result), addr, big_digits * bytes_per_dig);
-    }
-  return 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, char *dst)
+static void
+scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
 {
-  if (SCM_INUMP (src))
+  if (SCM_I_INUMP (src))
     {
-      uint32_t addr = htonl (SCM_INUM (src));
-
-      memset (dst, 0, 12);
-      memcpy (dst + 12, &addr, 4);
+      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)),
+              &n,
+              sizeof (scm_t_signed_bits));
+#else
+      memset (dst + sizeof (scm_t_signed_bits),
+              0,
+              16 - sizeof (scm_t_signed_bits));
+      /* FIXME: this pair of ops is kinda wasteful -- should rewrite as
+         a single loop perhaps, similar to the handling of bignums. */
+      memcpy (dst, &n, sizeof (scm_t_signed_bits));
+      FLIP_NET_HOST_128 (dst);
+#endif
     }
-  else
+  else if (SCM_BIGP (src))
     {
+      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);
-      memcpy (dst, SCM_BDIGITS (src),
-             SCM_NUMDIGS (src) * (SCM_BITSPERDIG / 8));
-      FLIP_NET_HOST_128 (dst);
+      mpz_export (dst,
+                  &count,
+                  1, /* big-endian chunk ordering */
+                  16, /* chunks are 16 bytes long */
+                  1, /* big-endian byte ordering */
+                  0, /* "nails" -- leading unused bits per chunk */
+                  SCM_I_BIG_MPZ (src));
+      scm_remember_upto_here_1 (src);
     }
+  else
+    scm_wrong_type_arg (NULL, 0, src);
 }
 
-/* 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,\
-                       !SCM_BIGSIGN (address)\
-                       && (SCM_BITSPERDIG\
-                           * SCM_NUMDIGS (address) <= 128));\
-   }
-
 #ifdef HAVE_INET_PTON
 SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
             (SCM family, SCM address),
@@ -367,20 +349,23 @@ SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
   int af;
   char *src;
   char dst[16];
-  int rv;
+  int rv, eno;
 
-  SCM_VALIDATE_INUM_COPY (1, family, af);
+  af = scm_to_int (family);
   SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
-  SCM_VALIDATE_STRING_COPY (2, address, src);
+  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 (*(uint32_t *) dst));
+    return scm_from_ulong (ntohl (*(scm_t_uint32 *) dst));
   else
-    return ipv6_net_to_num ((char *) dst);
+    return scm_from_ipv6 ((char *) dst);
 }
 #undef FUNC_NAME
 #endif
@@ -407,18 +392,15 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
 #endif
   char addr6[16];
 
-  SCM_VALIDATE_INUM_COPY (1, family, af);
+  af = scm_to_int (family);
   SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
   if (af == AF_INET)
-    *(uint32_t *) addr6 = htonl (SCM_NUM2ULONG (2, address));
+    *(scm_t_uint32 *) addr6 = htonl (SCM_NUM2ULONG (2, address));
   else
-    {
-      VALIDATE_INET6 (2, address);
-      ipv6_num_to_net (address, addr6);
-    }
+    scm_to_ipv6 (addr6, address);
   if (inet_ntop (af, &addr6, dst, sizeof dst) == NULL)
     SCM_SYSERROR;
-  return scm_makfrom0str (dst);
+  return scm_from_locale_string (dst);
 }
 #undef FUNC_NAME
 #endif
@@ -446,10 +428,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);
@@ -469,13 +450,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_INUM (family);
+  fam = scm_to_int (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]));
@@ -509,8 +486,8 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
 
   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)
@@ -524,11 +501,11 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
 #ifdef HAVE_STRUCT_LINGER
          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
@@ -542,10 +519,10 @@ 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
 
@@ -564,55 +541,47 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
 #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
+
+#if 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
@@ -626,23 +595,34 @@ 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)
+
+#if 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;
 }
@@ -651,7 +631,7 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 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\n"
-           "@code{shutdown} procedure allows reception or tranmission on a\n"
+           "@code{shutdown} procedure allows reception or transmission on a\n"
            "connection to be shut down individually, according to the parameter\n"
            "@var{how}:\n\n"
            "@table @asis\n"
@@ -669,11 +649,9 @@ 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));
+  SCM_VALIDATE_OPFPORT (1, sock);
   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;
 }
@@ -701,14 +679,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 *) malloc (sizeof (struct sockaddr_in));
+       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
+#if HAVE_STRUCT_SOCKADDR_SIN_LEN
        soka->sin_len = sizeof (struct sockaddr_in);
 #endif
        soka->sin_family = AF_INET;
@@ -726,29 +702,28 @@ 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);
                *args = SCM_CDR (*args);
              }
          }
-       soka = (struct sockaddr_in6 *) malloc (sizeof (struct sockaddr_in6));
+       soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));
        if (!soka)
          scm_memory_error (proc);
-#ifdef SIN_LEN6
+#if 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
@@ -763,8 +738,13 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
       {
        struct sockaddr_un *soka;
        int addr_size;
+       char *c_address;
+
+       scm_frame_begin (0);
+
+       c_address = scm_to_locale_string (address);
+       scm_frame_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
@@ -772,20 +752,19 @@ 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));
-       soka = (struct sockaddr_un *) malloc (addr_size);
-       if (!soka)
-         scm_memory_error (proc);
+         + max (0, strlen (c_address) + 1 - (sizeof soka->sun_path));
+       soka = (struct sockaddr_un *) scm_malloc (addr_size);
        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_frame_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
@@ -816,10 +795,9 @@ SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
   int size;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT (1,sock);
-  SCM_VALIDATE_INUM (2,fam);
+  SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
-  soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
+  soka = scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_NAME,
                            &size);
   if (connect (fd, soka, size) == -1)
     {
@@ -877,8 +855,7 @@ SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
 
   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,
+  soka = scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_NAME,
                            &size);
   fd = SCM_FPORT_FDES (sock);
   if (bind (fd, soka, size) == -1)
@@ -907,10 +884,9 @@ 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);
+  SCM_VALIDATE_OPFPORT (1, sock);
   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;
 }
@@ -918,11 +894,12 @@ SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
 
 /* Put the components of a sockaddr into a new SCM vector.  */
 static SCM
-scm_addr_vector (const struct sockaddr *address, const char *proc)
+scm_addr_vector (const struct sockaddr *address, int addr_size, 
+                const char *proc)
 {
   short int fam = address->sa_family;
-  SCM result;
-  SCM *ve;
+  SCM result =SCM_EOL;
+
 
   switch (fam)
     {
@@ -931,10 +908,13 @@ scm_addr_vector (const struct sockaddr *address, const char *proc)
        const struct sockaddr_in *nad = (struct sockaddr_in *) address;
 
        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));
+
+       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
@@ -943,15 +923,14 @@ scm_addr_vector (const struct sockaddr *address, const char *proc)
        const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;
 
        result = scm_c_make_vector (5, SCM_UNSPECIFIED);
-       ve = SCM_VELTS (result);
-       ve[0] = scm_ulong2num ((unsigned long) fam);
-       ve[1] = ipv6_net_to_num (nad->sin6_addr.s6_addr);
-       ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin6_port));
-       ve[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
-       ve[4] = scm_ulong2num ((unsigned long) nad->sin6_scope_id);
+       SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_ulong (nad->sin6_scope_id));
 #else
-       ve[4] = SCM_INUM0;
+       SCM_SIMPLE_VECTOR_SET(result, 4, SCM_INUM0);
 #endif
       }
       break;
@@ -962,15 +941,20 @@ scm_addr_vector (const struct sockaddr *address, const char *proc)
        const struct sockaddr_un *nad = (struct sockaddr_un *) address;
 
        result = scm_c_make_vector (2, SCM_UNSPECIFIED);
-       ve = SCM_VELTS (result);
-       ve[0] = scm_ulong2num ((unsigned long) fam);
-       ve[1] = scm_mem2string (nad->sun_path, strlen (nad->sun_path));
+
+       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_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F);
+       else
+         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_LIST1 (SCM_MAKINUM (fam)));
+                     scm_list_1 (scm_from_int (fam)));
     }
   return result;
 }
@@ -1025,7 +1009,7 @@ SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
   if (newfd == -1)
     SCM_SYSERROR;
   newsock = SCM_SOCK_FD_TO_PORT (newfd);
-  address = scm_addr_vector (addr, FUNC_NAME);
+  address = scm_addr_vector (addr, addr_size, FUNC_NAME);
   return scm_cons (newsock, address);
 }
 #undef FUNC_NAME
@@ -1043,11 +1027,11 @@ SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
   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);
   if (getsockname (fd, addr, &addr_size) == -1)
     SCM_SYSERROR;
-  return scm_addr_vector (addr, FUNC_NAME);
+  return scm_addr_vector (addr, addr_size, FUNC_NAME);
 }
 #undef FUNC_NAME
 
@@ -1065,11 +1049,11 @@ SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
   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);
   if (getpeername (fd, addr, &addr_size) == -1)
     SCM_SYSERROR;
-  return scm_addr_vector (addr, FUNC_NAME);
+  return scm_addr_vector (addr, addr_size, FUNC_NAME);
 }
 #undef FUNC_NAME
 
@@ -1097,17 +1081,27 @@ SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
   int rv;
   int fd;
   int flg;
+  char *dest;
+  size_t len;
 
-  SCM_VALIDATE_OPFPORT (1,sock);
-  SCM_VALIDATE_STRING (2,buf);
-  SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
+  SCM_VALIDATE_OPFPORT (1, sock);
+  SCM_VALIDATE_STRING (2, buf);
+  if (SCM_UNBNDP (flags))
+    flg = 0;
+  else
+    flg = scm_to_int (flags);
   fd = SCM_FPORT_FDES (sock);
 
-  SCM_SYSCALL (rv = recv (fd, SCM_STRING_CHARS (buf), SCM_STRING_LENGTH (buf), flg));
+  len =  scm_i_string_length (buf);
+  dest = scm_i_string_writable_chars (buf);
+  SCM_SYSCALL (rv = recv (fd, dest, len, flg));
+  scm_i_string_stop_writing ();
+
   if (rv == -1)
     SCM_SYSERROR;
 
-  return SCM_MAKINUM (rv);
+  scm_remember_upto_here_1 (buf);
+  return scm_from_int (rv);
 }
 #undef FUNC_NAME
 
@@ -1130,17 +1124,28 @@ SCM_DEFINE (scm_send, "send", 2, 1, 0,
   int rv;
   int fd;
   int flg;
+  const char *src;
+  size_t len;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_OPFPORT (1,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));
+  len = scm_i_string_length (message);
+  src = scm_i_string_writable_chars (message);
+  SCM_SYSCALL (rv = send (fd, src, len, flg));
+  scm_i_string_stop_writing ();
+
   if (rv == -1)
     SCM_SYSERROR;
-  return SCM_MAKINUM (rv);
+
+  scm_remember_upto_here_1 (message);
+  return scm_from_int (rv);
 }
 #undef FUNC_NAME
 
@@ -1171,17 +1176,20 @@ SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
   int fd;
   int flg;
   char *buf;
-  int offset;
-  int cend;
+  size_t offset;
+  size_t cend;
   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_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
-  SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, str, buf, 4, start, offset,
-                                   5, end, cend);
+  
+  SCM_VALIDATE_STRING (2, str);
+  scm_i_get_substring_spec (scm_i_string_length (str),
+                           start, &offset, end, &cend);
+
   if (SCM_UNBNDP (flags))
     flg = 0;
   else
@@ -1189,18 +1197,22 @@ SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
 
   /* recvfrom will not necessarily return an address.  usually nothing
      is returned for stream sockets.  */
+  buf = scm_i_string_writable_chars (str);
   addr->sa_family = AF_UNSPEC;
   SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
                              cend - offset, flg,
                              addr, &addr_size));
+  scm_i_string_stop_writing ();
+
   if (rv == -1)
     SCM_SYSERROR;
   if (addr->sa_family != AF_UNSPEC)
-    address = scm_addr_vector (addr, FUNC_NAME);
+    address = scm_addr_vector (addr, addr_size, FUNC_NAME);
   else
     address = SCM_BOOL_F;
 
-  return scm_cons (SCM_MAKINUM (rv), address);
+  scm_remember_upto_here_1 (str);
+  return scm_cons (scm_from_int (rv), address);
 }
 #undef FUNC_NAME
 
@@ -1232,21 +1244,21 @@ SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
   int size;
 
   sock = SCM_COERCE_OUTPORT (sock);
-  SCM_VALIDATE_FPORT (1,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,
+  soka = scm_fill_sockaddr (scm_to_int (fam), address, &args_and_flags, 4,
                            FUNC_NAME, &size);
-  if (SCM_NULLP (args_and_flags))
+  if (scm_is_null (args_and_flags))
     flg = 0;
   else
     {
-      SCM_VALIDATE_CONS (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_STRING_CHARS (message),
-                           SCM_STRING_LENGTH (message),
+  SCM_SYSCALL (rv = sendto (fd,
+                           scm_i_string_chars (message),
+                           scm_i_string_length (message),
                            flg, soka, size));
   if (rv == -1)
     {
@@ -1256,7 +1268,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
@@ -1267,130 +1281,147 @@ 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));
+  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.  */
 #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));
+  scm_c_define ("SOL_IP", scm_from_int (SOL_IP));
 #endif
 #ifdef SOL_TCP
-  scm_c_define ("SOL_TCP", SCM_MAKINUM (SOL_TCP));
+  scm_c_define ("SOL_TCP", scm_from_int (SOL_TCP));
 #endif
 #ifdef SOL_UDP
-  scm_c_define ("SOL_UDP", SCM_MAKINUM (SOL_UDP));
+  scm_c_define ("SOL_UDP", scm_from_int (SOL_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
 
   /* recv/send options.  */
 #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 __MINGW32__
+  scm_i_init_socket_Win32 ();
+#endif
+
+#ifdef IP_ADD_MEMBERSHIP
+  scm_c_define ("IP_ADD_MEMBERSHIP", scm_from_int (IP_ADD_MEMBERSHIP));
+  scm_c_define ("IP_DROP_MEMBERSHIP", scm_from_int (IP_DROP_MEMBERSHIP));
 #endif
 
   scm_add_feature ("socket");
 
-#ifndef SCM_MAGIC_SNARFER
 #include "libguile/socket.x"
-#endif
 }