From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludovic.courtes@laas.fr (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.user Subject: Re: Exposing common type wrapping/unwrapping methods Date: Wed, 21 Sep 2005 11:16:47 +0200 Organization: LAAS-CNRS Message-ID: <87aci6u6f4.fsf@laas.fr> References: <87oecutxox.fsf@laas.fr> <87vf58cxxq.fsf@zagadka.de> <87k6kwopv5.fsf@laas.fr> <87fysk7ady.fsf@zagadka.de> <87mzmpmcm2.fsf@laas.fr> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1127295010 2104 80.91.229.2 (21 Sep 2005 09:30:10 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 21 Sep 2005 09:30:10 +0000 (UTC) Cc: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Sep 21 11:30:03 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1EI0tt-0008AK-BV for guile-user@m.gmane.org; Wed, 21 Sep 2005 11:28:26 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EI0tr-0001mF-Ek for guile-user@m.gmane.org; Wed, 21 Sep 2005 05:28:23 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1EI0pj-0000ls-Ar for guile-user@gnu.org; Wed, 21 Sep 2005 05:24:07 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1EI0pX-0000iQ-DC for guile-user@gnu.org; Wed, 21 Sep 2005 05:24:06 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EI0pX-0000a8-18 for guile-user@gnu.org; Wed, 21 Sep 2005 05:23:55 -0400 Original-Received: from [140.93.0.15] (helo=laas.laas.fr) by monty-python.gnu.org with esmtp (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA:24) (Exim 4.34) id 1EI0jm-00058a-Uq for guile-user@gnu.org; Wed, 21 Sep 2005 05:17:59 -0400 Original-Received: by laas.laas.fr (8.13.1/8.13.1) with SMTP id j8L9Hilv025040; Wed, 21 Sep 2005 11:17:53 +0200 (CEST) Original-To: Marius Vollmer X-URL: http://www.laas.fr/~lcourtes/ X-Revolutionary-Date: Jour des =?iso-8859-1?Q?R=E9compenses?= de =?iso-8859-1?Q?l'Ann=E9e?= 213 de la =?iso-8859-1?Q?R=E9volution?= X-PGP-Key-ID: 0xEB1F5364 X-PGP-Key: http://www.laas.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 821D 815D 902A 7EAB 5CEE D120 7FBA 3D4F EB1F 5364 X-OS: powerpc-unknown-linux-gnu In-Reply-To: <87mzmpmcm2.fsf@laas.fr> (Ludovic =?iso-8859-1?Q?Court=E8s's?= message of "Wed, 07 Sep 2005 11:49:25 +0200") User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) X-Spam-Score: 0 () X-Scanned-By: MIMEDefang at CNRS-LAAS X-MIME-Autoconverted: from 8bit to quoted-printable by laas.laas.fr id j8L9Hilv025040 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:4761 Archived-At: Hi, ludovic.courtes@laas.fr (Ludovic Court=E8s) writes: > Right. I followed your suggestion (with slight modifications, namely > have an ADDRESS_SIZE output parameter for functions that return a > pointer to `struct sockaddr') and updated my patch. However, the only > thing I tested is `scm_from_sockaddr ()' on AF_INET addresses. > > If this looks good to you, then I guess I can update the doc, see what > we can do with `scm_connect ()' and the likes, and submit a new patch. Below is an updated patch that fixes two bugs in `scm_to_sockaddr ()' and modifies `scm_connect ()', `scm_bind ()', and `scm_sendto ()' according to Marius' suggestion. This means that there are now two ways to use them from Scheme, but their C API remains unchanged. Here is an example: guile> (define s (socket AF_INET SOCK_STREAM 0)) guile> (define host (gethostbyname "www.gnu.org")) guile> host #("gnu.org" ("www.gnu.org") 2 4 (3353880842)) guile> (define sc (connect s AF_INET 3353880842 80)) guile> (define sa (make-socket-address AF_INET 3353880842 80)) guile> (define s2 (socket AF_INET SOCK_STREAM 0)) guile> (define sc2 (connect s2 sa)) Again, I only tested AF_INET addresses and I didn't try `sendto' and `bind'. Is there any test case out there or a program that could serve as a test case? Unfortunately, the old Guile-WWW available in Debian cannot be used with Guile 1.7 because of the unavailability of `make-shared-substring'. I also emailed `assign@gnu.org' so that they could send me the copyright assignment forms if that is necessary. Thanks, Ludovic. ChangeLog entry for libguile: 2005-09-21 Ludovic Court=E8s * socket.c (scm_fill_sockaddr): Added SCM_C_INLINE_KEYWORD. Changed the type of SIZE to `size_t'. (scm_connect): Accept a socket address object as the second argument. (scm_bind): Likewise. (scm_sendto): Likewise. (scm_addr_vector): Renamed to `_scm_from_sockaddr'. (scm_from_sockaddr): New function. (scm_to_sockaddr): New function. (scm_c_make_socket_address): New function. (scm_make_socket_address): New function. * socket.h: Added declarations of the above new functions. =0C --- orig/libguile/socket.c +++ mod/libguile/socket.c @@ -664,9 +664,9 @@ proc is the name of the original procedure. size returns the size of the structure allocated. */ =20 -static struct sockaddr * +static SCM_C_INLINE_KEYWORD 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) @@ -768,9 +768,9 @@ } } #undef FUNC_NAME - =20 -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" @@ -787,22 +787,32 @@ "@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; =20 sock =3D SCM_COERCE_OUTPORT (sock); SCM_VALIDATE_OPFPORT (1, sock); fd =3D SCM_FPORT_FDES (sock); - soka =3D scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_= NAME, - &size); + + if (address =3D=3D SCM_UNDEFINED) + /* No third argument was passed to FAM_OR_SOCKADDR must actually be = a + `socket address' object. */ + soka =3D scm_to_sockaddr (fam_or_sockaddr, &size); + else + soka =3D scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address, + &args, 3, FUNC_NAME, &size); + if (connect (fd, soka, size) =3D=3D -1) { int save_errno =3D errno; - =20 + free (soka); errno =3D save_errno; SCM_SYSERROR; @@ -812,8 +822,8 @@ } #undef FUNC_NAME =20 -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" @@ -846,22 +856,33 @@ "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; =20 sock =3D SCM_COERCE_OUTPORT (sock); SCM_VALIDATE_OPFPORT (1, sock); - soka =3D scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_= NAME, - &size); fd =3D SCM_FPORT_FDES (sock); + + if (address =3D=3D SCM_UNDEFINED) + /* No third argument was passed to FAM_OR_SOCKADDR must actually be = a + `socket address' object. */ + soka =3D scm_to_sockaddr (fam_or_sockaddr, &size); + else + soka =3D scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address, + &args, 3, FUNC_NAME, &size); + + if (bind (fd, soka, size) =3D=3D -1) { int save_errno =3D errno; - =20 + free (soka); errno =3D save_errno; SCM_SYSERROR; @@ -893,8 +914,8 @@ #undef FUNC_NAME =20 /* Put the components of a sockaddr into a new SCM vector. */ -static SCM -scm_addr_vector (const struct sockaddr *address, int addr_size,=20 +static SCM_C_INLINE_KEYWORD SCM +_scm_from_sockaddr (const struct sockaddr *address, unsigned addr_size, const char *proc) { short int fam =3D address->sa_family; @@ -953,12 +974,198 @@ break; #endif default: - scm_misc_error (proc, "Unrecognised address family: ~A", + result =3D SCM_UNSPECIFIED; + scm_misc_error (proc, "unrecognised address family: ~A", scm_list_1 (scm_from_int (fam))); + } return result; } =20 +/* 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 (address, addr_size, "scm_from_sockaddr")); +} + +/* 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 =3D NULL; + + SCM_VALIDATE_VECTOR (1, address); + + *address_size =3D 0; + family =3D scm_to_short (SCM_SIMPLE_VECTOR_REF (address, 0)); + + switch (family) + { + case AF_INET: + { + struct sockaddr_in *c_inet; + + if (SCM_SIMPLE_VECTOR_LENGTH (address) !=3D 3) + scm_misc_error (FUNC_NAME, "invalid inet address representation: ~A", + scm_list_1 (address)); + else + { + c_address =3D scm_malloc (sizeof (struct sockaddr_in)); + c_inet =3D (struct sockaddr_in *)c_address; + + c_inet->sin_addr.s_addr =3D + htonl (scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 1))); + c_inet->sin_port =3D + htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2))); + + *address_size =3D sizeof (*c_inet); + } + + break; + } + +#ifdef HAVE_IPV6 + case AF_INET6: + { + struct sockaddr_in6 *c_inet6; + + if (SCM_SIMPLE_VECTOR_LENGTH (address) !=3D 5) + scm_misc_error (FUNC_NAME, "invalid inet6 address representation: ~A"= , + scm_list_1 (address)); + else + { + c_address =3D scm_malloc (sizeof (struct sockaddr_in6)); + c_inet6 =3D (struct sockaddr_in6 *)c_address; + + scm_to_ipv6 (c_inet6->sin6_addr.s6_addr, address); + c_inet6->sin6_port =3D + htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2))); + c_inet6->sin6_flowinfo =3D + scm_to_uint32 (SCM_SIMPLE_VECTOR_REF (address, 3)); +#ifdef HAVE_SIN6_SCOPE_ID + c_inet6->sin6_scope_id =3D + scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 4)); +#endif + + *address_size =3D sizeof (*c_inet6); + } + + break; + } +#endif + +#ifdef HAVE_UNIX_DOMAIN_SOCKETS + case AF_UNIX: + { + if (SCM_SIMPLE_VECTOR_LENGTH (address) !=3D 2) + scm_misc_error (FUNC_NAME, "invalid unix address representation: ~A", + scm_list_1 (address)); + else + { + SCM path; + size_t path_len =3D 0; + + path =3D SCM_SIMPLE_VECTOR_REF (address, 1); + if ((!scm_is_string (path)) && (path !=3D 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 =3D=3D SCM_BOOL_F) + path_len =3D 0; + else + path_len =3D scm_c_string_length (path); + +#ifndef UNIX_PATH_MAX +/* We can hope that this limit will eventually vanish, at least in GNU. + However, currently, while glibc doesn't define `UNIX_PATH_MAX', it + documents it has being limited to 108 bytes. */ +# define UNIX_PATH_MAX 108 +#endif + if (path_len >=3D UNIX_PATH_MAX) + scm_misc_error (FUNC_NAME, "unix address path " + "too long: ~A", scm_list_1 (path)); + else + { + c_address =3D scm_malloc (sizeof (struct sockaddr_un)); + c_unix =3D (struct sockaddr_un *)c_address; + + if (path_len) + { + scm_to_locale_stringbuf (path, c_unix->sun_path, + UNIX_PATH_MAX); + c_unix->sun_path[path_len - 1] =3D '\0'; + } + else + c_unix->sun_path[0] =3D '\0'; + + *address_size =3D SUN_LEN (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 ma= y be + freed using `free ()'. */ +struct sockaddr * +scm_c_make_socket_address (SCM family, SCM address, SCM args, + size_t *address_size) +{ + size_t size; + struct sockaddr *soka; + + soka =3D scm_fill_sockaddr (scm_to_ushort (family), address, &args, 1, + "scm_c_make_socket_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 +{ + struct sockaddr *c_address; + size_t c_address_size; + + c_address =3D scm_c_make_socket_address (family, address, args, + &c_address_size); + if (!c_address) + return SCM_BOOL_F; + + return (scm_from_sockaddr (c_address, c_address_size)); +} +#undef FUNC_NAME + +=0C /* 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. */ @@ -1009,7 +1216,7 @@ if (newfd =3D=3D -1) SCM_SYSERROR; newsock =3D SCM_SOCK_FD_TO_PORT (newfd); - address =3D scm_addr_vector (addr, addr_size, FUNC_NAME); + address =3D _scm_from_sockaddr (addr, addr_size, FUNC_NAME); return scm_cons (newsock, address); } #undef FUNC_NAME @@ -1031,7 +1238,7 @@ fd =3D SCM_FPORT_FDES (sock); if (getsockname (fd, addr, &addr_size) =3D=3D -1) SCM_SYSERROR; - return scm_addr_vector (addr, addr_size, FUNC_NAME); + return _scm_from_sockaddr (addr, addr_size, FUNC_NAME); } #undef FUNC_NAME =20 @@ -1053,7 +1260,7 @@ fd =3D SCM_FPORT_FDES (sock); if (getpeername (fd, addr, &addr_size) =3D=3D -1) SCM_SYSERROR; - return scm_addr_vector (addr, addr_size, FUNC_NAME); + return _scm_from_sockaddr (addr, addr_size, FUNC_NAME); } #undef FUNC_NAME =20 @@ -1207,7 +1414,7 @@ if (rv =3D=3D -1) SCM_SYSERROR; if (addr->sa_family !=3D AF_UNSPEC) - address =3D scm_addr_vector (addr, addr_size, FUNC_NAME); + address =3D _scm_from_sockaddr (addr, addr_size, FUNC_NAME); else address =3D SCM_BOOL_F; =20 @@ -1216,13 +1423,14 @@ } #undef FUNC_NAME =20 -SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1, - (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_f= lags), +SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1, + (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SC= M 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" @@ -1241,14 +1449,26 @@ int fd; int flg; struct sockaddr *soka; - int size; + size_t size; =20 sock =3D SCM_COERCE_OUTPORT (sock); SCM_VALIDATE_FPORT (1, sock); SCM_VALIDATE_STRING (2, message); fd =3D SCM_FPORT_FDES (sock); - soka =3D 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. Th= is + means that the following arguments, i.e. ADDRESS and those listed in + ARGS_AND_FLAGS, are the `MSG_' flags. */ + soka =3D scm_to_sockaddr (fam_or_sockaddr, &size); + if (address !=3D SCM_UNDEFINED) + args_and_flags =3D scm_cons (address, args_and_flags); + } + else + soka =3D 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 =3D 0; else --- orig/libguile/socket.h +++ mod/libguile/socket.h @@ -54,6 +54,16 @@ SCM_API SCM scm_sendto (SCM sockfd, SCM message, SCM fam, SCM address, S= CM args_and_flags); SCM_API void scm_init_socket (void); =20 +/* Wrapping/unwrapping address objects. */ +struct sockaddr; +SCM_API SCM scm_from_sockaddr (const struct sockaddr *address, + unsigned addr_size); +SCM_API struct sockaddr *scm_to_sockaddr (SCM address, size_t *adress_si= ze); +SCM_API struct sockaddr *scm_c_make_socket_address (SCM family, SCM addr= ess, + SCM args, + size_t *address_size); +SCM_API SCM scm_make_socket_address (SCM family, SCM address, SCM args); + #endif /* SCM_SOCKET_H */ =20 /* _______________________________________________ Guile-user mailing list Guile-user@gnu.org http://lists.gnu.org/mailman/listinfo/guile-user