-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiftop_api.c
120 lines (100 loc) · 2.6 KB
/
iftop_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include "iftop_api.h"
#include "options.h"
extern options_t options ;
//pthread_mutex_t info_mutex;
//extern int write_interval; //����д�ļ�������info��ʱ����
time_t write_first = 0;
time_t write_last = 0;
/**********************************************************************************************************************
*Function
*********************************************************************************************************************/
/* 功能:写通信信息到文件
* 参数:
* 返回:
*/
FILE *check_write(void)
{
time_t cur_time = 0;
FILE *fp = NULL;
cur_time = time(NULL);
if (cur_time - write_last >= options.write_interval)
{
options.write_interval_timeout = 1;
if (options.write_communication_info != 0)
{
fp = fopen(CI_FILEPATH, FOPEN_OPTION);
//check error
if (fp == NULL)
{
//options.write_interval_timeout = 0; //重来
}
}
}
return fp;
}
/* 名称: write_info
* 功能: 写连接信息到文件
* 参数: fd: 文件流
* info: 需要的信息的结构体
* 返回: 成功0,失败...
*/
int write_info(FILE *fp, communication_info_s *info)
{
char info_str[300] = {0};
snprintf(info_str, sizeof(info_str), "%s:%d <==> %s:%d TX:%s RX:%s\n",
info->src_addr, info->src_port, info->dst_addr, info->dst_port, info->tx_total, info->rx_total);
fwrite(info_str, strlen(info_str), 1, fp);
//出错检测
return 0;
}
/* 名称: close_write
* 功能: 设置超时标志,关闭文件流
* 参数: fd: 文件流
* 返回: 无
*/
void close_write(FILE *fp)
{
if (options.write_interval_timeout != 0)
{
options.write_interval_timeout = 0;
write_last = time(NULL);
}
if (options.write_communication_info != 0 && fp != NULL)
{
fclose(fp);
}
}
/* 名称:in_port_list
* 功能:判断端口是否在列表中
* 参数:
* list: 列表
* list_len: 列表长度
* port: 端口号
* 返回: 在:1, 不在:0
*/
int in_port_list(int *list, int list_len, int port)
{
//-----------------
if (port == 0) {
return 0;
}
//-----------------
int i = 0;
for (i = 0; i < list_len; i++)
{
//printf("list[%d] = %d, port = %d\n", i, list[i], port);
if (list[i] == port)
{
return 1;
}
else if (list[i] == -1)
{
break;
}
}
return 0;
}