Skip to content

Commit

Permalink
Refactor reliable socket implementation (#2250)
Browse files Browse the repository at this point in the history
Decoupled streaming component of reliable socket from message parsing.
This allows the server side of the connection to parse the messages, and
simplifies the transition to SEQPACKET sockets.
  • Loading branch information
scrye authored Dec 19, 2018
1 parent aeafedc commit 80278ea
Show file tree
Hide file tree
Showing 15 changed files with 1,258 additions and 809 deletions.
2 changes: 1 addition & 1 deletion go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ MOCK_SRC_IN =\
lib/infra/modules/trust/trustdb/trustdb.go
MOCK_SRC_OUT = $(foreach in,$(MOCK_SRC_IN),$(call mock_src_in_to_out,$(in)))
# This uses the format <stdlib package>/<interface>:
MOCK_STD_IN = net/Addr net/PacketConn
MOCK_STD_IN = net/Addr net/PacketConn net/Conn
MOCK_STD_OUT = $(foreach in,$(MOCK_STD_IN),$(call mock_std_in_to_out,$(in)))

all: deps_gen bin libs
Expand Down
9 changes: 7 additions & 2 deletions go/lib/snet/snet.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/overlay"
"github.com/scionproto/scion/go/lib/pathmgr"
"github.com/scionproto/scion/go/lib/sciond"
"github.com/scionproto/scion/go/lib/sock/reliable"
Expand Down Expand Up @@ -255,10 +256,14 @@ func (n *SCIONNetwork) ListenSCIONWithBindSVC(network string, laddr, baddr *Addr
return nil, common.NewBasicError("Unable to listen on non-local IA", nil,
"expected", conn.scionNet.localIA, "actual", conn.laddr.IA, "type", "public")
}
var bindAddr *addr.AppAddr
var bindAddr *overlay.OverlayAddr
if baddr != nil {
var err error
conn.baddr = baddr.Copy()
bindAddr = baddr.Host
bindAddr, err = overlay.NewOverlayAddr(baddr.Host.L3, baddr.Host.L4)
if err != nil {
return nil, common.NewBasicError("Unable to construct overlay bind address", err)
}
if !conn.baddr.IA.Eq(conn.scionNet.localIA) {
return nil, common.NewBasicError("Unable to listen on non-local IA", nil,
"expected", conn.scionNet.localIA, "actual", conn.baddr.IA, "type", "bind")
Expand Down
172 changes: 0 additions & 172 deletions go/lib/sock/reliable/bench_test.go

This file was deleted.

15 changes: 15 additions & 0 deletions go/lib/sock/reliable/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ import (
"github.com/scionproto/scion/go/lib/common"
)

const (
ErrNoAddress = "no address found"
ErrNoPort = "missing port"
ErrPayloadTooLong = "payload too long"
ErrIncompleteFrameHeader = "incomplete frame header"
ErrBadFrameLength = "bad frame length"
ErrBadCookie = "bad cookie"
ErrBadAddressType = "bad address type"
ErrIncompleteAddress = "incomplete IP address"
ErrIncompletePort = "incomplete UDP port"
ErrIncompleteMessage = "incomplete message"
ErrBadLength = "bad length"
ErrBufferTooSmall = "buffer too small"
)

func IsDispatcherError(err error) bool {
err = extractNestedError(err)
// On Linux, the following errors should prompt a reconnect:
Expand Down
146 changes: 146 additions & 0 deletions go/lib/sock/reliable/frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package reliable

import (
"net"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
)

// OverlayPacket contains metadata about a SCION packet going through the
// reliable socket framing protocol.
type OverlayPacket struct {
Address *net.UDPAddr
Payload []byte
}

func (p *OverlayPacket) SerializeTo(b []byte) (int, error) {
var f frame
f.Cookie = expectedCookie
f.AddressType = byte(getAddressType(p.Address))
f.Length = uint32(len(p.Payload))
if p.Address != nil {
if err := f.insertAddress(p.Address); err != nil {
return 0, err
}
}
f.Payload = p.Payload
return f.SerializeTo(b)
}

func (p *OverlayPacket) DecodeFromBytes(b []byte) error {
var f frame
if err := f.DecodeFromBytes(b); err != nil {
return err
}
if f.Cookie != expectedCookie {
return common.NewBasicError(ErrBadCookie, nil)
}
p.Address = f.extractAddress()
p.Payload = f.Payload
return nil
}

// frame describes the wire format of the reliable socket framing protocol.
type frame struct {
Cookie uint64
AddressType byte
Length uint32
Address []byte
Port []byte
Payload []byte
}

func (f *frame) SerializeTo(b []byte) (int, error) {
totalLength := f.length()
if totalLength > len(b) {
return 0, common.NewBasicError(ErrBufferTooSmall, nil, "have", len(b), "want", totalLength)
}
common.Order.PutUint64(b, f.Cookie)
b[8] = f.AddressType
common.Order.PutUint32(b[9:], uint32(f.Length))
copy(b[13:], f.Address)
copy(b[13+len(f.Address):], f.Port)
copy(b[13+len(f.Address)+len(f.Port):], f.Payload)
return totalLength, nil
}

func (f *frame) DecodeFromBytes(data []byte) error {
if len(data) < f.headerLength() {
return common.NewBasicError(ErrIncompleteFrameHeader, nil)
}
f.Cookie = common.Order.Uint64(data)
f.AddressType = data[8]
f.Length = common.Order.Uint32(data[9:])
offset := 13
addressType := addr.HostAddrType(f.AddressType)
if !isValidReliableSockDestination(addressType) {
return common.NewBasicError(ErrBadAddressType, nil, "type", addressType)
}
addrLen := getAddressLength(addressType)
portLen := getPortLength(addressType)
if len(data[offset:]) < addrLen {
return common.NewBasicError(ErrIncompleteAddress, nil)
}
f.Address = data[offset : offset+addrLen]
offset += addrLen
if len(data[offset:]) < portLen {
return common.NewBasicError(ErrIncompletePort, nil)
}
f.Port = data[offset : offset+portLen]
offset += portLen
f.Payload = data[offset:]
if len(f.Payload) != int(f.Length) {
return common.NewBasicError(ErrBadLength, nil)
}
return nil
}

// length returns the total length of the frame (including payload).
func (f *frame) length() int {
return f.headerLength() + len(f.Address) + len(f.Port) + len(f.Payload)
}

// header length returns the length of the fixed size start of the frame
// (cookie, address type and payload length field).
func (f *frame) headerLength() int {
return 8 + 1 + 4
}

func (f *frame) insertAddress(address *net.UDPAddr) error {
if address.IP == nil || address.IP.IsUnspecified() {
return common.NewBasicError(ErrNoAddress, nil)
}
if address.Port == 0 {
return common.NewBasicError(ErrNoPort, nil)
}
f.Address = []byte(normalizeIP(address.IP))
f.Port = make([]byte, 2)
common.Order.PutUint16(f.Port, uint16(address.Port))
return nil
}

func (f *frame) extractAddress() *net.UDPAddr {
t := addr.HostAddrType(f.AddressType)
if t == addr.HostTypeIPv4 || t == addr.HostTypeIPv6 {
return &net.UDPAddr{
IP: net.IP(f.Address),
Port: int(common.Order.Uint16(f.Port)),
}
}
return nil
}
Loading

0 comments on commit 80278ea

Please sign in to comment.