Skip to content

Commit

Permalink
add a wait-for-it script to check if a QUIC server is up
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 7, 2020
1 parent 037222b commit 97b32e4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sim/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ RUN ./waf build && \
cd out/lib && du -sh . && strip -v * && du -sh . && cd ../.. && \
cd out/scratch && rm -r subdir helper scratch-simulator*

RUN cd / && \
wget https://dl.google.com/go/go1.15.linux-amd64.tar.gz && \
tar xfz go1.15.linux-amd64.tar.gz && \
rm go1.15.linux-amd64.tar.gz

RUN ls -al /go

ENV PATH="/go/bin:${PATH}"
COPY wait-for-it-quic /wait-for-it-quic
RUN cd /wait-for-it-quic && go build .

FROM ubuntu:20.04

RUN apt-get update && \
Expand All @@ -36,6 +47,7 @@ WORKDIR /ns3
COPY --from=builder /ns3/out/src/fd-net-device/*optimized /ns3/out/src/fd-net-device/*debug /ns3/src/fd-net-device/
COPY --from=builder /ns3/out/scratch/*/* /ns3/scratch/
COPY --from=builder /ns3/out/lib/ /ns3/lib
COPY --from=builder /wait-for-it-quic/wait-for-it-quic /usr/bin

# see https://gitlab.com/nsnam/ns-3-dev/issues/97
ENV PATH="/ns3/src/fd-net-device/:${PATH}"
Expand Down
6 changes: 6 additions & 0 deletions sim/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

# We are using eth0 and eth1 as EmuFdNetDevices in ns3.
# ns3 usually uses MAC address spoofing to separate ns3 from other traffic,
# see https://www.nsnam.org/docs/models/html/fd-net-device.html#emufdnetdevicehelper.
Expand All @@ -18,6 +20,10 @@ ifconfig eth1 promisc
iptables -A FORWARD -i eth0 -o eth1 -j DROP
iptables -A FORWARD -i eth1 -o eth0 -j DROP

if [[ -n "$WAITFORSERVER" ]]; then
wait-for-it-quic -t 10s $WAITFORSERVER
fi

echo "Using scenario:" $SCENARIO

eval ./scratch/"$SCENARIO &"
Expand Down
3 changes: 3 additions & 0 deletions sim/wait-for-it-quic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/marten-seemann/quic-network-simulator/sim/wait-for-it-quic

go 1.15
70 changes: 70 additions & 0 deletions sim/wait-for-it-quic/wait-for-it.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"bytes"
"flag"
"fmt"
"log"
"net"
"time"
"os"
)

// compose a packet that will elicit a Version Negotiation packet
var packet = append([]byte{0xc0, 0x57, 0x41, 0x49, 0x54, 0, 0}, make([]byte, 1200)...)

func main() {
startTime := time.Now()
var timeout time.Duration
flag.DurationVar(&timeout, "t", 0, "timeout (e.g. 10s)")
flag.Parse()
host := flag.Arg(0)
if flag.NArg() < 1 {
flag.Usage()
return
}
if timeout == 0 {
fmt.Printf("waiting for %s without a timeout\n", host)
} else {
fmt.Printf("waiting %s for %s\n", timeout, host)
}
addr, err := net.ResolveUDPAddr("udp", host)
if err != nil {
log.Fatal(err)
}
for range time.NewTicker(time.Second/2).C {
dur := time.Since(startTime)
if timeout > 0 && dur > timeout {
fmt.Printf("Failed to connect after %s\n", dur)
os.Exit(1)
}
if err := waitForIt(addr); err == nil {
break
}
time.Sleep(time.Second)
}
fmt.Printf("%s is available after %s\n", host, time.Since(startTime))
}

func waitForIt(addr *net.UDPAddr) error {
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return err
}
if _, err := conn.Write(packet); err != nil {
return err
}
b := make([]byte, 1500)
n, err := conn.Read(b)
if err != nil {
return err
}
b = b[:n]
if len(b) < 5 {
log.Fatal("invalid packet")
}
if !bytes.Equal(b[1:5], []byte{0, 0, 0, 0}) {
log.Fatal("expected Version Negotiation packet")
}
return nil
}

0 comments on commit 97b32e4

Please sign in to comment.