diff --git a/libnetwork/types/types.go b/libnetwork/types/types.go index 7395a70c67..7fe7dc29a8 100644 --- a/libnetwork/types/types.go +++ b/libnetwork/types/types.go @@ -146,51 +146,6 @@ func (p *PortBinding) String() string { return ret } -// FromString reads the PortBinding structure from string s. -// String s is a triple of "protocol/containerIP:port/hostIP:port" -// containerIP and hostIP can be in dotted decimal ("192.0.2.1") or IPv6 ("2001:db8::68") form. -// Zoned addresses ("169.254.0.23%eth0" or "fe80::1ff:fe23:4567:890a%eth0") are not supported. -// If string s is incorrectly formatted or the IP addresses or ports cannot be parsed, FromString -// returns an error. -func (p *PortBinding) FromString(s string) error { - ps := strings.Split(s, "/") - if len(ps) != 3 { - return BadRequestErrorf("invalid format for port binding: %s", s) - } - - p.Proto = ParseProtocol(ps[0]) - - var err error - if p.IP, p.Port, err = parseIPPort(ps[1]); err != nil { - return BadRequestErrorf("failed to parse Container IP/Port in port binding: %s", err.Error()) - } - - if p.HostIP, p.HostPort, err = parseIPPort(ps[2]); err != nil { - return BadRequestErrorf("failed to parse Host IP/Port in port binding: %s", err.Error()) - } - - return nil -} - -func parseIPPort(s string) (net.IP, uint16, error) { - hoststr, portstr, err := net.SplitHostPort(s) - if err != nil { - return nil, 0, err - } - - ip := net.ParseIP(hoststr) - if ip == nil { - return nil, 0, BadRequestErrorf("invalid ip: %s", hoststr) - } - - port, err := strconv.ParseUint(portstr, 10, 16) - if err != nil { - return nil, 0, BadRequestErrorf("invalid port: %s", portstr) - } - - return ip, uint16(port), nil -} - // Equal checks if this instance of PortBinding is equal to the passed one func (p *PortBinding) Equal(o *PortBinding) bool { if p == o { diff --git a/libnetwork/types/types_test.go b/libnetwork/types/types_test.go index df97c57ca4..c529ab7f86 100644 --- a/libnetwork/types/types_test.go +++ b/libnetwork/types/types_test.go @@ -3,9 +3,6 @@ package types import ( "net" "testing" - - "gotest.tools/v3/assert" - is "gotest.tools/v3/assert/cmp" ) func TestTransportPortConv(t *testing.T) { @@ -25,64 +22,6 @@ func TestTransportPortConv(t *testing.T) { } } -func TestTransportPortBindingConv(t *testing.T) { - input := []struct { - sform string - pb PortBinding - shouldFail bool - }{ - { // IPv4 -> IPv4 - sform: "tcp/172.28.30.23:80/112.0.43.56:8001", - pb: PortBinding{ - Proto: TCP, - IP: net.IPv4(172, 28, 30, 23), - Port: uint16(80), - HostIP: net.IPv4(112, 0, 43, 56), - HostPort: uint16(8001), - }, - }, - { // IPv6 -> IPv4 - sform: "tcp/[2001:db8::1]:80/112.0.43.56:8001", - pb: PortBinding{ - Proto: TCP, - IP: net.IP{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - Port: uint16(80), - HostIP: net.IPv4(112, 0, 43, 56), - HostPort: uint16(8001), - }, - }, - { // IPv4inIPv6 -> IPv4 - sform: "tcp/[::ffff:172.28.30.23]:80/112.0.43.56:8001", - pb: PortBinding{ - Proto: TCP, - IP: net.IPv4(172, 28, 30, 23), - Port: uint16(80), - HostIP: net.IPv4(112, 0, 43, 56), - HostPort: uint16(8001), - }, - }, - { // IPv4 -> IPv4 zoned - sform: "tcp/172.28.30.23:80/169.254.0.23%eth0:8001", - shouldFail: true, - }, - { // IPv4 -> IPv6 zoned - sform: "tcp/172.28.30.23:80/[fe80::1ff:fe23:4567:890a%eth0]:8001", - shouldFail: true, - }, - } - - for _, in := range input { - rc := new(PortBinding) - err := rc.FromString(in.sform) - if in.shouldFail { - assert.Assert(t, is.ErrorContains(err, ""), "Unexpected success parsing %s", in.sform) - } else { - assert.NilError(t, err) - assert.Assert(t, is.DeepEqual(in.pb, *rc), "input %s: expected %#v, got %#v", in.sform, in.pb, rc) - } - } -} - func TestErrorConstructors(t *testing.T) { var err error