This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/src/packet/packet_utils.cpp

407 lines
11 KiB
C++
Raw Normal View History

2024-04-11 19:44:02 +08:00
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "udp_utils.h"
#include "tcp_utils.h"
#include "ipv4_utils.h"
#include "ipv6_utils.h"
#include "packet_priv.h"
2024-04-11 19:44:02 +08:00
#include "marsio.h"
/******************************************************************************
* set and get from struct packet
******************************************************************************/
void packet_set_origin(struct packet *pkt, enum packet_origin origin)
{
pkt->origin = origin;
}
enum packet_origin packet_get_origin(const struct packet *pkt)
{
return pkt->origin;
}
int packet_get_layer_count(const struct packet *pkt)
2024-04-11 19:44:02 +08:00
{
return pkt->layers_used;
}
const struct raw_layer *packet_get_raw_layer(const struct packet *pkt, int idx)
2024-04-11 19:44:02 +08:00
{
if (idx < 0 || idx >= pkt->layers_used)
{
return NULL;
}
return &pkt->layers[idx];
}
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;
}
const char *packet_get_payload(const struct packet *pkt)
{
if (pkt->layers_used == 0)
{
return NULL;
}
return pkt->layers[pkt->layers_used - 1].pld_ptr;
}
uint16_t packet_get_payload_len(const struct packet *pkt)
{
if (pkt->layers_used == 0)
{
return 0;
}
return pkt->layers[pkt->layers_used - 1].pld_len;
}
void packet_set_action(struct packet *pkt, enum packet_action action)
2024-04-11 19:44:02 +08:00
{
pkt->action = action;
2024-04-11 19:44:02 +08:00
}
enum packet_action packet_get_action(const struct packet *pkt)
2024-04-11 19:44:02 +08:00
{
return pkt->action;
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
void packet_set_origin_ctx(struct packet *pkt, void *ctx)
2024-04-11 19:44:02 +08:00
{
2024-05-08 18:24:26 +08:00
pkt->origin_ctx = ctx;
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
void *packet_get_origin_ctx(const struct packet *pkt)
2024-04-11 19:44:02 +08:00
{
2024-05-08 18:24:26 +08:00
return pkt->origin_ctx;
2024-04-11 19:44:02 +08:00
}
int packet_is_fragment(const struct packet *pkt)
{
return (pkt->frag_layer) ? 1 : 0;
}
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);
2024-05-08 18:24:26 +08:00
pkt->origin = PACKET_ORIGIN_USERHEAP;
2024-04-11 19:44:02 +08:00
return pkt;
}
void packet_free(struct packet *pkt)
{
2024-05-08 18:24:26 +08:00
if (pkt && pkt->origin == PACKET_ORIGIN_USERHEAP)
2024-04-11 19:44:02 +08:00
{
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;
}
2024-05-15 11:40:00 +08:00
dup_pkt->layers_used = pkt->layers_used;
dup_pkt->layers_size = pkt->layers_size;
2024-04-11 19:44:02 +08:00
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
2024-05-15 11:40:00 +08:00
dup_pkt->data_len = pkt->data_len;
2024-05-08 18:24:26 +08:00
dup_pkt->origin_ctx = NULL;
2024-05-15 11:40:00 +08:00
dup_pkt->origin = PACKET_ORIGIN_USERHEAP;
dup_pkt->action = PACKET_ACTION_DROP;
2024-04-11 19:44:02 +08:00
for (int8_t i = 0; i < pkt->layers_used; i++)
{
2024-05-15 11:40:00 +08:00
dup_pkt->layers[i].type = pkt->layers[i].type;
2024-04-11 19:44:02 +08:00
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;
2024-05-15 11:40:00 +08:00
dup_pkt->layers[i].hdr_offset = pkt->layers[i].hdr_offset;
dup_pkt->layers[i].hdr_len = pkt->layers[i].hdr_len;
dup_pkt->layers[i].pld_len = pkt->layers[i].pld_len;
2024-04-11 19:44:02 +08:00
}
// update frag_layer
if (pkt->frag_layer)
{
dup_pkt->frag_layer = &dup_pkt->layers[pkt->frag_layer - pkt->layers];
}
return dup_pkt;
}
/******************************************************************************
* set and get from mbuff
******************************************************************************/
void packet_set_ctrl(struct packet *pkt)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
marsio_buff_set_ctrlbuf(mbuff);
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to set ctrl");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
int packet_is_ctrl(const struct packet *pkt)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
return marsio_buff_is_ctrlbuf(mbuff);
}
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to check ctrl");
2024-04-11 19:44:02 +08:00
return 0;
}
}
void packet_set_direction(struct packet *pkt, enum packet_direction dir)
2024-04-11 19:44:02 +08:00
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DIR, &dir, sizeof(dir)) != 0)
{
PACKET_LOG_ERROR("failed to set direction");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to set direction");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
enum packet_direction packet_get_direction(const struct packet *pkt)
2024-04-11 19:44:02 +08:00
{
enum packet_direction dir = PACKET_DIRECTION_INCOMING;
2024-04-11 19:44:02 +08:00
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DIR, &dir, sizeof(dir)) <= 0)
{
PACKET_LOG_ERROR("failed to get direction");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to get direction");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
return dir;
2024-04-11 19:44:02 +08:00
}
void packet_set_session_id(struct packet *pkt, uint64_t sess_id)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
if (marsio_buff_set_metadata(mbuff, MR_BUFF_SESSION_ID, &sess_id, sizeof(sess_id)) != 0)
{
PACKET_LOG_ERROR("failed to set session id");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to set session id");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
uint64_t packet_get_session_id(const struct packet *pkt)
{
uint64_t sess_id = 0;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
if (marsio_buff_get_metadata(mbuff, MR_BUFF_SESSION_ID, &sess_id, sizeof(sess_id)) <= 0)
{
PACKET_LOG_ERROR("failed to get session id");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to get session id");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
return sess_id;
}
void packet_set_domain(struct packet *pkt, uint64_t domain)
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
// TODO
#if 0
if (marsio_buff_set_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) != 0)
{
PACKET_LOG_ERROR("failed to set domain");
}
#endif
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to set domain");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
uint64_t packet_get_domain(const struct packet *pkt)
{
uint64_t domain = 0;
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
// TODO
#if 0
if (marsio_buff_get_metadata(mbuff, MR_BUFF_DOMAIN, &domain, sizeof(domain)) <= 0)
{
PACKET_LOG_ERROR("failed to get domain");
}
#endif
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to get domain");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
return domain;
}
2024-05-08 18:24:26 +08:00
void packet_set_route_ctx(struct packet *pkt, const struct route_ctx *ctx)
2024-04-11 19:44:02 +08:00
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
2024-05-08 18:24:26 +08:00
if (marsio_buff_set_metadata(mbuff, MR_BUFF_ROUTE_CTX, (void *)ctx->data, ctx->used) != 0)
{
PACKET_LOG_ERROR("failed to set route ctx");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to set route ctx");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
void packet_get_route_ctx(const struct packet *pkt, struct route_ctx *ctx)
2024-04-11 19:44:02 +08:00
{
2024-05-08 18:24:26 +08:00
ctx->used = 0;
2024-04-11 19:44:02 +08:00
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
2024-05-08 18:24:26 +08:00
ctx->used = marsio_buff_get_metadata(mbuff, MR_BUFF_ROUTE_CTX, ctx->data, sizeof(ctx->data));
if (ctx->used <= 0)
{
PACKET_LOG_ERROR("failed to get route ctx");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to get route ctx");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
void packet_set_sid_list(struct packet *pkt, const struct sid_list *list)
2024-04-11 19:44:02 +08:00
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
2024-05-08 18:24:26 +08:00
if (marsio_buff_set_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
{
PACKET_LOG_ERROR("failed to set sid list");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to set sid list");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
void packet_get_sid_list(const struct packet *pkt, struct sid_list *list)
2024-04-11 19:44:02 +08:00
{
2024-05-08 18:24:26 +08:00
list->used = 0;
2024-04-11 19:44:02 +08:00
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
2024-05-08 18:24:26 +08:00
list->used = marsio_buff_get_sid_list(mbuff, (sid_t *)list->sid, sizeof(list->sid) / sizeof(list->sid[0]));
}
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to get sid list");
2024-04-11 19:44:02 +08:00
}
}
2024-05-08 18:24:26 +08:00
void packet_prepend_sid_list(struct packet *pkt, const struct sid_list *list)
2024-04-11 19:44:02 +08:00
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
2024-05-08 18:24:26 +08:00
if (marsio_buff_prepend_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
{
PACKET_LOG_ERROR("failed to prepend sid list");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to prepend sid list");
2024-05-08 18:24:26 +08:00
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
void packet_append_sid_list(struct packet *pkt, const struct sid_list *list)
2024-04-11 19:44:02 +08:00
{
if (packet_get_origin(pkt) == PACKET_ORIGIN_MARSIO)
{
2024-05-08 18:24:26 +08:00
marsio_buff_t *mbuff = (marsio_buff_t *)packet_get_origin_ctx(pkt);
2024-04-11 19:44:02 +08:00
assert(mbuff != NULL);
2024-05-08 18:24:26 +08:00
if (marsio_buff_append_sid_list(mbuff, (sid_t *)list->sid, list->used) != 0)
{
PACKET_LOG_ERROR("failed to append sid list");
}
2024-04-11 19:44:02 +08:00
}
2024-05-08 18:24:26 +08:00
else
{
2024-05-16 11:52:14 +08:00
PACKET_LOG_WARN("packet origin is not marsio, failed to append sid list");
2024-05-08 18:24:26 +08:00
}
}