Libtrap: Internal development docs  1.16.1
Data Structures | Macros | Typedefs | Functions | Variables
trap.c File Reference

TRAP library base. More...

#include <config.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <inttypes.h>
#include <assert.h>
#include <poll.h>
#include "../include/libtrap/trap.h"
#include "trap_internal.h"
#include "trap_error.h"
#include "trap_ifc.h"
#include "ifc_dummy.h"
#include "ifc_tcpip.h"
#include "ifc_tcpip_internal.h"
#include "ifc_file.h"
Include dependency graph for trap.c:

Go to the source code of this file.

Data Structures

struct  msg_header_s
 

Macros

#define _GNU_SOURCE
 
#define ifcdir2str(type)   (((type) == TRAPIFC_OUTPUT) ? "Output" : "Input")
 
#define SEND_DATA()
 
#define X(param, text, align, cut)
 
#define X(val)
 
#define X(val)
 
#define X(val)
 

Typedefs

typedef struct msg_header_s msg_header_t
 

Functions

const char trap_version [] __attribute__ ((used))
 
static char * get_param_by_delimiter (const char *source, char **dest, const char delimiter)
 
void * service_thread_routine (void *arg)
 
void trap_check_global_vars (void)
 
void trap_free_global_vars (void)
 
trap_module_info_t * trap_create_module_info (const char *mname, const char *mdesc, int8_t i_ifcs, int8_t o_ifcs, uint16_t param_count)
 
int trap_update_module_param (trap_module_info_t *m, uint16_t param_id, char shortopt, const char *longopt, const char *desc, int req_arg, const char *arg_type)
 
int trap_check_buffer_content (void *buffer, uint32_t buffer_size)
 Check content of buffer, iterate over message headers. More...
 
static int trap_read_from_buffer (trap_ctx_priv_t *ctx, uint32_t ifc_idx, const void **data, uint16_t *size, int timeout)
 
void trap_set_help_section (int level)
 
int trap_parse_params (int *argc, char **argv, trap_ifc_spec_t *ifc_spec)
 
int trap_free_ifc_spec (trap_ifc_spec_t ifc_spec)
 
void trap_free_ctx_t (trap_ctx_priv_t **ctx)
 
int trap_init (trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec)
 
int trap_terminate ()
 
int trap_finalize ()
 
void trap_get_internal_buffer (trap_ctx_priv_t *ctx, uint16_t ifc_idx, const void **data, uint32_t *size)
 Get pointer to data stored in buffer (with headers) and mark buffer as clean. More...
 
int trap_send_data (unsigned int ifcidx, const void *data, uint16_t size, int timeout)
 
int trap_send (uint32_t ifcidx, const void *data, uint16_t size)
 Send data via output interface. More...
 
int trap_recv (uint32_t ifcidx, const void **data, uint16_t *size)
 Receive data from input interface. More...
 
void trap_set_verbose_level (int level)
 
int trap_get_verbose_level ()
 
void trap_json_print_string (char *str)
 
void trap_convert_module_info_to_json (const trap_module_info_t *info)
 
static void print_aligned (const char *s, uint16_t align, uint16_t cut)
 
static void print_aligned_multiline (const char *s, uint16_t align, uint16_t cut)
 
static uint16_t get_terminal_width ()
 
char * get_module_name (void)
 
void trap_print_help (const trap_module_info_t *module_info)
 
void trap_print_ifc_spec_help ()
 
char * trap_get_param_by_delimiter (const char *source, char **dest, const char delimiter)
 Splitter of params string. Cut the first param, copy it into dest and returns pointer to the start of following parameter. More...
 
void trap_set_abs_timespec (struct timeval *tm, struct timespec *tmnblk)
 Internal function for setting of timeout structs according to libtrap timeout. More...
 
void trap_set_timeouts (int timeout, struct timeval *tm, struct timespec *tmnblk)
 Internal function for setting of timeout structs according to libtrap timeout. More...
 
int trap_ifcctl (int8_t type, uint32_t ifcidx, int32_t request,...)
 Control TRAP interface. More...
 
void trap_send_flush (uint32_t ifc)
 Force flush of buffer. More...
 
trap_ctx_priv_ttrap_create_ctx_t ()
 
int trap_ctx_terminate (trap_ctx_t *ctx)
 Terminate libtrap context. More...
 
static uint64_t get_cur_timestamp ()
 
int trap_ctx_recv (trap_ctx_t *ctx, uint32_t ifc, const void **data, uint16_t *size)
 Read data from input interface. More...
 
int trap_ctx_finalize (trap_ctx_t **ctx)
 Terminate libtrap context and free resources. More...
 
int trap_ctx_send (trap_ctx_t *ctx, unsigned int ifc, const void *data, uint16_t size)
 Send data via output interface. More...
 
static void remove_setter_from_param (char *params, char *setter)
 
static void handle_inifc_setters (trap_input_ifc_t *ifc, char *params)
 
static int trapifc_in_construct (trap_ctx_priv_t *ctx, trap_ifc_spec_t *ifc_spec, int idx)
 
static void handle_outifc_setters (trap_output_ifc_t *ifc, char *params)
 
static int trapifc_out_construct (trap_ctx_priv_t *ctx, trap_ifc_spec_t *ifc_spec, int idx)
 
trap_ctx_ttrap_ctx_init3 (const char *name, const char *description, int8_t i_ifcs, int8_t o_ifcs, const char *ifc_spec, const char *service_ifcname)
 Initialize and return the context of libtrap. More...
 
trap_ctx_ttrap_ctx_init (trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec)
 Initialize and return the context of libtrap. More...
 
trap_ctx_ttrap_ctx_init2 (trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec, const char *service_ifcname)
 Initialize and return the context of libtrap. More...
 
int trap_ctx_ifcctl (trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request,...)
 Control TRAP interface. More...
 
int trap_ctx_vifcctl (trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request, va_list ap)
 Control TRAP interface. More...
 
int trap_ctx_get_last_error (trap_ctx_t *ctx)
 Get last result code from libtrap context. More...
 
const char * trap_ctx_get_last_error_msg (trap_ctx_t *ctx)
 Get last (error) message from libtrap context. More...
 
void trap_ctx_send_flush (trap_ctx_t *ctx, uint32_t ifc)
 Force flush of buffer. More...
 
int service_get_data (int sock_d, uint32_t size, void **data)
 
int service_send_data (int sock_d, uint32_t size, void **data)
 
int encode_cnts_to_json (char **data, trap_ctx_priv_t *ctx)
 
void trap_ctx_create_ifc_dump (trap_ctx_t *ctx, const char *path)
 Create dump files. More...
 
int trap_ctx_get_client_count (trap_ctx_t *ctx, uint32_t ifcidx)
 Get number of connected clients. More...
 
void trap_ctx_vset_data_fmt (trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, va_list ap)
 
void trap_ctx_set_data_fmt (trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type,...)
 
int trap_ctx_vset_required_fmt (trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type, va_list ap)
 
int trap_ctx_set_required_fmt (trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type,...)
 
int trap_ctx_get_data_fmt (trap_ctx_t *ctx, uint8_t ifc_dir, uint32_t ifc_idx, uint8_t *data_type, const char **spec)
 
void trap_set_data_fmt (uint32_t out_ifc_idx, uint8_t data_type,...)
 
int trap_set_required_fmt (uint32_t in_ifc_idx, uint8_t data_type,...)
 
int trap_get_data_fmt (uint8_t ifc_dir, uint32_t in_ifc_idx, uint8_t *data_type, const char **spec)
 
int trap_ctx_get_in_ifc_state (trap_ctx_t *ctx, uint32_t ifc_idx)
 
int trap_get_in_ifc_state (uint32_t ifc_idx)
 
const char * trap_get_type_and_name_from_string (const char *source, const char **name, const char **type, int *length_name, int *length_type)
 
int trap_ctx_cmp_data_fmt (const char *sender_ifc_data_fmt, const char *receiver_ifc_data_fmt)
 
void * trap_get_global_ctx ()
 
int output_ifc_negotiation (void *ifc_priv_data, char ifc_type, uint32_t client_idx)
 
int input_ifc_negotiation (void *ifc_priv_data, char ifc_type)
 

Variables

char trap_help_spec = 0
 
char trap_ifc_type_supported []
 
trap_ctx_priv_ttrap_glob_ctx = NULL
 
int trap_last_error = TRAP_E_OK
 Code of last error (one of the codes above) More...
 
const char * trap_last_error_msg = NULL
 Human-readable message about last error. More...
 
char error_msg_buffer [MAX_ERROR_MSG_BUFF_SIZE]
 
char ** environ
 

Detailed Description

TRAP library base.

Author
Vaclav Bartos ibart.nosp@m.osv@.nosp@m.fit.v.nosp@m.utbr.nosp@m..cz
Tomas Cejka cejka.nosp@m.t@ce.nosp@m.snet..nosp@m.cz
Jan Neuzil neuzi.nosp@m.ja1@.nosp@m.fit.c.nosp@m.vut..nosp@m.cz
Marek Svepes svepe.nosp@m.mar@.nosp@m.fit.c.nosp@m.vut..nosp@m.cz
Tomas Jansky jansk.nosp@m.to1@.nosp@m.fit.c.nosp@m.vut..nosp@m.cz
Jaroslav Hlavac hlava.nosp@m.j20@.nosp@m.fit.c.nosp@m.vut..nosp@m.cz
Date
2013 - 2017

Definition in file trap.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 48 of file trap.c.

◆ ifcdir2str

#define ifcdir2str (   type)    (((type) == TRAPIFC_OUTPUT) ? "Output" : "Input")

String representation of interface direction (Input/Output)

Definition at line 130 of file trap.c.

◆ SEND_DATA

#define SEND_DATA ( )
Value:
do { \
int res = trap_ctx_send((trap_ctx_t *) trap_glob_ctx, ifcidx, data, size); \
if (res != TRAP_E_NOT_INITIALIZED) { \
trap_last_error_msg = trap_glob_ctx->trap_last_error_msg; \
trap_last_error = trap_glob_ctx->trap_last_error; \
} \
return res; \
} while (0);
#define trap_ctx_t
Definition: trap.h:59
int trap_ctx_send(trap_ctx_t *ctx, unsigned int ifc, const void *data, uint16_t size)
Send data via output interface.
Definition: trap.c:1448
uint8_t data[0]
trap_ctx_priv_t * trap_glob_ctx
Definition: trap.c:120
const char * trap_last_error_msg
#define TRAP_E_NOT_INITIALIZED
TRAP library not initilized.
Definition: trap.h:103

Definition at line 609 of file trap.c.

◆ X [1/4]

#define X (   param,
  text,
  align,
  cut 
)
Value:
do { \
written = printf(param); \
if (written < align_def) { \
printf("%*s", align - written, " "); \
} \
print_aligned(text, align, cut); \
puts(""); \
} while (0)

◆ X [2/4]

#define X (   val)
Value:
do { \
ifc->datatimeout = val; \
ifc->datatimeout_fixed = 1; \
} while (0)

◆ X [3/4]

#define X (   val)
Value:
do { \
ifc->datatimeout = val; \
ifc->datatimeout_fixed = 1; \
} while (0)

◆ X [4/4]

#define X (   val)
Value:
do { \
ifc->bufferswitch = val; \
ifc->bufferswitch_fixed = 1; \
} while (0)

Function Documentation

◆ __attribute__()

const char trap_version [] __attribute__ ( (used)  )

Version of libtrap

Used from config.h that is generated by the configure script.

Git revision of libtrap

Used from config.h that is generated by the configure script.

◆ get_module_name()

char* get_module_name ( void  )

Get name of this process/module.

Returns
Allocated string. If it is not possible to get the name, "module" is returned.

Definition at line 852 of file trap.c.

◆ get_param_by_delimiter()

char * get_param_by_delimiter ( const char *  source,
char **  dest,
const char  delimiter 
)
inlinestatic

Definition at line 1185 of file trap.c.

◆ get_terminal_width()

static uint16_t get_terminal_width ( )
static

Get size of terminal.

Returns
Returns columns of terminal.

Definition at line 833 of file trap.c.

◆ input_ifc_negotiation()

int input_ifc_negotiation ( void *  ifc_priv_data,
char  ifc_type 
)

Function handles input interface negotiation (receives hello message from output interface with its data format and data specifier and compares it with its own data format and data specifier). Hello message contains message header (data format and data specifier size) and data specifier.

Parameters
[in,out]ifc_priv_dataPointer to input interface private structure.
[in]ifc_typeType of IFC, e.g. TRAP_IFC_TYPE_FILE, TRAP_IFC_TYPE_TCPIP, or TRAP_IFC_TYPE_UNIX.
Returns
NEG_RES_FAILED if receiving the data from output interface fails, NEG_RES_FMT_UNKNOWN if the output interface has not specified data format, NEG_RES_FMT_SUBSET if the data format of input and output interfaces is the same and data specifier of the input interface is subset of the output interface data specifier, NEG_RES_CONT if the data format and data specifier of input and output interface are the same (input interface can receive the data for module right after the negotiation), NEG_RES_FMT_MISMATCH if the data format or data specifier of input and output interfaces does not match.

Receive hello msg header with data_type and data_fmt_spec_size



Compare data_type


Receive data_fmt_spec

Compare data_fmt_spec
Save senders data_type and data_fmt_spec

Definition at line 2995 of file trap.c.

◆ output_ifc_negotiation()

int output_ifc_negotiation ( void *  ifc_priv_data,
char  ifc_type,
uint32_t  client_idx 
)

Function handles output interface negotiation (sends hello message to input interface with its data format and data specifier). Hello message contains message header (data format and data specifier size) and data specifier.

Parameters
[in]ifc_priv_dataPointer to output interface private structure.
[in]ifc_typeType of IFC, e.g. TRAP_IFC_TYPE_FILE, TRAP_IFC_TYPE_TCPIP, or TRAP_IFC_TYPE_UNIX.
[in]client_idxIndex of new connected client.
Returns
NEG_RES_FAILED if sending the data to input interface fails, NEG_RES_FMT_UNKNOWN if the output interface has not specified data format, NEG_RES_OK signaling success (hello message successfully sent to input interface).

In case of file output interface, return NEG_RES_FMT_UNKNOWN In case of tcpip or unix output interface, send hello message header with format unknown value and return NEG_RES_FMT_UNKNOWN

Definition at line 2833 of file trap.c.

◆ print_aligned()

static void print_aligned ( const char *  s,
uint16_t  align,
uint16_t  cut 
)
static

Print helper for formatting text of help.

Parameters
[in]sString to print (e.g. description).
[in]alignThe number of spaces that should be add after line-break.
[in]cutApproximate (minimal) length of string that is printed on the line. Line-break is added if needed, after the current word.

Definition at line 723 of file trap.c.

◆ print_aligned_multiline()

static void print_aligned_multiline ( const char *  s,
uint16_t  align,
uint16_t  cut 
)
static

Print helper for formatting multiline text of help.

Multiline text means that the string contains '
'. print_aligned() is used for line breaking.

Parameters
[in]sString to print (e.g. description).
[in]alignThe number of spaces that should be add after line-break.
[in]cutApproximate (minimal) length of string that is printed on the line. Line-break is added if needed, after the current word.

Definition at line 788 of file trap.c.

◆ trap_check_global_vars()

void trap_check_global_vars ( void  )

Look for environment variables and change the change the related global variables.

Definition at line 138 of file trap.c.

◆ trap_convert_module_info_to_json()

void trap_convert_module_info_to_json ( const trap_module_info_t *  info)

Definition at line 688 of file trap.c.

◆ trap_create_module_info()

trap_module_info_t* trap_create_module_info ( const char *  mname,
const char *  mdesc,
int8_t  i_ifcs,
int8_t  o_ifcs,
uint16_t  param_count 
)

Definition at line 184 of file trap.c.

◆ trap_free_global_vars()

void trap_free_global_vars ( void  )

Definition at line 171 of file trap.c.

◆ trap_get_internal_buffer()

void trap_get_internal_buffer ( trap_ctx_priv_t ctx,
uint16_t  ifc_idx,
const void **  data,
uint32_t *  size 
)

Get pointer to data stored in buffer (with headers) and mark buffer as clean.

Definition at line 599 of file trap.c.

◆ trap_json_print_string()

void trap_json_print_string ( char *  str)

Definition at line 670 of file trap.c.

◆ trap_update_module_param()

int trap_update_module_param ( trap_module_info_t *  m,
uint16_t  param_id,
char  shortopt,
const char *  longopt,
const char *  desc,
int  req_arg,
const char *  arg_type 
)

Definition at line 203 of file trap.c.

Variable Documentation

◆ environ

char** environ

◆ error_msg_buffer

char error_msg_buffer[MAX_ERROR_MSG_BUFF_SIZE]

Definition at line 127 of file trap.c.

◆ trap_help_spec

char trap_help_spec = 0

Print Trap IFC help in trap_print_help() if non-zero.

Definition at line 102 of file trap.c.