Skip to content

Commit d28d4bf

Browse files
committed
support ipv6 for port binding
Signed-off-by: fahed dorgaa <[email protected]> support ipv6 for port binding Signed-off-by: fahed dorgaa <[email protected]> support ipv6 for port binding Signed-off-by: fahed dorgaa <[email protected]> support ipv6 for port binding Signed-off-by: fahed dorgaa <[email protected]>
1 parent 68bee7b commit d28d4bf

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

pkg/portutil/portutil.go

+52-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
Copyright (C) nerdctl authors.
3-
Copyright (C) containerd authors.
2+
Copyright The containerd Authors.
43
54
Licensed under the Apache License, Version 2.0 (the "License");
65
you may not use this file except in compliance with the License.
@@ -26,6 +25,23 @@ import (
2625
"github.com/pkg/errors"
2726
)
2827

28+
func splitParts(rawport string) (string, string, string) {
29+
parts := strings.Split(rawport, ":")
30+
n := len(parts)
31+
containerport := parts[n-1]
32+
33+
switch n {
34+
case 1:
35+
return "", "", containerport
36+
case 2:
37+
return "", parts[0], containerport
38+
case 3:
39+
return parts[0], parts[1], containerport
40+
default:
41+
return strings.Join(parts[:n-2], ":"), parts[n-2], containerport
42+
}
43+
}
44+
2945
func ParseFlagP(s string) (*gocni.PortMapping, error) {
3046
proto := "tcp"
3147
splitBySlash := strings.Split(s, "/")
@@ -48,39 +64,41 @@ func ParseFlagP(s string) (*gocni.PortMapping, error) {
4864
HostIP: "0.0.0.0",
4965
}
5066

51-
splitByColon := strings.Split(splitBySlash[0], ":")
52-
switch len(splitByColon) {
53-
case 1:
54-
return nil, errors.Errorf("automatic host port assignment is not supported yet (FIXME)")
55-
case 2:
56-
i, err := strconv.Atoi(splitByColon[0])
57-
if err != nil {
58-
return nil, err
59-
}
60-
res.HostPort = int32(i)
61-
i, err = strconv.Atoi(splitByColon[1])
62-
if err != nil {
63-
return nil, err
64-
}
65-
res.ContainerPort = int32(i)
66-
return res, nil
67-
case 3:
68-
res.HostIP = splitByColon[0]
69-
if net.ParseIP(res.HostIP) == nil {
70-
return nil, errors.Errorf("invalid IP %q", res.HostIP)
71-
}
72-
i, err := strconv.Atoi(splitByColon[1])
73-
if err != nil {
74-
return nil, err
75-
}
76-
res.HostPort = int32(i)
77-
i, err = strconv.Atoi(splitByColon[2])
67+
ip, hostPort, containerPort := splitParts(splitBySlash[0])
68+
69+
if ip != "" && ip[0] == '[' {
70+
// Strip [] from IPV6 addresses
71+
rawIP, _, err := net.SplitHostPort(ip + ":")
7872
if err != nil {
79-
return nil, err
73+
return nil, errors.Errorf("Invalid ip address %v: %s", ip, err)
8074
}
81-
res.ContainerPort = int32(i)
82-
return res, nil
83-
default:
84-
return nil, errors.Errorf("failed to parse %q, unexpected colons", s)
75+
ip = rawIP
76+
}
77+
78+
if ip != "" && net.ParseIP(ip) == nil {
79+
return nil, errors.Errorf("Invalid ip address: %s", ip)
80+
}
81+
if containerPort == "" {
82+
return nil, errors.Errorf("No port specified: %s<empty>", splitBySlash[0])
83+
}
84+
85+
if hostPort == "" {
86+
return nil, errors.Errorf("automatic host port assignment is not supported yet (FIXME)")
87+
}
88+
89+
i, err := strconv.Atoi(hostPort)
90+
if err != nil {
91+
return nil, err
8592
}
93+
res.HostPort = int32(i)
94+
95+
i, err = strconv.Atoi(containerPort)
96+
if err != nil {
97+
return nil, err
98+
}
99+
res.ContainerPort = int32(i)
100+
101+
res.HostIP = ip
102+
return res, nil
103+
86104
}

0 commit comments

Comments
 (0)