Modify socket and time functions for wide strings
[bpt/guile.git] / libguile / socket.c
1 /* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20 \f
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <gmp.h>
28
29 #include "libguile/_scm.h"
30 #include "libguile/unif.h"
31 #include "libguile/feature.h"
32 #include "libguile/fports.h"
33 #include "libguile/strings.h"
34 #include "libguile/vectors.h"
35 #include "libguile/dynwind.h"
36 #include "libguile/srfi-13.h"
37
38 #include "libguile/validate.h"
39 #include "libguile/socket.h"
40
41 #include "libguile/iselect.h"
42
43 #ifdef __MINGW32__
44 #include "win32-socket.h"
45 #endif
46
47 #ifdef HAVE_STDINT_H
48 #include <stdint.h>
49 #endif
50 #ifdef HAVE_STRING_H
51 #include <string.h>
52 #endif
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 #include <sys/types.h>
57 #ifdef HAVE_WINSOCK2_H
58 #include <winsock2.h>
59 #else
60 #include <sys/socket.h>
61 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
62 #include <sys/un.h>
63 #endif
64 #include <netinet/in.h>
65 #include <netdb.h>
66 #include <arpa/inet.h>
67 #endif
68
69 #if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
70 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
71 + strlen ((ptr)->sun_path))
72 #endif
73
74 /* The largest possible socket address. Wrapping it in a union guarantees
75 that the compiler will make it suitably aligned. */
76 typedef union
77 {
78 struct sockaddr sockaddr;
79 struct sockaddr_in sockaddr_in;
80
81 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
82 struct sockaddr_un sockaddr_un;
83 #endif
84 #ifdef HAVE_IPV6
85 struct sockaddr_in6 sockaddr_in6;
86 #endif
87 } scm_t_max_sockaddr;
88
89
90 /* Maximum size of a socket address. */
91 #define MAX_ADDR_SIZE (sizeof (scm_t_max_sockaddr))
92
93
94 \f
95
96 SCM_DEFINE (scm_htons, "htons", 1, 0, 0,
97 (SCM value),
98 "Convert a 16 bit quantity from host to network byte ordering.\n"
99 "@var{value} is packed into 2 bytes, which are then converted\n"
100 "and returned as a new integer.")
101 #define FUNC_NAME s_scm_htons
102 {
103 return scm_from_ushort (htons (scm_to_ushort (value)));
104 }
105 #undef FUNC_NAME
106
107 SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0,
108 (SCM value),
109 "Convert a 16 bit quantity from network to host byte ordering.\n"
110 "@var{value} is packed into 2 bytes, which are then converted\n"
111 "and returned as a new integer.")
112 #define FUNC_NAME s_scm_ntohs
113 {
114 return scm_from_ushort (ntohs (scm_to_ushort (value)));
115 }
116 #undef FUNC_NAME
117
118 SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0,
119 (SCM value),
120 "Convert a 32 bit quantity from host to network byte ordering.\n"
121 "@var{value} is packed into 4 bytes, which are then converted\n"
122 "and returned as a new integer.")
123 #define FUNC_NAME s_scm_htonl
124 {
125 return scm_from_ulong (htonl (scm_to_uint32 (value)));
126 }
127 #undef FUNC_NAME
128
129 SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0,
130 (SCM value),
131 "Convert a 32 bit quantity from network to host byte ordering.\n"
132 "@var{value} is packed into 4 bytes, which are then converted\n"
133 "and returned as a new integer.")
134 #define FUNC_NAME s_scm_ntohl
135 {
136 return scm_from_ulong (ntohl (scm_to_uint32 (value)));
137 }
138 #undef FUNC_NAME
139
140 #ifndef HAVE_INET_ATON
141 /* for our definition in inet_aton.c, not usually needed. */
142 extern int inet_aton ();
143 #endif
144
145 SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0,
146 (SCM address),
147 "Convert an IPv4 Internet address from printable string\n"
148 "(dotted decimal notation) to an integer. E.g.,\n\n"
149 "@lisp\n"
150 "(inet-aton \"127.0.0.1\") @result{} 2130706433\n"
151 "@end lisp")
152 #define FUNC_NAME s_scm_inet_aton
153 {
154 struct in_addr soka;
155 char *c_address;
156 int rv;
157
158 c_address = scm_to_locale_string (address);
159 rv = inet_aton (c_address, &soka);
160 free (c_address);
161 if (rv == 0)
162 SCM_MISC_ERROR ("bad address", SCM_EOL);
163 return scm_from_ulong (ntohl (soka.s_addr));
164 }
165 #undef FUNC_NAME
166
167
168 SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0,
169 (SCM inetid),
170 "Convert an IPv4 Internet address to a printable\n"
171 "(dotted decimal notation) string. E.g.,\n\n"
172 "@lisp\n"
173 "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n"
174 "@end lisp")
175 #define FUNC_NAME s_scm_inet_ntoa
176 {
177 struct in_addr addr;
178 char *s;
179 SCM answer;
180 addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid));
181 s = inet_ntoa (addr);
182 answer = scm_from_locale_string (s);
183 return answer;
184 }
185 #undef FUNC_NAME
186
187 #ifdef HAVE_INET_NETOF
188 SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0,
189 (SCM address),
190 "Return the network number part of the given IPv4\n"
191 "Internet address. E.g.,\n\n"
192 "@lisp\n"
193 "(inet-netof 2130706433) @result{} 127\n"
194 "@end lisp")
195 #define FUNC_NAME s_scm_inet_netof
196 {
197 struct in_addr addr;
198 addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
199 return scm_from_ulong (inet_netof (addr));
200 }
201 #undef FUNC_NAME
202 #endif
203
204 #ifdef HAVE_INET_LNAOF
205 SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0,
206 (SCM address),
207 "Return the local-address-with-network part of the given\n"
208 "IPv4 Internet address, using the obsolete class A/B/C system.\n"
209 "E.g.,\n\n"
210 "@lisp\n"
211 "(inet-lnaof 2130706433) @result{} 1\n"
212 "@end lisp")
213 #define FUNC_NAME s_scm_lnaof
214 {
215 struct in_addr addr;
216 addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
217 return scm_from_ulong (inet_lnaof (addr));
218 }
219 #undef FUNC_NAME
220 #endif
221
222 #ifdef HAVE_INET_MAKEADDR
223 SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
224 (SCM net, SCM lna),
225 "Make an IPv4 Internet address by combining the network number\n"
226 "@var{net} with the local-address-within-network number\n"
227 "@var{lna}. E.g.,\n\n"
228 "@lisp\n"
229 "(inet-makeaddr 127 1) @result{} 2130706433\n"
230 "@end lisp")
231 #define FUNC_NAME s_scm_inet_makeaddr
232 {
233 struct in_addr addr;
234 unsigned long netnum;
235 unsigned long lnanum;
236
237 netnum = SCM_NUM2ULONG (1, net);
238 lnanum = SCM_NUM2ULONG (2, lna);
239 addr = inet_makeaddr (netnum, lnanum);
240 return scm_from_ulong (ntohl (addr.s_addr));
241 }
242 #undef FUNC_NAME
243 #endif
244
245 #ifdef HAVE_IPV6
246
247 /* flip a 128 bit IPv6 address between host and network order. */
248 #ifdef WORDS_BIGENDIAN
249 #define FLIP_NET_HOST_128(addr)
250 #else
251 #define FLIP_NET_HOST_128(addr)\
252 {\
253 int i;\
254 \
255 for (i = 0; i < 8; i++)\
256 {\
257 scm_t_uint8 c = (addr)[i];\
258 \
259 (addr)[i] = (addr)[15 - i];\
260 (addr)[15 - i] = c;\
261 }\
262 }
263 #endif
264
265 #ifdef WORDS_BIGENDIAN
266 #define FLIPCPY_NET_HOST_128(dest, src) memcpy (dest, src, 16)
267 #else
268 #define FLIPCPY_NET_HOST_128(dest, src) \
269 { \
270 const scm_t_uint8 *tmp_srcp = (src) + 15; \
271 scm_t_uint8 *tmp_destp = (dest); \
272 \
273 do { \
274 *tmp_destp++ = *tmp_srcp--; \
275 } while (tmp_srcp != (src)); \
276 }
277 #endif
278
279
280 #if (SIZEOF_SCM_T_BITS * SCM_CHAR_BIT) > 128
281 #error "Assumption that scm_t_bits <= 128 bits has been violated."
282 #endif
283
284 #if (SIZEOF_UNSIGNED_LONG * SCM_CHAR_BIT) > 128
285 #error "Assumption that unsigned long <= 128 bits has been violated."
286 #endif
287
288 #if (SIZEOF_UNSIGNED_LONG_LONG * SCM_CHAR_BIT) > 128
289 #error "Assumption that unsigned long long <= 128 bits has been violated."
290 #endif
291
292 /* convert a 128 bit IPv6 address in network order to a host ordered
293 SCM integer. */
294 static SCM
295 scm_from_ipv6 (const scm_t_uint8 *src)
296 {
297 SCM result = scm_i_mkbig ();
298 mpz_import (SCM_I_BIG_MPZ (result),
299 1, /* chunk */
300 1, /* big-endian chunk ordering */
301 16, /* chunks are 16 bytes long */
302 1, /* big-endian byte ordering */
303 0, /* "nails" -- leading unused bits per chunk */
304 src);
305 return scm_i_normbig (result);
306 }
307
308 /* convert a host ordered SCM integer to a 128 bit IPv6 address in
309 network order. */
310 static void
311 scm_to_ipv6 (scm_t_uint8 dst[16], SCM src)
312 {
313 if (SCM_I_INUMP (src))
314 {
315 scm_t_signed_bits n = SCM_I_INUM (src);
316 if (n < 0)
317 scm_out_of_range (NULL, src);
318 #ifdef WORDS_BIGENDIAN
319 memset (dst, 0, 16 - sizeof (scm_t_signed_bits));
320 memcpy (dst + (16 - sizeof (scm_t_signed_bits)),
321 &n,
322 sizeof (scm_t_signed_bits));
323 #else
324 memset (dst + sizeof (scm_t_signed_bits),
325 0,
326 16 - sizeof (scm_t_signed_bits));
327 /* FIXME: this pair of ops is kinda wasteful -- should rewrite as
328 a single loop perhaps, similar to the handling of bignums. */
329 memcpy (dst, &n, sizeof (scm_t_signed_bits));
330 FLIP_NET_HOST_128 (dst);
331 #endif
332 }
333 else if (SCM_BIGP (src))
334 {
335 size_t count;
336
337 if ((mpz_sgn (SCM_I_BIG_MPZ (src)) < 0)
338 || mpz_sizeinbase (SCM_I_BIG_MPZ (src), 2) > 128)
339 scm_out_of_range (NULL, src);
340
341 memset (dst, 0, 16);
342 mpz_export (dst,
343 &count,
344 1, /* big-endian chunk ordering */
345 16, /* chunks are 16 bytes long */
346 1, /* big-endian byte ordering */
347 0, /* "nails" -- leading unused bits per chunk */
348 SCM_I_BIG_MPZ (src));
349 scm_remember_upto_here_1 (src);
350 }
351 else
352 scm_wrong_type_arg (NULL, 0, src);
353 }
354
355 #ifdef HAVE_INET_PTON
356 SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
357 (SCM family, SCM address),
358 "Convert a string containing a printable network address to\n"
359 "an integer address. Note that unlike the C version of this\n"
360 "function,\n"
361 "the result is an integer with normal host byte ordering.\n"
362 "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
363 "@lisp\n"
364 "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n"
365 "(inet-pton AF_INET6 \"::1\") @result{} 1\n"
366 "@end lisp")
367 #define FUNC_NAME s_scm_inet_pton
368 {
369 int af;
370 char *src;
371 scm_t_uint32 dst[4];
372 int rv, eno;
373
374 af = scm_to_int (family);
375 SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
376 src = scm_to_locale_string (address);
377 rv = inet_pton (af, src, dst);
378 eno = errno;
379 free (src);
380 errno = eno;
381 if (rv == -1)
382 SCM_SYSERROR;
383 else if (rv == 0)
384 SCM_MISC_ERROR ("Bad address", SCM_EOL);
385 if (af == AF_INET)
386 return scm_from_ulong (ntohl (*dst));
387 else
388 return scm_from_ipv6 ((scm_t_uint8 *) dst);
389 }
390 #undef FUNC_NAME
391 #endif
392
393 #ifdef HAVE_INET_NTOP
394 SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
395 (SCM family, SCM address),
396 "Convert a network address into a printable string.\n"
397 "Note that unlike the C version of this function,\n"
398 "the input is an integer with normal host byte ordering.\n"
399 "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
400 "@lisp\n"
401 "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n"
402 "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n"
403 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"
404 "@end lisp")
405 #define FUNC_NAME s_scm_inet_ntop
406 {
407 int af;
408 #ifdef INET6_ADDRSTRLEN
409 char dst[INET6_ADDRSTRLEN];
410 #else
411 char dst[46];
412 #endif
413 const char *result;
414
415 af = scm_to_int (family);
416 SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
417 if (af == AF_INET)
418 {
419 scm_t_uint32 addr4;
420
421 addr4 = htonl (SCM_NUM2ULONG (2, address));
422 result = inet_ntop (af, &addr4, dst, sizeof (dst));
423 }
424 else
425 {
426 char addr6[16];
427
428 scm_to_ipv6 ((scm_t_uint8 *) addr6, address);
429 result = inet_ntop (af, &addr6, dst, sizeof (dst));
430 }
431
432 if (result == NULL)
433 SCM_SYSERROR;
434
435 return scm_from_locale_string (dst);
436 }
437 #undef FUNC_NAME
438 #endif
439
440 #endif /* HAVE_IPV6 */
441
442 SCM_SYMBOL (sym_socket, "socket");
443
444 #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
445
446 SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
447 (SCM family, SCM style, SCM proto),
448 "Return a new socket port of the type specified by @var{family},\n"
449 "@var{style} and @var{proto}. All three parameters are\n"
450 "integers. Supported values for @var{family} are\n"
451 "@code{AF_UNIX}, @code{AF_INET} and @code{AF_INET6}.\n"
452 "Typical values for @var{style} are @code{SOCK_STREAM},\n"
453 "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n"
454 "@var{proto} can be obtained from a protocol name using\n"
455 "@code{getprotobyname}. A value of zero specifies the default\n"
456 "protocol, which is usually right.\n\n"
457 "A single socket port cannot by used for communication until it\n"
458 "has been connected to another socket.")
459 #define FUNC_NAME s_scm_socket
460 {
461 int fd;
462
463 fd = socket (scm_to_int (family),
464 scm_to_int (style),
465 scm_to_int (proto));
466 if (fd == -1)
467 SCM_SYSERROR;
468 return SCM_SOCK_FD_TO_PORT (fd);
469 }
470 #undef FUNC_NAME
471
472 #ifdef HAVE_SOCKETPAIR
473 SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
474 (SCM family, SCM style, SCM proto),
475 "Return a pair of connected (but unnamed) socket ports of the\n"
476 "type specified by @var{family}, @var{style} and @var{proto}.\n"
477 "Many systems support only socket pairs of the @code{AF_UNIX}\n"
478 "family. Zero is likely to be the only meaningful value for\n"
479 "@var{proto}.")
480 #define FUNC_NAME s_scm_socketpair
481 {
482 int fam;
483 int fd[2];
484
485 fam = scm_to_int (family);
486
487 if (socketpair (fam, scm_to_int (style), scm_to_int (proto), fd) == -1)
488 SCM_SYSERROR;
489
490 return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
491 }
492 #undef FUNC_NAME
493 #endif
494
495 /* Possible results for `getsockopt ()'. Wrapping it into a union guarantees
496 suitable alignment. */
497 typedef union
498 {
499 #ifdef HAVE_STRUCT_LINGER
500 struct linger linger;
501 #endif
502 size_t size;
503 int integer;
504 } scm_t_getsockopt_result;
505
506 SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
507 (SCM sock, SCM level, SCM optname),
508 "Return an option value from socket port @var{sock}.\n"
509 "\n"
510 "@var{level} is an integer specifying a protocol layer, either\n"
511 "@code{SOL_SOCKET} for socket level options, or a protocol\n"
512 "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
513 "(@pxref{Network Databases}).\n"
514 "\n"
515 "@defvar SOL_SOCKET\n"
516 "@defvarx IPPROTO_IP\n"
517 "@defvarx IPPROTO_TCP\n"
518 "@defvarx IPPROTO_UDP\n"
519 "@end defvar\n"
520 "\n"
521 "@var{optname} is an integer specifying an option within the\n"
522 "protocol layer.\n"
523 "\n"
524 "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
525 "defined (when provided by the system). For their meaning see\n"
526 "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
527 "Manual}, or @command{man 7 socket}.\n"
528 "\n"
529 "@defvar SO_DEBUG\n"
530 "@defvarx SO_REUSEADDR\n"
531 "@defvarx SO_STYLE\n"
532 "@defvarx SO_TYPE\n"
533 "@defvarx SO_ERROR\n"
534 "@defvarx SO_DONTROUTE\n"
535 "@defvarx SO_BROADCAST\n"
536 "@defvarx SO_SNDBUF\n"
537 "@defvarx SO_RCVBUF\n"
538 "@defvarx SO_KEEPALIVE\n"
539 "@defvarx SO_OOBINLINE\n"
540 "@defvarx SO_NO_CHECK\n"
541 "@defvarx SO_PRIORITY\n"
542 "The value returned is an integer.\n"
543 "@end defvar\n"
544 "\n"
545 "@defvar SO_LINGER\n"
546 "The @var{value} returned is a pair of integers\n"
547 "@code{(@var{ENABLE} . @var{TIMEOUT})}. On old systems without\n"
548 "timeout support (ie.@: without @code{struct linger}), only\n"
549 "@var{ENABLE} has an effect but the value in Guile is always a\n"
550 "pair.\n"
551 "@end defvar")
552 #define FUNC_NAME s_scm_getsockopt
553 {
554 int fd;
555 /* size of optval is the largest supported option. */
556 scm_t_getsockopt_result optval;
557 socklen_t optlen = sizeof (optval);
558 int ilevel;
559 int ioptname;
560
561 sock = SCM_COERCE_OUTPORT (sock);
562 SCM_VALIDATE_OPFPORT (1, sock);
563 ilevel = scm_to_int (level);
564 ioptname = scm_to_int (optname);
565
566 fd = SCM_FPORT_FDES (sock);
567 if (getsockopt (fd, ilevel, ioptname, (void *) &optval, &optlen) == -1)
568 SCM_SYSERROR;
569
570 if (ilevel == SOL_SOCKET)
571 {
572 #ifdef SO_LINGER
573 if (ioptname == SO_LINGER)
574 {
575 #ifdef HAVE_STRUCT_LINGER
576 struct linger *ling = (struct linger *) &optval;
577
578 return scm_cons (scm_from_long (ling->l_onoff),
579 scm_from_long (ling->l_linger));
580 #else
581 return scm_cons (scm_from_long (*(int *) &optval),
582 scm_from_int (0));
583 #endif
584 }
585 else
586 #endif
587 if (0
588 #ifdef SO_SNDBUF
589 || ioptname == SO_SNDBUF
590 #endif
591 #ifdef SO_RCVBUF
592 || ioptname == SO_RCVBUF
593 #endif
594 )
595 {
596 return scm_from_size_t (*(size_t *) &optval);
597 }
598 }
599 return scm_from_int (*(int *) &optval);
600 }
601 #undef FUNC_NAME
602
603 SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
604 (SCM sock, SCM level, SCM optname, SCM value),
605 "Set an option on socket port @var{sock}. The return value is\n"
606 "unspecified.\n"
607 "\n"
608 "@var{level} is an integer specifying a protocol layer, either\n"
609 "@code{SOL_SOCKET} for socket level options, or a protocol\n"
610 "number from the @code{IPPROTO} constants or @code{getprotoent}\n"
611 "(@pxref{Network Databases}).\n"
612 "\n"
613 "@defvar SOL_SOCKET\n"
614 "@defvarx IPPROTO_IP\n"
615 "@defvarx IPPROTO_TCP\n"
616 "@defvarx IPPROTO_UDP\n"
617 "@end defvar\n"
618 "\n"
619 "@var{optname} is an integer specifying an option within the\n"
620 "protocol layer.\n"
621 "\n"
622 "For @code{SOL_SOCKET} level the following @var{optname}s are\n"
623 "defined (when provided by the system). For their meaning see\n"
624 "@ref{Socket-Level Options,,, libc, The GNU C Library Reference\n"
625 "Manual}, or @command{man 7 socket}.\n"
626 "\n"
627 "@defvar SO_DEBUG\n"
628 "@defvarx SO_REUSEADDR\n"
629 "@defvarx SO_STYLE\n"
630 "@defvarx SO_TYPE\n"
631 "@defvarx SO_ERROR\n"
632 "@defvarx SO_DONTROUTE\n"
633 "@defvarx SO_BROADCAST\n"
634 "@defvarx SO_SNDBUF\n"
635 "@defvarx SO_RCVBUF\n"
636 "@defvarx SO_KEEPALIVE\n"
637 "@defvarx SO_OOBINLINE\n"
638 "@defvarx SO_NO_CHECK\n"
639 "@defvarx SO_PRIORITY\n"
640 "@var{value} is an integer.\n"
641 "@end defvar\n"
642 "\n"
643 "@defvar SO_LINGER\n"
644 "@var{value} is a pair of integers @code{(@var{ENABLE}\n"
645 ". @var{TIMEOUT})}. On old systems without timeout support\n"
646 "(ie.@: without @code{struct linger}), only @var{ENABLE} has an\n"
647 "effect but the value in Guile is always a pair.\n"
648 "@end defvar\n"
649 "\n"
650 "@c Note that we refer only to ``man ip'' here. On GNU/Linux it's\n"
651 "@c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.\n"
652 "@c \n"
653 "For IP level (@code{IPPROTO_IP}) the following @var{optname}s\n"
654 "are defined (when provided by the system). See @command{man\n"
655 "ip} for what they mean.\n"
656 "\n"
657 "@defvar IP_ADD_MEMBERSHIP\n"
658 "@defvarx IP_DROP_MEMBERSHIP\n"
659 "These can be used only with @code{setsockopt}, not\n"
660 "@code{getsockopt}. @var{value} is a pair\n"
661 "@code{(@var{MULTIADDR} . @var{INTERFACEADDR})} of IPv4\n"
662 "addresses (@pxref{Network Address Conversion}).\n"
663 "@var{MULTIADDR} is a multicast address to be added to or\n"
664 "dropped from the interface @var{INTERFACEADDR}.\n"
665 "@var{INTERFACEADDR} can be @code{INADDR_ANY} to have the system\n"
666 "select the interface. @var{INTERFACEADDR} can also be an\n"
667 "interface index number, on systems supporting that.\n"
668 "@end defvar")
669 #define FUNC_NAME s_scm_setsockopt
670 {
671 int fd;
672
673 int opt_int;
674 #ifdef HAVE_STRUCT_LINGER
675 struct linger opt_linger;
676 #endif
677
678 #if HAVE_STRUCT_IP_MREQ
679 struct ip_mreq opt_mreq;
680 #endif
681
682 const void *optval = NULL;
683 socklen_t optlen = 0;
684
685 int ilevel, ioptname;
686
687 sock = SCM_COERCE_OUTPORT (sock);
688
689 SCM_VALIDATE_OPFPORT (1, sock);
690 ilevel = scm_to_int (level);
691 ioptname = scm_to_int (optname);
692
693 fd = SCM_FPORT_FDES (sock);
694
695 if (ilevel == SOL_SOCKET)
696 {
697 #ifdef SO_LINGER
698 if (ioptname == SO_LINGER)
699 {
700 #ifdef HAVE_STRUCT_LINGER
701 SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
702 opt_linger.l_onoff = scm_to_int (SCM_CAR (value));
703 opt_linger.l_linger = scm_to_int (SCM_CDR (value));
704 optlen = sizeof (struct linger);
705 optval = &opt_linger;
706 #else
707 SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
708 opt_int = scm_to_int (SCM_CAR (value));
709 /* timeout is ignored, but may as well validate it. */
710 scm_to_int (SCM_CDR (value));
711 optlen = sizeof (int);
712 optval = &opt_int;
713 #endif
714 }
715 else
716 #endif
717 if (0
718 #ifdef SO_SNDBUF
719 || ioptname == SO_SNDBUF
720 #endif
721 #ifdef SO_RCVBUF
722 || ioptname == SO_RCVBUF
723 #endif
724 )
725 {
726 opt_int = scm_to_int (value);
727 optlen = sizeof (size_t);
728 optval = &opt_int;
729 }
730 }
731
732 #if HAVE_STRUCT_IP_MREQ
733 if (ilevel == IPPROTO_IP &&
734 (ioptname == IP_ADD_MEMBERSHIP || ioptname == IP_DROP_MEMBERSHIP))
735 {
736 /* Fourth argument must be a pair of addresses. */
737 SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
738 opt_mreq.imr_multiaddr.s_addr = htonl (scm_to_ulong (SCM_CAR (value)));
739 opt_mreq.imr_interface.s_addr = htonl (scm_to_ulong (SCM_CDR (value)));
740 optlen = sizeof (opt_mreq);
741 optval = &opt_mreq;
742 }
743 #endif
744
745 if (optval == NULL)
746 {
747 /* Most options take an int. */
748 opt_int = scm_to_int (value);
749 optlen = sizeof (int);
750 optval = &opt_int;
751 }
752
753 if (setsockopt (fd, ilevel, ioptname, optval, optlen) == -1)
754 SCM_SYSERROR;
755 return SCM_UNSPECIFIED;
756 }
757 #undef FUNC_NAME
758
759 SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
760 (SCM sock, SCM how),
761 "Sockets can be closed simply by using @code{close-port}. The\n"
762 "@code{shutdown} procedure allows reception or transmission on a\n"
763 "connection to be shut down individually, according to the parameter\n"
764 "@var{how}:\n\n"
765 "@table @asis\n"
766 "@item 0\n"
767 "Stop receiving data for this socket. If further data arrives, reject it.\n"
768 "@item 1\n"
769 "Stop trying to transmit data from this socket. Discard any\n"
770 "data waiting to be sent. Stop looking for acknowledgement of\n"
771 "data already sent; don't retransmit it if it is lost.\n"
772 "@item 2\n"
773 "Stop both reception and transmission.\n"
774 "@end table\n\n"
775 "The return value is unspecified.")
776 #define FUNC_NAME s_scm_shutdown
777 {
778 int fd;
779 sock = SCM_COERCE_OUTPORT (sock);
780 SCM_VALIDATE_OPFPORT (1, sock);
781 fd = SCM_FPORT_FDES (sock);
782 if (shutdown (fd, scm_to_signed_integer (how, 0, 2)) == -1)
783 SCM_SYSERROR;
784 return SCM_UNSPECIFIED;
785 }
786 #undef FUNC_NAME
787
788 /* convert fam/address/args into a sockaddr of the appropriate type.
789 args is modified by removing the arguments actually used.
790 which_arg and proc are used when reporting errors:
791 which_arg is the position of address in the original argument list.
792 proc is the name of the original procedure.
793 size returns the size of the structure allocated. */
794
795 static struct sockaddr *
796 scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
797 const char *proc, size_t *size)
798 #define FUNC_NAME proc
799 {
800 switch (fam)
801 {
802 case AF_INET:
803 {
804 struct sockaddr_in *soka;
805 unsigned long addr;
806 int port;
807
808 SCM_VALIDATE_ULONG_COPY (which_arg, address, addr);
809 SCM_VALIDATE_CONS (which_arg + 1, *args);
810 port = scm_to_int (SCM_CAR (*args));
811 *args = SCM_CDR (*args);
812 soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in));
813
814 #if HAVE_STRUCT_SOCKADDR_SIN_LEN
815 soka->sin_len = sizeof (struct sockaddr_in);
816 #endif
817 soka->sin_family = AF_INET;
818 soka->sin_addr.s_addr = htonl (addr);
819 soka->sin_port = htons (port);
820 *size = sizeof (struct sockaddr_in);
821 return (struct sockaddr *) soka;
822 }
823 #ifdef HAVE_IPV6
824 case AF_INET6:
825 {
826 /* see RFC2553. */
827 int port;
828 struct sockaddr_in6 *soka;
829 unsigned long flowinfo = 0;
830 unsigned long scope_id = 0;
831
832 SCM_VALIDATE_CONS (which_arg + 1, *args);
833 port = scm_to_int (SCM_CAR (*args));
834 *args = SCM_CDR (*args);
835 if (scm_is_pair (*args))
836 {
837 SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo);
838 *args = SCM_CDR (*args);
839 if (scm_is_pair (*args))
840 {
841 SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args),
842 scope_id);
843 *args = SCM_CDR (*args);
844 }
845 }
846 soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6));
847
848 #if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
849 soka->sin6_len = sizeof (struct sockaddr_in6);
850 #endif
851 soka->sin6_family = AF_INET6;
852 scm_to_ipv6 (soka->sin6_addr.s6_addr, address);
853 soka->sin6_port = htons (port);
854 soka->sin6_flowinfo = flowinfo;
855 #ifdef HAVE_SIN6_SCOPE_ID
856 soka->sin6_scope_id = scope_id;
857 #endif
858 *size = sizeof (struct sockaddr_in6);
859 return (struct sockaddr *) soka;
860 }
861 #endif
862 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
863 case AF_UNIX:
864 {
865 struct sockaddr_un *soka;
866 int addr_size;
867 char *c_address;
868
869 scm_dynwind_begin (0);
870
871 c_address = scm_to_locale_string (address);
872 scm_dynwind_free (c_address);
873
874 /* the static buffer size in sockaddr_un seems to be arbitrary
875 and not necessarily a hard limit. e.g., the glibc manual
876 suggests it may be possible to declare it size 0. let's
877 ignore it. if the O/S doesn't like the size it will cause
878 connect/bind etc., to fail. sun_path is always the last
879 member of the structure. */
880 addr_size = sizeof (struct sockaddr_un)
881 + max (0, strlen (c_address) + 1 - (sizeof soka->sun_path));
882 soka = (struct sockaddr_un *) scm_malloc (addr_size);
883 memset (soka, 0, addr_size); /* for sun_len: see sin_len above. */
884 soka->sun_family = AF_UNIX;
885 strcpy (soka->sun_path, c_address);
886 *size = SUN_LEN (soka);
887
888 scm_dynwind_end ();
889 return (struct sockaddr *) soka;
890 }
891 #endif
892 default:
893 scm_out_of_range (proc, scm_from_int (fam));
894 }
895 }
896 #undef FUNC_NAME
897
898 SCM_DEFINE (scm_connect, "connect", 2, 1, 1,
899 (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
900 "Initiate a connection from a socket using a specified address\n"
901 "family to the address\n"
902 "specified by @var{address} and possibly @var{args}.\n"
903 "The format required for @var{address}\n"
904 "and @var{args} depends on the family of the socket.\n\n"
905 "For a socket of family @code{AF_UNIX},\n"
906 "only @var{address} is specified and must be a string with the\n"
907 "filename where the socket is to be created.\n\n"
908 "For a socket of family @code{AF_INET},\n"
909 "@var{address} must be an integer IPv4 host address and\n"
910 "@var{args} must be a single integer port number.\n\n"
911 "For a socket of family @code{AF_INET6},\n"
912 "@var{address} must be an integer IPv6 host address and\n"
913 "@var{args} may be up to three integers:\n"
914 "port [flowinfo] [scope_id],\n"
915 "where flowinfo and scope_id default to zero.\n\n"
916 "Alternatively, the second argument can be a socket address object "
917 "as returned by @code{make-socket-address}, in which case the "
918 "no additional arguments should be passed.\n\n"
919 "The return value is unspecified.")
920 #define FUNC_NAME s_scm_connect
921 {
922 int fd;
923 struct sockaddr *soka;
924 size_t size;
925
926 sock = SCM_COERCE_OUTPORT (sock);
927 SCM_VALIDATE_OPFPORT (1, sock);
928 fd = SCM_FPORT_FDES (sock);
929
930 if (address == SCM_UNDEFINED)
931 /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
932 `socket address' object. */
933 soka = scm_to_sockaddr (fam_or_sockaddr, &size);
934 else
935 soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
936 &args, 3, FUNC_NAME, &size);
937
938 if (connect (fd, soka, size) == -1)
939 {
940 int save_errno = errno;
941
942 free (soka);
943 errno = save_errno;
944 SCM_SYSERROR;
945 }
946 free (soka);
947 return SCM_UNSPECIFIED;
948 }
949 #undef FUNC_NAME
950
951 SCM_DEFINE (scm_bind, "bind", 2, 1, 1,
952 (SCM sock, SCM fam_or_sockaddr, SCM address, SCM args),
953 "Assign an address to the socket port @var{sock}.\n"
954 "Generally this only needs to be done for server sockets,\n"
955 "so they know where to look for incoming connections. A socket\n"
956 "without an address will be assigned one automatically when it\n"
957 "starts communicating.\n\n"
958 "The format of @var{address} and @var{args} depends\n"
959 "on the family of the socket.\n\n"
960 "For a socket of family @code{AF_UNIX}, only @var{address}\n"
961 "is specified and must be a string with the filename where\n"
962 "the socket is to be created.\n\n"
963 "For a socket of family @code{AF_INET}, @var{address}\n"
964 "must be an integer IPv4 address and @var{args}\n"
965 "must be a single integer port number.\n\n"
966 "The values of the following variables can also be used for\n"
967 "@var{address}:\n\n"
968 "@defvar INADDR_ANY\n"
969 "Allow connections from any address.\n"
970 "@end defvar\n\n"
971 "@defvar INADDR_LOOPBACK\n"
972 "The address of the local host using the loopback device.\n"
973 "@end defvar\n\n"
974 "@defvar INADDR_BROADCAST\n"
975 "The broadcast address on the local network.\n"
976 "@end defvar\n\n"
977 "@defvar INADDR_NONE\n"
978 "No address.\n"
979 "@end defvar\n\n"
980 "For a socket of family @code{AF_INET6}, @var{address}\n"
981 "must be an integer IPv6 address and @var{args}\n"
982 "may be up to three integers:\n"
983 "port [flowinfo] [scope_id],\n"
984 "where flowinfo and scope_id default to zero.\n\n"
985 "Alternatively, the second argument can be a socket address object "
986 "as returned by @code{make-socket-address}, in which case the "
987 "no additional arguments should be passed.\n\n"
988 "The return value is unspecified.")
989 #define FUNC_NAME s_scm_bind
990 {
991 struct sockaddr *soka;
992 size_t size;
993 int fd;
994
995 sock = SCM_COERCE_OUTPORT (sock);
996 SCM_VALIDATE_OPFPORT (1, sock);
997 fd = SCM_FPORT_FDES (sock);
998
999 if (address == SCM_UNDEFINED)
1000 /* No third argument was passed to FAM_OR_SOCKADDR must actually be a
1001 `socket address' object. */
1002 soka = scm_to_sockaddr (fam_or_sockaddr, &size);
1003 else
1004 soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
1005 &args, 3, FUNC_NAME, &size);
1006
1007
1008 if (bind (fd, soka, size) == -1)
1009 {
1010 int save_errno = errno;
1011
1012 free (soka);
1013 errno = save_errno;
1014 SCM_SYSERROR;
1015 }
1016 free (soka);
1017 return SCM_UNSPECIFIED;
1018 }
1019 #undef FUNC_NAME
1020
1021 SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
1022 (SCM sock, SCM backlog),
1023 "Enable @var{sock} to accept connection\n"
1024 "requests. @var{backlog} is an integer specifying\n"
1025 "the maximum length of the queue for pending connections.\n"
1026 "If the queue fills, new clients will fail to connect until\n"
1027 "the server calls @code{accept} to accept a connection from\n"
1028 "the queue.\n\n"
1029 "The return value is unspecified.")
1030 #define FUNC_NAME s_scm_listen
1031 {
1032 int fd;
1033 sock = SCM_COERCE_OUTPORT (sock);
1034 SCM_VALIDATE_OPFPORT (1, sock);
1035 fd = SCM_FPORT_FDES (sock);
1036 if (listen (fd, scm_to_int (backlog)) == -1)
1037 SCM_SYSERROR;
1038 return SCM_UNSPECIFIED;
1039 }
1040 #undef FUNC_NAME
1041
1042 /* Put the components of a sockaddr into a new SCM vector. */
1043 static SCM_C_INLINE_KEYWORD SCM
1044 _scm_from_sockaddr (const scm_t_max_sockaddr *address, unsigned addr_size,
1045 const char *proc)
1046 {
1047 SCM result = SCM_EOL;
1048 short int fam = ((struct sockaddr *) address)->sa_family;
1049
1050 switch (fam)
1051 {
1052 case AF_INET:
1053 {
1054 const struct sockaddr_in *nad = (struct sockaddr_in *) address;
1055
1056 result = scm_c_make_vector (3, SCM_UNSPECIFIED);
1057
1058 SCM_SIMPLE_VECTOR_SET(result, 0,
1059 scm_from_short (fam));
1060 SCM_SIMPLE_VECTOR_SET(result, 1,
1061 scm_from_ulong (ntohl (nad->sin_addr.s_addr)));
1062 SCM_SIMPLE_VECTOR_SET(result, 2,
1063 scm_from_ushort (ntohs (nad->sin_port)));
1064 }
1065 break;
1066 #ifdef HAVE_IPV6
1067 case AF_INET6:
1068 {
1069 const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;
1070
1071 result = scm_c_make_vector (5, SCM_UNSPECIFIED);
1072 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
1073 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_ipv6 (nad->sin6_addr.s6_addr));
1074 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ushort (ntohs (nad->sin6_port)));
1075 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_uint32 (nad->sin6_flowinfo));
1076 #ifdef HAVE_SIN6_SCOPE_ID
1077 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_ulong (nad->sin6_scope_id));
1078 #else
1079 SCM_SIMPLE_VECTOR_SET(result, 4, SCM_INUM0);
1080 #endif
1081 }
1082 break;
1083 #endif
1084 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
1085 case AF_UNIX:
1086 {
1087 const struct sockaddr_un *nad = (struct sockaddr_un *) address;
1088
1089 result = scm_c_make_vector (2, SCM_UNSPECIFIED);
1090
1091 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam));
1092 /* When addr_size is not enough to cover sun_path, do not try
1093 to access it. */
1094 if (addr_size <= offsetof (struct sockaddr_un, sun_path))
1095 SCM_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F);
1096 else
1097 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (nad->sun_path));
1098 }
1099 break;
1100 #endif
1101 default:
1102 result = SCM_UNSPECIFIED;
1103 scm_misc_error (proc, "unrecognised address family: ~A",
1104 scm_list_1 (scm_from_int (fam)));
1105
1106 }
1107 return result;
1108 }
1109
1110 /* The publicly-visible function. Return a Scheme object representing
1111 ADDRESS, an address of ADDR_SIZE bytes. */
1112 SCM
1113 scm_from_sockaddr (const struct sockaddr *address, unsigned addr_size)
1114 {
1115 return (_scm_from_sockaddr ((scm_t_max_sockaddr *) address,
1116 addr_size, "scm_from_sockaddr"));
1117 }
1118
1119 /* Convert ADDRESS, an address object returned by either
1120 `scm_from_sockaddr ()' or `scm_make_socket_address ()', into its C
1121 representation. On success, a non-NULL pointer is returned and
1122 ADDRESS_SIZE is updated to the actual size (in bytes) of the returned
1123 address. The result must eventually be freed using `free ()'. */
1124 struct sockaddr *
1125 scm_to_sockaddr (SCM address, size_t *address_size)
1126 #define FUNC_NAME "scm_to_sockaddr"
1127 {
1128 short int family;
1129 struct sockaddr *c_address = NULL;
1130
1131 SCM_VALIDATE_VECTOR (1, address);
1132
1133 *address_size = 0;
1134 family = scm_to_short (SCM_SIMPLE_VECTOR_REF (address, 0));
1135
1136 switch (family)
1137 {
1138 case AF_INET:
1139 {
1140 if (SCM_SIMPLE_VECTOR_LENGTH (address) != 3)
1141 scm_misc_error (FUNC_NAME,
1142 "invalid inet address representation: ~A",
1143 scm_list_1 (address));
1144 else
1145 {
1146 struct sockaddr_in c_inet;
1147
1148 c_inet.sin_addr.s_addr =
1149 htonl (scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 1)));
1150 c_inet.sin_port =
1151 htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
1152 c_inet.sin_family = AF_INET;
1153
1154 *address_size = sizeof (c_inet);
1155 c_address = scm_malloc (sizeof (c_inet));
1156 memcpy (c_address, &c_inet, sizeof (c_inet));
1157 }
1158
1159 break;
1160 }
1161
1162 #ifdef HAVE_IPV6
1163 case AF_INET6:
1164 {
1165 if (SCM_SIMPLE_VECTOR_LENGTH (address) != 5)
1166 scm_misc_error (FUNC_NAME, "invalid inet6 address representation: ~A",
1167 scm_list_1 (address));
1168 else
1169 {
1170 struct sockaddr_in6 c_inet6;
1171
1172 scm_to_ipv6 (c_inet6.sin6_addr.s6_addr, address);
1173 c_inet6.sin6_port =
1174 htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2)));
1175 c_inet6.sin6_flowinfo =
1176 scm_to_uint32 (SCM_SIMPLE_VECTOR_REF (address, 3));
1177 #ifdef HAVE_SIN6_SCOPE_ID
1178 c_inet6.sin6_scope_id =
1179 scm_to_ulong (SCM_SIMPLE_VECTOR_REF (address, 4));
1180 #endif
1181
1182 c_inet6.sin6_family = AF_INET6;
1183
1184 *address_size = sizeof (c_inet6);
1185 c_address = scm_malloc (sizeof (c_inet6));
1186 memcpy (c_address, &c_inet6, sizeof (c_inet6));
1187 }
1188
1189 break;
1190 }
1191 #endif
1192
1193 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
1194 case AF_UNIX:
1195 {
1196 if (SCM_SIMPLE_VECTOR_LENGTH (address) != 2)
1197 scm_misc_error (FUNC_NAME, "invalid unix address representation: ~A",
1198 scm_list_1 (address));
1199 else
1200 {
1201 SCM path;
1202 size_t path_len = 0;
1203
1204 path = SCM_SIMPLE_VECTOR_REF (address, 1);
1205 if ((!scm_is_string (path)) && (path != SCM_BOOL_F))
1206 scm_misc_error (FUNC_NAME, "invalid unix address "
1207 "path: ~A", scm_list_1 (path));
1208 else
1209 {
1210 struct sockaddr_un c_unix;
1211
1212 if (path == SCM_BOOL_F)
1213 path_len = 0;
1214 else
1215 path_len = scm_c_string_length (path);
1216
1217 #ifdef UNIX_PATH_MAX
1218 if (path_len >= UNIX_PATH_MAX)
1219 #else
1220 /* We can hope that this limit will eventually vanish, at least on GNU.
1221 However, currently, while glibc doesn't define `UNIX_PATH_MAX', it
1222 documents it has being limited to 108 bytes. */
1223 if (path_len >= sizeof (c_unix.sun_path))
1224 #endif
1225 scm_misc_error (FUNC_NAME, "unix address path "
1226 "too long: ~A", scm_list_1 (path));
1227 else
1228 {
1229 if (path_len)
1230 {
1231 scm_to_locale_stringbuf (path, c_unix.sun_path,
1232 #ifdef UNIX_PATH_MAX
1233 UNIX_PATH_MAX);
1234 #else
1235 sizeof (c_unix.sun_path));
1236 #endif
1237 c_unix.sun_path[path_len] = '\0';
1238
1239 /* Sanity check. */
1240 if (strlen (c_unix.sun_path) != path_len)
1241 scm_misc_error (FUNC_NAME, "unix address path "
1242 "contains nul characters: ~A",
1243 scm_list_1 (path));
1244 }
1245 else
1246 c_unix.sun_path[0] = '\0';
1247
1248 c_unix.sun_family = AF_UNIX;
1249
1250 *address_size = SUN_LEN (&c_unix);
1251 c_address = scm_malloc (sizeof (c_unix));
1252 memcpy (c_address, &c_unix, sizeof (c_unix));
1253 }
1254 }
1255 }
1256
1257 break;
1258 }
1259 #endif
1260
1261 default:
1262 scm_misc_error (FUNC_NAME, "unrecognised address family: ~A",
1263 scm_list_1 (scm_from_ushort (family)));
1264 }
1265
1266 return c_address;
1267 }
1268 #undef FUNC_NAME
1269
1270
1271 /* Return a newly-allocated `sockaddr' structure that reflects ADDRESS, being
1272 an address of family FAMILY, with the family-specific parameters ARGS (see
1273 the description of `connect' for details). The returned structure may be
1274 freed using `free ()'. */
1275 struct sockaddr *
1276 scm_c_make_socket_address (SCM family, SCM address, SCM args,
1277 size_t *address_size)
1278 {
1279 struct sockaddr *soka;
1280
1281 soka = scm_fill_sockaddr (scm_to_ushort (family), address, &args, 1,
1282 "scm_c_make_socket_address", address_size);
1283
1284 return soka;
1285 }
1286
1287 SCM_DEFINE (scm_make_socket_address, "make-socket-address", 2, 0, 1,
1288 (SCM family, SCM address, SCM args),
1289 "Return a Scheme address object that reflects @var{address}, "
1290 "being an address of family @var{family}, with the "
1291 "family-specific parameters @var{args} (see the description of "
1292 "@code{connect} for details).")
1293 #define FUNC_NAME s_scm_make_socket_address
1294 {
1295 SCM result = SCM_BOOL_F;
1296 struct sockaddr *c_address;
1297 size_t c_address_size;
1298
1299 c_address = scm_c_make_socket_address (family, address, args,
1300 &c_address_size);
1301 if (c_address != NULL)
1302 {
1303 result = scm_from_sockaddr (c_address, c_address_size);
1304 free (c_address);
1305 }
1306
1307 return result;
1308 }
1309 #undef FUNC_NAME
1310
1311 \f
1312 SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
1313 (SCM sock),
1314 "Accept a connection on a bound, listening socket.\n"
1315 "If there\n"
1316 "are no pending connections in the queue, wait until\n"
1317 "one is available unless the non-blocking option has been\n"
1318 "set on the socket.\n\n"
1319 "The return value is a\n"
1320 "pair in which the @emph{car} is a new socket port for the\n"
1321 "connection and\n"
1322 "the @emph{cdr} is an object with address information about the\n"
1323 "client which initiated the connection.\n\n"
1324 "@var{sock} does not become part of the\n"
1325 "connection and will continue to accept new requests.")
1326 #define FUNC_NAME s_scm_accept
1327 {
1328 int fd, selected;
1329 int newfd;
1330 SCM address;
1331 SCM newsock;
1332 SELECT_TYPE readfds, exceptfds;
1333 socklen_t addr_size = MAX_ADDR_SIZE;
1334 scm_t_max_sockaddr addr;
1335
1336 sock = SCM_COERCE_OUTPORT (sock);
1337 SCM_VALIDATE_OPFPORT (1, sock);
1338 fd = SCM_FPORT_FDES (sock);
1339
1340 FD_ZERO (&readfds);
1341 FD_ZERO (&exceptfds);
1342 FD_SET (fd, &readfds);
1343 FD_SET (fd, &exceptfds);
1344
1345 /* Block until something happens on FD, leaving guile mode while
1346 waiting. */
1347 selected = scm_std_select (fd + 1, &readfds, NULL, &exceptfds,
1348 NULL);
1349 if (selected < 0)
1350 SCM_SYSERROR;
1351
1352 newfd = accept (fd, (struct sockaddr *) &addr, &addr_size);
1353 if (newfd == -1)
1354 SCM_SYSERROR;
1355 newsock = SCM_SOCK_FD_TO_PORT (newfd);
1356 address = _scm_from_sockaddr (&addr, addr_size,
1357 FUNC_NAME);
1358
1359 return scm_cons (newsock, address);
1360 }
1361 #undef FUNC_NAME
1362
1363 SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
1364 (SCM sock),
1365 "Return the address of @var{sock}, in the same form as the\n"
1366 "object returned by @code{accept}. On many systems the address\n"
1367 "of a socket in the @code{AF_FILE} namespace cannot be read.")
1368 #define FUNC_NAME s_scm_getsockname
1369 {
1370 int fd;
1371 socklen_t addr_size = MAX_ADDR_SIZE;
1372 scm_t_max_sockaddr addr;
1373
1374 sock = SCM_COERCE_OUTPORT (sock);
1375 SCM_VALIDATE_OPFPORT (1, sock);
1376 fd = SCM_FPORT_FDES (sock);
1377 if (getsockname (fd, (struct sockaddr *) &addr, &addr_size) == -1)
1378 SCM_SYSERROR;
1379
1380 return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
1381 }
1382 #undef FUNC_NAME
1383
1384 SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
1385 (SCM sock),
1386 "Return the address that @var{sock}\n"
1387 "is connected to, in the same form as the object returned by\n"
1388 "@code{accept}. On many systems the address of a socket in the\n"
1389 "@code{AF_FILE} namespace cannot be read.")
1390 #define FUNC_NAME s_scm_getpeername
1391 {
1392 int fd;
1393 socklen_t addr_size = MAX_ADDR_SIZE;
1394 scm_t_max_sockaddr addr;
1395
1396 sock = SCM_COERCE_OUTPORT (sock);
1397 SCM_VALIDATE_OPFPORT (1, sock);
1398 fd = SCM_FPORT_FDES (sock);
1399 if (getpeername (fd, (struct sockaddr *) &addr, &addr_size) == -1)
1400 SCM_SYSERROR;
1401
1402 return _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
1403 }
1404 #undef FUNC_NAME
1405
1406 SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
1407 (SCM sock, SCM buf, SCM flags),
1408 "Receive data from a socket port.\n"
1409 "@var{sock} must already\n"
1410 "be bound to the address from which data is to be received.\n"
1411 "@var{buf} is a string into which\n"
1412 "the data will be written. The size of @var{buf} limits\n"
1413 "the amount of\n"
1414 "data which can be received: in the case of packet\n"
1415 "protocols, if a packet larger than this limit is encountered\n"
1416 "then some data\n"
1417 "will be irrevocably lost.\n\n"
1418 "The data is assumed to be binary, and there is no decoding of\n"
1419 "of locale-encoded strings.\n\n"
1420 "The optional @var{flags} argument is a value or\n"
1421 "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
1422 "The value returned is the number of bytes read from the\n"
1423 "socket.\n\n"
1424 "Note that the data is read directly from the socket file\n"
1425 "descriptor:\n"
1426 "any unread buffered port data is ignored.")
1427 #define FUNC_NAME s_scm_recv
1428 {
1429 int rv;
1430 int fd;
1431 int flg;
1432 char *dest;
1433 size_t len;
1434 SCM msg;
1435
1436 SCM_VALIDATE_OPFPORT (1, sock);
1437 SCM_VALIDATE_STRING (2, buf);
1438 if (SCM_UNBNDP (flags))
1439 flg = 0;
1440 else
1441 flg = scm_to_int (flags);
1442 fd = SCM_FPORT_FDES (sock);
1443
1444 len = scm_i_string_length (buf);
1445 msg = scm_i_make_string (len, &dest);
1446 SCM_SYSCALL (rv = recv (fd, dest, len, flg));
1447 scm_string_copy_x (buf, scm_from_int (0),
1448 msg, scm_from_int (0), scm_from_size_t (len));
1449
1450 if (rv == -1)
1451 SCM_SYSERROR;
1452
1453 scm_remember_upto_here_2 (buf, msg);
1454 return scm_from_int (rv);
1455 }
1456 #undef FUNC_NAME
1457
1458 SCM_DEFINE (scm_send, "send", 2, 1, 0,
1459 (SCM sock, SCM message, SCM flags),
1460 "Transmit the string @var{message} on a socket port @var{sock}.\n"
1461 "@var{sock} must already be bound to a destination address. The\n"
1462 "value returned is the number of bytes transmitted --\n"
1463 "it's possible for\n"
1464 "this to be less than the length of @var{message}\n"
1465 "if the socket is\n"
1466 "set to be non-blocking. The optional @var{flags} argument\n"
1467 "is a value or\n"
1468 "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
1469 "Note that the data is written directly to the socket\n"
1470 "file descriptor:\n"
1471 "any unflushed buffered port data is ignored.\n\n"
1472 "This operation is defined only for strings containing codepoints\n"
1473 "zero to 255.")
1474 #define FUNC_NAME s_scm_send
1475 {
1476 int rv;
1477 int fd;
1478 int flg;
1479 char *src;
1480 size_t len;
1481
1482 sock = SCM_COERCE_OUTPORT (sock);
1483 SCM_VALIDATE_OPFPORT (1, sock);
1484 SCM_VALIDATE_STRING (2, message);
1485
1486 /* If the string is wide, see if it can be coerced into
1487 a narrow string. */
1488 if (!scm_i_is_narrow_string (message)
1489 || scm_i_try_narrow_string (message))
1490 SCM_MISC_ERROR ("the message string is not 8-bit: ~s",
1491 scm_list_1 (message));
1492
1493 if (SCM_UNBNDP (flags))
1494 flg = 0;
1495 else
1496 flg = scm_to_int (flags);
1497 fd = SCM_FPORT_FDES (sock);
1498
1499 len = scm_i_string_length (message);
1500 message = scm_i_string_start_writing (message);
1501 src = scm_i_string_writable_chars (message);
1502 SCM_SYSCALL (rv = send (fd, src, len, flg));
1503 scm_i_string_stop_writing ();
1504
1505 if (rv == -1)
1506 SCM_SYSERROR;
1507
1508 scm_remember_upto_here_1 (message);
1509 return scm_from_int (rv);
1510 }
1511 #undef FUNC_NAME
1512
1513 SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
1514 (SCM sock, SCM str, SCM flags, SCM start, SCM end),
1515 "Receive data from socket port @var{sock} (which must be already\n"
1516 "bound), returning the originating address as well as the data.\n"
1517 "This is usually for use on datagram sockets, but can be used on\n"
1518 "stream-oriented sockets too.\n"
1519 "\n"
1520 "The data received is stored in the given @var{str}, using\n"
1521 "either the whole string or just the region between the optional\n"
1522 "@var{start} and @var{end} positions. The size of @var{str}\n"
1523 "limits the amount of data which can be received. For datagram\n"
1524 "protocols, if a packet larger than this is received then excess\n"
1525 "bytes are irrevocably lost.\n"
1526 "\n"
1527 "The return value is a pair. The @code{car} is the number of\n"
1528 "bytes read. The @code{cdr} is a socket address object which is\n"
1529 "where the data come from, or @code{#f} if the origin is\n"
1530 "unknown.\n"
1531 "\n"
1532 "The optional @var{flags} argument is a or bitwise OR\n"
1533 "(@code{logior}) of @code{MSG_OOB}, @code{MSG_PEEK},\n"
1534 "@code{MSG_DONTROUTE} etc.\n"
1535 "\n"
1536 "Data is read directly from the socket file descriptor, any\n"
1537 "buffered port data is ignored.\n"
1538 "\n"
1539 "On a GNU/Linux system @code{recvfrom!} is not multi-threading,\n"
1540 "all threads stop while a @code{recvfrom!} call is in progress.\n"
1541 "An application may need to use @code{select}, @code{O_NONBLOCK}\n"
1542 "or @code{MSG_DONTWAIT} to avoid this.")
1543 #define FUNC_NAME s_scm_recvfrom
1544 {
1545 int rv;
1546 int fd;
1547 int flg;
1548 char *buf;
1549 size_t offset;
1550 size_t cend;
1551 SCM address;
1552 socklen_t addr_size = MAX_ADDR_SIZE;
1553 scm_t_max_sockaddr addr;
1554
1555 SCM_VALIDATE_OPFPORT (1, sock);
1556 fd = SCM_FPORT_FDES (sock);
1557
1558 SCM_VALIDATE_STRING (2, str);
1559 scm_i_get_substring_spec (scm_i_string_length (str),
1560 start, &offset, end, &cend);
1561
1562 if (SCM_UNBNDP (flags))
1563 flg = 0;
1564 else
1565 SCM_VALIDATE_ULONG_COPY (3, flags, flg);
1566
1567 /* recvfrom will not necessarily return an address. usually nothing
1568 is returned for stream sockets. */
1569 str = scm_i_string_start_writing (str);
1570 buf = scm_i_string_writable_chars (str);
1571 ((struct sockaddr *) &addr)->sa_family = AF_UNSPEC;
1572 SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
1573 cend - offset, flg,
1574 (struct sockaddr *) &addr, &addr_size));
1575 scm_i_string_stop_writing ();
1576
1577 if (rv == -1)
1578 SCM_SYSERROR;
1579 if (((struct sockaddr *) &addr)->sa_family != AF_UNSPEC)
1580 address = _scm_from_sockaddr (&addr, addr_size, FUNC_NAME);
1581 else
1582 address = SCM_BOOL_F;
1583
1584 scm_remember_upto_here_1 (str);
1585
1586 return scm_cons (scm_from_int (rv), address);
1587 }
1588 #undef FUNC_NAME
1589
1590 SCM_DEFINE (scm_sendto, "sendto", 3, 1, 1,
1591 (SCM sock, SCM message, SCM fam_or_sockaddr, SCM address, SCM args_and_flags),
1592 "Transmit the string @var{message} on the socket port\n"
1593 "@var{sock}. The\n"
1594 "destination address is specified using the @var{fam},\n"
1595 "@var{address} and\n"
1596 "@var{args_and_flags} arguments, or just a socket address object "
1597 "returned by @code{make-socket-address}, in a similar way to the\n"
1598 "@code{connect} procedure. @var{args_and_flags} contains\n"
1599 "the usual connection arguments optionally followed by\n"
1600 "a flags argument, which is a value or\n"
1601 "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
1602 "The value returned is the number of bytes transmitted --\n"
1603 "it's possible for\n"
1604 "this to be less than the length of @var{message} if the\n"
1605 "socket is\n"
1606 "set to be non-blocking.\n"
1607 "Note that the data is written directly to the socket\n"
1608 "file descriptor:\n"
1609 "any unflushed buffered port data is ignored.\n"
1610 "This operation is defined only for strings containing codepoints\n"
1611 "zero to 255.")
1612 #define FUNC_NAME s_scm_sendto
1613 {
1614 int rv;
1615 int fd;
1616 int flg;
1617 struct sockaddr *soka;
1618 size_t size;
1619
1620 sock = SCM_COERCE_OUTPORT (sock);
1621 SCM_VALIDATE_FPORT (1, sock);
1622 SCM_VALIDATE_STRING (2, message);
1623 fd = SCM_FPORT_FDES (sock);
1624
1625 if (!scm_is_number (fam_or_sockaddr))
1626 {
1627 /* FAM_OR_SOCKADDR must actually be a `socket address' object. This
1628 means that the following arguments, i.e. ADDRESS and those listed in
1629 ARGS_AND_FLAGS, are the `MSG_' flags. */
1630 soka = scm_to_sockaddr (fam_or_sockaddr, &size);
1631 if (address != SCM_UNDEFINED)
1632 args_and_flags = scm_cons (address, args_and_flags);
1633 }
1634 else
1635 soka = scm_fill_sockaddr (scm_to_int (fam_or_sockaddr), address,
1636 &args_and_flags, 3, FUNC_NAME, &size);
1637
1638 if (scm_is_null (args_and_flags))
1639 flg = 0;
1640 else
1641 {
1642 SCM_VALIDATE_CONS (5, args_and_flags);
1643 flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
1644 }
1645 SCM_SYSCALL (rv = sendto (fd,
1646 scm_i_string_chars (message),
1647 scm_i_string_length (message),
1648 flg, soka, size));
1649 if (rv == -1)
1650 {
1651 int save_errno = errno;
1652 free (soka);
1653 errno = save_errno;
1654 SCM_SYSERROR;
1655 }
1656 free (soka);
1657
1658 scm_remember_upto_here_1 (message);
1659 return scm_from_int (rv);
1660 }
1661 #undef FUNC_NAME
1662 \f
1663
1664
1665 void
1666 scm_init_socket ()
1667 {
1668 /* protocol families. */
1669 #ifdef AF_UNSPEC
1670 scm_c_define ("AF_UNSPEC", scm_from_int (AF_UNSPEC));
1671 #endif
1672 #ifdef AF_UNIX
1673 scm_c_define ("AF_UNIX", scm_from_int (AF_UNIX));
1674 #endif
1675 #ifdef AF_INET
1676 scm_c_define ("AF_INET", scm_from_int (AF_INET));
1677 #endif
1678 #ifdef AF_INET6
1679 scm_c_define ("AF_INET6", scm_from_int (AF_INET6));
1680 #endif
1681
1682 #ifdef PF_UNSPEC
1683 scm_c_define ("PF_UNSPEC", scm_from_int (PF_UNSPEC));
1684 #endif
1685 #ifdef PF_UNIX
1686 scm_c_define ("PF_UNIX", scm_from_int (PF_UNIX));
1687 #endif
1688 #ifdef PF_INET
1689 scm_c_define ("PF_INET", scm_from_int (PF_INET));
1690 #endif
1691 #ifdef PF_INET6
1692 scm_c_define ("PF_INET6", scm_from_int (PF_INET6));
1693 #endif
1694
1695 /* standard addresses. */
1696 #ifdef INADDR_ANY
1697 scm_c_define ("INADDR_ANY", scm_from_ulong (INADDR_ANY));
1698 #endif
1699 #ifdef INADDR_BROADCAST
1700 scm_c_define ("INADDR_BROADCAST", scm_from_ulong (INADDR_BROADCAST));
1701 #endif
1702 #ifdef INADDR_NONE
1703 scm_c_define ("INADDR_NONE", scm_from_ulong (INADDR_NONE));
1704 #endif
1705 #ifdef INADDR_LOOPBACK
1706 scm_c_define ("INADDR_LOOPBACK", scm_from_ulong (INADDR_LOOPBACK));
1707 #endif
1708
1709 /* socket types.
1710
1711 SOCK_PACKET is deliberately omitted, the GNU/Linux socket(2) and
1712 packet(7) advise that it's obsolete and strongly deprecated. */
1713
1714 #ifdef SOCK_STREAM
1715 scm_c_define ("SOCK_STREAM", scm_from_int (SOCK_STREAM));
1716 #endif
1717 #ifdef SOCK_DGRAM
1718 scm_c_define ("SOCK_DGRAM", scm_from_int (SOCK_DGRAM));
1719 #endif
1720 #ifdef SOCK_SEQPACKET
1721 scm_c_define ("SOCK_SEQPACKET", scm_from_int (SOCK_SEQPACKET));
1722 #endif
1723 #ifdef SOCK_RAW
1724 scm_c_define ("SOCK_RAW", scm_from_int (SOCK_RAW));
1725 #endif
1726 #ifdef SOCK_RDM
1727 scm_c_define ("SOCK_RDM", scm_from_int (SOCK_RDM));
1728 #endif
1729
1730 /* setsockopt level.
1731
1732 SOL_IP, SOL_TCP and SOL_UDP are defined on gnu/linux, but not on for
1733 instance NetBSD. We define IPPROTOs because that's what the posix spec
1734 shows in its example at
1735
1736 http://www.opengroup.org/onlinepubs/007904975/functions/getsockopt.html
1737 */
1738 #ifdef SOL_SOCKET
1739 scm_c_define ("SOL_SOCKET", scm_from_int (SOL_SOCKET));
1740 #endif
1741 #ifdef IPPROTO_IP
1742 scm_c_define ("IPPROTO_IP", scm_from_int (IPPROTO_IP));
1743 #endif
1744 #ifdef IPPROTO_TCP
1745 scm_c_define ("IPPROTO_TCP", scm_from_int (IPPROTO_TCP));
1746 #endif
1747 #ifdef IPPROTO_UDP
1748 scm_c_define ("IPPROTO_UDP", scm_from_int (IPPROTO_UDP));
1749 #endif
1750
1751 /* setsockopt names. */
1752 #ifdef SO_DEBUG
1753 scm_c_define ("SO_DEBUG", scm_from_int (SO_DEBUG));
1754 #endif
1755 #ifdef SO_REUSEADDR
1756 scm_c_define ("SO_REUSEADDR", scm_from_int (SO_REUSEADDR));
1757 #endif
1758 #ifdef SO_STYLE
1759 scm_c_define ("SO_STYLE", scm_from_int (SO_STYLE));
1760 #endif
1761 #ifdef SO_TYPE
1762 scm_c_define ("SO_TYPE", scm_from_int (SO_TYPE));
1763 #endif
1764 #ifdef SO_ERROR
1765 scm_c_define ("SO_ERROR", scm_from_int (SO_ERROR));
1766 #endif
1767 #ifdef SO_DONTROUTE
1768 scm_c_define ("SO_DONTROUTE", scm_from_int (SO_DONTROUTE));
1769 #endif
1770 #ifdef SO_BROADCAST
1771 scm_c_define ("SO_BROADCAST", scm_from_int (SO_BROADCAST));
1772 #endif
1773 #ifdef SO_SNDBUF
1774 scm_c_define ("SO_SNDBUF", scm_from_int (SO_SNDBUF));
1775 #endif
1776 #ifdef SO_RCVBUF
1777 scm_c_define ("SO_RCVBUF", scm_from_int (SO_RCVBUF));
1778 #endif
1779 #ifdef SO_KEEPALIVE
1780 scm_c_define ("SO_KEEPALIVE", scm_from_int (SO_KEEPALIVE));
1781 #endif
1782 #ifdef SO_OOBINLINE
1783 scm_c_define ("SO_OOBINLINE", scm_from_int (SO_OOBINLINE));
1784 #endif
1785 #ifdef SO_NO_CHECK
1786 scm_c_define ("SO_NO_CHECK", scm_from_int (SO_NO_CHECK));
1787 #endif
1788 #ifdef SO_PRIORITY
1789 scm_c_define ("SO_PRIORITY", scm_from_int (SO_PRIORITY));
1790 #endif
1791 #ifdef SO_LINGER
1792 scm_c_define ("SO_LINGER", scm_from_int (SO_LINGER));
1793 #endif
1794
1795 /* recv/send options. */
1796 #ifdef MSG_DONTWAIT
1797 scm_c_define ("MSG_DONTWAIT", scm_from_int (MSG_DONTWAIT));
1798 #endif
1799 #ifdef MSG_OOB
1800 scm_c_define ("MSG_OOB", scm_from_int (MSG_OOB));
1801 #endif
1802 #ifdef MSG_PEEK
1803 scm_c_define ("MSG_PEEK", scm_from_int (MSG_PEEK));
1804 #endif
1805 #ifdef MSG_DONTROUTE
1806 scm_c_define ("MSG_DONTROUTE", scm_from_int (MSG_DONTROUTE));
1807 #endif
1808
1809 #ifdef __MINGW32__
1810 scm_i_init_socket_Win32 ();
1811 #endif
1812
1813 #ifdef IP_ADD_MEMBERSHIP
1814 scm_c_define ("IP_ADD_MEMBERSHIP", scm_from_int (IP_ADD_MEMBERSHIP));
1815 scm_c_define ("IP_DROP_MEMBERSHIP", scm_from_int (IP_DROP_MEMBERSHIP));
1816 #endif
1817
1818 scm_add_feature ("socket");
1819
1820 #include "libguile/socket.x"
1821 }
1822
1823
1824 /*
1825 Local Variables:
1826 c-file-style: "gnu"
1827 End:
1828 */