From a2a598546fa06ed517e9b1acc7f0495324834cee Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 23 Sep 2010 13:19:15 -0500 Subject: [PATCH] Only return ipv4 addresses for binding to on linux. --- basis/io/servers/connection/connection.factor | 5 +--- basis/io/sockets/sockets.factor | 29 ++++++++++++++----- basis/io/sockets/unix/linux/authors.txt | 1 + basis/io/sockets/unix/linux/linux.factor | 9 ++++++ basis/io/sockets/unix/linux/platforms.txt | 1 + basis/io/sockets/unix/unix.factor | 17 ++++++----- 6 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 basis/io/sockets/unix/linux/authors.txt create mode 100644 basis/io/sockets/unix/linux/linux.factor create mode 100644 basis/io/sockets/unix/linux/platforms.txt diff --git a/basis/io/servers/connection/connection.factor b/basis/io/servers/connection/connection.factor index 861a621de4..fbe5421cea 100644 --- a/basis/io/servers/connection/connection.factor +++ b/basis/io/servers/connection/connection.factor @@ -87,12 +87,9 @@ M: f (>insecure) ; >insecure [ dup { [ secure? ] [ not ] } 1|| [ ] unless ] map ; -: filter-ipv6 ( seq -- seq' ) - ipv6-supported? [ [ ipv6? not ] filter ] unless ; - : listen-on ( threaded-server -- addrspecs ) [ secure>> >secure ] [ insecure>> >insecure ] bi append - [ resolve-host ] map concat filter-ipv6 ; + [ resolve-host ] map concat ; : accepted-connection ( remote local -- ) [ diff --git a/basis/io/sockets/sockets.factor b/basis/io/sockets/sockets.factor index efafef9d31..b2860b7f4a 100644 --- a/basis/io/sockets/sockets.factor +++ b/basis/io/sockets/sockets.factor @@ -4,10 +4,11 @@ USING: accessors alien.c-types alien.data alien.strings arrays assocs byte-arrays classes classes.struct combinators combinators.short-circuit continuations destructors fry generic -grouping io.backend io.binary io.encodings io.encodings.ascii -io.encodings.binary io.ports io.streams.duplex kernel math -math.parser namespaces parser present sequences splitting -strings summary system vocabs.loader vocabs.parser ; +grouping init io.backend io.binary io.encodings +io.encodings.ascii io.encodings.binary io.ports +io.streams.duplex kernel math math.parser memoize namespaces +parser present sequences splitting strings summary system +vocabs.loader vocabs.parser ; IN: io.sockets << { @@ -349,8 +350,16 @@ SYMBOL: remote-address : send ( packet addrspec datagram -- ) check-send (send) ; +MEMO: ipv6-supported? ( -- ? ) + [ "::1" 0 binary dispose t ] [ drop f ] recover ; + +[ \ ipv6-supported? reset-memoized ipv6-supported? drop ] +"ipv6-support-check" add-startup-hook + GENERIC: resolve-host ( addrspec -- seq ) +HOOK: resolve-localhost os ( -- obj ) + TUPLE: hostname { host ?string read-only } ; TUPLE: inet < hostname port ; @@ -378,7 +387,14 @@ M: inet6 resolve-host 1array ; M: local resolve-host 1array ; M: f resolve-host - drop { T{ ipv6 f "::" } T{ ipv4 f "0.0.0.0" } } ; + drop resolve-localhost ; + +M: object resolve-localhost + ipv6-supported? [ + { T{ ipv4 f "0.0.0.0" } T{ ipv6 f "::" } } + ] [ + { T{ ipv4 f "0.0.0.0" } } + ] if ; : host-name ( -- string ) 256 dup dup length gethostname @@ -407,9 +423,6 @@ M: invalid-local-address summary [ invalid-local-address ] if ] dip with-variable ; inline -: ipv6-supported? ( -- ? ) - [ "::1" 0 binary dispose t ] [ drop f ] recover ; - { { [ os unix? ] [ "io.sockets.unix" require ] } { [ os windows? ] [ "io.sockets.windows" require ] } diff --git a/basis/io/sockets/unix/linux/authors.txt b/basis/io/sockets/unix/linux/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/basis/io/sockets/unix/linux/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/basis/io/sockets/unix/linux/linux.factor b/basis/io/sockets/unix/linux/linux.factor new file mode 100644 index 0000000000..a2c4d96633 --- /dev/null +++ b/basis/io/sockets/unix/linux/linux.factor @@ -0,0 +1,9 @@ +! Copyright (C) 2010 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: io.sockets kernel system ; +IN: io.sockets.unix.linux + +! Linux seems to use the same port-space for ipv4 and ipv6. + +M: linux resolve-localhost { T{ ipv4 f "0.0.0.0" } } ; + diff --git a/basis/io/sockets/unix/linux/platforms.txt b/basis/io/sockets/unix/linux/platforms.txt new file mode 100644 index 0000000000..a08e1f35eb --- /dev/null +++ b/basis/io/sockets/unix/linux/platforms.txt @@ -0,0 +1 @@ +linux diff --git a/basis/io/sockets/unix/unix.factor b/basis/io/sockets/unix/unix.factor index ee8563e27a..4d6c699211 100644 --- a/basis/io/sockets/unix/unix.factor +++ b/basis/io/sockets/unix/unix.factor @@ -1,16 +1,15 @@ ! Copyright (C) 2004, 2008 Slava Pestov, Ivan Tikhonov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien alien.c-types alien.strings generic kernel math -threads sequences byte-arrays io.binary io.backend.unix -io.streams.duplex io.backend io.pathnames io.sockets.private -io.files.private io.encodings.utf8 math.parser continuations -libc combinators system accessors destructors unix locals init -classes.struct alien.data unix.ffi ; - +USING: accessors alien alien.c-types alien.data alien.strings +byte-arrays classes.struct combinators continuations +destructors generic init io.backend io.backend.unix io.binary +io.encodings.utf8 io.files.private io.pathnames +io.sockets.private io.streams.duplex kernel libc locals math +math.parser sequences system threads unix unix.ffi +vocabs.loader ; EXCLUDE: namespaces => bind ; EXCLUDE: io => read write ; EXCLUDE: io.sockets => accept ; - IN: io.sockets.unix : socket-fd ( domain type protocol -- fd ) @@ -185,3 +184,5 @@ M: local make-sockaddr M: local parse-sockaddr drop path>> utf8 alien>string ; + +os linux? [ "io.sockets.unix.linux" require ] when