在項目中我定義了一個結構體數組,頭文件以下:數據庫
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
|
#ifndef MY_TABLE_STRUCT_H
#define MY_TABLE_STRUCT_H
#define POLICY_UTIL_ARRAY_LENGTH 256*64
#define ARRAY_SIZE 256
long
saved_policy_id[10];
long
saved_udp_allow_IP_ID[10];
long
save_log_ID[10];
/*與日誌審計相關的文件路徑定義*/
#define SYSTEM_LOG "/tmp/log/system.log"
#define HTTP_LOG "/tmp/log/http.log"
#define SMTP_LOG "/tmp/log/smtp.log"
#define POP3_LOG "/tmp/log/pop3.log"
#define SYSTEM_INTERMEDIARY_LOG "/tmp/log/system_intermediary.log"
#define HTTP_INTERMEDIARY_LOG "/tmp/log/http_intermediary.log"
#define SMTP_INTERMEDIARY_LOG "/tmp/log/smtp_intermediary.log"
#define POP3_INTERMEDIARY_LOG "/tmp/log/pop3_intermediary.log"
typedef
struct
{
int
used;
//0 for unused
int
AppID;
/*
00 "系統",
01 "HTTP應用",
02 "SMTP應用",
03 "POP3應用",
04 "FTP應用",
05 "TELNET應用",
06 "NULL ",
07 "用戶自定義應用",
08 "TCP空白協議",
09 "UDP應用",
0a "ORACLE數據庫應用",
0b "SQL Server 數據庫應用",
0c "郵件服務器與郵件服務器應用",
*/
char
SourceIP[20];
short
SourcePort;
char
DestIP[20];
short
DestPort;
char
SourceType;
//I for inner;O for outter
char
IsDestProxyMode;
short
MaxConn;
char
IsWorking;
// 'Y' for "自動啓用,現啓用"
//'N' for "自動啓用,現停用"
//'U' for "手動啓用,現停用"
//'W' for "手動啓用,現啓用"
char
IsAuth;
// 0 for no auth
// 1 for auth
char
name[20];
UINT16 InnerPort;
//for udp
UINT16 OuterPort;
char
IsAllowed;
//安全策略黑白指示
// 'Y' for 白
// 'N' for 黑
char
FromTime[8][8];
char
ToTime[8][8];
char
WeekDay[8][8];
//for udp allowed ip
unsigned
int
inner_udp_ip[256];
unsigned
int
outer_udp_ip[256];
//for tcp allowed ip
struct
IP {
char
ip[255];
int
black_white;
} allowed_ip[256];
struct
POLICY {
long
p_id;
short
type;
char
IsWorking;
char
p_content[255];
char
IsAllowed;
} policy[64];
}TABLE_CHANNEL;
TABLE_CHANNEL table_channels[ARRAY_SIZE];
#endif
|
將結構體數組中的數據保存到文件中,實現函數以下:數組
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
void
chn_write_file()
{
FILE
*fp;
int
i;
DO_LOG(
"chn_write_file():write to the file start...\n"
);
fp=
fopen
(
"/tmp/data/db_file/tcp_file"
,
"wb"
);
if
(fp==NULL)
{
printf
(
"file /tmp/udp_chn open error\n"
);
return
;
}
//for(i=0;i<256;i++)
//fwrite(&table_channels[i],sizeof(TABLE_CHANNEL),1,fp);//把內存中的信息寫入到文件中去
fwrite
(table_channels,
sizeof
(TABLE_CHANNEL), 256, fp);
fclose
(fp);
DO_LOG(
"chn_write_file():write to the file end...\n"
);
}
|
從文件中讀取數據存儲到結構體數組中,實現函數以下:安全
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
void
chn_read_file()
{
MY_DEBUG_LOG(
"chn_read_file():read from the file start...\n"
);
DO_LOG(
"chn_read_file():read from the file start...\n"
);
FILE
*fp;
int
i, j;
fp=
fopen
(
"/tmp/data/db_file/tcp_file"
,
"rb"
);
if
(fp == NULL)
{
DO_LOG(
"chn_read_file():open file failed, because:%s!\n"
,
strerror
(
errno
));
return
;
}
/*for(i = 0; i< 256; i++)
{
fread(&table_channels[i], sizeof(TABLE_CHANNEL), 1, fp);
}*/
fread
(table_channels,
sizeof
(TABLE_CHANNEL), 256, fp);
/*****打印結構體數組中的數據*******/
MY_DEBUG_LOG(
"chn_read_file(): The informations of approuteways bellows:\n"
);
for
(i =0; i<256; i++)
{
if
(table_channels[i].used == 0)
{
continue
;
}
MY_DEBUG_LOG("[ReadFile]:ID:%d, used:%d, AppID:%d, SourceIP:%s, SourcePort:%d,\
DestIP:%s, DestPort:%d, SourceType:%c, IsDestProxyMode:%c, MaxConn:%d,\
IsWorking:%c, IsAuth:%c, InnerPort:%d, OuterPort:%d, IsAllowed:%c, name:%s!\n", \
i, table_channels[i].used, table_channels[i].AppID,table_channels[i].SourceIP,table_channels[i].SourcePort, \
table_channels[i].DestIP, table_channels[i].DestPort, table_channels[i].SourceType, table_channels[i].IsDestProxyMode,\
table_channels[i].MaxConn, table_channels[i].IsWorking,table_channels[i].IsAuth,table_channels[i].InnerPort,\
table_channels[i].OuterPort,table_channels[i].IsAllowed,table_channels[i].name);
}
MY_DEBUG_LOG(
"chn_read_file(): The informations of allowd time bellows:\n"
);
for
(i=0; i<256; i++)
{
if
(table_channels[i].used == 0)
{
continue
;
}
for
(j=0; j<8; j++)
{
if
(table_channels[i].FromTime[j][0] == 0)
{
continue
;
}
if
(table_channels[i].ToTime[j][0] == 0)
{
continue
;
}
if
(table_channels[i].WeekDay[j][0] == 0)
{
continue
;
}
MY_DEBUG_LOG(
"chn_read_file():ID:%d,FormTime:%s,ToTime:%s,WeekDay:%s\n"
,\
i,&table_channels[i].FromTime[j][0], &table_channels[i].ToTime[j][0], &table_channels[i].WeekDay[j][0]);
}
}
MY_DEBUG_LOG(
"chn_read_file(): The informations of allowd IPs bellows:\n"
);
for
(i=0; i<256; i++)
{
if
(table_channels[i].used == 0)
{
continue
;
}
for
(j=0; j<256; j++)
{
if
(table_channels[i].allowed_ip[j].ip[0] ==0)
{
continue
;
}
if
(table_channels[i].allowed_ip[j].black_white == 0)
{
MY_DEBUG_LOG(
"chn_read_file():ID:%d, ip %s, black_white:%d\n"
,
i, table_channels[i].allowed_ip[j].ip, table_channels[i].allowed_ip[j].black_white);
}
}
}
MY_DEBUG_LOG(
"chn_read_file(): The informations of allowd users bellows:\n"
);
for
(i=0; i<256; i++)
{
if
(table_channels[i].used == 0)
{
continue
;
}
for
(j=0; j<256; j++)
{
if
(table_channels[i].allowed_ip[j].ip[0] ==0)
{
continue
;
}
if
(table_channels[i].allowed_ip[j].black_white == 1)
{
MY_DEBUG_LOG(
"chn_read_file():ID:%d, users %s, black_white:%d\n"
,
i, table_channels[i].allowed_ip[j].ip, table_channels[i].allowed_ip[j].black_white);
}
}
}
MY_DEBUG_LOG(
"chn_read_file(): The informations of policies bellows:\n"
);
for
(i=0; i<256; i++)
{
if
(table_channels[i].used == 0)
{
continue
;
}
for
(j =0 ; j <64; j++)
{
if
(table_channels[i].policy[j].type == 0)
{
continue
;
}
MY_DEBUG_LOG(
"[ReadFile]:ID:%d, AppID:%d, used:%d, policy_id:%d , type:%x, IsWorking:%c, p_content:%s, IsAllowed:%c.\n"
,
i,table_channels[i].AppID,table_channels[i].used,table_channels[i].policy[j].p_id,table_channels[i].policy[j].type,
table_channels[i].policy[j].IsWorking,table_channels[i].policy[j].p_content, table_channels[i].policy[j].IsAllowed);
}
}
fclose
(fp);
MY_DEBUG_LOG(
"chn_read_file():read from the file start end...\n"
);
}
|
如今出現問題,請教網上各位高手:
在一個程序中能夠正確執行上面的兩個操做,並用日誌的形式打印數據,是正確的;但是在其它地方調用void chn_read_file()函數時候,並用日誌形式打印數據,卻顯示的是錯誤的數據(具體錯誤:allowed_ip結構體,和policy結構體中的數據是錯誤的,隨機生成的,也就是沒從文件中正確讀出來)函數我用的同樣,爲何會出現這種狀況?服務器