-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
199 lines (157 loc) · 4.86 KB
/
util.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "util.h"
#ifdef __cplusplus
extern "C"{
#endif
#include "rule.h"
#include "stdio.h"
#include "time.h"
extern sqlite3 *sqlitedb;
#ifdef __cplusplus
}
#endif
Util::Util(){}
void Util::executeSQL (char *pSQL,QTableWidget* pTable, function func){
char* szErrMsg = 0;
int nRow = 0,nColumn = 0;
char **dbResult = 0;
int ret = sqlite3_get_table(sqlitedb, pSQL, &dbResult, &nRow, &nColumn, &szErrMsg);
fprintf(stderr,pSQL);
if(NULL != szErrMsg)
{
sqlite3_free_table(dbResult);
sqlite3_free (szErrMsg);
return;
}
//执行插入列表函数
(*func)(pTable,nRow,nColumn,dbResult);
//释放空间
// sqlite3_free_table(dbResult);
}
void Util::executeSQL (char *pSQL){
char* szErrMsg = 0;
int rc = sqlite3_exec(sqlitedb, pSQL, NULL, NULL, &szErrMsg);
if(rc != SQLITE_OK)
{
//fprintf(stderr,"%s",szErrMsg);
sqlite3_free(szErrMsg);
return;
}
}
void Util::insertTableRule (QTableWidget *pTableWidget, int nRow,int nCol,char **argv){
//列的索引
int rowIndex = 0;
//临时变量
QString strTmp;
while(rowIndex < nRow){
//先插入一列
pTableWidget->insertRow (rowIndex);
//ID
strTmp = QString("%1").arg (argv[(rowIndex + 1)*nCol + 0]);
pTableWidget->setItem(rowIndex,0,new QTableWidgetItem(strTmp));
//alarmtype
switch(atoi(argv[(rowIndex + 1)*nCol + 1])){
case ALARM_LOG:
strTmp = "LOG";
break;
case ALARM_ALERT:
strTmp = "ALERT";
break;
case ALARM_PASS:
strTmp = "PASS";
break;
case ALARM_AKILL:
strTmp = "KILL";
break;
}
pTableWidget->setItem (rowIndex,1,new QTableWidgetItem(strTmp));
//prototype
switch(atoi(argv[(rowIndex + 1)*nCol + 2])){
case PROTCP:
strTmp = "TCP";
break;
case PROUDP:
strTmp = "UDP";
break;
case PROICMP:
strTmp = "ICMP";
break;
case PROANY:
strTmp = "ANY";
break;
}
pTableWidget->setItem (rowIndex,2,new QTableWidgetItem(strTmp));
//保存地址
struct in_addr netaddr;
//src
unsigned int ipsrc = atoi(argv[(rowIndex + 1)*nCol + 3]);
netaddr.s_addr = ipsrc;
strTmp = QString("%1:%2/%3").arg (\
inet_ntoa (netaddr)).arg (argv[(rowIndex + 1)*nCol + 4]).arg (argv[(rowIndex + 1)*nCol + 5]);
pTableWidget->setItem (rowIndex,3,new QTableWidgetItem(strTmp));
//dst
unsigned int ipdst = atoi(argv[(rowIndex + 1)*nCol + 6]);
netaddr.s_addr = ipdst;
strTmp = QString("%1:%2/%3").arg (\
inet_ntoa (netaddr)).arg (argv[(rowIndex + 1)*nCol + 7]).arg (argv[(rowIndex + 1)*nCol + 8]);
pTableWidget->setItem (rowIndex,4,new QTableWidgetItem(strTmp));
//content
pTableWidget->setItem (rowIndex,5,new QTableWidgetItem(QString(argv[(rowIndex + 1)*nCol + 9])));
//errmsg
pTableWidget->setItem (rowIndex,6,new QTableWidgetItem(QString(argv[(rowIndex + 1)*nCol + 10])));
rowIndex ++;
}
}
void Util::insertTableNids(QTableWidget *pTableWidget, int nRow,int nCol,char **argv){
int rowIndex = 0;
QString strTmp;
while(rowIndex < nRow){
//先插入一列
pTableWidget->insertRow(rowIndex);
//获得时间
time_t time = atoi(argv[(rowIndex + 1)*nCol + 1]);
pTableWidget->setItem(rowIndex,0,new QTableWidgetItem(QString(ctime(&time))));
//alarmtype
switch(atoi(argv[(rowIndex + 1)*nCol + 2])){
case ALARM_LOG:
strTmp = "LOG";
break;
case ALARM_ALERT:
strTmp = "ALERT";
break;
case ALARM_PASS:
strTmp = "PASS";
break;
case ALARM_AKILL:
strTmp = "KILL";
break;
}
pTableWidget->setItem (rowIndex,1,new QTableWidgetItem(strTmp));
//prototype
switch(atoi(argv[(rowIndex + 1)*nCol + 3])){
case PROTCP:
strTmp = "TCP";
break;
case PROUDP:
strTmp = "UDP";
break;
case PROICMP:
strTmp = "ICMP";
break;
case PROANY:
strTmp = "ANY";
break;
}
pTableWidget->setItem (rowIndex,2,new QTableWidgetItem(strTmp));
for (int i = 3;i < 6; i++)
pTableWidget->setItem(rowIndex,i,new QTableWidgetItem(QString(argv[(rowIndex + 1)*nCol + i + 1])));
rowIndex ++;
}
}
void Util::setCombox (QComboBox* pCombox,QStringList itemList){
int count = itemList.count ();
int i = 0;
while(i < count){
pCombox->addItem (itemList.at (i));
i++;
}
}