Use Gnulib's `inet_ntop' and `inet_pton' modules.
[bpt/guile.git] / libguile / socket.c
index bfac452..4f4d392 100644 (file)
@@ -1,24 +1,25 @@
-/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
 
 \f
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
 #include <gmp.h>
 
 #include "libguile/_scm.h"
-#include "libguile/unif.h"
+#include "libguile/arrays.h"
 #include "libguile/feature.h"
 #include "libguile/fports.h"
 #include "libguile/strings.h"
 #include "libguile/vectors.h"
 #include "libguile/dynwind.h"
+#include "libguile/srfi-13.h"
 
 #include "libguile/validate.h"
 #include "libguile/socket.h"
@@ -347,10 +349,9 @@ scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
       scm_remember_upto_here_1 (src);
     }
   else
-    scm_wrong_type_arg (NULL, 0, src);
+    scm_wrong_type_arg_msg ("scm_to_ipv6", 0, src, "integer");
 }
 
-#ifdef HAVE_INET_PTON
 SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
             (SCM family, SCM address),
            "Convert a string containing a printable network address to\n"
@@ -386,9 +387,7 @@ SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
     return scm_from_ipv6 ((scm_t_uint8 *) dst);
 }
 #undef FUNC_NAME
-#endif
 
-#ifdef HAVE_INET_NTOP
 SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
             (SCM family, SCM address),
            "Convert a network address into a printable string.\n"
@@ -397,8 +396,8 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
            "@var{family} can be @code{AF_INET} or @code{AF_INET6}.  E.g.,\n\n"
            "@lisp\n"
            "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n"
-           "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n"
-           "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"
+           "(inet-ntop AF_INET6 (- (expt 2 128) 1))\n"
+           "  @result{} \"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"\n"
            "@end lisp")
 #define FUNC_NAME s_scm_inet_ntop
 {
@@ -433,7 +432,6 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
   return scm_from_locale_string (dst);
 }
 #undef FUNC_NAME
-#endif
 
 #endif  /* HAVE_IPV6 */
 
@@ -1167,7 +1165,8 @@ scm_to_sockaddr (SCM address, size_t *address_size)
          {
            struct sockaddr_in6 c_inet6;
 
-           scm_to_ipv6 (c_inet6.sin6_addr.s6_addr, address);
+           scm_to_ipv6 (c_inet6.sin6_addr.s6_addr,
+                        SCM_SIMPLE_VECTOR_REF (address, 1));
            c_inet6.sin6_port =
              htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
            c_inet6.sin6_flowinfo =
@@ -1413,6 +1412,8 @@ SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
            "protocols, if a packet larger than this limit is encountered\n"
            "then some data\n"
            "will be irrevocably lost.\n\n"
+           "The data is assumed to be binary, and there is no decoding of\n"
+           "of locale-encoded strings.\n\n"
            "The optional @var{flags} argument is a value or\n"
            "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
            "The value returned is the number of bytes read from the\n"
@@ -1427,6 +1428,7 @@ SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
   int flg;
   char *dest;
   size_t len;
+  SCM msg;
 
   SCM_VALIDATE_OPFPORT (1, sock);
   SCM_VALIDATE_STRING (2, buf);
@@ -1436,15 +1438,16 @@ SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
     flg = scm_to_int (flags);
   fd = SCM_FPORT_FDES (sock);
 
-  len =  scm_i_string_length (buf);
-  dest = scm_i_string_writable_chars (buf);
+  len = scm_i_string_length (buf);
+  msg = scm_i_make_string (len, &dest);
   SCM_SYSCALL (rv = recv (fd, dest, len, flg));
-  scm_i_string_stop_writing ();
+  scm_string_copy_x (buf, scm_from_int (0), 
+                    msg, scm_from_int (0), scm_from_size_t (len));
 
   if (rv == -1)
     SCM_SYSERROR;
 
-  scm_remember_upto_here_1 (buf);
+  scm_remember_upto_here_2 (buf, msg);
   return scm_from_int (rv);
 }
 #undef FUNC_NAME
@@ -1462,18 +1465,28 @@ SCM_DEFINE (scm_send, "send", 2, 1, 0,
            "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
            "Note that the data is written directly to the socket\n"
            "file descriptor:\n"
-           "any unflushed buffered port data is ignored.")
+           "any unflushed buffered port data is ignored.\n\n"
+           "This operation is defined only for strings containing codepoints\n"
+           "zero to 255.")
 #define FUNC_NAME s_scm_send
 {
   int rv;
   int fd;
   int flg;
-  const char *src;
+  char *src;
   size_t len;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
   SCM_VALIDATE_STRING (2, message);
+  
+  /* If the string is wide, see if it can be coerced into
+     a narrow string.  */
+  if (!scm_i_is_narrow_string (message)
+      || scm_i_try_narrow_string (message))
+    SCM_MISC_ERROR ("the message string is not 8-bit: ~s", 
+                        scm_list_1 (message));
+
   if (SCM_UNBNDP (flags))
     flg = 0;
   else
@@ -1481,6 +1494,7 @@ SCM_DEFINE (scm_send, "send", 2, 1, 0,
   fd = SCM_FPORT_FDES (sock);
 
   len = scm_i_string_length (message);
+  message = scm_i_string_start_writing (message);
   src = scm_i_string_writable_chars (message);
   SCM_SYSCALL (rv = send (fd, src, len, flg));
   scm_i_string_stop_writing ();
@@ -1549,6 +1563,7 @@ SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
 
   /* recvfrom will not necessarily return an address.  usually nothing
      is returned for stream sockets.  */
+  str = scm_i_string_start_writing (str);
   buf = scm_i_string_writable_chars (str);
   ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
   SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
@@ -1588,7 +1603,9 @@ SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
            "set to be non-blocking.\n"
            "Note that the data is written directly to the socket\n"
            "file descriptor:\n"
-           "any unflushed buffered port data is ignored.")
+           "any unflushed buffered port data is ignored.\n"
+           "This operation is defined only for strings containing codepoints\n"
+           "zero to 255.")
 #define FUNC_NAME s_scm_sendto
 {
   int rv;