func (m *mapping) ExternalAddr() (ma.Multiaddr, error) { if m.ExternalPort() == 0 { // dont even try right now. return nil, ErrNoMapping } ip, err := m.nat.nat.GetExternalAddress() if err != nil { return nil, err } ipmaddr, err := manet.FromIP(ip) if err != nil { return nil, fmt.Errorf("error parsing ip") } // call m.ExternalPort again, as mapping may have changed under our feet. (tocttou) extport := m.ExternalPort() if extport == 0 { return nil, ErrNoMapping } tcp, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%d", m.Protocol(), extport)) if err != nil { return nil, err } maddr2 := ipmaddr.Encapsulate(tcp) return maddr2, nil }
func ConvertIPNet(n *net.IPNet) (string, error) { addr, err := manet.FromIP(n.IP) if err != nil { return "", err } b, _ := n.Mask.Size() return fmt.Sprintf("%s/ipcidr/%d", addr, b), nil }