Skip to content

Commit

Permalink
Merge pull request #477 from haytok/nerdctl_issue_3539
Browse files Browse the repository at this point in the history
fix: allow to propagate the address specified in -p option
  • Loading branch information
AkihiroSuda authored Jan 9, 2025
2 parents 9c9049a + 2363620 commit f669d45
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pkg/port/builtin/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func (d *childDriver) handleConnectRequest(c *net.UnixConn, req *msg.Request) er
ip := req.IP
if ip == "" {
ip = "127.0.0.1"
if req.ParentIP != "" {
if req.ParentIP != req.HostGatewayIP && req.ParentIP != "0.0.0.0" {
ip = req.ParentIP
}
}
} else {
p := net.ParseIP(ip)
if p == nil {
Expand Down
37 changes: 29 additions & 8 deletions pkg/port/builtin/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ const (

// Request and Response are encoded as JSON with uint32le length header.
type Request struct {
Type string // "init" or "connect"
Proto string // "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"
IP string
Port int
Type string // "init" or "connect"
Proto string // "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"
IP string
Port int
ParentIP string
HostGatewayIP string
}

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

func hostGatewayIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}

for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}

return ""
}

// ConnectToChild connects to the child UNIX socket, and obtains TCP or UDP socket FD
// that corresponds to the port spec.
func ConnectToChild(c *net.UnixConn, spec port.Spec) (int, error) {
req := Request{
Type: RequestTypeConnect,
Proto: spec.Proto,
Port: spec.ChildPort,
IP: spec.ChildIP,
Type: RequestTypeConnect,
Proto: spec.Proto,
Port: spec.ChildPort,
IP: spec.ChildIP,
ParentIP: spec.ParentIP,
HostGatewayIP: hostGatewayIP(),
}
if _, err := lowlevelmsgutil.MarshalToWriter(c, &req); err != nil {
return 0, err
Expand Down

0 comments on commit f669d45

Please sign in to comment.