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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
data.c - counts the data exchanged
(c) 1999-2002 Robert Cheramy <tibob@via.ecp.fr>
(c) 2000 Samuel Hocevar <sam@via.ecp.fr>
(c) 1999 Andres Krapf <dae@via.ecp.fr>
(c) 2001 Loc Tortay & IN2P3 Computing Center <tortay@cc.ipin2p3.fr>
200010 : sam : - attempt to create directory if error opening logfile.
- optional clearing of data
200010 : tibob : optional append to logfile
200210 : tibob : fix for Solaris
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include "config.h"
#include "data.h"
#include "filter.h"
#include "init.h"
#include "utils.h"
extern struct OptionsType Options;
extern struct AllLogsType *pAllLogs;
extern char *device;
extern ipfm_timezone tz;
void data_add(struct AllLogsType *pLog, u_int32_t ip, int in, int out) {
struct ipfm_data *p_datatmp;
if (NULL == pLog->Data || pLog->Data->ip > ip) {
/* p_datatmp = p_data; */
p_datatmp = xmalloc(sizeof(struct ipfm_data));
p_datatmp->prev = NULL;
p_datatmp->next = pLog->Data;
p_datatmp->ip = ip;
p_datatmp->in = in;
p_datatmp->out = out;
if (pLog->Data != NULL) {
pLog->Data->prev = p_datatmp;
}
pLog->Data = p_datatmp;
pLog->DataSize++;
} else {
for (p_datatmp = pLog->Data;
(NULL != p_datatmp->next) && (p_datatmp->ip < ip);
p_datatmp = p_datatmp->next);
if (p_datatmp->ip == ip) {
p_datatmp->in += in;
p_datatmp->out += out;
} else if (NULL == p_datatmp->next && p_datatmp->ip < ip) {
p_datatmp->next = xmalloc(sizeof(struct ipfm_data));
p_datatmp->next->prev = p_datatmp;
p_datatmp->next->next = NULL;
p_datatmp->next->ip = ip;
p_datatmp->next->in = in;
p_datatmp->next->out = out;
pLog->DataSize++;
} else {
/* if (NULL == p_datatmp->next && p_datatmp->ip > ip) {
or p_datatmp->ip > ip && NULL != p_datatmp) */
p_datatmp->prev->next = xmalloc(sizeof(struct ipfm_data));
p_datatmp->prev->next->prev = p_datatmp->prev;
p_datatmp->prev->next->next = p_datatmp;
p_datatmp->prev->next->ip = ip;
p_datatmp->prev->next->in = in;
p_datatmp->prev->next->out = out;
p_datatmp->prev = p_datatmp->prev->next;
pLog->DataSize++;
}
}
}
void data_dump(struct AllLogsType *pLog) {
pid_t pid;
char *FileName;
FileName = timefile(pLog->LogFile, pLog->NextDump);
/* Avoid stdout conflicts between son and father */
fflush(stdout);
pid = fork();
if (-1 == pid) {
fprintf(stderr, "couldn't fork\n");
xfree(FileName);
return;
} else if (0 == pid) {
FILE *logfile;
char DataToFile[MAX_DATA_SIZE];
char *stringTime;
char *timezonestring;
if (tz == local) {
timezonestring = "local time";
} else {
timezonestring = "UTC";
}
DataSort(pLog);
if (1 == pLog->Append) {
/* append to file */
logfile = fopen(FileName, "a");
} else {
/* replace file */
logfile = fopen(FileName, "w");
}
/* If we failed to create the file, it may be due to a nonexistent
* directory (which causes ENOENT). we try to create the directory. */
if (NULL == logfile && ENOENT == errno) {
if (-1 != makedirname(FileName)) {
if (1 == pLog->Append) {
/* append to file */
logfile = fopen(FileName, "a");
} else {
/* replace file */
logfile = fopen(FileName, "w");
}
}
}
xfree(FileName);
/* Check again if we could open the file */
if (NULL == logfile) {
fprintf(stderr, "Error opening log file. Exiting.\n");
/* As under linux, pcap uses atexit to restore non promiscuous mode,
we use _exit() to avoid unsetting promiscuous mode when the child
exits
*/
_exit(1);
}
stringTime = timefile("%Y/%m/%d %H:%M:%S", pLog->NextDump);
fprintf(logfile, "# IPFMv%s %s (%s) -- dump every %ldd%02ld:%02ld:%02ld -- listening on %s\n",
VERSION, stringTime, timezonestring,
pLog->DumpInterval / (60*60*24),
(pLog->DumpInterval / (60*60)) % 24,
(pLog->DumpInterval / (60)) % 60,
pLog->DumpInterval % 60,
device);
fprintf(logfile, "# %-33s%15s%15s%15s\n",
"Host",
"In (bytes)",
"Out (bytes)",
"Total (bytes)");
while (NULL != pLog->Data) {
DataFormat(pLog, pLog->Data, DataToFile, MAX_DATA_SIZE);
fprintf(logfile, "%s", DataToFile);
/* Do not forget to free tables */
if (NULL != pLog->Data->next) {
pLog->Data = pLog->Data->next;
xfree(pLog->Data->prev);
pLog->Data->prev = NULL;
} else {
pLog->Data = NULL;
}
}
pLog->DataSize = 0;
fprintf(logfile, "# end of dump %s\n", stringTime);
xfree(stringTime);
if (1 == pLog->Append) {
fprintf(logfile, "\n");
}
fclose(logfile);
/* As under linux, pcap uses atexit to restore non promiscuous mode,
we use _exit() to avoid unsetting promiscuous mode when the child
exits
*/
_exit(0);
}
else {
xfree(FileName);
}
}
void data_clear(struct AllLogsType *pLog) {
while (NULL != pLog->Data)
{
if (NULL != pLog->Data->next) {
pLog->Data = pLog->Data->next;
xfree(pLog->Data->prev);
pLog->Data->prev = NULL;
} else {
xfree(pLog->Data);
pLog->Data = NULL;
}
}
pLog->DataSize = 0;
}
void DataSort(struct AllLogsType *pLog) {
if (pLog->Sort) {
int j;
int DataSize = pLog->DataSize;
struct ipfm_data *pTempData;
struct ipfm_data **pDataArray;
pDataArray = xmalloc (DataSize * sizeof(struct ipfm_data *));
j = 0;
for (pTempData = pLog->Data;
NULL != pTempData;
pTempData = pTempData->next)
pDataArray[j++] = pTempData;
if (DataSize) {
qsort(pDataArray, DataSize, sizeof(struct ipfm_data *), pLog->SortFunc);
for (j = 1; j < DataSize-1; j++) {
pDataArray[j]->next = pDataArray[j+1];
pDataArray[j]->prev = pDataArray[j-1];
}
if (1 < DataSize) {
pDataArray[0]->next = pDataArray[1];
pDataArray[DataSize-1]->prev = pDataArray[DataSize-2];
}
pDataArray[0]->prev = NULL;
pDataArray[DataSize-1]->next = NULL;
pLog->Data = pDataArray[0];
xfree(pDataArray);
}
}
}
int DataCompareOut(const void *ptr1, const void *ptr2) {
if ((**(struct ipfm_data **)ptr1).out > (**(struct ipfm_data **)ptr2).out)
return(-1);
else if ((**(struct ipfm_data **)ptr1).out < (**(struct ipfm_data **)ptr2).out)
return(1);
else
return(0);
}
int DataCompareIn(const void *ptr1, const void *ptr2) {
if ((**(struct ipfm_data **)ptr1).in > (**(struct ipfm_data **)ptr2).in)
return(-1);
else if ((**(struct ipfm_data **)ptr1).in < (**(struct ipfm_data **)ptr2).in)
return(1);
else
return(0);
}
int DataCompareTotal(const void *ptr1, const void *ptr2) {
if ((**(struct ipfm_data **)ptr1).out + (**(struct ipfm_data **)ptr1).in >
(**(struct ipfm_data **)ptr2).out + (**(struct ipfm_data **)ptr2).in)
return(-1);
else if ((**(struct ipfm_data **)ptr1).out + (**(struct ipfm_data **)ptr1).in <
(**(struct ipfm_data **)ptr2).out + (**(struct ipfm_data **)ptr2).in)
return(1);
else
return(0);
}
void DataFormat(struct AllLogsType *pLog, struct ipfm_data *pData,
char *pFormatedData, int BufLen) {
struct in_addr addr;
int NotLookedUp = 0;
struct hostent *he = NULL;
memset(pFormatedData, 0, BufLen);
addr.s_addr = pData->ip;
if (pLog->ReverseLookup) {
he = gethostbyaddr((char *) &addr, sizeof (struct in_addr), AF_INET);
if ((NULL != he) && (strlen(he->h_name) <= 34) &&
(strlen(he->h_name) > 0)) {
snprintf(pFormatedData, BufLen, "%-35s%15" LONGINTFORMAT "%15" LONGINTFORMAT "%15" LONGINTFORMAT "\n",
he->h_name,
pData->in,
pData->out,
pData->in + pData->out);
} else {
NotLookedUp = 1;
}
}
if (!pLog->ReverseLookup || NotLookedUp) {
char *res;
res = inet_ntoa(addr);
snprintf(pFormatedData, BufLen, "%-35s%15" LONGINTFORMAT "%15" LONGINTFORMAT "%15" LONGINTFORMAT "\n",
res,
pData->in,
pData->out,
pData->in + pData->out);
}
}
|