Replaced posix regexp strings by sre expressions.

This commit is contained in:
mainzelm 2002-11-27 09:36:21 +00:00
parent 2442e88ab7
commit cbb4609c3a
1 changed files with 50 additions and 49 deletions

View File

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