Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #125 from zettio/123_udp_garbage
Browse files Browse the repository at this point in the history
LGTM; closes #123
  • Loading branch information
bboreham committed Oct 16, 2014
2 parents e8ec9e8 + 4ddfc47 commit 6e59006
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
13 changes: 10 additions & 3 deletions router/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,20 @@ func (nd *NonDecryptor) IterateFrames(fun FrameConsumer, packet *UDPPacket) erro
dstNameByte := buf[:NameSize]
buf = buf[NameSize:]
length := binary.BigEndian.Uint16(buf[:2])
frame := buf[2 : 2+length]
buf = buf[2+length:]
buf = buf[2:]
if len(buf) < int(length) {
return PacketDecodingError{Desc: fmt.Sprintf("too short; expected frame of length %d, got %d", length, len(buf))}
}
frame := buf[:length]
buf = buf[length:]
err := fun(nd.conn, packet.Sender, srcNameByte, dstNameByte, length, frame)
if err != nil {
return err
}
}
if len(buf) > 0 {
return PacketDecodingError{Desc: fmt.Sprintf("%d octets of trailing garbage", len(buf))}
}
return nil
}

Expand Down Expand Up @@ -279,7 +286,7 @@ func (nd *NaClDecryptor) ReceiveNonce(msg []byte) {
func (nd *NaClDecryptor) IterateFrames(fun FrameConsumer, packet *UDPPacket) error {
buf, err := nd.decrypt(packet.Packet)
if err != nil {
return err
return PacketDecodingError{Desc: fmt.Sprint("decryption failed; ", err)}
}
packet.Packet = buf
return nd.NonDecryptor.IterateFrames(fun, packet)
Expand Down
7 changes: 6 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ func (router *Router) udpReader(conn *net.UDPConn, po PacketSink) {
if !ok {
continue
}
checkWarn(relayConn.Decryptor.IterateFrames(handleUDPPacket, udpPacket))
err = relayConn.Decryptor.IterateFrames(handleUDPPacket, udpPacket)
if pde, ok := err.(PacketDecodingError); ok {
relayConn.log(pde.Error())
} else {
checkWarn(err)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions router/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type NameCollisionError struct {
Name PeerName
}

type PacketDecodingError struct {
Desc string
}

type LocalAddress struct {
ip net.IP
network *net.IPNet
Expand Down
4 changes: 4 additions & 0 deletions router/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (nce NameCollisionError) Error() string {
return fmt.Sprint("Multiple peers found with same name: ", nce.Name)
}

func (pde PacketDecodingError) Error() string {
return fmt.Sprint("Failed to decode packet: ", pde.Desc)
}

func (packet UDPPacket) String() string {
return fmt.Sprintf("UDP Packet\n name: %s\n sender: %v\n payload: % X", packet.Name, packet.Sender, packet.Packet)
}
Expand Down

0 comments on commit 6e59006

Please sign in to comment.