Search in sources :

Example 71 with Inet6Address

use of java.net.Inet6Address in project i2p.i2p by i2p.

the class HTTPMUSocket method getLocalAddress.

// //////////////////////////////////////////////
// bindAddr
// //////////////////////////////////////////////
public String getLocalAddress() {
    if (ssdpMultiGroup == null || ssdpMultiIf == null)
        return "";
    InetAddress mcastAddr = ssdpMultiGroup.getAddress();
    Enumeration<InetAddress> addrs = ssdpMultiIf.getInetAddresses();
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (mcastAddr instanceof Inet6Address && addr instanceof Inet6Address)
            return addr.getHostAddress();
        if (mcastAddr instanceof Inet4Address && addr instanceof Inet4Address)
            return addr.getHostAddress();
    }
    return "";
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 72 with Inet6Address

use of java.net.Inet6Address in project i2p.i2p by i2p.

the class NTCPTransport method startListening.

/**
 *  Called by TransportManager.
 *  Caller should stop the transport first, then
 *  verify stopped with isAlive()
 *  Unfortunately TransportManager doesn't do that, so we
 *  check here to prevent two pumpers.
 */
public synchronized void startListening() {
    // try once again to prevent two pumpers which is fatal
    if (_pumper.isAlive())
        return;
    if (_log.shouldLog(Log.WARN))
        _log.warn("Starting NTCP transport listening");
    startIt();
    RouterAddress addr = configureLocalAddress();
    int port;
    if (addr != null)
        // probably not set
        port = addr.getPort();
    else
        // received by externalAddressReceived() from TransportManager
        port = _ssuPort;
    RouterAddress myAddress = bindAddress(port);
    if (myAddress != null) {
        // fixed interface, or bound to the specified host
        replaceAddress(myAddress);
    } else if (addr != null) {
        // specified host, bound to wildcard
        replaceAddress(addr);
    } else if (port > 0) {
        // all detected interfaces
        for (InetAddress ia : getSavedLocalAddresses()) {
            OrderedProperties props = new OrderedProperties();
            props.setProperty(RouterAddress.PROP_HOST, ia.getHostAddress());
            props.setProperty(RouterAddress.PROP_PORT, Integer.toString(port));
            int cost = getDefaultCost(ia instanceof Inet6Address);
            myAddress = new RouterAddress(STYLE, props, cost);
            replaceAddress(myAddress);
        }
    }
// TransportManager.startListening() calls router.rebuildRouterInfo()
}
Also used : RouterAddress(net.i2p.data.router.RouterAddress) OrderedProperties(net.i2p.util.OrderedProperties) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 73 with Inet6Address

use of java.net.Inet6Address in project udger-java by udger.

the class UdgerParser method parseIp.

/**
 * Parses the IP string and stores results of parsing in UdgerIpResult.
 *
 * @param ipString the IP string
 * @return the instance of UdgerIpResult storing results of parsing
 * @throws SQLException         the SQL exception
 * @throws UnknownHostException the unknown host exception
 */
public UdgerIpResult parseIp(String ipString) throws SQLException, UnknownHostException {
    UdgerIpResult ret = new UdgerIpResult(ipString);
    InetAddress addr = InetAddress.getByName(ipString);
    Long ipv4int = null;
    String normalizedIp = null;
    if (addr instanceof Inet4Address) {
        ipv4int = 0L;
        for (byte b : addr.getAddress()) {
            ipv4int = ipv4int << 8 | (b & 0xFF);
        }
        normalizedIp = addr.getHostAddress();
    } else if (addr instanceof Inet6Address) {
        normalizedIp = addr.getHostAddress().replaceAll("((?:(?:^|:)0+\\b){2,}):?(?!\\S*\\b\\1:0+\\b)(\\S*)", "::$2");
    }
    ret.setIpClassification("Unrecognized");
    ret.setIpClassificationCode("unrecognized");
    if (normalizedIp != null) {
        prepare();
        try (ResultSet ipRs = getFirstRow(UdgerSqlQuery.SQL_IP, normalizedIp)) {
            if (ipRs != null && ipRs.next()) {
                fetchUdgerIp(ipRs, ret);
                if (!ID_CRAWLER.equals(ret.getIpClassificationCode())) {
                    ret.setCrawlerFamilyInfoUrl("");
                }
            }
        }
        if (ipv4int != null) {
            ret.setIpVer(4);
            ResultSet dataCenterRs = getFirstRow(UdgerSqlQuery.SQL_DATACENTER, ipv4int, ipv4int);
            fetchDataCenterAndCloseRs(dataCenterRs, ret);
        } else {
            ret.setIpVer(6);
            int[] ipArray = ip6ToArray((Inet6Address) addr);
            ResultSet dataCenterRs = getFirstRow(UdgerSqlQuery.SQL_DATACENTER_RANGE6, ipArray[0], ipArray[0], ipArray[1], ipArray[1], ipArray[2], ipArray[2], ipArray[3], ipArray[3], ipArray[4], ipArray[4], ipArray[5], ipArray[5], ipArray[6], ipArray[6], ipArray[7], ipArray[7]);
            fetchDataCenterAndCloseRs(dataCenterRs, ret);
        }
    }
    return ret;
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 74 with Inet6Address

use of java.net.Inet6Address in project NetGuard by M66B.

the class ServiceSinkhole method prepareUidIPFilters.

private void prepareUidIPFilters(String dname) {
    SharedPreferences lockdown = getSharedPreferences("lockdown", Context.MODE_PRIVATE);
    lock.writeLock().lock();
    if (dname == null) {
        mapUidIPFilters.clear();
        if (!IAB.isPurchased(ActivityPro.SKU_FILTER, ServiceSinkhole.this)) {
            lock.writeLock().unlock();
            return;
        }
    }
    Cursor cursor = DatabaseHelper.getInstance(ServiceSinkhole.this).getAccessDns(dname);
    int colUid = cursor.getColumnIndex("uid");
    int colVersion = cursor.getColumnIndex("version");
    int colProtocol = cursor.getColumnIndex("protocol");
    int colDAddr = cursor.getColumnIndex("daddr");
    int colResource = cursor.getColumnIndex("resource");
    int colDPort = cursor.getColumnIndex("dport");
    int colBlock = cursor.getColumnIndex("block");
    int colTime = cursor.getColumnIndex("time");
    int colTTL = cursor.getColumnIndex("ttl");
    while (cursor.moveToNext()) {
        int uid = cursor.getInt(colUid);
        int version = cursor.getInt(colVersion);
        int protocol = cursor.getInt(colProtocol);
        String daddr = cursor.getString(colDAddr);
        String dresource = (cursor.isNull(colResource) ? null : cursor.getString(colResource));
        int dport = cursor.getInt(colDPort);
        boolean block = (cursor.getInt(colBlock) > 0);
        long time = (cursor.isNull(colTime) ? new Date().getTime() : cursor.getLong(colTime));
        long ttl = (cursor.isNull(colTTL) ? 7 * 24 * 3600 * 1000L : cursor.getLong(colTTL));
        if (isLockedDown(last_metered)) {
            String[] pkg = getPackageManager().getPackagesForUid(uid);
            if (pkg != null && pkg.length > 0) {
                if (!lockdown.getBoolean(pkg[0], false))
                    continue;
            }
        }
        IPKey key = new IPKey(version, protocol, dport, uid);
        synchronized (mapUidIPFilters) {
            if (!mapUidIPFilters.containsKey(key))
                mapUidIPFilters.put(key, new HashMap());
            try {
                String name = (dresource == null ? daddr : dresource);
                if (Util.isNumericAddress(name)) {
                    InetAddress iname = InetAddress.getByName(name);
                    if (version == 4 && !(iname instanceof Inet4Address))
                        continue;
                    if (version == 6 && !(iname instanceof Inet6Address))
                        continue;
                    // if (dname != null)
                    Log.i(TAG, "Set filter " + key + " " + daddr + "/" + dresource + "=" + block);
                    boolean exists = mapUidIPFilters.get(key).containsKey(iname);
                    if (!exists || !mapUidIPFilters.get(key).get(iname).isBlocked()) {
                        IPRule rule = new IPRule(key, name + "/" + iname, block, time + ttl);
                        mapUidIPFilters.get(key).put(iname, rule);
                        if (exists)
                            Log.w(TAG, "Address conflict " + key + " " + daddr + "/" + dresource);
                    } else if (exists) {
                        mapUidIPFilters.get(key).get(iname).updateExpires(time + ttl);
                        Log.w(TAG, "Address updated " + key + " " + daddr + "/" + dresource);
                    }
                } else
                    Log.w(TAG, "Address not numeric " + name);
            } catch (UnknownHostException ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }
        }
    }
    cursor.close();
    lock.writeLock().unlock();
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) SharedPreferences(android.content.SharedPreferences) HashMap(java.util.HashMap) Inet6Address(java.net.Inet6Address) SpannableString(android.text.SpannableString) Cursor(android.database.Cursor) Paint(android.graphics.Paint) Date(java.util.Date) InetAddress(java.net.InetAddress)

Example 75 with Inet6Address

use of java.net.Inet6Address in project lispflowmapping by opendaylight.

the class MaskUtil method normalize.

public static Eid normalize(Eid eid, short mask) {
    Address address = eid.getAddress();
    try {
        if (address instanceof Ipv4PrefixBinary) {
            byte[] addr = ((Ipv4PrefixBinary) address).getIpv4AddressBinary().getValue();
            return LispAddressUtil.asIpv4PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
        } else if (address instanceof Ipv6PrefixBinary) {
            byte[] addr = ((Ipv6PrefixBinary) address).getIpv6AddressBinary().getValue();
            return LispAddressUtil.asIpv6PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
        } else if (address instanceof Ipv4Prefix) {
            final String addr = getPrefixAddress(((Ipv4Prefix) address).getIpv4Prefix().getValue());
            InetAddress normalized = normalizeIP(InetAddresses.forString(addr), mask);
            return LispAddressUtil.asIpv4PrefixEid(eid, (Inet4Address) normalized, mask);
        } else if (address instanceof Ipv6Prefix) {
            final String addr = getPrefixAddress(((Ipv6Prefix) address).getIpv6Prefix().getValue());
            InetAddress normalized = normalizeIP(InetAddresses.forString(addr), mask);
            return LispAddressUtil.asIpv6PrefixEid(eid, (Inet6Address) normalized, mask);
        } else if (address instanceof InstanceId) {
            // TODO - not absolutely necessary, but should be implemented
            return eid;
        }
    } catch (UnknownHostException e) {
        LOG.warn("Failed to normalize EID {} with mask {}, returning original EID", eid, mask, e);
    }
    return eid;
}
Also used : Ipv4PrefixBinary(org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary) InetAddress(java.net.InetAddress) Address(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address) Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) SimpleAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress) UnknownHostException(java.net.UnknownHostException) InstanceId(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.InstanceId) Ipv6PrefixBinary(org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary) Inet6Address(java.net.Inet6Address) Ipv4Prefix(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix) InetAddress(java.net.InetAddress) Ipv6Prefix(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix)

Aggregations

Inet6Address (java.net.Inet6Address)286 InetAddress (java.net.InetAddress)196 Inet4Address (java.net.Inet4Address)110 NetworkInterface (java.net.NetworkInterface)54 UnknownHostException (java.net.UnknownHostException)51 SocketException (java.net.SocketException)32 InetSocketAddress (java.net.InetSocketAddress)31 IOException (java.io.IOException)29 LinkAddress (android.net.LinkAddress)28 Test (org.junit.Test)26 RouteInfo (android.net.RouteInfo)21 IpPrefix (android.net.IpPrefix)19 ArrayList (java.util.ArrayList)19 LinkProperties (android.net.LinkProperties)15 ByteBuffer (java.nio.ByteBuffer)9 InterfaceAddress (java.net.InterfaceAddress)7 StringJoiner (java.util.StringJoiner)7 PrintWriter (java.io.PrintWriter)6 HashMap (java.util.HashMap)6 Test (org.junit.jupiter.api.Test)6