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 (
|
|
|
|
PortSpecTemplate = "ip:hostPort:containerPort"
|
2014-09-08 21:16:02 -04:00
|
|
|
PortSpecTemplateFormat = "ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort"
|
2014-02-11 19:48:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type PortBinding struct {
|
|
|
|
HostIp string
|
|
|
|
HostPort string
|
|
|
|
}
|
|
|
|
|
|
|
|
type PortMap map[Port][]PortBinding
|
|
|
|
|
|
|
|
type PortSet map[Port]struct{}
|
|
|
|
|
|
|
|
// 80/tcp
|
|
|
|
type Port string
|
|
|
|
|
|
|
|
func NewPort(proto, port string) Port {
|
|
|
|
return Port(fmt.Sprintf("%s/%s", port, proto))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParsePort(rawPort string) (int, error) {
|
|
|
|
port, err := strconv.ParseUint(rawPort, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (p Port) Int() int {
|
2014-09-16 21:08:30 -04:00
|
|
|
port, err := ParsePort(p.Port())
|
2014-02-11 19:48:44 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-09-16 21:08:30 -04:00
|
|
|
return port
|
2014-02-11 19:48:44 -05:00
|
|
|
}
|
|
|
|
|
2014-09-11 19:32:09 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-02-11 19:48:44 -05:00
|
|
|
// We will receive port specs in the format of ip:public:private/proto and these need to be
|
|
|
|
// parsed in the internal types
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-07-28 20:23:38 -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"]
|
|
|
|
rawIp = parts["ip"]
|
|
|
|
hostPort = parts["hostPort"]
|
|
|
|
)
|
|
|
|
|
2014-08-07 17:23:28 -04:00
|
|
|
if rawIp != "" && net.ParseIP(rawIp) == nil {
|
|
|
|
return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIp)
|
|
|
|
}
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-03 13:15:55 -05:00
|
|
|
if !validateProto(proto) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
port := NewPort(proto, containerPort)
|
|
|
|
if _, exists := exposedPorts[port]; !exists {
|
|
|
|
exposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
binding := PortBinding{
|
|
|
|
HostIp: rawIp,
|
|
|
|
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
|
|
|
|
}
|