Support struct tunnel
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
add_library(packet packet.cpp packet_utils.cpp packet_layer.cpp)
|
||||
add_library(packet packet.cpp packet_utils.cpp packet_layer.cpp packet_tunnel.cpp)
|
||||
target_include_directories(packet PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
||||
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash)
|
||||
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
@@ -1,23 +1,16 @@
|
||||
#include "packet_priv.h"
|
||||
#include "packet_utils.h"
|
||||
|
||||
int packet_get_layer(const struct packet *pkt, int idx, struct layer *out)
|
||||
{
|
||||
if (pkt == NULL || out == NULL)
|
||||
const struct raw_layer *raw = packet_get_raw_layer(pkt, idx);
|
||||
if (raw == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (idx < 0 || idx >= pkt->layers_used)
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
layer_convert(raw, out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct raw_layer *raw = &pkt->layers[idx];
|
||||
out->proto = raw->proto;
|
||||
out->header_len = raw->hdr_len;
|
||||
out->payload_len = raw->pld_len;
|
||||
out->header.raw = raw->hdr_ptr;
|
||||
out->payload = raw->pld_ptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
99
src/packet/packet_tunnel.cpp
Normal file
99
src/packet/packet_tunnel.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "stellar/tunnel.h"
|
||||
#include "packet_priv.h"
|
||||
#include "packet_utils.h"
|
||||
|
||||
static void layers_to_tunnel(const struct raw_layer *curr, const struct raw_layer *next1, const struct raw_layer *next2, struct tunnel *out)
|
||||
{
|
||||
assert(curr);
|
||||
|
||||
// GRE tunnel
|
||||
if (next1 && next1->proto == LAYER_PROTO_GRE)
|
||||
{
|
||||
out->type = TUNNEL_GRE;
|
||||
out->used = 2;
|
||||
layer_convert(curr, &out->layers[0]);
|
||||
layer_convert(next1, &out->layers[1]);
|
||||
return;
|
||||
}
|
||||
|
||||
// GTP tunnel
|
||||
if (next1 && next1->proto == LAYER_PROTO_UDP && next2 && next2->proto == LAYER_PROTO_GTPV1_U)
|
||||
{
|
||||
out->type = TUNNEL_GTP;
|
||||
out->used = 3;
|
||||
layer_convert(curr, &out->layers[0]);
|
||||
layer_convert(next1, &out->layers[1]);
|
||||
layer_convert(next2, &out->layers[2]);
|
||||
return;
|
||||
}
|
||||
|
||||
// IP tunnel
|
||||
if (next1 && (next1->proto == LAYER_PROTO_IPV4 || next1->proto == LAYER_PROTO_IPV6))
|
||||
{
|
||||
out->type = curr->proto == LAYER_PROTO_IPV4 ? TUNNEL_IPV4 : TUNNEL_IPV6;
|
||||
out->used = 1;
|
||||
layer_convert(curr, &out->layers[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
// none tunnel
|
||||
out->type = TUNNEL_NONE;
|
||||
layer_convert(curr, &out->layers[out->used++]);
|
||||
if (next1 && (next1->proto == LAYER_PROTO_TCP || next1->proto == LAYER_PROTO_UDP))
|
||||
{
|
||||
layer_convert(next1, &out->layers[out->used++]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int packet_get_tunnel_count(const struct packet *pkt)
|
||||
{
|
||||
int count = 0;
|
||||
int used = packet_get_layer_count(pkt);
|
||||
const struct raw_layer *curr = NULL;
|
||||
|
||||
for (int i = 0; i < used; i++)
|
||||
{
|
||||
curr = packet_get_raw_layer(pkt, i);
|
||||
if (curr->proto == LAYER_PROTO_IPV4 || curr->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
// return 0: success
|
||||
// return -1: failed
|
||||
int packet_get_tunnel(const struct packet *pkt, int idx, struct tunnel *out)
|
||||
{
|
||||
int count = 0;
|
||||
int used = packet_get_layer_count(pkt);
|
||||
const struct raw_layer *curr = NULL;
|
||||
const struct raw_layer *next1 = NULL;
|
||||
const struct raw_layer *next2 = NULL;
|
||||
memset(out, 0, sizeof(struct tunnel));
|
||||
|
||||
for (int i = 0; i < used; i++)
|
||||
{
|
||||
curr = packet_get_raw_layer(pkt, i);
|
||||
next1 = packet_get_raw_layer(pkt, i + 1);
|
||||
next2 = packet_get_raw_layer(pkt, i + 2);
|
||||
|
||||
if (curr->proto == LAYER_PROTO_IPV4 || curr->proto == LAYER_PROTO_IPV6)
|
||||
{
|
||||
if (count == idx)
|
||||
{
|
||||
layers_to_tunnel(curr, next1, next2, out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -186,4 +186,18 @@ void packet_free(struct packet *pkt)
|
||||
{
|
||||
free((void *)pkt);
|
||||
}
|
||||
}
|
||||
|
||||
void layer_convert(const struct raw_layer *in, struct layer *out)
|
||||
{
|
||||
if (in == NULL || out == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
out->proto = in->proto;
|
||||
out->header_len = in->hdr_len;
|
||||
out->payload_len = in->pld_len;
|
||||
out->header.raw = in->hdr_ptr;
|
||||
out->payload = in->pld_ptr;
|
||||
}
|
||||
@@ -50,6 +50,8 @@ struct packet *packet_new(uint16_t pkt_len);
|
||||
struct packet *packet_dup(const struct packet *pkt);
|
||||
void packet_free(struct packet *pkt);
|
||||
|
||||
void layer_convert(const struct raw_layer *in, struct layer *out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user