From 5bc2128d6d016ed968fa1b61d1f7e02e990696cc Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 5 Oct 2010 21:44:22 -0500 Subject: [PATCH] Write a real resolv.conf parser --- extra/dns/unix/unix.factor | 12 +--- extra/resolv-conf/authors.txt | 1 + extra/resolv-conf/resolv-conf.factor | 88 ++++++++++++++++++++++++++++ extra/resolv-conf/resolv-conf.test | 28 +++++++++ 4 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 extra/resolv-conf/authors.txt create mode 100644 extra/resolv-conf/resolv-conf.factor create mode 100644 extra/resolv-conf/resolv-conf.test diff --git a/extra/dns/unix/unix.factor b/extra/dns/unix/unix.factor index c313c9145a..31af530f5c 100644 --- a/extra/dns/unix/unix.factor +++ b/extra/dns/unix/unix.factor @@ -1,13 +1,7 @@ ! Copyright (C) 2010 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: assocs dns io.encodings.utf8 io.files kernel -math.statistics sequences splitting system unicode.categories ; +USING: accessors dns resolv-conf system ; IN: dns.unix -: load-resolve.conf ( -- seq ) - "/etc/resolv.conf" utf8 file-lines - [ [ blank? ] trim ] map harvest - [ "#" head? not ] filter - [ [ " " split1 swap ] dip push-at ] sequence>hashtable "nameserver" swap at ; - -M: unix initial-dns-servers load-resolve.conf ; +M: unix initial-dns-servers + default-resolv.conf nameserver>> ; diff --git a/extra/resolv-conf/authors.txt b/extra/resolv-conf/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/extra/resolv-conf/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/extra/resolv-conf/resolv-conf.factor b/extra/resolv-conf/resolv-conf.factor new file mode 100644 index 0000000000..8b38f49116 --- /dev/null +++ b/extra/resolv-conf/resolv-conf.factor @@ -0,0 +1,88 @@ +! Copyright (C) 2010 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors combinators constructors io.encodings.utf8 +io.files kernel math math.parser sequences splitting +unicode.categories ; +IN: resolv-conf + +TUPLE: network ip netmask ; +CONSTRUCTOR: network ( ip netmask -- network ) ; + +TUPLE: options +debug? +{ ndots integer initial: 1 } +{ timeout integer initial: 5 } +{ attempts integer initial: 2 } +rotate? no-check-names? inet6? ; + +CONSTRUCTOR: options ( -- options ) ; + +TUPLE: resolv.conf nameserver domain search sortlist options ; + +CONSTRUCTOR: resolv.conf ( -- resolv.conf ) + V{ } clone >>nameserver + V{ } clone >>domain + V{ } clone >>search + V{ } clone >>sortlist + >>options ; + +> push-all ; + +: parse-domain ( resolv.conf string -- resolv.conf ) + trim-blanks " " split + [ trim-blanks ] map harvest over domain>> push-all ; + +: parse-search ( resolv.conf string -- resolv.conf ) + trim-blanks " " split + [ trim-blanks ] map harvest over search>> push-all ; + +: parse-sortlist ( resolv.conf string -- resolv.conf ) + trim-blanks " " split + [ trim-blanks "/" split1 ] map >>sortlist ; + +ERROR: unsupported-resolv.conf-option string ; + +: parse-integer ( string -- n ) + trim-blanks ":" ?head drop trim-blanks string>number ; + +: parse-option ( resolv.conf string -- resolv.conf ) + [ dup options>> ] dip trim-blanks { + { [ "debug" ?head ] [ drop t >>debug? ] } + { [ "ndots:" ?head ] [ parse-integer >>ndots ] } + { [ "timeout" ?head ] [ parse-integer >>timeout ] } + { [ "attempts" ?head ] [ parse-integer >>attempts ] } + { [ "rotate" ?head ] [ drop t >>rotate? ] } + { [ "no-check-names" ?head ] [ drop t >>no-check-names? ] } + { [ "inet6" ?head ] [ drop t >>inet6? ] } + [ unsupported-resolv.conf-option ] + } cond drop ; + +ERROR: unsupported-resolv.conf-line string ; + +: parse-resolv.conf-line ( resolv.conf string -- resolv.conf ) + { + { [ "nameserver" ?head ] [ parse-nameserver ] } + { [ "domain" ?head ] [ parse-domain ] } + { [ "search" ?head ] [ parse-search ] } + { [ "sortlist" ?head ] [ parse-sortlist ] } + { [ "options" ?head ] [ parse-option ] } + [ unsupported-resolv.conf-line ] + } cond ; + +PRIVATE> + +: parse-resolve.conf ( path -- resolv.conf ) + [ ] dip + utf8 file-lines + [ [ blank? ] trim ] map harvest + [ "#" head? not ] filter + [ parse-resolv.conf-line ] each ; + +: default-resolv.conf ( -- resolv.conf ) + "/etc/resolv.conf" parse-resolve.conf ; diff --git a/extra/resolv-conf/resolv-conf.test b/extra/resolv-conf/resolv-conf.test new file mode 100644 index 0000000000..1b17c3a70e --- /dev/null +++ b/extra/resolv-conf/resolv-conf.test @@ -0,0 +1,28 @@ +# +# Mac OS X Notice +# +# This file is not used by the host name and address resolution +# or the DNS query routing mechanisms used by most processes on +# this Mac OS X system. + # + # This file is automatically generated. + # + nameserver 8.8.8.8 + domain hmm.lol.com + search a.com b.com c.com + +sortlist 130.155.160.0/255.255.240.0 130.155.0.0 131.155.160.0/255.255.240.0 130.155.0.1 + + options debug + options ndots:10 + options timeout:11 + options attempts : 12 + options rotate + options no-check-names + options inet6 + + + + + +