diff --git a/scheme/lib/dns.scm b/scheme/lib/dns.scm index 283a97d..e3469aa 100644 --- a/scheme/lib/dns.scm +++ b/scheme/lib/dns.scm @@ -264,7 +264,8 @@ (if *debug* (display "name->octets\n")) (let loop ((s s)) (cond - ((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s) + ((regexp-search (rx (: bos (submatch (* (~ "."))) "." (submatch (* any)))) + s) => (lambda (match) (append (encode-portion (match:substring match 1)) @@ -276,6 +277,7 @@ (encode-portion s) (list *nul*))))))) + ;; for tcp: message has to be tagged with its length (define (add-size-tag m) (if *debug* (display "add-size-tag\n")) @@ -322,17 +324,25 @@ (char->ascii (list-ref s 2)) (char->ascii (list-ref s 3)))) +(define ip-string-regexp (rx (: bos + (submatch (** 1 3 digit)) "." + (submatch (** 1 3 digit)) "." + (submatch (** 1 3 digit)) "." + (submatch (** 1 3 digit)) + eos))) + ;; converts an ip-string to octets (define (string->octet-ip s) - (if *debug* (display "string->octet-ip\n")) - (let loop ((s s) - (result '())) - (cond - ((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s) - => (lambda (match) - (loop (match:substring match 2) (append result (list (ascii->char (string->number (match:substring match 1)))))))) - (else - (append result (list (ascii->char (string->number s)))))))) + (cond + ((regexp-search ip-string-regexp s) + => (lambda (match) + (list + (ascii->char (string->number (match:substring match 1))) + (ascii->char (string->number (match:substring match 2))) + (ascii->char (string->number (match:substring match 3))) + (ascii->char (string->number (match:substring match 4)))))) + (else + (error "invalid ip-string" s)))) ;; calculates a "random" number, needed for message-ids (define random @@ -344,24 +354,21 @@ ;; checks if a string is a ip (define (ip-string? s) - (if *debug* (display "ip-string->in-addr\n")) - (let loop ((s s) - (count 0)) - (cond - ((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s) - => (lambda (match) - (let* ((portion (match:substring match 1)) - (number (string->number portion))) - (if (and number (< number 256)) - (loop (match:substring match 2) (+ count 1)) - #f)))) - (else - (let ((number (string->number s))) - (and number - (< number 256) - (= count 3) - #t)))))) + (define (byte-as-string? string) + (let ((number (string->number string))) + (and number + (>= number 0) + (< number 256)))) + (cond + ((regexp-search ip-string-regexp s) + => (lambda (match) + (and (byte-as-string? (match:substring match 1)) + (byte-as-string? (match:substring match 2)) + (byte-as-string? (match:substring match 3)) + (byte-as-string? (match:substring match 4))))) + (else #f))) + ;; checks if v is a address32 (define (address32? v) (and (number? v) @@ -369,25 +376,17 @@ ;; returns a in-addr.arpa name-string or #f (needed to resolve hostname by ip) (define (ip-string->in-addr s) - (if *debug* (display "ip-string->in-addr\n")) - (let loop ((s s) - (count 0) - (result "")) - (cond - ((regexp-search (posix-string->regexp "^([^.]*)\\.(.*)") s) - => (lambda (match) - (let* ((portion (match:substring match 1)) - (number (string->number portion))) - (if (and number (< number 256)) - (loop (match:substring match 2) (+ count 1) (string-append portion "." result)) - #f)))) - (else - (let ((number (string->number s))) - (and number - (< number 256) - (= count 3) - (string-append s "." result "in-addr.arpa"))))))) - + (cond + ((regexp-search ip-string-regexp s) + => (lambda (match) + (string-append + (match:substring match 4) "." + (match:substring match 3) "." + (match:substring match 2) "." + (match:substring match 1) "." + "in-addr.arpa"))) + (else #f))) + ;; filters types in a list of rrs (define (filter-type list type) (if *debug* (display "ip-string->in-addr\n")) @@ -1045,9 +1044,11 @@ (if (null? ns) (dns-error 'no-nameservers) ns)) - ((regexp-search - (posix-string->regexp - "nameserver[ ]+([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)") l) + ((regexp-search + (rx (: "nameserver" (+ (| " " "\t") + (submatch (** 1 3 digit) + (= 3 (: "." (** 1 3 digit))))))) + l) => (lambda (match) (loop (append ns (list (ip-string->address32 (match:substring match 1))))))) (else