1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package portutil
import (
"net"
"regexp"
"strconv"
"github.com/pkg/errors"
"github.com/rootless-containers/rootlesskit/pkg/port"
)
// ParsePortSpec parses a Docker-like representation of PortSpec.
// e.g. "127.0.0.1:8080:80/tcp"
func ParsePortSpec(s string) (*port.Spec, error) {
r := regexp.MustCompile("^([0-9a-f\\.]+):([0-9]+):([0-9]+)/([a-z]+)$")
g := r.FindStringSubmatch(s)
if len(g) != 5 {
return nil, errors.Errorf("unexpected PortSpec string: %q", s)
}
parentIP := g[1]
parentPort, err := strconv.Atoi(g[2])
if err != nil {
return nil, errors.Wrapf(err, "unexpected ParentPort in PortSpec string: %q", s)
}
childPort, err := strconv.Atoi(g[3])
if err != nil {
return nil, errors.Wrapf(err, "unexpected ChildPort in PortSpec string: %q", s)
}
proto := g[4]
// validation is up to the caller (as json.Unmarshal doesn't validate values)
return &port.Spec{
Proto: proto,
ParentIP: parentIP,
ParentPort: parentPort,
ChildPort: childPort,
}, nil
}
// ValidatePortSpec validates *port.Spec.
// existingPorts can be optionally passed for detecting conflicts.
func ValidatePortSpec(spec port.Spec, existingPorts map[int]*port.Status) error {
if spec.Proto != "tcp" && spec.Proto != "udp" {
return errors.Errorf("unknown proto: %q", spec.Proto)
}
if spec.ParentIP != "" {
if net.ParseIP(spec.ParentIP) == nil {
return errors.Errorf("invalid ParentIP: %q", spec.ParentIP)
}
}
if spec.ParentPort <= 0 || spec.ParentPort > 65535 {
return errors.Errorf("invalid ParentPort: %q", spec.ParentPort)
}
if spec.ChildPort <= 0 || spec.ChildPort > 65535 {
return errors.Errorf("invalid ChildPort: %q", spec.ChildPort)
}
for id, p := range existingPorts {
sp := p.Spec
sameProto := sp.Proto == spec.Proto
sameParent := sp.ParentIP == spec.ParentIP && sp.ParentPort == spec.ParentPort
sameChild := sp.ChildPort == spec.ChildPort
if sameProto && (sameParent || sameChild) {
return errors.Errorf("conflict with ID %d", id)
}
}
return nil
}
|