使用自定义的udp_hdr替换linux的udphdr

This commit is contained in:
luwenpeng
2023-02-17 17:53:21 +08:00
parent 0ba7fefba5
commit 551abde887
6 changed files with 33 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
add_library(common src/addr_tuple4.cpp src/session_table.cpp src/raw_packet.cpp src/ctrl_packet.cpp src/bfd.cpp src/utils.cpp src/g_vxlan.cpp)
target_link_libraries(common PUBLIC cjson)
target_link_libraries(common PUBLIC mrzcpd)
target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

View File

@@ -40,7 +40,7 @@ int fixed_num_array_index_elem(struct fixed_num_array *array, int index);
* sids
******************************************************************************/
#include "marsio.h"
#include <marsio.h>
struct sids
{
@@ -67,7 +67,15 @@ void throughput_metrics_inc(struct throughput_metrics *iterm, uint64_t n_pkts, u
* protocol
******************************************************************************/
void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udphdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len);
struct udp_hdr
{
u_int16_t uh_sport; /* source port */
u_int16_t uh_dport; /* destination port */
u_int16_t uh_ulen; /* udp length */
u_int16_t uh_sum; /* udp checksum */
} __attribute__((__packed__));
void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len);
void build_ip_header(struct ip *ip_hdr, u_int8_t next_protocol, const char *src_addr, const char *dst_addr, uint16_t payload_len);
void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac);

View File

@@ -1,7 +1,7 @@
#include <netinet/udp.h>
#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)
@@ -38,7 +38,7 @@ int g_vxlan_get_traffic_type(struct g_vxlan *hdr)
// 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 udphdr) + sizeof(struct g_vxlan)))
if (raw_len <= (int)(sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan)))
{
return -1;
}
@@ -55,13 +55,13 @@ int g_vxlan_decode(struct g_vxlan **g_vxlan_hdr, const char *raw_data, int raw_l
return -1;
}
struct udphdr *udp_hdr = (struct udphdr *)((char *)ip_hdr + sizeof(struct ip));
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 udphdr));
*g_vxlan_hdr = (struct g_vxlan *)((char *)udp_hdr + sizeof(struct udp_hdr));
return 0;
}

View File

@@ -7,7 +7,6 @@
#include <netinet/ip6.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ether.h>
#include <linux/ppp_defs.h>
@@ -471,7 +470,7 @@ static const char *ldbc_method_to_string(enum ldbc_method ldbc_method)
static void set_addr_tuple4(const void *data, enum layer_type layer_type, struct addr_tuple4 *addr)
{
const struct tcphdr *tcp_hdr = NULL;
const struct udphdr *udp_hdr = NULL;
const struct udp_hdr *udp_hdr = NULL;
const struct ip *ipv4_hdr = NULL;
const struct ip6_hdr *ipv6_hdr = NULL;
@@ -483,7 +482,7 @@ static void set_addr_tuple4(const void *data, enum layer_type layer_type, struct
addr->dst_port = tcp_hdr->th_dport;
break;
case LAYER_TYPE_UDP:
udp_hdr = (const struct udphdr *)data;
udp_hdr = (const struct udp_hdr *)data;
addr->src_port = udp_hdr->uh_sport;
addr->dst_port = udp_hdr->uh_dport;
break;
@@ -737,7 +736,7 @@ static const void *parse_tcp(struct raw_pkt_parser *handler, const void *data, s
static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, size_t length, enum layer_type this_type)
{
if (length < sizeof(struct udphdr))
if (length < sizeof(struct udp_hdr))
{
LOG_ERROR("%s: pkt_trace_id: %lu, this_layer: %s, err: data not enough", LOG_TAG_RAWPKT, handler->pkt_trace_id, layer_type2str(this_type));
return data;
@@ -748,8 +747,8 @@ static const void *parse_udp(struct raw_pkt_parser *handler, const void *data, s
return data;
}
struct udphdr *hdr = (struct udphdr *)data;
uint16_t hdr_len = sizeof(struct udphdr);
struct udp_hdr *hdr = (struct udp_hdr *)data;
uint16_t hdr_len = sizeof(struct udp_hdr);
const void *data_next_layer = (const char *)data + hdr_len;
size_t data_next_length = length - hdr_len;

View File

@@ -2,12 +2,11 @@
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/if.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/ether.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ether.h>
#include "utils.h"
#include "log.h"
@@ -161,11 +160,11 @@ static int checksum(u_int16_t *addr, int len)
return sum;
}
void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udphdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len)
void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, u_int16_t udp_sport, u_int16_t udp_dport, int payload_len)
{
memset(udp_hdr, 0, sizeof(struct udphdr));
memset(udp_hdr, 0, sizeof(struct udp_hdr));
int udp_hlen = sizeof(struct udphdr) + payload_len;
int udp_hlen = sizeof(struct udp_hdr) + payload_len;
udp_hdr->uh_sport = htons(udp_sport);
udp_hdr->uh_dport = htons(udp_dport);