mirror of https://github.com/golang/go.git
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:
parent
a980e8a690
commit
152ffca82f
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue