67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
#include <netinet/ip.h>
|
|
#include <netinet/ether.h>
|
|
|
|
#include "utils.h"
|
|
#include "g_vxlan.h"
|
|
|
|
void g_vxlan_set_packet_dir(struct g_vxlan *hdr, int dir_is_e2i)
|
|
{
|
|
hdr->dir_is_e2i = (!!dir_is_e2i);
|
|
}
|
|
|
|
void g_vxlan_set_sf_index(struct g_vxlan *hdr, int sf_index)
|
|
{
|
|
hdr->sf_index = (0x1f & sf_index);
|
|
}
|
|
|
|
void g_vxlan_set_traffic_type(struct g_vxlan *hdr, int traffic_is_decrypted)
|
|
{
|
|
hdr->traffic_is_decrypted = (!!traffic_is_decrypted);
|
|
}
|
|
|
|
int g_vxlan_get_packet_dir(struct g_vxlan *hdr)
|
|
{
|
|
return (!!hdr->dir_is_e2i);
|
|
}
|
|
|
|
int g_vxlan_get_sf_index(struct g_vxlan *hdr)
|
|
{
|
|
return hdr->sf_index;
|
|
}
|
|
|
|
int g_vxlan_get_traffic_type(struct g_vxlan *hdr)
|
|
{
|
|
return (!!hdr->traffic_is_decrypted);
|
|
}
|
|
|
|
// return 0 : success
|
|
// return -1 : error
|
|
int g_vxlan_decode(struct g_vxlan **g_vxlan_hdr, const char *raw_data, int raw_len)
|
|
{
|
|
if (raw_len <= (int)(sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan)))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
struct ethhdr *eth_hdr = (struct ethhdr *)raw_data;
|
|
if (eth_hdr->h_proto != htons(ETH_P_IP))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
struct ip *ip_hdr = (struct ip *)((char *)eth_hdr + sizeof(struct ethhdr));
|
|
if (ip_hdr->ip_p != IPPROTO_UDP)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
struct udp_hdr *udp_hdr = (struct udp_hdr *)((char *)ip_hdr + sizeof(struct ip));
|
|
if (udp_hdr->uh_dport != htons(4789))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
*g_vxlan_hdr = (struct g_vxlan *)((char *)udp_hdr + sizeof(struct udp_hdr));
|
|
|
|
return 0;
|
|
} |