package com.seewo.remote.g;

import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkUtils;
import android.net.RouteInfo;
import android.util.Log;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: compiled from: EthernetHelper.java */
/* JADX INFO: loaded from: classes.dex */
public class j {
    private static Inet4Address a;

    /* JADX INFO: compiled from: EthernetHelper.java */
    public static class a {
        private String a = "0.0.0.0";
        private String b = "0.0.0.0";

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private String f274c = "0.0.0.0";

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private String f275d = "0.0.0.0";

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private String f276e = "0.0.0.0";

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private boolean f277f = true;

        public String a() {
            return this.f275d;
        }

        public String b() {
            return this.f276e;
        }

        public String c() {
            return this.a;
        }

        public String d() {
            return this.b;
        }

        public String e() {
            return this.f274c;
        }

        public boolean f() {
            return this.f277f;
        }

        public void g(boolean z) {
            this.f277f = z;
        }

        public void h(String str) {
            this.f275d = str;
        }

        public void i(String str) {
            this.f276e = str;
        }

        public void j(String str) {
            this.a = str;
        }

        public void k(String str) {
            this.b = str;
        }

        public void l(String str) {
            this.f274c = str;
        }

        public String toString() {
            return "IpMsg{mIpAddress='" + this.a + "', mNetMask='" + this.b + "', mRouteAddr='" + this.f274c + "', mDns1Addr='" + this.f275d + "', mDns2Addr='" + this.f276e + "', mIsDHCP=" + this.f277f + '}';
        }
    }

    private static Inet4Address a(LinkProperties linkProperties) {
        List<Inet4Address> listC = c(linkProperties);
        return listC.size() > 0 ? listC.get(0) : a;
    }

    private static Inet4Address b(LinkProperties linkProperties) {
        List<Inet4Address> listC = c(linkProperties);
        return listC.size() > 1 ? listC.get(1) : a;
    }

    private static List<Inet4Address> c(LinkProperties linkProperties) {
        ArrayList arrayList = new ArrayList();
        List<InetAddress> dnsServers = linkProperties.getDnsServers();
        Log.d("EthernetHelper", "getDnsV4 dnsServerssize " + dnsServers.size());
        for (InetAddress inetAddress : dnsServers) {
            Log.d("EthernetHelper", "getDnsV4List: dnsServer=" + inetAddress);
            if (inetAddress instanceof Inet4Address) {
                arrayList.add((Inet4Address) inetAddress);
            }
        }
        return arrayList;
    }

    private static Inet4Address d(LinkProperties linkProperties) {
        for (Inet4Address inet4Address : e(linkProperties)) {
            Log.d("EthernetHelper", "getGateWayV4: address=" + inet4Address);
            if (!inet4Address.isAnyLocalAddress() && !inet4Address.isLoopbackAddress()) {
                Log.d("EthernetHelper", "getGateWayV4: found address=" + inet4Address);
                return inet4Address;
            }
        }
        return a;
    }

    private static List<Inet4Address> e(LinkProperties linkProperties) {
        ArrayList arrayList = new ArrayList();
        Iterator it = ((List) com.seewo.libsvendors.utils.d.c.l(linkProperties).b("getAllRoutes").g()).iterator();
        while (it.hasNext()) {
            InetAddress gateway = ((RouteInfo) it.next()).getGateway();
            if (gateway instanceof Inet4Address) {
                arrayList.add((Inet4Address) gateway);
            }
        }
        return arrayList;
    }

    public static a f() {
        try {
            a = (Inet4Address) InetAddress.getByName("0.0.0.0");
        } catch (UnknownHostException e2) {
            Log.e("EthernetHelper", "init: EMPTY_ADDRESS", e2);
        }
        LinkProperties linkPropertiesI = i();
        a aVar = new a();
        if (linkPropertiesI != null) {
            aVar.j(g(linkPropertiesI).getHostAddress());
            aVar.k(NetworkUtils.intToInetAddress(k(j(linkPropertiesI))).getHostAddress());
            aVar.l(d(linkPropertiesI).getHostAddress());
            aVar.h(a(linkPropertiesI).getHostAddress());
            aVar.i(b(linkPropertiesI).getHostAddress());
        } else {
            Log.e("EthernetHelper", "AndroidEthernetDevInfo: mLinkProperties is null.");
        }
        return aVar;
    }

    private static Inet4Address g(LinkProperties linkProperties) {
        List<Inet4Address> listH = h(linkProperties);
        return listH.size() > 0 ? listH.get(0) : a;
    }

    private static List<Inet4Address> h(LinkProperties linkProperties) {
        ArrayList arrayList = new ArrayList();
        Iterator<LinkAddress> it = linkProperties.getLinkAddresses().iterator();
        while (it.hasNext()) {
            InetAddress address = it.next().getAddress();
            if (address instanceof Inet4Address) {
                arrayList.add((Inet4Address) address);
            }
        }
        return arrayList;
    }

    private static LinkProperties i() {
        ConnectivityManager connectivityManager = (ConnectivityManager) com.seewo.remote.d.b.b().a().getSystemService("connectivity");
        Network network = (Network) com.seewo.libsvendors.utils.d.c.l(connectivityManager).b("getActiveNetwork").g();
        if (network != null) {
            return connectivityManager.getLinkProperties(network);
        }
        return null;
    }

    private static int j(LinkProperties linkProperties) {
        for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) {
            if (linkAddress.getAddress() instanceof Inet4Address) {
                return linkAddress.getPrefixLength();
            }
        }
        return 0;
    }

    private static int k(int i2) throws IllegalArgumentException {
        if (i2 < 0 || i2 > 32) {
            throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
        }
        return Integer.reverseBytes((-1) << (32 - i2));
    }
}
