|
|
|
|
@ -2,9 +2,35 @@
|
|
|
|
|
#include "../lib/debug_config.h" |
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <netinet/in.h> |
|
|
|
|
#include <arpa/inet.h> |
|
|
|
|
|
|
|
|
|
static const char* tcp_flags_to_str(uint8_t flags) { |
|
|
|
|
static char buf[32]; |
|
|
|
|
int pos = 0; |
|
|
|
|
buf[0] = '\0'; |
|
|
|
|
|
|
|
|
|
if (flags & 0x01) { pos += snprintf(buf + pos, sizeof(buf) - pos, "FIN,"); } |
|
|
|
|
if (flags & 0x02) { pos += snprintf(buf + pos, sizeof(buf) - pos, "SYN,"); } |
|
|
|
|
if (flags & 0x04) { pos += snprintf(buf + pos, sizeof(buf) - pos, "RST,"); } |
|
|
|
|
if (flags & 0x08) { pos += snprintf(buf + pos, sizeof(buf) - pos, "PSH,"); } |
|
|
|
|
if (flags & 0x10) { pos += snprintf(buf + pos, sizeof(buf) - pos, "ACK,"); } |
|
|
|
|
if (flags & 0x20) { pos += snprintf(buf + pos, sizeof(buf) - pos, "URG,"); } |
|
|
|
|
if (flags & 0x40) { pos += snprintf(buf + pos, sizeof(buf) - pos, "ECE,"); } |
|
|
|
|
if (flags & 0x80) { pos += snprintf(buf + pos, sizeof(buf) - pos, "CWR,"); } |
|
|
|
|
|
|
|
|
|
if (pos > 0 && buf[pos - 1] == ',') { |
|
|
|
|
buf[pos - 1] = '\0'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (buf[0] == '\0') { |
|
|
|
|
return "none"; |
|
|
|
|
} |
|
|
|
|
return buf; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char* dump_ip_packet_to_buffer(const uint8_t* data, size_t len) { |
|
|
|
|
static char buffer[512]; |
|
|
|
|
static char buffer[1024]; |
|
|
|
|
buffer[0] = '\0'; |
|
|
|
|
|
|
|
|
|
if (len < 20) { |
|
|
|
|
@ -38,19 +64,92 @@ char* dump_ip_packet_to_buffer(const uint8_t* data, size_t len) {
|
|
|
|
|
|
|
|
|
|
const char* proto_name = "UNKNOWN"; |
|
|
|
|
uint16_t src_port = 0, dst_port = 0; |
|
|
|
|
char proto_info[128] = {'\0'}; |
|
|
|
|
int is_special_proto = 0; |
|
|
|
|
|
|
|
|
|
if (protocol == 6 && len > ihl + 4) { |
|
|
|
|
if (protocol == 6 && len >= ihl + 20) { |
|
|
|
|
proto_name = "TCP"; |
|
|
|
|
src_port = (data[ihl] << 8) | data[ihl + 1]; |
|
|
|
|
dst_port = (data[ihl + 2] << 8) | data[ihl + 3]; |
|
|
|
|
} else if (protocol == 17 && len > ihl + 4) { |
|
|
|
|
|
|
|
|
|
uint32_t seq_num = ((uint32_t)data[ihl + 4] << 24) | |
|
|
|
|
((uint32_t)data[ihl + 5] << 16) | |
|
|
|
|
((uint32_t)data[ihl + 6] << 8) | |
|
|
|
|
((uint32_t)data[ihl + 7]); |
|
|
|
|
uint32_t ack_num = ((uint32_t)data[ihl + 8] << 24) | |
|
|
|
|
((uint32_t)data[ihl + 9] << 16) | |
|
|
|
|
((uint32_t)data[ihl + 10] << 8) | |
|
|
|
|
((uint32_t)data[ihl + 11]); |
|
|
|
|
uint8_t flags = data[ihl + 13]; |
|
|
|
|
uint16_t window = (data[ihl + 14] << 8) | data[ihl + 15]; |
|
|
|
|
|
|
|
|
|
snprintf(proto_info, sizeof(proto_info), "[%s] seq=%u ack=%u win=%u", |
|
|
|
|
tcp_flags_to_str(flags), seq_num, ack_num, window); |
|
|
|
|
is_special_proto = 1; |
|
|
|
|
|
|
|
|
|
} else if (protocol == 17 && len >= ihl + 8) { |
|
|
|
|
proto_name = "UDP"; |
|
|
|
|
src_port = (data[ihl] << 8) | data[ihl + 1]; |
|
|
|
|
dst_port = (data[ihl + 2] << 8) | data[ihl + 3]; |
|
|
|
|
} else if (protocol == 1) { |
|
|
|
|
uint16_t udp_len = (data[ihl + 4] << 8) | data[ihl + 5]; |
|
|
|
|
|
|
|
|
|
snprintf(proto_info, sizeof(proto_info), "len=%u", udp_len); |
|
|
|
|
is_special_proto = 1; |
|
|
|
|
|
|
|
|
|
} else if (protocol == 1 && len >= ihl + 8) { |
|
|
|
|
proto_name = "ICMP"; |
|
|
|
|
uint8_t icmp_type = data[ihl]; |
|
|
|
|
uint16_t icmp_id = (data[ihl + 4] << 8) | data[ihl + 5]; |
|
|
|
|
uint16_t icmp_seq = (data[ihl + 6] << 8) | data[ihl + 7]; |
|
|
|
|
if (icmp_type == 0) { |
|
|
|
|
snprintf(proto_info, sizeof(proto_info), "echo_reply seq=%u id=%u", icmp_seq, icmp_id); |
|
|
|
|
} else if (icmp_type == 8) { |
|
|
|
|
snprintf(proto_info, sizeof(proto_info), "echo_request seq=%u id=%u", icmp_seq, icmp_id); |
|
|
|
|
} else { |
|
|
|
|
snprintf(proto_info, sizeof(proto_info), "type=%u code=%u", icmp_type, data[ihl + 1]); |
|
|
|
|
} |
|
|
|
|
is_special_proto = 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (is_special_proto && proto_info[0] != '\0') { |
|
|
|
|
if (src_port > 0 && dst_port > 0) { |
|
|
|
|
int offset = snprintf(buffer, sizeof(buffer), "IPv4 %s %s:%d -> %s:%d (%zd bytes) %s", |
|
|
|
|
proto_name, src_str, src_port, dst_str, dst_port, len, proto_info); |
|
|
|
|
if (offset > 0 && offset < (int)sizeof(buffer) - 50) { |
|
|
|
|
snprintf(buffer + offset, sizeof(buffer) - offset, " data: " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x", |
|
|
|
|
data[ihl + 0], data[ihl + 1], data[ihl + 2], data[ihl + 3], |
|
|
|
|
data[ihl + 4], data[ihl + 5], data[ihl + 6], data[ihl + 7], |
|
|
|
|
data[ihl + 8], data[ihl + 9], data[ihl + 10], data[ihl + 11], |
|
|
|
|
data[ihl + 12], data[ihl + 13], data[ihl + 14], data[ihl + 15], |
|
|
|
|
data[ihl + 16], data[ihl + 17], data[ihl + 18], data[ihl + 19], |
|
|
|
|
data[ihl + 20], data[ihl + 21], data[ihl + 22], data[ihl + 23], |
|
|
|
|
data[ihl + 24], data[ihl + 25], data[ihl + 26], data[ihl + 27], |
|
|
|
|
data[ihl + 28], data[ihl + 29], data[ihl + 30], data[ihl + 31]); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
int offset = snprintf(buffer, sizeof(buffer), "IPv4 %s %s -> %s (%zd bytes) %s", |
|
|
|
|
proto_name, src_str, dst_str, len, proto_info); |
|
|
|
|
if (offset > 0 && offset < (int)sizeof(buffer) - 50) { |
|
|
|
|
snprintf(buffer + offset, sizeof(buffer) - offset, " data: " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x", |
|
|
|
|
data[ihl + 0], data[ihl + 1], data[ihl + 2], data[ihl + 3], |
|
|
|
|
data[ihl + 4], data[ihl + 5], data[ihl + 6], data[ihl + 7], |
|
|
|
|
data[ihl + 8], data[ihl + 9], data[ihl + 10], data[ihl + 11], |
|
|
|
|
data[ihl + 12], data[ihl + 13], data[ihl + 14], data[ihl + 15], |
|
|
|
|
data[ihl + 16], data[ihl + 17], data[ihl + 18], data[ihl + 19], |
|
|
|
|
data[ihl + 20], data[ihl + 21], data[ihl + 22], data[ihl + 23], |
|
|
|
|
data[ihl + 24], data[ihl + 25], data[ihl + 26], data[ihl + 27], |
|
|
|
|
data[ihl + 28], data[ihl + 29], data[ihl + 30], data[ihl + 31]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
int offset = snprintf(buffer, sizeof(buffer), "IPv4 %s %s:%d -> %s:%d (%zd bytes) data: " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
"%02x%02x%02x%02x%02x%02x%02x%02x " |
|
|
|
|
@ -65,10 +164,10 @@ char* dump_ip_packet_to_buffer(const uint8_t* data, size_t len) {
|
|
|
|
|
data[ihl + 20], data[ihl + 21], data[ihl + 22], data[ihl + 23], |
|
|
|
|
data[ihl + 24], data[ihl + 25], data[ihl + 26], data[ihl + 27], |
|
|
|
|
data[ihl + 28], data[ihl + 29], data[ihl + 30], data[ihl + 31]); |
|
|
|
|
|
|
|
|
|
if (offset > 0 && offset < (int)sizeof(buffer)) { |
|
|
|
|
buffer[offset] = '\0'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return buffer; |
|
|
|
|
} |
|
|
|
|
|