clean log pcap

remotes/origin/master
Anoop Saldanha 14 years ago committed by Victor Julien
parent 6392202872
commit f1863370a5

@ -16,7 +16,8 @@
*/ */
/** \file /**
* \file
* *
* \author William Metcalf <William.Metcalf@gmail.com> * \author William Metcalf <William.Metcalf@gmail.com>
* *
@ -73,7 +74,6 @@
#define USE_STREAM_DEPTH_DISABLED 0 #define USE_STREAM_DEPTH_DISABLED 0
#define USE_STREAM_DEPTH_ENABLED 1 #define USE_STREAM_DEPTH_ENABLED 1
/*prototypes*/
TmEcode PcapLog(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *); TmEcode PcapLog(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *);
TmEcode PcapLogDataInit(ThreadVars *, void *, void **); TmEcode PcapLogDataInit(ThreadVars *, void *, void **);
TmEcode PcapLogDataDeinit(ThreadVars *, void *); TmEcode PcapLogDataDeinit(ThreadVars *, void *);
@ -114,7 +114,8 @@ typedef struct PcapLogData_ {
int PcapLogOpenFileCtx(PcapLogData *); int PcapLogOpenFileCtx(PcapLogData *);
void TmModulePcapLogRegister (void) { void TmModulePcapLogRegister(void)
{
tmm_modules[TMM_PCAPLOG].name = MODULE_NAME; tmm_modules[TMM_PCAPLOG].name = MODULE_NAME;
tmm_modules[TMM_PCAPLOG].ThreadInit = PcapLogDataInit; tmm_modules[TMM_PCAPLOG].ThreadInit = PcapLogDataInit;
tmm_modules[TMM_PCAPLOG].Func = PcapLog; tmm_modules[TMM_PCAPLOG].Func = PcapLog;
@ -122,6 +123,8 @@ void TmModulePcapLogRegister (void) {
tmm_modules[TMM_PCAPLOG].RegisterTests = NULL; tmm_modules[TMM_PCAPLOG].RegisterTests = NULL;
OutputRegisterModule(MODULE_NAME, "pcap-log", PcapLogInitCtx); OutputRegisterModule(MODULE_NAME, "pcap-log", PcapLogInitCtx);
return;
} }
/** /**
@ -130,7 +133,8 @@ void TmModulePcapLogRegister (void) {
* \param t Thread Variable containing input/output queue, cpu affinity etc. * \param t Thread Variable containing input/output queue, cpu affinity etc.
* \param pl PcapLog thread variable. * \param pl PcapLog thread variable.
*/ */
int PcapLogCloseFile(ThreadVars *t, PcapLogData *pl) { int PcapLogCloseFile(ThreadVars *t, PcapLogData *pl)
{
if (pl != NULL) { if (pl != NULL) {
if (pl->pcap_dumper != NULL) if (pl->pcap_dumper != NULL)
pcap_dump_close(pl->pcap_dumper); pcap_dump_close(pl->pcap_dumper);
@ -141,10 +145,12 @@ int PcapLogCloseFile(ThreadVars *t, PcapLogData *pl) {
pcap_close(pl->pcap_dead_handle); pcap_close(pl->pcap_dead_handle);
pl->pcap_dead_handle = NULL; pl->pcap_dead_handle = NULL;
} }
return 0; return 0;
} }
static void PcapFileNameFree(PcapFileName *pf) { static void PcapFileNameFree(PcapFileName *pf)
{
if (pf != NULL) { if (pf != NULL) {
if (pf->filename != NULL) { if (pf->filename != NULL) {
SCFree(pf->filename); SCFree(pf->filename);
@ -154,6 +160,8 @@ static void PcapFileNameFree(PcapFileName *pf) {
} }
SCFree(pf); SCFree(pf);
} }
return;
} }
/** /**
@ -165,8 +173,8 @@ static void PcapFileNameFree(PcapFileName *pf) {
* \retval 0 on succces * \retval 0 on succces
* \retval -1 on failure * \retval -1 on failure
*/ */
int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl) { int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl)
{
PcapFileName *pf; PcapFileName *pf;
PcapFileName *pfnext; PcapFileName *pfnext;
@ -189,7 +197,6 @@ int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl) {
/* Remove directory if Sguil mode and no files left in sguil dir */ /* Remove directory if Sguil mode and no files left in sguil dir */
if (pl->mode == LOGMODE_SGUIL) { if (pl->mode == LOGMODE_SGUIL) {
pfnext = TAILQ_NEXT(pf,next); pfnext = TAILQ_NEXT(pf,next);
if (strcmp(pf->dirname, pfnext->dirname) == 0) { if (strcmp(pf->dirname, pfnext->dirname) == 0) {
@ -219,8 +226,8 @@ int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl) {
SCLogError(SC_ERR_FOPEN, "opening new pcap log file failed"); SCLogError(SC_ERR_FOPEN, "opening new pcap log file failed");
return -1; return -1;
} }
pl->file_cnt++; pl->file_cnt++;
return 0; return 0;
} }
@ -236,7 +243,8 @@ int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl) {
* \retval TM_ECODE_OK on succes * \retval TM_ECODE_OK on succes
* \retval TM_ECODE_FAILED on serious error * \retval TM_ECODE_FAILED on serious error
*/ */
TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq) TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq,
PacketQueue *postpq)
{ {
size_t len; size_t len;
int rotate = 0; int rotate = 0;
@ -245,7 +253,8 @@ TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQu
PcapLogData *pl = (PcapLogData *)data; PcapLogData *pl = (PcapLogData *)data;
if (p->flags & PKT_PSEUDO_STREAM_END || if (p->flags & PKT_PSEUDO_STREAM_END ||
((p->flags & PKT_STREAM_NOPCAPLOG) && (pl->use_stream_depth == USE_STREAM_DEPTH_ENABLED)) || ((p->flags & PKT_STREAM_NOPCAPLOG) &&
(pl->use_stream_depth == USE_STREAM_DEPTH_ENABLED)) ||
(IS_TUNNEL_PKT(p) && !IS_TUNNEL_ROOT_PKT(p))) (IS_TUNNEL_PKT(p) && !IS_TUNNEL_ROOT_PKT(p)))
{ {
return TM_ECODE_OK; return TM_ECODE_OK;
@ -279,8 +288,7 @@ TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQu
} }
if ((pl->size_current + len) > pl->size_limit || rotate) { if ((pl->size_current + len) > pl->size_limit || rotate) {
if (PcapLogRotateFile(t,pl) < 0) if (PcapLogRotateFile(t,pl) < 0) {
{
SCMutexUnlock(&pl->plog_lock); SCMutexUnlock(&pl->plog_lock);
SCLogDebug("rotation of pcap failed"); SCLogDebug("rotation of pcap failed");
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
@ -293,8 +301,7 @@ TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQu
SCLogDebug("Setting pcap-log link type to %u", p->datalink); SCLogDebug("Setting pcap-log link type to %u", p->datalink);
if ((pl->pcap_dead_handle = pcap_open_dead(p->datalink, if ((pl->pcap_dead_handle = pcap_open_dead(p->datalink,
LIBPCAP_SNAPLEN)) == NULL) LIBPCAP_SNAPLEN)) == NULL) {
{
SCLogDebug("Error opening dead pcap handle"); SCLogDebug("Error opening dead pcap handle");
SCMutexUnlock(&pl->plog_lock); SCMutexUnlock(&pl->plog_lock);
@ -305,8 +312,7 @@ TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQu
* handle here */ * handle here */
if (pl->pcap_dumper == NULL) { if (pl->pcap_dumper == NULL) {
if ((pl->pcap_dumper = pcap_dump_open(pl->pcap_dead_handle, if ((pl->pcap_dumper = pcap_dump_open(pl->pcap_dead_handle,
pl->filename)) == NULL) pl->filename)) == NULL) {
{
SCLogInfo("Error opening dump file %s", pcap_geterr(pl->pcap_dead_handle)); SCLogInfo("Error opening dump file %s", pcap_geterr(pl->pcap_dead_handle));
SCMutexUnlock(&pl->plog_lock); SCMutexUnlock(&pl->plog_lock);
@ -316,7 +322,8 @@ TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQu
pcap_dump((u_char *)pl->pcap_dumper, pl->h, GET_PKT_DATA(p)); pcap_dump((u_char *)pl->pcap_dumper, pl->h, GET_PKT_DATA(p));
pl->size_current += len; pl->size_current += len;
SCLogDebug("pl->size_current %"PRIu64", pl->size_limit %"PRIu64, pl->size_current, pl->size_limit); SCLogDebug("pl->size_current %"PRIu64", pl->size_limit %"PRIu64,
pl->size_current, pl->size_limit);
SCMutexUnlock(&pl->plog_lock); SCMutexUnlock(&pl->plog_lock);
return TM_ECODE_OK; return TM_ECODE_OK;
@ -324,8 +331,7 @@ TmEcode PcapLog (ThreadVars *t, Packet *p, void *data, PacketQueue *pq, PacketQu
TmEcode PcapLogDataInit(ThreadVars *t, void *initdata, void **data) TmEcode PcapLogDataInit(ThreadVars *t, void *initdata, void **data)
{ {
if (initdata == NULL) if (initdata == NULL) {
{
SCLogDebug("Error getting context for PcapLog. \"initdata\" argument NULL"); SCLogDebug("Error getting context for PcapLog. \"initdata\" argument NULL");
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
} }
@ -506,13 +512,13 @@ OutputCtx *PcapLogInitCtx(ConfNode *conf)
const char *max_number_of_files_s = NULL; const char *max_number_of_files_s = NULL;
max_number_of_files_s = ConfNodeLookupChildValue(conf, "max-files"); max_number_of_files_s = ConfNodeLookupChildValue(conf, "max-files");
if (max_number_of_files_s != NULL) { if (max_number_of_files_s != NULL) {
if (ByteExtractStringUint32(&max_file_limit, 10, 0, max_number_of_files_s) == -1) { if (ByteExtractStringUint32(&max_file_limit, 10, 0,
SCLogError(SC_ERR_INVALID_ARGUMENT, max_number_of_files_s) == -1) {
"Failed to initialize pcap-log output, invalid number of files limit: %s", SCLogError(SC_ERR_INVALID_ARGUMENT, "Failed to initialize "
"pcap-log output, invalid number of files limit: %s",
max_number_of_files_s); max_number_of_files_s);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} } else if (max_file_limit < 1) {
else if (max_file_limit < 1) {
SCLogError(SC_ERR_INVALID_ARGUMENT, SCLogError(SC_ERR_INVALID_ARGUMENT,
"Failed to initialize pcap-log output, limit less than " "Failed to initialize pcap-log output, limit less than "
"allowed minimum."); "allowed minimum.");
@ -636,22 +642,27 @@ int PcapLogOpenFileCtx(PcapLogData *pl)
(void)mkdir(dirfull); (void)mkdir(dirfull);
#endif #endif
if ((pf->dirname = SCStrdup(dirfull)) == NULL) { if ((pf->dirname = SCStrdup(dirfull)) == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for directory name"); SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for "
"directory name");
goto error; goto error;
} }
if (pl->timestamp_format == TS_FORMAT_SEC) { if (pl->timestamp_format == TS_FORMAT_SEC) {
snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32, dirfull, pl->prefix, (uint32_t)ts.tv_sec); snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32, dirfull,
pl->prefix, (uint32_t)ts.tv_sec);
} else { } else {
snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32, dirfull, pl->prefix, (uint32_t)ts.tv_sec, (uint32_t)ts.tv_usec); snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32,
dirfull, pl->prefix, (uint32_t)ts.tv_sec, (uint32_t)ts.tv_usec);
} }
} else { } else {
/* create the filename to use */ /* create the filename to use */
if (pl->timestamp_format == TS_FORMAT_SEC) { if (pl->timestamp_format == TS_FORMAT_SEC) {
snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32, pl->dir, pl->prefix, (uint32_t)ts.tv_sec); snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32, pl->dir,
pl->prefix, (uint32_t)ts.tv_sec);
} else { } else {
snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32, pl->dir, pl->prefix, (uint32_t)ts.tv_sec, (uint32_t)ts.tv_usec); snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32, pl->dir,
pl->prefix, (uint32_t)ts.tv_sec, (uint32_t)ts.tv_usec);
} }
} }

Loading…
Cancel
Save