Skip to content

Commit

Permalink
pass
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiSubira committed Feb 27, 2024
1 parent 233e444 commit f72949d
Show file tree
Hide file tree
Showing 20 changed files with 158 additions and 100 deletions.
7 changes: 2 additions & 5 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type PacketConnFactory struct {
func (pcf PacketConnFactory) New() (net.PacketConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
conn, err := pcf.Network.Listen(ctx, "udp", pcf.Addr, addr.SvcNone)
conn, err := pcf.Network.Listen(ctx, "udp", pcf.Addr)
if err != nil {
return nil, serrors.WrapStr("creating packet conn", err)
}
Expand Down Expand Up @@ -431,7 +431,6 @@ func (g *Gateway) Run(ctx context.Context) error {
context.TODO(),
"udp",
&net.UDPAddr{IP: g.ControlClientIP},
addr.SvcNone,
)
if err != nil {
return serrors.WrapStr("unable to initialize client QUIC connection", err)
Expand Down Expand Up @@ -530,7 +529,6 @@ func (g *Gateway) Run(ctx context.Context) error {
context.TODO(),
"udp",
g.ControlServerAddr,
addr.SvcNone,
)
if err != nil {
return serrors.WrapStr("unable to initialize server QUIC connection", err)
Expand Down Expand Up @@ -575,7 +573,7 @@ func (g *Gateway) Run(ctx context.Context) error {
// received from the session monitors of the remote gateway.
// *********************************************************************************

probeConn, err := scionNetwork.Listen(context.TODO(), "udp", g.ProbeServerAddr, addr.SvcNone)
probeConn, err := scionNetwork.Listen(context.TODO(), "udp", g.ProbeServerAddr)
if err != nil {
return serrors.WrapStr("creating server probe conn", err)
}
Expand Down Expand Up @@ -773,7 +771,6 @@ func StartIngress(ctx context.Context, scionNetwork *snet.SCIONNetwork, dataAddr
context.TODO(),
"udp",
dataAddr,
addr.SvcNone,
)
if err != nil {
return serrors.WrapStr("creating ingress conn", err)
Expand Down
14 changes: 13 additions & 1 deletion pkg/snet/addrutil/addrutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,17 @@ func DefaultLocalIP(ctx context.Context, sdConn daemon.Connector) (net.IP, error
if err != nil {
return nil, err
}
return snet.ResolveLocal(csAddr.IP)
return ResolveLocal(csAddr.IP)
}

// ResolveLocal returns the local IP address used for traffic destined to dst.
func ResolveLocal(dst net.IP) (net.IP, error) {
udpAddr := net.UDPAddr{IP: dst, Port: 1}
udpConn, err := net.DialUDP(udpAddr.Network(), nil, &udpAddr)
if err != nil {
return nil, err
}
defer udpConn.Close()
srcIP := udpConn.LocalAddr().(*net.UDPAddr).IP
return srcIP, nil
}
9 changes: 0 additions & 9 deletions pkg/snet/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ package snet

import (
"net"

"github.com/scionproto/scion/pkg/addr"
)

type scionConnBase struct {
// Local and remote SCION addresses (IA, L3, L4)
listen *UDPAddr
remote *UDPAddr

// svc address
svc addr.SVC

// Reference to SCION networking context
scionNet *SCIONNetwork
}
Expand All @@ -40,7 +35,3 @@ func (c *scionConnBase) LocalAddr() net.Addr {
func (c *scionConnBase) RemoteAddr() net.Addr {
return c.remote
}

func (c *scionConnBase) SVC() addr.SVC {
return c.svc
}
8 changes: 2 additions & 6 deletions pkg/snet/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ package snet
import (
"context"
"net"

"github.com/scionproto/scion/pkg/addr"
)

type Network interface {
Listen(ctx context.Context, network string, listen *net.UDPAddr,
svc addr.SVC) (*Conn, error)
Dial(ctx context.Context, network string, listen *net.UDPAddr, remote *UDPAddr,
svc addr.SVC) (*Conn, error)
Listen(ctx context.Context, network string, listen *net.UDPAddr) (*Conn, error)
Dial(ctx context.Context, network string, listen *net.UDPAddr, remote *UDPAddr) (*Conn, error)
}
16 changes: 8 additions & 8 deletions pkg/snet/mock_snet/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 43 additions & 27 deletions pkg/snet/packet_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ func (c *SCIONPacketConn) SetWriteDeadline(d time.Time) error {
func (c *SCIONPacketConn) ReadFrom(pkt *Packet, ov *net.UDPAddr) error {
for {
// Read until we get an error or a data packet
if err := c.readFrom(pkt, ov); err != nil {
remoteAddr, err := c.readFrom(pkt)
if err != nil {
return err
}
*ov = *remoteAddr
if scmp, ok := pkt.Payload.(SCMPPayload); ok {
if c.SCMPHandler == nil {
metrics.CounterInc(c.Metrics.SCMPErrors)
Expand All @@ -190,32 +192,32 @@ func (c *SCIONPacketConn) SyscallConn() (syscall.RawConn, error) {
return c.Conn.SyscallConn()
}

func (c *SCIONPacketConn) readFrom(pkt *Packet, ov *net.UDPAddr) error {
func (c *SCIONPacketConn) readFrom(pkt *Packet) (*net.UDPAddr, error) {
pkt.Prepare()
n, err := c.Conn.Read(pkt.Bytes)
n, remoteAddr, err := c.Conn.ReadFrom(pkt.Bytes)
if err != nil {
metrics.CounterInc(c.Metrics.UnderlayConnectionErrors)
return serrors.WrapStr("Reliable socket read error", err)
return nil, serrors.WrapStr("Reliable socket read error", err)
}
metrics.CounterAdd(c.Metrics.ReadBytes, float64(n))
metrics.CounterInc(c.Metrics.ReadPackets)

pkt.Bytes = pkt.Bytes[:n]
if err := pkt.Decode(); err != nil {
metrics.CounterInc(c.Metrics.ParseErrors)
return serrors.WrapStr("decoding packet", err)
return nil, serrors.WrapStr("decoding packet", err)
}

// Get ingress interface internal address
lastHop, err := c.lastHop(pkt)
if err != nil {
return serrors.WrapStr("extracting last hop based on packet path", err)
}

if ov != nil {
*ov = *lastHop
udpRemoteAddr := remoteAddr.(*net.UDPAddr)
lastHop := udpRemoteAddr
if c.isShimDispatcher(udpRemoteAddr) {
// If packet comes from shim get ingress interface internal address
lastHop, err = c.lastHop(pkt)
if err != nil {
return nil, serrors.WrapStr("extracting last hop based on packet path", err)
}
}
return nil
return lastHop, nil
}

func (c *SCIONPacketConn) SetReadDeadline(d time.Time) error {
Expand All @@ -226,19 +228,18 @@ func (c *SCIONPacketConn) LocalAddr() net.Addr {
return c.Conn.LocalAddr()
}

type SerializationOptions struct {
// If ComputeChecksums is true, the checksums in sent Packets are
// recomputed. Otherwise, the checksum value is left intact.
ComputeChecksums bool
// If FixLengths is true, any lengths in sent Packets are recomputed
// to match the data contained in payloads/inner layers. This currently
// concerns extension headers and the L4 header.
FixLengths bool
// If InitializePaths is set to true, then forwarding paths are reset to
// their starting InfoField/HopField during serialization, irrespective of
// previous offsets. If it is set to false, then the fields are left
// unchanged.
InitializePaths bool
// isShimDispatcher checks that udpAddr corresponds to the address where the
// shim is/should listen on. The shim only sends forwards packets whose underlay
// IP (i.e., the address on the UDP/IP header) corresponds to the SCION Destination
// address (i.e., the address on the UDP/SCION header). Therefore, the underlay address
// for the application using SCIONPacketConn will be the same as the underlay from where
// the shim dispatcher forwards the packets.
func (c *SCIONPacketConn) isShimDispatcher(udpAddr *net.UDPAddr) bool {
localAddr := c.LocalAddr().(*net.UDPAddr)
if udpAddr.IP.Equal(localAddr.IP) && udpAddr.Port == underlay.EndhostPort {
return true
}
return false
}

func (c *SCIONPacketConn) lastHop(p *Packet) (*net.UDPAddr, error) {
Expand Down Expand Up @@ -319,3 +320,18 @@ func (c *SCIONPacketConn) lastHop(p *Packet) (*net.UDPAddr, error) {
return nil, serrors.New("Unknown type", "type", rpath.PathType.String())
}
}

type SerializationOptions struct {
// If ComputeChecksums is true, the checksums in sent Packets are
// recomputed. Otherwise, the checksum value is left intact.
ComputeChecksums bool
// If FixLengths is true, any lengths in sent Packets are recomputed
// to match the data contained in payloads/inner layers. This currently
// concerns extension headers and the L4 header.
FixLengths bool
// If InitializePaths is set to true, then forwarding paths are reset to
// their starting InfoField/HopField during serialization, irrespective of
// previous offsets. If it is set to false, then the fields are left
// unchanged.
InitializePaths bool
}
26 changes: 7 additions & 19 deletions pkg/snet/snet.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ type CPInfoProvider interface {

type Connector interface {
// OpenUDP returns a PacketConn which listens on the specified address.
// If address is nil or unspecified it listens on all available interfaces except
// for multicast IP addresses.
// Nil or unspecified addresses are not supported.
// If the address port is 0 a valid and free SCION/UDP port is automatically chosen. Otherwise,
// the specified port must be a valid SCION/UDP port.
OpenUDP(ctx context.Context, address *net.UDPAddr) (PacketConn, error)
Expand All @@ -72,6 +71,9 @@ type DefaultConnector struct {
func (d *DefaultConnector) OpenUDP(ctx context.Context, addr *net.UDPAddr) (PacketConn, error) {
var pconn *net.UDPConn
var err error
if addr == nil || addr.IP.IsUnspecified() {
return nil, serrors.New("Nil or unspecified address is not permitted")
}
start, end, err := d.CPInfoProvider.PortRange(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -158,13 +160,13 @@ type SCIONNetwork struct {
// The context is used for connection setup, it doesn't affect the returned
// connection.
func (n *SCIONNetwork) Dial(ctx context.Context, network string, listen *net.UDPAddr,
remote *UDPAddr, svc addr.SVC) (*Conn, error) {
remote *UDPAddr) (*Conn, error) {

metrics.CounterInc(n.Metrics.Dials)
if remote == nil {
return nil, serrors.New("Unable to dial to nil remote")
}
conn, err := n.Listen(ctx, network, listen, svc)
conn, err := n.Listen(ctx, network, listen)
if err != nil {
return nil, err
}
Expand All @@ -180,8 +182,7 @@ func (n *SCIONNetwork) Dial(ctx context.Context, network string, listen *net.UDP
//
// The context is used for connection setup, it doesn't affect the returned
// connection.
func (n *SCIONNetwork) Listen(ctx context.Context, network string, listen *net.UDPAddr,
svc addr.SVC) (*Conn, error) {
func (n *SCIONNetwork) Listen(ctx context.Context, network string, listen *net.UDPAddr) (*Conn, error) {

metrics.CounterInc(n.Metrics.Listens)

Expand All @@ -198,7 +199,6 @@ func (n *SCIONNetwork) Listen(ctx context.Context, network string, listen *net.U

conn := scionConnBase{
scionNet: n,
svc: svc,
listen: &UDPAddr{
IA: n.LocalIA,
Host: packetConn.LocalAddr().(*net.UDPAddr),
Expand All @@ -215,15 +215,3 @@ func (n *SCIONNetwork) Listen(ctx context.Context, network string, listen *net.U
}
return newConn(conn, packetConn, replyPather, start, end), nil
}

// ResolveLocal returns the local IP address used for traffic destined to dst.
func ResolveLocal(dst net.IP) (net.IP, error) {
udpAddr := net.UDPAddr{IP: dst, Port: 1}
udpConn, err := net.DialUDP(udpAddr.Network(), nil, &udpAddr)
if err != nil {
return nil, err
}
defer udpConn.Close()
srcIP := udpConn.LocalAddr().(*net.UDPAddr).IP
return srcIP, nil
}
14 changes: 0 additions & 14 deletions pkg/snet/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,6 @@ func (c *scionConnWriter) WriteTo(b []byte, raddr net.Addr) (int, error) {
return 0, serrors.New("invalid listen host IP", "ip", c.base.listen.Host.IP)
}

// XXX(JordiSubira): This simulates dynamic local address selection transparent to
// the application, if it decides to configure an unspecified address. For finer-grained
// control, the application should bind to a specific address only.
if listenHostIP.Unmap().IsUnspecified() {
resolvedLocal, err := ResolveLocal(nextHop.IP)
if err != nil {
return 0, err
}
listenHostIP, ok = netip.AddrFromSlice(resolvedLocal)
if !ok {
return 0, serrors.New("invalid resolved local addr", "ip", resolvedLocal)
}
}

pkt := &Packet{
Bytes: Bytes(c.buffer),
PacketInfo: PacketInfo{
Expand Down
9 changes: 5 additions & 4 deletions private/app/appnet/infraenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (nc *NetworkConfig) OpenListener(a string) (*squic.ConnListener, error) {
if err != nil {
return nil, serrors.WrapStr("parsing server QUIC address", err)
}
server, err := scionNet.Listen(context.Background(), "udp", udpAddr, addr.SvcNone)
server, err := scionNet.Listen(context.Background(), "udp", udpAddr)
if err != nil {
return nil, serrors.WrapStr("creating server connection", err)
}
Expand Down Expand Up @@ -297,11 +297,12 @@ func (nc *NetworkConfig) initSvcRedirect(quicAddress string) (func(), error) {
Handler: &svc.BaseHandler{
Message: svcResolutionReply,
},
SVC: addr.SvcWildcard,
},
Metrics: nc.SCIONNetworkMetrics,
}

conn, err := network.Listen(context.Background(), "udp", nc.Public, addr.SvcWildcard)
conn, err := network.Listen(context.Background(), "udp", nc.Public)
if err != nil {
return nil, serrors.WrapStr("listening on SCION", err, "addr", conn.LocalAddr())
}
Expand Down Expand Up @@ -351,7 +352,7 @@ func (nc *NetworkConfig) initQUICSockets() (net.PacketConn, net.PacketConn, erro
if err != nil {
return nil, nil, serrors.WrapStr("parsing server QUIC address", err)
}
server, err := serverNet.Listen(context.Background(), "udp", serverAddr, addr.SvcNone)
server, err := serverNet.Listen(context.Background(), "udp", serverAddr)
if err != nil {
return nil, nil, serrors.WrapStr("creating server connection", err)
}
Expand All @@ -375,7 +376,7 @@ func (nc *NetworkConfig) initQUICSockets() (net.PacketConn, net.PacketConn, erro
IP: serverAddr.IP,
Zone: serverAddr.Zone,
}
client, err := clientNet.Listen(context.Background(), "udp", clientAddr, addr.SvcNone)
client, err := clientNet.Listen(context.Background(), "udp", clientAddr)
if err != nil {
return nil, nil, serrors.WrapStr("creating client connection", err)
}
Expand Down
1 change: 1 addition & 0 deletions private/app/path/pathprobe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//pkg/private/serrors:go_default_library",
"//pkg/slayers/path/scion:go_default_library",
"//pkg/snet:go_default_library",
"//pkg/snet/addrutil:go_default_library",
"//pkg/snet/path:go_default_library",
"@org_golang_x_sync//errgroup:go_default_library",
],
Expand Down
Loading

0 comments on commit f72949d

Please sign in to comment.