Skip to content

Commit

Permalink
add options pattern NewCookedConn
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiSubira committed May 7, 2024
1 parent 8bb2dcc commit c8e7b03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
45 changes: 36 additions & 9 deletions pkg/snet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ type Conn struct {
// send/receive SCION traffic with the usual methods.
// It takes as arguments a non-nil PacketConn and a non-nil Topology parameter.
// Nil or unspecified addresses for the PacketConn object are not supported.
// If replyPather is nil it will use a default ReplyPather.
// This is an advanced API, that allows fine-tunning of the Conn underlay functionality.
// The general methods for obtaining a Conn object are still SCIONNetwork.Listen and
// SCIONNetwork.Dial.
func NewCookedConn(
pconn PacketConn,
topo Topology,
replyPather ReplyPather,
remote *UDPAddr,
options ...ConnOption,
) (*Conn, error) {
o := apply(options)
localIA, err := topo.LocalIA(context.Background())
if err != nil {
return nil, err
Expand All @@ -77,29 +76,26 @@ func NewCookedConn(
if local.Host == nil || local.Host.IP.IsUnspecified() {
return nil, serrors.New("nil or unspecified address is not for the raw connection")
}
if replyPather == nil {
replyPather = DefaultReplyPather{}
}
start, end, err := topo.PortRange(context.Background())
if err != nil {
return nil, err
}
return &Conn{
conn: pconn,
local: local,
remote: remote,
remote: o.remote,
scionConnWriter: scionConnWriter{
conn: pconn,
buffer: make([]byte, common.SupportedMTU),
local: local,
remote: remote,
remote: o.remote,
endhostStartPort: start,
endhostEndPort: end,
},
scionConnReader: scionConnReader{
conn: pconn,
buffer: make([]byte, common.SupportedMTU),
replyPather: replyPather,
replyPather: o.replyPather,
local: local,
},
}, nil
Expand All @@ -126,3 +122,34 @@ func (c *Conn) SetDeadline(t time.Time) error {
func (c *Conn) Close() error {
return c.conn.Close()
}

type ConnOption func(o *options)

func WithReplyPather(replyPather ReplyPather) ConnOption {
return func(o *options) {
if replyPather != nil {
o.replyPather = replyPather
}
}
}

func WithRemote(addr *UDPAddr) ConnOption {
return func(o *options) {
o.remote = addr
}
}

type options struct {
replyPather ReplyPather
remote *UDPAddr
}

func apply(opts []ConnOption) options {
o := options{
replyPather: DefaultReplyPather{},
}
for _, option := range opts {
option(&o)
}
return o
}
2 changes: 1 addition & 1 deletion pkg/snet/snet.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (n *SCIONNetwork) Listen(
return nil, err
}
log.FromCtx(ctx).Debug("UDP socket openned on", "addr", packetConn.LocalAddr())
return NewCookedConn(packetConn, n.Topology, n.ReplyPather, nil)
return NewCookedConn(packetConn, n.Topology, WithReplyPather(n.ReplyPather))
}

func listenUDPRange(addr *net.UDPAddr, start, end uint16) (*net.UDPConn, error) {
Expand Down
2 changes: 1 addition & 1 deletion private/app/appnet/infraenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (nc *NetworkConfig) initQUICSockets() (net.PacketConn, net.PacketConn, erro
Message: svcResolutionReply,
},
}
server, err := snet.NewCookedConn(resolvedPacketConn, nc.Topology, nil, nil)
server, err := snet.NewCookedConn(resolvedPacketConn, nc.Topology)
if err != nil {
return nil, nil, serrors.WrapStr("creating server connection", err)
}
Expand Down

0 comments on commit c8e7b03

Please sign in to comment.