Skip to content

Commit

Permalink
pkg/snet: format UDPAddr to comply with net.SplitHostPort (#4648)
Browse files Browse the repository at this point in the history
Change the snet.UDPAddr.String() method to return the address in a
format that is compatible with net.SplitHostPort. This will allow non
SCION aware applications to extract the host and port part without the
need to understand it is a SCION address.
  • Loading branch information
oncilla authored Nov 1, 2024
1 parent bae8d39 commit db3e6c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions pkg/snet/udpaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package snet

import (
"fmt"
"net"
"net/netip"
"regexp"
"strconv"
"strings"

"github.com/scionproto/scion/pkg/addr"
Expand Down Expand Up @@ -117,7 +117,15 @@ func (a *UDPAddr) Network() string {

// String implements net.Addr interface.
func (a *UDPAddr) String() string {
return fmt.Sprintf("%v,%s", a.IA, a.Host.String())
host, port, suffix := "<nil>", "0", ""
if a.Host != nil {
host = a.Host.IP.String()
port = strconv.Itoa(a.Host.Port)
if a.Host.Zone != "" {
suffix = "%" + a.Host.Zone
}
}
return net.JoinHostPort(a.IA.String()+","+host+suffix, port)
}

// GetPath returns a path with attached metadata.
Expand Down
10 changes: 5 additions & 5 deletions pkg/snet/udpaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,32 @@ func TestUDPAddrString(t *testing.T) {
}{
"empty": {
input: &snet.UDPAddr{},
want: "0-0,<nil>",
want: "0-0,<nil>:0",
},
"empty host": {
input: &snet.UDPAddr{Host: &net.UDPAddr{}},
want: "0-0,:0",
want: "0-0,<nil>:0",
},
"ipv4": {
input: &snet.UDPAddr{
IA: addr.MustParseIA("1-ff00:0:320"),
Host: &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 10000},
},
want: "1-ff00:0:320,1.2.3.4:10000",
want: "[1-ff00:0:320,1.2.3.4]:10000",
},
"ipv6": {
input: &snet.UDPAddr{
IA: addr.MustParseIA("1-ff00:0:320"),
Host: &net.UDPAddr{IP: net.ParseIP("2001::1"), Port: 20000},
},
want: "1-ff00:0:320,[2001::1]:20000",
want: "[1-ff00:0:320,2001::1]:20000",
},
"ipv6-zone": {
input: &snet.UDPAddr{
IA: addr.MustParseIA("1-ff00:0:320"),
Host: &net.UDPAddr{IP: net.ParseIP("2001::1"), Port: 20000, Zone: "some-zone"},
},
want: "1-ff00:0:320,[2001::1%some-zone]:20000",
want: "[1-ff00:0:320,2001::1%some-zone]:20000",
},
}
for n, tc := range tests {
Expand Down

0 comments on commit db3e6c5

Please sign in to comment.