Skip to content

Commit f669d45

Browse files
authored
Merge pull request #477 from haytok/nerdctl_issue_3539
fix: allow to propagate the address specified in -p option
2 parents 9c9049a + 2363620 commit f669d45

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

pkg/port/builtin/child/child.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ func (d *childDriver) handleConnectRequest(c *net.UnixConn, req *msg.Request) er
123123
ip := req.IP
124124
if ip == "" {
125125
ip = "127.0.0.1"
126+
if req.ParentIP != "" {
127+
if req.ParentIP != req.HostGatewayIP && req.ParentIP != "0.0.0.0" {
128+
ip = req.ParentIP
129+
}
130+
}
126131
} else {
127132
p := net.ParseIP(ip)
128133
if p == nil {

pkg/port/builtin/msg/msg.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ const (
1919

2020
// Request and Response are encoded as JSON with uint32le length header.
2121
type Request struct {
22-
Type string // "init" or "connect"
23-
Proto string // "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"
24-
IP string
25-
Port int
22+
Type string // "init" or "connect"
23+
Proto string // "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"
24+
IP string
25+
Port int
26+
ParentIP string
27+
HostGatewayIP string
2628
}
2729

2830
// Reply may contain FD as OOB
@@ -48,14 +50,33 @@ func Initiate(c *net.UnixConn) error {
4850
return c.CloseRead()
4951
}
5052

53+
func hostGatewayIP() string {
54+
addrs, err := net.InterfaceAddrs()
55+
if err != nil {
56+
return ""
57+
}
58+
59+
for _, addr := range addrs {
60+
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
61+
if ipnet.IP.To4() != nil {
62+
return ipnet.IP.String()
63+
}
64+
}
65+
}
66+
67+
return ""
68+
}
69+
5170
// ConnectToChild connects to the child UNIX socket, and obtains TCP or UDP socket FD
5271
// that corresponds to the port spec.
5372
func ConnectToChild(c *net.UnixConn, spec port.Spec) (int, error) {
5473
req := Request{
55-
Type: RequestTypeConnect,
56-
Proto: spec.Proto,
57-
Port: spec.ChildPort,
58-
IP: spec.ChildIP,
74+
Type: RequestTypeConnect,
75+
Proto: spec.Proto,
76+
Port: spec.ChildPort,
77+
IP: spec.ChildIP,
78+
ParentIP: spec.ParentIP,
79+
HostGatewayIP: hostGatewayIP(),
5980
}
6081
if _, err := lowlevelmsgutil.MarshalToWriter(c, &req); err != nil {
6182
return 0, err

0 commit comments

Comments
 (0)