Moving the remaining code from Layer2.* into Packet.* and documenting

the Packet API.

Plus, some more cleanup, including removing a legacy option
time_machine_profiling.
This commit is contained in:
Robin Sommer 2015-07-21 07:37:04 -07:00
parent f69edd1437
commit f97b2b180c
11 changed files with 350 additions and 326 deletions

View file

@ -5,94 +5,125 @@
#include "IP.h"
#include "NetVar.h"
/**
* The Layer 3 type of a packet, as determined by the parsing code in Packet.
*/
enum Layer3Proto {
L3_UNKNOWN = -1,
L3_IPV4 = 1,
L3_IPV6 = 2,
L3_ARP = 3,
L3_UNKNOWN = -1, /// Layer 3 type could not be determined.
L3_IPV4 = 1, /// Layer 3 is IPv4.
L3_IPV6 = 2, /// Layer 3 is IPv6.
L3_ARP = 3, /// Layer 3 is ARP.
};
// A link-layer packet.
//
// Note that for serialization we don't use much of the support provided by
// the serialization framework. Serialize/Unserialize do all the work by
// themselves. In particular, Packets aren't derived from SerialObj. They are
// completely seperate and self-contained entities, and we don't need any of
// the sophisticated features like object caching.
/**
* A link-layer packet.
*
* Note that for serialization we don't use much of the support provided by
* the serialization framework. Serialize/Unserialize do all the work by
* themselves. In particular, Packets aren't derived from SerialObj. They are
* completely seperate and self-contained entities, and we don't need any of
* the sophisticated features like object caching.
*/
class Packet {
public:
Packet()
/**
* Construct and initialize from packet data.
*
* @param link_type The link type in the form of a \c DLT_* constant.
*
* @param ts The timestamp associated with the packet.
*
* @param caplen The number of bytes valid in *data*.
*
* @param len The wire length of the packet, which must be more or
* equal *caplen* (but can't be less).
*
* @param data A pointer to the raw packet data, starting with the
* layer 2 header. The pointer must remain valid for the lifetime of
* the Packet instance, unless *copy* is true.
*
* @param copy If true, the constructor will make an internal copy of
* *data*, so that the caller can release its version.
*
* @param tag A textual tag to associate with the packet for
* differentiating the input streams.
*/
Packet(int link_type, struct timeval *ts, uint32 caplen,
uint32 len, const u_char *data, int copy = false,
std::string tag = std::string("")) : data(0)
{
Init(link_type, ts, caplen, len, data, copy, tag);
}
/**
* Default constructor. For internal use only.
*/
Packet() : data(0)
{
struct timeval ts = {0, 0};
Init(0, &ts, 0, 0, 0);
}
// Construct and initialize from packet data.
//
// arg_free: If true makes an internal copy of the *data*. If false,
// stores just a pointer to *data*, which must remain valid.
Packet(int arg_link_type, struct timeval *arg_ts, uint32 arg_caplen,
uint32 arg_len, const u_char *arg_data, int arg_free = false,
std::string arg_tag = std::string(""))
{
Init(arg_link_type, arg_ts, arg_caplen, arg_len, arg_data, arg_free, arg_tag);
}
/**
* Destructor.
*/
~Packet()
{
if ( free )
if ( copy )
delete [] data;
}
// Initialize with data from pointer.
//
// arg_free: If true makes an internal copy of the *data*. If false,
// stores just a pointer to *data*, which must remain valid.
void Init(int arg_link_type, struct timeval *arg_ts, uint32 arg_caplen,
uint32 arg_len, const u_char *arg_data, int arg_free = false,
std::string arg_tag = std::string(""), uint32 arg_hdrsize = 0)
{
link_type = arg_link_type;
ts = *arg_ts;
cap_len = arg_caplen;
len = arg_len;
free = arg_free;
/**
* (Re-)initialize from packet data.
*
* @param link_type The link type in the form of a \c DLT_* constant.
*
* @param ts The timestamp associated with the packet.
*
* @param caplen The number of bytes valid in *data*.
*
* @param len The wire length of the packet, which must be more or
* equal *caplen* (but can't be less).
*
* @param data A pointer to the raw packet data, starting with the
* layer 2 header. The pointer must remain valid for the lifetime of
* the Packet instance, unless *copy* is true.
*
* @param copy If true, the constructor will make an internal copy of
* *data*, so that the caller can release its version.
*
* @param tag A textual tag to associate with the packet for
* differentiating the input streams.
*/
void Init(int link_type, struct timeval *ts, uint32 caplen,
uint32 len, const u_char *data, int copy = false,
std::string tag = std::string(""));
if ( free )
{
data = new u_char[cap_len];
memcpy(const_cast<u_char *>(data), arg_data, cap_len);
}
else
data = arg_data;
hdr_size = arg_hdrsize;
l3_proto = L3_UNKNOWN;
tag = arg_tag;
time = ts.tv_sec + double(ts.tv_usec) / 1e6;
eth_type = 0;
vlan = 0;
l2_valid = false;
if ( data )
ProcessLayer2();
}
const IP_Hdr IP() const
{ return IP_Hdr((struct ip *) (data + hdr_size), false); }
// Returns true if parsing the Layer 2 fields failed, including when
// no data was passed into the constructor in the first place.
/**
* Returns true if parsing the layer 2 fields failed, including when
* no data was passed into the constructor in the first place.
*/
bool Layer2Valid()
{
return l2_valid;
}
void Describe(ODesc* d) const;
/**
* Interprets the Layer 3 of the packet as IP and returns a
* correspondign object.
*/
const IP_Hdr IP() const
{ return IP_Hdr((struct ip *) (data + hdr_size), false); }
/**
* Helper method to return the header size for a given link tyoe.
* Returns a \c raw_pkt_hdr RecordVal, which includes layer 2 and
* also everything in IP_Hdr (i.e., IP4/6 + TCP/UDP/ICMP).
*/
RecordVal* BuildPktHdrVal() const;
/**
* Static method returning the link-layer header size for a given
* link type.
*
* @param link_type The link tyoe.
*
@ -100,7 +131,19 @@ public:
*/
static int GetLinkHeaderSize(int link_type);
/**
* Describes the packet, with standard signature.
*/
void Describe(ODesc* d) const;
/**
* Serializes the packet, with standard signature.
*/
bool Serialize(SerialInfo* info) const;
/**
* Unserializes the packet, with standard signature.
*/
static Packet* Unserialize(UnserialInfo* info);
// These are passed in through the constructor.
@ -114,10 +157,29 @@ public:
// These are computed from Layer 2 data. These fields are only valid if
// Layer2Valid() returns true.
uint32 hdr_size; /// Layer 2 header size
Layer3Proto l3_proto; /// Layer 3 protocol identified (if any)
uint32 eth_type; /// If L2==ethernet, innermost ethertype field
uint32 vlan; /// (Outermost) VLan tag if any, else 0
/**
* Layer 2 header size. Valid iff Layer2Valid() returns true.
*/
uint32 hdr_size;
/**
* Layer 3 protocol identified (if any). Valid iff Layer2Valid()
* returns true.
*/
Layer3Proto l3_proto; ///
/**
* If layer 2 is Ethernet, innermost ethertype field. Valid iff
* Layer2Valid() returns true.
*/
uint32 eth_type; ///
/**
* (Outermost) VLAN tag if any, else 0. Valid iff Layer2Valid()
* returns true.
*/
uint32 vlan; ///
private:
// Calculate layer 2 attributes. Sets
@ -126,8 +188,12 @@ private:
// Wrapper to generate a packet-level weird.
void Weird(const char* name);
// should we delete associated packet memory upon destruction.
bool free;
// Renders an MAC address into its ASCII representation.
Val *FmtEUI48(const u_char *mac) const;
// True if we need to delete associated packet memory upon
// destruction.
bool copy;
// True if L2 processing succeeded.
bool l2_valid;