验证持地址是否是合法的--仅验证bech32
@param {String} addr 地址
@param {String} networkType 地址网络类型
198524
function verifyAddress(addr,networkType) {
  try {
    //验证传入的地址类型    
    assert(typeof(addr) === 'string');    
    assert(addr.length > 0);
    assert(addr.length <= 100);
    // 默认为testnet
    if(!networkType){
      networkType = "testnet";
    }
    // 使用bech32解码
    const verifiedAddr = decode(addr);    
    // 验证地址前缀
    let verifiddPrefix = false;
    for (const prefix of networks.prefix) {     
      if (prefix.type === networkType && prefix.bech32 === verifiedAddr.hrp){
        verifiddPrefix = true;
        break;
      }
    }
    if(!verifiddPrefix)
      throw new Error(`Network not found for ${verifiedAddr.hrp}.`);
  
    //验证地址构成
    let hash = verifiedAddr.hash;
    if (typeof(verifiedAddr.hash) === 'string'){
      hash = Buffer.from(verifiedAddr.hash, 'hex'); 
    }
    assert(Buffer.isBuffer(hash));
    // version === 0 && type === Address.types.WITNESS
    assert(hash.length === 20 || hash.length === 32,
      'Witness program hash is the wrong size.');       
    assert(hash.length >= 2 && hash.length <= 40, 'Hash is the wrong size.');
    
    return {
      result: true,
    }
  } catch (e) {
    return {
      result: false,
      error: {
        type: e.type || 'Error',
        message: e.message,
        code: e.code
      }
    }
  }
}
1210898
boolean registerEndpoint(String addr, Endpoint endpoint)
    {
        endpointsSync.lock();

        Endpoint inserted = null;
        try {
            inserted = endpoints.put(addr, endpoint);
        }
        finally {
            endpointsSync.unlock();
        }
        if (inserted != null) {
            return false;
        }
        return true;
    }
790209
def is_ip_addr(self, ipstr):
        
        ipstr_chunks = ipstr.split('.')
        if len(ipstr_chunks) != 4:
            return False
        for ipstr_chunk in ipstr_chunks:
            if not ipstr_chunk.isdigit():
                return False
            ipno_part = int(ipstr_chunk)
            if ipno_part < 0 or ipno_part > 255:
                return False
        return True
1116794
function bl_addr() {
    var normal = sint(0, 22, 1); // un-split address
    return function(value, labels) {
      var v = normal(value, labels);
      return ((v>>11)&0x7FF)<<16 | (v&0x7FF);
    };
  }
1151935
public static String toSocketAddressString(InetSocketAddress addr) {
        String port = String.valueOf(addr.getPort());
        final StringBuilder sb;

        if (addr.isUnresolved()) {
            String hostname = getHostname(addr);
            sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname));
        } else {
            InetAddress address = addr.getAddress();
            String hostString = toAddressString(address);
            sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address);
        }
        return sb.append(':').append(port).toString();
    }
247112
function ip6addrCompare(a, b) {
  a = _toAddr(a);
  b = _toAddr(b);

  var i;
  for (i = 0; i < 8; i++) {
    if (a._fields[i] < b._fields[i]) {
      return -1;
    } else if (a._fields[i] > b._fields[i]) {
      return 1;
    }
  }
  return 0;
}
657948
public IPRangeNode get(InetAddress addr) {

	if (version == 0) // no data was added
	    return null;

	IPRangeNode node = isV4(addr) ? ipv4 : ipv6;

	if (!this.isSorted) this.optimize();

	return node.findFast(addr);
    }
1179857
function pubKeyHashToAddress (pubKeyHash, network, type) {
  pubKeyHash = ensureBuffer(pubKeyHash)
  const prefixHash = Buffer.concat([Buffer.from(networks[network][type], 'hex'), pubKeyHash])
  const checksum = Buffer.from(sha256(sha256(prefixHash)).slice(0, 8), 'hex')
  const addr = base58.encode(Buffer.concat([prefixHash, checksum]).slice(0, 25))
  return addr
}
1211199
public static NbtAddress getByName( String host,
                                        int type,
                                        String scope,
                                        InetAddress svr )
                                        throws UnknownHostException {

        if( host == null || host.length() == 0 ) {
            return getLocalHost();
        }
        if( !Character.isDigit( host.charAt(0) )) {
            return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
        } else {
            int IP = 0x00;
            int hitDots = 0;
            char[] data = host.toCharArray();

            for( int i = 0; i < data.length; i++ ) {
                char c = data[i];
                if( c < 48 || c > 57 ) {
                    return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
                }
                int b = 0x00;
                while( c != '.' ) {
                    if( c < 48 || c > 57 ) {
                        return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
                    }
                    b = b * 10 + c - '0';

                    if( ++i >= data.length )
                        break;

                    c = data[i];
                }
                if( b > 0xFF ) {
                    return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
                }
                IP = ( IP << 8 ) + b;
                hitDots++;
            }
            if( hitDots != 4 || host.endsWith( "." )) {
                return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
            }
            return new NbtAddress( UNKNOWN_NAME, IP, false, B_NODE );
        }
    }
1020476
public static Transport UDPInstance(SocketAddress addr) throws IOException {
        DatagramSocket sock = new DatagramSocket();
        sock.setSoTimeout(getTimeout());
        sock.connect(addr);

        StreamTransport trans = new StreamTransport();
        trans.setStreams(new UDPInputStream(sock), new UDPOutputStream(sock));
        trans.setAddressLabel(addr.toString());
        return trans;
    }
931864
public static boolean isLocalAddress(String addr)
	{
		try
		{
			InetAddress iAddr = InetAddress.getByName(addr);
			return isLocalAddress(iAddr);
		}
		catch (UnknownHostException e)
		{
			return false;
		}
	}
69337
public boolean setAddress(final String addr)
    {
        //  Convert the textual address into address structure.
        address = new TcpAddress(addr, options.ipv6);
        endpoint = address.toString();

        //  Create a listening socket.
        try {
            if (options.selectorChooser == null) {
                fd = ServerSocketChannel.open();
            }
            else {
                fd = options.selectorChooser.choose(address, options).openServerSocketChannel();
            }

            // On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
            // Switch it on in such cases.
            // The method enableIpv4Mapping is empty. Still to be written
            if (address.family() == StandardProtocolFamily.INET6) {
                TcpUtils.enableIpv4Mapping(fd);
            }

            TcpUtils.unblockSocket(fd);

            //  Set the socket buffer limits for the underlying socket.
            if (options.sndbuf != 0) {
                TcpUtils.setTcpSendBuffer(fd, options.sndbuf);
            }
            if (options.rcvbuf != 0) {
                TcpUtils.setTcpReceiveBuffer(fd, options.rcvbuf);
            }

            if (!isWindows) {
                TcpUtils.setReuseAddress(fd, true);
            }

            //  Bind the socket to the network interface and port.
            // NB: fd.socket().bind(...) for Android environments
            fd.socket().bind(address.address(), options.backlog);
            // find the address in case of wildcard
            endpoint = getAddress();
        }
        catch (IOException e) {
            close();
            errno.set(ZError.EADDRINUSE);
            return false;
        }
        socket.eventListening(endpoint, fd);
        return true;
    }
94712
def register_on_network_adapter_changed(self, callback):
        
        event_type = library.VBoxEventType.on_network_adapter_changed
        return self.event_source.register_callback(callback, event_type)
1326745
def __ip_addr(addr, address_family=socket.AF_INET):
    '''
    
    '''
    mask_max = '32'
    if address_family == socket.AF_INET6:
        mask_max = '128'

    try:
        if '/' not in addr:
            addr = '{addr}/{mask_max}'.format(addr=addr, mask_max=mask_max)
    except TypeError:
        return False

    ip, mask = addr.rsplit('/', 1)

    # Verify that IP address is valid
    try:
        socket.inet_pton(address_family, ip)
    except socket.error:
        return False

    # Verify that mask is valid
    try:
        mask = int(mask)
    except ValueError:
        return False
    else:
        if not 1 <= mask <= int(mask_max):
            return False

    return True
1347473
private static boolean isIPv4MappedAddress(final byte[] addr) {
    if (addr.length < INADDR16SZ) {
      return false;
    }
    return addr[0] == 0x00 && addr[1] == 0x00 && addr[2] == 0x00 && addr[3] == 0x00
        && addr[4] == 0x00 && addr[5] == 0x00 && addr[6] == 0x00 && addr[7] == 0x00
        && addr[8] == 0x00 && addr[9] == 0x00 && addr[10] == (byte) 0xff && addr[11] == (byte) 0xff;
  }
638018
def unpack_ip_addr(addr):
    """"""
    if isinstance(addr, bytearray):
        addr = bytes(addr)
    return (socket.inet_ntoa(addr[0:4]), struct.unpack('!H', addr[4:6])[0])
239387
public static Transport TCPInstance(SocketAddress addr) throws IOException {
        Socket sock = new Socket();
        sock.setSoTimeout(getTimeout());
        sock.connect(addr);

        StreamTransport trans = new StreamTransport();
        trans.setStreams(sock.getInputStream(), sock.getOutputStream());
        trans.setAddressLabel(addr.toString());
        return trans;
    }