From fd28921bdd81ae5f98aa46d44e62d16dbaf2534b Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Thu, 21 Apr 2016 09:21:21 +0200 Subject: [PATCH] test version negotiation packet --- server.go | 23 ++++++++++++----------- server_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 server_test.go diff --git a/server.go b/server.go index 3eb816c9..bc296632 100644 --- a/server.go +++ b/server.go @@ -68,11 +68,13 @@ func (s *Server) ListenAndServe(address string) error { continue } - fmt.Printf("<- Got packet %d (%d bytes) from %v\n", publicHeader.PacketNumber, n, remoteAddr) + // fmt.Printf("<- Got packet %d (%d bytes) from %v\n", publicHeader.PacketNumber, n, remoteAddr) // Send Version Negotiation Packet if the client is speaking a different protocol version if publicHeader.VersionFlag && !protocol.IsSupportedVersion(publicHeader.VersionNumber) { - if err := sendVersionNegotiation(conn, remoteAddr, publicHeader); err != nil { + fmt.Println("Sending VersionNegotiationPacket") + _, err = conn.WriteToUDP(composeVersionNegotiation(publicHeader.ConnectionID), remoteAddr) + if err != nil { fmt.Printf("Error sending version negotiation: %s", err.Error()) } continue @@ -91,18 +93,17 @@ func (s *Server) ListenAndServe(address string) error { } } -func sendVersionNegotiation(conn *net.UDPConn, remoteAddr *net.UDPAddr, publicHeader *PublicHeader) error { - fmt.Println("Sending VersionNegotiationPacket") +func composeVersionNegotiation(connectionID protocol.ConnectionID) []byte { fullReply := &bytes.Buffer{} - responsePublicHeader := PublicHeader{ConnectionID: publicHeader.ConnectionID, PacketNumber: 1, VersionFlag: true} + responsePublicHeader := PublicHeader{ + ConnectionID: connectionID, + PacketNumber: 1, + VersionFlag: true, + } err := responsePublicHeader.WritePublicHeader(fullReply) if err != nil { - return err + panic(err) // Should not happen ;) } fullReply.Write(protocol.SupportedVersionsAsTags) - _, err = conn.WriteToUDP(fullReply.Bytes(), remoteAddr) - if err != nil { - return err - } - return nil + return fullReply.Bytes() } diff --git a/server_test.go b/server_test.go new file mode 100644 index 00000000..c65a5f06 --- /dev/null +++ b/server_test.go @@ -0,0 +1,17 @@ +package quic + +import ( + "github.com/lucas-clemente/quic-go/protocol" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Server", func() { + It("composes version negotiation packets", func() { + expected := append( + []byte{0x2d, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0}, + protocol.SupportedVersionsAsTags..., + ) + Expect(composeVersionNegotiation(1)).To(Equal(expected)) + }) +})