Push packet reencoding back into the chassis too

This commit is contained in:
Tony Garnock-Jones 2011-12-15 12:22:59 -05:00
parent 2599833904
commit b15ce583e3
2 changed files with 12 additions and 6 deletions

View File

@ -2,6 +2,7 @@
;; Simple imperative UDP server harness.
(require racket/match)
(require racket/udp)
(require "dump-bytes.rkt")
@ -21,8 +22,9 @@
(define (start-udp-service
port-number ;; Uint16
packet-classifier ;; UdpPacket -> Maybe<ClassifiedPacket>
bad-packet-handler ;; UdpPacket ServerState -> ListOf<UdpPacket> ServerState
good-packet-handler ;; ClassifiedPacket ServerState -> ListOf<UdpPacket> ServerState
bad-packet-handler ;; UdpPacket ServerState -> ListOf<ClassifiedPacket> ServerState
good-packet-handler ;; ClassifiedPacket ServerState -> ListOf<ClassifiedPacket> ServerState
packet-encoder ;; ClassifiedPacket -> UdpPacket
initial-state ;; ServerState
#:packet-size-limit
[packet-size-limit 65536])
@ -48,7 +50,8 @@
(else (good-packet-handler classified-packet old-state))))
(for-each (lambda (p)
(udp-send-to s (udp-packet-host p) (udp-packet-port p) (udp-packet-body p)))
(match-define (udp-packet body host port) (packet-encoder p))
(udp-send-to s host port body))
reply-packets)
new-state)

View File

@ -71,6 +71,7 @@
classify-dns-packet
(lambda (packet old-state) (values (log-error packet) old-state))
(lambda (message old-state) (values (handle-request soa-rr zone message) old-state))
encode-dns-packet
#f
#:packet-size-limit 512))
@ -79,6 +80,10 @@
(with-handlers ((exn? (lambda (e) #f)))
(list (packet->dns-message body) host port)))
(define (encode-dns-packet data)
(match-define (list message host port) data)
(udp-packet (dns-message->packet message) host port))
;; UdpPacket -> ListOf<UdpPacket>
(define (log-error packet)
(pretty-print `(bad-packet-received ,packet))
@ -159,9 +164,7 @@
;; TODO: check opcode and direction in request
;; TODO: think again about multiple questions in one packet
(map (lambda (q)
(udp-packet (dns-message->packet (answer-question q make-reply))
request-host
request-port))
(list (answer-question q make-reply) request-host request-port))
(dns-message-questions request-message)))
(start-server 5555