io.sockets: add word addrinfo-error to signal host lookup failures

If the getaddrinfo call fails, and you want to handle that failure in
a structured way, then you need a designated error type to know what
went wrong.
db4
Björn Lindqvist 2014-03-16 12:56:48 +01:00 committed by John Benediktsson
parent 516ffcc311
commit e1206d5ef1
5 changed files with 20 additions and 11 deletions

View File

@ -228,6 +228,7 @@ HELP: send
HELP: resolve-host HELP: resolve-host
{ $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } } { $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } }
{ $description "Resolves host names to IP addresses." } { $description "Resolves host names to IP addresses." }
{ $errors "Throws an " { $link addrinfo-error } " if the host name cannot be resolved." }
{ $examples { $examples
{ $code { $code
"\"www.facebook.com\" resolve-host . " "\"www.facebook.com\" resolve-host . "
@ -235,7 +236,6 @@ HELP: resolve-host
} }
} ; } ;
HELP: with-local-address HELP: with-local-address
{ $values { "addr" "an " { $link inet4 } " or " { $link inet6 } " address specifier" } { "quot" quotation } } { $values { "addr" "an " { $link inet4 } " or " { $link inet6 } " address specifier" } { "quot" quotation } }
{ $description "Client sockets opened within the scope of the quotation passed to this combinator will have their local address bound to the given address." } { $description "Client sockets opened within the scope of the quotation passed to this combinator will have their local address bound to the given address." }

View File

@ -1,5 +1,5 @@
USING: io.sockets io.sockets.private sequences math tools.test USING: continuations io.sockets io.sockets.private sequences math
namespaces accessors kernel destructors calendar io.timeouts tools.test namespaces accessors kernel destructors calendar io.timeouts
io.encodings.utf8 io concurrency.promises threads io.encodings.utf8 io concurrency.promises threads
io.streams.string present system ; io.streams.string present system ;
IN: io.sockets.tests IN: io.sockets.tests
@ -172,3 +172,7 @@ os unix? [
[ 80 ] [ "http" protocol-port ] unit-test [ 80 ] [ "http" protocol-port ] unit-test
[ f ] [ f protocol-port ] unit-test [ f ] [ f protocol-port ] unit-test
[ t ] [
[ "you-cant-resolve-me!" resolve-host ] [ addrinfo-error? ] recover
] unit-test

View File

@ -228,6 +228,8 @@ M: inet6 present
M: inet6 protocol drop 0 ; M: inet6 protocol drop 0 ;
ERROR: addrinfo-error n string ;
<PRIVATE <PRIVATE
GENERIC: (get-local-address) ( handle remote -- sockaddr ) GENERIC: (get-local-address) ( handle remote -- sockaddr )
@ -311,7 +313,7 @@ HOOK: (send) io-backend ( packet addrspec datagram -- )
[ addrinfo>addrspec ] map [ addrinfo>addrspec ] map
sift ; sift ;
HOOK: addrinfo-error io-backend ( n -- ) HOOK: addrinfo-error-string io-backend ( n -- string )
: prepare-addrinfo ( -- addrinfo ) : prepare-addrinfo ( -- addrinfo )
addrinfo <struct> addrinfo <struct>
@ -407,8 +409,11 @@ M: inet present
C: <inet> inet C: <inet> inet
M: string resolve-host M: string resolve-host
f prepare-addrinfo f void* <ref> f prepare-addrinfo f void* <ref> [
[ getaddrinfo addrinfo-error ] keep void* deref addrinfo memory>struct getaddrinfo 0 or [
dup addrinfo-error-string addrinfo-error
] unless-zero
] keep void* deref addrinfo memory>struct
[ parse-addrinfo-list ] keep freeaddrinfo ; [ parse-addrinfo-list ] keep freeaddrinfo ;
M: string with-port <inet> ; M: string with-port <inet> ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2004, 2008 Slava Pestov, Ivan Tikhonov. ! Copyright (C) 2004, 2008 Slava Pestov, Ivan Tikhonov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.data alien.strings USING: accessors alien alien.c-types alien.data alien.strings
classes.struct combinators destructors io.backend.unix arrays classes.struct combinators destructors io.backend.unix
io.encodings.utf8 io.files io.pathnames io.sockets.private kernel io.encodings.utf8 io.files io.pathnames io.sockets.private kernel
libc locals math namespaces sequences system unix libc locals math namespaces sequences system unix
unix.ffi vocabs ; unix.ffi vocabs ;
@ -15,8 +15,8 @@ IN: io.sockets.unix
: set-socket-option ( fd level opt -- ) : set-socket-option ( fd level opt -- )
[ handle-fd ] 2dip 1 int <ref> dup byte-length setsockopt io-error ; [ handle-fd ] 2dip 1 int <ref> dup byte-length setsockopt io-error ;
M: unix addrinfo-error ( n -- ) M: unix addrinfo-error-string ( n -- string )
[ gai_strerror throw ] unless-zero ; gai_strerror ;
M: unix sockaddr-of-family ( alien af -- addrspec ) M: unix sockaddr-of-family ( alien af -- addrspec )
{ {

View File

@ -14,8 +14,8 @@ IN: io.sockets.windows
: set-ioctl-socket ( handle cmd arg -- ) : set-ioctl-socket ( handle cmd arg -- )
[ handle>> ] 2dip ulong <ref> ioctlsocket socket-error ; [ handle>> ] 2dip ulong <ref> ioctlsocket socket-error ;
M: windows addrinfo-error ( n -- ) M: windows addrinfo-error-string ( n -- string )
winsock-return-check ; n>win32-error-string ;
M: windows sockaddr-of-family ( alien af -- addrspec ) M: windows sockaddr-of-family ( alien af -- addrspec )
{ {