Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file open bug & update readme #520

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eBPF_Supermarket/Network_Subsystem/net_watch/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.output
.cache
tcpwatch
netwatch
8 changes: 7 additions & 1 deletion eBPF_Supermarket/Network_Subsystem/net_watch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ lmp现有许多用于监控linux网络协议栈相关信息的小工具,但这


### 1.2 功能介绍
`netwatch`是一款基于eBPF的网络检测工具,其旨在使用户方便快捷的获取主机环境下linux网络协议栈的各种信息。目前,其实现的功能包括:
`netwatch`是一款基于eBPF的网络检测工具,其旨在使用户方便快捷的获取主机环境下linux网络协议栈的各种信息。

目前,其实现的功能包括:
- TCP相关的信息监测:主机环境下对tcp/ip协议的分析,可以统计流量,延时,错误,链接信息等主要信息
- HTTP1/1.1相关信息检测:通过截取相应TCP包的HTTP头实现主机环境下对用户态http1的分析

TODO:
- [ ] HTTP1/1.1、HTTP2、HTTP3协议的全面监控
- [ ] 其他常用协议(UDP、ICMP等)相关信息的监控

### 1.3 组织结构
- netwatch.bpf.c:在各个内核探针点对TCP包信息、TCP连接信息以及各个包的HTTP1/1.1信息进行记录
- netwatch.c: 对bpf.c文件中记录的信息进行输出
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
packet{sock="0xffff9d1ecb3b9180",seq="3705894662",ack="522176002",mac_time="-",ip_time="-",tcp_time="-",http_info="GET / HTTP/1.1",rx="0"} 0
packet{sock="0xffff9d1ecb3b9180",seq="522176002",ack="3705894739",mac_time="-",ip_time="-",tcp_time="-",http_info="HTTP/1.1 200 OK",rx="1"} 0
Binary file not shown.
24 changes: 19 additions & 5 deletions eBPF_Supermarket/Network_Subsystem/net_watch/netwatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

static volatile bool exiting = false;

static char connects_file_path[1024];
static char err_file_path[1024];
static char packets_file_path[1024];

static int sport = 0, dport = 0; // for filter
static int all_conn = 0, err_packet = 0, extra_conn_info = 0, layer_time = 0,
http_info = 0, retrans_info = 0; // flag
Expand Down Expand Up @@ -104,7 +108,7 @@ static void bytes_to_str(char *str, unsigned long long num) {

static int print_conns(struct netwatch_bpf *skel) {

FILE *file = fopen("./data/connects.log", "w");
FILE *file = fopen(connects_file_path, "w+");
if (file == NULL) {
fprintf(stderr, "Failed to open connects.log: (%s)\n", strerror(errno));
return 0;
Expand Down Expand Up @@ -194,7 +198,7 @@ static int print_packet(void *ctx, void *packet_info, size_t size) {

const struct pack_t *pack_info = packet_info;
if (pack_info->err) {
FILE *file = fopen("./data/err.log", "a");
FILE *file = fopen(err_file_path, "a");
char reason[20];
if (pack_info->err == 1) {
printf("[X] invalid SEQ: sock = %p,seq= %u,ack = %u\n",
Expand All @@ -213,7 +217,7 @@ static int print_packet(void *ctx, void *packet_info, size_t size) {
pack_info->sock, pack_info->seq, pack_info->ack, reason);
fclose(file);
} else {
FILE *file = fopen("./data/packets.log", "a");
FILE *file = fopen(packets_file_path, "a");
char http_data[256];
if (strstr((char *)pack_info->data, "HTTP/1")) {
for (int i = 0; i < sizeof(pack_info->data); ++i) {
Expand Down Expand Up @@ -258,6 +262,16 @@ static int print_packet(void *ctx, void *packet_info, size_t size) {
}

int main(int argc, char **argv) {
char *last_slash = strrchr(argv[0], '/');
if (last_slash) {
*(last_slash+1) = '\0';
}
strcpy(connects_file_path, argv[0]);
strcpy(err_file_path, argv[0]);
strcpy(packets_file_path, argv[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要判断一下,避免输入路径为空的时候成了绝对路径

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argv[0]应该永远是exe文件的绝对路径吧

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,这里这样处理一般情况下都没问题的,除非程序就在根目录

strcat(connects_file_path, "data/connects.log");
strcat(err_file_path, "data/err.log");
strcat(packets_file_path, "data/packets.log");
struct ring_buffer *rb = NULL;
struct netwatch_bpf *skel;
int err;
Expand Down Expand Up @@ -312,13 +326,13 @@ int main(int argc, char **argv) {

printf("%-22s %-10s %-10s %-10s %-10s %-10s %-5s %s\n", "SOCK", "SEQ",
"ACK", "MAC_TIME", "IP_TIME", "TCP_TIME", "RX", "HTTP");
FILE *err_file = fopen("./data/err.log", "w");
FILE *err_file = fopen(err_file_path, "w+");
if (err_file == NULL) {
fprintf(stderr, "Failed to open err.log: (%s)\n", strerror(errno));
return 0;
}
fclose(err_file);
FILE *packet_file = fopen("./data/packets.log", "w");
FILE *packet_file = fopen(packets_file_path, "w+");
if (packet_file == NULL) {
fprintf(stderr, "Failed to open packets.log: (%s)\n", strerror(errno));
return 0;
Expand Down