Libtrap: Internal development docs  1.16.1
trap.h
Go to the documentation of this file.
1 /**
2  * \file trap.h
3  * \brief Interface of TRAP library.
4  * \author Vaclav Bartos <ibartosv@fit.vutbr.cz>
5  * \author Tomas Cejka <cejkat@cesnet.cz>
6  * \author Tomas Jansky <janskto1@fit.cvut.cz>
7  * \date 2013 - 2018
8  */
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 
61 /**
62  * \defgroup commonapi Common libtrap API
63  *
64  * This module declares basic public constants, structures and functions of libtrap.
65  * @{
66  */
67 
68 /**
69  * Text string with libtrap version.
70  */
71 extern const char trap_version[];
72 
73 /**
74  * Text string with Git revision of libtrap.
75  */
76 extern const char trap_git_version[];
77 
78 /**
79  * Text string with default path format to sockets (UNIX IFC and service IFC).
80  * Assigned in ifc_tcpip.h
81  */
83 
84 /**
85  * \defgroup errorcodes Error codes
86  * @{*/
87 #define TRAP_E_OK 0 ///< Success, no error
88 #define TRAP_E_TIMEOUT 1 ///< Read or write operation timeout
89 #define TRAP_E_INITIALIZED 10 ///< TRAP library already initilized
90 #define TRAP_E_BADPARAMS 11 ///< Bad parameters passed to interface initializer
91 #define TRAP_E_BAD_IFC_INDEX 12 ///< Interface index out of range
92 #define TRAP_E_BAD_FPARAMS 13 ///< Bad parameters of function
93 #define TRAP_E_IO_ERROR 14 ///< IO Error
94 #define TRAP_E_TERMINATED 15 ///< Interface was terminated during reading/writing
95 #define TRAP_E_NOT_SELECTED 16 ///< Interface was not selected reading/writing
96 #define TRAP_E_BAD_CERT 17 ///< Wrong certificate given to TLS interface
97 #define TRAP_E_HELP 20 ///< Returned by parse_parameters when help is requested
98 #define TRAP_E_FIELDS_MISMATCH 21 ///< Returned when receiver fields are not subset of sender fields
99 #define TRAP_E_FIELDS_SUBSET 22 ///< Returned when receivers fields are subset of senders fields and both sets are not identical
100 #define TRAP_E_FORMAT_CHANGED 23 ///< Returned by trap_recv when format or format spec of the receivers interface has been changed
101 #define TRAP_E_FORMAT_MISMATCH 24 ///< Returned by trap_recv when data format or data specifier of the output and input interfaces doesn't match
102 #define TRAP_E_NEGOTIATION_FAILED 25 ///< Returned by trap_recv when negotiation of the output and input interfaces failed
103 #define TRAP_E_NOT_INITIALIZED 254 ///< TRAP library not initilized
104 #define TRAP_E_MEMORY 255 ///< Memory allocation error
105 /**@}*/
106 
107 /**
108  * \defgroup trap_timeout TRAP Timeout
109  *
110  * TRAP IFC works with timeout to decide wether it should block or just wait
111  * for some time.
112  * The timeout is usually in microseconds.
113  * This section lists some special timeout values.
114  * @{
115  */
116 /**
117  * Non-Blocking mode, do not wait ever.
118  */
119 #define TRAP_NO_WAIT 0
120 /**
121  * Blocking mode, wait for client's connection, for message transport to/from
122  * internal system buffer.
123  */
124 #define TRAP_WAIT -1
125 /**
126  * Blocking mode, do not wait for client's connection, clients do not try to
127  * reconnect, there is no recovering of clients during get_data/send_data in this mode.
128  * For input ifc it is the same as TRAP_NO_WAIT.
129  */
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) ///< value to disable autoflushing on output interface
135 
136 /**@}*/
137 
138 /**
139  * \defgroup trapifcspec Specifier of TRAP interfaces
140  * The format of -i parameter of modules sets up TRAP interfaces.
141  * #TRAP_IFC_DELIMITER divides specifier into interfaces.
142  * #TRAP_IFC_PARAM_DELIMITER divides parameters of one interface.
143  *
144  * Each TRAP interface must have the type (\ref ifctypes) as the first parameter.
145  * The following parameters are passed to the interface and they are interface-dependent.
146  *
147  * The format example, let's assume the module has 1 input IFC and 1 output IFC:
148  *
149  * <BLOCKQUOTE>-i t:localhost:7600,u:my_socket</BLOCKQUOTE>
150  *
151  * This sets the input IFC to TCP type and it will connect to localhost, port 7600.
152  * The output IFC will listen on UNIX socket with identifier my_socket.
153  *
154  * @{
155  */
156 
157 /**
158  * Delimiter of TRAP interfaces in IFC specifier
159  */
160 #define TRAP_IFC_DELIMITER ','
161 
162 /**
163  * Delimiter of TRAP interface's parameters in IFC specifier
164  */
165 #define TRAP_IFC_PARAM_DELIMITER ':'
166 
167 /**
168  * \defgroup ifctypes Types of IFC
169  * @{
170  */
171 #define TRAP_IFC_TYPE_GENERATOR 'g' ///< trap_ifc_dummy generator (input)
172 #define TRAP_IFC_TYPE_BLACKHOLE 'b' ///< trap_ifc_dummy blackhole (output)
173 #define TRAP_IFC_TYPE_TCPIP 't' ///< trap_ifc_tcpip (input&output part)
174 #define TRAP_IFC_TYPE_TLS 'T' ///< trap_ifc_tls (input&output part)
175 #define TRAP_IFC_TYPE_UNIX 'u' ///< trap_ifc_tcpip via UNIX socket(input&output part)
176 #define TRAP_IFC_TYPE_SERVICE 's' ///< service ifc
177 #define TRAP_IFC_TYPE_FILE 'f' ///< trap_ifc_file (input&output part)
178 extern char trap_ifc_type_supported[];
179 
180 /**
181  * Type of interface (direction)
182  */
184  TRAPIFC_INPUT = 1, ///< interface acts as source of data for module
185  TRAPIFC_OUTPUT = 2 ///< interface is used for sending data out of module
186 };
187 
188 /**
189  * @}
190  *//* ifctypes */
191 /**
192  * @}
193  *//* trapifcspec */
194 
195 /**
196  * \name TRAP interface control request
197  * @{*/
199  TRAPCTL_AUTOFLUSH_TIMEOUT = 1, ///< Set timeout of automatic buffer flushing for interface, expects uint64_t argument with number of microseconds. It can be set to #TRAP_NO_AUTO_FLUSH to disable autoflush.
200  TRAPCTL_BUFFERSWITCH = 2, ///< Enable/disable buffering - could be dangerous on input interface!!! expects char argument with value 1 (default value after libtrap initialization - enabled) or 0 (for disabling buffering on interface).
201  TRAPCTL_SETTIMEOUT = 3 ///< Set interface timeout (int32_t): in microseconds for non-blocking mode; timeout can be also: TRAP_WAIT, TRAP_HALFWAIT, or TRAP_NO_WAIT.
202 };
203 /**@}*/
204 
205 #ifndef TRAP_IFC_MESSAGEQ_SIZE
206 #define TRAP_IFC_MESSAGEQ_SIZE 100000 ///< size of message queue used for buffering
207 #endif
208 
209 /** Structure with specification of interface types and their parameters.
210  * This can be filled by command-line parameters using trap_parse_params
211  * function.
212  */
213 typedef struct trap_ifc_spec_s {
214  char *types;
215  char **params;
217 
218 /**
219  * \defgroup trap_mess_fmt Message format
220  * @{
221  */
222 /**
223  * Type of messages that are sent via IFC
224  */
225 typedef enum {
226  /** unknown - message format was not specified yet */
228 
229  /** raw data, no format specified */
231 
232  /** UniRec records */
234 
235  /** structured data serialized using JSON */
238 
239 /**
240  * Possible states of an IFC during data format negotiation.
241  */
242 typedef enum {
243  /** Negotiation is not completed */
245 
246  /** Negotiation was successful */
247  FMT_OK = 1,
248 
249  /** Negotiation failed, format mismatch */
251 
252  /** Negotiation was successful, but receivers (input ifc) template is subset of senders (output ifc) template and missing fields has to be defined */
255 
256 /**
257  * Set format of messages on output IFC.
258  *
259  * \param[in] out_ifc_idx index of output IFC
260  * \param[in] data_type format of messages defined by #trap_data_format_t
261  * \param[in] ... if data_type is TRAP_FMT_UNIREC or TRAP_FMT_JSON, additional parameter
262  * that specifies template is expected
263  */
264 void trap_set_data_fmt(uint32_t out_ifc_idx, uint8_t data_type, ...);
265 
266 /**
267  * Set format of messages expected on input IFC.
268  *
269  * \param[in] in_ifc_idx index of input IFC
270  * \param[in] data_type format of messages defined by #trap_data_format_t
271  * \param[in] ... if data_type is TRAP_FMT_UNIREC or TRAP_FMT_JSON, additional parameter
272  * that specifies template is expected
273  * \return TRAP_E_OK on success
274  */
275 int trap_set_required_fmt(uint32_t in_ifc_idx, uint8_t data_type, ...);
276 
277 /**
278  * Get message format and template that is set on IFC.
279  *
280  * On output IFC it should return the values that were set. On input IFC
281  * it should return format and template that was received.
282  *
283  * \param[in] ifc_dir #trap_ifc_type direction of interface
284  * \param[in] ifc_idx index of IFC
285  * \param[out] data_type format of messages defined by #trap_data_format_t
286  * \param[out] spec Template specifier - UniRec specifier in case of TRAP_FMT_UNIREC data_type, otherwise, it can be any string.
287  * \return TRAP_E_OK on success, on error see trap_ctx_get_data_fmt().
288  */
289 int trap_get_data_fmt(uint8_t ifc_dir, uint32_t ifc_idx, uint8_t *data_type, const char **spec);
290 
291 /**
292  * Set format of messages on output IFC.
293  *
294  * This function is thread safe.
295  *
296  * \param[in,out] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
297  * \param[in] out_ifc_idx Index of output IFC.
298  * \param[in] data_type Format of messages defined by #trap_data_format_t.
299  * \param[in] ... If data_type is TRAP_FMT_UNIREC or TRAP_FMT_JSON, additional parameter
300  * that specifies template (char *) is expected.
301  */
302 void trap_ctx_set_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, ...);
303 
304 /**
305  * Set format of messages on output IFC.
306  *
307  * This function is thread safe.
308  *
309  * \param[in,out] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
310  * \param[in] out_ifc_idx Index of output IFC.
311  * \param[in] data_type Format of messages defined by #trap_data_format_t.
312  * \param[in] ap If data_type is TRAP_FMT_UNIREC or TRAP_FMT_JSON, additional parameter
313  * that specifies template (char *) is expected.
314  */
315 void trap_ctx_vset_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, va_list ap);
316 
317 /**
318  * Returns current state of an input interface on specified index.
319  *
320  * This function is thread safe.
321  *
322  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
323  * \param[in] ifc_idx Index of the input interface
324  * \return Value of #trap_in_ifc_state_t on success, otherwise TRAP_E_NOT_INITIALIZED when libtrap context is not initialized or
325  * TRAP_E_BAD_IFC_INDEX (ifc_idx >= number of input ifcs).
326  */
327 int trap_ctx_get_in_ifc_state(trap_ctx_t *ctx, uint32_t ifc_idx);
328 
329 /**
330  * Set format of messages expected on input IFC.
331  *
332  * This function is thread safe.
333  *
334  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
335  * \param[in] in_ifc_idx Index of input IFC.
336  * \param[in] data_type Format of messages defined by #trap_data_format_t.
337  * \param[in] ... If data_type is TRAP_FMT_UNIREC or TRAP_FMT_JSON, additional parameter
338  * that specifies template (char *) is expected.
339  * \return TRAP_E_OK on success, on error see #trap_ctx_vset_required_fmt().
340  */
341 int trap_ctx_set_required_fmt(trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type, ...);
342 
343 /**
344  * Set format of messages expected on input IFC.
345  *
346  * This function is thread safe.
347  *
348  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
349  * \param[in] in_ifc_idx Index of input IFC.
350  * \param[in] data_type Format of messages defined by #trap_data_format_t.
351  * \param[in] ap If data_type is TRAP_FMT_UNIREC or TRAP_FMT_JSON, additional parameter
352  * that specifies template (char *) is expected.
353  * \return TRAP_E_OK on success, TRAP_E_NOT_INITIALIZED when libtrap context is not initialized, TRAP_E_BAD_IFC_INDEX or TRAP_E_BADPARAMS on error.
354  */
355 int trap_ctx_vset_required_fmt(trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type, va_list ap);
356 
357 /**
358  * Get message format and template that is set on IFC.
359  *
360  * On output IFC it should return the values that were set. On input IFC
361  * it should return format and template that was received.
362  * This function is thread safe.
363  *
364  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
365  * \param[in] ifc_dir #trap_ifc_type direction of interface
366  * \param[in] ifc_idx Index of IFC.
367  * \param[out] data_type Format of messages defined by #trap_data_format_t.
368  * \param[out] spec Specifier of data format specifies the template (char *) is expected.
369  * \return TRAP_E_OK on success, TRAP_E_NOT_INITIALIZED if libtrap context is not initialized or negotiation is not successful yet for input IFC, TRAP_E_BAD_IFC_INDEX or TRAP_E_BADPARAMS on error.
370  */
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 
373 /**
374  * @}
375  *//* trap_mess_fmt */
376 
377 /**
378  * Parse Fields name and types from string.
379  *
380  * Function parses the source string and sets the given pointers (pointers to source string). Than it sets length of name and type
381  *
382  * \param[in] source Source string to parse.
383  * \param[in] name ouput parameter, where will be set the pointer to name of a field (pointer to source string).
384  * \param[in] type ouput parameter, where will be set the pointer to type of a field (pointer to source string).
385  * \param[in] length_name ouput parameter, where will be set the length of a name.
386  * \param[in] length_type ouput parameter, where will be set the length of a type.
387  * \return pointer to source string, moved to next field
388  */
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 
391 /**
392  * Compares sender_ifc template and receiver_ifc template
393  * and returns whether receivers template is subset of the senders template.
394  *
395  * \param[in] sender_ifc_data_fmt sender_ifc template (char *)
396  * \param[in] receiver_ifc_data_fmt receiver_ifc template (char *)
397  * \return TRAP_E_OK on success (receivers template is subset of the senders template),
398  * TRAP_E_FIELDS_MISMATCH (receivers template has field which is not in senders template).
399  */
400 int trap_ctx_cmp_data_fmt(const char *sender_ifc_data_fmt, const char *receiver_ifc_data_fmt);
401 
402 /**
403  * Returns global context.
404  *
405  * \return pointer to global context.
406  */
407 void *trap_get_global_ctx();
408 
409 /**
410  * Returns current state of an input interface on specified index.
411  *
412  * \param[in] ifc_idx Index of the input interface
413  * \return See #trap_ctx_get_in_ifc_state().
414  */
415 int trap_get_in_ifc_state(uint32_t ifc_idx);
416 
417 /** Parse command-line arguments.
418  * Extract arguments needed by TRAP to set up interfaces (-i params), verbosity
419  * level (-v/-vv/-vvv) and return the rest (argc and argv are modified, i.e.
420  * processed parameter is removed). Extracted information is stored into
421  * ifc_spec. These variables should be passed to trap_init. Data in ifc_spec
422  * must be freed by trap_free_ifc_spec. If help is requested (-h/--help)
423  * TRAP_E_HELP is returned (argc and argv are modified also).
424  * @param[in,out] argc Pointer to number of command-line arguments.
425  * @param[in,out] argv Command-line arguments.
426  * @param[out] ifc_spec Structure with specification of interface types and
427  * their parameters.
428  * @return Error code (0 on success)
429  */
430 int trap_parse_params(int *argc, char **argv, trap_ifc_spec_t *ifc_spec);
431 
432 /**
433  * \brief Splitter of *params* string.
434  * Cut the first param, copy it into *dest* and returns pointer to the start of following
435  * parameter.
436  * \param[in] source source string, typically *params*
437  * \param[out] dest destination string, target of first paramater copying
438  * \param[in] delimiter separator of values in *params*
439  * \return Pointer to the start of following parameter (char after delimiter). \note If NULL, no other parameter is present or error during allocation occured.
440  */
441 char *trap_get_param_by_delimiter(const char *source, char **dest, const char delimiter);
442 
443 /**
444  * \brief Check content of buffer, iterate over message headers
445  * \param [in] buffer start of buffer
446  * \param [in] buffer_size size of buffer
447  * \return 0 on success, number of errors otherwise
448  */
449 int trap_check_buffer_content(void *buffer, uint32_t buffer_size);
450 
451 /**
452  * @}
453  *//* commonapi */
454 
455 /*****************************************************************************/
456 /***************************** Library interface *****************************/
457 
458 /**
459  * \defgroup simpleapi Simple API
460  * @{
461  */
462 
463 extern int trap_last_error; ///< Code of last error (one of the codes above)
464 extern const char *trap_last_error_msg; ///< Human-readable message about last error
465 
466 /** Destructor of trap_ifc_spec_t structure.
467  * @param[in] ifc_spec trap_ifc_spec_t structure to clear.
468  * @return Error code (0 on success)
469  */
470 int trap_free_ifc_spec(trap_ifc_spec_t ifc_spec);
471 
472 /** Initialization function.
473  * Create and initialize all interfaces.
474  * @param[in] module_info Pointer to struct containing info about the module.
475  * @param[in] ifc_spec Structure with specification of interface types and
476  * their parameters.
477  * @return Error code (0 on success)
478  */
479 int trap_init(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec);
480 
481 /** Function to terminate module's operation.
482  * This function stops all read/write operations on all interfaces.
483  * Any waiting in trap_recv() and trap_send()_data is interrupted and these
484  * functions return immediately with TRAP_E_TERMINATED.
485  * Any call of trap_recv() or trap_send() after call of this function
486  * returns TRAP_E_TERMINATED.
487  *
488  * This function is used to terminate module's operation (asynchronously), e.g.
489  * in SIGTERM handler.
490  * @return Always TRAP_E_OK (0).
491  */
492 int trap_terminate();
493 
494 /** Cleanup function.
495  * Disconnect all interfaces and do all necessary cleanup.
496  * @return Error code
497  */
498 int trap_finalize();
499 
500 /** Send data to output interface.
501  * Write data of size `size` given by `data` pointer into interface `ifc`.
502  * If data cannot be written immediately (e.g. because of full buffer or
503  * lost connection), wait until write is possible or `timeout` microseconds
504  * elapses. If `timeout` < 0, wait indefinitely.
505  * @param[in] ifcidx Index of interface to write into.
506  * @param[in] data Pointer to data.
507  * @param[in] size Number of bytes of data.
508  * @param[in] timeout Timeout in microseconds for non-blocking mode; timeout
509  * can be also: TRAP_WAIT, TRAP_HALFWAIT, or TRAP_NO_WAIT.
510  * @return Error code - 0 on success, TRAP_E_TIMEOUT if timeout elapses.
511  * \deprecated This function should be replaced by trap_send().
512  */
513 int trap_send_data(unsigned int ifcidx, const void *data, uint16_t size, int timeout);
514 
515 /**
516  * \brief Receive data from input interface.
517  *
518  * Receive a message from interface specified by `ifcidx` and set
519  * pointer to the `data`.
520  * When function returns due to timeout, contents of `data` and `size` are undefined.
521  *
522  * @param[in] ifcidx Index of input IFC.
523  * @param[out] data Pointer to received data.
524  * @param[out] size Size of received data in bytes of data.
525  * @return Error code - #TRAP_E_OK on success, #TRAP_E_TIMEOUT if timeout elapses.
526  *
527  * \note Data must not be freed! Library stores incomming data into static array and rewrites it during every trap_recv() call.
528  * \see trap_ifcctl() to set timeout (#TRAPCTL_SETTIMEOUT)
529  */
530 int trap_recv(uint32_t ifcidx, const void **data, uint16_t *size);
531 
532 /**
533  * \brief Send data via output interface.
534  *
535  * Send a message given by `data` pointer of `size` message size via interface specified by `ifcidx`
536  * that is the index of output interfaces (counted from 0).
537  *
538  * @param[in] ifcidx Index of input IFC.
539  * @param[out] data Pointer to message to send.
540  * @param[out] size Size of message in bytes.
541  * @return Error code - #TRAP_E_OK on success, #TRAP_E_TIMEOUT if timeout elapses.
542  *
543  * \see trap_ifcctl() to set timeout (#TRAPCTL_SETTIMEOUT)
544  */
545 int trap_send(uint32_t ifcidx, const void *data, uint16_t size);
546 
547 /** Set verbosity level of library functions.
548  * Verbosity levels may be:
549  * - -3 - errors
550  * - -2 - warnings
551  * - -1 - notices (default)
552  * - 0 - verbose
553  * - 1 - more verbose
554  * - 2 - even more verbose
555  *
556  * @param[in] level Desired level of verbosity.
557  */
558 void trap_set_verbose_level(int level);
559 
560 /** Get verbosity level.
561  * See trap_set_verbose_level for list of levels.
562  * @return Verbosity level currently set in the library.
563  */
565 
566 /** Set section for trap_print_help()
567  *
568  * \param [in] level 0 for default info about module, 1 for info about IFC specifier
569  */
570 void trap_set_help_section(int level);
571 
572 /** Print common TRAP help message.
573  * The help message contains information from module_info and describes common
574  * TRAP command-line parameters.
575  * @param[in] module_info Pointer to struct containing info about the module.
576  */
577 void trap_print_help(const trap_module_info_t *module_info);
578 
579 /** Print help about interface specifier.
580  * Prints help message about format of interface specifier and description of
581  * all available interface types.
582  * This message is normally a part of help printed by trap_print_help, this
583  * function is useful when you don't use standard TRAP command-line parameters
584  * but you still use the same format of interface specifier.
585  */
587 
588 /**
589  * \brief Control TRAP interface.
590  *
591  * \note Type and request types were changed from enum because of python wrapper.
592  * \param [in] type #trap_ifc_type direction of interface
593  * \param [in] ifcidx index of TRAP interface
594  * \param [in] request #trap_ifcctl_request type of operation
595  * \param [in,out] ... arguments of request, see #trap_ifcctl_request for more details on requests and their arguments.
596  *
597  * Examples:
598  * \code{C}
599  * // set 4 seconds timeout
600  * trap_ifcctl(TRAPIFC_INPUT, 0, TRAPCTL_SETTIMEOUT, 4000000);
601  * // disable auto-flush
602  * trap_ifcctl(TRAPIFC_OUTPUT, 0, TRAPCTL_AUTOFLUSH_TIMEOUT, TRAP_NO_AUTO_FLUSH);
603  * // disable buffering
604  * trap_ifcctl(TRAPIFC_OUTPUT, 0, TRAPCTL_BUFFERSWITCH, 0);
605  * \endcode
606  *
607  * \return TRAP_E_OK on success
608  */
609 int trap_ifcctl(int8_t type, uint32_t ifcidx, int32_t request, ... /* arg */);
610 
611 /**
612  * \brief Force flush of buffer.
613  *
614  * \param[in] ifc IFC Index of interface to write into.
615  */
616 void trap_send_flush(uint32_t ifc);
617 
618 /**
619  * @}
620  *//* basic API */
621 
622 /********** Macros generating pieces of common code **********/
623 
624 /**
625  * \addtogroup contextapi Context API
626  *
627  * This API allows user to use multiple instances of libtrap in the same process.
628  *
629  * The API is similar to \ref simpleapi. The difference is in the private context memory
630  * that is returned by trap_ctx_init() and is freed by trap_ctx_finalize().
631  * Obtained context pointer must be passed to all functions from context API.
632  * @{
633  */
634 
635 /**
636  * \brief Initialize and return the context of libtrap.
637  *
638  * This function is thread safe.
639  *
640  * \param[in] module_info Pointer to struct containing info about the module.
641  * \param[in] ifc_spec Structure with specification of interface types and
642  * their parameters.
643  * \return Pointer to context (context needs to be checked for error value by trap_ctx_get_last_error() function), NULL on memory error.
644  */
645 trap_ctx_t *trap_ctx_init(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec);
646 
647 /**
648  * \brief Initialize and return the context of libtrap.
649  *
650  * This function is thread safe.
651  *
652  * \param[in] module_info Pointer to struct containing info about the module.
653  * \param[in] ifc_spec Structure with specification of interface types and their parameters.
654  * \param[in] service_ifcname Identifier of the service IFC (used as a part of path to the UNIX socket). When NULL is used, no service IFC will be opened.
655  *
656  * \return Pointer to context (context needs to be checked for error value by trap_ctx_get_last_error() function), NULL on memory error.
657  */
658 trap_ctx_t *trap_ctx_init2(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec, const char *service_ifcname);
659 
660 /**
661  * \brief Initialize and return the context of libtrap.
662  *
663  * This function is thread safe.
664  *
665  * \param[in] name Name of the NEMEA module (libtrap context).
666  * \param[in] description - Detailed description of the module, can be NULL ("" will be used in such case)
667  * \param[in] i_ifcs Number of input IFCs, it can be -1 if o_ifcs > -1 (-1 means variable number of IFCs, it is then computed from ifc_spec).
668  * \param[in] o_ifcs Number of output IFCs, it can be -1 if i_ifcs > -1 (-1 means variable number of IFCs, it is then computed from ifc_spec).
669  * \param[in] ifc_spec IFC_SPEC stringdescribed in README.ifcspec.md
670  * \param[in] service_ifcname Identifier of the service IFC (used as a part of path to the UNIX socket). When NULL is used, no service IFC will be opened.
671  *
672  * \return Pointer to context (context needs to be checked for error value by trap_ctx_get_last_error() function), NULL on memory error.
673  */
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 
676 /**
677  * \brief Terminate libtrap context and free resources.
678  *
679  * This function is thread safe.
680  *
681  * \param[in] ctx Pointer to the private libtrap context data (trap_ctx_init()).
682  * \return TRAP_E_OK on success.
683  */
684 int trap_ctx_finalize(trap_ctx_t **ctx);
685 
686 /**
687  * \brief Terminate libtrap context.
688  *
689  * This function is thread safe.
690  *
691  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
692  * \return TRAP_E_OK on success.
693  */
695 
696 /**
697  * \brief Read data from input interface.
698  *
699  * This function is thread safe.
700  *
701  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
702  * \param[in] ifc Index of input interface (counted from 0).
703  * \param[out] data Pointer to received data.
704  * \param[out] size Size of received data in bytes.
705  *
706  * \return Error code - TRAP_E_OK on success, TRAP_E_TIMEOUT if timeout elapses.
707  * \see #trap_ctx_ifcctl
708  */
709 int trap_ctx_recv(trap_ctx_t *ctx, uint32_t ifc, const void **data, uint16_t *size);
710 
711 /**
712  * \brief Send data via output interface.
713  *
714  * Write data of size `size` given by `data` pointer into interface `ifc`.
715  * If data cannot be written immediately (e.g. because of full buffer or
716  * lost connection), wait until write is possible or `timeout` microseconds
717  * elapses. If `timeout` < 0, wait indefinitely.
718  * This function is thread safe.
719  *
720  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
721  * \param[in] ifc Index of interface to write into.
722  * \param[in] data Pointer to data.
723  * \param[in] size Number of bytes of data.
724  * \return Error code - 0 on success, TRAP_E_TIMEOUT if timeout elapses.
725  * \see #trap_ctx_ifcctl
726  */
727 int trap_ctx_send(trap_ctx_t *ctx, unsigned int ifc, const void *data, uint16_t size);
728 
729 /**
730  * \brief Set verbosity level of library functions.
731  *
732  * Verbosity levels may be:
733  * - -3 - errors
734  * - -2 - warnings
735  * - -1 - notices (default)
736  * - 0 - verbose
737  * - 1 - more verbose
738  * - 2 - even more verbose
739  *
740  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
741  * \param[in] level Desired level of verbosity.
742  */
743 void trap_ctx_set_verbose_level(trap_ctx_t *ctx, int level);
744 
745 /**
746  * \brief Get verbosity level.
747  *
748  * \see #trap_set_verbose_level for the list of levels.
749  *
750  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
751  * \return Verbosity level currently set in the library.
752  */
754 
755 /**
756  * \brief Control TRAP interface.
757  *
758  * This function is thread safe.
759  *
760  * \note Type and request types were changed from enum because of python wrapper.
761  *
762  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
763  * \param [in] type #trap_ifc_type direction of interface
764  * \param [in] ifcidx index of TRAP interface
765  * \param [in] request #trap_ifcctl_request type of operation
766  * \param [in,out] ... arguments of request, see #trap_ifcctl_request for more details on requests and their arguments.
767  *
768  * Examples:
769  * \code{C}
770  * // set 4 seconds timeout
771  * trap_ctx_ifcctl(ctx, TRAPIFC_INPUT, 0, TRAPCTL_SETTIMEOUT, 4000000);
772  * // disable auto-flush
773  * trap_ctx_ifcctl(ctx, TRAPIFC_OUTPUT, 0, TRAPCTL_AUTOFLUSH_TIMEOUT, TRAP_NO_AUTO_FLUSH);
774  * // disable buffering
775  * trap_ctx_ifcctl(ctx, TRAPIFC_OUTPUT, 0, TRAPCTL_BUFFERSWITCH, 0);
776  * \endcode
777  *
778  * \return TRAP_E_OK on success
779  */
780 int trap_ctx_ifcctl(trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request, ... /* arg */);
781 
782 /**
783  * \brief Control TRAP interface.
784  *
785  * This function is thread safe.
786  *
787  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
788  * \param [in] type #trap_ifc_type direction of interface
789  * \param [in] ifcidx index of TRAP interface
790  * \param [in] request #trap_ifcctl_request type of operation
791  * \param [in,out] ap arguments of request.
792  * \return TRAP_E_OK on success
793  *
794  * \see trap_ctx_ifcctl().
795  */
796 int trap_ctx_vifcctl(trap_ctx_t *ctx, int8_t type, uint32_t ifcidx, int32_t request, va_list ap);
797 
798 /**
799  * \brief Get last result code from libtrap context.
800  *
801  * This function is thread safe.
802  *
803  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
804  * \return \ref errorcodes
805  */
807 
808 /**
809  * \brief Get last (error) message from libtrap context.
810  *
811  * This function is thread safe.
812  *
813  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
814  * \return Text string with last (error) message from libtrap context.
815  */
816 const char *trap_ctx_get_last_error_msg(trap_ctx_t *ctx);
817 
818 /**
819  * \brief Force flush of buffer.
820  *
821  * This function is thread safe.
822  *
823  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
824  * \param[in] ifc IFC Index of interface to write into.
825  */
826 void trap_ctx_send_flush(trap_ctx_t *ctx, uint32_t ifc);
827 
828 /**
829  * \brief Get number of connected clients.
830  *
831  * Output interface (TCP or UNIX) allows to send messages to multiple clients. This
832  * function reads number of connected clients from internal interface
833  * structure.
834  *
835  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
836  * \param[in] ifcidx IFC Index of output interface.
837  * \return Number of connected clients. -1 on error.
838  */
839 int trap_ctx_get_client_count(trap_ctx_t *ctx, uint32_t ifcidx);
840 
841 /**
842  * \brief Create dump files.
843  *
844  * Create dump files for debug as follows:
845  * trap-i[number]-config.txt Output interface configuration.
846  * trap-i[number]-buffer.dat Output interface buffer
847  * trap-o[number]-config.txt Input interface configuration.
848  * trap-o[number]-buffer.dat Input interface buffer
849  *
850  * \param[in] ctx Pointer to the private libtrap context data (#trap_ctx_init()).
851  * \param[in] path Output directory, if NULL use current working directory.
852  */
853 void trap_ctx_create_ifc_dump(trap_ctx_t *ctx, const char *path);
854 
855 /**
856  * @}
857  *//* contextapi */
858 
859 /**
860  * \defgroup modulemacros Nemea module macros
861  *
862  * Set of preprocessor macros for rapid NEMEA module development.
863  * @{
864  */
865 /** \brief Define default signal handler function
866  * Defines function to handle SIGTERM and SIGINT signals. When a signal is
867  * received, it runs the specified command and calls trap_terminate().
868  * Place this macro before your main function.
869  * \param[in] stop_cmd Command which stops operation of a module. Usually
870  * setting a variable which is tested in module's main loop.
871  */
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 
881 /** \brief Register default signal handler.
882  * Register function defined by TRAP_DEFAULT_SIGNAL_HANDLER as handler of
883  * SIGTERM and SIGINT signals.
884  * Place this macro between TRAP initialization and the main loop.
885  */
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 
915 /** \brief Initialize TRAP using command-line parameters and handle errors.
916  * Generates code that parses command-line parameters using trap_parse_params,
917  * intializes TRAP library using trap_init and handle possible errors.
918  * It calls exit(1) when an error has occured.
919  * Place this macro at the beginning of your main function.
920  * \param[in,out] argc Number of command-line parameters.
921  * \param[in,out] argv List of command-line parameters.
922  * \param[in] module_info trap_module_info_t structure containing information
923  * about the module.
924  */
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 
950 /** \brief Generate TRAP cleanup code.
951  * Only calls trap_finalize function.
952  * Place this macro at the end of your main function.
953  */
954 #define TRAP_DEFAULT_FINALIZATION() \
955  trap_finalize();
956 
957 /** \brief Handle possible errors after call to trap_recv().
958  * \param[in] ret_code Return code of trap_recv().
959  * \param timeout_cmd Command to run when a timeout has occured, e.g. "continue".
960  * \param error_cmd Command to run when an error has occured or interface was
961  * terminated, e.g. "break".
962  * \deprecated This macro should be replaced by TRAP_DEFAULT_RECV_ERROR_HANDLING.
963  */
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 
985 /** \brief Handle possible errors after call to trap_recv.
986  * \param[in] ret_code Return code of trap_recv.
987  * \param timeout_cmd Command to run when a timeout has occured, e.g. "continue".
988  * \param error_cmd Command to run when an error has occured or interface was
989  * terminated, e.g. "break".
990  */
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 
1013 /** \brief Handle possible errors after call to trap_send_data.
1014  * \param[in] ret_code Return code of trap_send_data.
1015  * \param timeout_cmd Command to run when a timeout has occured, e.g. "0" to
1016  * do nothing.
1017  * \param error_cmd Command to run when an error has occured or interface was
1018  * terminated, e.g. "break".
1019  * \deprecated This macro should be replaced by TRAP_DEFAULT_SEND_ERROR_HANDLING.
1020  */
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 
1033 /** \brief Handle possible errors after call to trap_send.
1034  * \param[in] ret_code Return code of trap_send.
1035  * \param timeout_cmd Command to run when a timeout has occured, e.g. "0" to
1036  * do nothing.
1037  * \param error_cmd Command to run when an error has occured or interface was
1038  * terminated, e.g. "break".
1039  */
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 /**
1052  * @}
1053  *//* modulemacros */
1054 
1055 
1056 
1057 /**
1058  * Function handles output interface negotiation (sends hello message to input interface with its data format
1059  * and data specifier). Hello message contains message header (data format and data specifier size) and data specifier.
1060  *
1061  * \param[in] ifc_priv_data Pointer to output interface private structure.
1062  * \param[in] ifc_type Type of IFC, e.g. TRAP_IFC_TYPE_FILE, TRAP_IFC_TYPE_TCPIP, or TRAP_IFC_TYPE_UNIX.
1063  * \param[in] client_idx Index of new connected client.
1064  *
1065  * \return NEG_RES_FAILED if sending the data to input interface fails,
1066  * NEG_RES_FMT_UNKNOWN if the output interface has not specified data format,
1067  * NEG_RES_OK signaling success (hello message successfully sent to input interface).
1068  */
1069 int output_ifc_negotiation(void *ifc_priv_data, char ifc_type, uint32_t client_idx);
1070 
1071 
1072 /**
1073  * Function handles input interface negotiation (receives hello message from output interface with its data format
1074  * and data specifier and compares it with its own data format and data specifier).
1075  * Hello message contains message header (data format and data specifier size) and data specifier.
1076  *
1077  * \param[in,out] ifc_priv_data Pointer to input interface private structure.
1078  * \param[in] ifc_type Type of IFC, e.g. TRAP_IFC_TYPE_FILE, TRAP_IFC_TYPE_TCPIP, or TRAP_IFC_TYPE_UNIX.
1079  *
1080  * \return NEG_RES_FAILED if receiving the data from output interface fails,
1081  * NEG_RES_FMT_UNKNOWN if the output interface has not specified data format,
1082  * 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,
1083  * 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),
1084  * NEG_RES_FMT_MISMATCH if the data format or data specifier of input and output interfaces does not match.
1085  */
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_get_data_fmt(uint8_t ifc_dir, uint32_t in_ifc_idx, uint8_t *data_type, const char **spec)
Definition: trap.c:2714
int trap_ctx_cmp_data_fmt(const char *sender_ifc_data_fmt, const char *receiver_ifc_data_fmt)
Definition: trap.c:2765
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.
Definition: trap.c:2473
int trap_ctx_get_in_ifc_state(trap_ctx_t *ctx, uint32_t ifc_idx)
Definition: trap.c:2719
int trap_ctx_set_required_fmt(trap_ctx_t *ctx, uint32_t in_ifc_idx, uint8_t data_type,...)
Definition: trap.c:2602
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...
Definition: trap.c:1160
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_ifc_name)
Initialize and return the context of libtrap.
Definition: trap.c:1731
#define trap_ctx_t
Definition: trap.h:59
void * trap_get_global_ctx()
Definition: trap.c:2820
const char trap_version[]
int trap_last_error
Code of last error (one of the codes above)
Definition: trap.c:123
char * trap_default_socket_path_format
const char * trap_last_error_msg
Human-readable message about last error.
Definition: trap.c:125
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.
Definition: trap.c:2032
const char * trap_ctx_get_last_error_msg(trap_ctx_t *ctx)
Get last (error) message from libtrap context.
Definition: trap.c:2108
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
void trap_set_help_section(int level)
Definition: trap.c:324
int trap_get_in_ifc_state(uint32_t ifc_idx)
Definition: trap.c:2734
int trap_ctx_get_client_count(trap_ctx_t *ctx, uint32_t ifcidx)
Get number of connected clients.
Definition: trap.c:2495
int trap_init(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec)
Definition: trap.c:526
void trap_ctx_vset_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type, va_list ap)
Definition: trap.c:2516
uint8_t data[0]
int trap_finalize()
Definition: trap.c:578
int trap_ifcctl(int8_t type, uint32_t ifcidx, int32_t request,...)
Control TRAP interface.
Definition: trap.c:1231
void trap_ctx_send_flush(trap_ctx_t *ctx, uint32_t ifc)
Force flush of buffer.
Definition: trap.c:2115
int output_ifc_negotiation(void *ifc_priv_data, char ifc_type, uint32_t client_idx)
Definition: trap.c:2833
void trap_send_flush(uint32_t ifc)
Force flush of buffer.
Definition: trap.c:1241
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.
Definition: trap.c:2019
int trap_ctx_finalize(trap_ctx_t **ctx)
Terminate libtrap context and free resources.
Definition: trap.c:1409
int trap_ctx_recv(trap_ctx_t *ctx, uint32_t ifcidx, const void **data, uint16_t *size)
Read data from input interface.
Definition: trap.c:1379
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)
Definition: trap.c:2565
int trap_ctx_get_last_error(trap_ctx_t *ctx)
Get last result code from libtrap context.
Definition: trap.c:2102
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)
Definition: trap.c:2617
int trap_get_verbose_level()
Definition: trap.c:663
void trap_set_verbose_level(int level)
Definition: trap.c:654
int input_ifc_negotiation(void *ifc_priv_data, char ifc_type)
Definition: trap.c:2995
trap_ctx_t * trap_ctx_init2(trap_module_info_t *module_info, trap_ifc_spec_t ifc_spec, const char *service_ifc_name)
Initialize and return the context of libtrap.
Definition: trap.c:1802
int trap_parse_params(int *argc, char **argv, trap_ifc_spec_t *ifc_spec)
Definition: trap.c:339
int trap_free_ifc_spec(trap_ifc_spec_t ifc_spec)
Definition: trap.c:497
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)
Definition: trap.c:618
int trap_ctx_terminate(trap_ctx_t *ctx)
Terminate libtrap context.
Definition: trap.c:1342
void trap_set_data_fmt(uint32_t out_ifc_idx, uint8_t data_type,...)
Definition: trap.c:2693
int trap_terminate()
Definition: trap.c:563
int trap_recv(uint32_t ifcidx, const void **data, uint16_t *size)
Receive data from input interface.
Definition: trap.c:631
int trap_set_required_fmt(uint32_t in_ifc_idx, uint8_t data_type,...)
Definition: trap.c:2702
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)
Definition: trap.c:898
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.
Definition: trap.c:1786
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.
Definition: trap.c:222
void trap_ctx_set_data_fmt(trap_ctx_t *ctx, uint32_t out_ifc_idx, uint8_t data_type,...)
Definition: trap.c:2551
const char * trap_get_type_and_name_from_string(const char *source, const char **name, const char **type, int *length_name, int *length_type)
Definition: trap.c:2739
trap_in_ifc_state_t
Definition: trap.h:242
const char trap_git_version[]
void trap_print_ifc_spec_help()
Definition: trap.c:1138
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.
Definition: trap.c:624