1
2(c-system-include "arpa/inet.h")
3(c-system-include "sys/types.h")
4(c-system-include "sys/socket.h")
5(c-system-include "netinet/in.h")
6(c-system-include "netdb.h")
7
8(define-c-int-type socklen_t)
9
10(define-c-struct sockaddr
11  constructor: (make-sockaddr)
12  predicate:   sockaddr?)
13
14(define-c-struct addrinfo
15  constructor: (%make-address-info ai_family ai_socktype ai_protocol ai_flags)
16  finalizer: freeaddrinfo
17  predicate: address-info?
18  (int              ai_family    address-info-family)
19  (int              ai_socktype  address-info-socket-type)
20  (int              ai_protocol  address-info-protocol)
21  (int              ai_flags     address-info-flags)
22  ((link sockaddr)  ai_addr      address-info-address)
23  (size_t           ai_addrlen   address-info-address-length)
24  (string           ai_canonname address-info-canonname)
25  ((link addrinfo)  ai_next      address-info-next))
26
27;;> The addrinfo struct accessors.
28;;/
29
30(define-c errno (%get-address-info getaddrinfo)
31  ((maybe-null string) (maybe-null string) (maybe-null addrinfo)
32   (result free addrinfo)))
33
34;;> Bind a name to a socket.
35
36(define-c sexp (bind "sexp_bind")
37  ((value ctx sexp) (value self sexp) fileno sockaddr int))
38
39;;> Listen on a socket.
40
41(define-c sexp (listen "sexp_listen")
42  ((value ctx sexp) (value self sexp) sexp sexp))
43
44;;> Accept a connection on a socket.
45
46(define-c sexp (accept "sexp_accept")
47  ((value ctx sexp) (value self sexp) fileno sockaddr int))
48
49;;> Create an endpoint for communication.
50
51(define-c fileno socket (int int int))
52
53;;> Initiate a connection on a socket.
54
55(define-c int connect (fileno sockaddr int))
56
57(define-c sexp (%send "sexp_sendto")
58  ((value ctx sexp) (value self sexp)
59   fileno bytevector (value (bytevector-length arg3) size_t) int
60   (maybe-null sockaddr) socklen_t sexp))
61
62(define-c sexp (%receive! "sexp_recvfrom")
63  ((value ctx sexp) (value self sexp)
64   fileno bytevector (value (bytevector-length arg3) size_t) int
65   (maybe-null sockaddr) socklen_t sexp))
66
67;;> Returns a list of 2 new sockets, the input and output end of a new
68;;> pipe, respectively.
69
70(define-c errno (open-socket-pair "socketpair")
71  (int int int (result (array fileno 2))))
72
73;;> Return the IP address of a sockaddr as an "N.N.N.N" string.
74
75(define-c sexp (sockaddr-name "sexp_sockaddr_name")
76  ((value ctx sexp) (value self sexp) sockaddr))
77
78;;> Return the port a sockaddr is connected on.
79
80(define-c int (sockaddr-port "sexp_sockaddr_port")
81  ((value ctx sexp) (value self sexp) sockaddr))
82
83(define-c-const int (address-family/unspecified "AF_UNSPEC"))
84(define-c-const int (address-family/unix "AF_UNIX"))
85(define-c-const int (address-family/inet "AF_INET"))
86(define-c-const int (address-family/inet6 "AF_INET6"))
87(define-c-const int (socket-type/stream "SOCK_STREAM"))
88(define-c-const int (socket-type/datagram "SOCK_DGRAM"))
89(define-c-const int (socket-type/raw "SOCK_RAW"))
90(define-c-const int (ip-proto/ip "IPPROTO_IP"))
91(define-c-const int (ip-proto/icmp "IPPROTO_ICMP"))
92(define-c-const int (ip-proto/tcp "IPPROTO_TCP"))
93(define-c-const int (ip-proto/udp "IPPROTO_UDP"))
94(define-c-const int (ai/passive "AI_PASSIVE"))
95(define-c-const int (ai/canonname "AI_CANONNAME"))
96(define-c-const int (ai/numeric-host "AI_NUMERICHOST"))
97
98;;> The constants for the addrinfo struct.
99;;/
100
101(c-include-verbatim "accept.c")
102
103(define-c errno (get-peer-name getpeername)
104  (fileno sockaddr (result (value (sizeof sockaddr) socklen_t))))
105
106(define-c errno getsockopt
107  (fileno int int (result int) (result (value (sizeof int) socklen_t))))
108
109;;> Set an option for the given socket.  For example, to make the
110;;> address reusable:
111;;> \scheme{(set-socket-option! sock level/socket socket-opt/reuseaddr 1)}
112
113(define-c errno (set-socket-option! "setsockopt")
114  (fileno int int (pointer int) (value (sizeof int) socklen_t)))
115
116(define-c-const int (level/socket "SOL_SOCKET"))
117
118(define-c-const int (socket-opt/debug "SO_DEBUG"))
119(define-c-const int (socket-opt/broadcast "SO_BROADCAST"))
120(define-c-const int (socket-opt/reuseaddr "SO_REUSEADDR"))
121(define-c-const int (socket-opt/keepalive "SO_KEEPALIVE"))
122(define-c-const int (socket-opt/oobinline "SO_OOBINLINE"))
123(define-c-const int (socket-opt/sndbuf "SO_SNDBUF"))
124(define-c-const int (socket-opt/rcvbuf "SO_RCVBUF"))
125(define-c-const int (socket-opt/dontroute "SO_DONTROUTE"))
126(define-c-const int (socket-opt/rcvlowat "SO_RCVLOWAT"))
127(define-c-const int (socket-opt/sndlowat "SO_SNDLOWAT"))
128
129;;> The constants for the \scheme{get-socket-option} and
130;;> \scheme{set-socket-option!}.
131;;/
132