flowmon-http-plugins  1.0
FlowMon HTTP Input/Process/export plugins
 All Data Structures Files Functions Variables Typedefs Macros
flowmon-export-csv.c
Go to the documentation of this file.
1 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <stdbool.h>
44 #include <flowmonexp/plugin_export.h>
45 
47 #define PRINTV(format, args...) do {if (VERBOSE) { fprintf(stdout, format, ##args); fflush(stdout); } } while (0)
48 
49 #define PRINTD(format, args...) do {if (DEBUG) { fprintf(stdout, format, ##args); fflush(stdout); } } while (0)
50 
51 #define PRINTA(format, args...) do {if (VERBOSE || DEBUG) { fprintf(stdout, format, ##args); fflush(stdout); } } while (0)
52 
56 SET_PLUGIN_TYPE(PLUGIN_TYPE_EXPORT);
57 
59 extern int verbose_level;
60 
62 extern int debug_level;
63 
65 bool VERBOSE;
66 
68 bool DEBUG;
69 
73 typedef struct {
74 
76  int counter;
77 
79  flow_record_getter_t *getters;
80 
82  FILE *f;
83 
85 
87 static plugin_desc_t pcap_desc = {
88  "export-csv",
89  "Plugin which outputs all records to file in csv format\n"
90  "parameters :\n"
91  " file=/path/file file to write into\n"
92  " verbose=y/n verbose mode(default same as flowmonexp)\n"
93  " debug=y/n debug mode(default same as flowmonexp\n)",
94  0,
95  0
96 };
97 
102 PLUGIN_EXPORT_DESC {
103  return &pcap_desc;
104 }
105 
113 int fei_parse_params(char *params, plugin_private_csv *conf)
114 {
115  char *token, *value, *param;
116  uint8_t state = 0; /* state 0: expect param, state 1: expect value */
117 
118  /* Check that there are parameters */
119  if (params == NULL) {
120  fprintf(stderr, "input-http-flow need parameters\n");
121  return 1;
122  }
123 
124  /* Parse parameter string */
125  token = strtok(params, ",=");
126  while (token != NULL) {
127  if (state == 0) {
128  param = token;
129  } else {
130  value = token;
131  PRINTA("param: %s\t", param);
132  PRINTA("value: %s\n", value);
133  if (strcmp("file", param) == 0) {
134  conf->f = fopen(value, "a");
135  } else if (strcmp("verbose", param) == 0) {
136  if (strcmp(value, "y") == 0) {
137  VERBOSE = true;
138  } else if (strcmp(value, "n") == 0) {
139  VERBOSE = false;
140  } else {
141  fprintf(stderr, " value of 'verbose' parameter is not valid");
142  }
143  } else if (strcmp("debug", param) == 0) {
144  if (strcmp(value, "y") == 0) {
145  DEBUG = true;
146  } else if (strcmp(value, "n") == 0) {
147  DEBUG = false;
148  } else {
149  fprintf(stderr, " value of 'debug' parameter is not valid");
150  }
151  } else {
152  fprintf(stderr, "Unknown parameter '%s'\n", param);
153  return 1;
154  }
155  }
156 
157  /* Move to next token */
158  token = strtok(NULL, ",=");
159  state = 1 - state;
160  }
161  return 0;
162 }
163 
171 void *plugin_export_init(char *params, flow_record_getter_t *getter_list)
172 {
173  if (verbose_level >= 0) {
174  VERBOSE = true;
175  } else{
176  VERBOSE = false;
177  }
178  if (debug_level >= 0) {
179  DEBUG = true;
180  } else{
181  DEBUG = false;
182  }
183  PRINTA("plugin export init start\n");
184  plugin_private_csv *retval;
185  flow_record_getter_t *tmp = getter_list;
186 
187  retval = malloc(sizeof(plugin_private_csv));
188  if (!retval) {
189  return NULL;
190  }
191  fei_parse_params(params, retval);
192  retval->counter = 0;
193 
194  retval->getters = NULL;
195  while (tmp && tmp->name) {
196  PRINTD("getter: %s - %i\n", tmp->name, tmp->length);
197  getter_copy_to(&retval->getters, tmp);
198  tmp++;
199  }
200 
201  PRINTA("plugin export init end\n");
202  return retval;
203 }
204 
212 int plugin_export_export(void *plugin_private, flow_record_t *record)
213 {
214  PRINTD("plugin export export start\n");
215  plugin_private_csv *p = (plugin_private_csv *) plugin_private;
216  flow_record_getter_t *tmp = p->getters;
217  unsigned char block[1024];
218  int i;
219  int len;
220  FILE *output;
221 
222  output = p->f;
223  if (!output) {
224  fputs("Cant open output file.\n", stderr);
225  return 5;
226  }
227 
228  fprintf(output, "FLOW: %i, ", p->counter++);
229  while (tmp && tmp->name) {
230  if (tmp->valid(tmp->self, record)) {
231  fprintf(output, ", ");
232  len = tmp->current_length(tmp->self, record);
233 
234  if (len > 1024) {
235  len = 1024;
236  }
237 
238  fprintf(output, "%s: ", tmp->name);
239 
240  tmp->filler(tmp->self, record, block, len, 1);
241  if (tmp->length > 0) {
242  for (i = 0; i < len; i++) {
243  fprintf(output, "%02X", block[i]);
244  }
245  } else {
246  block[len] = 0;
247  printf("%s", block);
248  }
249  }
250  tmp++;
251  }
252  fprintf(output, "\n");
253  PRINTV("flow data written into file\n");
254  PRINTD("plugin export export end\n");
255  fflush(stdout);
256  return 0;
257 }
258 
265 int plugin_export_flush(void *plugin_private)
266 {
267  PRINTD("plugin flush start\n");
268  fflush(stdout);
269  PRINTD("plugin flush end\n");
270  return 0;
271 }