Merge from mvo-vcell-cleanup-1-branch.
[bpt/guile.git] / libguile / socket.c
1 /* Copyright (C) 1996,1997,1998,2000,2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <errno.h>
48
49 #include "libguile/_scm.h"
50 #include "libguile/unif.h"
51 #include "libguile/feature.h"
52 #include "libguile/fports.h"
53 #include "libguile/strings.h"
54 #include "libguile/vectors.h"
55
56 #include "libguile/validate.h"
57 #include "libguile/socket.h"
58
59 #ifdef HAVE_STRING_H
60 #include <string.h>
61 #endif
62 #ifdef HAVE_UNISTD_H
63 #include <unistd.h>
64 #endif
65 #include <sys/types.h>
66 #include <sys/socket.h>
67 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
68 #include <sys/un.h>
69 #endif
70 #include <netinet/in.h>
71 #include <netdb.h>
72 #include <arpa/inet.h>
73
74 #if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN)
75 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
76 + strlen ((ptr)->sun_path))
77 #endif
78
79 #if !defined (HAVE_UINT32_T)
80 #if SIZEOF_INT == 4
81 typedef unsigned int uint32_t;
82 #elif SIZEOF_LONG == 4
83 typedef unsigned long uint32_t;
84 #else
85 #error can not define uint32_t
86 #endif
87 #endif
88
89 /* we are not currently using socklen_t. it's not defined on all systems,
90 so would need to be checked by configure. in the meantime, plain
91 int is the best alternative. */
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 unsigned short c_in;
103
104 SCM_VALIDATE_INUM_COPY (1, value, c_in);
105 if (c_in != SCM_INUM (value))
106 SCM_OUT_OF_RANGE (1, value);
107
108 return SCM_MAKINUM (htons (c_in));
109 }
110 #undef FUNC_NAME
111
112 SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0,
113 (SCM value),
114 "Convert a 16 bit quantity from network to host byte ordering.\n"
115 "@var{value} is packed into 2 bytes, which are then converted\n"
116 "and returned as a new integer.")
117 #define FUNC_NAME s_scm_ntohs
118 {
119 unsigned short c_in;
120
121 SCM_VALIDATE_INUM_COPY (1, value, c_in);
122 if (c_in != SCM_INUM (value))
123 SCM_OUT_OF_RANGE (1, value);
124
125 return SCM_MAKINUM (ntohs (c_in));
126 }
127 #undef FUNC_NAME
128
129 SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0,
130 (SCM value),
131 "Convert a 32 bit quantity from host to network 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_htonl
135 {
136 uint32_t c_in = SCM_NUM2ULONG (1, value);
137
138 return scm_ulong2num (htonl (c_in));
139 }
140 #undef FUNC_NAME
141
142 SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0,
143 (SCM value),
144 "Convert a 32 bit quantity from network to host byte ordering.\n"
145 "@var{value} is packed into 4 bytes, which are then converted\n"
146 "and returned as a new integer.")
147 #define FUNC_NAME s_scm_ntohl
148 {
149 uint32_t c_in = SCM_NUM2ULONG (1, value);
150
151 return scm_ulong2num (ntohl (c_in));
152 }
153 #undef FUNC_NAME
154
155 #ifndef HAVE_INET_ATON
156 /* for our definition in inet_aton.c, not usually needed. */
157 extern int inet_aton ();
158 #endif
159
160 SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0,
161 (SCM address),
162 "Convert an IPv4 Internet address from printable string\n"
163 "(dotted decimal notation) to an integer. E.g.,\n\n"
164 "@lisp\n"
165 "(inet-aton \"127.0.0.1\") @result{} 2130706433\n"
166 "@end lisp")
167 #define FUNC_NAME s_scm_inet_aton
168 {
169 struct in_addr soka;
170
171 SCM_VALIDATE_STRING (1, address);
172 SCM_STRING_COERCE_0TERMINATION_X (address);
173 if (inet_aton (SCM_STRING_CHARS (address), &soka) == 0)
174 SCM_MISC_ERROR ("bad address", SCM_EOL);
175 return scm_ulong2num (ntohl (soka.s_addr));
176 }
177 #undef FUNC_NAME
178
179
180 SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0,
181 (SCM inetid),
182 "Convert an IPv4 Internet address to a printable\n"
183 "(dotted decimal notation) string. E.g.,\n\n"
184 "@lisp\n"
185 "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n"
186 "@end lisp")
187 #define FUNC_NAME s_scm_inet_ntoa
188 {
189 struct in_addr addr;
190 char *s;
191 SCM answer;
192 addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid));
193 s = inet_ntoa (addr);
194 answer = scm_makfromstr (s, strlen (s), 0);
195 return answer;
196 }
197 #undef FUNC_NAME
198
199 #ifdef HAVE_INET_NETOF
200 SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0,
201 (SCM address),
202 "Return the network number part of the given IPv4\n"
203 "Internet address. E.g.,\n\n"
204 "@lisp\n"
205 "(inet-netof 2130706433) @result{} 127\n"
206 "@end lisp")
207 #define FUNC_NAME s_scm_inet_netof
208 {
209 struct in_addr addr;
210 addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
211 return scm_ulong2num ((unsigned long) inet_netof (addr));
212 }
213 #undef FUNC_NAME
214 #endif
215
216 #ifdef HAVE_INET_LNAOF
217 SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0,
218 (SCM address),
219 "Return the local-address-with-network part of the given\n"
220 "IPv4 Internet address, using the obsolete class A/B/C system.\n"
221 "E.g.,\n\n"
222 "@lisp\n"
223 "(inet-lnaof 2130706433) @result{} 1\n"
224 "@end lisp")
225 #define FUNC_NAME s_scm_lnaof
226 {
227 struct in_addr addr;
228 addr.s_addr = htonl (SCM_NUM2ULONG (1, address));
229 return scm_ulong2num ((unsigned long) inet_lnaof (addr));
230 }
231 #undef FUNC_NAME
232 #endif
233
234 #ifdef HAVE_INET_MAKEADDR
235 SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
236 (SCM net, SCM lna),
237 "Make an IPv4 Internet address by combining the network number\n"
238 "@var{net} with the local-address-within-network number\n"
239 "@var{lna}. E.g.,\n\n"
240 "@lisp\n"
241 "(inet-makeaddr 127 1) @result{} 2130706433\n"
242 "@end lisp")
243 #define FUNC_NAME s_scm_inet_makeaddr
244 {
245 struct in_addr addr;
246 unsigned long netnum;
247 unsigned long lnanum;
248
249 netnum = SCM_NUM2ULONG (1, net);
250 lnanum = SCM_NUM2ULONG (2, lna);
251 addr = inet_makeaddr (netnum, lnanum);
252 return scm_ulong2num (ntohl (addr.s_addr));
253 }
254 #undef FUNC_NAME
255 #endif
256
257 #ifdef HAVE_IPV6
258
259 /* flip a 128 bit IPv6 address between host and network order. */
260 #ifdef WORDS_BIGENDIAN
261 #define FLIP_NET_HOST_128(addr)
262 #else
263 #define FLIP_NET_HOST_128(addr)\
264 {\
265 int i;\
266 \
267 for (i = 0; i < 8; i++)\
268 {\
269 char c = (addr)[i];\
270 \
271 (addr)[i] = (addr)[15 - i];\
272 (addr)[15 - i] = c;\
273 }\
274 }
275 #endif
276
277 /* convert a 128 bit IPv6 address in network order to a host ordered
278 SCM integer. */
279 static SCM ipv6_net_to_num (const char *src)
280 {
281 int big_digits = 128 / SCM_BITSPERDIG;
282 const int bytes_per_dig = SCM_BITSPERDIG / 8;
283 char addr[16];
284 char *ptr = addr;
285 SCM result;
286
287 memcpy (addr, src, 16);
288 /* get rid of leading zeros. */
289 while (big_digits > 0)
290 {
291 long test = 0;
292
293 memcpy (&test, ptr, bytes_per_dig);
294 if (test != 0)
295 break;
296 ptr += bytes_per_dig;
297 big_digits--;
298 }
299 FLIP_NET_HOST_128 (addr);
300 if (big_digits * bytes_per_dig <= sizeof (unsigned long))
301 {
302 /* this is just so that we use INUM where possible. */
303 unsigned long l_addr;
304
305 memcpy (&l_addr, addr, sizeof (unsigned long));
306 result = scm_ulong2num (l_addr);
307 }
308 else
309 {
310 result = scm_mkbig (big_digits, 0);
311 memcpy (SCM_BDIGITS (result), addr, big_digits * bytes_per_dig);
312 }
313 return result;
314 }
315
316 /* convert a host ordered SCM integer to a 128 bit IPv6 address in
317 network order. */
318 static void ipv6_num_to_net (SCM src, char *dst)
319 {
320 if (SCM_INUMP (src))
321 {
322 uint32_t addr = htonl (SCM_INUM (src));
323
324 memset (dst, 0, 12);
325 memcpy (dst + 12, &addr, 4);
326 }
327 else
328 {
329 memset (dst, 0, 16);
330 memcpy (dst, SCM_BDIGITS (src),
331 SCM_NUMDIGS (src) * (SCM_BITSPERDIG / 8));
332 FLIP_NET_HOST_128 (dst);
333 }
334 }
335
336 /* check that an SCM variable contains an IPv6 integer address. */
337 #define VALIDATE_INET6(which_arg, address)\
338 if (SCM_INUMP (address))\
339 SCM_ASSERT_RANGE (which_arg, address, SCM_INUM (address) >= 0);\
340 else\
341 {\
342 SCM_VALIDATE_BIGINT (which_arg, address);\
343 SCM_ASSERT_RANGE (which_arg, address,\
344 !SCM_BIGSIGN (address)\
345 && (SCM_BITSPERDIG\
346 * SCM_NUMDIGS (address) <= 128));\
347 }
348
349 #ifdef HAVE_INET_PTON
350 SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0,
351 (SCM family, SCM address),
352 "Convert a string containing a printable network address to\n"
353 "an integer address. Note that unlike the C version of this\n"
354 "function,\n"
355 "the result is an integer with normal host byte ordering.\n"
356 "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
357 "@lisp\n"
358 "(inet-pton AF_INET "127.0.0.1") @result{} 2130706433\n"
359 "(inet-pton AF_INET6 "::1") @result{} 1\n"
360 "@end lisp")
361 #define FUNC_NAME s_scm_inet_pton
362 {
363 int af;
364 char *src;
365 char dst[16];
366 int rv;
367
368 SCM_VALIDATE_INUM_COPY (1, family, af);
369 SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
370 SCM_VALIDATE_STRING_COPY (2, address, src);
371 rv = inet_pton (af, src, dst);
372 if (rv == -1)
373 SCM_SYSERROR;
374 else if (rv == 0)
375 SCM_MISC_ERROR ("Bad address", SCM_EOL);
376 if (af == AF_INET)
377 return scm_ulong2num (ntohl (*(uint32_t *) dst));
378 else
379 return ipv6_net_to_num ((char *) dst);
380 }
381 #undef FUNC_NAME
382 #endif
383
384 #ifdef HAVE_INET_NTOP
385 SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0,
386 (SCM family, SCM address),
387 "Convert a network address into a printable string.\n"
388 "Note that unlike the C version of this function,\n"
389 "the input is an integer with normal host byte ordering.\n"
390 "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n"
391 "@lisp\n"
392 "(inet-ntop AF_INET 2130706433) @result{} "127.0.0.1"\n"
393 "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n"
394 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n"
395 "@end lisp")
396 #define FUNC_NAME s_scm_inet_ntop
397 {
398 int af;
399 #ifdef INET6_ADDRSTRLEN
400 char dst[INET6_ADDRSTRLEN];
401 #else
402 char dst[46];
403 #endif
404 char addr6[16];
405
406 SCM_VALIDATE_INUM_COPY (1, family, af);
407 SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6);
408 if (af == AF_INET)
409 *(uint32_t *) addr6 = htonl (SCM_NUM2ULONG (2, address));
410 else
411 {
412 VALIDATE_INET6 (2, address);
413 ipv6_num_to_net (address, addr6);
414 }
415 if (inet_ntop (af, &addr6, dst, sizeof dst) == NULL)
416 SCM_SYSERROR;
417 return scm_makfrom0str (dst);
418 }
419 #undef FUNC_NAME
420 #endif
421
422 #endif /* HAVE_IPV6 */
423
424 SCM_SYMBOL (sym_socket, "socket");
425
426 #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket)
427
428 SCM_DEFINE (scm_socket, "socket", 3, 0, 0,
429 (SCM family, SCM style, SCM proto),
430 "Return a new socket port of the type specified by @var{family},\n"
431 "@var{style} and @var{proto}. All three parameters are\n"
432 "integers. Supported values for @var{family} are\n"
433 "@code{AF_UNIX}, @code{AF_INET} and @code{AF_INET6}.\n"
434 "Typical values for @var{style} are @code{SOCK_STREAM},\n"
435 "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n"
436 "@var{proto} can be obtained from a protocol name using\n"
437 "@code{getprotobyname}. A value of zero specifies the default\n"
438 "protocol, which is usually right.\n\n"
439 "A single socket port cannot by used for communication until it\n"
440 "has been connected to another socket.")
441 #define FUNC_NAME s_scm_socket
442 {
443 int fd;
444
445 SCM_VALIDATE_INUM (1, family);
446 SCM_VALIDATE_INUM (2, style);
447 SCM_VALIDATE_INUM (3, proto);
448 fd = socket (SCM_INUM (family), SCM_INUM (style), SCM_INUM (proto));
449 if (fd == -1)
450 SCM_SYSERROR;
451 return SCM_SOCK_FD_TO_PORT (fd);
452 }
453 #undef FUNC_NAME
454
455 #ifdef HAVE_SOCKETPAIR
456 SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0,
457 (SCM family, SCM style, SCM proto),
458 "Return a pair of connected (but unnamed) socket ports of the\n"
459 "type specified by @var{family}, @var{style} and @var{proto}.\n"
460 "Many systems support only socket pairs of the @code{AF_UNIX}\n"
461 "family. Zero is likely to be the only meaningful value for\n"
462 "@var{proto}.")
463 #define FUNC_NAME s_scm_socketpair
464 {
465 int fam;
466 int fd[2];
467
468 SCM_VALIDATE_INUM (1,family);
469 SCM_VALIDATE_INUM (2,style);
470 SCM_VALIDATE_INUM (3,proto);
471
472 fam = SCM_INUM (family);
473
474 if (socketpair (fam, SCM_INUM (style), SCM_INUM (proto), fd) == -1)
475 SCM_SYSERROR;
476
477 return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1]));
478 }
479 #undef FUNC_NAME
480 #endif
481
482 SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
483 (SCM sock, SCM level, SCM optname),
484 "Return the value of a particular socket option for the socket\n"
485 "port @var{sock}. @var{level} is an integer code for type of\n"
486 "option being requested, e.g., @code{SOL_SOCKET} for\n"
487 "socket-level options. @var{optname} is an integer code for the\n"
488 "option required and should be specified using one of the\n"
489 "symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n"
490 "The returned value is typically an integer but @code{SO_LINGER}\n"
491 "returns a pair of integers.")
492 #define FUNC_NAME s_scm_getsockopt
493 {
494 int fd;
495 /* size of optval is the largest supported option. */
496 #ifdef HAVE_STRUCT_LINGER
497 char optval[sizeof (struct linger)];
498 int optlen = sizeof (struct linger);
499 #else
500 char optval[sizeof (scm_sizet)];
501 int optlen = sizeof (scm_sizet);
502 #endif
503 int ilevel;
504 int ioptname;
505
506 sock = SCM_COERCE_OUTPORT (sock);
507 SCM_VALIDATE_OPFPORT (1, sock);
508 SCM_VALIDATE_INUM_COPY (2, level, ilevel);
509 SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
510
511 fd = SCM_FPORT_FDES (sock);
512 if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1)
513 SCM_SYSERROR;
514
515 if (ilevel == SOL_SOCKET)
516 {
517 #ifdef SO_LINGER
518 if (ioptname == SO_LINGER)
519 {
520 #ifdef HAVE_STRUCT_LINGER
521 struct linger *ling = (struct linger *) optval;
522
523 return scm_cons (scm_long2num (ling->l_onoff),
524 scm_long2num (ling->l_linger));
525 #else
526 return scm_cons (scm_long2num (*(int *) optval)
527 SCM_MAKINUM (0));
528 #endif
529 }
530 else
531 #endif
532 if (0
533 #ifdef SO_SNDBUF
534 || ioptname == SO_SNDBUF
535 #endif
536 #ifdef SO_RCVBUF
537 || ioptname == SO_RCVBUF
538 #endif
539 )
540 {
541 return scm_long2num (*(scm_sizet *) optval);
542 }
543 }
544 return scm_long2num (*(int *) optval);
545 }
546 #undef FUNC_NAME
547
548 SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
549 (SCM sock, SCM level, SCM optname, SCM value),
550 "Set the value of a particular socket option for the socket\n"
551 "port @var{sock}. @var{level} is an integer code for type of option\n"
552 "being set, e.g., @code{SOL_SOCKET} for socket-level options.\n"
553 "@var{optname} is an\n"
554 "integer code for the option to set and should be specified using one of\n"
555 "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n"
556 "@var{value} is the value to which the option should be set. For\n"
557 "most options this must be an integer, but for @code{SO_LINGER} it must\n"
558 "be a pair.\n\n"
559 "The return value is unspecified.")
560 #define FUNC_NAME s_scm_setsockopt
561 {
562 int fd;
563 int optlen = -1;
564 /* size of optval is the largest supported option. */
565 #ifdef HAVE_STRUCT_LINGER
566 char optval[sizeof (struct linger)];
567 #else
568 char optval[sizeof (scm_sizet)];
569 #endif
570 int ilevel, ioptname;
571
572 sock = SCM_COERCE_OUTPORT (sock);
573
574 SCM_VALIDATE_OPFPORT (1, sock);
575 SCM_VALIDATE_INUM_COPY (2, level, ilevel);
576 SCM_VALIDATE_INUM_COPY (3, optname, ioptname);
577
578 fd = SCM_FPORT_FDES (sock);
579
580 if (ilevel == SOL_SOCKET)
581 {
582 #ifdef SO_LINGER
583 if (ioptname == SO_LINGER)
584 {
585 #ifdef HAVE_STRUCT_LINGER
586 struct linger ling;
587 long lv;
588
589 SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
590 lv = SCM_NUM2LONG (4, SCM_CAR (value));
591 ling.l_onoff = (int) lv;
592 SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_onoff == lv);
593 lv = SCM_NUM2LONG (4, SCM_CDR (value));
594 ling.l_linger = (int) lv;
595 SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_linger == lv);
596 optlen = (int) sizeof (struct linger);
597 memcpy (optval, (void *) &ling, optlen);
598 #else
599 int ling;
600 long lv;
601
602 SCM_ASSERT (SCM_CONSP (value), value, SCM_ARG4, FUNC_NAME);
603 /* timeout is ignored, but may as well validate it. */
604 lv = SCM_NUM2LONG (4, SCM_CDR (value));
605 ling = (int) lv;
606 SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv);
607 lv = SCM_NUM2LONG (4, SCM_CAR (value));
608 ling = (int) lv;
609 SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv);
610 optlen = (int) sizeof (int);
611 (*(int *) optval) = ling;
612 #endif
613 }
614 else
615 #endif
616 if (0
617 #ifdef SO_SNDBUF
618 || ioptname == SO_SNDBUF
619 #endif
620 #ifdef SO_RCVBUF
621 || ioptname == SO_RCVBUF
622 #endif
623 )
624 {
625 long lv = SCM_NUM2LONG (4, value);
626
627 optlen = (int) sizeof (scm_sizet);
628 (*(scm_sizet *) optval) = (scm_sizet) lv;
629 }
630 }
631 if (optlen == -1)
632 {
633 /* Most options take an int. */
634 long lv = SCM_NUM2LONG (4, value);
635 int val = (int) lv;
636
637 SCM_ASSERT_RANGE (SCM_ARG4, value, val == lv);
638 optlen = (int) sizeof (int);
639 (*(int *) optval) = val;
640 }
641 if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1)
642 SCM_SYSERROR;
643 return SCM_UNSPECIFIED;
644 }
645 #undef FUNC_NAME
646
647 SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0,
648 (SCM sock, SCM how),
649 "Sockets can be closed simply by using @code{close-port}. The\n"
650 "@code{shutdown} procedure allows reception or tranmission on a\n"
651 "connection to be shut down individually, according to the parameter\n"
652 "@var{how}:\n\n"
653 "@table @asis\n"
654 "@item 0\n"
655 "Stop receiving data for this socket. If further data arrives, reject it.\n"
656 "@item 1\n"
657 "Stop trying to transmit data from this socket. Discard any\n"
658 "data waiting to be sent. Stop looking for acknowledgement of\n"
659 "data already sent; don't retransmit it if it is lost.\n"
660 "@item 2\n"
661 "Stop both reception and transmission.\n"
662 "@end table\n\n"
663 "The return value is unspecified.")
664 #define FUNC_NAME s_scm_shutdown
665 {
666 int fd;
667 sock = SCM_COERCE_OUTPORT (sock);
668 SCM_VALIDATE_OPFPORT (1,sock);
669 SCM_VALIDATE_INUM (2,how);
670 SCM_ASSERT_RANGE(2,how,0 <= SCM_INUM (how) && 2 >= SCM_INUM (how));
671 fd = SCM_FPORT_FDES (sock);
672 if (shutdown (fd, SCM_INUM (how)) == -1)
673 SCM_SYSERROR;
674 return SCM_UNSPECIFIED;
675 }
676 #undef FUNC_NAME
677
678 /* convert fam/address/args into a sockaddr of the appropriate type.
679 args is modified by removing the arguments actually used.
680 which_arg and proc are used when reporting errors:
681 which_arg is the position of address in the original argument list.
682 proc is the name of the original procedure.
683 size returns the size of the structure allocated. */
684
685 static struct sockaddr *
686 scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
687 const char *proc, int *size)
688 #define FUNC_NAME proc
689 {
690 switch (fam)
691 {
692 case AF_INET:
693 {
694 struct sockaddr_in *soka;
695 unsigned long addr;
696 int port;
697
698 SCM_VALIDATE_ULONG_COPY (which_arg, address, addr);
699 SCM_VALIDATE_CONS (which_arg + 1, *args);
700 SCM_VALIDATE_INUM_COPY (which_arg + 1, SCM_CAR (*args), port);
701 *args = SCM_CDR (*args);
702 soka = (struct sockaddr_in *) malloc (sizeof (struct sockaddr_in));
703 if (!soka)
704 scm_memory_error (proc);
705 /* 4.4BSD-style interface includes sin_len member and defines SIN_LEN,
706 4.3BSD does not. */
707 #ifdef SIN_LEN
708 soka->sin_len = sizeof (struct sockaddr_in);
709 #endif
710 soka->sin_family = AF_INET;
711 soka->sin_addr.s_addr = htonl (addr);
712 soka->sin_port = htons (port);
713 *size = sizeof (struct sockaddr_in);
714 return (struct sockaddr *) soka;
715 }
716 #ifdef HAVE_IPV6
717 case AF_INET6:
718 {
719 /* see RFC2553. */
720 int port;
721 struct sockaddr_in6 *soka;
722 unsigned long flowinfo = 0;
723 unsigned long scope_id = 0;
724
725 VALIDATE_INET6 (which_arg, address);
726 SCM_VALIDATE_CONS (which_arg + 1, *args);
727 SCM_VALIDATE_INUM_COPY (which_arg + 1, SCM_CAR (*args), port);
728 *args = SCM_CDR (*args);
729 if (SCM_CONSP (*args))
730 {
731 SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo);
732 *args = SCM_CDR (*args);
733 if (SCM_CONSP (*args))
734 {
735 SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args),
736 scope_id);
737 *args = SCM_CDR (*args);
738 }
739 }
740 soka = (struct sockaddr_in6 *) malloc (sizeof (struct sockaddr_in6));
741 if (!soka)
742 scm_memory_error (proc);
743 #ifdef SIN_LEN6
744 soka->sin6_len = sizeof (struct sockaddr_in6);
745 #endif
746 soka->sin6_family = AF_INET6;
747 ipv6_num_to_net (address, soka->sin6_addr.s6_addr);
748 soka->sin6_port = htons (port);
749 soka->sin6_flowinfo = flowinfo;
750 #ifdef HAVE_SIN6_SCOPE_ID
751 soka->sin6_scope_id = scope_id;
752 #endif
753 *size = sizeof (struct sockaddr_in6);
754 return (struct sockaddr *) soka;
755 }
756 #endif
757 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
758 case AF_UNIX:
759 {
760 struct sockaddr_un *soka;
761 int addr_size;
762
763 SCM_ASSERT (SCM_STRINGP (address), address, which_arg, proc);
764 /* the static buffer size in sockaddr_un seems to be arbitrary
765 and not necessarily a hard limit. e.g., the glibc manual
766 suggests it may be possible to declare it size 0. let's
767 ignore it. if the O/S doesn't like the size it will cause
768 connect/bind etc., to fail. sun_path is always the last
769 member of the structure. */
770 addr_size = sizeof (struct sockaddr_un)
771 + max (0, SCM_STRING_LENGTH (address) + 1 - (sizeof soka->sun_path));
772 soka = (struct sockaddr_un *) malloc (addr_size);
773 if (!soka)
774 scm_memory_error (proc);
775 memset (soka, 0, addr_size); /* for sun_len: see sin_len above. */
776 soka->sun_family = AF_UNIX;
777 memcpy (soka->sun_path, SCM_STRING_CHARS (address),
778 SCM_STRING_LENGTH (address));
779 *size = SUN_LEN (soka);
780 return (struct sockaddr *) soka;
781 }
782 #endif
783 default:
784 scm_out_of_range (proc, SCM_MAKINUM (fam));
785 }
786 }
787 #undef FUNC_NAME
788
789 SCM_DEFINE (scm_connect, "connect", 3, 0, 1,
790 (SCM sock, SCM fam, SCM address, SCM args),
791 "Initiate a connection from a socket using a specified address\n"
792 "family to the address\n"
793 "specified by @var{address} and possibly @var{args}.\n"
794 "The format required for @var{address}\n"
795 "and @var{args} depends on the family of the socket.\n\n"
796 "For a socket of family @code{AF_UNIX},\n"
797 "only @var{address} is specified and must be a string with the\n"
798 "filename where the socket is to be created.\n\n"
799 "For a socket of family @code{AF_INET},\n"
800 "@var{address} must be an integer IPv4 host address and\n"
801 "@var{args} must be a single integer port number.\n\n"
802 "For a socket of family @code{AF_INET6},\n"
803 "@var{address} must be an integer IPv6 host address and\n"
804 "@var{args} may be up to three integers:\n"
805 "port [flowinfo] [scope_id],\n"
806 "where flowinfo and scope_id default to zero.\n\n"
807 "The return value is unspecified.")
808 #define FUNC_NAME s_scm_connect
809 {
810 int fd;
811 struct sockaddr *soka;
812 int size;
813
814 sock = SCM_COERCE_OUTPORT (sock);
815 SCM_VALIDATE_OPFPORT (1,sock);
816 SCM_VALIDATE_INUM (2,fam);
817 fd = SCM_FPORT_FDES (sock);
818 soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
819 &size);
820 if (connect (fd, soka, size) == -1)
821 {
822 int save_errno = errno;
823
824 free (soka);
825 errno = save_errno;
826 SCM_SYSERROR;
827 }
828 free (soka);
829 return SCM_UNSPECIFIED;
830 }
831 #undef FUNC_NAME
832
833 SCM_DEFINE (scm_bind, "bind", 3, 0, 1,
834 (SCM sock, SCM fam, SCM address, SCM args),
835 "Assign an address to the socket port @var{sock}.\n"
836 "Generally this only needs to be done for server sockets,\n"
837 "so they know where to look for incoming connections. A socket\n"
838 "without an address will be assigned one automatically when it\n"
839 "starts communicating.\n\n"
840 "The format of @var{address} and @var{args} depends\n"
841 "on the family of the socket.\n\n"
842 "For a socket of family @code{AF_UNIX}, only @var{address}\n"
843 "is specified and must be a string with the filename where\n"
844 "the socket is to be created.\n\n"
845 "For a socket of family @code{AF_INET}, @var{address}\n"
846 "must be an integer IPv4 address and @var{args}\n"
847 "must be a single integer port number.\n\n"
848 "The values of the following variables can also be used for\n"
849 "@var{address}:\n\n"
850 "@defvar INADDR_ANY\n"
851 "Allow connections from any address.\n"
852 "@end defvar\n\n"
853 "@defvar INADDR_LOOPBACK\n"
854 "The address of the local host using the loopback device.\n"
855 "@end defvar\n\n"
856 "@defvar INADDR_BROADCAST\n"
857 "The broadcast address on the local network.\n"
858 "@end defvar\n\n"
859 "@defvar INADDR_NONE\n"
860 "No address.\n"
861 "@end defvar\n\n"
862 "For a socket of family @code{AF_INET6}, @var{address}\n"
863 "must be an integer IPv6 address and @var{args}\n"
864 "may be up to three integers:\n"
865 "port [flowinfo] [scope_id],\n"
866 "where flowinfo and scope_id default to zero.\n\n"
867 "The return value is unspecified.")
868 #define FUNC_NAME s_scm_bind
869 {
870 struct sockaddr *soka;
871 int size;
872 int fd;
873
874 sock = SCM_COERCE_OUTPORT (sock);
875 SCM_VALIDATE_OPFPORT (1, sock);
876 SCM_VALIDATE_INUM (2, fam);
877 soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args, 3, FUNC_NAME,
878 &size);
879 fd = SCM_FPORT_FDES (sock);
880 if (bind (fd, soka, size) == -1)
881 {
882 int save_errno = errno;
883
884 free (soka);
885 errno = save_errno;
886 SCM_SYSERROR;
887 }
888 free (soka);
889 return SCM_UNSPECIFIED;
890 }
891 #undef FUNC_NAME
892
893 SCM_DEFINE (scm_listen, "listen", 2, 0, 0,
894 (SCM sock, SCM backlog),
895 "Enable @var{sock} to accept connection\n"
896 "requests. @var{backlog} is an integer specifying\n"
897 "the maximum length of the queue for pending connections.\n"
898 "If the queue fills, new clients will fail to connect until\n"
899 "the server calls @code{accept} to accept a connection from\n"
900 "the queue.\n\n"
901 "The return value is unspecified.")
902 #define FUNC_NAME s_scm_listen
903 {
904 int fd;
905 sock = SCM_COERCE_OUTPORT (sock);
906 SCM_VALIDATE_OPFPORT (1,sock);
907 SCM_VALIDATE_INUM (2,backlog);
908 fd = SCM_FPORT_FDES (sock);
909 if (listen (fd, SCM_INUM (backlog)) == -1)
910 SCM_SYSERROR;
911 return SCM_UNSPECIFIED;
912 }
913 #undef FUNC_NAME
914
915 /* Put the components of a sockaddr into a new SCM vector. */
916 static SCM
917 scm_addr_vector (const struct sockaddr *address, const char *proc)
918 {
919 short int fam = address->sa_family;
920 SCM result;
921 SCM *ve;
922
923 switch (fam)
924 {
925 case AF_INET:
926 {
927 const struct sockaddr_in *nad = (struct sockaddr_in *) address;
928
929 result = scm_c_make_vector (3, SCM_UNSPECIFIED);
930 ve = SCM_VELTS (result);
931 ve[0] = scm_ulong2num ((unsigned long) fam);
932 ve[1] = scm_ulong2num (ntohl (nad->sin_addr.s_addr));
933 ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin_port));
934 }
935 break;
936 #ifdef HAVE_IPV6
937 case AF_INET6:
938 {
939 const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address;
940
941 result = scm_c_make_vector (5, SCM_UNSPECIFIED);
942 ve = SCM_VELTS (result);
943 ve[0] = scm_ulong2num ((unsigned long) fam);
944 ve[1] = ipv6_net_to_num (nad->sin6_addr.s6_addr);
945 ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin6_port));
946 ve[3] = scm_ulong2num ((unsigned long) nad->sin6_flowinfo);
947 #ifdef HAVE_SIN6_SCOPE_ID
948 ve[4] = scm_ulong2num ((unsigned long) nad->sin6_scope_id);
949 #else
950 ve[4] = SCM_INUM0;
951 #endif
952 }
953 break;
954 #endif
955 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
956 case AF_UNIX:
957 {
958 const struct sockaddr_un *nad = (struct sockaddr_un *) address;
959
960 result = scm_c_make_vector (2, SCM_UNSPECIFIED);
961 ve = SCM_VELTS (result);
962 ve[0] = scm_ulong2num ((unsigned long) fam);
963 ve[1] = scm_makfromstr (nad->sun_path,
964 (scm_sizet) strlen (nad->sun_path), 0);
965 }
966 break;
967 #endif
968 default:
969 scm_misc_error (proc, "Unrecognised address family: ~A",
970 SCM_LIST1 (SCM_MAKINUM (fam)));
971 }
972 return result;
973 }
974
975 /* calculate the size of a buffer large enough to hold any supported
976 sockaddr type. if the buffer isn't large enough, certain system
977 calls will return a truncated address. */
978
979 #if defined (HAVE_UNIX_DOMAIN_SOCKETS)
980 #define MAX_SIZE_UN sizeof (struct sockaddr_un)
981 #else
982 #define MAX_SIZE_UN 0
983 #endif
984
985 #if defined (HAVE_IPV6)
986 #define MAX_SIZE_IN6 sizeof (struct sockaddr_in6)
987 #else
988 #define MAX_SIZE_IN6 0
989 #endif
990
991 #define MAX_ADDR_SIZE max (max (sizeof (struct sockaddr_in), MAX_SIZE_IN6),\
992 MAX_SIZE_UN)
993
994 SCM_DEFINE (scm_accept, "accept", 1, 0, 0,
995 (SCM sock),
996 "Accept a connection on a bound, listening socket.\n"
997 "If there\n"
998 "are no pending connections in the queue, wait until\n"
999 "one is available unless the non-blocking option has been\n"
1000 "set on the socket.\n\n"
1001 "The return value is a\n"
1002 "pair in which the @emph{car} is a new socket port for the\n"
1003 "connection and\n"
1004 "the @emph{cdr} is an object with address information about the\n"
1005 "client which initiated the connection.\n\n"
1006 "@var{sock} does not become part of the\n"
1007 "connection and will continue to accept new requests.")
1008 #define FUNC_NAME s_scm_accept
1009 {
1010 int fd;
1011 int newfd;
1012 SCM address;
1013 SCM newsock;
1014 int addr_size = MAX_ADDR_SIZE;
1015 char max_addr[MAX_ADDR_SIZE];
1016 struct sockaddr *addr = (struct sockaddr *) max_addr;
1017
1018 sock = SCM_COERCE_OUTPORT (sock);
1019 SCM_VALIDATE_OPFPORT (1, sock);
1020 fd = SCM_FPORT_FDES (sock);
1021 newfd = accept (fd, addr, &addr_size);
1022 if (newfd == -1)
1023 SCM_SYSERROR;
1024 newsock = SCM_SOCK_FD_TO_PORT (newfd);
1025 address = scm_addr_vector (addr, FUNC_NAME);
1026 return scm_cons (newsock, address);
1027 }
1028 #undef FUNC_NAME
1029
1030 SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0,
1031 (SCM sock),
1032 "Return the address of @var{sock}, in the same form as the\n"
1033 "object returned by @code{accept}. On many systems the address\n"
1034 "of a socket in the @code{AF_FILE} namespace cannot be read.")
1035 #define FUNC_NAME s_scm_getsockname
1036 {
1037 int fd;
1038 int addr_size = MAX_ADDR_SIZE;
1039 char max_addr[MAX_ADDR_SIZE];
1040 struct sockaddr *addr = (struct sockaddr *) max_addr;
1041
1042 sock = SCM_COERCE_OUTPORT (sock);
1043 SCM_VALIDATE_OPFPORT (1,sock);
1044 fd = SCM_FPORT_FDES (sock);
1045 if (getsockname (fd, addr, &addr_size) == -1)
1046 SCM_SYSERROR;
1047 return scm_addr_vector (addr, FUNC_NAME);
1048 }
1049 #undef FUNC_NAME
1050
1051 SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0,
1052 (SCM sock),
1053 "Return the address that @var{sock}\n"
1054 "is connected to, in the same form as the object returned by\n"
1055 "@code{accept}. On many systems the address of a socket in the\n"
1056 "@code{AF_FILE} namespace cannot be read.")
1057 #define FUNC_NAME s_scm_getpeername
1058 {
1059 int fd;
1060 int addr_size = MAX_ADDR_SIZE;
1061 char max_addr[MAX_ADDR_SIZE];
1062 struct sockaddr *addr = (struct sockaddr *) max_addr;
1063
1064 sock = SCM_COERCE_OUTPORT (sock);
1065 SCM_VALIDATE_OPFPORT (1,sock);
1066 fd = SCM_FPORT_FDES (sock);
1067 if (getpeername (fd, addr, &addr_size) == -1)
1068 SCM_SYSERROR;
1069 return scm_addr_vector (addr, FUNC_NAME);
1070 }
1071 #undef FUNC_NAME
1072
1073 SCM_DEFINE (scm_recv, "recv!", 2, 1, 0,
1074 (SCM sock, SCM buf, SCM flags),
1075 "Receive data from a socket port.\n"
1076 "@var{sock} must already\n"
1077 "be bound to the address from which data is to be received.\n"
1078 "@var{buf} is a string into which\n"
1079 "the data will be written. The size of @var{buf} limits\n"
1080 "the amount of\n"
1081 "data which can be received: in the case of packet\n"
1082 "protocols, if a packet larger than this limit is encountered\n"
1083 "then some data\n"
1084 "will be irrevocably lost.\n\n"
1085 "The optional @var{flags} argument is a value or\n"
1086 "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
1087 "The value returned is the number of bytes read from the\n"
1088 "socket.\n\n"
1089 "Note that the data is read directly from the socket file\n"
1090 "descriptor:\n"
1091 "any unread buffered port data is ignored.")
1092 #define FUNC_NAME s_scm_recv
1093 {
1094 int rv;
1095 int fd;
1096 int flg;
1097
1098 SCM_VALIDATE_OPFPORT (1,sock);
1099 SCM_VALIDATE_STRING (2,buf);
1100 SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
1101 fd = SCM_FPORT_FDES (sock);
1102
1103 SCM_SYSCALL (rv = recv (fd, SCM_STRING_CHARS (buf), SCM_STRING_LENGTH (buf), flg));
1104 if (rv == -1)
1105 SCM_SYSERROR;
1106
1107 return SCM_MAKINUM (rv);
1108 }
1109 #undef FUNC_NAME
1110
1111 SCM_DEFINE (scm_send, "send", 2, 1, 0,
1112 (SCM sock, SCM message, SCM flags),
1113 "Transmit the string @var{message} on a socket port @var{sock}.\n"
1114 "@var{sock} must already be bound to a destination address. The\n"
1115 "value returned is the number of bytes transmitted --\n"
1116 "it's possible for\n"
1117 "this to be less than the length of @var{message}\n"
1118 "if the socket is\n"
1119 "set to be non-blocking. The optional @var{flags} argument\n"
1120 "is a value or\n"
1121 "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
1122 "Note that the data is written directly to the socket\n"
1123 "file descriptor:\n"
1124 "any unflushed buffered port data is ignored.")
1125 #define FUNC_NAME s_scm_send
1126 {
1127 int rv;
1128 int fd;
1129 int flg;
1130
1131 sock = SCM_COERCE_OUTPORT (sock);
1132 SCM_VALIDATE_OPFPORT (1,sock);
1133 SCM_VALIDATE_STRING (2, message);
1134 SCM_VALIDATE_INUM_DEF_COPY (3,flags,0,flg);
1135 fd = SCM_FPORT_FDES (sock);
1136
1137 SCM_SYSCALL (rv = send (fd, SCM_STRING_CHARS (message), SCM_STRING_LENGTH (message), flg));
1138 if (rv == -1)
1139 SCM_SYSERROR;
1140 return SCM_MAKINUM (rv);
1141 }
1142 #undef FUNC_NAME
1143
1144 SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0,
1145 (SCM sock, SCM str, SCM flags, SCM start, SCM end),
1146 "Return data from the socket port @var{sock} and also\n"
1147 "information about where the data was received from.\n"
1148 "@var{sock} must already be bound to the address from which\n"
1149 "data is to be received. @code{str}, is a string into which the\n"
1150 "data will be written. The size of @var{str} limits the amount\n"
1151 "of data which can be received: in the case of packet protocols,\n"
1152 "if a packet larger than this limit is encountered then some\n"
1153 "data will be irrevocably lost.\n\n"
1154 "The optional @var{flags} argument is a value or bitwise OR of\n"
1155 "@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.\n\n"
1156 "The value returned is a pair: the @emph{car} is the number of\n"
1157 "bytes read from the socket and the @emph{cdr} an address object\n"
1158 "in the same form as returned by @code{accept}. The address\n"
1159 "will given as @code{#f} if not available, as is usually the\n"
1160 "case for stream sockets.\n\n"
1161 "The @var{start} and @var{end} arguments specify a substring of\n"
1162 "@var{str} to which the data should be written.\n\n"
1163 "Note that the data is read directly from the socket file\n"
1164 "descriptor: any unread buffered port data is ignored.")
1165 #define FUNC_NAME s_scm_recvfrom
1166 {
1167 int rv;
1168 int fd;
1169 int flg;
1170 char *buf;
1171 int offset;
1172 int cend;
1173 SCM address;
1174 int addr_size = MAX_ADDR_SIZE;
1175 char max_addr[MAX_ADDR_SIZE];
1176 struct sockaddr *addr = (struct sockaddr *) max_addr;
1177
1178 SCM_VALIDATE_OPFPORT (1,sock);
1179 fd = SCM_FPORT_FDES (sock);
1180 SCM_VALIDATE_SUBSTRING_SPEC_COPY (2, str, buf, 4, start, offset,
1181 5, end, cend);
1182 if (SCM_UNBNDP (flags))
1183 flg = 0;
1184 else
1185 SCM_VALIDATE_ULONG_COPY (3, flags, flg);
1186
1187 /* recvfrom will not necessarily return an address. usually nothing
1188 is returned for stream sockets. */
1189 addr->sa_family = AF_UNSPEC;
1190 SCM_SYSCALL (rv = recvfrom (fd, buf + offset,
1191 cend - offset, flg,
1192 addr, &addr_size));
1193 if (rv == -1)
1194 SCM_SYSERROR;
1195 if (addr->sa_family != AF_UNSPEC)
1196 address = scm_addr_vector (addr, FUNC_NAME);
1197 else
1198 address = SCM_BOOL_F;
1199
1200 return scm_cons (SCM_MAKINUM (rv), address);
1201 }
1202 #undef FUNC_NAME
1203
1204 SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1,
1205 (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags),
1206 "Transmit the string @var{message} on the socket port\n"
1207 "@var{sock}. The\n"
1208 "destination address is specified using the @var{fam},\n"
1209 "@var{address} and\n"
1210 "@var{args_and_flags} arguments, in a similar way to the\n"
1211 "@code{connect} procedure. @var{args_and_flags} contains\n"
1212 "the usual connection arguments optionally followed by\n"
1213 "a flags argument, which is a value or\n"
1214 "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n"
1215 "The value returned is the number of bytes transmitted --\n"
1216 "it's possible for\n"
1217 "this to be less than the length of @var{message} if the\n"
1218 "socket is\n"
1219 "set to be non-blocking.\n"
1220 "Note that the data is written directly to the socket\n"
1221 "file descriptor:\n"
1222 "any unflushed buffered port data is ignored.")
1223 #define FUNC_NAME s_scm_sendto
1224 {
1225 int rv;
1226 int fd;
1227 int flg;
1228 struct sockaddr *soka;
1229 int size;
1230
1231 sock = SCM_COERCE_OUTPORT (sock);
1232 SCM_VALIDATE_FPORT (1,sock);
1233 SCM_VALIDATE_STRING (2, message);
1234 SCM_VALIDATE_INUM (3,fam);
1235 fd = SCM_FPORT_FDES (sock);
1236 soka = scm_fill_sockaddr (SCM_INUM (fam), address, &args_and_flags, 4,
1237 FUNC_NAME, &size);
1238 if (SCM_NULLP (args_and_flags))
1239 flg = 0;
1240 else
1241 {
1242 SCM_VALIDATE_CONS (5,args_and_flags);
1243 flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags));
1244 }
1245 SCM_SYSCALL (rv = sendto (fd, SCM_STRING_CHARS (message),
1246 SCM_STRING_LENGTH (message),
1247 flg, soka, size));
1248 if (rv == -1)
1249 {
1250 int save_errno = errno;
1251 free (soka);
1252 errno = save_errno;
1253 SCM_SYSERROR;
1254 }
1255 free (soka);
1256 return SCM_MAKINUM (rv);
1257 }
1258 #undef FUNC_NAME
1259 \f
1260
1261
1262 void
1263 scm_init_socket ()
1264 {
1265 /* protocol families. */
1266 #ifdef AF_UNSPEC
1267 scm_c_define ("AF_UNSPEC", SCM_MAKINUM (AF_UNSPEC));
1268 #endif
1269 #ifdef AF_UNIX
1270 scm_c_define ("AF_UNIX", SCM_MAKINUM (AF_UNIX));
1271 #endif
1272 #ifdef AF_INET
1273 scm_c_define ("AF_INET", SCM_MAKINUM (AF_INET));
1274 #endif
1275 #ifdef AF_INET6
1276 scm_c_define ("AF_INET6", SCM_MAKINUM (AF_INET6));
1277 #endif
1278
1279 #ifdef PF_UNSPEC
1280 scm_c_define ("PF_UNSPEC", SCM_MAKINUM (PF_UNSPEC));
1281 #endif
1282 #ifdef PF_UNIX
1283 scm_c_define ("PF_UNIX", SCM_MAKINUM (PF_UNIX));
1284 #endif
1285 #ifdef PF_INET
1286 scm_c_define ("PF_INET", SCM_MAKINUM (PF_INET));
1287 #endif
1288 #ifdef PF_INET6
1289 scm_c_define ("PF_INET6", SCM_MAKINUM (PF_INET6));
1290 #endif
1291
1292 /* standard addresses. */
1293 #ifdef INADDR_ANY
1294 scm_c_define ("INADDR_ANY", scm_ulong2num (INADDR_ANY));
1295 #endif
1296 #ifdef INADDR_BROADCAST
1297 scm_c_define ("INADDR_BROADCAST", scm_ulong2num (INADDR_BROADCAST));
1298 #endif
1299 #ifdef INADDR_NONE
1300 scm_c_define ("INADDR_NONE", scm_ulong2num (INADDR_NONE));
1301 #endif
1302 #ifdef INADDR_LOOPBACK
1303 scm_c_define ("INADDR_LOOPBACK", scm_ulong2num (INADDR_LOOPBACK));
1304 #endif
1305
1306 /* socket types. */
1307 #ifdef SOCK_STREAM
1308 scm_c_define ("SOCK_STREAM", SCM_MAKINUM (SOCK_STREAM));
1309 #endif
1310 #ifdef SOCK_DGRAM
1311 scm_c_define ("SOCK_DGRAM", SCM_MAKINUM (SOCK_DGRAM));
1312 #endif
1313 #ifdef SOCK_RAW
1314 scm_c_define ("SOCK_RAW", SCM_MAKINUM (SOCK_RAW));
1315 #endif
1316
1317 /* setsockopt level. */
1318 #ifdef SOL_SOCKET
1319 scm_c_define ("SOL_SOCKET", SCM_MAKINUM (SOL_SOCKET));
1320 #endif
1321 #ifdef SOL_IP
1322 scm_c_define ("SOL_IP", SCM_MAKINUM (SOL_IP));
1323 #endif
1324 #ifdef SOL_TCP
1325 scm_c_define ("SOL_TCP", SCM_MAKINUM (SOL_TCP));
1326 #endif
1327 #ifdef SOL_UDP
1328 scm_c_define ("SOL_UDP", SCM_MAKINUM (SOL_UDP));
1329 #endif
1330
1331 /* setsockopt names. */
1332 #ifdef SO_DEBUG
1333 scm_c_define ("SO_DEBUG", SCM_MAKINUM (SO_DEBUG));
1334 #endif
1335 #ifdef SO_REUSEADDR
1336 scm_c_define ("SO_REUSEADDR", SCM_MAKINUM (SO_REUSEADDR));
1337 #endif
1338 #ifdef SO_STYLE
1339 scm_c_define ("SO_STYLE", SCM_MAKINUM (SO_STYLE));
1340 #endif
1341 #ifdef SO_TYPE
1342 scm_c_define ("SO_TYPE", SCM_MAKINUM (SO_TYPE));
1343 #endif
1344 #ifdef SO_ERROR
1345 scm_c_define ("SO_ERROR", SCM_MAKINUM (SO_ERROR));
1346 #endif
1347 #ifdef SO_DONTROUTE
1348 scm_c_define ("SO_DONTROUTE", SCM_MAKINUM (SO_DONTROUTE));
1349 #endif
1350 #ifdef SO_BROADCAST
1351 scm_c_define ("SO_BROADCAST", SCM_MAKINUM (SO_BROADCAST));
1352 #endif
1353 #ifdef SO_SNDBUF
1354 scm_c_define ("SO_SNDBUF", SCM_MAKINUM (SO_SNDBUF));
1355 #endif
1356 #ifdef SO_RCVBUF
1357 scm_c_define ("SO_RCVBUF", SCM_MAKINUM (SO_RCVBUF));
1358 #endif
1359 #ifdef SO_KEEPALIVE
1360 scm_c_define ("SO_KEEPALIVE", SCM_MAKINUM (SO_KEEPALIVE));
1361 #endif
1362 #ifdef SO_OOBINLINE
1363 scm_c_define ("SO_OOBINLINE", SCM_MAKINUM (SO_OOBINLINE));
1364 #endif
1365 #ifdef SO_NO_CHECK
1366 scm_c_define ("SO_NO_CHECK", SCM_MAKINUM (SO_NO_CHECK));
1367 #endif
1368 #ifdef SO_PRIORITY
1369 scm_c_define ("SO_PRIORITY", SCM_MAKINUM (SO_PRIORITY));
1370 #endif
1371 #ifdef SO_LINGER
1372 scm_c_define ("SO_LINGER", SCM_MAKINUM (SO_LINGER));
1373 #endif
1374
1375 /* recv/send options. */
1376 #ifdef MSG_OOB
1377 scm_c_define ("MSG_OOB", SCM_MAKINUM (MSG_OOB));
1378 #endif
1379 #ifdef MSG_PEEK
1380 scm_c_define ("MSG_PEEK", SCM_MAKINUM (MSG_PEEK));
1381 #endif
1382 #ifdef MSG_DONTROUTE
1383 scm_c_define ("MSG_DONTROUTE", SCM_MAKINUM (MSG_DONTROUTE));
1384 #endif
1385
1386 scm_add_feature ("socket");
1387
1388 #ifndef SCM_MAGIC_SNARFER
1389 #include "libguile/socket.x"
1390 #endif
1391 }
1392
1393
1394 /*
1395 Local Variables:
1396 c-file-style: "gnu"
1397 End:
1398 */