Add packet utils to packet.h
This commit is contained in:
@@ -1131,7 +1131,6 @@ const char *packet_parse(struct packet *pkt, const char *data, uint16_t len)
|
||||
pkt->layers_size = PACKET_MAX_LAYERS;
|
||||
pkt->data_ptr = data;
|
||||
pkt->data_len = len;
|
||||
memset(&pkt->meta, 0, sizeof(struct metadata));
|
||||
|
||||
// TESTED
|
||||
return parse_ether(pkt, data, len);
|
||||
@@ -1291,7 +1290,7 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
const struct packet_layer *layer_l3 = NULL;
|
||||
const struct packet_layer *layer_l4 = NULL;
|
||||
const struct packet_layer *layer = NULL;
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
uint64_t domain = packet_get_domain(pkt);
|
||||
|
||||
for (int8_t i = pkt->layers_used - 1; i >= 0; i--)
|
||||
{
|
||||
@@ -1312,10 +1311,10 @@ int packet_get_innermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
}
|
||||
}
|
||||
|
||||
if (layer_l3 && layer_l4)
|
||||
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
|
||||
{
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, meta->domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, meta->domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, domain);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1332,7 +1331,7 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
const struct packet_layer *layer_l3 = NULL;
|
||||
const struct packet_layer *layer_l4 = NULL;
|
||||
const struct packet_layer *layer = NULL;
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
uint64_t domain = packet_get_domain(pkt);
|
||||
|
||||
for (int8_t i = 0; i < pkt->layers_used; i++)
|
||||
{
|
||||
@@ -1353,10 +1352,10 @@ int packet_get_outermost_tuple6(const struct packet *pkt, struct tuple6 *tuple)
|
||||
}
|
||||
}
|
||||
|
||||
if (layer_l3 && layer_l4)
|
||||
if (layer_l3 && layer_l4 && layer_l4 - layer_l3 == 1)
|
||||
{
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, meta->domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, meta->domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l3->hdr_offset, layer_l3->type, tuple, domain);
|
||||
set_tuple6((const char *)pkt->data_ptr + layer_l4->hdr_offset, layer_l4->type, tuple, domain);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
@@ -1515,276 +1514,4 @@ uint64_t packet_get_hash(const struct packet *pkt, enum ldbc_method method, int
|
||||
}
|
||||
|
||||
return hash_value;
|
||||
}
|
||||
|
||||
int8_t packet_get_layers(const struct packet *pkt)
|
||||
{
|
||||
return pkt->layers_used;
|
||||
}
|
||||
|
||||
const struct packet_layer *packet_get_layer(const struct packet *pkt, int8_t idx)
|
||||
{
|
||||
if (idx < 0 || idx >= pkt->layers_used)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return &pkt->layers[idx];
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Packet Meta Data
|
||||
******************************************************************************/
|
||||
|
||||
// return 0 : success
|
||||
// return -1 : failed
|
||||
int packet_set_sid(struct packet *pkt, uint16_t *sid, int num)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
if (num > MAX_SID_NUM)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
meta->sid.used = num;
|
||||
memcpy(meta->sid.list, sid, num * sizeof(uint16_t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return number of sid
|
||||
int packet_get_sid(const struct packet *pkt, uint16_t *sid, int size)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
if (size < meta->sid.used)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(sid, meta->sid.list, meta->sid.used * sizeof(uint16_t));
|
||||
return meta->sid.used;
|
||||
}
|
||||
|
||||
// return 0 : success
|
||||
// return -1 : failed
|
||||
int packet_prepend_sid(struct packet *pkt, uint16_t sid)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
if (meta->sid.used >= MAX_SID_NUM)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memmove(meta->sid.list + 1, meta->sid.list, meta->sid.used * sizeof(uint16_t));
|
||||
meta->sid.list[0] = sid;
|
||||
meta->sid.used++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return 0 : success
|
||||
// return -1 : failed
|
||||
int packet_append_sid(struct packet *pkt, uint16_t sid)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
if (meta->sid.used >= MAX_SID_NUM)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
meta->sid.list[meta->sid.used] = sid;
|
||||
meta->sid.used++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return 0 : success
|
||||
// return -1 : failed
|
||||
int packet_set_route_ctx(struct packet *pkt, const char *route, int len)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
if (len > MAX_ROUTE_LEN)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(meta->route.data, route, len);
|
||||
meta->route.len = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return len of route ctx
|
||||
int packet_get_route_ctx(const struct packet *pkt, char *buff, int size)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
if (meta->route.len > size)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(buff, meta->route.data, meta->route.len);
|
||||
return meta->route.len;
|
||||
}
|
||||
|
||||
void packet_set_io_ctx(struct packet *pkt, void *ctx)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
meta->io_ctx = ctx;
|
||||
}
|
||||
|
||||
void *packet_get_io_ctx(const struct packet *pkt)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
return meta->io_ctx;
|
||||
}
|
||||
|
||||
void packet_set_domain(struct packet *pkt, uint64_t domain)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
meta->domain = domain;
|
||||
}
|
||||
|
||||
uint64_t packet_get_domain(const struct packet *pkt)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
return meta->domain;
|
||||
}
|
||||
|
||||
void packet_set_session_id(struct packet *pkt, uint64_t session_id)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
meta->session_id = session_id;
|
||||
}
|
||||
|
||||
uint64_t packet_get_session_id(const struct packet *pkt)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
return meta->session_id;
|
||||
}
|
||||
|
||||
void packet_set_direction(struct packet *pkt, enum packet_direction direction)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
meta->direction = direction;
|
||||
}
|
||||
|
||||
enum packet_direction packet_get_direction(const struct packet *pkt)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
return meta->direction;
|
||||
}
|
||||
|
||||
void packet_set_action(struct packet *pkt, enum packet_action action)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
meta->action = action;
|
||||
}
|
||||
|
||||
enum packet_action packet_get_action(const struct packet *pkt)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
return meta->action;
|
||||
}
|
||||
|
||||
void packet_set_type(struct packet *pkt, enum packet_type type)
|
||||
{
|
||||
struct metadata *meta = &pkt->meta;
|
||||
|
||||
meta->type = type;
|
||||
}
|
||||
|
||||
enum packet_type packet_get_type(const struct packet *pkt)
|
||||
{
|
||||
const struct metadata *meta = &pkt->meta;
|
||||
|
||||
return meta->type;
|
||||
}
|
||||
|
||||
struct packet *packet_new(uint16_t pkt_len)
|
||||
{
|
||||
struct packet *pkt = (struct packet *)calloc(1, sizeof(struct packet) + pkt_len);
|
||||
if (pkt == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
pkt->data_len = pkt_len;
|
||||
pkt->data_ptr = (const char *)pkt + sizeof(struct packet);
|
||||
pkt->need_free = true;
|
||||
|
||||
return pkt;
|
||||
}
|
||||
|
||||
void packet_free(struct packet *pkt)
|
||||
{
|
||||
if (pkt && pkt->need_free)
|
||||
{
|
||||
pkt->need_free = false;
|
||||
free((void *)pkt);
|
||||
}
|
||||
}
|
||||
|
||||
struct packet *packet_dup(const struct packet *pkt)
|
||||
{
|
||||
if (pkt == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct packet *dup_pkt = packet_new(pkt->data_len);
|
||||
if (dup_pkt == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memcpy(dup_pkt, pkt, sizeof(struct packet));
|
||||
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
|
||||
dup_pkt->need_free = true;
|
||||
|
||||
struct metadata *meta = &dup_pkt->meta;
|
||||
meta->io_ctx = NULL;
|
||||
|
||||
// update layers
|
||||
for (int8_t i = 0; i < pkt->layers_used; i++)
|
||||
{
|
||||
dup_pkt->layers[i].hdr_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset;
|
||||
dup_pkt->layers[i].pld_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset + pkt->layers[i].hdr_len;
|
||||
}
|
||||
|
||||
// update frag_layer
|
||||
if (pkt->frag_layer)
|
||||
{
|
||||
dup_pkt->frag_layer = &dup_pkt->layers[pkt->frag_layer - pkt->layers];
|
||||
}
|
||||
|
||||
return dup_pkt;
|
||||
}
|
||||
|
||||
const char *packet_get_data(const struct packet *pkt)
|
||||
{
|
||||
return pkt->data_ptr;
|
||||
}
|
||||
|
||||
uint16_t packet_get_len(const struct packet *pkt)
|
||||
{
|
||||
return pkt->data_len;
|
||||
}
|
||||
|
||||
bool packet_is_fragment(const struct packet *pkt)
|
||||
{
|
||||
if (pkt->frag_layer)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user