예제 #1
0
        // forResolve returns the most appropriate address in address for
        // a call to ResolveTCPAddr, ResolveUDPAddr, or ResolveIPAddr.
        // IPv4 is preferred, unless addr contains an IPv6 literal.
        private static Addr forResolve(this addrList addrs, @string network, @string addr)
        {
            bool want6 = default;

            switch (network)
            {
            case "ip":
                // IPv6 literal (addr does NOT contain a port)
                want6 = count(addr, ':') > 0L;
                break;

            case "tcp":
            // IPv6 literal. (addr contains a port, so look for '[')

            case "udp":
                // IPv6 literal. (addr contains a port, so look for '[')
                want6 = count(addr, '[') > 0L;
                break;
            }
            if (want6)
            {
                return(addrs.first(isNotIPv4));
            }

            return(addrs.first(isIPv4));
        }
예제 #2
0
 // first returns the first address which satisfies strategy, or if
 // none do, then the first address of any kind.
 private static Addr first(this addrList addrs, Func <Addr, bool> strategy)
 {
     foreach (var(_, addr) in addrs)
     {
         if (strategy(addr))
         {
             return(addr);
         }
     }
     return(addrs[0L]);
 }
예제 #3
0
        // partition divides an address list into two categories, using a
        // strategy function to assign a boolean label to each address.
        // The first address, and any with a matching label, are returned as
        // primaries, while addresses with the opposite label are returned
        // as fallbacks. For non-empty inputs, primaries is guaranteed to be
        // non-empty.
        private static (addrList, addrList) partition(this addrList addrs, Func <Addr, bool> strategy)
        {
            addrList primaries = default;
            addrList fallbacks = default;

            bool primaryLabel = default;

            foreach (var(i, addr) in addrs)
            {
                var label = strategy(addr);
                if (i == 0L || label == primaryLabel)
                {
                    primaryLabel = label;
                    primaries    = append(primaries, addr);
                }
                else
                {
                    fallbacks = append(fallbacks, addr);
                }
            }
            return;
        }
예제 #4
0
 // filterAddrList applies a filter to a list of IP addresses,
 // yielding a list of Addr objects. Known filters are nil, ipv4only,
 // and ipv6only. It returns every address when the filter is nil.
 // The result contains at least one address when error is nil.
 private static (addrList, error) filterAddrList(Func <IPAddr, bool> filter, slice <IPAddr> ips, Func <IPAddr, Addr> inetaddr, @string originalAddr)
 {
     addrList _p0 = default;
     error    _p0 = default !;