net: add support for dialing from a custom local address on Plan 9

Make use of the extra parameter on "connect" control message to set the
local IP address and port. The ip(3) man page doesn't document that the
local IP address is settable, but upon inspection of the source code,
it's clearly settable.

Fixes #39747

Change-Id: Ied3d60452f20d6e5af23d1c1dcb34774af0dbd5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/240064
Run-TryBot: David du Colombier <0intro@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David du Colombier <0intro@gmail.com>
This commit is contained in:
Fazlul Shahriar 2020-06-25 19:41:13 -04:00 committed by David du Colombier
parent a980e8a690
commit 152ffca82f
1 changed files with 31 additions and 1 deletions

View File

@ -199,7 +199,11 @@ func dialPlan9Blocking(ctx context.Context, net string, laddr, raddr Addr) (fd *
if err != nil {
return nil, err
}
_, err = f.WriteString("connect " + dest)
if la := plan9LocalAddr(laddr); la == "" {
_, err = f.WriteString("connect " + dest)
} else {
_, err = f.WriteString("connect " + dest + " " + la)
}
if err != nil {
f.Close()
return nil, err
@ -303,3 +307,29 @@ func toLocal(a Addr, net string) Addr {
}
return a
}
// plan9LocalAddr returns a Plan 9 local address string.
// See setladdrport at https://9p.io/sources/plan9/sys/src/9/ip/devip.c.
func plan9LocalAddr(addr Addr) string {
ip := ""
port := 0
switch a := addr.(type) {
case *TCPAddr:
if a != nil {
ip = ipEmptyString(a.IP)
port = a.Port
}
case *UDPAddr:
if a != nil {
ip = ipEmptyString(a.IP)
port = a.Port
}
}
if ip == "" {
if port == 0 {
return ""
}
return itoa(port)
}
return ip + "!" + itoa(port)
}