1 ;;;; Lysiane Bouchard - Vincent St-Amour
4 ;; TODO maybe rethink the interface with the outside, and reuse some of the functions of the old udp for i/o, they can be found in ../orig
6 ;; called when an UDP datagram is received
8 (if (or (= (pkt-ref-2 udp-checksum) 0) ; valid or no checksum ?
9 (= 65535(compute-udp-checksum)))
10 (let ((port (search-port (pkt-ref-2 udp-destination-portnum) udp-ports)))
11 (if (and port (pass-app-filter? udp-source-portnum port))
12 ((conf-ref port conf-reception)
13 (u8vector-ref-field pkt ip-source-ip 4) ; length 4 u8vector
14 (u8vector->portnum (u8vector-ref-field pkt udp-source-portnum 2)) ; integer
15 (u8vector-ref-field pkt udp-data (- (pkt-ref-2 udp-length) 8))) ; u8vector
16 (icmp-unreachable icmp-port-unreachable))))) ; no app listens to this port
18 (define (compute-udp-checksum)
19 (let ((udp-len (pkt-ref-2 udp-length)))
22 ;; the UDP pseudo-header uses values located before the UDP header
23 (+ udp-header udp-len)
24 (add-16bits-1comp 17 ; UDP protocol ID, with leading zeroes up to 16 bits
28 ;; UDP outgoing packet treatment
29 ;; send an UDP datagram, takes an IP adress (u8vector of length 4), the source
30 ;; port number (integer), destination port number (integer) and data (u8vector)
31 ;; if no data should be sent, an empty vector should be give TODO when would we want no data ?
32 ;; TODO is it clean to take the src-port as parameter ?
33 ;; TODO was not tested at all
34 ;; TODO won't work, we don't know the hardware address if this was not sent in response to something else, would have to send an ARP request, or keep a cache of who sent data, linking ip addresses with mac (if we do only that, we can't initiate anything) DANGER
35 (define (udp-write dst-ip src-portnum dst-portnum data)
36 (let* ((data-length (u8vector-length data))
37 (len (+ 8 data-length)))
38 (u8vector-copy! data 0 pkt udp-data data-length)
39 (u8vector-copy! (portnum->u8vector dst-portnum) 0
40 pkt udp-destination-portnum 2)
41 (u8vector-copy! (portnum->u8vector src-portnum) 0
42 pkt udp-source-portnum 2)
43 (integer->pkt 0 udp-checksum 2)
44 (integer->pkt len udp-length 2)
45 (ip-encapsulation dst-ip udp-checksum compute-udp-checksum len)))