io.sockets: add scope-id to ipv6 addrspecs, fixing a problem with connecting to localhost on Mac OS X (and other systems) where localhost is associated with a link-local address such as fe80::1

db4
Slava Pestov 2011-02-25 22:33:28 -08:00
parent e9bccc0d59
commit 266c6d32e7
3 changed files with 36 additions and 12 deletions

View File

@ -41,7 +41,7 @@ M: icmp4 resolve-host 1array ;
TUPLE: icmp6 < ipv6 ;
C: <icmp6> icmp6
: <icmp6> ( host -- icmp6 ) 0 icmp6 boa ;
M: ipv6 with-icmp host>> <icmp6> ;

View File

@ -1,11 +1,12 @@
USING: io.sockets io.sockets.private sequences math tools.test
namespaces accessors kernel destructors calendar io.timeouts
io.encodings.utf8 io concurrency.promises threads
io.streams.string ;
io.streams.string present ;
IN: io.sockets.tests
[ T{ local f "/tmp/foo" } ] [ "/tmp/foo" <local> ] unit-test
[ T{ inet4 f f 0 } ] [ f 0 <inet4> ] unit-test
[ T{ inet6 f f 0 } ] [ f 0 <inet6> ] unit-test
[ T{ inet6 f f 0 1 } ] [ f 1 <inet6> ] unit-test
[ T{ inet f "google.com" f } ] [ "google.com" f <inet> ] unit-test
@ -13,10 +14,23 @@ IN: io.sockets.tests
[ T{ inet f "google.com" 80 } ] [ "google.com" 0 <inet> 80 with-port ] unit-test
[ T{ inet4 f "8.8.8.8" 0 } ] [ "8.8.8.8" 0 <inet4> ] unit-test
[ T{ inet4 f "8.8.8.8" 53 } ] [ "8.8.8.8" 0 <inet4> 53 with-port ] unit-test
[ T{ inet6 f "5:5:5:5:6:6:6:6" 12 } ] [ "5:5:5:5:6:6:6:6" 0 <inet6> 12 with-port ] unit-test
[ T{ inet6 f "5:5:5:5:6:6:6:6" 0 12 } ] [ "5:5:5:5:6:6:6:6" 0 <inet6> 12 with-port ] unit-test
[ T{ inet6 f "fe80::1" 1 80 } ] [ T{ ipv6 f "fe80::1" 1 } 80 with-port ] unit-test
: test-sockaddr ( addrspec -- )
[ dup make-sockaddr ] keep parse-sockaddr assert= ;
[ ] [ T{ inet4 f "8.8.8.8" 53 } test-sockaddr ] unit-test
[ ] [ T{ inet6 f "5:5:5:5:6:6:6:6" 0 12 } test-sockaddr ] unit-test
[ ] [ T{ inet6 f "fe80:0:0:0:0:0:0:1" 1 80 } test-sockaddr ] unit-test
[ T{ inet f "google.com" 80 } ] [ "google.com" 80 with-port ] unit-test
! Test present on addrspecs
[ "4.4.4.4:12" ] [ "4.4.4.4" 12 <inet4> present ] unit-test
[ "::1:12" ] [ "::1" 12 <inet6> present ] unit-test
[ "fe80::1%1:12" ] [ "fe80::1" 1 12 inet6 boa present ] unit-test
[ B{ 1 2 3 4 } ]
[ "1.2.3.4" T{ inet4 } inet-pton ] unit-test

View File

@ -125,9 +125,11 @@ M: inet4 present
M: inet4 protocol drop 0 ;
TUPLE: ipv6 { host ?string read-only } ;
TUPLE: ipv6
{ host ?string read-only }
{ scope-id integer read-only } ;
C: <ipv6> ipv6
: <ipv6> ( host -- ipv6 ) 0 ipv6 boa ;
M: ipv6 inet-ntop ( data addrspec -- str )
drop 16 memory>byte-array 2 <groups> [ be> >hex ] map ":" join ;
@ -184,23 +186,31 @@ M: ipv6 make-sockaddr ( inet -- sockaddr )
AF_INET6 >>family
swap
[ port>> htons >>port ]
[ host>> "::" or ]
[ inet-pton >>addr ] tri ;
[ [ host>> "::" or ] keep inet-pton >>addr ]
[ scope-id>> >>scopeid ]
tri ;
M: ipv6 parse-sockaddr
[ addr>> ] dip inet-ntop <ipv6> ;
[ [ addr>> ] dip inet-ntop ] [ drop scopeid>> ] 2bi
ipv6 boa ;
M: ipv6 present
[ host>> ] [ scope-id>> ] bi
[ number>string "%" glue ] unless-zero ;
TUPLE: inet6 < ipv6 { port integer read-only } ;
C: <inet6> inet6
: <inet6> ( host port -- inet6 ) [ 0 ] dip inet6 boa ;
M: ipv6 with-port [ host>> ] dip <inet6> ;
M: ipv6 with-port
[ [ host>> ] [ scope-id>> ] bi ] dip
inet6 boa ;
M: inet6 parse-sockaddr
[ call-next-method ] [ drop port>> ntohs ] 2bi with-port ;
M: inet6 present
[ host>> ] [ port>> number>string ] bi ":" glue ;
[ call-next-method ] [ port>> number>string ] bi ":" glue ;
M: inet6 protocol drop 0 ;