38 #include "wandio_utils.h"
44 #if (defined (__SVR4) && defined (__sun))
45 extern int vasprintf(
char **,
const char *, __va_list);
49 #define CORSARO_FILE_ASCII_CHECK "# CORSARO"
52 #define CORSARO_FILE_TRACE_FORMAT "pcapfile:"
56 return wandio_detect_compression_type(filename);
68 size_t flen, rlen, len;
73 corsaro_log(__func__, corsaro,
"could not malloc new corsaro_file_t");
83 flen = strlen(CORSARO_FILE_TRACE_FORMAT);
84 rlen = strlen(filename);
86 if((ptr = traceuri = malloc(len)) == NULL)
88 corsaro_log(__func__, corsaro,
"could not malloc traceuri");
91 strncpy(traceuri, CORSARO_FILE_TRACE_FORMAT, flen);
93 strncpy(ptr, filename, rlen);
94 traceuri[len-1] =
'\0';
95 f->trace_io = trace_create_output(traceuri);
98 if (trace_is_err_output(f->trace_io))
100 corsaro_log(__func__, corsaro,
"trace_create_output failed for %s",
104 if(trace_config_output(f->trace_io, TRACE_OPTION_OUTPUT_COMPRESS,
106 trace_config_output(f->trace_io, TRACE_OPTION_OUTPUT_COMPRESSTYPE,
107 &compress_type) != 0)
110 "could not set compression levels for trace");
113 if (trace_start_output(f->trace_io) == -1) {
114 corsaro_log(__func__, corsaro,
"trace_start_output failed for %s",
123 if((f->wand_io = wandio_wcreate(filename, compress_type,
124 compress_level, flags)) == NULL)
126 corsaro_log(__func__, corsaro,
"wandio could not create file %s",
134 corsaro_log(__func__, corsaro,
"invalid file mode %d", mode);
149 assert(file->wand_io != NULL);
151 return wandio_wwrite(file->wand_io, buffer, len);
157 uint8_t *pkt_buf = NULL;
158 libtrace_linktype_t linktype;
163 assert(file->wand_io != NULL);
164 #ifdef HAVE_LIBPACKETDUMP
166 "libpacketdump currently does not support dumping "
171 "corsaro must be built with libpacketdump to dump "
172 "a packet to ASCII");
178 assert(file->wand_io != NULL);
179 if((pkt_buf = trace_get_packet_buffer(packet,
180 &linktype, NULL)) == NULL)
182 corsaro_log(__func__, corsaro,
"could not get packet buffer");
186 trace_get_capture_length(packet));
189 assert(file->trace_io != NULL);
190 return trace_write_packet(file->trace_io, packet);
193 corsaro_log(__func__, corsaro,
"invalid corsaro file mode %d", file->
mode);
201 const char *format, va_list args)
204 assert(file != NULL);
208 assert(file->wand_io != NULL);
210 return wandio_vprintf(file->wand_io, format, args);
219 va_start(ap, format);
237 assert(file->wand_io != NULL);
238 wandio_wdestroy(file->wand_io);
239 file->wand_io = NULL;
243 assert(file->trace_io != NULL);
244 trace_destroy_output(file->trace_io);
245 file->trace_io = NULL;
276 if(strchr(filename,
':') != NULL)
281 f->trace_io = trace_create(filename);
283 if(trace_is_err(f->trace_io))
289 if (trace_start(f->trace_io) == -1) {
299 if((f->wand_io = wandio_create(filename)) == NULL)
305 len = wandio_peek(f->wand_io, buffer,
sizeof(buffer));
308 if(len >= strlen(CORSARO_FILE_ASCII_CHECK) &&
309 memcmp(CORSARO_FILE_ASCII_CHECK, buffer,
310 strlen(CORSARO_FILE_ASCII_CHECK)) == 0)
317 else if(len >= 4 && buffer[0] ==
'E' && buffer[1] ==
'D' &&
318 buffer[2] ==
'G' && buffer[3] ==
'R')
338 assert(file->wand_io != NULL);
340 return wandio_read(file->wand_io, buffer, len);
346 assert(file != NULL);
350 assert(file->wand_io != NULL);
353 return wandio_fgets(file->wand_io, buffer, len, 0);
357 libtrace_packet_t *
packet,
365 if((pktbuf = malloc(len)) == NULL)
367 fprintf(stderr,
"could not malloc the packet buffer\n");
370 if(wandio_read(file->wand_io, pktbuf, len) != len)
372 fprintf(stderr,
"could not read packet into buffer\n");
375 trace_construct_packet(packet, TRACE_TYPE_ETH,
381 return trace_read_packet(file->trace_io, packet);
401 assert(file->wand_io != NULL);
403 return wandio_peek(file->wand_io, buffer, len);
412 assert(file->wand_io != NULL);
414 return wandio_seek(file->wand_io, offset, whence);
423 assert(file->wand_io != NULL);
425 return wandio_tell(file->wand_io);
436 if(file->wand_io != NULL)
438 wandio_destroy(file->wand_io);
439 file->wand_io = NULL;
442 file->trace_io = NULL;
446 if(file->trace_io != NULL)
448 trace_destroy(file->trace_io);
449 file->trace_io = NULL;
452 file->wand_io = NULL;
An opaque structure defining an corsaro input file.
off_t corsaro_file_rgets(corsaro_file_in_t *file, void *buffer, off_t len)
Reads a string from an corsaro input file into the provided buffer.
Header file dealing with the corsaro logging sub-system.
An opaque structure defining an corsaro output file.
off_t corsaro_file_printf(struct corsaro *corsaro, corsaro_file_t *file, const char *format,...)
Print a string to an corsaro file.
off_t corsaro_file_rtell(corsaro_file_in_t *file)
Returns the current offset of the read pointer for an corsaro input file.
off_t corsaro_file_write_packet(struct corsaro *corsaro, corsaro_file_t *file, libtrace_packet_t *packet)
Write a libtrace packet to an corsaro output file.
off_t corsaro_file_rpeek(corsaro_file_in_t *file, void *buffer, off_t len)
Reads from an corsaro input file into the provided buffer, but does not update the read pointer...
void corsaro_file_close(struct corsaro *corsaro, corsaro_file_t *file)
Closes an corsaro output file and frees the writer structure.
static libtrace_packet_t * packet
A pointer to a libtrace packet.
corsaro_file_mode_t mode
The requested output format for the file.
void corsaro_file_flush(struct corsaro *corsaro, corsaro_file_t *file)
Force all buffered data for the file to be written out.
void corsaro_file_rclose(corsaro_file_in_t *file)
Closes an corsaro input file and frees the reader structure.
Header file dealing with the low-level file IO.
off_t corsaro_file_vprintf(struct corsaro *corsaro, corsaro_file_t *file, const char *format, va_list args)
Print a string to an corsaro file.
Pseudo IO mode which allows trace files to be written.
enum corsaro_file_compress corsaro_file_compress_t
Supported compression types (must be kept in sync with wandio)
off_t corsaro_file_rseek(corsaro_file_in_t *file, off_t offset, int whence)
Changes the read pointer offset to the specified value for an corsaro input file. ...
off_t corsaro_file_rread(corsaro_file_in_t *file, void *buffer, off_t len)
Reads from an corsaro input file into the provided buffer.
off_t corsaro_file_write(struct corsaro *corsaro, corsaro_file_t *file, const void *buffer, off_t len)
Writes the contents of a buffer using an corsaro output file.
corsaro_file_mode_t mode
The requested/detected input format for the file.
corsaro_file_t * corsaro_file_open(struct corsaro *corsaro, const char *filename, corsaro_file_mode_t mode, corsaro_file_compress_t compress_type, int compress_level, int flags)
Creates a new corsaro file write and opens the provided file for writing.
corsaro_file_compress_t corsaro_file_detect_compression(char *filename)
Attempts to detect the type of compression for a file based on the suffix.
corsaro_file_in_t * corsaro_file_ropen(const char *filename)
Creates a new corsaro file reader and opens the provided file for reading.
void corsaro_log(const char *func, corsaro_t *corsaro, const char *format,...)
Write a formatted string to the logfile associated with an corsaro object.
enum corsaro_file_mode corsaro_file_mode_t
Enum of supported file modes.
Header file dealing with the internal corsaro functions.
off_t corsaro_file_rread_packet(corsaro_file_in_t *file, libtrace_packet_t *packet, uint16_t len)
Read a libtrace packet from an corsaro input file.