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>
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
}
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)
+}