Add IP proto utils function

This commit is contained in:
luwenpeng
2024-06-02 00:36:50 +08:00
parent 18fe1e2e41
commit eb1056b4f9
5 changed files with 119 additions and 79 deletions

View File

@@ -61,7 +61,7 @@ static inline void eth_hdr_set_proto(struct ethhdr *hdr, uint16_t proto)
* identify
******************************************************************************/
// /usr/include/linux/if_ether.h.html
// /usr/include/linux/if_ether.h
static inline int is_eth_proto(uint16_t proto)
{
switch (proto)
@@ -134,6 +134,10 @@ static inline int is_eth_proto(uint16_t proto)
}
}
/******************************************************************************
* print
******************************************************************************/
static inline const char *eth_proto_to_str(uint16_t proto)
{
switch (proto)
@@ -267,10 +271,6 @@ static inline const char *eth_proto_to_str(uint16_t proto)
}
}
/******************************************************************************
* print
******************************************************************************/
static inline int eth_hdr_to_str(const struct ethhdr *hdr, char *buf, size_t size)
{
memset(buf, 0, size);

View File

@@ -256,26 +256,132 @@ static inline void ipv4_hdr_set_opt_data(struct ip *hdr, const char *opt_data)
memcpy((char *)hdr + sizeof(struct ip), opt_data, ipv4_hdr_get_opt_len(hdr));
}
/******************************************************************************
* identify
******************************************************************************/
// /usr/include/netinet/in.h
static inline int is_ip_proto(uint16_t proto)
{
switch (proto)
{
case IPPROTO_IP:
case IPPROTO_ICMP:
case IPPROTO_IGMP:
case IPPROTO_IPIP:
case IPPROTO_TCP:
case IPPROTO_EGP:
case IPPROTO_PUP:
case IPPROTO_UDP:
case IPPROTO_IDP:
case IPPROTO_TP:
case IPPROTO_DCCP:
case IPPROTO_IPV6:
case IPPROTO_RSVP:
case IPPROTO_GRE:
case IPPROTO_ESP:
case IPPROTO_AH:
case IPPROTO_MTP:
case IPPROTO_BEETPH:
case IPPROTO_ENCAP:
case IPPROTO_PIM:
case IPPROTO_COMP:
case IPPROTO_SCTP:
case IPPROTO_UDPLITE:
case IPPROTO_MPLS:
case IPPROTO_ETHERNET:
case IPPROTO_RAW:
case IPPROTO_MPTCP:
return 1;
default:
return 0;
}
}
/******************************************************************************
* print
******************************************************************************/
static inline const char *ip_proto_to_str(uint16_t proto)
{
switch (proto)
{
case IPPROTO_IP:
return "IPPROTO_IP";
case IPPROTO_ICMP:
return "IPPROTO_ICMP";
case IPPROTO_IGMP:
return "IPPROTO_IGMP";
case IPPROTO_IPIP:
return "IPPROTO_IPIP";
case IPPROTO_TCP:
return "IPPROTO_TCP";
case IPPROTO_EGP:
return "IPPROTO_EGP";
case IPPROTO_PUP:
return "IPPROTO_PUP";
case IPPROTO_UDP:
return "IPPROTO_UDP";
case IPPROTO_IDP:
return "IPPROTO_IDP";
case IPPROTO_TP:
return "IPPROTO_TP";
case IPPROTO_DCCP:
return "IPPROTO_DCCP";
case IPPROTO_IPV6:
return "IPPROTO_IPV6";
case IPPROTO_RSVP:
return "IPPROTO_RSVP";
case IPPROTO_GRE:
return "IPPROTO_GRE";
case IPPROTO_ESP:
return "IPPROTO_ESP";
case IPPROTO_AH:
return "IPPROTO_AH";
case IPPROTO_MTP:
return "IPPROTO_MTP";
case IPPROTO_BEETPH:
return "IPPROTO_BEETPH";
case IPPROTO_ENCAP:
return "IPPROTO_ENCAP";
case IPPROTO_PIM:
return "IPPROTO_PIM";
case IPPROTO_COMP:
return "IPPROTO_COMP";
case IPPROTO_SCTP:
return "IPPROTO_SCTP";
case IPPROTO_UDPLITE:
return "IPPROTO_UDPLITE";
case IPPROTO_MPLS:
return "IPPROTO_MPLS";
case IPPROTO_ETHERNET:
return "IPPROTO_ETHERNET";
case IPPROTO_RAW:
return "IPPROTO_RAW";
case IPPROTO_MPTCP:
return "IPPROTO_MPTCP";
default:
return "IPPROTO_UNKNOWN";
}
}
static inline int ipv4_hdr_to_str(const struct ip *hdr, char *buf, size_t size)
{
memset(buf, 0, size);
char src_addr_str[INET6_ADDRSTRLEN] = {0};
char dst_addr_str[INET6_ADDRSTRLEN] = {0};
uint16_t proto = ipv4_hdr_get_proto(hdr);
struct in_addr src_addr = ipv4_hdr_get_src_in_addr(hdr);
struct in_addr dst_addr = ipv4_hdr_get_dst_in_addr(hdr);
inet_ntop(AF_INET, &src_addr, src_addr_str, sizeof(src_addr_str));
inet_ntop(AF_INET, &dst_addr, dst_addr_str, sizeof(dst_addr_str));
return snprintf(buf, size, "IPv4: version=%u hdr_len=%u tos=%u total_len=%u ipid=%u flags=%u(rf=%u df=%u mf=%u) frag_offset=%u ttl=%u proto=%u checksum=%u src_addr=%s dst_addr=%s opt_len=%u",
return snprintf(buf, size, "IPv4: version=%u hdr_len=%u tos=%u total_len=%u ipid=%u flags=%u(rf=%u df=%u mf=%u) frag_offset=%u ttl=%u proto=%s checksum=0x%x src_addr=%s dst_addr=%s opt_len=%u",
ipv4_hdr_get_version(hdr), ipv4_hdr_get_hdr_len(hdr), ipv4_hdr_get_tos(hdr),
ipv4_hdr_get_total_len(hdr), ipv4_hdr_get_ipid(hdr), ipv4_hdr_get_flags(hdr),
ipv4_hdr_get_rf_flag(hdr), ipv4_hdr_get_df_flag(hdr), ipv4_hdr_get_mf_flag(hdr),
ipv4_hdr_get_frag_offset(hdr), ipv4_hdr_get_ttl(hdr), ipv4_hdr_get_proto(hdr),
ipv4_hdr_get_frag_offset(hdr), ipv4_hdr_get_ttl(hdr), ip_proto_to_str(proto),
ipv4_hdr_get_checksum(hdr), src_addr_str, dst_addr_str, ipv4_hdr_get_opt_len(hdr));
}

View File

@@ -34,17 +34,16 @@
(tag), (next_proto), eth_proto_to_str(next_proto)); \
}
#define PACKET_LOG_UNSUPPORT_IPPROTO(tag, next_proto) \
{ \
PACKET_LOG_WARN("%s: unsupport next ip proto %d: %s", \
(tag), (next_proto), ipproto_to_str(next_proto)); \
#define PACKET_LOG_UNSUPPORT_IPPROTO(tag, next_proto) \
{ \
PACKET_LOG_WARN("%s: unsupport next ip proto %d: %s", \
(tag), (next_proto), ip_proto_to_str(next_proto)); \
}
/******************************************************************************
* Static API
******************************************************************************/
static const char *ipproto_to_str(uint16_t proto);
static inline const char *ldbc_method_to_str(enum ldbc_method method);
static inline const char *layer_type_to_str(enum layer_type type);
@@ -91,71 +90,6 @@ static inline const char *parse_l4(struct packet *pkt, uint8_t next_proto, const
* Private API -- Utils
******************************************************************************/
static const char *ipproto_to_str(uint16_t proto)
{
switch (proto)
{
case IPPROTO_IP:
return "IP";
case IPPROTO_ICMP:
return "ICMP";
case IPPROTO_IGMP:
return "IGMP";
case IPPROTO_IPIP:
return "IPIP";
case IPPROTO_TCP:
return "TCP";
case IPPROTO_EGP:
return "EGP";
case IPPROTO_PUP:
return "PUP";
case IPPROTO_UDP:
return "UDP";
case IPPROTO_IDP:
return "IDP";
case IPPROTO_TP:
return "TP";
case IPPROTO_DCCP:
return "DCCP";
case IPPROTO_IPV6:
return "IPV6";
case IPPROTO_ROUTING:
return "ROUTING";
case IPPROTO_FRAGMENT:
return "FRAGMENT";
case IPPROTO_RSVP:
return "RSVP";
case IPPROTO_GRE:
return "GRE";
case IPPROTO_ESP:
return "ESP";
case IPPROTO_AH:
return "AH";
case IPPROTO_ICMPV6:
return "ICMPV6";
case IPPROTO_NONE:
return "NONE";
case IPPROTO_DSTOPTS:
return "DSTOPTS";
case IPPROTO_MTP:
return "MTP";
case IPPROTO_ENCAP:
return "ENCAP";
case IPPROTO_PIM:
return "PIM";
case IPPROTO_COMP:
return "COMP";
case IPPROTO_SCTP:
return "SCTP";
case IPPROTO_UDPLITE:
return "UDPLITE";
case IPPROTO_RAW:
return "RAW";
default:
return "UNKNOWN";
}
}
static inline const char *ldbc_method_to_str(enum ldbc_method method)
{
switch (method)

View File

@@ -269,7 +269,7 @@ static inline void tcp_hdr_set_opt_data(struct tcphdr *hdr, const char *ptr)
static inline int tcp_hdr_to_str(const struct tcphdr *hdr, char *buf, size_t size)
{
memset(buf, 0, size);
return snprintf(buf, size, "TCP: src_port=%u dst_port=%u seq=%u ack=%u hdr_len=%u flags=0x%02x(%s%s%s%s%s%s) window=%u checksum=%u urg_ptr=%u opt_len=%u",
return snprintf(buf, size, "TCP: src_port=%u dst_port=%u seq=%u ack=%u hdr_len=%u flags=0x%02x(%s%s%s%s%s%s) window=%u checksum=0x%x urg_ptr=%u opt_len=%u",
tcp_hdr_get_src_port(hdr), tcp_hdr_get_dst_port(hdr),
tcp_hdr_get_seq(hdr), tcp_hdr_get_ack(hdr),
tcp_hdr_get_hdr_len(hdr), tcp_hdr_get_flags(hdr),

View File

@@ -81,7 +81,7 @@ static inline void udp_hdr_set_checksum(struct udphdr *hdr, uint16_t sum)
static inline int udp_hdr_to_str(const struct udphdr *hdr, char *buf, size_t size)
{
memset(buf, 0, size);
return snprintf(buf, size, "UDP: src_port=%u dst_port=%u total_len=%u checksum=%u",
return snprintf(buf, size, "UDP: src_port=%u dst_port=%u total_len=%u checksum=0x%x",
udp_hdr_get_src_port(hdr), udp_hdr_get_dst_port(hdr),
udp_hdr_get_total_len(hdr), udp_hdr_get_checksum(hdr));
}