libtrap  1.16.1
trap.h
Go to the documentation of this file.
1 
9 /*
10  * Copyright (C) 2013 - 2018 CESNET
11  *
12  * LICENSE TERMS
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in
21  * the documentation and/or other materials provided with the
22  * distribution.
23  * 3. Neither the name of the Company nor the names of its contributors
24  * may be used to endorse or promote products derived from this
25  * software without specific prior written permission.
26  *
27  * ALTERNATIVELY, provided that this notice is retained in full, this
28  * product may be distributed under the terms of the GNU General Public
29  * License (GPL) version 2 or later, in which case the provisions
30  * of the GPL apply INSTEAD OF those given above.
31  *
32  * This software is provided ``as is'', and any express or implied
33  * warranties, including, but not limited to, the implied warranties of
34  * merchantability and fitness for a particular purpose are disclaimed.
35  * In no event shall the company or contributors be liable for any
36  * direct, indirect, incidental, special, exemplary, or consequential
37  * damages (including, but not limited to, procurement of substitute
38  * goods or services; loss of use, data, or profits; or business
39  * interruption) however caused and on any theory of liability, whether
40  * in contract, strict liability, or tort (including negligence or
41  * otherwise) arising in any way out of the use of this software, even
42  * if advised of the possibility of such damage.
43  *
44  */
45 
46 #ifndef _TRAP_H_
47 #define _TRAP_H_
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #include <stdint.h>
54 #include <stdarg.h>
55 
56 #include "trap_module_info.h"
57 #include "jansson.h"
58 
59 #define trap_ctx_t void
60 
71 extern const char trap_version[];
72 
76 extern const char trap_git_version[];
77 
83 
87 #define TRAP_E_OK 0
88 #define TRAP_E_TIMEOUT 1
89 #define TRAP_E_INITIALIZED 10
90 #define TRAP_E_BADPARAMS 11
91 #define TRAP_E_BAD_IFC_INDEX 12
92 #define TRAP_E_BAD_FPARAMS 13
93 #define TRAP_E_IO_ERROR 14
94 #define TRAP_E_TERMINATED 15
95 #define TRAP_E_NOT_SELECTED 16
96 #define TRAP_E_BAD_CERT 17
97 #define TRAP_E_HELP 20
98 #define TRAP_E_FIELDS_MISMATCH 21
99 #define TRAP_E_FIELDS_SUBSET 22
100 #define TRAP_E_FORMAT_CHANGED 23
101 #define TRAP_E_FORMAT_MISMATCH 24
102 #define TRAP_E_NEGOTIATION_FAILED 25
103 #define TRAP_E_NOT_INITIALIZED 254
104 #define TRAP_E_MEMORY 255
105 
119 #define TRAP_NO_WAIT 0
120 
124 #define TRAP_WAIT -1
125 
130 #define TRAP_HALFWAIT -2
131 
132 #define TRAP_TIMEOUT_STR(t) (t==TRAP_WAIT?"TRAP_WAIT":(t==TRAP_NO_WAIT?"TRAP_NO_WAIT":(t==TRAP_HALFWAIT?"TRAP_HALFWAIT":"")))
133 
134 #define TRAP_NO_AUTO_FLUSH (-1l)
135 
136 
160 #define TRAP_IFC_DELIMITER ','
161 
165 #define TRAP_IFC_PARAM_DELIMITER ':'
166 
171 #define TRAP_IFC_TYPE_GENERATOR 'g'
172 #define TRAP_IFC_TYPE_BLACKHOLE 'b'
173 #define TRAP_IFC_TYPE_TCPIP 't'
174 #define TRAP_IFC_TYPE_TLS 'T'
175 #define TRAP_IFC_TYPE_UNIX 'u'
176 #define TRAP_IFC_TYPE_SERVICE 's'
177 #define TRAP_IFC_TYPE_FILE 'f'
178 extern char trap_ifc_type_supported[];
179 
186 };
187 /* ifctypes *//* trapifcspec */
194 
202 };
205 #ifndef TRAP_IFC_MESSAGEQ_SIZE
206 #define TRAP_IFC_MESSAGEQ_SIZE 100000
207 #endif
208 
213 typedef struct trap_ifc_spec_s {
214  char *types;
215  char **params;
217 
225 typedef enum {
228 
231 
234 
238 
242 typedef enum {
245 
247  FMT_OK = 1,
248 
251 
255 
264 void trap_set_data_fmt(uint32_t out_ifc_idx, uint8_t data_type, ...);
265 
275 int trap_set_required_fmt(uint32_t in_ifc_idx, uint8_t data_type, ...);
276 
289 int trap_get_data_fmt(uint8_t ifc_dir, uint32_t ifc_idx, uint8_t *data_type, const char **spec);
290 
302 void trap_ctx_set_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, ...);
303 
315 void trap_ctx_vset_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, va_list ap);
316 
327 int trap_ctx_get_in_ifc_state(trap_ctx_t *ctx, uint32_t ifc_idx);
328 
341 int trap_ctx_set_required_fmt(trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type, ...);
342 
355 int trap_ctx_vset_required_fmt(trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type, va_list ap);
356 
371 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);
372 /* trap_mess_fmt */
376 
389 const char *trap_get_type_and_name_from_string(const char *source, const char **name, const char **type, int *length_name, int *length_type);
390 
400 int trap_ctx_cmp_data_fmt(const char *sender_ifc_data_fmt, const char *receiver_ifc_data_fmt);
401 
407 void *trap_get_global_ctx();
408 
415 int trap_get_in_ifc_state(uint32_t ifc_idx);
416 
430 int trap_parse_params(int *argc, char **argv, trap_ifc_spec_t *ifc_spec);
431 
441 char *trap_get_param_by_delimiter(const char *source, char **dest, const char delimiter);
442 
449 int trap_check_buffer_content(void *buffer, uint32_t buffer_size);
450 /* commonapi */
454 
455 /*****************************************************************************/
456 /***************************** Library interface *****************************/
457 
463 extern int trap_last_error;
464 extern const char *trap_last_error_msg;
465 
470 int trap_free_ifc_spec(trap_ifc_spec_t ifc_spec);
471 
479 int trap_init(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec);
480 
492 int trap_terminate();
493 
498 int trap_finalize();
499 
513 int trap_send_data(unsigned int ifcidx, const void *data, uint16_t size, int timeout);
514 
530 int trap_recv(uint32_t ifcidx, const void **data, uint16_t *size);
531 
545 int trap_send(uint32_t ifcidx, const void *data, uint16_t size);
546 
558 void trap_set_verbose_level(int level);
559 
565 
570 void trap_set_help_section(int level);
571 
577 void trap_print_help(const trap_module_info_t *module_info);
578 
587 
609 int trap_ifcctl(int8_t type, uint32_t ifcidx, int32_t request, ... /* arg */);
610 
616 void trap_send_flush(uint32_t ifc);
617 /* basic API */
621 
622 /********** Macros generating pieces of common code **********/
623 
646 
658 trap_ctx_t *trap_ctx_init2(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec, const char *service_ifcname);
659 
674 trap_ctx_t *trap_ctx_init3(const char *name, const char *description, int8_t i_ifcs, int8_t o_ifcs, const char *ifc_spec, const char *service_ifcname);
675 
684 int trap_ctx_finalize(trap_ctx_t **ctx);
685 
695 
709 int trap_ctx_recv(trap_ctx_t *ctx, uint32_t ifc, const void **data, uint16_t *size);
710 
727 int trap_ctx_send(trap_ctx_t *ctx, unsigned int ifc, const void *data, uint16_t size);
728 
743 void trap_ctx_set_verbose_level(trap_ctx_t *ctx, int level);
744 
754 
780 int trap_ctx_ifcctl(trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request, ... /* arg */);
781 
796 int trap_ctx_vifcctl(trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request, va_list ap);
797 
807 
816 const char *trap_ctx_get_last_error_msg(trap_ctx_t *ctx);
817 
826 void trap_ctx_send_flush(trap_ctx_t *ctx, uint32_t ifc);
827 
839 int trap_ctx_get_client_count(trap_ctx_t *ctx, uint32_t ifcidx);
840 
853 void trap_ctx_create_ifc_dump(trap_ctx_t *ctx, const char *path);
854 /* contextapi */
858 
872 #define TRAP_DEFAULT_SIGNAL_HANDLER(stop_cmd) \
873  void trap_default_signal_handler(int signal)\
874  {\
875  if (signal == SIGTERM || signal == SIGINT) { \
876  stop_cmd;\
877  trap_terminate();\
878  }\
879  }
880 
886 #ifdef HAVE_SIGACTION
887 #define TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER() \
888  do {\
889  if (trap_get_verbose_level() >= 1)\
890  printf("Setting signal handler for SIGINT and SIGTERM using 'sigaction' function.\n");\
891  struct sigaction act;\
892  /* Set default signal handler function*/\
893  act.sa_handler = trap_default_signal_handler;\
894  act.sa_flags = 0;\
895  /* Prevent interruption of signal handler by another SIGTERM or SIGINT */\
896  sigemptyset(&act.sa_mask);\
897  sigaddset(&act.sa_mask, SIGTERM);\
898  sigaddset(&act.sa_mask, SIGINT);\
899  /* Register signal hander */\
900  sigaction(SIGTERM, &act, NULL);\
901  sigaction(SIGINT, &act, NULL);\
902  } while(0)
903 #else
904 #define TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER() \
905  do {\
906  if (trap_get_verbose_level() >= 1)\
907  printf("Setting signal handler for SIGINT and SIGTERM using 'signal' function.\n");\
908  signal(SIGTERM, trap_default_signal_handler);\
909  signal(SIGINT, trap_default_signal_handler);\
910  } while(0)
911 #endif
912 
913 
914 
925 #define TRAP_DEFAULT_INITIALIZATION(argc, argv, module_info) \
926  {\
927  trap_ifc_spec_t ifc_spec;\
928  int ret = trap_parse_params(&argc, argv, &ifc_spec);\
929  if (ret != TRAP_E_OK) {\
930  if (ret == TRAP_E_HELP) {\
931  trap_print_help(&module_info);\
932  FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) \
933  return 0;\
934  }\
935  trap_free_ifc_spec(ifc_spec);\
936  fprintf(stderr, "ERROR in parsing of parameters for TRAP: %s\n", trap_last_error_msg);\
937  FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) \
938  return 1;\
939  }\
940  ret = trap_init(&module_info, ifc_spec);\
941  if (ret != TRAP_E_OK) {\
942  trap_free_ifc_spec(ifc_spec);\
943  fprintf(stderr, "ERROR in TRAP initialization: %s\n", trap_last_error_msg);\
944  FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) \
945  return 1;\
946  }\
947  trap_free_ifc_spec(ifc_spec);\
948  }
949 
954 #define TRAP_DEFAULT_FINALIZATION() \
955  trap_finalize();
956 
964 #define TRAP_DEFAULT_GET_DATA_ERROR_HANDLING(ret_code, timeout_cmd, error_cmd) \
965  if ((ret_code) != TRAP_E_OK) {\
966  if ((ret_code) == TRAP_E_TIMEOUT) {\
967  timeout_cmd;\
968  } else if ((ret_code) == TRAP_E_TERMINATED) {\
969  error_cmd;\
970  } else if (ret_code == TRAP_E_FORMAT_CHANGED) { \
971  /* Nothing to do here, TRAP_E_FORMAT_CHANGED has to be skipped by this macro */ \
972  /* (module can perform some special operations with templates after trap_recv() signals format change) */ \
973  } else if (ret_code == TRAP_E_FORMAT_MISMATCH) { \
974  fprintf(stderr, "trap_recv() error: output and input interfaces data formats or data specifiers mismatch.\n"); \
975  error_cmd; \
976  } else if (ret_code == TRAP_E_NEGOTIATION_FAILED) { \
977  fprintf(stderr, "trap_recv() error: interface negotiation failed (caused by invalid reply from a remote module, corrupted file or an unknown error).\n"); \
978  error_cmd; \
979  } else {\
980  fprintf(stderr, "Error: trap_recv() returned %i (%s)\n", (ret_code), trap_last_error_msg);\
981  error_cmd;\
982  }\
983  }
984 
991 #define TRAP_DEFAULT_RECV_ERROR_HANDLING(ret_code, timeout_cmd, error_cmd) \
992  if ((ret_code) != TRAP_E_OK) {\
993  if ((ret_code) == TRAP_E_TIMEOUT) {\
994  timeout_cmd;\
995  } else if ((ret_code) == TRAP_E_TERMINATED) {\
996  error_cmd;\
997  } else if (ret_code == TRAP_E_FORMAT_CHANGED) { \
998  /* Nothing to do here, TRAP_E_FORMAT_CHANGED has to be skipped by this macro */ \
999  /* (module can perform some special operations with templates after trap_recv() signals format change) */ \
1000  } else if (ret_code == TRAP_E_FORMAT_MISMATCH) { \
1001  fprintf(stderr, "trap_recv() error: output and input interfaces data formats or data specifiers mismatch.\n"); \
1002  error_cmd; \
1003  } else if (ret_code == TRAP_E_NEGOTIATION_FAILED) { \
1004  fprintf(stderr, "trap_recv() error: interface negotiation failed (caused by invalid reply from a remote module, corrupted file or an unknown error).\n"); \
1005  error_cmd; \
1006  } else {\
1007  fprintf(stderr, "Error: trap_recv() returned %i (%s)\n", (ret_code), trap_last_error_msg);\
1008  error_cmd;\
1009  }\
1010  }
1011 
1012 
1021 #define TRAP_DEFAULT_SEND_DATA_ERROR_HANDLING(ret_code, timeout_cmd, error_cmd) \
1022  if ((ret_code) != TRAP_E_OK) {\
1023  if ((ret_code) == TRAP_E_TIMEOUT) {\
1024  timeout_cmd;\
1025  } else if ((ret_code) == TRAP_E_TERMINATED) {\
1026  error_cmd;\
1027  } else {\
1028  fprintf(stderr, "Error: trap_send_data() returned %i (%s)\n", (ret_code), trap_last_error_msg);\
1029  error_cmd;\
1030  }\
1031  }
1032 
1040 #define TRAP_DEFAULT_SEND_ERROR_HANDLING(ret_code, timeout_cmd, error_cmd) \
1041  if ((ret_code) != TRAP_E_OK) {\
1042  if ((ret_code) == TRAP_E_TIMEOUT) {\
1043  timeout_cmd;\
1044  } else if ((ret_code) == TRAP_E_TERMINATED) {\
1045  error_cmd;\
1046  } else {\
1047  fprintf(stderr, "Error: trap_send() returned %i (%s)\n", (ret_code), trap_last_error_msg);\
1048  error_cmd;\
1049  }\
1050  }
1051 /* modulemacros */
1054 
1055 
1056 
1069 int output_ifc_negotiation(void *ifc_priv_data, char ifc_type, uint32_t client_idx);
1070 
1071 
1086 int input_ifc_negotiation(void *ifc_priv_data, char ifc_type);
1087 
1088 
1089 #ifdef __cplusplus
1090 } // extern "C"
1091 #endif
1092 
1093 #endif
void trap_ctx_set_verbose_level(trap_ctx_t *ctx, int level)
Set verbosity level of library functions.
int trap_ctx_cmp_data_fmt(const char *sender_ifc_data_fmt, const char *receiver_ifc_data_fmt)
Set interface timeout (int32_t): in microseconds for non-blocking mode; timeout can be also: TRAP_WAI...
Definition: trap.h:201
void trap_ctx_create_ifc_dump(trap_ctx_t *ctx, const char *path)
Create dump files.
int trap_ctx_get_in_ifc_state(trap_ctx_t *ctx, uint32_t ifc_idx)
int trap_ctx_set_required_fmt(trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type,...)
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...
trap_ctx_t * trap_ctx_init(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec)
Initialize and return the context of libtrap.
#define trap_ctx_t
Definition: trap.h:59
int trap_get_data_fmt(uint8_t ifc_dir, uint32_t ifc_idx, uint8_t *data_type, const char **spec)
void * trap_get_global_ctx()
const char trap_version[]
int trap_last_error
Code of last error (one of the codes above)
char * trap_default_socket_path_format
const char * trap_last_error_msg
Human-readable message about last error.
trap_ifc_type
Definition: trap.h:183
Set timeout of automatic buffer flushing for interface, expects uint64_t argument with number of micr...
Definition: trap.h:199
struct trap_ifc_spec_s trap_ifc_spec_t
int trap_ctx_vifcctl(trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request, va_list ap)
Control TRAP interface.
int trap_ctx_send(trap_ctx_t *ctx, unsigned int ifc, const void *data, uint16_t size)
Send data via output interface.
trap_ctx_t * trap_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.
void trap_set_help_section(int level)
int trap_get_in_ifc_state(uint32_t ifc_idx)
int trap_ctx_get_client_count(trap_ctx_t *ctx, uint32_t ifcidx)
Get number of connected clients.
int trap_init(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec)
void trap_ctx_vset_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, va_list ap)
int trap_finalize()
int trap_ifcctl(int8_t type, uint32_t ifcidx, int32_t request,...)
Control TRAP interface.
void trap_ctx_send_flush(trap_ctx_t *ctx, uint32_t ifc)
Force flush of buffer.
int output_ifc_negotiation(void *ifc_priv_data, char ifc_type, uint32_t client_idx)
const char * trap_ctx_get_last_error_msg(trap_ctx_t *ctx)
Get last (error) message from libtrap context.
void trap_send_flush(uint32_t ifc)
Force flush of buffer.
interface acts as source of data for module
Definition: trap.h:184
int trap_ctx_ifcctl(trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request,...)
Control TRAP interface.
int trap_ctx_finalize(trap_ctx_t **ctx)
Terminate libtrap context and free resources.
int trap_ctx_recv(trap_ctx_t *ctx, uint32_t ifc, const void **data, uint16_t *size)
Read data from input interface.
trap_ifcctl_request
Definition: trap.h:198
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_get_last_error(trap_ctx_t *ctx)
Get last result code from libtrap context.
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)
int trap_get_verbose_level()
const char trap_git_version[]
void trap_set_verbose_level(int level)
int input_ifc_negotiation(void *ifc_priv_data, char ifc_type)
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)
Enable/disable buffering - could be dangerous on input interface!!! expects char argument with value ...
Definition: trap.h:200
int trap_send_data(unsigned int ifcidx, const void *data, uint16_t size, int timeout)
Structures containing information about Nemea modules and macros for initialization and release of th...
int trap_ctx_terminate(trap_ctx_t *ctx)
Terminate libtrap context.
void trap_set_data_fmt(uint32_t out_ifc_idx, uint8_t data_type,...)
trap_ctx_t * trap_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.
int trap_terminate()
int trap_recv(uint32_t ifcidx, const void **data, uint16_t *size)
Receive data from input interface.
int trap_set_required_fmt(uint32_t in_ifc_idx, uint8_t data_type,...)
trap_data_format_t
Definition: trap.h:225
int trap_ctx_get_verbose_level(trap_ctx_t *ctx)
Get verbosity level.
char ** params
Definition: trap.h:215
Definition: trap.h:247
void trap_print_help(const trap_module_info_t *module_info)
char * types
Definition: trap.h:214
int trap_check_buffer_content(void *buffer, uint32_t buffer_size)
Check content of buffer, iterate over message headers.
void trap_ctx_set_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type,...)
const char * trap_get_type_and_name_from_string(const char *source, const char **name, const char **type, int *length_name, int *length_type)
trap_in_ifc_state_t
Definition: trap.h:242
void trap_print_ifc_spec_help()
interface is used for sending data out of module
Definition: trap.h:185
int trap_send(uint32_t ifcidx, const void *data, uint16_t size)
Send data via output interface.