Libtrap: Internal development docs  1.16.1
ifc_dummy.c
Go to the documentation of this file.
1 /**
2  * \file ifc_dummy.c
3  * \brief TRAP dummy interfaces (generator and blackhole)
4  * \author Vaclav Bartos <ibartosv@fit.vutbr.cz>
5  * \date 2013
6  * \date 2014
7  */
8 /*
9  * Copyright (C) 2013,2014 CESNET
10  *
11  * LICENSE TERMS
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  * 3. Neither the name of the Company nor the names of its contributors
23  * may be used to endorse or promote products derived from this
24  * software without specific prior written permission.
25  *
26  * ALTERNATIVELY, provided that this notice is retained in full, this
27  * product may be distributed under the terms of the GNU General Public
28  * License (GPL) version 2 or later, in which case the provisions
29  * of the GPL apply INSTEAD OF those given above.
30  *
31  * This software is provided ``as is'', and any express or implied
32  * warranties, including, but not limited to, the implied warranties of
33  * merchantability and fitness for a particular purpose are disclaimed.
34  * In no event shall the company or contributors be liable for any
35  * direct, indirect, incidental, special, exemplary, or consequential
36  * damages (including, but not limited to, procurement of substitute
37  * goods or services; loss of use, data, or profits; or business
38  * interruption) however caused and on any theory of liability, whether
39  * in contract, strict liability, or tort (including negligence or
40  * otherwise) arising in any way out of the use of this software, even
41  * if advised of the possibility of such damage.
42  *
43  */
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdint.h>
47 #include <inttypes.h>
48 #include <assert.h>
49 
50 #include "../include/libtrap/trap.h"
51 #include "trap_ifc.h"
52 #include "trap_error.h"
53 
54 /***** Generator *****/
55 
56 typedef struct generator_private_s {
58  char *data_to_send;
59  int data_size;
62 
63 static void create_dump(void *priv, uint32_t idx, const char *path)
64 {
65  VERBOSE(CL_ERROR, "Unimplemented. (%s:%d)", __FILE__, __LINE__);
66  return;
67 }
68 
69 int generator_recv(void *priv, void *data, uint32_t *size, int timeout)
70 {
71  assert(data != NULL);
72  assert(size != NULL);
73 
74  uint16_t *mh = data;
75  void *p = (void *) (mh + 1);
76 
77  generator_private_t *config = (generator_private_t*) priv;
78  if (config->is_terminated) {
79  return trap_error(config->ctx, TRAP_E_TERMINATED);
80  }
81  *mh = config->data_size;
82  memcpy(p, config->data_to_send, config->data_size);
83  *size = config->data_size;
84  return TRAP_E_OK;
85 }
86 
87 void generator_terminate(void *priv)
88 {
89  if (priv) {
90  ((generator_private_t*)priv)->is_terminated = 1;
91  }
92 }
93 
94 
95 void generator_destroy(void *priv)
96 {
97  // Free private data
98  if (priv) {
99  free(((generator_private_t*)priv)->data_to_send);
100  free(priv);
101  }
102 }
103 
104 char *generator_ifc_get_id(void *priv)
105 {
106  return NULL;
107 }
108 
109 uint8_t generator_ifc_is_conn(void *priv) {
110  return 1;
111 }
112 
114 {
115  generator_private_t *priv = NULL;
116  char *param_iterator = NULL;
117  char *n_str = NULL;
118  int n, ret;
119 
120  // Check parameter
121  if (params == NULL) {
122  VERBOSE(CL_ERROR, "parameter is null pointer");
123  return TRAP_E_BADPARAMS;
124  }
125 
126  /* Parsing params */
127  param_iterator = trap_get_param_by_delimiter(params, &n_str, TRAP_IFC_PARAM_DELIMITER);
128  if (n_str == NULL) {
129  VERBOSE(CL_ERROR, "Missing parameter of generator IFC.");
130  ret = TRAP_E_BADPARAMS;
131  goto failure;
132  }
133  ret = sscanf(n_str, "%d", &n);
134  free(n_str);
135  if ((ret != 1) || (n <= 0) || (n > 255)) {
136  VERBOSE(CL_ERROR, "Generator IFC expects a number from 1 to 255 as the 1st parameter.");
137  ret = TRAP_E_BADPARAMS;
138  goto failure;
139  }
140 
141  // Create structure to store private data
142  priv = calloc(1, sizeof(generator_private_t));
143  if (!priv) {
144  ret = TRAP_E_MEMORY;
145  goto failure;
146  }
147  param_iterator = trap_get_param_by_delimiter(param_iterator, &priv->data_to_send, TRAP_IFC_PARAM_DELIMITER);
148 
149  // Store data to send (param) into private data
150  if (!priv->data_to_send) {
151  VERBOSE(CL_ERROR, "Generator IFC expects %d bytes as the 2nd parameter.", priv->data_size);
152  ret = TRAP_E_MEMORY;
153  goto failure;
154  }
155  if (strlen(priv->data_to_send) != n) {
156  VERBOSE(CL_ERROR, "Bad length of the 2nd parameter of generator IFC.");
157  ret = TRAP_E_BADPARAMS;
158  goto failure;
159  }
160 
161  priv->ctx = ctx;
162  priv->is_terminated = 0;
163  priv->data_size = n;
164 
165  // Fill struct defining the interface
166  ifc->recv = generator_recv;
168  ifc->destroy = generator_destroy;
169  ifc->create_dump = create_dump;
170  ifc->priv = priv;
173 
174  return TRAP_E_OK;
175 failure:
176  if (priv != NULL) {
177  free(priv->data_to_send);
178  }
179  free(priv);
180  return ret;
181 }
182 
183 
184 
185 /***** Blackhole *****/
186 // Everything sent to blackhole is dropped
187 
188 int blackhole_send(void *priv, const void *data, uint16_t size, int timeout)
189 {
190  return TRAP_E_OK;
191 }
192 
193 void blackhole_flush(void *priv)
194 {
195  return;
196 }
197 
198 void blackhole_terminate(void *priv)
199 {
200  return;
201 }
202 
203 
204 void blackhole_destroy(void *priv)
205 {
206  return;
207 }
208 
209 int32_t blackhole_get_client_count(void *priv)
210 {
211  /* this interface does not support multiple clients */
212  return 1;
213 }
214 
215 int8_t blackhole_get_client_stats_json(void *priv, json_t *client_stats_arr)
216 {
217  /* do not collect client statistics for this interface */
218  return 1;
219 }
220 
221 
222 char *blackhole_ifc_get_id(void *priv)
223 {
224  return NULL;
225 }
226 
228 {
229  ifc->send = blackhole_send;
230  ifc->flush = blackhole_flush;
232  ifc->destroy = blackhole_destroy;
235  ifc->create_dump = create_dump;
236  ifc->priv = NULL;
238  return TRAP_E_OK;
239 }
240 
int blackhole_send(void *priv, const void *data, uint16_t size, int timeout)
Definition: ifc_dummy.c:188
ifc_get_id_func_t get_id
Pointer to get_id function.
Definition: trap_ifc.h:182
void generator_terminate(void *priv)
Definition: ifc_dummy.c:87
#define TRAP_E_OK
Success, no error.
Definition: trap.h:87
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
ifc_get_client_stats_json_func_t get_client_stats_json
Pointer to get_client_stats_json function.
Definition: trap_ifc.h:244
#define TRAP_IFC_PARAM_DELIMITER
Definition: trap.h:165
void blackhole_destroy(void *priv)
Definition: ifc_dummy.c:204
ifc_get_id_func_t get_id
Pointer to get_id function.
Definition: trap_ifc.h:236
void blackhole_terminate(void *priv)
Definition: ifc_dummy.c:198
char * blackhole_ifc_get_id(void *priv)
Definition: ifc_dummy.c:222
struct generator_private_s generator_private_t
void blackhole_flush(void *priv)
Definition: ifc_dummy.c:193
#define TRAP_E_TERMINATED
Interface was terminated during reading/writing.
Definition: trap.h:94
int create_blackhole_ifc(trap_ctx_priv_t *ctx, char *params, trap_output_ifc_t *ifc)
Definition: ifc_dummy.c:227
ifc_create_dump_func_t create_dump
Pointer to function for generating of dump.
Definition: trap_ifc.h:242
#define VERBOSE(level, format, args...)
uint8_t data[0]
ifc_get_client_count_func_t get_client_count
Pointer to get_client_count function.
Definition: trap_ifc.h:243
ifc_terminate_func_t terminate
Pointer to terminate function.
Definition: trap_ifc.h:184
trap_ctx_priv_t * ctx
Definition: ifc_dummy.c:57
static void create_dump(void *priv, uint32_t idx, const char *path)
Definition: ifc_dummy.c:63
ifc_destroy_func_t destroy
Pointer to destructor function.
Definition: trap_ifc.h:185
ifc_send_func_t send
Pointer to send function.
Definition: trap_ifc.h:238
int32_t blackhole_get_client_count(void *priv)
Definition: ifc_dummy.c:209
ifc_recv_func_t recv
Pointer to receive function.
Definition: trap_ifc.h:183
ifc_create_dump_func_t create_dump
Pointer to function for generating of dump.
Definition: trap_ifc.h:186
uint8_t generator_ifc_is_conn(void *priv)
Definition: ifc_dummy.c:109
Error handling for TRAP.
#define TRAP_E_MEMORY
Memory allocation error.
Definition: trap.h:104
ifc_destroy_func_t destroy
Pointer to destructor function.
Definition: trap_ifc.h:241
void generator_destroy(void *priv)
Definition: ifc_dummy.c:95
ifc_terminate_func_t terminate
Pointer to terminate function.
Definition: trap_ifc.h:240
char * generator_ifc_get_id(void *priv)
Definition: ifc_dummy.c:104
int generator_recv(void *priv, void *data, uint32_t *size, int timeout)
Definition: ifc_dummy.c:69
static int trap_error(trap_ctx_priv_t *ctx, int err_num)
Definition: trap_error.h:63
int create_generator_ifc(trap_ctx_priv_t *ctx, char *params, trap_input_ifc_t *ifc)
Definition: ifc_dummy.c:113
ifc_flush_func_t flush
Pointer to flush function.
Definition: trap_ifc.h:239
void * priv
Pointer to instance&#39;s private data.
Definition: trap_ifc.h:187
ifc_is_conn_func_t is_conn
Pointer to is_connected function.
Definition: trap_ifc.h:181
void * priv
Pointer to instance&#39;s private data.
Definition: trap_ifc.h:245
Interface of TRAP interfaces.
int8_t blackhole_get_client_stats_json(void *priv, json_t *client_stats_arr)
Definition: ifc_dummy.c:215
#define TRAP_E_BADPARAMS
Bad parameters passed to interface initializer.
Definition: trap.h:90