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