adding generic csv output module
This commit is contained in:
94
src/output_modules/module_csv.c
Normal file
94
src/output_modules/module_csv.c
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* ZMap Copyright 2013 Regents of the University of Michigan
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy
|
||||
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "../../lib/logger.h"
|
||||
#include "../fieldset.h"
|
||||
|
||||
#include "output_modules.h"
|
||||
|
||||
static FILE *file = NULL;
|
||||
|
||||
int csv_init(struct state_conf *conf, fielddefset_t *fds)
|
||||
{
|
||||
assert(conf);
|
||||
if (conf->output_filename) {
|
||||
if (!strcmp(conf->output_filename, "-")) {
|
||||
file = stdout;
|
||||
} else {
|
||||
if (!(file = fopen(conf->output_filename, "w"))) {
|
||||
log_fatal("csv", "could not open output file (%s)",
|
||||
conf->output_filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
// add output headers
|
||||
for (int i=0; i < fds->len; i++) {
|
||||
if (i) {
|
||||
fprintf(file, ", ");
|
||||
}
|
||||
fprintf(file, "%s", fds->fielddefs[i].name);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int csv_close(__attribute__((unused)) struct state_conf* c,
|
||||
__attribute__((unused)) struct state_send* s,
|
||||
__attribute__((unused)) struct state_recv* r)
|
||||
{
|
||||
if (file) {
|
||||
fflush(file);
|
||||
fclose(file);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static void hex_encode(FILE *f, unsigned char* readbuf, size_t len)
|
||||
{
|
||||
for(size_t i=0; i < len; i++) {
|
||||
fprintf(f, "%02x", readbuf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int csv_process(fieldset_t *fs)
|
||||
{
|
||||
for (int i=0; i < fs->len; i++) {
|
||||
field_t *f = &(fs->fields[i]);
|
||||
if (i) {
|
||||
fprintf(file, ", ");
|
||||
}
|
||||
if (f->type == FS_STRING) {
|
||||
fprintf(file, "%s", (char*) f->value);
|
||||
} else if (f->type == FS_UINT64) {
|
||||
fprintf(file, "%lu", (uint64_t) f->value);
|
||||
} else if (f->type == FS_BINARY) {
|
||||
hex_encode(file, (unsigned char*) f->value, f->len);
|
||||
}
|
||||
}
|
||||
fprintf(file, "\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
output_module_t module_csv_file = {
|
||||
.name = "csv",
|
||||
.init = &csv_init,
|
||||
.start = NULL,
|
||||
.update = NULL,
|
||||
.update_interval = 0,
|
||||
.close = &csv_close,
|
||||
.process_ip = &csv_process,
|
||||
};
|
||||
|
@ -6,7 +6,6 @@
|
||||
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -12,20 +12,21 @@
|
||||
|
||||
#include "output_modules.h"
|
||||
|
||||
extern output_module_t module_simple_file;
|
||||
extern output_module_t module_extended_file;
|
||||
//extern output_module_t module_simple_file;
|
||||
//extern output_module_t module_extended_file;
|
||||
extern output_module_t module_csv_file;
|
||||
// ADD YOUR MODULE HERE
|
||||
|
||||
#ifdef REDIS
|
||||
extern output_module_t module_redis;
|
||||
#endif
|
||||
|
||||
|
||||
output_module_t* output_modules[] = {
|
||||
&module_simple_file,
|
||||
&module_extended_file,
|
||||
&module_csv_file
|
||||
//&module_simple_file,
|
||||
//&module_extended_file,
|
||||
#ifdef REDIS
|
||||
&module_redis,
|
||||
//&module_redis,
|
||||
#endif
|
||||
// ADD YOUR MODULE HERE
|
||||
};
|
||||
@ -50,3 +51,4 @@ void print_output_modules(void)
|
||||
printf("%s\n", output_modules[i]->name);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,35 +10,31 @@
|
||||
#define HEADER_OUTPUT_MODULES_H
|
||||
|
||||
#include "../state.h"
|
||||
#include "../fieldset.h"
|
||||
|
||||
// called at scanner initialization
|
||||
typedef int (*output_init_cb)(struct state_conf *);
|
||||
typedef int (*output_init_cb)(struct state_conf *, fielddefset_t *fds);
|
||||
|
||||
// called on packet receipt
|
||||
typedef int (*output_packet_cb)(ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
|
||||
const char* response_type,
|
||||
int is_repeat, int in_cooldown,
|
||||
const u_char* packetbuf, size_t buflen);
|
||||
typedef int (*output_packet_cb)(fieldset_t *fs);
|
||||
|
||||
// called periodically during the scan
|
||||
typedef int (*output_update_cb)(struct state_conf*, struct state_send*, struct state_recv*);
|
||||
|
||||
typedef int (*output_update_cb)(struct state_conf*,
|
||||
struct state_send*, struct state_recv*);
|
||||
|
||||
typedef struct output_module {
|
||||
const char *name;
|
||||
unsigned update_interval;
|
||||
|
||||
output_init_cb init;
|
||||
output_update_cb start;
|
||||
output_update_cb update;
|
||||
output_update_cb close;
|
||||
output_packet_cb success_ip;
|
||||
output_packet_cb other_ip;
|
||||
|
||||
output_packet_cb process_ip;
|
||||
} output_module_t;
|
||||
|
||||
|
||||
output_module_t* get_output_module_by_name(const char*);
|
||||
|
||||
void print_output_modules(void);
|
||||
|
||||
#endif // HEADER_OUTPUT_MODULES_H
|
||||
|
Reference in New Issue
Block a user