Skip to content

Commit

Permalink
ParsePortSpec: handle IPv6 addresses
Browse files Browse the repository at this point in the history
split out of moby/moby#20315
in order to fix moby/moby#11518

Signed-off-by: Michael Stapelberg <stapelberg@google.com>
  • Loading branch information
stapelberg committed Jul 30, 2016
1 parent 990a1a1 commit 8e1e732
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,31 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
rawPort = fmt.Sprintf(":%s", rawPort)
}

parts, err := PartParser(portSpecTemplate, rawPort)
if err != nil {
return nil, err
parts := make(map[string]string)
if rawPort != "" && rawPort[0] == '[' {
ipEnd := strings.Index(rawPort, "]")
if ipEnd == -1 {
return nil, fmt.Errorf("IP address starts with \"[\", but corresponding \"]\" was not found")
}
parts["ip"] = rawPort[1:ipEnd]
if ipEnd >= len(rawPort)-2 {
return nil, fmt.Errorf("port spec too short (expected at least 2 characters after \"]\"): %q", rawPort)
}
if rawPort[ipEnd+1] != ':' {
return nil, fmt.Errorf("\"]\" must be followed by \":\": %q", rawPort)
}
nonIPParts := strings.Split(rawPort[ipEnd+2:], ":")
if len(nonIPParts) != 2 {
return nil, fmt.Errorf("got %d parts, wanted 2: %q", len(nonIPParts), rawPort[ipEnd+2:])
}
parts["hostPort"] = nonIPParts[0]
parts["containerPort"] = nonIPParts[1]
} else {
var err error
parts, err = PartParser(portSpecTemplate, rawPort)
if err != nil {
return nil, err
}
}

var (
Expand Down

0 comments on commit 8e1e732

Please sign in to comment.