java.lang.Object
org.jnetpcap.PcapIf
A Java representation of the native
pcap_if_t
structure which
contains information about a network interface. This class provides access to
interface properties such as name, addresses, flags, and hardware
information.
Structure Members
The native pcap_if_t structure contains the following members:next
- Points to the next interface in the list, or NULL if this is the last interfacename
- A string containing the name of the interface (e.g., "eth0", "en0")description
- A human-readable string describing the interface, may be NULLaddresses
- A pointer to the first element in a linked list of addresses for the interfaceflags
- Interface flags indicating various interface properties and states
Interface Flags
Theflags
field can include the following
values:
PCAP_IF_LOOPBACK
- Set if the interface is a loopback interfacePCAP_IF_UP
- Set if the interface is up (active)PCAP_IF_RUNNING
- Set if the interface is runningPCAP_IF_WIRELESS
- Set if the interface is wireless (includes IrDA, IEEE 802.15.4, IEEE 802.11)
Connection Status
The flags field also includes connection status information throughPCAP_IF_CONNECTION_STATUS
:
PCAP_IF_CONNECTION_STATUS_UNKNOWN
- Connection status is unknownPCAP_IF_CONNECTION_STATUS_CONNECTED
- Interface is connectedPCAP_IF_CONNECTION_STATUS_DISCONNECTED
- Interface is disconnectedPCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
- Connection status doesn't apply (e.g., loopback)
Network Addresses
The interface may have multiple network addresses, accessible through theaddresses()
method. Each address is
represented by a PcapIf.PcapAddr
object which may contain:
- Network address (IPv4, IPv6, or other)
- Netmask (if applicable)
- Broadcast address (if the interface supports broadcasts)
- Destination address (for point-to-point interfaces)
Address Types
The addresses can be of different types, determined by thesa_family
field:
AF_INET
- IPv4 addresses (struct sockaddr_in)AF_INET6
- IPv6 addresses (struct sockaddr_in6)- Other address types may be present depending on the platform
Usage Example
PcapIf iface = ...;
// Get interface name
String name = iface.name();
// Check if interface is up and running
Set<PcapIfFlag> flags = iface.flagsAsEnumSet();
boolean isUp = flags.contains(PcapIfFlag.UP);
// Get IPv4 address if available
Optional<PcapAddr<InetSockAddr>> ipv4Addr = iface.findAddressOfType(InetSockAddr.class);
- Since:
- libpcap 0.7
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
PcapIf.PcapAddr<T extends SockAddr>
The struct pcap_addr structure containing network interfaces/devices addresses. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
interface is loopback.static final int
interface is running.static final int
interface is up.static final String
System property if set to true, pcap uses BSD style sockaddr structure which has the addr_len field. -
Method Summary
Modifier and TypeMethodDescriptionList
<PcapIf.PcapAddr<?>> Returns all network addresses associated with this interface.Returns the human-readable description of this interface.Optional
<PcapIf.PcapAddr<? extends SockAddr>> findAddressOfFamily
(SockAddrFamily family) Searches for a network address of a specific address family (e.g., AF_INET, AF_INET6).<T extends SockAddr>
Optional<PcapIf.PcapAddr<T>> findAddressOfType
(Class<T> familyClassType) Searches for the first network address of a specific type.int
flags()
Returns the interface flags as a raw integer value.Returns the interface flags as an EnumSet of PcapIfFlag values.Optional
<byte[]> Returns the hardware (MAC) address of this interface.name()
Returns the name of this interface.toString()
Returns a string representation of this interface, including its name, flags, description (if available), and addresses (if any).
-
Field Details
-
SYSTEM_PROPERTY_PCAPIF_SOCKADDR_BSD_STYLE
System property if set to true, pcap uses BSD style sockaddr structure which has the addr_len field. Otherwise the default heuristic are used to determine the sock address structure format.- See Also:
-
PCAP_IF_LOOPBACK
public static final int PCAP_IF_LOOPBACKinterface is loopback.- See Also:
-
PCAP_IF_UP
public static final int PCAP_IF_UPinterface is up.- See Also:
-
PCAP_IF_RUNNING
public static final int PCAP_IF_RUNNINGinterface is running.- See Also:
-
-
Method Details
-
addresses
Returns all network addresses associated with this interface. The returned list may contain IPv4, IPv6, and other types of addresses. Each PcapAddr object contains the network address and optional netmask, broadcast, and destination addresses.- Returns:
- An unmodifiable list of all addresses associated with this interface
-
findAddressOfFamily
Searches for a network address of a specific address family (e.g., AF_INET, AF_INET6).- Parameters:
family
- The address family to search for, as defined in SockAddrFamily- Returns:
- An Optional containing the first matching address, or empty if no address of the specified family exists on this interface
- See Also:
-
findAddressOfType
public <T extends SockAddr> Optional<PcapIf.PcapAddr<T>> findAddressOfType(Class<T> familyClassType) Searches for the first network address of a specific type. This method allows finding addresses of a particular class, such as IPv4 (InetSockAddr) or IPv6 (Inet6SockAddr) addresses.- Type Parameters:
T
- The type of socket address to find- Parameters:
familyClassType
- The Class object representing the desired address type- Returns:
- An Optional containing the first matching address, or empty if no address of the specified type exists on this interface
-
description
Returns the human-readable description of this interface. The description is typically more detailed than the interface name and may include information about the interface type, manufacturer, or other details.- Returns:
- An Optional containing the interface description, or empty if no description is available
-
flags
public int flags()Returns the interface flags as a raw integer value. The returned value is a bitmask containing various interface state flags such as PCAP_IF_LOOPBACK, PCAP_IF_UP, PCAP_IF_RUNNING, etc.- Returns:
- The raw interface flags as an integer bitmask
- See Also:
-
flagsAsEnumSet
Returns the interface flags as an EnumSet of PcapIfFlag values. This method provides a more type-safe way to check interface flags compared to using raw integer values.- Returns:
- An EnumSet containing the active flags for this interface
- See Also:
-
hardwareAddress
Returns the hardware (MAC) address of this interface. This method attempts to retrieve the hardware address using the Java NetworkInterface API, which may require appropriate system permissions.- Returns:
- An Optional containing the hardware address as a byte array, or empty if the address is not available or accessible
-
name
Returns the name of this interface. The interface name is system-dependent (e.g., "eth0" on Linux, "en0" on macOS, "\\Device\\NPF_{GUID}" on Windows) and can be used with pcap_open_live() to open this interface for packet capture.- Returns:
- The system-dependent name of this interface
-
toString
-