if c == nil {
panic("Dial returned a nil Conn")
}
- rc := reflect.ValueOf(c)
- if rc.IsNil() {
+ if rc := reflect.ValueOf(c); rc.Kind() == reflect.Pointer && rc.IsNil() {
panic(fmt.Sprintf("Dial returned a nil %T", c))
}
- fd := rc.Elem().FieldByName("fd")
- if fd.IsNil() {
- panic(fmt.Sprintf("Dial returned a %T with a nil fd", c))
- }
- if addr := fd.Elem().FieldByName("laddr"); addr.IsNil() {
- panic(fmt.Sprintf("Dial returned a %T whose fd has a nil laddr", c))
- }
addr := c.LocalAddr()
if addr == nil {
panic(fmt.Sprintf("(%T).LocalAddr returned a nil Addr", c))
}
}
// Record the local and remote addresses from the actual socket.
- // For the local address, use
- // 1) the one returned by Getsockname, if that succeeds; or
- // 2) the one passed to us as the laddr parameter; or
- // 3) nil.
+ // Get the local address by calling Getsockname.
// For the remote address, use
// 1) the one returned by the connect method, if any; or
// 2) the one from Getpeername, if it succeeds; or
// 3) the one passed to us as the raddr parameter.
- var laddrName Addr = laddr
- if lsa, err := syscall.Getsockname(fd.pfd.Sysfd); err == nil {
- laddrName = fd.addrFunc()(lsa)
- }
+ lsa, _ = syscall.Getsockname(fd.pfd.Sysfd)
if crsa != nil {
- fd.setAddr(laddrName, fd.addrFunc()(crsa))
+ fd.setAddr(fd.addrFunc()(lsa), fd.addrFunc()(crsa))
} else if rsa, _ = syscall.Getpeername(fd.pfd.Sysfd); rsa != nil {
- fd.setAddr(laddrName, fd.addrFunc()(rsa))
+ fd.setAddr(fd.addrFunc()(lsa), fd.addrFunc()(rsa))
} else {
- fd.setAddr(laddrName, raddr)
+ fd.setAddr(fd.addrFunc()(lsa), raddr)
}
return nil
}