]> gitweb.factorcode.org Git - factor.git/commitdiff
io.sockets: add word addrinfo-error to signal host lookup failures
authorBjörn Lindqvist <bjourne@gmail.com>
Sun, 16 Mar 2014 11:56:48 +0000 (12:56 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 20 Apr 2014 16:29:31 +0000 (09:29 -0700)
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.

basis/io/sockets/sockets-docs.factor
basis/io/sockets/sockets-tests.factor
basis/io/sockets/sockets.factor
basis/io/sockets/unix/unix.factor
basis/io/sockets/windows/windows.factor

index a83465f009289e1d0a3aad6932ceedff907e2d20..d40991991bc615904e2eed125db0bb9580ef2702 100644 (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." }
index 7adcc93cb64a00f4e3963f0ebf8765ad96b06991..9e146dacfb18e2fbbb7c2130b21484302d81b9c5 100644 (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
index 4e5c3a72a4901871ff485896f81edc5182d008a5..51e5979c2f8ad3cd3f67923868a2483eb854e6ff 100644 (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> ;
index 09e57edb5b60c6494035789c671bb162e6c77488..73fa9060857257bcc0c30776d366ead92d5e9a26 100644 (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 )
     {
index 2df94f854480b41cd25cdcdbf55b6e3bd27aadfc..86e5555dddbe9c9ca31f00e9cc2add6173805047 100755 (executable)
@@ -14,8 +14,8 @@ IN: io.sockets.windows
 : set-ioctl-socket ( handle cmd arg -- )\r
     [ handle>> ] 2dip ulong <ref> ioctlsocket socket-error ;\r
 \r
-M: windows addrinfo-error ( n -- )\r
-    winsock-return-check ;\r
+M: windows addrinfo-error-string ( n -- string )\r
+    n>win32-error-string ;\r
 \r
 M: windows sockaddr-of-family ( alien af -- addrspec )\r
     {\r