public class Packet extends Object
An onion metaphor is used to describe the layers of a packet. The layer index corresponds to the depth inside the packet. For example, if the following packet is constructed:
Packet p = new Packet(eth, ip, udp, dhcp)The layers are of the form:
innermostId()
would return
ProtocolId.DHCP
.
Constructor and Description |
---|
Packet(List<Protocol> layers)
Constructor creates a packet of protocol layers.
|
Packet(Protocol... layers)
Constructor creates a packet of protocol layers.
|
Modifier and Type | Method and Description |
---|---|
static long |
computeProtocolMask(ProtocolId... protocols)
Computes the mask of all protocol IDs.
|
Protocol |
get(int i)
Returns the protocol layer at the given index.
|
<P extends Protocol> |
get(ProtocolId id)
Returns the first occurrence of the protocol (outer to inner) with the
given protocol ID.
|
<P extends Protocol> |
get(ProtocolId id,
int nth)
Returns the Nth occurrence of the protocol (outer to inner) with the
given protocol ID.
|
boolean |
has(ProtocolId id)
Returns true if the protocols corresponding to the given ID is
contained in the packet.
|
boolean |
hasAll(ProtocolId... ids)
Returns true if all the protocols corresponding to the IDs
are contained in the packet.
|
boolean |
hasAny(ProtocolId... ids)
Returns true if any of the protocols corresponding to the IDs
are contained in the packet.
|
Protocol |
innermost()
Returns the innermost layer protocol (highest in the network stack).
|
Protocol |
innermost(ProtocolId id)
Returns the innermost layer protocol (highest in the network stack) that
matches the provided protocol ID.
|
ProtocolId |
innermostId()
Returns the innermost layer protocol ID (highest in the the network
stack).
|
List<ProtocolId> |
protocolIds()
Returns a list of protocol IDs from the outermost to innermost layer.
|
long |
protocolMask()
Returns the bit mask representing present protocol IDs.
|
int |
size()
Returns the number of protocol layers.
|
String |
toDebugString()
Returns the formatted debug string for the packet.
|
String |
toString() |
public Packet(Protocol... layers)
layers
- protocol layerspublic static long computeProtocolMask(ProtocolId... protocols)
protocols
- collection of protocolspublic int size()
public Protocol get(int i)
i
- layer indexpublic <P extends Protocol> P get(ProtocolId id)
id
- protocol IDpublic <P extends Protocol> P get(ProtocolId id, int nth)
get(ETHERNET, 0) // returns layers[0], same as get(ETHERNET) get(ETHERNET, 1) // returns layers[3]
Suppress warning is needed here because of the cast from Protocol
to the generic type. It is safe because each concrete protocol must
implement Protocol.id()
to return its distinct ID.
id
- protocol IDnth
- Nth protocol layerpublic boolean has(ProtocolId id)
id
- protocol IDpublic boolean hasAll(ProtocolId... ids)
ids
- protocol IDspublic boolean hasAny(ProtocolId... ids)
ids
- protocol IDspublic List<ProtocolId> protocolIds()
public Protocol innermost()
public Protocol innermost(ProtocolId id)
id
- protocol IDpublic ProtocolId innermostId()
UNKNOWN
if there are no known
protocols in the packetpublic long protocolMask()
public String toDebugString()
Copyright © 2015. All Rights Reserved.