1 ! Copyright (C) 2007, 2008 Slava Pestov, Doug Coleman,
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: generic kernel io.backend namespaces continuations
5 sequences arrays io.encodings io.ports io.streams.duplex
6 io.encodings.ascii alien.strings io.binary accessors destructors
7 classes byte-arrays system combinators parser
8 alien.c-types math.parser splitting grouping math assocs summary
9 system vocabs.loader combinators present fry vocabs.parser ;
13 { [ os windows? ] [ "windows.winsock" ] }
14 { [ os unix? ] [ "unix" ] }
18 GENERIC: protocol-family ( addrspec -- af )
20 GENERIC: sockaddr-size ( addrspec -- n )
22 GENERIC: make-sockaddr ( addrspec -- sockaddr )
24 GENERIC: empty-sockaddr ( addrspec -- sockaddr )
26 GENERIC: address-size ( addrspec -- n )
28 GENERIC: inet-ntop ( data addrspec -- str )
30 GENERIC: inet-pton ( str addrspec -- data )
32 : make-sockaddr/size ( addrspec -- sockaddr size )
33 [ make-sockaddr ] [ sockaddr-size ] bi ;
35 : empty-sockaddr/size ( addrspec -- sockaddr size )
36 [ empty-sockaddr ] [ sockaddr-size ] bi ;
38 GENERIC: parse-sockaddr ( sockaddr addrspec -- newaddrspec )
42 : <local> ( path -- addrspec )
43 normalize-path local boa ;
45 M: local present path>> "Unix domain socket: " prepend ;
47 TUPLE: abstract-inet host port ;
49 M: abstract-inet present
50 [ host>> ":" ] [ port>> number>string ] bi 3append ;
52 TUPLE: inet4 < abstract-inet ;
56 M: inet4 inet-ntop ( data addrspec -- str )
57 drop 4 memory>byte-array [ number>string ] { } map-as "." join ;
59 ERROR: invalid-inet4 string reason ;
61 M: invalid-inet4 summary drop "Invalid IPv4 address" ;
63 M: inet4 inet-pton ( str addrspec -- data )
66 "." split dup length 4 = [
67 "Must have four components" throw
71 [ "Dotted component not a number" throw ] unless*
73 ] [ invalid-inet4 ] recover ;
75 M: inet4 address-size drop 4 ;
77 M: inet4 protocol-family drop PF_INET ;
79 M: inet4 sockaddr-size drop "sockaddr-in" heap-size ;
81 M: inet4 empty-sockaddr drop "sockaddr-in" <c-object> ;
83 M: inet4 make-sockaddr ( inet -- sockaddr )
84 "sockaddr-in" <c-object>
85 AF_INET over set-sockaddr-in-family
86 over port>> htons over set-sockaddr-in-port
89 rot inet-pton *uint over set-sockaddr-in-addr ;
91 M: inet4 parse-sockaddr
92 [ dup sockaddr-in-addr <uint> ] dip inet-ntop
93 swap sockaddr-in-port ntohs <inet4> ;
95 TUPLE: inet6 < abstract-inet ;
99 M: inet6 inet-ntop ( data addrspec -- str )
100 drop 16 memory>byte-array 2 <groups> [ be> >hex ] map ":" join ;
102 ERROR: invalid-inet6 string reason ;
104 M: invalid-inet6 summary drop "Invalid IPv6 address" ;
108 : parse-inet6 ( string -- seq )
111 hex> [ "Component not a number" throw ] unless*
115 : pad-inet6 ( string1 string2 -- seq )
116 2dup [ length ] bi@ + 8 swap -
117 dup 0 < [ "More than 8 components" throw ] when
120 : inet6-bytes ( seq -- bytes )
121 [ 2 >be ] { } map-as concat >byte-array ;
125 M: inet6 inet-pton ( str addrspec -- data )
128 "::" split1 [ parse-inet6 ] bi@ pad-inet6 inet6-bytes
129 ] [ invalid-inet6 ] recover ;
131 M: inet6 address-size drop 16 ;
133 M: inet6 protocol-family drop PF_INET6 ;
135 M: inet6 sockaddr-size drop "sockaddr-in6" heap-size ;
137 M: inet6 empty-sockaddr drop "sockaddr-in6" <c-object> ;
139 M: inet6 make-sockaddr ( inet -- sockaddr )
140 "sockaddr-in6" <c-object>
141 AF_INET6 over set-sockaddr-in6-family
142 over port>> htons over set-sockaddr-in6-port
144 rot inet-pton over set-sockaddr-in6-addr ;
146 M: inet6 parse-sockaddr
147 [ dup sockaddr-in6-addr ] dip inet-ntop
148 swap sockaddr-in6-port ntohs <inet6> ;
150 : addrspec-of-family ( af -- addrspec )
152 { AF_INET [ T{ inet4 } ] }
153 { AF_INET6 [ T{ inet6 } ] }
154 { AF_UNIX [ T{ local } ] }
158 M: f parse-sockaddr nip ;
160 GENERIC: (get-local-address) ( handle remote -- sockaddr )
162 : get-local-address ( handle remote -- local )
163 [ (get-local-address) ] keep parse-sockaddr ;
165 GENERIC: (get-remote-address) ( handle remote -- sockaddr )
167 : get-remote-address ( handle local -- remote )
168 [ (get-remote-address) ] keep parse-sockaddr ;
170 : <ports> ( handle -- input-port output-port )
172 [ <input-port> |dispose ] [ <output-port> |dispose ] bi
175 GENERIC: establish-connection ( client-out remote -- )
177 GENERIC: ((client)) ( remote -- handle )
179 GENERIC: (client) ( remote -- client-in client-out local )
181 M: array (client) [ (client) 3array ] attempt-all first3 ;
183 M: object (client) ( remote -- client-in client-out local )
187 [ <ports> [ |dispose ] bi@ dup ] dip
190 [ get-local-address ]
194 : <client> ( remote encoding -- stream local )
195 [ (client) -rot ] dip <encoder-duplex> swap ;
197 SYMBOL: local-address
199 SYMBOL: remote-address
201 : with-client ( remote encoding quot -- )
204 over remote-address set
205 <client> local-address set
207 ] with-scope ; inline
209 TUPLE: server-port < port addr encoding ;
211 : check-server-port ( port -- port )
213 dup server-port? [ "Not a server port" throw ] unless ; inline
215 GENERIC: (server) ( addrspec -- handle )
217 : <server> ( addrspec encoding -- server )
220 [ drop server-port <port> ] [ get-local-address ] 2bi
224 GENERIC: (accept) ( server addrspec -- handle sockaddr )
226 : accept ( server -- client remote )
232 ] keep encoding>> <encoder-duplex> swap ;
234 TUPLE: datagram-port < port addr ;
236 HOOK: (datagram) io-backend ( addr -- datagram )
238 : <datagram> ( addrspec -- datagram )
240 [ (datagram) |dispose ] keep
241 [ drop datagram-port <port> ] [ get-local-address ] 2bi
245 : check-datagram-port ( port -- port )
247 dup datagram-port? [ "Not a datagram port" throw ] unless ; inline
249 HOOK: (receive) io-backend ( datagram -- packet addrspec )
251 : receive ( datagram -- packet addrspec )
253 [ (receive) ] [ addr>> ] bi parse-sockaddr ;
255 : check-datagram-send ( packet addrspec port -- packet addrspec port )
257 2dup addr>> [ class ] bi@ assert=
258 pick class byte-array assert= ;
260 HOOK: (send) io-backend ( packet addrspec datagram -- )
262 : send ( packet addrspec datagram -- )
263 check-datagram-send (send) ;
265 : addrinfo>addrspec ( addrinfo -- addrspec )
266 [ addrinfo-addr ] [ addrinfo-family addrspec-of-family ] bi
269 : parse-addrinfo-list ( addrinfo -- seq )
270 [ addrinfo-next ] follow
271 [ addrinfo>addrspec ] map
274 HOOK: addrinfo-error io-backend ( n -- )
276 GENERIC: resolve-host ( addrspec -- seq )
278 TUPLE: inet < abstract-inet ;
282 : resolve-passive-host ( -- addrspecs )
283 { T{ inet6 f "::" f } T{ inet4 f "0.0.0.0" f } } [ clone ] map ;
285 : prepare-addrinfo ( -- addrinfo )
286 "addrinfo" <c-object>
287 PF_UNSPEC over set-addrinfo-family
288 IPPROTO_TCP over set-addrinfo-protocol ;
290 : fill-in-ports ( addrspecs port -- addrspecs )
294 [ port>> ] [ host>> ] bi [
295 f prepare-addrinfo f <void*>
296 [ getaddrinfo addrinfo-error ] keep *void*
297 [ parse-addrinfo-list ] keep freeaddrinfo
298 ] [ resolve-passive-host ] if*
301 M: f resolve-host drop { } ;
303 M: object resolve-host 1array ;
305 : host-name ( -- string )
306 256 <byte-array> dup dup length gethostname
307 zero? [ "gethostname failed" throw ] unless
310 M: inet (client) resolve-host (client) ;
312 ERROR: invalid-inet-server addrspec ;
314 M: invalid-inet-server summary
315 drop "Cannot use <server> with <inet>; use <inet4> or <inet6> instead" ;
318 invalid-inet-server ;
321 { [ os unix? ] [ "io.sockets.unix" require ] }
322 { [ os winnt? ] [ "io.sockets.windows.nt" require ] }