1995-10-13 23:34:21 -04:00
|
|
|
;;; Networking for the Scheme Shell
|
1995-10-31 12:44:11 -05:00
|
|
|
;;; Copyright (c) 1994-1995 by Brian D. Carlstrom.
|
1995-10-13 23:34:21 -04:00
|
|
|
;;; Copyright (c) 1994 by Olin Shivers.
|
1999-08-06 09:28:52 -04:00
|
|
|
;;; See file COPYING.
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
;;; Scheme48 implementation.
|
|
|
|
|
|
|
|
(foreign-source
|
|
|
|
"#include <sys/types.h>"
|
|
|
|
"#include <sys/socket.h>"
|
1999-06-21 00:35:42 -04:00
|
|
|
"#include <errno.h>"
|
1995-10-13 23:34:21 -04:00
|
|
|
""
|
1995-10-22 08:34:53 -04:00
|
|
|
"/* Make sure foreign-function stubs interface to the C funs correctly: */"
|
|
|
|
"#include \"network1.h\""
|
1995-10-13 23:34:21 -04:00
|
|
|
""
|
|
|
|
"extern int h_errno;"
|
|
|
|
""
|
|
|
|
"#define errno_on_zero_or_false(x) ((x) ? SCHFALSE : ENTER_FIXNUM(errno))"
|
|
|
|
"#define errno_or_false(x) (((x) == -1) ? ENTER_FIXNUM(errno) : SCHFALSE)"
|
|
|
|
"#define False_on_zero(x) ((x) ? ENTER_FIXNUM(x) : SCHFALSE)"
|
|
|
|
"" )
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; High Level Prototypes
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
|
|
|
|
(define (socket-connect protocol-family socket-type . args)
|
|
|
|
(let* ((sock (create-socket protocol-family
|
|
|
|
socket-type))
|
|
|
|
(addr (cond ((= protocol-family
|
|
|
|
protocol-family/internet)
|
|
|
|
(let* ((host (car args))
|
|
|
|
(port (cadr args))
|
|
|
|
(host (car (host-info:addresses
|
|
|
|
(name->host-info host))))
|
|
|
|
(port (cond ((integer? port) port)
|
|
|
|
((string? port)
|
|
|
|
(service-info:port
|
|
|
|
(service-info (cadr args) "tcp")))
|
|
|
|
(else
|
1995-10-31 15:22:27 -05:00
|
|
|
(error
|
|
|
|
"socket-connect: bad arg ~s"
|
|
|
|
args)))))
|
1995-10-13 23:34:21 -04:00
|
|
|
(internet-address->socket-address host port)))
|
|
|
|
((= protocol-family
|
|
|
|
protocol-family/unix)
|
|
|
|
(unix-address->socket-address (car args)))
|
|
|
|
(else
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-connect: unsupported protocol-family ~s"
|
|
|
|
protocol-family)))))
|
1999-09-20 13:45:15 -04:00
|
|
|
;; Close the socket and free the file-descriptors
|
|
|
|
;; if the connect fails:
|
|
|
|
(let ((connected #f))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda () (connect-socket sock addr) (set! connected #t))
|
|
|
|
(lambda ()
|
|
|
|
(if (not connected)
|
|
|
|
(close-socket sock))))
|
|
|
|
(if connected
|
|
|
|
sock
|
|
|
|
#f))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (bind-listen-accept-loop protocol-family proc arg)
|
2000-06-13 18:08:57 -04:00
|
|
|
(let ((sock (create-socket protocol-family socket-type/stream))
|
|
|
|
(addr (cond ((= protocol-family protocol-family/internet)
|
|
|
|
(internet-address->socket-address internet-address/any
|
|
|
|
(cond ((integer? arg) arg)
|
|
|
|
((string? arg)
|
|
|
|
(service-info:port (service-info arg "tcp")))
|
|
|
|
(else (error "socket-connect: bad arg ~s" arg)))))
|
|
|
|
|
|
|
|
((= protocol-family protocol-family/unix)
|
|
|
|
(unix-address->socket-address arg))
|
|
|
|
|
|
|
|
(else
|
|
|
|
(error "bind-listen-accept-loop: unsupported protocol-family ~s"
|
|
|
|
protocol-family)))))
|
|
|
|
|
1995-10-13 23:34:21 -04:00
|
|
|
(set-socket-option sock level/socket socket/reuse-address #t)
|
|
|
|
(bind-socket sock addr)
|
|
|
|
(listen-socket sock 5)
|
|
|
|
(let loop ()
|
2000-06-13 18:08:57 -04:00
|
|
|
(call-with-values (lambda () (accept-connection sock)) proc)
|
1995-10-13 23:34:21 -04:00
|
|
|
(loop))))
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; Socket Record Structure
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define-record socket
|
|
|
|
family ; protocol family
|
|
|
|
inport ; input port
|
|
|
|
outport) ; output port
|
|
|
|
|
|
|
|
(define-record socket-address
|
|
|
|
family ; address family
|
|
|
|
address) ; address
|
|
|
|
|
|
|
|
;;; returns the fdes of a socket
|
|
|
|
;;; not exported
|
|
|
|
(define (socket->fdes sock)
|
|
|
|
(fdport-data:fd (extensible-input-port-local-data (socket:inport sock))))
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; Socket Address Routines
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (internet-address->socket-address address32 port16)
|
|
|
|
(cond ((not (<= 0 address32 #xffffffff))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "internet-address->socket-address: address out of range ~s"
|
|
|
|
address32))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (<= 0 port16 #xffff))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "internet-address->socket-address: port out of range ~s"
|
|
|
|
port16))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(make-socket-address address-family/internet
|
|
|
|
(string-append (integer->string address32)
|
|
|
|
(integer->string port16))))))
|
|
|
|
|
|
|
|
(define (socket-address->internet-address sockaddr)
|
|
|
|
(if (or (not (socket-address? sockaddr))
|
|
|
|
(not (= (socket-address:family sockaddr)
|
|
|
|
address-family/internet)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-address->internet-address: internet socket expected ~s"
|
|
|
|
sockaddr)
|
1995-10-13 23:34:21 -04:00
|
|
|
(values (string->integer (substring (socket-address:address sockaddr)
|
|
|
|
0 4))
|
|
|
|
(string->integer (substring (socket-address:address sockaddr)
|
|
|
|
4 8)))))
|
|
|
|
|
|
|
|
(define (unix-address->socket-address path)
|
|
|
|
(if (> (string-length path) 108)
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "unix-address->socket-address: path too long ~s" path)
|
1995-10-13 23:34:21 -04:00
|
|
|
(make-socket-address address-family/unix path)))
|
|
|
|
|
|
|
|
(define (socket-address->unix-address sockaddr)
|
|
|
|
(if (or (not (socket-address? sockaddr))
|
|
|
|
(not (= (socket-address:family sockaddr)
|
|
|
|
address-family/unix)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-address->unix-address expects an unix socket ~s" sockaddr)
|
1996-09-26 16:08:06 -04:00
|
|
|
(socket-address:address sockaddr)))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (make-addr af)
|
|
|
|
(make-string (cond ((= af address-family/unix) 108)
|
|
|
|
((= af address-family/internet) 8)
|
1995-10-31 15:22:27 -05:00
|
|
|
(else
|
|
|
|
(error "make-addr: unknown address-family ~s" af)))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; socket syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (create-socket pf type . maybe-protocol)
|
1996-04-19 14:39:14 -04:00
|
|
|
(let ((protocol (:optional maybe-protocol 0)))
|
1995-10-13 23:34:21 -04:00
|
|
|
(if (not (and (integer? pf)
|
|
|
|
(integer? type)
|
|
|
|
(integer? protocol)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "create-socket: integer arguments expected ~s ~s ~s"
|
|
|
|
pf type protocol)
|
1995-10-13 23:34:21 -04:00
|
|
|
(let* ((fd (%socket pf type protocol))
|
1996-09-11 21:43:53 -04:00
|
|
|
(in (make-input-fdport fd 0))
|
1995-10-13 23:34:21 -04:00
|
|
|
(out (dup->outport in)))
|
|
|
|
(make-socket pf in out)))))
|
|
|
|
|
|
|
|
(define-foreign %socket/errno
|
|
|
|
(socket (integer pf)
|
|
|
|
(integer type)
|
|
|
|
(integer protocol))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer))
|
|
|
|
|
|
|
|
(define-errno-syscall (%socket pf type protocol) %socket/errno
|
|
|
|
sockfd)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; close syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (close-socket sock)
|
|
|
|
(close (socket:inport sock))
|
|
|
|
(close (socket:outport sock)))
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; bind syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (bind-socket sock name)
|
|
|
|
(cond ((not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "bind-socket: socket expected ~s" sock))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (socket-address? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "bind-socket: socket-address expected ~s" name))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(let ((family (socket:family sock)))
|
|
|
|
(if (not (= family (socket-address:family name)))
|
|
|
|
(error
|
1995-10-31 15:22:27 -05:00
|
|
|
"bind-socket: trying to bind incompatible address to socket ~s"
|
|
|
|
name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(%bind (socket->fdes sock)
|
|
|
|
family
|
|
|
|
(socket-address:address name)))))))
|
|
|
|
|
|
|
|
(define-foreign %bind/errno
|
|
|
|
(scheme_bind (integer sockfd) ; socket fdes
|
|
|
|
(integer family) ; address family
|
|
|
|
(string-desc name)) ; scheme descriptor
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall (%bind sockfd family name) %bind/errno)
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; connect syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (connect-socket sock name)
|
|
|
|
(cond ((not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "connect-socket: socket expected ~s" sock))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (socket-address? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "connect-socket: socket-address expected ~s" name))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(let ((family (socket:family sock)))
|
|
|
|
(cond ((not (= family (socket-address:family name)))
|
|
|
|
(error
|
1995-10-31 15:22:27 -05:00
|
|
|
"connect: trying to connect socket to incompatible address ~s"
|
|
|
|
name))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(%connect (socket->fdes sock)
|
|
|
|
(socket:family sock)
|
|
|
|
(socket-address:address name))))))))
|
|
|
|
|
|
|
|
(define-foreign %connect/errno
|
|
|
|
(scheme_connect (integer sockfd) ; socket fdes
|
|
|
|
(integer family) ; address family
|
|
|
|
(desc name)) ; scheme descriptor
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall (%connect sockfd family name) %connect/errno)
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; listen syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (listen-socket sock backlog)
|
|
|
|
(cond ((not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "listen-socket: socket expected ~s" sock))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (integer? backlog))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "listen-socket: integer expected ~s" backlog))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(%listen (socket->fdes sock) backlog))))
|
|
|
|
|
|
|
|
(define-foreign %listen/errno
|
|
|
|
(listen (integer sockfd) ; socket fdes
|
|
|
|
(integer backlog)) ; backlog
|
1999-07-14 21:27:15 -04:00
|
|
|
no-declare ; for Linux
|
1995-10-13 23:34:21 -04:00
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall (%listen sockfd backlog) %listen/errno)
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; accept syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (accept-connection sock)
|
|
|
|
(if (not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "accept-connection: socket expected ~s" sock)
|
1995-10-13 23:34:21 -04:00
|
|
|
(let* ((family (socket:family sock))
|
|
|
|
(name (make-addr family))
|
|
|
|
(fd (%accept (socket->fdes sock) family name))
|
1996-09-11 21:43:53 -04:00
|
|
|
(in (make-input-fdport fd 0))
|
1995-10-13 23:34:21 -04:00
|
|
|
(out (dup->outport in)))
|
|
|
|
(values (make-socket family in out)
|
|
|
|
(make-socket-address family name)))))
|
|
|
|
|
|
|
|
(define-foreign %accept/errno
|
|
|
|
(scheme_accept (integer sockfd)
|
|
|
|
(integer family)
|
|
|
|
(string-desc name))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer))
|
|
|
|
|
|
|
|
(define-errno-syscall (%accept sock family name) %accept/errno
|
|
|
|
sockfd)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; getpeername syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (socket-remote-address sock)
|
|
|
|
(if (or (not (socket? sock))
|
|
|
|
(not (= (socket:family sock) address-family/internet)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-remote-address: internet socket expected ~s" sock)
|
1995-10-13 23:34:21 -04:00
|
|
|
(let* ((family (socket:family sock))
|
|
|
|
(name (make-addr family)))
|
|
|
|
(%peer-name (socket->fdes sock)
|
|
|
|
family
|
|
|
|
name)
|
|
|
|
(make-socket-address family name))))
|
|
|
|
|
|
|
|
(define-foreign %peer-name/errno
|
|
|
|
(scheme_peer_name (integer sockfd)
|
|
|
|
(integer family)
|
|
|
|
(string-desc name))
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall (%peer-name sock family name) %peer-name/errno)
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; getsockname syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (socket-local-address sock)
|
|
|
|
(if (or (not (socket? sock))
|
|
|
|
(not (= (socket:family sock) address-family/internet)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-local-address: internet socket expected ~s" sock)
|
1995-10-13 23:34:21 -04:00
|
|
|
(let* ((family (socket:family sock))
|
|
|
|
(name (make-addr family)))
|
|
|
|
(%socket-name (socket->fdes sock)
|
|
|
|
family
|
|
|
|
name)
|
|
|
|
(make-socket-address family name))))
|
|
|
|
|
|
|
|
(define-foreign %socket-name/errno
|
|
|
|
(scheme_socket_name (integer sockfd)
|
|
|
|
(integer family)
|
|
|
|
(string-desc name))
|
|
|
|
(to-scheme integer "False_on_zero"))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall
|
1995-10-13 23:34:21 -04:00
|
|
|
(%socket-name sock family name) %socket-name/errno)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; shutdown syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (shutdown-socket sock how)
|
|
|
|
(cond ((not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "shutdown-socket: socket expected ~s" sock))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (integer? how))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "shutdown-socket: integer expected ~s" how))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(%shutdown (socket->fdes sock) how))))
|
|
|
|
|
|
|
|
(define-foreign %shutdown/errno
|
|
|
|
(shutdown (integer sockfd)
|
|
|
|
(integer how))
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall
|
1995-10-13 23:34:21 -04:00
|
|
|
(%shutdown sock how) %shutdown/errno)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; socketpair syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (create-socket-pair type)
|
|
|
|
(if (not (integer? type))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "create-socket-pair: integer argument expected ~s" type)
|
1995-10-13 23:34:21 -04:00
|
|
|
(receive (s1 s2)
|
|
|
|
(%socket-pair type)
|
1996-09-11 21:43:53 -04:00
|
|
|
(let* ((in1 (make-input-fdport s1 0))
|
1995-10-13 23:34:21 -04:00
|
|
|
(out1 (dup->outport in1))
|
1996-09-11 21:43:53 -04:00
|
|
|
(in2 (make-input-fdport s2 0))
|
1995-10-13 23:34:21 -04:00
|
|
|
(out2 (dup->outport in2)))
|
|
|
|
(values (make-socket protocol-family/unix in1 out1)
|
|
|
|
(make-socket protocol-family/unix in2 out2))))))
|
|
|
|
|
|
|
|
;; based on pipe in syscalls.scm
|
|
|
|
(define-foreign %socket-pair/errno
|
|
|
|
(scheme_socket_pair (integer type))
|
|
|
|
(to-scheme integer errno_or_false)
|
|
|
|
integer
|
|
|
|
integer)
|
|
|
|
|
|
|
|
(define-errno-syscall
|
|
|
|
(%socket-pair type) %socket-pair/errno
|
|
|
|
sockfd1
|
|
|
|
sockfd2)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; recv syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define (receive-message socket len . maybe-flags)
|
1996-04-19 14:39:14 -04:00
|
|
|
(let ((flags (:optional maybe-flags 0)))
|
1995-10-13 23:34:21 -04:00
|
|
|
(cond ((not (socket? socket))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "receive-message: socket expected ~s" socket))
|
1995-10-13 23:34:21 -04:00
|
|
|
((or (not (integer? flags))
|
|
|
|
(not (integer? len)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "receive-message: integer expected ~s ~s" flags len))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(let ((s (make-string len)))
|
|
|
|
(receive (nread from)
|
1995-10-31 12:44:11 -05:00
|
|
|
(receive-message! socket s 0 len flags)
|
1995-10-13 23:34:21 -04:00
|
|
|
(values
|
|
|
|
(cond ((not nread) #f) ; EOF
|
|
|
|
((= nread len) s)
|
|
|
|
(else (substring s 0 nread)))
|
|
|
|
from)))))))
|
|
|
|
|
1996-04-19 14:39:14 -04:00
|
|
|
(define (receive-message! socket s . args)
|
1995-10-31 12:44:11 -05:00
|
|
|
(if (not (string? s))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "receive-message!: string expected ~s" s)
|
1996-04-19 14:39:14 -04:00
|
|
|
(let-optionals args ((start 0) (end (string-length s)) (flags 0))
|
|
|
|
(cond ((not (socket? socket))
|
|
|
|
(error "receive-message!: socket expected ~s" socket))
|
|
|
|
((not (or (integer? flags)
|
|
|
|
(integer? start)
|
|
|
|
(integer? end)))
|
|
|
|
(error "receive-message!: integer expected ~s ~s ~s"
|
|
|
|
flags start end))
|
|
|
|
(else
|
|
|
|
(generic-receive-message! (socket->fdes socket) flags
|
|
|
|
s start end
|
|
|
|
recv-substring!/errno
|
|
|
|
(socket:family socket)))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (generic-receive-message! sockfd flags s start end reader from)
|
|
|
|
(if (bogus-substring-spec? s start end)
|
|
|
|
(error "Bad substring indices"
|
|
|
|
reader sockfd flags
|
|
|
|
s start end from))
|
|
|
|
(let ((addr (make-addr from)))
|
|
|
|
(let loop ((i start))
|
|
|
|
(if (>= i end) (- i start)
|
|
|
|
(receive (err nread)
|
|
|
|
(reader sockfd flags s i end addr)
|
|
|
|
(cond (err (if (= err errno/intr) (loop i)
|
|
|
|
;; Give info on partially-read data in error packet.
|
|
|
|
(errno-error err reader sockfd flags
|
|
|
|
s start i end addr)))
|
|
|
|
|
|
|
|
((zero? nread) ; EOF
|
|
|
|
(values
|
|
|
|
(let ((result (- i start)))
|
|
|
|
(and (not (zero? result)) result))
|
|
|
|
from))
|
|
|
|
(else (loop (+ i nread)))))))))
|
|
|
|
|
|
|
|
(define (receive-message/partial socket len . maybe-flags)
|
1996-04-19 14:39:14 -04:00
|
|
|
(let ((flags (:optional maybe-flags 0)))
|
1995-10-13 23:34:21 -04:00
|
|
|
(cond ((not (socket? socket))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "receive-message/partial: socket expected ~s" socket))
|
1995-10-13 23:34:21 -04:00
|
|
|
((or (not (integer? flags))
|
|
|
|
(not (integer? len)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "receive-message/partial: integer expected ~s ~s" flags len))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
1995-10-31 12:44:11 -05:00
|
|
|
(let ((s (make-string len)))
|
|
|
|
(receive (nread addr)
|
|
|
|
(receive-message!/partial socket s 0 len flags)
|
|
|
|
(values
|
|
|
|
(cond ((not nread) #f) ; EOF
|
|
|
|
((= nread len) s)
|
|
|
|
(else (substring s 0 nread)))
|
|
|
|
addr)))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
1996-04-19 14:39:14 -04:00
|
|
|
(define (receive-message!/partial socket s . args)
|
1995-10-31 12:44:11 -05:00
|
|
|
(if (not (string? s))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "receive-message!/partial: string expected ~s" s)
|
1996-04-19 14:39:14 -04:00
|
|
|
(let-optionals args ((start 0) (end (string-length s)) (flags 0))
|
|
|
|
(cond ((not (socket? socket))
|
|
|
|
(error "receive-message!/partial: socket expected ~s"
|
|
|
|
socket))
|
|
|
|
((not (integer? flags))
|
|
|
|
(error "receive-message!/partial: integer expected ~s"
|
|
|
|
flags))
|
|
|
|
(else
|
|
|
|
(generic-receive-message!/partial (socket->fdes socket)
|
|
|
|
flags
|
|
|
|
s start end
|
|
|
|
recv-substring!/errno
|
|
|
|
(socket:family socket)))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (generic-receive-message!/partial sockfd flags s start end reader from)
|
|
|
|
(if (bogus-substring-spec? s start end)
|
|
|
|
(error "Bad substring indices" reader s start end))
|
|
|
|
|
|
|
|
(if (= start end) 0 ; Vacuous request.
|
|
|
|
(let ((addr (make-addr from)))
|
|
|
|
(let loop ()
|
|
|
|
(receive (err nread)
|
|
|
|
(reader sockfd flags s start end addr)
|
1996-08-23 17:43:22 -04:00
|
|
|
|
|
|
|
(cond ((not err)
|
|
|
|
(values (and (not (zero? nread)) nread)
|
|
|
|
(make-socket-address from addr)))
|
|
|
|
|
|
|
|
((= err errno/intr) (loop))
|
|
|
|
|
1995-10-13 23:34:21 -04:00
|
|
|
; No forward-progess here.
|
1996-08-23 17:43:22 -04:00
|
|
|
((or (= err errno/wouldblock)
|
|
|
|
(= err errno/again))
|
|
|
|
0)
|
|
|
|
|
1995-10-13 23:34:21 -04:00
|
|
|
(else (errno-error err reader sockfd flags
|
1996-08-23 17:43:22 -04:00
|
|
|
s start start end addr))))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define-foreign recv-substring!/errno
|
|
|
|
(recv_substring (integer sockfd)
|
|
|
|
(integer flags)
|
|
|
|
(string-desc buf)
|
|
|
|
(integer start)
|
|
|
|
(integer end)
|
|
|
|
(string-desc name))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer))
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; send syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
|
1996-04-19 14:39:14 -04:00
|
|
|
(define (send-message socket s . args)
|
|
|
|
(let-optionals args ((start 0) (end (string-length s)) (flags 0) (addr #f))
|
1995-10-13 23:34:21 -04:00
|
|
|
(cond ((not (socket? socket))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "send-message: socket expected ~s" socket))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (integer? flags))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "send-message: integer expected ~s" flags))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (string? s))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "send-message: string expected ~s" s))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(generic-send-message (socket->fdes socket) flags
|
|
|
|
s start end
|
|
|
|
send-substring/errno
|
|
|
|
(if addr (socket-address:family addr) 0)
|
|
|
|
(and addr (socket-address:address addr)))))))
|
|
|
|
|
|
|
|
(define (generic-send-message sockfd flags s start end writer family addr)
|
|
|
|
(if (bogus-substring-spec? s start end)
|
|
|
|
(error "Bad substring indices"
|
|
|
|
sockfd flags family addr
|
|
|
|
s start end writer))
|
|
|
|
(let ((addr (if addr (make-addr family) "")))
|
|
|
|
(let loop ((i start))
|
|
|
|
(if (< i end)
|
|
|
|
(receive (err nwritten)
|
|
|
|
(writer sockfd flags s i end family addr)
|
1996-08-23 17:43:22 -04:00
|
|
|
(cond ((not err) (loop (+ i nwritten)))
|
|
|
|
((= err errno/intr) (loop i))
|
|
|
|
(else (errno-error err sockfd flags family addr
|
|
|
|
s start i end writer))))))))
|
|
|
|
|
1995-10-13 23:34:21 -04:00
|
|
|
|
1996-04-19 14:39:14 -04:00
|
|
|
(define (send-message/partial socket s . args)
|
|
|
|
(let-optionals args ((start 0) (end (string-length s)) (flags 0) (addr #f))
|
1995-10-13 23:34:21 -04:00
|
|
|
(cond ((not (socket? socket))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "send-message/partial: socket expected ~s" socket))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (integer? flags))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "send-message/partial: integer expected ~s" flags))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (string? s))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "send-message/partial: string expected ~s" s))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(generic-send-message/partial (socket->fdes socket) flags
|
|
|
|
s start end
|
|
|
|
send-substring/errno
|
|
|
|
(if addr (socket-address:family addr) 0)
|
1995-10-31 12:44:11 -05:00
|
|
|
(if addr (socket-address:address addr)))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (generic-send-message/partial sockfd flags s start end writer family addr)
|
|
|
|
(if (bogus-substring-spec? s start end)
|
|
|
|
(error "Bad substring indices"
|
|
|
|
sockfd flags family addr
|
|
|
|
s start end writer))
|
|
|
|
|
|
|
|
(if (= start end) 0 ; Vacuous request.
|
1995-10-31 12:44:11 -05:00
|
|
|
(let loop ()
|
|
|
|
(receive (err nwritten)
|
|
|
|
(writer sockfd flags s start end family addr)
|
1996-08-23 17:43:22 -04:00
|
|
|
(cond ((not err) nwritten)
|
|
|
|
((= err errno/intr) (loop))
|
|
|
|
((or (= err errno/again) (= err errno/wouldblock)) 0)
|
1995-10-31 12:44:11 -05:00
|
|
|
(else (errno-error err sockfd flags family addr
|
1996-08-23 17:43:22 -04:00
|
|
|
s start start end writer)))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define-foreign send-substring/errno
|
|
|
|
(send_substring (integer sockfd)
|
|
|
|
(integer flags)
|
|
|
|
(string-desc buf)
|
|
|
|
(integer start)
|
|
|
|
(integer end)
|
|
|
|
(integer family)
|
|
|
|
(string-desc name))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer))
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; getsockopt syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
|
|
|
|
(define (socket-option sock level option)
|
|
|
|
(cond ((not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-option: socket expected ~s" sock))
|
1995-10-13 23:34:21 -04:00
|
|
|
((or (not (integer? level))(not (integer? option)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "socket-option: integer expected ~s ~s" level option))
|
1995-10-13 23:34:21 -04:00
|
|
|
((boolean-option? option)
|
|
|
|
(let ((result (%getsockopt (socket->fdes sock) level option)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(cond ((= result -1)
|
|
|
|
(error "socket-option ~s ~s ~s" sock level option))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else (not (= result 0))))))
|
|
|
|
((value-option? option)
|
|
|
|
(let ((result (%getsockopt (socket->fdes sock) level option)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(cond ((= result -1)
|
|
|
|
(error "socket-option ~s ~s ~s" sock level option))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else result))))
|
|
|
|
((linger-option? option)
|
|
|
|
(receive (result/on-off time)
|
|
|
|
(%getsockopt-linger (socket->fdes sock) level option)
|
1995-10-31 15:22:27 -05:00
|
|
|
(cond ((= result/on-off -1)
|
|
|
|
(error "socket-option ~s ~s ~s" sock level option))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else (if (= result/on-off 0) #f time)))))
|
|
|
|
((timeout-option? option)
|
|
|
|
(receive (result/secs usecs)
|
|
|
|
(%getsockopt-linger (socket->fdes sock) level option)
|
1995-10-31 15:22:27 -05:00
|
|
|
(cond ((= result/secs -1)
|
|
|
|
(error "socket-option ~s ~s ~s" sock level option))
|
|
|
|
(else (+ result/secs (/ usecs 1000))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
1995-10-31 15:22:27 -05:00
|
|
|
"socket-option: unknown option type ~s" option)))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define-foreign %getsockopt/errno
|
|
|
|
(scheme_getsockopt (integer sockfd)
|
|
|
|
(integer level)
|
|
|
|
(integer optname))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer))
|
|
|
|
|
|
|
|
(define-errno-syscall (%getsockopt sock level option) %getsockopt/errno
|
|
|
|
value)
|
|
|
|
|
|
|
|
(define-foreign %getsockopt-linger/errno
|
|
|
|
(scheme_getsockopt_linger (integer sockfd)
|
|
|
|
(integer level)
|
|
|
|
(integer optname))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer) ; error/on-off
|
|
|
|
integer) ; linger time
|
|
|
|
|
|
|
|
(define-errno-syscall
|
|
|
|
(%getsockopt-linger sock level option) %getsockopt-linger/errno
|
|
|
|
on-off
|
|
|
|
linger)
|
|
|
|
|
|
|
|
(define-foreign %getsockopt-timeout/errno
|
|
|
|
(scheme_getsockopt_timeout (integer sockfd)
|
|
|
|
(integer level)
|
|
|
|
(integer optname))
|
|
|
|
(multi-rep (to-scheme integer errno_or_false)
|
|
|
|
integer) ; error/secs
|
|
|
|
integer) ; usecs
|
|
|
|
|
|
|
|
(define-errno-syscall
|
|
|
|
(%getsockopt-timeout sock level option) %getsockopt-timeout/errno
|
|
|
|
secs
|
|
|
|
usecs)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; setsockopt syscall
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
|
|
|
|
(define (set-socket-option sock level option value)
|
|
|
|
(cond ((not (socket? sock))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "set-socket-option: socket expected ~s" sock))
|
1995-10-13 23:34:21 -04:00
|
|
|
((or (not (integer? level)) (not (integer? option)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "set-socket-option: integer expected ~s ~s" level option))
|
1995-10-13 23:34:21 -04:00
|
|
|
((boolean-option? option)
|
|
|
|
(%setsockopt (socket->fdes sock) level option (if value 1 0)))
|
|
|
|
((value-option? option)
|
|
|
|
(%setsockopt (socket->fdes sock) level option value))
|
|
|
|
((linger-option? option)
|
|
|
|
(%setsockopt-linger (socket->fdes sock)
|
|
|
|
level option
|
|
|
|
(if value 1 0)
|
|
|
|
(if value value 0)))
|
|
|
|
((timeout-option? option)
|
|
|
|
(let ((secs (truncate value)))
|
|
|
|
(%setsockopt-timeout (socket->fdes sock) level option
|
|
|
|
secs
|
|
|
|
(truncate (* (- value secs) 1000)))))
|
|
|
|
(else
|
|
|
|
"set-socket-option: unknown option type")))
|
|
|
|
|
|
|
|
(define-foreign %setsockopt/errno
|
|
|
|
(scheme_setsockopt (integer sockfd)
|
|
|
|
(integer level)
|
|
|
|
(integer optname)
|
|
|
|
(integer optval))
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall
|
1995-10-13 23:34:21 -04:00
|
|
|
(%setsockopt sock level option value) %setsockopt/errno)
|
|
|
|
|
|
|
|
|
|
|
|
(define-foreign %setsockopt-linger/errno
|
|
|
|
(scheme_setsockopt_linger (integer sockfd)
|
|
|
|
(integer level)
|
|
|
|
(integer optname)
|
|
|
|
(integer on-off)
|
|
|
|
(integer time))
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall
|
1995-10-13 23:34:21 -04:00
|
|
|
(%setsockopt-linger sock level option on-off time) %setsockopt-linger/errno)
|
|
|
|
|
|
|
|
(define-foreign %setsockopt-timeout/errno
|
|
|
|
(scheme_setsockopt_timeout (integer sockfd)
|
|
|
|
(integer level)
|
|
|
|
(integer optname)
|
|
|
|
(integer secs)
|
|
|
|
(integer usecs))
|
|
|
|
(to-scheme integer errno_or_false))
|
|
|
|
|
1996-08-22 16:13:14 -04:00
|
|
|
(define-errno-syscall
|
1995-10-13 23:34:21 -04:00
|
|
|
(%setsockopt-timeout sock level option secs usecs) %setsockopt-timeout/errno)
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; socket-option routines
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
|
|
|
|
(define (boolean-option? opt)
|
|
|
|
(member opt options/boolean))
|
|
|
|
|
|
|
|
(define (value-option? opt)
|
|
|
|
(member opt options/value))
|
|
|
|
|
|
|
|
(define (linger-option? opt)
|
|
|
|
(member opt options/linger))
|
|
|
|
|
|
|
|
(define (timeout-option? opt)
|
|
|
|
(member opt options/timeout))
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; host lookup
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define-record host-info
|
|
|
|
name ; Host name
|
|
|
|
aliases ; Alternative names
|
|
|
|
addresses ; Host addresses
|
|
|
|
|
|
|
|
((disclose hi) ; Make host-info records print like
|
|
|
|
(list "host" (host-info:name hi)))) ; #{host clark.lcs.mit.edu}.
|
|
|
|
|
|
|
|
(define (host-info arg)
|
|
|
|
(cond ((string? arg) (name->host-info arg))
|
|
|
|
((socket-address? arg) (address->host-info arg))
|
1995-10-31 15:22:27 -05:00
|
|
|
(else (error "host-info: string or socket-address expected ~s" arg))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (address->host-info name)
|
|
|
|
(if (or (not (socket-address? name))
|
|
|
|
(not (= (socket-address:family name) address-family/internet)))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "address->host-info: internet address expected ~s" name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(receive (herrno name aliases addresses)
|
|
|
|
(%host-address->host-info/h-errno
|
|
|
|
(socket-address:address name))
|
|
|
|
(if herrno
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "address->host-info: non-zero herrno ~s ~s" name herrno)
|
1995-10-13 23:34:21 -04:00
|
|
|
(make-host-info name
|
|
|
|
(vector->list
|
|
|
|
(C-string-vec->Scheme aliases #f))
|
|
|
|
(vector->list
|
1996-12-02 17:18:25 -05:00
|
|
|
(C-long-vec->Scheme addresses #f)))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define-foreign %host-address->host-info/h-errno
|
|
|
|
(scheme_host_address2host_info (string-desc name))
|
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; host name
|
|
|
|
(C char**) ; alias list
|
|
|
|
(C char**)) ; address list
|
|
|
|
|
|
|
|
(define (name->host-info name)
|
|
|
|
(if (not (string? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "name->host-info: string expected ~s" name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(receive (herrno name aliases addresses)
|
|
|
|
(%host-name->host-info/h-errno name)
|
|
|
|
(if herrno
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "name->host-info: non-zero herrno ~s ~s" herrno name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(make-host-info name
|
|
|
|
(vector->list
|
|
|
|
(C-string-vec->Scheme aliases #f))
|
|
|
|
(vector->list
|
|
|
|
(C-long-vec->Scheme addresses #f)))))))
|
|
|
|
|
|
|
|
(define-foreign %host-name->host-info/h-errno
|
|
|
|
(scheme_host_name2host_info (string name))
|
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; host name
|
|
|
|
(C char**) ; alias list
|
|
|
|
(C char**)) ; address list
|
|
|
|
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; network lookup
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define-record network-info
|
|
|
|
name ; Network name
|
|
|
|
aliases ; Alternative names
|
|
|
|
net) ; Network number
|
|
|
|
|
|
|
|
(define (network-info arg)
|
|
|
|
(cond ((string? arg) (name->network-info arg))
|
|
|
|
((socket-address? arg) (address->network-info arg))
|
1995-10-31 15:22:27 -05:00
|
|
|
(else
|
|
|
|
(error "network-info: string or socket-address expected ~s" arg))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (address->network-info name)
|
|
|
|
(if (not (integer? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "address->network-info: integer expected ~s" name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(let ((name (integer->string name))
|
|
|
|
(net (make-string 4)))
|
|
|
|
(receive (result name aliases)
|
|
|
|
(%net-address->network-info name net)
|
|
|
|
(make-network-info name
|
|
|
|
(vector->list
|
|
|
|
(C-string-vec->Scheme aliases #f))
|
|
|
|
(string->integer net))))))
|
|
|
|
|
|
|
|
(define-foreign %net-address->network-info
|
1995-10-22 08:34:53 -04:00
|
|
|
(scheme_net_address2net_info (string-desc name) (string-desc net))
|
1995-10-13 23:34:21 -04:00
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; net name
|
|
|
|
(C char**)) ; alias list
|
|
|
|
|
|
|
|
|
|
|
|
(define (name->network-info name)
|
|
|
|
(if (not (string? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "name->network-info: string expected ~s" name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(let ((net (make-string 4)))
|
|
|
|
(receive (result name aliases)
|
|
|
|
(%net-name->network-info name net)
|
|
|
|
(make-network-info name
|
|
|
|
(vector->list
|
|
|
|
(C-string-vec->Scheme aliases #f))
|
|
|
|
(string->integer net))))))
|
|
|
|
|
|
|
|
(define-foreign %net-name->network-info
|
1995-10-22 08:34:53 -04:00
|
|
|
(scheme_net_name2net_info (string name) (string-desc net))
|
1995-10-13 23:34:21 -04:00
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; net name
|
|
|
|
(C char**)) ; alias list
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; service lookup
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define-record service-info
|
|
|
|
name ; Service name
|
|
|
|
aliases ; Alternative names
|
|
|
|
port ; Port number
|
|
|
|
protocol) ; Protocol name
|
|
|
|
|
|
|
|
(define (service-info . args)
|
1996-04-19 14:39:14 -04:00
|
|
|
(apply (cond ((string? (car args)) name->service-info)
|
|
|
|
((integer? (car args)) port->service-info)
|
|
|
|
(else (error "service-info: string or integer expected ~s" args)))
|
|
|
|
args))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (port->service-info name . maybe-proto)
|
1996-04-19 14:39:14 -04:00
|
|
|
(let ((proto (:optional maybe-proto "")))
|
1995-10-13 23:34:21 -04:00
|
|
|
(cond ((not (integer? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "port->service-info: integer expected ~s" name))
|
1995-10-13 23:34:21 -04:00
|
|
|
((not (string? proto))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "port->service-info: string expected ~s" proto))
|
1995-10-13 23:34:21 -04:00
|
|
|
(else
|
|
|
|
(receive (result name aliases port protocol)
|
|
|
|
(%service-port->service-info name proto)
|
1996-04-19 14:39:14 -04:00
|
|
|
(make-service-info name
|
|
|
|
(vector->list (C-string-vec->Scheme aliases #f))
|
|
|
|
port
|
|
|
|
protocol))))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define-foreign %service-port->service-info
|
1995-10-22 08:34:53 -04:00
|
|
|
(scheme_serv_port2serv_info (integer name) (string proto))
|
1995-10-13 23:34:21 -04:00
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; service name
|
|
|
|
(C char**) ; alias list
|
|
|
|
integer ; port number
|
|
|
|
static-string) ; protocol name
|
|
|
|
|
|
|
|
|
|
|
|
(define (name->service-info name . maybe-proto)
|
1996-04-19 14:39:14 -04:00
|
|
|
(receive (result name aliases port protocol)
|
|
|
|
(%service-name->service-info name (:optional maybe-proto ""))
|
|
|
|
(make-service-info name (vector->list (C-string-vec->Scheme aliases #f))
|
|
|
|
port protocol)))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define-foreign %service-name->service-info
|
1995-10-22 08:34:53 -04:00
|
|
|
(scheme_serv_name2serv_info (string name) (string proto))
|
1995-10-13 23:34:21 -04:00
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; service name
|
|
|
|
(C char**) ; alias list
|
|
|
|
integer ; port number
|
|
|
|
static-string) ; protocol name
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; protocol lookup
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
(define-record protocol-info
|
|
|
|
name ; Protocol name
|
|
|
|
aliases ; Alternative names
|
|
|
|
number) ; Protocol number
|
|
|
|
|
|
|
|
(define (protocol-info arg)
|
|
|
|
(cond ((string? arg) (name->protocol-info arg))
|
|
|
|
((integer? arg) (number->protocol-info arg))
|
1995-10-31 15:22:27 -05:00
|
|
|
(else (error "protocol-info: string or integer expected ~s" arg))))
|
1995-10-13 23:34:21 -04:00
|
|
|
|
|
|
|
(define (number->protocol-info name)
|
|
|
|
(if (not (integer? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "number->protocol-info: integer expected ~s" name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(receive (result name aliases protocol)
|
|
|
|
(%protocol-port->protocol-info name)
|
|
|
|
(make-protocol-info name
|
|
|
|
(vector->list
|
|
|
|
(C-string-vec->Scheme aliases #f))
|
|
|
|
protocol))))
|
|
|
|
|
|
|
|
(define-foreign %protocol-port->protocol-info
|
|
|
|
(scheme_proto_num2proto_info (integer name))
|
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; protocol name
|
|
|
|
(C char**) ; alias list
|
|
|
|
integer) ; protocol number
|
|
|
|
|
|
|
|
(define (name->protocol-info name)
|
|
|
|
(if (not (string? name))
|
1995-10-31 15:22:27 -05:00
|
|
|
(error "name->protocol-info: string expected ~s" name)
|
1995-10-13 23:34:21 -04:00
|
|
|
(receive (result name aliases protocol)
|
|
|
|
(%protocol-name->protocol-info name)
|
|
|
|
(make-protocol-info name
|
|
|
|
(vector->list
|
|
|
|
(C-string-vec->Scheme aliases #f))
|
|
|
|
protocol))))
|
|
|
|
|
|
|
|
(define-foreign %protocol-name->protocol-info
|
|
|
|
(scheme_proto_name2proto_info (string name))
|
|
|
|
(to-scheme integer "False_on_zero")
|
|
|
|
static-string ; protocol name
|
|
|
|
(C char**) ; alias list
|
|
|
|
integer) ; protocol number
|
|
|
|
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;;; Lowlevel junk
|
|
|
|
;;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
|
|
;; Used to pull address list back
|
|
|
|
;; based on C-string-vec->Scheme from cig/libcig.scm
|
|
|
|
(define (C-long-vec->Scheme cvec veclen) ; No free.
|
|
|
|
(let ((vec (make-vector (or veclen (%c-veclen-or-false cvec) 0))))
|
|
|
|
(mapv! (lambda (ignore) (make-string 4)) vec)
|
|
|
|
(%set-long-vector-carriers! vec cvec)
|
|
|
|
(mapv! string->integer vec)))
|
|
|
|
|
|
|
|
(define (integer->string num32)
|
|
|
|
(let* ((str (make-string 4))
|
|
|
|
(num24 (arithmetic-shift num32 -8))
|
|
|
|
(num16 (arithmetic-shift num24 -8))
|
|
|
|
(num08 (arithmetic-shift num16 -8))
|
|
|
|
(byte0 (bitwise-and #b11111111 num08))
|
|
|
|
(byte1 (bitwise-and #b11111111 num16))
|
|
|
|
(byte2 (bitwise-and #b11111111 num24))
|
|
|
|
(byte3 (bitwise-and #b11111111 num32)))
|
|
|
|
(string-set! str 0 (ascii->char byte0))
|
|
|
|
(string-set! str 1 (ascii->char byte1))
|
|
|
|
(string-set! str 2 (ascii->char byte2))
|
|
|
|
(string-set! str 3 (ascii->char byte3))
|
|
|
|
str))
|
|
|
|
|
|
|
|
(define (string->integer str)
|
|
|
|
(+ (arithmetic-shift(char->ascii(string-ref str 0))24)
|
|
|
|
(arithmetic-shift(char->ascii(string-ref str 1))16)
|
|
|
|
(arithmetic-shift(char->ascii(string-ref str 2)) 8)
|
|
|
|
(char->ascii(string-ref str 3))))
|
|
|
|
|
|
|
|
;; also from cig/libcig.scm
|
|
|
|
(define-foreign %c-veclen-or-false
|
1995-10-22 08:34:53 -04:00
|
|
|
(veclen ((C "const long * ~a") c-vec)); redefining can we open cig-aux?
|
1995-10-13 23:34:21 -04:00
|
|
|
desc) ; integer or #f if arg is NULL.
|
|
|
|
|
|
|
|
;; also from cig/libcig.scm
|
|
|
|
(define-foreign %set-long-vector-carriers!
|
1995-10-22 08:34:53 -04:00
|
|
|
(set_longvec_carriers (vector-desc svec)
|
|
|
|
((C "long const * const * ~a") cvec))
|
1995-10-13 23:34:21 -04:00
|
|
|
ignore)
|
|
|
|
|
|
|
|
;; also from cig/libcig.scm
|
|
|
|
(define (mapv! f v)
|
|
|
|
(let ((len (vector-length v)))
|
|
|
|
(do ((i 0 (+ i 1)))
|
|
|
|
((= i len) v)
|
|
|
|
(vector-set! v i (f (vector-ref v i))))))
|