2020-07-02 15:53:16 +00:00
|
|
|
(load-shared-object "libc.so.6")
|
|
|
|
|
2020-07-09 10:12:38 +00:00
|
|
|
(define (domain->int flag)
|
2020-07-09 07:55:45 +00:00
|
|
|
(case flag
|
|
|
|
((AF_INET) 2)
|
|
|
|
))
|
|
|
|
|
2020-07-09 10:12:38 +00:00
|
|
|
(define (type->int flag)
|
2020-07-09 07:55:45 +00:00
|
|
|
(case flag
|
|
|
|
((SOCK_DGRAM) 2)
|
|
|
|
((SOCK_DCCP) 6)
|
|
|
|
))
|
|
|
|
|
2020-07-09 10:12:38 +00:00
|
|
|
(define (protocol->int flag)
|
2020-07-09 07:55:45 +00:00
|
|
|
(case flag
|
2020-07-09 10:12:38 +00:00
|
|
|
((IPPROTO_IP) 0)
|
2020-07-09 07:55:45 +00:00
|
|
|
((IPPROTO_DCCP) 33)
|
|
|
|
))
|
|
|
|
|
|
|
|
(define (perror str)
|
|
|
|
((foreign-procedure
|
|
|
|
"perror"
|
|
|
|
(string)
|
|
|
|
void) str))
|
|
|
|
|
|
|
|
(define (socket domain type protocol)
|
|
|
|
((foreign-procedure
|
|
|
|
"socket"
|
|
|
|
(int int int)
|
|
|
|
int)
|
2020-07-09 10:12:38 +00:00
|
|
|
(domain->int domain)
|
|
|
|
(type->int type)
|
|
|
|
(protocol->int protocol)))
|
|
|
|
|
2020-07-09 15:51:13 +00:00
|
|
|
(define-ftype
|
|
|
|
[socklen_t unsigned-32]
|
|
|
|
[in_addr_t unsigned-32]
|
2020-07-09 10:12:38 +00:00
|
|
|
[in_addr
|
|
|
|
(struct
|
2020-07-09 15:51:13 +00:00
|
|
|
[addr in_addr_t])]
|
|
|
|
[sa_family_t unsigned-short]
|
|
|
|
[in_port_t unsigned-16]
|
|
|
|
[sockaddr_common
|
|
|
|
(struct
|
|
|
|
[family sa_family_t])]
|
|
|
|
[sockaddr
|
|
|
|
(struct
|
|
|
|
[common sockaddr_common]
|
|
|
|
[data (array 14 char)])]
|
2020-07-09 10:12:38 +00:00
|
|
|
[sockaddr_in
|
|
|
|
(struct
|
2020-07-09 15:51:13 +00:00
|
|
|
[common sockaddr_common]
|
|
|
|
[port in_port_t]
|
|
|
|
[addr in_addr]
|
|
|
|
[zero (array 8 char)])])
|
2020-07-09 10:12:38 +00:00
|
|
|
|
|
|
|
(define (level->int flag)
|
|
|
|
(case flag
|
|
|
|
((SOL_SOCKET) #x1)
|
|
|
|
))
|
|
|
|
|
|
|
|
(define (optname->int flag)
|
|
|
|
(case flag
|
|
|
|
((SO_REUSEADDR) #x2)
|
|
|
|
))
|
|
|
|
|
2020-07-09 10:51:53 +00:00
|
|
|
(define (inet_pton af src dst)
|
|
|
|
((foreign-procedure
|
|
|
|
"inet_pton"
|
|
|
|
(int string void*)
|
|
|
|
int)
|
|
|
|
(domain->int af)
|
|
|
|
src dst))
|
|
|
|
|
2020-07-09 13:59:25 +00:00
|
|
|
(define (inet_ntop af src dst size)
|
2020-07-09 10:51:53 +00:00
|
|
|
((foreign-procedure
|
2020-07-09 13:59:25 +00:00
|
|
|
"inet_ntop"
|
2020-07-09 15:51:13 +00:00
|
|
|
(int void* (* char) unsigned-32)
|
2020-07-09 14:20:59 +00:00
|
|
|
string)
|
2020-07-09 13:59:25 +00:00
|
|
|
(domain->int af)
|
|
|
|
src dst size))
|
|
|
|
|
|
|
|
(define (htons host)
|
2020-07-09 14:20:59 +00:00
|
|
|
((foreign-procedure "htons" (unsigned-16) unsigned-16) host))
|
2020-07-09 13:59:25 +00:00
|
|
|
|
|
|
|
(define (ntohs net)
|
2020-07-09 14:20:59 +00:00
|
|
|
((foreign-procedure "ntohs" (unsigned-16) unsigned-16) net))
|
2020-07-09 10:12:38 +00:00
|
|
|
|
|
|
|
(define (setsockopt sockfd level optname optval optlen)
|
|
|
|
((foreign-procedure
|
|
|
|
"setsockopt"
|
|
|
|
(int int int void* int)
|
|
|
|
int)
|
|
|
|
sockfd
|
|
|
|
(level->int level)
|
|
|
|
(optname->int optname)
|
|
|
|
optval optlen))
|
|
|
|
|
2020-07-09 10:51:53 +00:00
|
|
|
(define (bind sockfd address address_len)
|
|
|
|
((foreign-procedure
|
|
|
|
"bind"
|
|
|
|
(int void* int)
|
|
|
|
int) sockfd address address_len))
|
|
|
|
|
2020-07-09 13:59:25 +00:00
|
|
|
(define (listen sockfd backlog)
|
|
|
|
((foreign-procedure
|
|
|
|
"listen"
|
|
|
|
(int int)
|
|
|
|
int) sockfd backlog))
|
|
|
|
|
|
|
|
(define (msgflag->int flag)
|
|
|
|
(case flag
|
|
|
|
((MSG_DEFAULT) 0)
|
|
|
|
))
|
2020-07-09 10:12:38 +00:00
|
|
|
|
2020-07-09 13:59:25 +00:00
|
|
|
(define (recvfrom sockfd buf len msgflag src-addr addrlen)
|
|
|
|
((foreign-procedure
|
|
|
|
"recvfrom"
|
2020-07-09 15:51:13 +00:00
|
|
|
(int void* size_t int (* sockaddr_in) (* socklen_t))
|
2020-07-09 13:59:25 +00:00
|
|
|
int)
|
|
|
|
sockfd
|
|
|
|
buf
|
|
|
|
len
|
|
|
|
(msgflag->int msgflag)
|
|
|
|
src-addr
|
|
|
|
addrlen))
|
2020-07-10 08:32:46 +00:00
|
|
|
|
|
|
|
(define (sendto sockfd buf len msgflag dest-addr addrlen)
|
|
|
|
((foreign-procedure
|
|
|
|
"sendto"
|
|
|
|
(int void* size_t int (* sockaddr_in) socklen_t)
|
|
|
|
ssize_t)
|
|
|
|
sockfd buf len
|
|
|
|
(msgflag->int msgflag)
|
|
|
|
dest-addr addrlen))
|