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