Class EnumNet

java.lang.Object
com.frostwire.jlibtorrent.EnumNet

public final class EnumNet extends Object
Utilities for enumerating network interfaces and routing tables on the local system.

EnumNet provides static methods to discover local network configuration including active network interfaces, routing table entries, and gateway information. This is useful for binding to specific network interfaces, diagnosing network connectivity, and implementing network-aware features in BitTorrent applications.

Understanding Network Enumeration:
Network enumeration discovers the local system's network setup:

  • Network Interfaces: IP addresses, netmasks, interface names, network adapters
  • Routing Table: Routes to destinations, gateways, MTU values
  • Gateway Discovery: Default gateway for a particular interface
  • Interface Selection: Choose which IP address to advertise or bind to

Enumerating Network Interfaces:

 SessionManager sm = new SessionManager();
 sm.start();

 // Get all network interfaces on the system
 java.util.List<EnumNet.IpInterface> interfaces = EnumNet.enumInterfaces(sm);

 System.out.println("Network interfaces found: \" + interfaces.size());
 for (EnumNet.IpInterface iface : interfaces) {
     System.out.println(iface);
     // Output:
     // address: 192.168.1.100, netmask: 255.255.255.0, name: eth0,
     // friendlyName: Ethernet, description: Ethernet Adapter,
     // preferred: false
 }
 

IpInterface Properties:

 for (EnumNet.IpInterface iface : interfaces) {
     // Get the IP address
     Address ifaceAddr = iface.interfaceAddress();
     System.out.println(\"IP: \" + ifaceAddr);

     // Get the netmask (determines network range)
     Address netmask = iface.netmask();
     System.out.println(\"Netmask: \" + netmask);

     // Get interface name (eth0, wlan0, en0, etc)
     String name = iface.name();
     System.out.println(\"Interface: \" + name);

     // Get friendly name (human-readable)
     String friendlyName = iface.friendlyName();
     System.out.println(\"Friendly: \" + friendlyName);

     // Get description
     String description = iface.description();
     System.out.println(\"Description: \" + description);

     // Check if preferred (marked as default/primary)
     boolean preferred = iface.preferred();
     System.out.println(\"Preferred: \" + preferred);
 }
 

Enumerating Routing Table:

 // Get all routes known to the system
 java.util.List<EnumNet.IpRoute> routes = EnumNet.enumRoutes(sm);

 System.out.println(\"System routes: \" + routes.size());
 for (EnumNet.IpRoute route : routes) {
     System.out.println(route);
     // Output:
     // destination: 192.168.1.0, netmask: 255.255.255.0,
     // gateway: 192.168.1.1, name: eth0, mtu: 1500
 }
 

IpRoute Properties:

 for (EnumNet.IpRoute route : routes) {
     // Get destination network (e.g., 192.168.0.0)
     Address destination = route.destination();
     System.out.println(\"Destination: \" + destination);

     // Get netmask (determines network size)
     Address netmask = route.netmask();
     System.out.println(\"Netmask: \" + netmask);

     // Get gateway to reach this destination
     Address gateway = route.gateway();
     System.out.println(\"Gateway: \" + gateway);

     // Get interface name (which adapter this route uses)
     String name = route.name();
     System.out.println(\"Interface: \" + name);

     // Get MTU (Maximum Transmission Unit - max packet size)
     int mtu = route.mtu();
     System.out.println(\"MTU: \" + mtu + \" bytes\");
 }
 

Finding Default Gateway:

 SessionManager sm = new SessionManager();
 sm.start();

 java.util.List<EnumNet.IpInterface> interfaces = EnumNet.enumInterfaces(sm);
 java.util.List<EnumNet.IpRoute> routes = EnumNet.enumRoutes(sm);

 // Convert routes to native format for gateway lookup
 com.frostwire.jlibtorrent.swig.ip_route_vector route_vector =
     new com.frostwire.jlibtorrent.swig.ip_route_vector();
 for (EnumNet.IpRoute route : routes) {
     route_vector.add(route.swig());
 }

 // Find gateway for each interface
 for (EnumNet.IpInterface iface : interfaces) {
     Address gateway = EnumNet.getGateway(sm, iface, route_vector);
     System.out.println(\"Interface \" + iface.name());
     System.out.println(\"  Gateway: \" + gateway);
 }
 

Identifying Preferred Network Interface:

 // Find the preferred interface (usually the main internet connection)
 java.util.List<EnumNet.IpInterface> interfaces = EnumNet.enumInterfaces(sm);

 EnumNet.IpInterface preferred = null;
 for (EnumNet.IpInterface iface : interfaces) {
     if (iface.preferred()) {
         preferred = iface;
         break;
     }
 }

 if (preferred != null) {
     System.out.println(\"Preferred interface: \" + preferred.name());
     System.out.println(\"Address: \" + preferred.interfaceAddress());
 } else {
     System.out.println(\"No preferred interface found\");
 }
 

Network Connectivity Diagnostics:

 // Check if system has IPv4 connectivity
 java.util.List<EnumNet.IpInterface> interfaces = EnumNet.enumInterfaces(sm);
 boolean hasIPv4 = false;

 for (EnumNet.IpInterface iface : interfaces) {
     Address addr = iface.interfaceAddress();
     if (addr.isV4() && !addr.isLoopback()) {
         hasIPv4 = true;
         System.out.println(\"IPv4 via \" + iface.name() + \": \" + addr);
     }
 }

 if (!hasIPv4) {
     System.out.println(\"Warning: No external IPv4 interface detected\");
 }

 // Check for IPv6
 boolean hasIPv6 = false;
 for (EnumNet.IpInterface iface : interfaces) {
     Address addr = iface.interfaceAddress();
     if (addr.isV6() && !addr.isLoopback()) {
         hasIPv6 = true;
         System.out.println(\"IPv6 via \" + iface.name() + \": \" + addr);
     }
 }

 if (hasIPv6) {
     System.out.println(\"System supports IPv6\");
 }
 

Network Configuration for BitTorrent:

 // Find suitable interface for listening on peer port
 java.util.List<EnumNet.IpInterface> interfaces = EnumNet.enumInterfaces(sm);

 for (EnumNet.IpInterface iface : interfaces) {
     Address addr = iface.interfaceAddress();

     // Skip loopback and IPv6 link-local
     if (addr.isLoopback()) continue;

     // Good interface for accepting peer connections
     if (addr.isV4() || (!addr.isLoopback() && addr.isV6())) {
         System.out.println(\"Good peer interface: \" + iface.name());
         System.out.println(\"  Address: \" + addr);
         System.out.println(\"  Listen on port 6881\");
     }
 }
 

Important Notes:

  • SessionManager Required: Pass an active SessionManager to these methods
  • Null Check: Methods return empty lists if SessionManager is not started
  • Network Changes: Enumerate dynamically; network configuration may change
  • Platform Dependent: Interface names and properties vary by OS (Linux, macOS, Windows)
  • Special Interfaces: Loopback, VPN, docker interfaces may appear in results
See Also: