1 /* Copyright (C) 1996,1997,1998 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
54 #include "scm_validate.h"
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
68 #include <netinet/in.h>
70 #include <arpa/inet.h>
74 SCM_DEFINE (scm_htons
, "htons", 1, 0, 0,
76 "Returns a new integer from @var{value} by converting from host to
77 network order. @var{value} must be within the range of a C unsigned
79 #define FUNC_NAME s_scm_htons
83 SCM_VALIDATE_INUM_COPY (1,in
,c_in
);
84 if (c_in
!= SCM_INUM (in
))
85 SCM_OUT_OF_RANGE (1,in
);
87 return SCM_MAKINUM (htons (c_in
));
91 SCM_DEFINE (scm_ntohs
, "ntohs", 1, 0, 0,
93 "Returns a new integer from @var{value} by converting from network to
94 host order. @var{value} must be within the range of a C unsigned short
96 #define FUNC_NAME s_scm_ntohs
100 SCM_VALIDATE_INUM_COPY (1,in
,c_in
);
101 if (c_in
!= SCM_INUM (in
))
102 SCM_OUT_OF_RANGE (1,in
);
104 return SCM_MAKINUM (ntohs (c_in
));
108 SCM_DEFINE (scm_htonl
, "htonl", 1, 0, 0,
110 "Returns a new integer from @var{value} by converting from host to
111 network order. @var{value} must be within the range of a C unsigned
113 #define FUNC_NAME s_scm_htonl
115 unsigned long c_in
= SCM_NUM2ULONG (1,in
);
116 return scm_ulong2num (htonl (c_in
));
120 SCM_DEFINE (scm_ntohl
, "ntohl", 1, 0, 0,
122 "Returns a new integer from @var{value} by converting from network to
123 host order. @var{value} must be within the range of a C unsigned
125 #define FUNC_NAME s_scm_ntohl
127 unsigned long c_in
= SCM_NUM2ULONG (1,in
);
128 return scm_ulong2num (ntohl (c_in
));
132 SCM_SYMBOL (sym_socket
, "socket");
135 scm_sock_fd_to_port (int fd
, const char *proc
)
140 result
= scm_fdes_to_port (fd
, "r+0", sym_socket
);
145 #define SCM_SOCK_FD_TO_PORT(fd) (scm_sock_fd_to_port((fd),FUNC_NAME))
147 SCM_DEFINE (scm_socket
, "socket", 3, 0, 0,
148 (SCM family
, SCM style
, SCM proto
),
149 "Returns a new socket port of the type specified by @var{family}, @var{style}
150 and @var{protocol}. All three parameters are integers. Typical values
151 for @var{family} are the values of @code{AF_UNIX}
152 and @code{AF_INET}. Typical values for @var{style} are
153 the values of @code{SOCK_STREAM}, @code{SOCK_DGRAM} and @code{SOCK_RAW}.
155 @var{protocol} can be obtained from a protocol name using
156 @code{getprotobyname}. A value of
157 zero specifies the default protocol, which is usually right.
159 A single socket port cannot by used for communication until
160 it has been connected to another socket.")
161 #define FUNC_NAME s_scm_socket
166 SCM_VALIDATE_INUM (1,family
);
167 SCM_VALIDATE_INUM (2,style
);
168 SCM_VALIDATE_INUM (3,proto
);
169 fd
= socket (SCM_INUM (family
), SCM_INUM (style
), SCM_INUM (proto
));
170 result
= SCM_SOCK_FD_TO_PORT (fd
);
177 #ifdef HAVE_SOCKETPAIR
178 SCM_DEFINE (scm_socketpair
, "socketpair", 3, 0, 0,
179 (SCM family
, SCM style
, SCM proto
),
180 "Returns a pair of connected (but unnamed) socket ports of the type specified
181 by @var{family}, @var{style} and @var{protocol}.
182 Many systems support only
183 socket pairs of the @code{AF_UNIX} family. Zero is likely to be
184 the only meaningful value for @var{protocol}.")
185 #define FUNC_NAME s_scm_socketpair
192 SCM_VALIDATE_INUM (1,family
);
193 SCM_VALIDATE_INUM (2,style
);
194 SCM_VALIDATE_INUM (3,proto
);
196 fam
= SCM_INUM (family
);
198 if (socketpair (fam
, SCM_INUM (style
), SCM_INUM (proto
), fd
) == -1)
201 a
= SCM_SOCK_FD_TO_PORT(fd
[0]);
202 b
= SCM_SOCK_FD_TO_PORT(fd
[1]);
203 return scm_cons (a
, b
);
208 SCM_DEFINE (scm_getsockopt
, "getsockopt", 3, 0, 0,
209 (SCM sock
, SCM level
, SCM optname
),
210 "Returns the value of a particular socket option for the socket
211 port @var{socket}. @var{level} is an integer code for type of option
212 being requested, e.g., @code{SOL_SOCKET} for socket-level options.
214 integer code for the option required and should be specified using one of
215 the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.
217 The returned value is typically an integer but @code{SO_LINGER} returns a
219 #define FUNC_NAME s_scm_getsockopt
223 #ifdef HAVE_STRUCT_LINGER
224 char optval
[sizeof (struct linger
)];
226 char optval
[sizeof (scm_sizet
)];
231 #ifdef HAVE_STRUCT_LINGER
232 optlen
= (int) sizeof (struct linger
);
234 optlen
= (int) sizeof (scm_sizet
);
237 sock
= SCM_COERCE_OUTPORT (sock
);
238 SCM_VALIDATE_OPFPORT (1,sock
);
239 SCM_VALIDATE_INUM_COPY (2,level
,ilevel
);
240 SCM_VALIDATE_INUM_COPY (3,optname
,ioptname
);
242 fd
= SCM_FPORT_FDES (sock
);
243 if (getsockopt (fd
, ilevel
, ioptname
, (void *) optval
, &optlen
) == -1)
247 if (ilevel
== SOL_SOCKET
&& ioptname
== SO_LINGER
)
249 #ifdef HAVE_STRUCT_LINGER
250 struct linger
*ling
= (struct linger
*) optval
;
251 return scm_cons (SCM_MAKINUM (ling
->l_onoff
),
252 SCM_MAKINUM (ling
->l_linger
));
254 scm_sizet
*ling
= (scm_sizet
*) optval
;
255 return scm_cons (SCM_MAKINUM (*ling
),
261 if (ilevel
== SOL_SOCKET
&& ioptname
== SO_SNDBUF
)
263 scm_sizet
*bufsize
= (scm_sizet
*) optval
;
264 return SCM_MAKINUM (*bufsize
);
268 if (ilevel
== SOL_SOCKET
&& ioptname
== SO_RCVBUF
)
270 scm_sizet
*bufsize
= (scm_sizet
*) optval
;
271 return SCM_MAKINUM (*bufsize
);
274 return SCM_MAKINUM (*(int *) optval
);
278 SCM_DEFINE (scm_setsockopt
, "setsockopt", 4, 0, 0,
279 (SCM sock
, SCM level
, SCM optname
, SCM value
),
280 "Sets the value of a particular socket option for the socket
281 port @var{socket}. @var{level} is an integer code for type of option
282 being set, e.g., @code{SOL_SOCKET} for socket-level options.
284 integer code for the option to set and should be specified using one of
285 the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.
286 @var{value} is the value to which the option should be set. For
287 most options this must be an integer, but for @code{SO_LINGER} it must
290 The return value is unspecified.")
291 #define FUNC_NAME s_scm_setsockopt
295 #ifdef HAVE_STRUCT_LINGER
296 char optval
[sizeof (struct linger
)]; /* Biggest option :-( */
298 char optval
[sizeof (scm_sizet
)];
300 int ilevel
, ioptname
;
301 sock
= SCM_COERCE_OUTPORT (sock
);
302 SCM_VALIDATE_OPFPORT (1,sock
);
303 SCM_VALIDATE_INUM_COPY (2,level
,ilevel
);
304 SCM_VALIDATE_INUM_COPY (3,optname
,ioptname
);
305 fd
= SCM_FPORT_FDES (sock
);
308 else if (ilevel
== SOL_SOCKET
&& ioptname
== SO_LINGER
)
310 #ifdef HAVE_STRUCT_LINGER
312 SCM_ASSERT (SCM_CONSP (value
)
313 && SCM_INUMP (SCM_CAR (value
))
314 && SCM_INUMP (SCM_CDR (value
)),
315 value
, SCM_ARG4
, FUNC_NAME
);
316 ling
.l_onoff
= SCM_INUM (SCM_CAR (value
));
317 ling
.l_linger
= SCM_INUM (SCM_CDR (value
));
318 optlen
= (int) sizeof (struct linger
);
319 memcpy (optval
, (void *) &ling
, optlen
);
322 SCM_ASSERT (SCM_CONSP (value
)
323 && SCM_INUMP (SCM_CAR (value
))
324 && SCM_INUMP (SCM_CDR (value
)),
325 value
, SCM_ARG4
, FUNC_NAME
);
326 ling
= SCM_INUM (SCM_CAR (value
));
327 optlen
= (int) sizeof (scm_sizet
);
328 (*(scm_sizet
*) optval
) = (scm_sizet
) SCM_INUM (value
);
333 else if (ilevel
== SOL_SOCKET
&& ioptname
== SO_SNDBUF
)
335 SCM_VALIDATE_INUM (4,value
);
336 optlen
= (int) sizeof (scm_sizet
);
337 (*(scm_sizet
*) optval
) = (scm_sizet
) SCM_INUM (value
);
341 else if (ilevel
== SOL_SOCKET
&& ioptname
== SO_RCVBUF
)
343 SCM_VALIDATE_INUM (4,value
);
344 optlen
= (int) sizeof (scm_sizet
);
345 (*(scm_sizet
*) optval
) = (scm_sizet
) SCM_INUM (value
);
350 /* Most options just take an int. */
351 SCM_VALIDATE_INUM (4,value
);
352 optlen
= (int) sizeof (int);
353 (*(int *) optval
) = (int) SCM_INUM (value
);
355 if (setsockopt (fd
, ilevel
, ioptname
, (void *) optval
, optlen
) == -1)
357 return SCM_UNSPECIFIED
;
361 SCM_DEFINE (scm_shutdown
, "shutdown", 2, 0, 0,
363 "Sockets can be closed simply by using @code{close-port}. The
364 @code{shutdown} procedure allows reception or tranmission on a
365 connection to be shut down individually, according to the parameter
370 Stop receiving data for this socket. If further data arrives, reject it.
372 Stop trying to transmit data from this socket. Discard any
373 data waiting to be sent. Stop looking for acknowledgement of
374 data already sent; don't retransmit it if it is lost.
376 Stop both reception and transmission.
379 The return value is unspecified.")
380 #define FUNC_NAME s_scm_shutdown
383 sock
= SCM_COERCE_OUTPORT (sock
);
384 SCM_VALIDATE_OPFPORT (1,sock
);
385 SCM_VALIDATE_INUM (2,how
);
386 SCM_ASSERT_RANGE(2,how
,0 <= SCM_INUM (how
) && 2 >= SCM_INUM (how
));
387 fd
= SCM_FPORT_FDES (sock
);
388 if (shutdown (fd
, SCM_INUM (how
)) == -1)
390 return SCM_UNSPECIFIED
;
394 /* convert fam/address/args into a sockaddr of the appropriate type.
395 args is modified by removing the arguments actually used.
396 which_arg and proc are used when reporting errors:
397 which_arg is the position of address in the original argument list.
398 proc is the name of the original procedure.
399 size returns the size of the structure allocated. */
402 static struct sockaddr
*
403 scm_fill_sockaddr (int fam
,SCM address
,SCM
*args
,int which_arg
,const char *proc
,scm_sizet
*size
)
410 struct sockaddr_in
*soka
;
412 soka
= (struct sockaddr_in
*)
413 scm_must_malloc (sizeof (struct sockaddr_in
), proc
);
414 /* e.g., for BSDs which don't like invalid sin_len. */
415 memset (soka
, 0, sizeof (struct sockaddr_in
));
416 soka
->sin_family
= AF_INET
;
417 soka
->sin_addr
.s_addr
=
418 htonl (scm_num2ulong (address
, (char *) which_arg
, proc
));
419 SCM_ASSERT (SCM_CONSP (*args
), *args
,
420 which_arg
+ 1, proc
);
421 isport
= SCM_CAR (*args
);
422 *args
= SCM_CDR (*args
);
423 SCM_ASSERT (SCM_INUMP (isport
), isport
, which_arg
+ 1, proc
);
424 soka
->sin_port
= htons (SCM_INUM (isport
));
425 *size
= sizeof (struct sockaddr_in
);
426 return (struct sockaddr
*) soka
;
428 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
431 struct sockaddr_un
*soka
;
433 soka
= (struct sockaddr_un
*)
434 scm_must_malloc (sizeof (struct sockaddr_un
), proc
);
435 memset (soka
, 0, sizeof (struct sockaddr_un
));
436 soka
->sun_family
= AF_UNIX
;
437 SCM_ASSERT (SCM_ROSTRINGP (address
), address
,
439 memcpy (soka
->sun_path
, SCM_ROCHARS (address
),
440 1 + SCM_ROLENGTH (address
));
441 *size
= sizeof (struct sockaddr_un
);
442 return (struct sockaddr
*) soka
;
446 scm_out_of_range (proc
, SCM_MAKINUM (fam
));
450 SCM_DEFINE (scm_connect
, "connect", 3, 0, 1,
451 (SCM sock
, SCM fam
, SCM address
, SCM args
),
452 "Initiates a connection from @var{socket} to the address
453 specified by @var{address} and possibly @var{arg @dots{}}. The format
454 required for @var{address}
455 and @var{arg} @dots{} depends on the family of the socket.
457 For a socket of family @code{AF_UNIX},
458 only @code{address} is specified and must be a string with the
459 filename where the socket is to be created.
461 For a socket of family @code{AF_INET},
462 @code{address} must be an integer Internet host address and @var{arg} @dots{}
463 must be a single integer port number.
465 The return value is unspecified.")
466 #define FUNC_NAME s_scm_connect
469 struct sockaddr
*soka
;
472 sock
= SCM_COERCE_OUTPORT (sock
);
473 SCM_VALIDATE_OPFPORT (1,sock
);
474 SCM_VALIDATE_INUM (2,fam
);
475 fd
= SCM_FPORT_FDES (sock
);
476 soka
= scm_fill_sockaddr (SCM_INUM (fam
), address
, &args
, 3, FUNC_NAME
, &size
);
477 if (connect (fd
, soka
, size
) == -1)
479 scm_must_free ((char *) soka
);
480 return SCM_UNSPECIFIED
;
484 SCM_DEFINE (scm_bind
, "bind", 3, 0, 1,
485 (SCM sock
, SCM fam
, SCM address
, SCM args
),
486 "Assigns an address to the socket port @var{socket}.
487 Generally this only needs to be done for server sockets,
488 so they know where to look for incoming connections. A socket
489 without an address will be assigned one automatically when it
490 starts communicating.
492 The format of @var{address} and @var{ARG} @dots{} depends on the family
495 For a socket of family @code{AF_UNIX}, only @var{address}
496 is specified and must
497 be a string with the filename where the socket is to be created.
499 For a socket of family @code{AF_INET}, @var{address} must be an integer
500 Internet host address and @var{arg} @dots{} must be a single integer
503 The values of the following variables can also be used for @var{address}:
506 Allow connections from any address.
509 @defvar INADDR_LOOPBACK
510 The address of the local host using the loopback device.
513 @defvar INADDR_BROADCAST
514 The broadcast address on the local network.
521 The return value is unspecified.")
522 #define FUNC_NAME s_scm_bind
525 struct sockaddr
*soka
;
529 sock
= SCM_COERCE_OUTPORT (sock
);
530 SCM_VALIDATE_OPFPORT (1,sock
);
531 SCM_VALIDATE_INUM (2,fam
);
532 soka
= scm_fill_sockaddr (SCM_INUM (fam
), address
, &args
, 3, FUNC_NAME
, &size
);
533 fd
= SCM_FPORT_FDES (sock
);
534 rv
= bind (fd
, soka
, size
);
537 scm_must_free ((char *) soka
);
538 return SCM_UNSPECIFIED
;
542 SCM_DEFINE (scm_listen
, "listen", 2, 0, 0,
543 (SCM sock
, SCM backlog
),
544 "This procedure enables @var{socket} to accept connection
545 requests. @var{backlog} is an integer specifying
546 the maximum length of the queue for pending connections.
547 If the queue fills, new clients will fail to connect until the
548 server calls @code{accept} to accept a connection from the queue.
550 The return value is unspecified.")
551 #define FUNC_NAME s_scm_listen
554 sock
= SCM_COERCE_OUTPORT (sock
);
555 SCM_VALIDATE_OPFPORT (1,sock
);
556 SCM_VALIDATE_INUM (2,backlog
);
557 fd
= SCM_FPORT_FDES (sock
);
558 if (listen (fd
, SCM_INUM (backlog
)) == -1)
560 return SCM_UNSPECIFIED
;
564 /* Put the components of a sockaddr into a new SCM vector. */
567 scm_addr_vector (struct sockaddr
*address
,const char *proc
)
569 short int fam
= address
->sa_family
;
572 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
575 struct sockaddr_un
*nad
= (struct sockaddr_un
*) address
;
576 result
= scm_make_vector (SCM_MAKINUM (2), SCM_UNSPECIFIED
);
577 ve
= SCM_VELTS (result
);
578 ve
[0] = scm_ulong2num ((unsigned long) fam
);
579 ve
[1] = scm_makfromstr (nad
->sun_path
,
580 (scm_sizet
) strlen (nad
->sun_path
), 0);
586 struct sockaddr_in
*nad
= (struct sockaddr_in
*) address
;
587 result
= scm_make_vector (SCM_MAKINUM (3), SCM_UNSPECIFIED
);
588 ve
= SCM_VELTS (result
);
589 ve
[0] = scm_ulong2num ((unsigned long) fam
);
590 ve
[1] = scm_ulong2num (ntohl (nad
->sin_addr
.s_addr
));
591 ve
[2] = scm_ulong2num ((unsigned long) ntohs (nad
->sin_port
));
594 scm_misc_error (proc
, "Unrecognised address family: %s",
595 scm_listify (SCM_MAKINUM (fam
), SCM_UNDEFINED
));
600 /* Allocate a buffer large enough to hold any sockaddr type. */
601 static char *scm_addr_buffer
;
602 static int scm_addr_buffer_size
;
605 scm_init_addr_buffer (void)
607 scm_addr_buffer_size
=
608 #ifdef HAVE_UNIX_DOMAIN_SOCKETS
609 (int) sizeof (struct sockaddr_un
)
614 if (sizeof (struct sockaddr_in
) > scm_addr_buffer_size
)
615 scm_addr_buffer_size
= (int) sizeof (struct sockaddr_in
);
616 scm_addr_buffer
= scm_must_malloc (scm_addr_buffer_size
, "address buffer");
619 SCM_DEFINE (scm_accept
, "accept", 1, 0, 0,
621 "Accepts a connection on a bound, listening socket @var{socket}. If there
622 are no pending connections in the queue, it waits until
623 one is available unless the non-blocking option has been set on the
626 The return value is a
627 pair in which the CAR is a new socket port for the connection and
628 the CDR is an object with address information about the client which
629 initiated the connection.
631 If the address is not available then the CDR will be an empty vector.
633 @var{socket} does not become part of the
634 connection and will continue to accept new requests.")
635 #define FUNC_NAME s_scm_accept
643 sock
= SCM_COERCE_OUTPORT (sock
);
644 SCM_VALIDATE_OPFPORT (1,sock
);
645 fd
= SCM_FPORT_FDES (sock
);
646 tmp_size
= scm_addr_buffer_size
;
647 newfd
= accept (fd
, (struct sockaddr
*) scm_addr_buffer
, &tmp_size
);
648 newsock
= scm_sock_fd_to_port (newfd
, FUNC_NAME
);
650 address
= scm_addr_vector ((struct sockaddr
*) scm_addr_buffer
, FUNC_NAME
);
652 address
= SCM_BOOL_F
;
654 return scm_cons (newsock
, address
);
658 SCM_DEFINE (scm_getsockname
, "getsockname", 1, 0, 0,
660 "Returns the address of @var{socket}, in the same form as the object
661 returned by @code{accept}. On many systems the address of a socket
662 in the @code{AF_FILE} namespace cannot be read.")
663 #define FUNC_NAME s_scm_getsockname
668 sock
= SCM_COERCE_OUTPORT (sock
);
669 SCM_VALIDATE_OPFPORT (1,sock
);
670 fd
= SCM_FPORT_FDES (sock
);
671 tmp_size
= scm_addr_buffer_size
;
672 if (getsockname (fd
, (struct sockaddr
*) scm_addr_buffer
, &tmp_size
) == -1)
675 result
= scm_addr_vector ((struct sockaddr
*) scm_addr_buffer
, FUNC_NAME
);
682 SCM_DEFINE (scm_getpeername
, "getpeername", 1, 0, 0,
684 "Returns the address of the socket that the socket @var{socket} is connected to,
685 in the same form as the object
686 returned by @code{accept}. On many systems the address of a socket
687 in the @code{AF_FILE} namespace cannot be read.")
688 #define FUNC_NAME s_scm_getpeername
693 sock
= SCM_COERCE_OUTPORT (sock
);
694 SCM_VALIDATE_OPFPORT (1,sock
);
695 fd
= SCM_FPORT_FDES (sock
);
696 tmp_size
= scm_addr_buffer_size
;
697 if (getpeername (fd
, (struct sockaddr
*) scm_addr_buffer
, &tmp_size
) == -1)
700 result
= scm_addr_vector ((struct sockaddr
*) scm_addr_buffer
, FUNC_NAME
);
707 SCM_DEFINE (scm_recv
, "recv!", 2, 1, 0,
708 (SCM sock
, SCM buf
, SCM flags
),
709 "Receives data from the socket port @var{socket}. @var{socket} must already
710 be bound to the address from which data is to be received.
711 @var{buf} is a string into which
712 the data will be written. The size of @var{buf} limits the amount of
713 data which can be received: in the case of packet
714 protocols, if a packet larger than this limit is encountered then some data
715 will be irrevocably lost.
717 The optional @var{flags} argument is a value or
718 bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
720 The value returned is the number of bytes read from the socket.
722 Note that the data is read directly from the socket file descriptor:
723 any unread buffered port data is ignored.")
724 #define FUNC_NAME s_scm_recv
730 SCM_VALIDATE_OPFPORT (1,sock
);
731 SCM_VALIDATE_STRING (2,buf
);
732 SCM_VALIDATE_INUM_DEF_COPY (3,flags
,0,flg
);
733 fd
= SCM_FPORT_FDES (sock
);
735 SCM_SYSCALL (rv
= recv (fd
, SCM_CHARS (buf
), SCM_LENGTH (buf
), flg
));
739 return SCM_MAKINUM (rv
);
743 SCM_DEFINE (scm_send
, "send", 2, 1, 0,
744 (SCM sock
, SCM message
, SCM flags
),
745 "Transmits the string @var{message} on the socket port @var{socket}.
746 @var{socket} must already be bound to a destination address. The
747 value returned is the number of bytes transmitted -- it's possible for
748 this to be less than the length of @var{message} if the socket is
749 set to be non-blocking. The optional @var{flags} argument is a value or
750 bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
752 Note that the data is written directly to the socket file descriptor:
753 any unflushed buffered port data is ignored.")
754 #define FUNC_NAME s_scm_send
760 sock
= SCM_COERCE_OUTPORT (sock
);
761 SCM_VALIDATE_OPFPORT (1,sock
);
762 SCM_VALIDATE_ROSTRING (2,message
);
763 SCM_VALIDATE_INUM_DEF_COPY (3,flags
,0,flg
);
764 fd
= SCM_FPORT_FDES (sock
);
766 SCM_SYSCALL (rv
= send (fd
, SCM_ROCHARS (message
), SCM_ROLENGTH (message
), flg
));
769 return SCM_MAKINUM (rv
);
773 SCM_DEFINE (scm_recvfrom
, "recvfrom!", 2, 3, 0,
774 (SCM sock
, SCM buf
, SCM flags
, SCM start
, SCM end
),
775 "Returns data from the socket port @var{socket} and also information about
776 where the data was received from. @var{socket} must already
777 be bound to the address from which data is to be received.
778 @code{buf}, is a string into which
779 the data will be written. The size of @var{buf} limits the amount of
780 data which can be received: in the case of packet
781 protocols, if a packet larger than this limit is encountered then some data
782 will be irrevocably lost.
784 The optional @var{flags} argument is a value or
785 bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
787 The value returned is a pair: the CAR is the number of bytes read from
788 the socket and the CDR an address object in the same form as returned by
791 The @var{start} and @var{end} arguments specify a substring of @var{buf}
792 to which the data should be written.
794 Note that the data is read directly from the socket file descriptor:
795 any unread buffered port data is ignored.")
796 #define FUNC_NAME s_scm_recvfrom
806 SCM_VALIDATE_OPFPORT (1,sock
);
807 SCM_VALIDATE_STRING (2,buf
);
808 cend
= SCM_LENGTH (buf
);
810 if (SCM_UNBNDP (flags
))
814 flg
= SCM_NUM2ULONG (3,flags
);
816 if (!SCM_UNBNDP (start
))
818 offset
= (int) SCM_NUM2LONG (4,start
);
820 if (offset
< 0 || offset
>= cend
)
821 SCM_OUT_OF_RANGE (4, start
);
823 if (!SCM_UNBNDP (end
))
825 int tend
= (int) SCM_NUM2LONG (5,end
);
827 if (tend
<= offset
|| tend
> cend
)
828 SCM_OUT_OF_RANGE (5, end
);
835 fd
= SCM_FPORT_FDES (sock
);
837 tmp_size
= scm_addr_buffer_size
;
838 SCM_SYSCALL (rv
= recvfrom (fd
, SCM_CHARS (buf
) + offset
,
840 (struct sockaddr
*) scm_addr_buffer
,
845 address
= scm_addr_vector ((struct sockaddr
*) scm_addr_buffer
, FUNC_NAME
);
847 address
= SCM_BOOL_F
;
849 return scm_cons (SCM_MAKINUM (rv
), address
);
853 SCM_DEFINE (scm_sendto
, "sendto", 4, 0, 1,
854 (SCM sock
, SCM message
, SCM fam
, SCM address
, SCM args_and_flags
),
855 "Transmits the string @var{message} on the socket port @var{socket}. The
856 destination address is specified using the @var{family}, @var{address} and
857 @var{arg} arguments, in a similar way to the @code{connect}
859 value returned is the number of bytes transmitted -- it's possible for
860 this to be less than the length of @var{message} if the socket is
861 set to be non-blocking. The optional @var{flags} argument is a value or
862 bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.
864 Note that the data is written directly to the socket file descriptor:
865 any unflushed buffered port data is ignored.")
866 #define FUNC_NAME s_scm_sendto
871 struct sockaddr
*soka
;
875 sock
= SCM_COERCE_OUTPORT (sock
);
876 SCM_VALIDATE_FPORT (1,sock
);
877 SCM_VALIDATE_ROSTRING (2,message
);
878 SCM_VALIDATE_INUM (3,fam
);
879 fd
= SCM_FPORT_FDES (sock
);
880 soka
= scm_fill_sockaddr (SCM_INUM (fam
), address
, &args_and_flags
, 4,
882 if (SCM_NULLP (args_and_flags
))
886 SCM_VALIDATE_CONS (5,args_and_flags
);
887 flg
= SCM_NUM2ULONG (5,SCM_CAR (args_and_flags
));
889 SCM_SYSCALL (rv
= sendto (fd
, SCM_ROCHARS (message
), SCM_ROLENGTH (message
),
892 scm_must_free ((char *) soka
);
896 return SCM_MAKINUM (rv
);
905 /* protocol families. */
907 scm_sysintern ("AF_UNSPEC", SCM_MAKINUM (AF_UNSPEC
));
910 scm_sysintern ("AF_UNIX", SCM_MAKINUM (AF_UNIX
));
913 scm_sysintern ("AF_INET", SCM_MAKINUM (AF_INET
));
917 scm_sysintern ("PF_UNSPEC", SCM_MAKINUM (PF_UNSPEC
));
920 scm_sysintern ("PF_UNIX", SCM_MAKINUM (PF_UNIX
));
923 scm_sysintern ("PF_INET", SCM_MAKINUM (PF_INET
));
928 scm_sysintern ("SOCK_STREAM", SCM_MAKINUM (SOCK_STREAM
));
931 scm_sysintern ("SOCK_DGRAM", SCM_MAKINUM (SOCK_DGRAM
));
934 scm_sysintern ("SOCK_RAW", SCM_MAKINUM (SOCK_RAW
));
937 /* setsockopt level. */
939 scm_sysintern ("SOL_SOCKET", SCM_MAKINUM (SOL_SOCKET
));
942 scm_sysintern ("SOL_IP", SCM_MAKINUM (SOL_IP
));
945 scm_sysintern ("SOL_TCP", SCM_MAKINUM (SOL_TCP
));
948 scm_sysintern ("SOL_UDP", SCM_MAKINUM (SOL_UDP
));
951 /* setsockopt names. */
953 scm_sysintern ("SO_DEBUG", SCM_MAKINUM (SO_DEBUG
));
956 scm_sysintern ("SO_REUSEADDR", SCM_MAKINUM (SO_REUSEADDR
));
959 scm_sysintern ("SO_STYLE", SCM_MAKINUM (SO_STYLE
));
962 scm_sysintern ("SO_TYPE", SCM_MAKINUM (SO_TYPE
));
965 scm_sysintern ("SO_ERROR", SCM_MAKINUM (SO_ERROR
));
968 scm_sysintern ("SO_DONTROUTE", SCM_MAKINUM (SO_DONTROUTE
));
971 scm_sysintern ("SO_BROADCAST", SCM_MAKINUM (SO_BROADCAST
));
974 scm_sysintern ("SO_SNDBUF", SCM_MAKINUM (SO_SNDBUF
));
977 scm_sysintern ("SO_RCVBUF", SCM_MAKINUM (SO_RCVBUF
));
980 scm_sysintern ("SO_KEEPALIVE", SCM_MAKINUM (SO_KEEPALIVE
));
983 scm_sysintern ("SO_OOBINLINE", SCM_MAKINUM (SO_OOBINLINE
));
986 scm_sysintern ("SO_NO_CHECK", SCM_MAKINUM (SO_NO_CHECK
));
989 scm_sysintern ("SO_PRIORITY", SCM_MAKINUM (SO_PRIORITY
));
992 scm_sysintern ("SO_LINGER", SCM_MAKINUM (SO_LINGER
));
995 /* recv/send options. */
997 scm_sysintern ("MSG_OOB", SCM_MAKINUM (MSG_OOB
));
1000 scm_sysintern ("MSG_PEEK", SCM_MAKINUM (MSG_PEEK
));
1002 #ifdef MSG_DONTROUTE
1003 scm_sysintern ("MSG_DONTROUTE", SCM_MAKINUM (MSG_DONTROUTE
));
1006 scm_add_feature ("socket");
1007 scm_init_addr_buffer ();