NewNetAddress returns a new NetAddress using the provided TCP address. When testing, other net.Addr (except TCP) will result in using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will panic. Panics if ID is invalid. TODO: socks proxies?
(id ID, addr net.Addr)
| 41 | // panic. Panics if ID is invalid. |
| 42 | // TODO: socks proxies? |
| 43 | func NewNetAddress(id ID, addr net.Addr) *NetAddress { |
| 44 | tcpAddr, ok := addr.(*net.TCPAddr) |
| 45 | if !ok { |
| 46 | if flag.Lookup("test.v") == nil { // normal run |
| 47 | panic(fmt.Sprintf("Only TCPAddrs are supported. Got: %v", addr)) |
| 48 | } else { // in testing |
| 49 | netAddr := NewNetAddressIPPort(net.IP("127.0.0.1"), 0) |
| 50 | netAddr.ID = id |
| 51 | return netAddr |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if err := validateID(id); err != nil { |
| 56 | panic(fmt.Sprintf("Invalid ID %v: %v (addr: %v)", id, err, addr)) |
| 57 | } |
| 58 | |
| 59 | ip := tcpAddr.IP |
| 60 | port := uint16(tcpAddr.Port) |
| 61 | na := NewNetAddressIPPort(ip, port) |
| 62 | na.ID = id |
| 63 | return na |
| 64 | } |
| 65 | |
| 66 | // NewNetAddressString returns a new NetAddress using the provided address in |
| 67 | // the form of "ID@IP:Port". |