Написал я один поток ... В нем функция ... Собственно ниже представляю исходник ...
Дело в том, что с каждым выполнением функции count(); у меня процесс разрастается ровно на 8 кб чы байт ... Как это обмануть ? Где дырка ????
Вот усе:
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_fw.h>
#include <net/if.h>
#include <iostream.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sqlplus.hh>
#include <string.h>
#include <unistd.h>
#include "common.h"
#define SLP 1
int b; // mysql num rows
char ports_in[32];
char ports_out[32];
count_struct cnt; // traf in, out, ip ...
int rnum; // number of firewall rule
MYSQL_ROW row;
MYSQL_RES *spisok = NULL; // result from daemon
MYSQL fw_list;
int count();
void *svbil_ctc(void *param) {
while(1) {
count();
sleep(SLP);
}
}
int count() {
rnum = 0;
ports_in[0] = '\0';
ports_out[0] = '\0';
cnt.ip.ip_1[0] = '\0';
cnt.bytes_in = 0;
cnt.bytes_out = 0;
cnt.port_in[0][0] = '\0';
cnt.port_out[0][0] = '\0';
if (!mysql_connect(&fw_list, HOST, DBUSER, "")) {
return 1;
}
if (mysql_select_db(&fw_list, MDB) != 0) {
mysql_close(&fw_list);
return 1;
}
if (mysql_query(&fw_list, "select fw from fw_all") != 0) {
mysql_close(&fw_list);
return 1;
}
spisok = mysql_store_result(&fw_list);
if ( spisok == NULL ) {
mysql_free_result(spisok);
mysql_close(&fw_list);
return 1;
}
if ( mysql_num_fields(spisok) != 1 ) {
mysql_free_result(spisok);
cout << "Rows bigger ...\n";
return 1;
}
b = mysql_num_rows(spisok);
for ( int i = 0; i < b; i++ ) {
row = mysql_fetch_row(spisok);
rnum = atoi(row[0]);
check_ipfw(&cnt, rnum);
if (cnt.port_in[0][0] != '\0') {
// Порты есть ... входящие :)
sprintf(ports_in, "%s %s %s", cnt.port_in[0], cnt.port_in[1], cnt.port_in[2
} else { strcpy(ports_in, "any");
}
if (cnt.port_out[0][0] != '\0') {
// Порты есть ... выходящие :)
sprintf(ports_out, "%s %s %s", cnt.port_out[0], cnt.port_out[1], cnt.port_o
} else { strcpy(ports_out, "any");
}
cout << "IP_1: " << cnt.ip.ip_1 << "\nPORTS_IN: " <<
ports_in << " PORTS_OUT: " << ports_out << "\nTRAFFIC IN: " <<
cnt.bytes_in << " OUT: " << cnt.bytes_out << '\n';
}
mysql_free_result(spisok);
mysql_close(&fw_list);
return 0;
}
void check_ipfw(count_struct *check, int rnum) {
int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (s < 0) {
return;
}
struct ip_fw *rules;
void *data = NULL;
int num = 0; // To make gcc happy ;-)
// int nb;
// int nbytes;
int nalloc = sizeof(*rules);
short traf_mode;
int nbytes = nalloc;
while(nbytes >= nalloc) {
nalloc = nalloc * 2 + 200;
nbytes = nalloc;
if ((data = realloc(data, nbytes)) == NULL) {
return;
}
if (getsockopt(s, IPPROTO_IP, IP_FW_GET, data, (socklen_t *)&nbytes) < 0) {
return;
}
}
close(s); // closing open descriptor ...
rules = (struct ip_fw *)data;
while(rules[num].fw_number < 65535) num++; num++;
for (int n = 0; n < num; n++) {
if ( rules[n].fw_number > rnum ) {
break;
}
if ( rules[n].fw_number == rnum ) {
if ((rules[n].fw_flg & IP_FW_F_COMMAND) != IP_FW_F_COUNT) {
continue; // Это не правило счетчика
}
traf_mode = 0;
u_long adrt = ntohl(rules[n].fw_smsk.s_addr);
if (adrt == ULONG_MAX) {
traf_mode = 1; // Считаем исходящий траффик
} else traf_mode = 2;
if (rules[n].fw_flg & IP_FW_F_DME) {
// to any
if (traf_mode == 2) {
continue; // from any to any ->count<- !???
}
} else { adrt = ntohl(rules[n].fw_dmsk.s_addr);
if (adrt != ULONG_MAX) {
if (traf_mode == 2) continue; // from any to any
}
}
if (traf_mode == 2) {
sprintf(check->ip.ip_1, "%s", inet_ntoa(rules[n].fw_dst));
if (rules[n].fw_prot == IPPROTO_TCP || rules[n].fw_prot == IPPROTO_UDP) {
int nb = IP_FW_GETNDSTP(&rules[n]);
for (int i = 0; i < nb; i++) {
if ( i > 3 ) break; // 3 - maximum ports of count
sprintf(check->port_in[i], "%d", rules[n].fw_uar.fw_pts[i]);
}
}
check->bytes_in = rules[n].fw_pcnt;
}
if (traf_mode == 1) {
sprintf(check->ip.ip_2, "%s", inet_ntoa(rules[n].fw_src));
if (rules[n].fw_prot == IPPROTO_TCP || rules[n].fw_prot == IPPROTO_UDP) {
int nb = IP_FW_GETNDSTP(&rules[n]);
for (int i = 0; i < nb; i++) {
if ( i > 3 ) break; // 3 - maximum ports of count
sprintf(check->port_out[i], "%d", rules[n].fw_uar.fw_pts[i]);
}
}
check->bytes_out = rules[n].fw_pcnt;
}
}
}
}
Вот что в файле common.h :
#ifndef _COMMON_H_
#define _COMMON_H_
#include <sqlplus.hh>
#define gettime(a) \
strftime(a, 9, "%H:%M:%S", get_tm());
#define getdate(a) \
strftime(a, 11, "%Y-%m-%d", get_tm());
#define getfuldate(a) \
strftime(a, 20, "%Y-%m-%d %H:%M:%S", get_tm());
#define MDB "bil"
#define DBUSER "root"
#define HOST "localhost"
extern "C++" {
typedef union {
char ip_1[15];
char ip_2[15];
} svbil_ips;
typedef struct {
svbil_ips ip;
char port_in[3][8]; // input ports
char port_out[3][8]; // output ports
int bytes_in;
int bytes_out;
} count_struct;
struct tm *get_tm();
int get_var(char *to_buf, const char *ot_buf, const char *var_name, const int s_1);
MYSQL_RES *svbil_select(char *db, char *select_t);
void check_ipfw(count_struct *check, int rnum);
};
#endif
ПОМОГИТЕ ПОЖАЛУЙСТА .. Я В ОТЧАЯНИИ !!!
Best regards,
Vladislav.