Class Address

java.lang.Object
com.frostwire.jlibtorrent.Address
All Implemented Interfaces:
Cloneable, Comparable<Address>

public final class Address extends Object implements Comparable<Address>, Cloneable
IP address abstraction supporting both IPv4 and IPv6 formats.

Address represents a network IP address that can be either IPv4 (32-bit) or IPv6 (128-bit). This class provides utilities for parsing, comparing, and querying IP addresses used throughout BitTorrent networking including peer discovery, DHT nodes, tracker communication, and local network interface enumeration.

Understanding IP Addresses in BitTorrent:
Addresses are fundamental to BitTorrent networking:

  • IPv4: Traditional 32-bit addresses (e.g., 192.168.1.1)
  • IPv6: Modern 128-bit addresses (e.g., 2001:db8::1)
  • Used For: Peers, DHT nodes, trackers, local network interfaces
  • Special Types: Loopback, multicast, unspecified, private ranges

Creating Addresses:

 // From IPv4 string (dotted decimal)
 Address ipv4 = new Address("192.168.1.100");
 System.out.println("IPv4: " + ipv4);  // Output: 192.168.1.100

 // From IPv6 string (hexadecimal notation)
 Address ipv6 = new Address("2001:db8::8a2e:370:7334");
 System.out.println("IPv6: " + ipv6);  // Output: 2001:db8::8a2e:370:7334

 // From localhost (works for both IPv4 and IPv6)
 Address localhost4 = new Address("127.0.0.1");
 Address localhost6 = new Address("::1");

 // Empty address (unspecified)
 Address empty = new Address();

 // Invalid addresses throw IllegalArgumentException
 try {
     Address bad = new Address("not.an.ip.address");
 } catch (IllegalArgumentException e) {
     System.err.println("Parse error: " + e.getMessage());
 }
 

Querying Address Properties:

 Address addr = new Address("192.168.1.1");

 // Check IP version
 if (addr.isV4()) {
     System.out.println("IPv4 address");
 } else if (addr.isV6()) {
     System.out.println("IPv6 address");
 }

 // Check special address types
 if (addr.isLoopback()) {
     System.out.println("Loopback (local communication only)\");
 }

 if (addr.isUnspecified()) {
     System.out.println("Unspecified (0.0.0.0 or ::)\");
 }

 if (addr.isMulticast()) {
     System.out.println("Multicast address\");
 }

 // String representation
 String ipString = addr.toString();  // "192.168.1.1"
 

Address Classification:

 // Private IPv4 ranges (RFC 1918)
 // 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
 Address private1 = new Address("10.0.0.1");
 Address private2 = new Address("172.31.255.255");
 Address private3 = new Address("192.168.1.1");

 // Link-local address
 Address linkLocal = new Address("169.254.1.1");

 // Loopback addresses
 Address loopbackV4 = new Address("127.0.0.1");
 Address loopbackV6 = new Address("::1");

 // Multicast addresses
 Address multicastV4 = new Address("224.0.0.1");  // All hosts multicast
 Address multicastV6 = new Address("ff02::1");    // All nodes multicast
 

Ordering Addresses:

 Address addr1 = new Address("10.0.0.1");
 Address addr2 = new Address("192.168.1.1");

 // Addresses implement Comparable<Address>
 int comparison = addr1.compareTo(addr2);
 if (comparison < 0) {
     System.out.println("addr1 is before addr2 in sort order\");
 }

 // Useful for sorting lists of addresses
 java.util.List<Address> addresses = new java.util.ArrayList<>();
 addresses.add(new Address("192.168.1.1\"));
 addresses.add(new Address("10.0.0.1\"));
 addresses.add(new Address("172.16.0.1\"));
 java.util.Collections.sort(addresses);
 

Using Addresses with Endpoints:

 // Create endpoint from address
 Address peerAddr = new Address("203.0.113.50\");
 TcpEndpoint peer = new TcpEndpoint(peerAddr, 6881);
 System.out.println("Connecting to: \" + peer);

 UdpEndpoint dhtNode = new UdpEndpoint(peerAddr, 6881);
 System.out.println("DHT node: \" + dhtNode);
 

Network Interface Enumeration:

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

 // Enumerate local network interfaces
 java.util.List<EnumNet.IpInterface> interfaces = EnumNet.enumInterfaces(sm);
 for (EnumNet.IpInterface iface : interfaces) {
     Address ifaceAddr = iface.interfaceAddress();
     Address netmask = iface.netmask();

     System.out.println(\"Interface \" + iface.name());
     System.out.println(\"  Address: \" + ifaceAddr);
     System.out.println(\"  IPv4: \" + ifaceAddr.isV4());
     System.out.println(\"  Netmask: \" + netmask);
 }

 // Enumerate routing table
 java.util.List<EnumNet.IpRoute> routes = EnumNet.enumRoutes(sm);
 for (EnumNet.IpRoute route : routes) {
     Address destination = route.destination();
     Address gateway = route.gateway();

     System.out.println(\"Route to \" + destination);
     System.out.println(\"  Via gateway: \" + gateway);
 }
 

Clone and Copy Semantics:

 Address original = new Address("192.168.1.1");

 // Create a copy
 Address copy = original.clone();

 // Both represent the same IP address
 System.out.println(original.toString());  // 192.168.1.1
 System.out.println(copy.toString());      // 192.168.1.1
 

Performance Considerations:

  • String parsing only happens in constructors; queries are O(1)
  • Comparison is efficient (native bit comparison)
  • IPv4 addresses require less memory than IPv6
  • Addresses are immutable; thread-safe
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Address(com.frostwire.jlibtorrent.swig.address addr)
     
    Create an address from an IPv4 address string in dotted decimal form, or from an IPv6 address in hexadecimal notation.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    int
    Compare addresses for ordering.
    boolean
    Determine whether the address is a loopback address.
    boolean
    Determine whether the address is a multicast address.
    boolean
    Determine whether the address is unspecified.
    boolean
    Get whether the address is an IP version 4 address.
    boolean
    Get whether the address is an IP version 6 address.
    com.frostwire.jlibtorrent.swig.address
     
    Get the address as a string in dotted decimal format.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Address

      public Address(com.frostwire.jlibtorrent.swig.address addr)
      Parameters:
      addr - the native object
    • Address

      public Address(String ip)
      Create an address from an IPv4 address string in dotted decimal form, or from an IPv6 address in hexadecimal notation.
      Parameters:
      ip - the ip string representation
    • Address

      public Address()
  • Method Details

    • swig

      public com.frostwire.jlibtorrent.swig.address swig()
      Returns:
      native object
    • isV4

      public boolean isV4()
      Get whether the address is an IP version 4 address.
      Returns:
      if it's an IPv4 address
    • isV6

      public boolean isV6()
      Get whether the address is an IP version 6 address.
      Returns:
      if it's an IPv6 address
    • isLoopback

      public boolean isLoopback()
      Determine whether the address is a loopback address.
      Returns:
      if it's a loopback address
    • isUnspecified

      public boolean isUnspecified()
      Determine whether the address is unspecified.
      Returns:
      if it's an unspecified address
    • isMulticast

      public boolean isMulticast()
      Determine whether the address is a multicast address.
      Returns:
      if it's an multicast address
    • compareTo

      public int compareTo(Address o)
      Compare addresses for ordering.
      Specified by:
      compareTo in interface Comparable<Address>
      Parameters:
      o - the other address
      Returns:
      -1, 0 or 1
    • toString

      public String toString()
      Get the address as a string in dotted decimal format.
      Overrides:
      toString in class Object
      Returns:
      string representation
    • clone

      public Address clone()
      Overrides:
      clone in class Object