Skip to Content
[CAIDA - Cooperative Association for Internet Data Analysis logo]
The Cooperative Association for Internet Data Analysis
corsaro_file.h File Reference

Header file dealing with the low-level file IO. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  corsaro_file
 An opaque structure defining an corsaro output file. More...
struct  corsaro_file_in
 An opaque structure defining an corsaro input file. More...

Macros

#define CORSARO_FILE_COMPRESS_LEVEL_DEFAULT   6
 The default compression level.
#define CORSARO_FILE_ZLIB_SUFFIX   ".gz"
 The suffix used to detect gzip output is desired.
#define CORSARO_FILE_BZ2_SUFFIX   ".bz2"
 The suffix used to detect bzip output is desired.
#define CORSARO_FILE_MODE(file)   (file->mode)
 Accessor macro for getting the mode of a file.
#define state_wandio   mode_state.ms_wandio
 Shortcut to the non-trace (wandio) state structure.
#define wand_io   mode_state.ms_wandio.io
 Shortcut to a non-trace io object.
#define state_trace   mode_state.ms_trace
 Shortcut to the trace state structure.
#define trace_io   mode_state.ms_trace.trace
 Shortcut to the libtrace object.

Typedefs

typedef struct corsaro_file corsaro_file_t
 An opaque structure defining an corsaro output file.
typedef struct corsaro_file_in corsaro_file_in_t
 An opaque structure defining an corsaro input file.

Functions

Corsaro file output API functions

These are the functions that should be called by the plugins to open and write to files with the corsaro IO sub-system.

corsaro_file_compress_t corsaro_file_detect_compression (struct corsaro *corsaro, char *filename)
 Attempts to detect the type of compression for a file based on the suffix.
corsaro_file_tcorsaro_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.
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.
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_vprintf (struct corsaro *corsaro, corsaro_file_t *file, const char *format, va_list args)
 Print a string to an corsaro file.
off_t corsaro_file_printf (struct corsaro *corsaro, corsaro_file_t *file, const char *format,...)
 Print a string to an corsaro 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_close (struct corsaro *corsaro, corsaro_file_t *file)
 Closes an corsaro output file and frees the writer structure.
Corsaro file input API functions

These are the functions that should be called by the plugins to open and read from files with the corsaro IO sub-system.

Todo:
create a corsaro_file_rreadline function?
corsaro_file_in_tcorsaro_file_ropen (struct corsaro_in *corsaro, const char *filename)
 Creates a new corsaro file reader and opens the provided file for reading.
off_t corsaro_file_rread (struct corsaro_in *corsaro, corsaro_file_in_t *file, void *buffer, off_t len)
 Reads from an corsaro input file into the provided buffer.
off_t corsaro_file_rgets (struct corsaro_in *corsaro, corsaro_file_in_t *file, void *buffer, off_t len)
 Reads a string from an corsaro input file into the provided buffer.
off_t corsaro_file_rread_packet (struct corsaro_in *corsaro, corsaro_file_in_t *file, libtrace_packet_t *packet, uint16_t len)
 Read a libtrace packet from an corsaro input file.
off_t corsaro_file_rpeek (struct corsaro_in *corsaro, 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.
off_t corsaro_file_rseek (struct corsaro_in *corsaro, 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_rtell (struct corsaro_in *corsaro, corsaro_file_in_t *file)
 Returns the current offset of the read pointer for an corsaro input file.
void corsaro_file_rclose (struct corsaro_in *corsaro, corsaro_file_in_t *file)
 Closes an corsaro intput file and frees the reader structure.

Detailed Description

Header file dealing with the low-level file IO.

Author:
Alistair King

Definition in file corsaro_file.h.


Macro Definition Documentation

#define CORSARO_FILE_BZ2_SUFFIX   ".bz2"

The suffix used to detect bzip output is desired.

Definition at line 56 of file corsaro_file.h.

Referenced by corsaro_file_detect_compression().

#define CORSARO_FILE_COMPRESS_LEVEL_DEFAULT   6

The default compression level.

Todo:
make this an option to corsaro_main.c and corsaro_alloc_output

Definition at line 50 of file corsaro_file.h.

#define CORSARO_FILE_MODE (   file)    (file->mode)
#define CORSARO_FILE_ZLIB_SUFFIX   ".gz"

The suffix used to detect gzip output is desired.

Definition at line 53 of file corsaro_file.h.

Referenced by corsaro_file_detect_compression().

#define state_trace   mode_state.ms_trace

Shortcut to the trace state structure.

Definition at line 120 of file corsaro_file.h.

#define state_wandio   mode_state.ms_wandio

Shortcut to the non-trace (wandio) state structure.

Definition at line 115 of file corsaro_file.h.

#define trace_io   mode_state.ms_trace.trace

Shortcut to the libtrace object.

Definition at line 122 of file corsaro_file.h.

#define wand_io   mode_state.ms_wandio.io

Shortcut to a non-trace io object.

Definition at line 117 of file corsaro_file.h.


Typedef Documentation

An opaque structure defining an corsaro input file.

typedef struct corsaro_file corsaro_file_t

An opaque structure defining an corsaro output file.


Function Documentation

void corsaro_file_close ( struct corsaro corsaro,
corsaro_file_t file 
)

Closes an corsaro output file and frees the writer structure.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to close

Definition at line 259 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, CORSARO_FILE_MODE_TRACE, and corsaro_file::mode.

corsaro_file_compress_t corsaro_file_detect_compression ( struct corsaro corsaro,
char *  filename 
)

Attempts to detect the type of compression for a file based on the suffix.

Parameters:
corsaroThe corsaro object the file is associated with
filenameThe name of the file to check
Returns:
the compression type to use, -1 if an error occurs

Definition at line 52 of file corsaro_file.c.

References CORSARO_FILE_BZ2_SUFFIX, CORSARO_FILE_COMPRESS_BZ2, CORSARO_FILE_COMPRESS_NONE, CORSARO_FILE_COMPRESS_ZLIB, and CORSARO_FILE_ZLIB_SUFFIX.

void corsaro_file_flush ( struct corsaro corsaro,
corsaro_file_t file 
)

Force all buffered data for the file to be written out.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to flush

Definition at line 253 of file corsaro_file.c.

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.

Parameters:
corsaroThe corsaro object the file is associated with
filenameThe name of the file to open
modeThe corsaro output mode to use when writing
compress_typeCompression type
compress_levelThe compression level to use when writing
flagsFlags to apply when opening the file, e.g. O_CREATE
Returns:
A pointer to a new corsaro output file, or NULL if an error occurs

Definition at line 76 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, CORSARO_FILE_MODE_TRACE, and corsaro_file::mode.

off_t corsaro_file_printf ( struct corsaro corsaro,
corsaro_file_t file,
const char *  format,
  ... 
)

Print a string to an corsaro file.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to write to
formatThe format string to write
...The arguments to the format string
Returns:
The amount of data written, or -1 if an error occurs

The arguments for this function are the same as those for printf(3). See the printf(3) manpage for more details.

Definition at line 243 of file corsaro_file.c.

void corsaro_file_rclose ( struct corsaro_in corsaro,
corsaro_file_in_t file 
)

Closes an corsaro intput file and frees the reader structure.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to close

Definition at line 503 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, CORSARO_FILE_MODE_TRACE, and corsaro_file_in::mode.

off_t corsaro_file_rgets ( struct corsaro_in corsaro,
corsaro_file_in_t file,
void *  buffer,
off_t  len 
)

Reads a string from an corsaro input file into the provided buffer.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to read from
bufferThe buffer to read into
lenThe size of the buffer
Returns:
The amount of bytes read, 0 if EOF is reached, -1 if an error occurs

This function is almost identical to fgets(3), it will read at most one less than len bytes from the file and store them in buffer. Reading stops after an EOF or a newline. If a newline is read, it is stored in the buffer. A null byte will also be stored after the last character in the buffer.

Definition at line 388 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file_in::mode.

corsaro_file_in_t* corsaro_file_ropen ( struct corsaro_in corsaro,
const char *  filename 
)

Creates a new corsaro file reader and opens the provided file for reading.

Parameters:
corsaroThe corsaro object the file is associated with
filenameThe name of the file to open
Returns:
A pointer to a new corsaro input file, or NULL if an error occurs

This function will use wandio/libtrace to attempt to detect the compression format used for given file (if any), provided that libtrace was built with the appropriate libraries. It will also attempt to detect the mode that was used to write the file.

Definition at line 295 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, CORSARO_FILE_MODE_TRACE, and corsaro_file_in::mode.

off_t corsaro_file_rpeek ( struct corsaro_in corsaro,
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.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to read from
bufferThe buffer to read into
lenThe size of the buffer
Returns:
The amount of bytes read, 0 if EOF is reached, -1 if an error occurs

Definition at line 471 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file_in::mode.

off_t corsaro_file_rread ( struct corsaro_in corsaro,
corsaro_file_in_t file,
void *  buffer,
off_t  len 
)

Reads from an corsaro input file into the provided buffer.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to read from
bufferThe buffer to read into
lenThe size of the buffer
Returns:
The amount of bytes read, 0 if EOF is reached, -1 if an error occurs

Definition at line 377 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file_in::mode.

off_t corsaro_file_rread_packet ( struct corsaro_in corsaro,
corsaro_file_in_t file,
libtrace_packet_t *  packet,
uint16_t  len 
)

Read a libtrace packet from an corsaro input file.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to read from
packetThe packet to read into
lenThe size of the packet to be read
Returns:
The amount of bytes read, 0 if EOF is reached, -1 if an error occurs

This can be used on Corsaro Binary and Libtrace mode file to retrieve a single packet. If the file is in trace mode, the len parameter is ignored.

Definition at line 432 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, CORSARO_FILE_MODE_TRACE, and corsaro_file_in::mode.

off_t corsaro_file_rseek ( struct corsaro_in corsaro,
corsaro_file_in_t file,
off_t  offset,
int  whence 
)

Changes the read pointer offset to the specified value for an corsaro input file.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to adjust the read pointer for
offsetThe new offset for the read pointer
whenceIndicates where to set the read pointer from. Can be one of SEEK_SET, SEEK_CUR or SEEK_END.
Returns:
The new value for the read pointer, or -1 if an error occurs

The arguments for this function are the same as those for lseek(2). See the lseek(2) manpage for more details.

Definition at line 482 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file_in::mode.

off_t corsaro_file_rtell ( struct corsaro_in corsaro,
corsaro_file_in_t file 
)

Returns the current offset of the read pointer for an corsaro input file.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to get the read offset for
Returns:
The offset of the read pointer, or -1 if an error occurs

Definition at line 493 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file_in::mode.

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.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to write to
formatThe format string to write
argsThe arguments to the format string
Returns:
The amount of data written, or -1 if an error occurs

The arguments for this function are the same as those for vprintf(3). See the vprintf(3) manpage for more details.

Definition at line 231 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file::mode.

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.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to write the data to
bufferThe buffer to write out
lenThe amount of writable data in the buffer
Returns:
The amount of data written, or -1 if an error occurs

Definition at line 159 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, and corsaro_file::mode.

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.

Parameters:
corsaroThe corsaro object the file is associated with
fileThe file to write the packet to
packetThe packet to written
Returns:
The amount of bytes written, 0 if EOF is reached, -1 if an error occurs

This can be used on Corsaro Binary and Libtrace mode file to write a single packet.

Definition at line 170 of file corsaro_file.c.

References CORSARO_FILE_MODE_ASCII, CORSARO_FILE_MODE_BINARY, CORSARO_FILE_MODE_TRACE, and corsaro_file::mode.