Skip to Content
[CAIDA - Center for Applied Internet Data Analysis logo]
The Center for Applied Internet Data Analysis
cors2ascii.c
Go to the documentation of this file.
1 /*
2  * corsaro
3  *
4  * Alistair King, CAIDA, UC San Diego
5  * corsaro-info@caida.org
6  *
7  * Copyright (C) 2012 The Regents of the University of California.
8  *
9  * This file is part of corsaro.
10  *
11  * corsaro is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * corsaro is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with corsaro. If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #include "config.h"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "libtrace.h"
34 
35 #include "corsaro.h"
36 #include "corsaro_log.h"
37 #include "corsaro_io.h"
38 
48 static corsaro_in_t *corsaro = NULL;
49 
51 static corsaro_in_record_t *record = NULL;
52 
54 static void clean()
55 {
56  if(record != NULL)
57  {
58  corsaro_in_free_record(record);
59  record = NULL;
60  }
61 
62  if(corsaro != NULL)
63  {
64  corsaro_finalize_input(corsaro);
65  corsaro = NULL;
66  }
67 }
68 
70 static int init_corsaro(char *corsarouri)
71 {
72  /* get an corsaro_in object */
73  if((corsaro = corsaro_alloc_input(corsarouri)) == NULL)
74  {
75  fprintf(stderr, "could not alloc corsaro_in\n");
76  clean();
77  return -1;
78  }
79 
80  /* get a record */
81  if ((record = corsaro_in_alloc_record(corsaro)) == NULL) {
82  fprintf(stderr, "could not alloc record\n");
83  clean();
84  return -1;
85  }
86 
87  /* start corsaro */
88  if(corsaro_start_input(corsaro) != 0)
89  {
90  fprintf(stderr, "could not start corsaro\n");
91  clean();
92  return -1;
93  }
94 
95  return 0;
96 }
97 
99 static void usage(const char *name)
100 {
101  fprintf(stderr,
102  "usage: %s input_file\n", name);
103 }
104 
106 int main(int argc, char *argv[])
107 {
108  char *file = NULL;
109 
111  off_t len = 0;
112 
113  if(argc != 2)
114  {
115  usage(argv[0]);
116  exit(-1);
117  }
118 
119  /* argv[1] is the corsaro file */
120  file = argv[1];
121 
122  /* this must be done before corsaro_init_output */
123  if(init_corsaro(file) != 0)
124  {
125  fprintf(stderr, "failed to init corsaro\n");
126  clean();
127  return -1;
128  }
129 
130  while ((len = corsaro_in_read_record(corsaro, &type, record)) > 0) {
131  if(type == CORSARO_IN_RECORD_TYPE_NULL)
132  {
133  clean();
134  return -1;
135  }
136 
137  corsaro_io_print_record(corsaro->plugin_manager, type, record);
138 
139  /* reset the type to NULL to indicate we don't care */
141  }
142 
143  if(len < 0)
144  {
145  fprintf(stderr, "corsaro_in_read_record failed to read record\n");
146  clean();
147  return -1;
148  }
149 
150  clean();
151  return 0;
152 }
void corsaro_in_free_record(corsaro_in_record_t *record)
Free an corsaro record object.
Definition: corsaro.c:1542
int main(int argc, char *argv[])
Entry point for the cors2ascii tool.
Definition: cors2ascii.c:106
corsaro_in_t * corsaro_alloc_input(const char *corsarouri)
Allocate an corsaro object for reading an corsaro file.
Definition: corsaro.c:1414
Header file which exports the public libcorsaro API.
Header file dealing with the corsaro logging sub-system.
A reusable opaque structure for corsaro to read an input record into.
Definition: corsaro_int.h:350
corsaro_plugin_manager_t * plugin_manager
A pointer to the corsaro plugin manager state.
Definition: corsaro_int.h:336
static corsaro_in_record_t * record
The record object to read into.
Definition: cors2ascii.c:51
int corsaro_finalize_input(corsaro_in_t *corsaro)
Close the input file and free resources allocated by corsaro.
Definition: corsaro.c:1588
int corsaro_start_input(corsaro_in_t *corsaro)
Initialize an corsaro input object that has already been allocated.
Definition: corsaro.c:1428
int corsaro_io_print_record(corsaro_plugin_manager_t *plugin_manager, corsaro_in_record_type_t record_type, corsaro_in_record_t *record)
Print a generic corsaro record to stdout.
Definition: corsaro_io.c:1016
corsaro_in_record_t * corsaro_in_alloc_record(corsaro_in_t *corsaro)
Allocate a reusable corsaro record object.
Definition: corsaro.c:1514
Corsaro input state.
Definition: corsaro_int.h:323
static int init_corsaro(char *corsarouri)
Initialize a corsaro_in object for the given file name.
Definition: cors2ascii.c:70
Header file dealing with the corsaro file IO.
static void clean()
Cleanup and free state.
Definition: cors2ascii.c:54
The null type used for wildcard matching.
Definition: corsaro.h:100
Corsaro output state.
Definition: corsaro_int.h:230
off_t corsaro_in_read_record(corsaro_in_t *corsaro, corsaro_in_record_type_t *record_type, corsaro_in_record_t *record)
Read the next corsaro record from the given corsaro input file.
Definition: corsaro.c:1562
static void usage(const char *name)
Print usage information to stderr.
Definition: cors2ascii.c:99
enum corsaro_in_record_type corsaro_in_record_type_t
Corsaro input record types.