Browse code

janssonrpcc: remove compilation warning

> janssonrpcc_mod.c:345:25: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
> if (jsonrpc_keep_alive < 0) {
> ~~~~~~~~~~~~~~~~~~ ^ ~
> 1 warning generated.

Victor Seva authored on 15/05/2019 09:13:17
Showing 1 changed files
... ...
@@ -342,9 +342,7 @@ int parse_keep_alive_param(modparam_t type, void* val)
342 342
 		return -1;
343 343
 	}
344 344
 	jsonrpc_keep_alive = (int)(long)val;
345
-	if (jsonrpc_keep_alive < 0) {
346
-		jsonrpc_keep_alive = 0;
347
-	}
345
+
348 346
 	INFO("jsonrpc_keep_alive set to %d\n", jsonrpc_keep_alive);
349 347
 	return 0;
350 348
 }
Browse code

janssonrpcc: add new parameter "keep_alive" - send periodic tcp keepalive packets to server in order to prevent connection from closing

Emmanuel Schmidbauer authored on 11/01/2019 17:47:07
Showing 1 changed files
... ...
@@ -51,6 +51,7 @@ void mod_destroy(void);
51 51
 int parse_server_param(modparam_t type, void* val);
52 52
 int parse_retry_codes_param(modparam_t type, void* val);
53 53
 int parse_min_ttl_param(modparam_t type, void* val);
54
+int parse_keep_alive_param(modparam_t type, void* val);
54 55
 static int fixup_req(void** param, int param_no);
55 56
 static int fixup_req_free(void** param, int param_no);
56 57
 static int fixup_notify(void** param, int param_no);
... ...
@@ -100,6 +101,7 @@ static param_export_t mod_params[]={
100 101
 	{"retry_codes",  STR_PARAM|USE_FUNC_PARAM, (void*)parse_retry_codes_param},
101 102
 	{"min_srv_ttl", INT_PARAM|USE_FUNC_PARAM, (void*)parse_min_ttl_param},
102 103
 	{"result_pv",   STR_PARAM,                &result_pv_str.s},
104
+	{"keep_alive", INT_PARAM|USE_FUNC_PARAM, (void*)parse_keep_alive_param},
103 105
 	{ 0,0,0 }
104 106
 };
105 107
 
... ...
@@ -333,6 +335,20 @@ int parse_min_ttl_param(modparam_t type, void* val)
333 335
 	return 0;
334 336
 }
335 337
 
338
+int parse_keep_alive_param(modparam_t type, void* val)
339
+{
340
+	if (PARAM_TYPE_MASK(type) != INT_PARAM) {
341
+		ERR("keep_alive must be of type %d, not %d!\n", INT_PARAM, type);
342
+		return -1;
343
+	}
344
+	jsonrpc_keep_alive = (int)(long)val;
345
+	if (jsonrpc_keep_alive < 0) {
346
+		jsonrpc_keep_alive = 0;
347
+	}
348
+	INFO("jsonrpc_keep_alive set to %d\n", jsonrpc_keep_alive);
349
+	return 0;
350
+}
351
+
336 352
 /* Fixup Functions */
337 353
 
338 354
 static int fixup_req(void** param, int param_no)
Browse code

janssonrpcc: updated to the new mod interface

Victor Seva authored on 28/09/2018 07:54:25
Showing 1 changed files
... ...
@@ -107,18 +107,16 @@ static param_export_t mod_params[]={
107 107
  * Exports
108 108
  */
109 109
 struct module_exports exports = {
110
-		"janssonrpcc",       /* module name */
111
-		DEFAULT_DLFLAGS, /* dlopen flags */
112
-		cmds,            /* Exported functions */
113
-		mod_params,      /* Exported parameters */
114
-		0,               /* exported statistics */
115
-		0,               /* exported MI functions */
116
-		0,               /* exported pseudo-variables */
117
-		0,               /* extra processes */
118
-		mod_init,        /* module initialization function */
119
-		0,               /* response function*/
120
-		mod_destroy,     /* destroy function */
121
-		child_init       /* per-child init function */
110
+	"janssonrpcc",       /* module name */
111
+	DEFAULT_DLFLAGS, /* dlopen flags */
112
+	cmds,            /* cmd (cfg function) exports */
113
+	mod_params,      /* param exports */
114
+	0,               /* RPC method exports */
115
+	0,               /* pseudo-variables exports */
116
+	0,               /* response handling function */
117
+	mod_init,        /* module init function */
118
+	child_init,      /* per-child init function */
119
+	mod_destroy      /* module destroy function */
122 120
 };
123 121
 
124 122
 
Browse code

janssonrpcc: use size of data type to allocate memory

Daniel-Constantin Mierla authored on 29/12/2017 10:17:25
Showing 1 changed files
... ...
@@ -225,7 +225,7 @@ void mod_destroy(void)
225 225
 int parse_server_param(modparam_t type, void* val)
226 226
 {
227 227
 	if(global_server_group == NULL) {
228
-		global_server_group = shm_malloc(sizeof(void*));
228
+		global_server_group = (jsonrpc_server_group_t**)shm_malloc(sizeof(jsonrpc_server_group_t*));
229 229
 		*global_server_group = NULL;
230 230
 	}
231 231
 	return jsonrpc_parse_server((char*)val, global_server_group);
Browse code

janssonrpcc: do not lock in mod destroy

- all children are killed already and if lock was already set results in
a deadlock

Daniel-Constantin Mierla authored on 23/09/2017 07:30:08
Showing 1 changed files
... ...
@@ -215,7 +215,6 @@ static int child_init(int rank)
215 215
 void mod_destroy(void)
216 216
 {
217 217
 	if(jsonrpc_server_group_lock) {
218
-		lock_get(jsonrpc_server_group_lock); /* blocking */
219 218
 		lock_dealloc(jsonrpc_server_group_lock);
220 219
 	}
221 220
 
Browse code

jsonrpcc: do not declare variables in header files

- avoid declaration in each .c that includes the .h

Daniel-Constantin Mierla authored on 23/09/2017 07:29:10
Showing 1 changed files
... ...
@@ -61,6 +61,15 @@ int  pipe_fds[2] = {-1,-1};
61 61
 
62 62
 struct tm_binds tmb;
63 63
 
64
+/* globals */
65
+int cmd_pipe;
66
+str result_pv_str;
67
+retry_range_t* global_retry_ranges;
68
+#ifndef TEST
69
+jansson_to_val_f jsontoval;
70
+pv_spec_t jsonrpc_result_pv;
71
+#endif
72
+
64 73
 /*
65 74
  * Exported Functions
66 75
  */
Browse code

janssonrcp-c: Fix segfault in mod_destroy

Check variable before dereferencing it.

Alex Hermann authored on 24/02/2017 13:23:57
Showing 1 changed files
... ...
@@ -205,8 +205,10 @@ static int child_init(int rank)
205 205
 
206 206
 void mod_destroy(void)
207 207
 {
208
-	lock_get(jsonrpc_server_group_lock); /* blocking */
209
-	if(jsonrpc_server_group_lock) lock_dealloc(jsonrpc_server_group_lock);
208
+	if(jsonrpc_server_group_lock) {
209
+		lock_get(jsonrpc_server_group_lock); /* blocking */
210
+		lock_dealloc(jsonrpc_server_group_lock);
211
+	}
210 212
 
211 213
 	free_server_group(global_server_group);
212 214
 	CHECK_AND_FREE(global_server_group);
Browse code

jsonrpc modules: removed - (dash) from json rpc modules

- jsonrpc-c is now jsonrpcc
- jsonrpc-s is now jsonrpcs
- janssonrpc-s is now janssonrpcs

Daniel-Constantin Mierla authored on 12/12/2016 20:53:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,364 @@
1
+/**
2
+ * Copyright (C) 2013 Flowroute LLC (flowroute.com)
3
+ *
4
+ * This file is part of Kamailio, a free SIP server.
5
+ *
6
+ * This file is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ *
12
+ * This file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software
19
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
+ *
21
+ */
22
+
23
+#include <arpa/inet.h>
24
+#include <sys/types.h>
25
+#include <errno.h>
26
+
27
+#include "../../core/pvar.h"
28
+#include "../../core/mod_fix.h"
29
+#include "../../core/trim.h"
30
+#include "../../core/sr_module.h"
31
+#include "../../core/timer_proc.h"
32
+#include "../../core/cfg/cfg_struct.h"
33
+#include "../tm/tm_load.h"
34
+#include "../jansson/jansson_utils.h"
35
+
36
+#include "janssonrpc_funcs.h"
37
+#include "janssonrpc_request.h"
38
+#include "janssonrpc_io.h"
39
+#include "janssonrpc_connect.h"
40
+#include "janssonrpc_server.h"
41
+#include "janssonrpc_srv.h"
42
+#include "janssonrpc.h"
43
+
44
+
45
+MODULE_VERSION
46
+
47
+
48
+static int mod_init(void);
49
+static int child_init(int);
50
+void mod_destroy(void);
51
+int parse_server_param(modparam_t type, void* val);
52
+int parse_retry_codes_param(modparam_t type, void* val);
53
+int parse_min_ttl_param(modparam_t type, void* val);
54
+static int fixup_req(void** param, int param_no);
55
+static int fixup_req_free(void** param, int param_no);
56
+static int fixup_notify(void** param, int param_no);
57
+static int fixup_notify_free(void** param, int param_no);
58
+int		  fixup_pvar_shm(void** param, int param_no);
59
+
60
+int  pipe_fds[2] = {-1,-1};
61
+
62
+struct tm_binds tmb;
63
+
64
+/*
65
+ * Exported Functions
66
+ */
67
+int jsonrpc_request_no_options(struct sip_msg* msg,
68
+		char* conn,
69
+		char* method,
70
+		char* params) {
71
+	return jsonrpc_request(msg, conn, method, params, NULL);
72
+}
73
+
74
+static cmd_export_t cmds[]={
75
+	{"janssonrpc_request", (cmd_function)jsonrpc_request,
76
+		4, fixup_req, fixup_req_free, ANY_ROUTE},
77
+	{"jsansonrpc_request", (cmd_function)jsonrpc_request_no_options,
78
+		3, fixup_req, fixup_req_free, ANY_ROUTE},
79
+	{"janssonrpc_notification", (cmd_function)jsonrpc_notification,
80
+		3, fixup_notify, fixup_notify_free, ANY_ROUTE},
81
+	{"mod_janssonrpc_request", (cmd_function)mod_jsonrpc_request,
82
+		0, 0, 0, 0},
83
+	{0, 0, 0, 0, 0, 0}
84
+};
85
+
86
+/*
87
+ * Script Parameters
88
+ */
89
+static param_export_t mod_params[]={
90
+	{"server",      STR_PARAM|USE_FUNC_PARAM, (void*)parse_server_param},
91
+	{"retry_codes",  STR_PARAM|USE_FUNC_PARAM, (void*)parse_retry_codes_param},
92
+	{"min_srv_ttl", INT_PARAM|USE_FUNC_PARAM, (void*)parse_min_ttl_param},
93
+	{"result_pv",   STR_PARAM,                &result_pv_str.s},
94
+	{ 0,0,0 }
95
+};
96
+
97
+/*
98
+ * Exports
99
+ */
100
+struct module_exports exports = {
101
+		"janssonrpcc",       /* module name */
102
+		DEFAULT_DLFLAGS, /* dlopen flags */
103
+		cmds,            /* Exported functions */
104
+		mod_params,      /* Exported parameters */
105
+		0,               /* exported statistics */
106
+		0,               /* exported MI functions */
107
+		0,               /* exported pseudo-variables */
108
+		0,               /* extra processes */
109
+		mod_init,        /* module initialization function */
110
+		0,               /* response function*/
111
+		mod_destroy,     /* destroy function */
112
+		child_init       /* per-child init function */
113
+};
114
+
115
+
116
+static int mod_init(void)
117
+{
118
+	/* load the tm functions  */
119
+	if(load_tm_api(&tmb)<0) return -1;
120
+
121
+	/* load json_to_val from json module */
122
+	jsontoval = (jansson_to_val_f)find_export("jansson_to_val", 0, 0);
123
+	if(jsontoval == 0) {
124
+		ERR("ERROR:jsonrpc:mod_init: cannot import json_to_val\n");
125
+		return -1;
126
+	}
127
+
128
+	/* setup result pvar */
129
+	if (result_pv_str.s == NULL)
130
+		result_pv_str.s = JSONRPC_RESULT_STR;
131
+	result_pv_str.len = strlen(result_pv_str.s);
132
+
133
+	if(pv_parse_spec(&result_pv_str, &jsonrpc_result_pv)<0) {
134
+		ERR("cannot parse result_pv: %.*s\n", STR(result_pv_str));
135
+		return -1;
136
+	}
137
+
138
+	if(!(pv_is_w(&jsonrpc_result_pv))) {
139
+		ERR("%.*s is read only\n", STR(result_pv_str));
140
+		return -1;
141
+	}
142
+
143
+	register_procs(1);
144
+	register_basic_timers(1);
145
+
146
+	if (pipe(pipe_fds) < 0) {
147
+		ERR("pipe() failed\n");
148
+		return -1;
149
+	}
150
+
151
+	if(jsonrpc_min_srv_ttl < ABSOLUTE_MIN_SRV_TTL) {
152
+		jsonrpc_min_srv_ttl = JSONRPC_DEFAULT_MIN_SRV_TTL; /* 5s */
153
+	}
154
+
155
+	return 0;
156
+}
157
+
158
+static int child_init(int rank)
159
+{
160
+	int pid;
161
+
162
+	if (rank!=PROC_INIT)
163
+		cmd_pipe = pipe_fds[1];
164
+
165
+	if (rank!=PROC_MAIN)
166
+		return 0;
167
+
168
+	jsonrpc_server_group_lock = lock_alloc();
169
+	if(jsonrpc_server_group_lock == NULL) {
170
+		ERR("cannot allocate the server_group_lock\n");
171
+		return -1;
172
+	}
173
+
174
+	if(lock_init(jsonrpc_server_group_lock) == 0) {
175
+		ERR("failed to initialized the server_group_lock\n");
176
+		lock_dealloc(jsonrpc_server_group_lock);
177
+		return -1;
178
+	}
179
+
180
+	srv_cb_params_t* params = (srv_cb_params_t*)shm_malloc(sizeof(srv_cb_params_t));
181
+	CHECK_MALLOC(params);
182
+	params->cmd_pipe = pipe_fds[1];
183
+	params->srv_ttl = jsonrpc_min_srv_ttl;
184
+
185
+	/* start timer to check SRV ttl every second */
186
+	if(fork_basic_timer(PROC_TIMER, "jsonrpc SRV timer", 1 /*socks flag*/,
187
+				refresh_srv_cb, (void*)params, ABSOLUTE_MIN_SRV_TTL)<0) {
188
+		ERR("Failed to start SRV timer\n");
189
+		return -1;
190
+	}
191
+
192
+	pid=fork_process(PROC_RPC, "jsonrpc io handler", 1);
193
+
194
+	if (pid<0)
195
+		return -1; /* error */
196
+	if(pid==0){
197
+		/* child */
198
+		if (cfg_child_init()) return -1;
199
+		close(pipe_fds[1]);
200
+		return jsonrpc_io_child_process(pipe_fds[0]);
201
+	}
202
+
203
+	return 0;
204
+}
205
+
206
+void mod_destroy(void)
207
+{
208
+	lock_get(jsonrpc_server_group_lock); /* blocking */
209
+	if(jsonrpc_server_group_lock) lock_dealloc(jsonrpc_server_group_lock);
210
+
211
+	free_server_group(global_server_group);
212
+	CHECK_AND_FREE(global_server_group);
213
+}
214
+
215
+int parse_server_param(modparam_t type, void* val)
216
+{
217
+	if(global_server_group == NULL) {
218
+		global_server_group = shm_malloc(sizeof(void*));
219
+		*global_server_group = NULL;
220
+	}
221
+	return jsonrpc_parse_server((char*)val, global_server_group);
222
+}
223
+
224
+/* helper function for parse_retry_codes_param */
225
+int s2i(char* str, int* result)
226
+{
227
+	char* endptr;
228
+	errno = 0;
229
+
230
+	long val = strtol(str, &endptr, 10);
231
+
232
+	if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
233
+			|| (errno != 0 && val == 0)) {
234
+		ERR("%s is not a number: %s\n", str, strerror(errno));
235
+		return -1;
236
+	}
237
+
238
+	if (endptr == str) {
239
+		ERR("failed to convert %s to integer\n", str);
240
+		return -1;
241
+	}
242
+
243
+	*result = (int)val;
244
+	return 0;
245
+}
246
+
247
+int parse_retry_codes_param(modparam_t type, void* val)
248
+{
249
+	if (val==NULL) {
250
+		ERR("retry_codes cannot be NULL!\n");
251
+		return -1;
252
+	}
253
+
254
+	if (PARAM_TYPE_MASK(type) != STR_PARAM) {
255
+		ERR("retry_codes must be a string\n");
256
+		return -1;
257
+	}
258
+
259
+	global_retry_ranges = NULL;
260
+
261
+	char* save_comma;
262
+	char* save_elipse;
263
+	char* token;
264
+	char* start_s;
265
+	int start;
266
+	char* end_s;
267
+	int end;
268
+	char* codes_s = (char*)val;
269
+
270
+	char* tmp;
271
+	retry_range_t** tmp_range;
272
+	tmp_range = &global_retry_ranges;
273
+	for (tmp = codes_s; ; tmp = NULL) {
274
+		token = strtok_r(tmp, ",", &save_comma);
275
+		if (token == NULL)
276
+			break;
277
+
278
+		start_s = strtok_r(token, ".", &save_elipse);
279
+		if (start_s == NULL) {
280
+			continue;
281
+		}
282
+
283
+		if(s2i(start_s, &start)<0) return -1;
284
+
285
+		*tmp_range = shm_malloc(sizeof(retry_range_t));
286
+		CHECK_MALLOC(*tmp_range);
287
+		memset(*tmp_range, 0, sizeof(retry_range_t));
288
+
289
+		(*tmp_range)->start = start;
290
+
291
+		end_s = strtok_r(NULL, ".", &save_elipse);
292
+		if (end_s == NULL) {
293
+			end_s = start_s;
294
+		}
295
+
296
+		if(s2i(end_s, &end)<0) return -1;
297
+		(*tmp_range)->end = end;
298
+
299
+		tmp_range = &((*tmp_range)->next);
300
+	}
301
+
302
+	return 0;
303
+}
304
+
305
+int parse_min_ttl_param(modparam_t type, void* val)
306
+{
307
+	if (val==0) {
308
+		ERR("min_srv_ttl cannot be NULL!\n");
309
+		return -1;
310
+	}
311
+
312
+	if (PARAM_TYPE_MASK(type) != INT_PARAM) {
313
+		ERR("min_srv_ttl must be of type %d, not %d!\n", INT_PARAM, type);
314
+		return -1;
315
+	}
316
+
317
+	jsonrpc_min_srv_ttl = (int)(long)val;
318
+	if(jsonrpc_min_srv_ttl < ABSOLUTE_MIN_SRV_TTL) {
319
+		ERR("Cannot set min_srv_ttl lower than %d", ABSOLUTE_MIN_SRV_TTL);
320
+		return -1;
321
+	}
322
+
323
+	INFO("min_srv_ttl set to %d\n", jsonrpc_min_srv_ttl);
324
+
325
+	return 0;
326
+}
327
+
328
+/* Fixup Functions */
329
+
330
+static int fixup_req(void** param, int param_no)
331
+{
332
+	if (param_no <= 4) {
333
+		return fixup_spve_null(param, 1);
334
+	}
335
+	ERR("function takes at most 4 parameters.\n");
336
+	return -1;
337
+}
338
+
339
+static int fixup_req_free(void** param, int param_no)
340
+{
341
+	if (param_no <= 4) {
342
+		return fixup_free_spve_null(param, 1);
343
+	}
344
+	ERR("function takes at most 4 parameters.\n");
345
+	return -1;
346
+}
347
+
348
+static int fixup_notify(void** param, int param_no)
349
+{
350
+	if (param_no <= 3) {
351
+		return fixup_spve_null(param, 1);
352
+	}
353
+	ERR("function takes at most 3 parameters.\n");
354
+	return -1;
355
+}
356
+
357
+static int fixup_notify_free(void** param, int param_no)
358
+{
359
+	if (param_no <= 3) {
360
+		return fixup_free_spve_null(param, 1);
361
+	}
362
+	ERR("function takes at most 3 parameters.\n");
363
+	return -1;
364
+}