Skip to content

Commit

Permalink
port: add ChildIP
Browse files Browse the repository at this point in the history
allow users to override the IP to use for the connection inside the
network namespace.

It is useful e.g. with slirp4netns to override the IP to "10.0.2.100".

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jan 20, 2021
1 parent 9fd61ce commit a8e1e82
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/port/builtin/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func (d *childDriver) handleConnectRequest(c *net.UnixConn, req *msg.Request) er
return errors.Errorf("unknown proto: %q", req.Proto)
}
var dialer net.Dialer
targetConn, err := dialer.Dial(req.Proto, fmt.Sprintf("127.0.0.1:%d", req.Port))
ip := req.IP
if ip == "" {
ip = "127.0.0.1"
}
targetConn, err := dialer.Dial(req.Proto, fmt.Sprintf("%s:%d", ip, req.Port))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/port/builtin/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
type Request struct {
Type string // "init" or "connect"
Proto string // "tcp" or "udp"
IP string
Port int
}

Expand Down Expand Up @@ -53,6 +54,7 @@ func ConnectToChild(c *net.UnixConn, spec port.Spec) (int, error) {
Type: RequestTypeConnect,
Proto: spec.Proto,
Port: spec.ChildPort,
IP: spec.ChildIP,
}
if _, err := msgutil.MarshalToWriter(c, &req); err != nil {
return 0, err
Expand Down
1 change: 1 addition & 0 deletions pkg/port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Spec struct {
ParentIP string `json:"parentIP,omitempty"` // IPv4 address. can be empty (0.0.0.0).
ParentPort int `json:"parentPort,omitempty"`
ChildPort int `json:"childPort,omitempty"`
ChildIP string `json:"childIP,omitempty"`
}

type Status struct {
Expand Down

0 comments on commit a8e1e82

Please sign in to comment.