2014-02-11 19:48:44 -05:00
|
|
|
package nat
|
|
|
|
|
|
|
|
// nat is a convenience package for docker's manipulation of strings describing
|
|
|
|
// network ports.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-07 17:23:28 -04:00
|
|
|
"net"
|
2014-02-11 19:48:44 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-06-02 19:03:10 -04:00
|
|
|
|
2014-07-28 20:23:38 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2014-02-11 19:48:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-07-21 09:43:32 -04:00
|
|
|
// portSpecTemplate is the expected format for port specifications
|
|
|
|
portSpecTemplate = "ip:hostPort:containerPort"
|
2014-02-11 19:48:44 -05:00
|
|
|
)
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// PortBinding represents a binding between a Host IP address and a Host Port
|
2014-02-11 19:48:44 -05:00
|
|
|
type PortBinding struct {
|
2015-07-21 09:43:32 -04:00
|
|
|
// HostIP is the host IP Address
|
|
|
|
HostIP string `json:"HostIp"`
|
|
|
|
// HostPort is the host port number
|
2014-02-11 19:48:44 -05:00
|
|
|
HostPort string
|
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// PortMap is a collection of PortBinding indexed by Port
|
2014-02-11 19:48:44 -05:00
|
|
|
type PortMap map[Port][]PortBinding
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// PortSet is a collection of structs indexed by Port
|
2014-02-11 19:48:44 -05:00
|
|
|
type PortSet map[Port]struct{}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// Port is a string containing port number and protocol in the format "80/tcp"
|
2014-02-11 19:48:44 -05:00
|
|
|
type Port string
|
|
|
|
|
2015-05-01 15:35:26 -04:00
|
|
|
// NewPort creates a new instance of a Port given a protocol and port number or port range
|
2015-07-15 23:45:48 -04:00
|
|
|
func NewPort(proto, port string) (Port, error) {
|
|
|
|
// Check for parsing issues on "port" now so we can avoid having
|
|
|
|
// to check it later on.
|
|
|
|
|
2015-05-01 15:35:26 -04:00
|
|
|
portStartInt, portEndInt, err := ParsePortRange(port)
|
2015-07-15 23:45:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-05-01 15:35:26 -04:00
|
|
|
if portStartInt == portEndInt {
|
|
|
|
return Port(fmt.Sprintf("%d/%s", portStartInt, proto)), nil
|
|
|
|
}
|
|
|
|
return Port(fmt.Sprintf("%d-%d/%s", portStartInt, portEndInt, proto)), nil
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// ParsePort parses the port number string and returns an int
|
2014-02-11 19:48:44 -05:00
|
|
|
func ParsePort(rawPort string) (int, error) {
|
2015-04-04 00:06:48 -04:00
|
|
|
if len(rawPort) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2014-02-11 19:48:44 -05:00
|
|
|
port, err := strconv.ParseUint(rawPort, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(port), nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 15:35:26 -04:00
|
|
|
// ParsePortRange parses the port range string and returns start/end ints
|
|
|
|
func ParsePortRange(rawPort string) (int, int, error) {
|
|
|
|
if len(rawPort) == 0 {
|
|
|
|
return 0, 0, nil
|
|
|
|
}
|
|
|
|
start, end, err := parsers.ParsePortRange(rawPort)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return int(start), int(end), nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// Proto returns the protocol of a Port
|
2014-02-11 19:48:44 -05:00
|
|
|
func (p Port) Proto() string {
|
2014-09-16 21:08:30 -04:00
|
|
|
proto, _ := SplitProtoPort(string(p))
|
|
|
|
return proto
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// Port returns the port number of a Port
|
2014-02-11 19:48:44 -05:00
|
|
|
func (p Port) Port() string {
|
2014-09-16 21:08:30 -04:00
|
|
|
_, port := SplitProtoPort(string(p))
|
|
|
|
return port
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// Int returns the port number of a Port as an int
|
2014-02-11 19:48:44 -05:00
|
|
|
func (p Port) Int() int {
|
2015-07-15 23:45:48 -04:00
|
|
|
portStr := p.Port()
|
|
|
|
if len(portStr) == 0 {
|
|
|
|
return 0
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
2015-07-15 23:45:48 -04:00
|
|
|
|
|
|
|
// We don't need to check for an error because we're going to
|
|
|
|
// assume that any error would have been found, and reported, in NewPort()
|
|
|
|
port, _ := strconv.ParseUint(portStr, 10, 16)
|
|
|
|
return int(port)
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2015-05-01 15:35:26 -04:00
|
|
|
// Range returns the start/end port numbers of a Port range as ints
|
|
|
|
func (p Port) Range() (int, int, error) {
|
|
|
|
return ParsePortRange(p.Port())
|
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// SplitProtoPort splits a port in the format of proto/port
|
2014-02-11 19:48:44 -05:00
|
|
|
func SplitProtoPort(rawPort string) (string, string) {
|
|
|
|
parts := strings.Split(rawPort, "/")
|
2014-09-16 21:08:30 -04:00
|
|
|
l := len(parts)
|
|
|
|
if len(rawPort) == 0 || l == 0 || len(parts[0]) == 0 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
if l == 1 {
|
|
|
|
return "tcp", rawPort
|
|
|
|
}
|
|
|
|
if len(parts[1]) == 0 {
|
|
|
|
return "tcp", parts[0]
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
2014-09-16 21:08:30 -04:00
|
|
|
return parts[1], parts[0]
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2014-06-02 19:03:10 -04:00
|
|
|
func validateProto(proto string) bool {
|
|
|
|
for _, availableProto := range []string{"tcp", "udp"} {
|
|
|
|
if availableProto == proto {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
// ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
|
|
|
|
// these in to the internal types
|
2014-02-11 19:48:44 -05:00
|
|
|
func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
|
|
|
|
var (
|
|
|
|
exposedPorts = make(map[Port]struct{}, len(ports))
|
|
|
|
bindings = make(map[Port][]PortBinding)
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, rawPort := range ports {
|
|
|
|
proto := "tcp"
|
|
|
|
|
|
|
|
if i := strings.LastIndex(rawPort, "/"); i != -1 {
|
|
|
|
proto = rawPort[i+1:]
|
|
|
|
rawPort = rawPort[:i]
|
|
|
|
}
|
|
|
|
if !strings.Contains(rawPort, ":") {
|
|
|
|
rawPort = fmt.Sprintf("::%s", rawPort)
|
|
|
|
} else if len(strings.Split(rawPort, ":")) == 2 {
|
|
|
|
rawPort = fmt.Sprintf(":%s", rawPort)
|
|
|
|
}
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
parts, err := parsers.PartParser(portSpecTemplate, rawPort)
|
2014-02-11 19:48:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
containerPort = parts["containerPort"]
|
2015-07-21 09:43:32 -04:00
|
|
|
rawIP = parts["ip"]
|
2014-02-11 19:48:44 -05:00
|
|
|
hostPort = parts["hostPort"]
|
|
|
|
)
|
|
|
|
|
2015-07-21 09:43:32 -04:00
|
|
|
if rawIP != "" && net.ParseIP(rawIP) == nil {
|
|
|
|
return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP)
|
2014-08-07 17:23:28 -04:00
|
|
|
}
|
2014-02-11 19:48:44 -05:00
|
|
|
if containerPort == "" {
|
|
|
|
return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
|
|
|
|
}
|
2014-11-03 13:15:55 -05:00
|
|
|
|
|
|
|
startPort, endPort, err := parsers.ParsePortRange(containerPort)
|
|
|
|
if err != nil {
|
2014-02-11 19:48:44 -05:00
|
|
|
return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
|
|
|
|
}
|
2014-08-07 17:23:28 -04:00
|
|
|
|
2014-11-03 13:15:55 -05:00
|
|
|
var startHostPort, endHostPort uint64 = 0, 0
|
|
|
|
if len(hostPort) > 0 {
|
|
|
|
startHostPort, endHostPort, err = parsers.ParsePortRange(hostPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
|
|
|
|
}
|
2014-06-02 19:03:10 -04:00
|
|
|
}
|
2014-02-11 19:48:44 -05:00
|
|
|
|
2014-11-03 13:15:55 -05:00
|
|
|
if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
|
2015-05-01 15:35:26 -04:00
|
|
|
// Allow host port range iff containerPort is not a range.
|
|
|
|
// In this case, use the host port range as the dynamic
|
|
|
|
// host port range to allocate into.
|
|
|
|
if endPort != startPort {
|
|
|
|
return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
|
|
|
|
}
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2015-02-06 01:07:17 -05:00
|
|
|
if !validateProto(strings.ToLower(proto)) {
|
2014-11-03 13:15:55 -05:00
|
|
|
return nil, nil, fmt.Errorf("Invalid proto: %s", proto)
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
2014-11-03 13:15:55 -05:00
|
|
|
|
|
|
|
for i := uint64(0); i <= (endPort - startPort); i++ {
|
|
|
|
containerPort = strconv.FormatUint(startPort+i, 10)
|
|
|
|
if len(hostPort) > 0 {
|
|
|
|
hostPort = strconv.FormatUint(startHostPort+i, 10)
|
|
|
|
}
|
2015-05-01 15:35:26 -04:00
|
|
|
// Set hostPort to a range only if there is a single container port
|
|
|
|
// and a dynamic host port.
|
|
|
|
if startPort == endPort && startHostPort != endHostPort {
|
|
|
|
hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
|
|
|
|
}
|
2015-07-15 23:45:48 -04:00
|
|
|
port, err := NewPort(strings.ToLower(proto), containerPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-11-03 13:15:55 -05:00
|
|
|
if _, exists := exposedPorts[port]; !exists {
|
|
|
|
exposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
binding := PortBinding{
|
2015-07-21 09:43:32 -04:00
|
|
|
HostIP: rawIP,
|
2014-11-03 13:15:55 -05:00
|
|
|
HostPort: hostPort,
|
|
|
|
}
|
|
|
|
bslice, exists := bindings[port]
|
|
|
|
if !exists {
|
|
|
|
bslice = []PortBinding{}
|
|
|
|
}
|
|
|
|
bindings[port] = append(bslice, binding)
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return exposedPorts, bindings, nil
|
|
|
|
}
|