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
{ $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } }
{ $description "Resolves host names to IP addresses." }
{ $errors "Throws an " { $link addrinfo-error } " if the host name cannot be resolved." }
{ $examples
{ $code
"\"www.facebook.com\" resolve-host . "
@ -235,7 +236,6 @@ HELP: resolve-host
}
} ;
HELP: with-local-address
{ $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." }

View File

@ -1,5 +1,5 @@
USING: io.sockets io.sockets.private sequences math tools.test
namespaces accessors kernel destructors calendar io.timeouts
USING: continuations 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 present system ;
IN: io.sockets.tests
@ -172,3 +172,7 @@ os unix? [
[ 80 ] [ "http" 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 ;
ERROR: addrinfo-error n string ;
<PRIVATE
GENERIC: (get-local-address) ( handle remote -- sockaddr )
@ -311,7 +313,7 @@ HOOK: (send) io-backend ( packet addrspec datagram -- )
[ addrinfo>addrspec ] map
sift ;
HOOK: addrinfo-error io-backend ( n -- )
HOOK: addrinfo-error-string io-backend ( n -- string )
: prepare-addrinfo ( -- addrinfo )
addrinfo <struct>
@ -407,8 +409,11 @@ M: inet present
C: <inet> inet
M: string resolve-host
f prepare-addrinfo f void* <ref>
[ getaddrinfo addrinfo-error ] keep void* deref addrinfo memory>struct
f prepare-addrinfo f void* <ref> [
getaddrinfo 0 or [
dup addrinfo-error-string addrinfo-error
] unless-zero
] keep void* deref addrinfo memory>struct
[ parse-addrinfo-list ] keep freeaddrinfo ;
M: string with-port <inet> ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2004, 2008 Slava Pestov, Ivan Tikhonov.
! See http://factorcode.org/license.txt for BSD license.
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
libc locals math namespaces sequences system unix
unix.ffi vocabs ;
@ -15,8 +15,8 @@ IN: io.sockets.unix
: set-socket-option ( fd level opt -- )
[ handle-fd ] 2dip 1 int <ref> dup byte-length setsockopt io-error ;
M: unix addrinfo-error ( n -- )
[ gai_strerror throw ] unless-zero ;
M: unix addrinfo-error-string ( n -- string )
gai_strerror ;
M: unix sockaddr-of-family ( alien af -- addrspec )
{

View File

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