Add tools packet_parser
This commit is contained in:
@@ -6,6 +6,7 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/ip.h>
|
||||
@@ -255,6 +256,25 @@ 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));
|
||||
}
|
||||
|
||||
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};
|
||||
|
||||
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",
|
||||
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_checksum(hdr), src_addr_str, dst_addr_str, ipv4_hdr_get_opt_len(hdr));
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user