java.lang.Object
org.jnetpcap.SockAddr
- Direct Known Subclasses:
SockAddr.Inet6SockAddr
,SockAddr.InetSockAddr
,SockAddr.IpxSockAddr
,SockAddr.IrdaSockAddr
,SockAddr.LinkSockAddr
,SockAddr.PacketSockAddr
A Java representation of the native socket address (sockaddr) structure and
its protocol-specific variants. Socket addresses are used to identify network
endpoints in various networking protocols.
Platform-Specific Variants
The native sockaddr structure has two main variants:BSD-style Structure
struct sockaddr {
uint8_t sa_len; // Total length of the structure
uint8_t sa_family; // Address family (AF_*)
char sa_data[14];// Protocol-specific address data
};
POSIX-style Structure
struct sockaddr {
uint16_t sa_family; // Address family (AF_*)
char sa_data[14];// Protocol-specific address data
};
Protocol Families
This class serves as the base for protocol-specific socket address structures:SockAddr.InetSockAddr
- IPv4 addresses (AF_INET)SockAddr.Inet6SockAddr
- IPv6 addresses (AF_INET6)SockAddr.PacketSockAddr
- Link-layer packet info (AF_PACKET) [Linux]SockAddr.LinkSockAddr
- Link-layer info (AF_LINK) [BSD platforms]SockAddr.IpxSockAddr
- IPX/SPX addresses (AF_IPX)SockAddr.IrdaSockAddr
- IrDA addresses (AF_IRDA) [Windows]
Memory Management
This class manages native memory through theMemorySegment
API. The lifetime of the native
memory is controlled by the provided Arena
.
Platform Detection
The class automatically detects the platform's socket address format (BSD vs POSIX) and adjusts its behavior accordingly. This detection affects how the family field is read and whether the length field is present.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Represents an IPv6 socket address (sockaddr_in6 structure).static final class
Represents an IPv4 socket address (sockaddr_in structure).static final class
The structure ofsockaddr_ipx
, used for AF_IPX sockets.static final class
The structure ofsockaddr_irda
, used with AF_IRDA sockets on windows (winsock2.h) to access link-layer information.static final class
The structure ofsockaddr_dl
, used with AF_LINK sockets on macOS to access link-layer information.static final class
The structure ofsockaddr_ll
, used with AF_PACKET sockets for raw packet access on Linux. -
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
SockAddr
(MemorySegment mseg, SockAddrFamily familyConstant, OptionalInt totalLength) Instantiates a new sock addr. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
data()
Returns the protocol-specific address data from the sa_data field.int
family()
Returns the address family identifier as defined in the sa_family field.Returns the address family as an enumerated constant.boolean
isFamily
(SockAddrFamily family) Checks if this socket address belongs to the specified address family.toString()
Creates a string representation of the socket address.Returns the total length of the socket address structure.
-
Field Details
-
saSegment
The sa segment.
-
-
Constructor Details
-
SockAddr
Instantiates a new sock addr.- Parameters:
mseg
- the msegfamilyConstant
- the family.totalLength
- the addr len.
-
-
Method Details
-
data
public byte[] data()Returns the protocol-specific address data from the sa_data field. The format and meaning of this data depends on the address family.- Returns:
- A byte array containing the raw address data
-
family
public int family()Returns the address family identifier as defined in the sa_family field. Common values include AF_INET (IPv4), AF_INET6 (IPv6), AF_PACKET (Linux raw), AF_LINK (BSD raw), etc.- Returns:
- The address family value as an integer
-
familyConstant
Returns the address family as an enumerated constant.- Returns:
- An Optional containing the SockAddrFamily constant, or empty if the family value doesn't match any known constant
- See Also:
-
isFamily
Checks if this socket address belongs to the specified address family.- Parameters:
family
- The address family to check against- Returns:
- true if this address belongs to the specified family
-
toString
-
totalLength
Returns the total length of the socket address structure. This method is primarily relevant for BSD-style socket addresses which include an explicit length field.- Returns:
- An OptionalInt containing the structure length on BSD systems, or empty on POSIX systems
-