-/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004 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"
+#include "libguile/iselect.h"
+
#ifdef __MINGW32__
#include "win32-socket.h"
#endif
+ strlen ((ptr)->sun_path))
#endif
+/* The largest possible socket address. Wrapping it in a union guarantees
+ that the compiler will make it suitably aligned. */
+typedef union
+{
+ struct sockaddr sockaddr;
+ struct sockaddr_in sockaddr_in;
+
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
+ struct sockaddr_un sockaddr_un;
+#endif
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 sockaddr_in6;
+#endif
+} scm_t_max_sockaddr;
+
+
+/* Maximum size of a socket address. */
+#define MAX_ADDR_SIZE (sizeof (scm_t_max_sockaddr))
+
+
\f
SCM_DEFINE (scm_htons, "htons", 1, 0, 0,
"and returned as a new integer.")
#define FUNC_NAME s_scm_htonl
{
- scm_t_uint32 c_in = SCM_NUM2ULONG (1, value);
-
- return scm_from_ulong (htonl (c_in));
+ return scm_from_ulong (htonl (scm_to_uint32 (value)));
}
#undef FUNC_NAME
"and returned as a new integer.")
#define FUNC_NAME s_scm_ntohl
{
- scm_t_uint32 c_in = SCM_NUM2ULONG (1, value);
-
- return scm_from_ulong (ntohl (c_in));
+ return scm_from_ulong (ntohl (scm_to_uint32 (value)));
}
#undef FUNC_NAME
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"
{
int af;
char *src;
- char dst[16];
+ scm_t_uint32 dst[4];
int rv, eno;
af = scm_to_int (family);
else if (rv == 0)
SCM_MISC_ERROR ("Bad address", SCM_EOL);
if (af == AF_INET)
- return scm_from_ulong (ntohl (*(scm_t_uint32 *) dst));
+ return scm_from_ulong (ntohl (*dst));
else
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"
"@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
{
#else
char dst[46];
#endif
- char addr6[16];
+ const char *result;
af = scm_to_int (family);
SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
if (af == AF_INET)
- *(scm_t_uint32 *) addr6 = htonl (SCM_NUM2ULONG (2, address));
+ {
+ scm_t_uint32 addr4;
+
+ addr4 = htonl (SCM_NUM2ULONG (2, address));
+ result = inet_ntop (af, &addr4, dst, sizeof (dst));
+ }
else
- scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
- if (inet_ntop (af, &addr6, dst, sizeof dst) == NULL)
+ {
+ char addr6[16];
+
+ scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
+ result = inet_ntop (af, &addr6, dst, sizeof (dst));
+ }
+
+ if (result == NULL)
SCM_SYSERROR;
+
return scm_from_locale_string (dst);
}
#undef FUNC_NAME
-#endif
#endif /* HAVE_IPV6 */
#undef FUNC_NAME
#endif
+/* Possible results for `getsockopt ()'. Wrapping it into a union guarantees
+ suitable alignment. */
+typedef union
+{
+#ifdef HAVE_STRUCT_LINGER
+ struct linger linger;
+#endif
+ size_t size;
+ int integer;
+} scm_t_getsockopt_result;
+
SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
(SCM sock, SCM level, SCM optname),
- "Return the value of a particular socket option for the socket\n"
- "port @var{sock}. @var{level} is an integer code for type of\n"
- "option being requested, e.g., @code{SOL_SOCKET} for\n"
- "socket-level options. @var{optname} is an integer code for the\n"
- "option required and should be specified using one of the\n"
- "symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n"
- "The returned value is typically an integer but @code{SO_LINGER}\n"
- "returns a pair of integers.")
+ "Return an option value from socket port @var{sock}.\n"
+ "\n"
+ "@var{level} is an integer specifying a protocol layer, either\n"
+ "@code{SOL_SOCKET} for socket level options, or a protocol\n"
+ "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
+ "(@pxref{Network Databases}).\n"
+ "\n"
+ "@defvar SOL_SOCKET\n"
+ "@defvarx IPPROTO_IP\n"
+ "@defvarx IPPROTO_TCP\n"
+ "@defvarx IPPROTO_UDP\n"
+ "@end defvar\n"
+ "\n"
+ "@var{optname} is an integer specifying an option within the\n"
+ "protocol layer.\n"
+ "\n"
+ "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
+ "defined (when provided by the system). For their meaning see\n"
+ "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
+ "Manual}, or @command{man 7 socket}.\n"
+ "\n"
+ "@defvar SO_DEBUG\n"
+ "@defvarx SO_REUSEADDR\n"
+ "@defvarx SO_STYLE\n"
+ "@defvarx SO_TYPE\n"
+ "@defvarx SO_ERROR\n"
+ "@defvarx SO_DONTROUTE\n"
+ "@defvarx SO_BROADCAST\n"
+ "@defvarx SO_SNDBUF\n"
+ "@defvarx SO_RCVBUF\n"
+ "@defvarx SO_KEEPALIVE\n"
+ "@defvarx SO_OOBINLINE\n"
+ "@defvarx SO_NO_CHECK\n"
+ "@defvarx SO_PRIORITY\n"
+ "The value returned is an integer.\n"
+ "@end defvar\n"
+ "\n"
+ "@defvar SO_LINGER\n"
+ "The @var{value} returned is a pair of integers\n"
+ "@code{(@var{ENABLE} . @var{TIMEOUT})}. On old systems without\n"
+ "timeout support (ie.@: without @code{struct linger}), only\n"
+ "@var{ENABLE} has an effect but the value in Guile is always a\n"
+ "pair.\n"
+ "@end defvar")
#define FUNC_NAME s_scm_getsockopt
{
int fd;
/* size of optval is the largest supported option. */
-#ifdef HAVE_STRUCT_LINGER
- char optval[sizeof (struct linger)];
- socklen_t optlen = sizeof (struct linger);
-#else
- char optval[sizeof (size_t)];
- socklen_t optlen = sizeof (size_t);
-#endif
+ scm_t_getsockopt_result optval;
+ socklen_t optlen = sizeof (optval);
int ilevel;
int ioptname;
ioptname = scm_to_int (optname);
fd = SCM_FPORT_FDES (sock);
- if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
+ if (getsockopt (fd, ilevel, ioptname, (void *) &optval, &optlen) == -1)
SCM_SYSERROR;
if (ilevel == SOL_SOCKET)
if (ioptname == SO_LINGER)
{
#ifdef HAVE_STRUCT_LINGER
- struct linger *ling = (struct linger *) optval;
+ struct linger *ling = (struct linger *) &optval;
return scm_cons (scm_from_long (ling->l_onoff),
scm_from_long (ling->l_linger));
#else
- return scm_cons (scm_from_long (*(int *) optval),
+ return scm_cons (scm_from_long (*(int *) &optval),
scm_from_int (0));
#endif
}
#endif
)
{
- return scm_from_size_t (*(size_t *) optval);
+ return scm_from_size_t (*(size_t *) &optval);
}
}
- return scm_from_int (*(int *) optval);
+ return scm_from_int (*(int *) &optval);
}
#undef FUNC_NAME
SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
(SCM sock, SCM level, SCM optname, SCM value),
- "Set the value of a particular socket option for the socket\n"
- "port @var{sock}. @var{level} is an integer code for type of option\n"
- "being set, e.g., @code{SOL_SOCKET} for socket-level options.\n"
- "@var{optname} is an\n"
- "integer code for the option to set and should be specified using one of\n"
- "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n"
- "@var{value} is the value to which the option should be set. For\n"
- "most options this must be an integer, but for @code{SO_LINGER} it must\n"
- "be a pair.\n\n"
- "The return value is unspecified.")
+ "Set an option on socket port @var{sock}. The return value is\n"
+ "unspecified.\n"
+ "\n"
+ "@var{level} is an integer specifying a protocol layer, either\n"
+ "@code{SOL_SOCKET} for socket level options, or a protocol\n"
+ "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
+ "(@pxref{Network Databases}).\n"
+ "\n"
+ "@defvar SOL_SOCKET\n"
+ "@defvarx IPPROTO_IP\n"
+ "@defvarx IPPROTO_TCP\n"
+ "@defvarx IPPROTO_UDP\n"
+ "@end defvar\n"
+ "\n"
+ "@var{optname} is an integer specifying an option within the\n"
+ "protocol layer.\n"
+ "\n"
+ "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
+ "defined (when provided by the system). For their meaning see\n"
+ "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
+ "Manual}, or @command{man 7 socket}.\n"
+ "\n"
+ "@defvar SO_DEBUG\n"
+ "@defvarx SO_REUSEADDR\n"
+ "@defvarx SO_STYLE\n"
+ "@defvarx SO_TYPE\n"
+ "@defvarx SO_ERROR\n"
+ "@defvarx SO_DONTROUTE\n"
+ "@defvarx SO_BROADCAST\n"
+ "@defvarx SO_SNDBUF\n"
+ "@defvarx SO_RCVBUF\n"
+ "@defvarx SO_KEEPALIVE\n"
+ "@defvarx SO_OOBINLINE\n"
+ "@defvarx SO_NO_CHECK\n"
+ "@defvarx SO_PRIORITY\n"
+ "@var{value} is an integer.\n"
+ "@end defvar\n"
+ "\n"
+ "@defvar SO_LINGER\n"
+ "@var{value} is a pair of integers @code{(@var{ENABLE}\n"
+ ". @var{TIMEOUT})}. On old systems without timeout support\n"
+ "(ie.@: without @code{struct linger}), only @var{ENABLE} has an\n"
+ "effect but the value in Guile is always a pair.\n"
+ "@end defvar\n"
+ "\n"
+ "@c Note that we refer only to ``man ip'' here. On GNU/Linux it's\n"
+ "@c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.\n"
+ "@c \n"
+ "For IP level (@code{IPPROTO_IP}) the following @var{optname}s\n"
+ "are defined (when provided by the system). See @command{man\n"
+ "ip} for what they mean.\n"
+ "\n"
+ "@defvar IP_ADD_MEMBERSHIP\n"
+ "@defvarx IP_DROP_MEMBERSHIP\n"
+ "These can be used only with @code{setsockopt}, not\n"
+ "@code{getsockopt}. @var{value} is a pair\n"
+ "@code{(@var{MULTIADDR} . @var{INTERFACEADDR})} of IPv4\n"
+ "addresses (@pxref{Network Address Conversion}).\n"
+ "@var{MULTIADDR} is a multicast address to be added to or\n"
+ "dropped from the interface @var{INTERFACEADDR}.\n"
+ "@var{INTERFACEADDR} can be @code{INADDR_ANY} to have the system\n"
+ "select the interface. @var{INTERFACEADDR} can also be an\n"
+ "interface index number, on systems supporting that.\n"
+ "@end defvar")
#define FUNC_NAME s_scm_setsockopt
{
int fd;
static struct sockaddr *
scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
- const char *proc, int *size)
+ const char *proc, size_t *size)
#define FUNC_NAME proc
{
switch (fam)
port = scm_to_int (SCM_CAR (*args));
*args = SCM_CDR (*args);
soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in));
- if (!soka)
- scm_memory_error (proc);
+
#if HAVE_STRUCT_SOCKADDR_SIN_LEN
soka->sin_len = sizeof (struct sockaddr_in);
#endif
}
}
soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));
- if (!soka)
- scm_memory_error (proc);
+
#if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
soka->sin6_len = sizeof (struct sockaddr_in6);
#endif
int addr_size;
char *c_address;
- scm_frame_begin (0);
+ scm_dynwind_begin (0);
c_address = scm_to_locale_string (address);
- scm_frame_free (c_address);
+ scm_dynwind_free (c_address);
/* the static buffer size in sockaddr_un seems to be arbitrary
and not necessarily a hard limit. e.g., the glibc manual
strcpy (soka->sun_path, c_address);
*size = SUN_LEN (soka);
- scm_frame_end ();
+ scm_dynwind_end ();
return (struct sockaddr *) soka;
}
#endif
}
}
#undef FUNC_NAME
-
-SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
- (SCM sock, SCM fam, SCM address, SCM args),
+
+SCM_DEFINE (scm_connect, "connect", 2, 1, 1,
+ (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
"Initiate a connection from a socket using a specified address\n"
"family to the address\n"
"specified by @var{address} and possibly @var{args}.\n"
"@var{args} may be up to three integers:\n"
"port [flowinfo] [scope_id],\n"
"where flowinfo and scope_id default to zero.\n\n"
+ "Alternatively, the second argument can be a socket address object "
+ "as returned by @code{make-socket-address}, in which case the "
+ "no additional arguments should be passed.\n\n"
"The return value is unspecified.")
#define FUNC_NAME s_scm_connect
{
int fd;
struct sockaddr *soka;
- int size;
+ size_t size;
sock = SCM_COERCE_OUTPORT (sock);
SCM_VALIDATE_OPFPORT (1, sock);
fd = SCM_FPORT_FDES (sock);
- soka = scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_NAME,
- &size);
+
+ if (address == SCM_UNDEFINED)
+ /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
+ `socket address' object. */
+ soka = scm_to_sockaddr (fam_or_sockaddr, &size);
+ else
+ soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
+ &args, 3, FUNC_NAME, &size);
+
if (connect (fd, soka, size) == -1)
{
int save_errno = errno;
-
+
free (soka);
errno = save_errno;
SCM_SYSERROR;
}
#undef FUNC_NAME
-SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
- (SCM sock, SCM fam, SCM address, SCM args),
+SCM_DEFINE (scm_bind, "bind", 2, 1, 1,
+ (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
"Assign an address to the socket port @var{sock}.\n"
"Generally this only needs to be done for server sockets,\n"
"so they know where to look for incoming connections. A socket\n"
"may be up to three integers:\n"
"port [flowinfo] [scope_id],\n"
"where flowinfo and scope_id default to zero.\n\n"
+ "Alternatively, the second argument can be a socket address object "
+ "as returned by @code{make-socket-address}, in which case the "
+ "no additional arguments should be passed.\n\n"
"The return value is unspecified.")
#define FUNC_NAME s_scm_bind
{
struct sockaddr *soka;
- int size;
+ size_t size;
int fd;
sock = SCM_COERCE_OUTPORT (sock);
SCM_VALIDATE_OPFPORT (1, sock);
- soka = scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_NAME,
- &size);
fd = SCM_FPORT_FDES (sock);
+
+ if (address == SCM_UNDEFINED)
+ /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
+ `socket address' object. */
+ soka = scm_to_sockaddr (fam_or_sockaddr, &size);
+ else
+ soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
+ &args, 3, FUNC_NAME, &size);
+
+
if (bind (fd, soka, size) == -1)
{
int save_errno = errno;
-
+
free (soka);
errno = save_errno;
SCM_SYSERROR;
#undef FUNC_NAME
/* Put the components of a sockaddr into a new SCM vector. */
-static SCM
-scm_addr_vector (const struct sockaddr *address, int addr_size,
- const char *proc)
+static SCM_C_INLINE_KEYWORD SCM
+_scm_from_sockaddr (const scm_t_max_sockaddr *address, unsigned addr_size,
+ const char *proc)
{
- short int fam = address->sa_family;
- SCM result =SCM_EOL;
-
+ SCM result = SCM_EOL;
+ short int fam = ((struct sockaddr *) address)->sa_family;
switch (fam)
{
break;
#endif
default:
- scm_misc_error (proc, "Unrecognised address family: ~A",
+ result = SCM_UNSPECIFIED;
+ scm_misc_error (proc, "unrecognised address family: ~A",
scm_list_1 (scm_from_int (fam)));
+
}
return result;
}
-/* calculate the size of a buffer large enough to hold any supported
- sockaddr type. if the buffer isn't large enough, certain system
- calls will return a truncated address. */
+/* The publicly-visible function. Return a Scheme object representing
+ ADDRESS, an address of ADDR_SIZE bytes. */
+SCM
+scm_from_sockaddr (const struct sockaddr *address, unsigned addr_size)
+{
+ return (_scm_from_sockaddr ((scm_t_max_sockaddr *) address,
+ addr_size, "scm_from_sockaddr"));
+}
-#if defined (HAVE_UNIX_DOMAIN_SOCKETS)
-#define MAX_SIZE_UN sizeof (struct sockaddr_un)
-#else
-#define MAX_SIZE_UN 0
+/* Convert ADDRESS, an address object returned by either
+ `scm_from_sockaddr ()' or `scm_make_socket_address ()', into its C
+ representation. On success, a non-NULL pointer is returned and
+ ADDRESS_SIZE is updated to the actual size (in bytes) of the returned
+ address. The result must eventually be freed using `free ()'. */
+struct sockaddr *
+scm_to_sockaddr (SCM address, size_t *address_size)
+#define FUNC_NAME "scm_to_sockaddr"
+{
+ short int family;
+ struct sockaddr *c_address = NULL;
+
+ SCM_VALIDATE_VECTOR (1, address);
+
+ *address_size = 0;
+ family = scm_to_short (SCM_SIMPLE_VECTOR_REF (address, 0));
+
+ switch (family)
+ {
+ case AF_INET:
+ {
+ if (SCM_SIMPLE_VECTOR_LENGTH (address) != 3)
+ scm_misc_error (FUNC_NAME,
+ "invalid inet address representation: ~A",
+ scm_list_1 (address));
+ else
+ {
+ struct sockaddr_in c_inet;
+
+ c_inet.sin_addr.s_addr =
+ htonl (scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 1)));
+ c_inet.sin_port =
+ htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
+ c_inet.sin_family = AF_INET;
+
+ *address_size = sizeof (c_inet);
+ c_address = scm_malloc (sizeof (c_inet));
+ memcpy (c_address, &c_inet, sizeof (c_inet));
+ }
+
+ break;
+ }
+
+#ifdef HAVE_IPV6
+ case AF_INET6:
+ {
+ if (SCM_SIMPLE_VECTOR_LENGTH (address) != 5)
+ scm_misc_error (FUNC_NAME, "invalid inet6 address representation: ~A",
+ scm_list_1 (address));
+ else
+ {
+ struct sockaddr_in6 c_inet6;
+
+ scm_to_ipv6 (c_inet6.sin6_addr.s6_addr,
+ SCM_SIMPLE_VECTOR_REF (address, 1));
+ c_inet6.sin6_port =
+ htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
+ c_inet6.sin6_flowinfo =
+ scm_to_uint32 (SCM_SIMPLE_VECTOR_REF (address, 3));
+#ifdef HAVE_SIN6_SCOPE_ID
+ c_inet6.sin6_scope_id =
+ scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 4));
+#endif
+
+ c_inet6.sin6_family = AF_INET6;
+
+ *address_size = sizeof (c_inet6);
+ c_address = scm_malloc (sizeof (c_inet6));
+ memcpy (c_address, &c_inet6, sizeof (c_inet6));
+ }
+
+ break;
+ }
#endif
-#if defined (HAVE_IPV6)
-#define MAX_SIZE_IN6 sizeof (struct sockaddr_in6)
+#ifdef HAVE_UNIX_DOMAIN_SOCKETS
+ case AF_UNIX:
+ {
+ if (SCM_SIMPLE_VECTOR_LENGTH (address) != 2)
+ scm_misc_error (FUNC_NAME, "invalid unix address representation: ~A",
+ scm_list_1 (address));
+ else
+ {
+ SCM path;
+ size_t path_len = 0;
+
+ path = SCM_SIMPLE_VECTOR_REF (address, 1);
+ if ((!scm_is_string (path)) && (path != SCM_BOOL_F))
+ scm_misc_error (FUNC_NAME, "invalid unix address "
+ "path: ~A", scm_list_1 (path));
+ else
+ {
+ struct sockaddr_un c_unix;
+
+ if (path == SCM_BOOL_F)
+ path_len = 0;
+ else
+ path_len = scm_c_string_length (path);
+
+#ifdef UNIX_PATH_MAX
+ if (path_len >= UNIX_PATH_MAX)
+#else
+/* We can hope that this limit will eventually vanish, at least on GNU.
+ However, currently, while glibc doesn't define `UNIX_PATH_MAX', it
+ documents it has being limited to 108 bytes. */
+ if (path_len >= sizeof (c_unix.sun_path))
+#endif
+ scm_misc_error (FUNC_NAME, "unix address path "
+ "too long: ~A", scm_list_1 (path));
+ else
+ {
+ if (path_len)
+ {
+ scm_to_locale_stringbuf (path, c_unix.sun_path,
+#ifdef UNIX_PATH_MAX
+ UNIX_PATH_MAX);
#else
-#define MAX_SIZE_IN6 0
+ sizeof (c_unix.sun_path));
#endif
+ c_unix.sun_path[path_len] = '\0';
+
+ /* Sanity check. */
+ if (strlen (c_unix.sun_path) != path_len)
+ scm_misc_error (FUNC_NAME, "unix address path "
+ "contains nul characters: ~A",
+ scm_list_1 (path));
+ }
+ else
+ c_unix.sun_path[0] = '\0';
+
+ c_unix.sun_family = AF_UNIX;
+
+ *address_size = SUN_LEN (&c_unix);
+ c_address = scm_malloc (sizeof (c_unix));
+ memcpy (c_address, &c_unix, sizeof (c_unix));
+ }
+ }
+ }
+
+ break;
+ }
+#endif
+
+ default:
+ scm_misc_error (FUNC_NAME, "unrecognised address family: ~A",
+ scm_list_1 (scm_from_ushort (family)));
+ }
+
+ return c_address;
+}
+#undef FUNC_NAME
+
+
+/* Return a newly-allocated `sockaddr' structure that reflects ADDRESS, being
+ an address of family FAMILY, with the family-specific parameters ARGS (see
+ the description of `connect' for details). The returned structure may be
+ freed using `free ()'. */
+struct sockaddr *
+scm_c_make_socket_address (SCM family, SCM address, SCM args,
+ size_t *address_size)
+{
+ struct sockaddr *soka;
+
+ soka = scm_fill_sockaddr (scm_to_ushort (family), address, &args, 1,
+ "scm_c_make_socket_address", address_size);
+
+ return soka;
+}
+
+SCM_DEFINE (scm_make_socket_address, "make-socket-address", 2, 0, 1,
+ (SCM family, SCM address, SCM args),
+ "Return a Scheme address object that reflects @var{address}, "
+ "being an address of family @var{family}, with the "
+ "family-specific parameters @var{args} (see the description of "
+ "@code{connect} for details).")
+#define FUNC_NAME s_scm_make_socket_address
+{
+ SCM result = SCM_BOOL_F;
+ struct sockaddr *c_address;
+ size_t c_address_size;
+
+ c_address = scm_c_make_socket_address (family, address, args,
+ &c_address_size);
+ if (c_address != NULL)
+ {
+ result = scm_from_sockaddr (c_address, c_address_size);
+ free (c_address);
+ }
-#define MAX_ADDR_SIZE max (max (sizeof (struct sockaddr_in), MAX_SIZE_IN6),\
- MAX_SIZE_UN)
+ return result;
+}
+#undef FUNC_NAME
+\f
SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
(SCM sock),
"Accept a connection on a bound, listening socket.\n"
"connection and will continue to accept new requests.")
#define FUNC_NAME s_scm_accept
{
- int fd;
+ int fd, selected;
int newfd;
SCM address;
SCM newsock;
+ SELECT_TYPE readfds, exceptfds;
socklen_t addr_size = MAX_ADDR_SIZE;
- char max_addr[MAX_ADDR_SIZE];
- struct sockaddr *addr = (struct sockaddr *) max_addr;
+ scm_t_max_sockaddr addr;
sock = SCM_COERCE_OUTPORT (sock);
SCM_VALIDATE_OPFPORT (1, sock);
fd = SCM_FPORT_FDES (sock);
- newfd = accept (fd, addr, &addr_size);
+
+ FD_ZERO (&readfds);
+ FD_ZERO (&exceptfds);
+ FD_SET (fd, &readfds);
+ FD_SET (fd, &exceptfds);
+
+ /* Block until something happens on FD, leaving guile mode while
+ waiting. */
+ selected = scm_std_select (fd + 1, &readfds, NULL, &exceptfds,
+ NULL);
+ if (selected < 0)
+ SCM_SYSERROR;
+
+ newfd = accept (fd, (struct sockaddr *) &addr, &addr_size);
if (newfd == -1)
SCM_SYSERROR;
newsock = SCM_SOCK_FD_TO_PORT (newfd);
- address = scm_addr_vector (addr, addr_size, FUNC_NAME);
+ address = _scm_from_sockaddr (&addr, addr_size,
+ FUNC_NAME);
+
return scm_cons (newsock, address);
}
#undef FUNC_NAME
{
int fd;
socklen_t addr_size = MAX_ADDR_SIZE;
- char max_addr[MAX_ADDR_SIZE];
- struct sockaddr *addr = (struct sockaddr *) max_addr;
+ scm_t_max_sockaddr addr;
sock = SCM_COERCE_OUTPORT (sock);
SCM_VALIDATE_OPFPORT (1, sock);
fd = SCM_FPORT_FDES (sock);
- if (getsockname (fd, addr, &addr_size) == -1)
+ if (getsockname (fd, (struct sockaddr *) &addr, &addr_size) == -1)
SCM_SYSERROR;
- return scm_addr_vector (addr, addr_size, FUNC_NAME);
+
+ return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
}
#undef FUNC_NAME
{
int fd;
socklen_t addr_size = MAX_ADDR_SIZE;
- char max_addr[MAX_ADDR_SIZE];
- struct sockaddr *addr = (struct sockaddr *) max_addr;
+ scm_t_max_sockaddr addr;
sock = SCM_COERCE_OUTPORT (sock);
SCM_VALIDATE_OPFPORT (1, sock);
fd = SCM_FPORT_FDES (sock);
- if (getpeername (fd, addr, &addr_size) == -1)
+ if (getpeername (fd, (struct sockaddr *) &addr, &addr_size) == -1)
SCM_SYSERROR;
- return scm_addr_vector (addr, addr_size, FUNC_NAME);
+
+ return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
}
#undef FUNC_NAME
"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"
int flg;
char *dest;
size_t len;
+ SCM msg;
SCM_VALIDATE_OPFPORT (1, sock);
SCM_VALIDATE_STRING (2, buf);
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
"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
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 ();
SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
(SCM sock, SCM str, SCM flags, SCM start, SCM end),
- "Return data from the socket port @var{sock} and also\n"
- "information about where the data was received from.\n"
- "@var{sock} must already be bound to the address from which\n"
- "data is to be received. @code{str}, is a string into which the\n"
- "data will be written. The size of @var{str} limits the amount\n"
- "of data which can be received: in the case of packet protocols,\n"
- "if a packet larger than this limit is encountered then some\n"
- "data will be irrevocably lost.\n\n"
- "The optional @var{flags} argument is a value or bitwise OR of\n"
- "@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.\n\n"
- "The value returned is a pair: the @emph{car} is the number of\n"
- "bytes read from the socket and the @emph{cdr} an address object\n"
- "in the same form as returned by @code{accept}. The address\n"
- "will given as @code{#f} if not available, as is usually the\n"
- "case for stream sockets.\n\n"
- "The @var{start} and @var{end} arguments specify a substring of\n"
- "@var{str} to which the data should be written.\n\n"
- "Note that the data is read directly from the socket file\n"
- "descriptor: any unread buffered port data is ignored.")
+ "Receive data from socket port @var{sock} (which must be already\n"
+ "bound), returning the originating address as well as the data.\n"
+ "This is usually for use on datagram sockets, but can be used on\n"
+ "stream-oriented sockets too.\n"
+ "\n"
+ "The data received is stored in the given @var{str}, using\n"
+ "either the whole string or just the region between the optional\n"
+ "@var{start} and @var{end} positions. The size of @var{str}\n"
+ "limits the amount of data which can be received. For datagram\n"
+ "protocols, if a packet larger than this is received then excess\n"
+ "bytes are irrevocably lost.\n"
+ "\n"
+ "The return value is a pair. The @code{car} is the number of\n"
+ "bytes read. The @code{cdr} is a socket address object which is\n"
+ "where the data come from, or @code{#f} if the origin is\n"
+ "unknown.\n"
+ "\n"
+ "The optional @var{flags} argument is a or bitwise OR\n"
+ "(@code{logior}) of @code{MSG_OOB}, @code{MSG_PEEK},\n"
+ "@code{MSG_DONTROUTE} etc.\n"
+ "\n"
+ "Data is read directly from the socket file descriptor, any\n"
+ "buffered port data is ignored.\n"
+ "\n"
+ "On a GNU/Linux system @code{recvfrom!} is not multi-threading,\n"
+ "all threads stop while a @code{recvfrom!} call is in progress.\n"
+ "An application may need to use @code{select}, @code{O_NONBLOCK}\n"
+ "or @code{MSG_DONTWAIT} to avoid this.")
#define FUNC_NAME s_scm_recvfrom
{
int rv;
size_t cend;
SCM address;
socklen_t addr_size = MAX_ADDR_SIZE;
- char max_addr[MAX_ADDR_SIZE];
- struct sockaddr *addr = (struct sockaddr *) max_addr;
+ scm_t_max_sockaddr addr;
SCM_VALIDATE_OPFPORT (1, sock);
fd = SCM_FPORT_FDES (sock);
/* 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);
- addr->sa_family = AF_UNSPEC;
+ ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
cend - offset, flg,
- addr, &addr_size));
+ (struct sockaddr *) &addr, &addr_size));
scm_i_string_stop_writing ();
if (rv == -1)
SCM_SYSERROR;
- if (addr->sa_family != AF_UNSPEC)
- address = scm_addr_vector (addr, addr_size, FUNC_NAME);
+ if (((struct sockaddr *) &addr)->sa_family != AF_UNSPEC)
+ address = _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
else
address = SCM_BOOL_F;
scm_remember_upto_here_1 (str);
+
return scm_cons (scm_from_int (rv), address);
}
#undef FUNC_NAME
-SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
- (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags),
+SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
+ (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SCM args_and_flags),
"Transmit the string @var{message} on the socket port\n"
"@var{sock}. The\n"
"destination address is specified using the @var{fam},\n"
"@var{address} and\n"
- "@var{args_and_flags} arguments, in a similar way to the\n"
+ "@var{args_and_flags} arguments, or just a socket address object "
+ "returned by @code{make-socket-address}, in a similar way to the\n"
"@code{connect} procedure. @var{args_and_flags} contains\n"
"the usual connection arguments optionally followed by\n"
"a flags argument, which is a value or\n"
"set to be non-blocking.\n"
"Note that the data is written directly to the socket\n"
"file descriptor:\n"
- "any unflushed buffered port data is ignored.")
+ "any unflushed buffered port data is ignored.\n"
+ "This operation is defined only for strings containing codepoints\n"
+ "zero to 255.")
#define FUNC_NAME s_scm_sendto
{
int rv;
int fd;
int flg;
struct sockaddr *soka;
- int size;
+ size_t size;
sock = SCM_COERCE_OUTPORT (sock);
SCM_VALIDATE_FPORT (1, sock);
SCM_VALIDATE_STRING (2, message);
fd = SCM_FPORT_FDES (sock);
- soka = scm_fill_sockaddr (scm_to_int (fam), address, &args_and_flags, 4,
- FUNC_NAME, &size);
+
+ if (!scm_is_number (fam_or_sockaddr))
+ {
+ /* FAM_OR_SOCKADDR must actually be a `socket address' object. This
+ means that the following arguments, i.e. ADDRESS and those listed in
+ ARGS_AND_FLAGS, are the `MSG_' flags. */
+ soka = scm_to_sockaddr (fam_or_sockaddr, &size);
+ if (address != SCM_UNDEFINED)
+ args_and_flags = scm_cons (address, args_and_flags);
+ }
+ else
+ soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
+ &args_and_flags, 3, FUNC_NAME, &size);
+
if (scm_is_null (args_and_flags))
flg = 0;
else
scm_c_define ("SOCK_RDM", scm_from_int (SOCK_RDM));
#endif
- /* setsockopt level. */
+ /* setsockopt level.
+
+ SOL_IP, SOL_TCP and SOL_UDP are defined on gnu/linux, but not on for
+ instance NetBSD. We define IPPROTOs because that's what the posix spec
+ shows in its example at
+
+ http://www.opengroup.org/onlinepubs/007904975/functions/getsockopt.html
+ */
#ifdef SOL_SOCKET
scm_c_define ("SOL_SOCKET", scm_from_int (SOL_SOCKET));
#endif
-#ifdef SOL_IP
- scm_c_define ("SOL_IP", scm_from_int (SOL_IP));
+#ifdef IPPROTO_IP
+ scm_c_define ("IPPROTO_IP", scm_from_int (IPPROTO_IP));
#endif
-#ifdef SOL_TCP
- scm_c_define ("SOL_TCP", scm_from_int (SOL_TCP));
+#ifdef IPPROTO_TCP
+ scm_c_define ("IPPROTO_TCP", scm_from_int (IPPROTO_TCP));
#endif
-#ifdef SOL_UDP
- scm_c_define ("SOL_UDP", scm_from_int (SOL_UDP));
+#ifdef IPPROTO_UDP
+ scm_c_define ("IPPROTO_UDP", scm_from_int (IPPROTO_UDP));
#endif
/* setsockopt names. */
#endif
/* recv/send options. */
+#ifdef MSG_DONTWAIT
+ scm_c_define ("MSG_DONTWAIT", scm_from_int (MSG_DONTWAIT));
+#endif
#ifdef MSG_OOB
scm_c_define ("MSG_OOB", scm_from_int (MSG_OOB));
#endif