Browse code

keepalive: check if the destinations list is initialized at runtime use

- do not initialize, because is no longer seen in all processes,
initialization must be done in mod init

Daniel-Constantin Mierla authored on 05/02/2021 08:55:58
Showing 1 changed files
... ...
@@ -201,8 +201,10 @@ static int w_add_destination(sip_msg_t *msg, char *uri, char *owner)
201 201
  */
202 202
 static int ki_add_destination(sip_msg_t *msg, str *uri, str *owner)
203 203
 {
204
-	if(ka_alloc_destinations_list() < 0)
204
+	if(ka_destinations_list == NULL) {
205
+		LM_ERR("destinations list not initialized\n");
205 206
 		return -1;
207
+	}
206 208
 
207 209
 	return ka_add_dest(uri, owner, 0, ka_ping_interval, 0, 0, 0);
208 210
 }
Browse code

keepalive: init 0 allocated keepalive structures

- GH #2618

Daniel-Constantin Mierla authored on 05/02/2021 08:49:56
Showing 1 changed files
... ...
@@ -253,7 +253,7 @@ static int ka_mod_add_destination(modparam_t type, void *val) {
253 253
 	char *uri = (char *)val;
254 254
 
255 255
 	ka_initial_dest_t *current_position = NULL;
256
-	ka_initial_dest_t *new_destination = (ka_initial_dest_t *) shm_malloc(sizeof(ka_initial_dest_t));
256
+	ka_initial_dest_t *new_destination = (ka_initial_dest_t *) shm_mallocxz(sizeof(ka_initial_dest_t));
257 257
 	new_destination->uri.s = shm_malloc(sizeof(char) * strlen(uri));
258 258
 	new_destination->owner.s = shm_malloc(sizeof(char) * strlen(owner));
259 259
 
... ...
@@ -308,7 +308,7 @@ int ka_alloc_destinations_list()
308 308
 		return 1;
309 309
 	}
310 310
 
311
-	ka_destinations_list = (ka_destinations_list_t *)shm_malloc(
311
+	ka_destinations_list = (ka_destinations_list_t *)shm_mallocxz(
312 312
 			sizeof(ka_destinations_list_t));
313 313
 	if(ka_destinations_list == NULL) {
314 314
 		LM_ERR("no more memory.\n");
Browse code

keepalive: fix initialization when using destination mod param

- Fixed the way the module is initialized, causing problems when using
destination mod parameter because when invoking this parameter some
utilities are not initialized (timers).

Nacho Garcia Segovia authored on 16/12/2020 18:21:59
Showing 1 changed files
... ...
@@ -40,6 +40,8 @@
40 40
 #include "../tm/tm_load.h"
41 41
 #include "../dispatcher/api.h"
42 42
 
43
+#include "../../core/mem/shm_mem.h"
44
+
43 45
 #include "keepalive.h"
44 46
 #include "api.h"
45 47
 
... ...
@@ -55,12 +57,14 @@ static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
55 57
 static int fixup_add_destination(void** param, int param_no);
56 58
 static int w_add_destination(sip_msg_t *msg, char *uri, char *owner);
57 59
 static int w_del_destination(sip_msg_t *msg, char *uri, char *owner);
60
+static int ka_add_initial_destinations();
58 61
 
59 62
 
60 63
 extern struct tm_binds tmb;
61 64
 
62 65
 int ka_ping_interval = 30;
63 66
 ka_destinations_list_t *ka_destinations_list = NULL;
67
+ka_initial_dest_t *ka_initial_destinations_list = NULL;
64 68
 sruid_t ka_sruid;
65 69
 str ka_ping_from = str_init("sip:keepalive@kamailio.org");
66 70
 int ka_counter_del = 5;
... ...
@@ -129,6 +133,10 @@ static int mod_init(void)
129 133
 		return -1;
130 134
 	}
131 135
 
136
+	if (ka_add_initial_destinations() < 0) {
137
+		return -1;
138
+	}
139
+
132 140
 	return 0;
133 141
 }
134 142
 
... ...
@@ -235,24 +243,63 @@ static int ki_del_destination(sip_msg_t *msg, str *uri, str *owner)
235 243
 
236 244
 /*
237 245
  * Function callback executer per module param "destination".
238
- * Is just a wrapper to ka_add_dest() api function
246
+ * It just adds destinations to an initial list to be added later in mod_init
247
+ * This is required because of initialization requirements.
239 248
  */
240
-static int ka_mod_add_destination(modparam_t type, void *val)
241
-{
242
-	if(ka_alloc_destinations_list() < 0)
243
-		return -1;
249
+static int ka_mod_add_destination(modparam_t type, void *val) {
250
+	LM_DBG("adding destination to initial list %s\n", (char *)val);
251
+
252
+	char *owner = "_params";
253
+	char *uri = (char *)val;
254
+
255
+	ka_initial_dest_t *current_position = NULL;
256
+	ka_initial_dest_t *new_destination = (ka_initial_dest_t *) shm_malloc(sizeof(ka_initial_dest_t));
257
+	new_destination->uri.s = shm_malloc(sizeof(char) * strlen(uri));
258
+	new_destination->owner.s = shm_malloc(sizeof(char) * strlen(owner));
259
+
260
+	memcpy(new_destination->uri.s, uri, strlen(uri));
261
+	new_destination->uri.len = strlen(uri);
262
+
263
+	memcpy(new_destination->owner.s, owner, strlen(owner));
264
+	new_destination->owner.len = strlen(owner);
265
+	
266
+	new_destination->next = NULL;
267
+
268
+	if (ka_initial_destinations_list == NULL) {
269
+		ka_initial_destinations_list = new_destination;
270
+	} else {
271
+		current_position = ka_initial_destinations_list;
272
+		while (current_position->next != NULL) {
273
+			current_position = current_position->next;
274
+		}
275
+		current_position->next = new_destination;
276
+	}
244 277
 
245
-	str dest = {val, strlen(val)};
246
-	str owner = str_init("_params");
247
-	LM_DBG("adding destination %.*s\n", dest.len, dest.s);
278
+	return 1;
279
+}
280
+
281
+static int ka_add_initial_destinations() {
282
+	LM_DBG("ka_add_initial_destinations called \n");
283
+	int res = 1;
284
+	ka_initial_dest_t *old_position = NULL;
285
+
286
+	ka_initial_dest_t *current_position = ka_initial_destinations_list;
287
+	while ( res > 0 && current_position != NULL) {
288
+		res = ka_add_dest(&(current_position->uri), &(current_position->owner), 0, ka_ping_interval, 0, 0, 0);
289
+		LM_INFO("Added initial destination Via \"destination\" parameter <%.*s> \n", current_position->uri.len, current_position->uri.s);
290
+		shm_free(current_position->uri.s);
291
+		shm_free(current_position->owner.s);
292
+		old_position = current_position;
293
+		current_position = old_position->next;
294
+		shm_free(old_position);
295
+	}
296
+	ka_initial_destinations_list = NULL;
248 297
 
249
-	return ka_add_dest(&dest, &owner, 0, ka_ping_interval, 0, 0, 0);
298
+	return res;
250 299
 }
251 300
 
252 301
 /*
253 302
  * Allocate global variable *ka_destination_list* if not already done
254
- * WHY:  when specifying static destinations as module param, ka_mod_add_destination() is
255
- *       executed BEFORE mod_init()
256 303
  */
257 304
 int ka_alloc_destinations_list()
258 305
 {
Browse code

keepalive: added uuid to ka_dest structure to avoid passing the whole struct to tm

- This avoids a race condition that may happen processing tm_request callbacl
- Allows to identify uniquely a ka_dest record

Nacho Garcia Segovia authored on 02/09/2020 13:15:41
Showing 1 changed files
... ...
@@ -61,6 +61,7 @@ extern struct tm_binds tmb;
61 61
 
62 62
 int ka_ping_interval = 30;
63 63
 ka_destinations_list_t *ka_destinations_list = NULL;
64
+sruid_t ka_sruid;
64 65
 str ka_ping_from = str_init("sip:keepalive@kamailio.org");
65 66
 int ka_counter_del = 5;
66 67
 
... ...
@@ -124,6 +125,10 @@ static int mod_init(void)
124 125
 	if(ka_alloc_destinations_list() < 0)
125 126
 		return -1;
126 127
 
128
+	if(sruid_init(&ka_sruid, '-', "ka", SRUID_INC) < 0) {
129
+		return -1;
130
+	}
131
+
127 132
 	return 0;
128 133
 }
129 134
 
Browse code

keepalive: manage the `ping_from` config value as str.

Managing the config value as a string (`char*`) doesn't change the string size, so the value is cut off to the default value size (26 chars).

Daniel Martínez Ruiz authored on 24/06/2020 08:28:04
Showing 1 changed files
... ...
@@ -83,8 +83,8 @@ static param_export_t params[] = {
83 83
 	{"ping_interval", PARAM_INT, &ka_ping_interval},
84 84
 	{"destination", PARAM_STRING | USE_FUNC_PARAM,
85 85
 				(void *)ka_mod_add_destination},
86
-	{"ping_from", PARAM_STRING,	&ka_ping_from},
87
-	{"delete_counter", PARAM_INT,	&ka_counter_del},
86
+	{"ping_from", PARAM_STR, &ka_ping_from},
87
+	{"delete_counter", PARAM_INT, &ka_counter_del},
88 88
 	{0, 0, 0}
89 89
 };
90 90
 
Browse code

keepalive: Added callback to run on each destination response.

- This functionality it's just available when using api.h bindings. For exported functions no callback will be used, so this doesn't break cfg or rpc api.
- Modified add_destination function to provide this new callback as a parameter.

Nacho Garcia Segovia authored on 07/05/2020 21:50:10
Showing 1 changed files
... ...
@@ -180,7 +180,7 @@ static int w_add_destination(sip_msg_t *msg, char *uri, char *owner)
180 180
 		return -1;
181 181
 	}
182 182
 
183
-	return ka_add_dest(&suri, &sowner, 0, ka_ping_interval, 0, 0);
183
+	return ka_add_dest(&suri, &sowner, 0, ka_ping_interval, 0, 0, 0);
184 184
 }
185 185
 
186 186
 /*!
... ...
@@ -191,7 +191,7 @@ static int ki_add_destination(sip_msg_t *msg, str *uri, str *owner)
191 191
 	if(ka_alloc_destinations_list() < 0)
192 192
 		return -1;
193 193
 
194
-	return ka_add_dest(uri, owner, 0, ka_ping_interval, 0, 0);
194
+	return ka_add_dest(uri, owner, 0, ka_ping_interval, 0, 0, 0);
195 195
 }
196 196
 
197 197
 /*!
... ...
@@ -241,7 +241,7 @@ static int ka_mod_add_destination(modparam_t type, void *val)
241 241
 	str owner = str_init("_params");
242 242
 	LM_DBG("adding destination %.*s\n", dest.len, dest.s);
243 243
 
244
-	return ka_add_dest(&dest, &owner, 0, ka_ping_interval, 0, 0);
244
+	return ka_add_dest(&dest, &owner, 0, ka_ping_interval, 0, 0, 0);
245 245
 }
246 246
 
247 247
 /*
Browse code

keepalive: custom pinging interval per destination

- This functionality it's just available when using api.h bindings. For exported functions current value ka_ping_interval is used.
- Modified add_destination function to provide this new parameter.
- Now we have one timer per destination, instead of multiple, so we don't need to iterate over all destinations. Timers are cleaned when destinations are removed.

Nacho Garcia Segovia authored on 06/05/2020 21:10:40
Showing 1 changed files
... ...
@@ -51,7 +51,6 @@ static void mod_destroy(void);
51 51
 static int ka_mod_add_destination(modparam_t type, void *val);
52 52
 int ka_init_rpc(void);
53 53
 int ka_alloc_destinations_list();
54
-extern void ka_check_timer(unsigned int ticks, void *param);
55 54
 static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
56 55
 static int fixup_add_destination(void** param, int param_no);
57 56
 static int w_add_destination(sip_msg_t *msg, char *uri, char *owner);
... ...
@@ -125,11 +124,6 @@ static int mod_init(void)
125 124
 	if(ka_alloc_destinations_list() < 0)
126 125
 		return -1;
127 126
 
128
-	if(register_timer(ka_check_timer, NULL, ka_ping_interval) < 0) {
129
-		LM_ERR("failed registering timer\n");
130
-		return -1;
131
-	}
132
-
133 127
 	return 0;
134 128
 }
135 129
 
... ...
@@ -186,7 +180,7 @@ static int w_add_destination(sip_msg_t *msg, char *uri, char *owner)
186 180
 		return -1;
187 181
 	}
188 182
 
189
-	return ka_add_dest(&suri, &sowner, 0, 0, 0);
183
+	return ka_add_dest(&suri, &sowner, 0, ka_ping_interval, 0, 0);
190 184
 }
191 185
 
192 186
 /*!
... ...
@@ -197,7 +191,7 @@ static int ki_add_destination(sip_msg_t *msg, str *uri, str *owner)
197 191
 	if(ka_alloc_destinations_list() < 0)
198 192
 		return -1;
199 193
 
200
-	return ka_add_dest(uri, owner, 0, 0, 0);
194
+	return ka_add_dest(uri, owner, 0, ka_ping_interval, 0, 0);
201 195
 }
202 196
 
203 197
 /*!
... ...
@@ -247,7 +241,7 @@ static int ka_mod_add_destination(modparam_t type, void *val)
247 241
 	str owner = str_init("_params");
248 242
 	LM_DBG("adding destination %.*s\n", dest.len, dest.s);
249 243
 
250
-	return ka_add_dest(&dest, &owner, 0, 0, 0);
244
+	return ka_add_dest(&dest, &owner, 0, ka_ping_interval, 0, 0);
251 245
 }
252 246
 
253 247
 /*
Browse code

keepalive: use dedicated wrappers for kemi exports

Daniel-Constantin Mierla authored on 09/01/2020 16:09:28
Showing 1 changed files
... ...
@@ -188,6 +188,18 @@ static int w_add_destination(sip_msg_t *msg, char *uri, char *owner)
188 188
 
189 189
 	return ka_add_dest(&suri, &sowner, 0, 0, 0);
190 190
 }
191
+
192
+/*!
193
+ *
194
+ */
195
+static int ki_add_destination(sip_msg_t *msg, str *uri, str *owner)
196
+{
197
+	if(ka_alloc_destinations_list() < 0)
198
+		return -1;
199
+
200
+	return ka_add_dest(uri, owner, 0, 0, 0);
201
+}
202
+
191 203
 /*!
192 204
 * @function w_del_destination_f
193 205
 * @abstract deletes given sip uri in allocated destination stack as named ka_alloc_destinations_list
... ...
@@ -214,6 +226,14 @@ static int w_del_destination(sip_msg_t *msg, char *uri, char *owner)
214 226
 	return ka_del_destination(&suri, &sowner);
215 227
 }
216 228
 
229
+/*!
230
+ *
231
+ */
232
+static int ki_del_destination(sip_msg_t *msg, str *uri, str *owner)
233
+{
234
+	return ka_del_destination(uri, owner);
235
+}
236
+
217 237
 /*
218 238
  * Function callback executer per module param "destination".
219 239
  * Is just a wrapper to ka_add_dest() api function
... ...
@@ -290,12 +310,12 @@ static sr_kemi_t sr_kemi_keepalive_exports[] = {
290 310
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
291 311
 	},
292 312
 	{ str_init("keepalive"), str_init("add_destination"),
293
-		SR_KEMIP_INT, ka_add_dest,
294
-		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
295
-			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
313
+		SR_KEMIP_INT, ki_add_destination,
314
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
315
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
296 316
 	},
297 317
 	{ str_init("keepalive"), str_init("del_destination"),
298
-		SR_KEMIP_INT, ka_del_destination,
318
+		SR_KEMIP_INT, ki_del_destination,
299 319
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
300 320
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
301 321
 	},
Browse code

keepalive : added new function del_destination and added cfg functions (#2133)

* keepalive : added new function del_destination and added .cfg functions

added new function del_destination that deletes sip address from list
added add_destination module functions for kamailio.cfg
added counter parameter for attempt count. after count pass, module dont try until it adds again.
added kemi interfaces both of them
added lock to stack
added find_destination function

* keepalive : fixed function names and re-placed un/lock functions

fixed function names and re-placed un/lock functions

* keepalive : added doc for exported new functions ka_add_destination and ka_del_destination [skip ci]

added documents for exported new function ka_add_destination and ka_del_destination
renamed in documents from is_alive to ka_is_alive
added missing rpc command in documents

Yasin CANER authored on 12/12/2019 11:06:24 • Daniel-Constantin Mierla committed on 12/12/2019 11:06:24
Showing 1 changed files
... ...
@@ -52,19 +52,29 @@ static int ka_mod_add_destination(modparam_t type, void *val);
52 52
 int ka_init_rpc(void);
53 53
 int ka_alloc_destinations_list();
54 54
 extern void ka_check_timer(unsigned int ticks, void *param);
55
-
56 55
 static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
56
+static int fixup_add_destination(void** param, int param_no);
57
+static int w_add_destination(sip_msg_t *msg, char *uri, char *owner);
58
+static int w_del_destination(sip_msg_t *msg, char *uri, char *owner);
59
+
57 60
 
58 61
 extern struct tm_binds tmb;
59 62
 
60 63
 int ka_ping_interval = 30;
61 64
 ka_destinations_list_t *ka_destinations_list = NULL;
65
+str ka_ping_from = str_init("sip:keepalive@kamailio.org");
66
+int ka_counter_del = 5;
67
+
62 68
 
63 69
 
64 70
 static cmd_export_t cmds[] = {
65
-	{"is_alive", (cmd_function)w_cmd_is_alive, 1,
71
+	{"ka_is_alive", (cmd_function)w_cmd_is_alive, 1,
66 72
 			fixup_spve_null, 0, ANY_ROUTE},
67 73
 	// internal API
74
+	{"ka_add_destination", (cmd_function)w_add_destination, 2,
75
+		fixup_add_destination, 0, REQUEST_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE},
76
+	{"ka_del_destination", (cmd_function)w_del_destination, 2,
77
+		fixup_add_destination, 0, ANY_ROUTE},
68 78
 	{"bind_keepalive", (cmd_function)bind_keepalive, 0, 0, 0, 0},
69 79
 	{0, 0, 0, 0, 0, 0}
70 80
 };
... ...
@@ -74,6 +84,8 @@ static param_export_t params[] = {
74 84
 	{"ping_interval", PARAM_INT, &ka_ping_interval},
75 85
 	{"destination", PARAM_STRING | USE_FUNC_PARAM,
76 86
 				(void *)ka_mod_add_destination},
87
+	{"ping_from", PARAM_STRING,	&ka_ping_from},
88
+	{"delete_counter", PARAM_INT,	&ka_counter_del},
77 89
 	{0, 0, 0}
78 90
 };
79 91
 
... ...
@@ -126,6 +138,10 @@ static int mod_init(void)
126 138
  */
127 139
 static void mod_destroy(void)
128 140
 {
141
+	if(ka_destinations_list){
142
+		lock_release(ka_destinations_list->lock);
143
+		lock_dealloc(ka_destinations_list->lock);
144
+	}
129 145
 }
130 146
 
131 147
 
... ...
@@ -139,6 +155,65 @@ int ka_parse_flags(char *flag_str, int flag_len)
139 155
 }
140 156
 
141 157
 
158
+static int fixup_add_destination(void** param, int param_no)
159
+{
160
+	if (param_no == 1 || param_no == 2) {
161
+		return fixup_spve_all(param, param_no);
162
+	}
163
+
164
+	return 0;
165
+}
166
+/*!
167
+* @function w_add_destination
168
+* @abstract adds given sip uri in allocated destination stack as named ka_alloc_destinations_list
169
+* wrapper for ka_add_dest
170
+* @param msg sip message
171
+* @param uri given uri
172
+* @param owner given owner name
173
+*
174
+* @result 1 successful  , -1 fail
175
+*/
176
+static int w_add_destination(sip_msg_t *msg, char *uri, char *owner)
177
+{
178
+	str suri ={0,0};
179
+	str sowner={0,0};
180
+	if(fixup_get_svalue(msg, (gparam_t*)uri, &suri)!=0) {
181
+		LM_ERR("unable to get uri string\n");
182
+		return -1;
183
+	}
184
+	if(fixup_get_svalue(msg, (gparam_t*)owner, &sowner)!=0) {
185
+		LM_ERR("unable to get owner regex\n");
186
+		return -1;
187
+	}
188
+
189
+	return ka_add_dest(&suri, &sowner, 0, 0, 0);
190
+}
191
+/*!
192
+* @function w_del_destination_f
193
+* @abstract deletes given sip uri in allocated destination stack as named ka_alloc_destinations_list
194
+* wrapper for ka_del_destination
195
+* @param msg sip message
196
+* @param uri given uri
197
+* @param owner given owner name, not using now
198
+*
199
+* @result 1 successful  , -1 fail
200
+*/
201
+static int w_del_destination(sip_msg_t *msg, char *uri, char *owner)
202
+{
203
+	str suri ={0,0};
204
+	str sowner={0,0};
205
+	if(fixup_get_svalue(msg, (gparam_t*)uri, &suri)!=0) {
206
+		LM_ERR("unable to get uri string\n");
207
+		return -1;
208
+	}
209
+	if(fixup_get_svalue(msg, (gparam_t*)owner, &sowner)!=0) {
210
+		LM_ERR("unable to get owner regex\n");
211
+		return -1;
212
+	}
213
+
214
+	return ka_del_destination(&suri, &sowner);
215
+}
216
+
142 217
 /*
143 218
  * Function callback executer per module param "destination".
144 219
  * Is just a wrapper to ka_add_dest() api function
... ...
@@ -174,6 +249,11 @@ int ka_alloc_destinations_list()
174 249
 		return -1;
175 250
 	}
176 251
 
252
+	ka_destinations_list->lock = lock_alloc();
253
+	if(!ka_destinations_list->lock) {
254
+		LM_ERR("Couldnt allocate Lock \n");
255
+		return -1;
256
+	}
177 257
 	return 0;
178 258
 }
179 259
 
... ...
@@ -209,7 +289,16 @@ static sr_kemi_t sr_kemi_keepalive_exports[] = {
209 289
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
210 290
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
211 291
 	},
212
-
292
+	{ str_init("keepalive"), str_init("add_destination"),
293
+		SR_KEMIP_INT, ka_add_dest,
294
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
295
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
296
+	},
297
+	{ str_init("keepalive"), str_init("del_destination"),
298
+		SR_KEMIP_INT, ka_del_destination,
299
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
300
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
301
+	},
213 302
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
214 303
 };
215 304
 /* clang-format on */
... ...
@@ -221,4 +310,4 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
221 310
 {
222 311
 	sr_kemi_modules_add(sr_kemi_keepalive_exports);
223 312
 	return 0;
224
-}
225 313
\ No newline at end of file
314
+}
Browse code

keepalive: updated to the new mod interface

Victor Seva authored on 28/09/2018 08:16:38
Showing 1 changed files
... ...
@@ -80,18 +80,16 @@ static param_export_t params[] = {
80 80
 
81 81
 /** module exports */
82 82
 struct module_exports exports = {
83
-	"keepalive",
83
+	"keepalive",     /* module name */
84 84
 	DEFAULT_DLFLAGS, /* dlopen flags */
85
-	cmds,
86
-	params,
87
-	0,		  /* exported statistics */
88
-	0,		  /* exported MI functions - no available anymore since 5.0 */
89
-	0,		  /* exported pseudo-variables */
90
-	0,		  /* extra processes */
91
-	mod_init, /* module initialization function */
92
-	0,
93
-	(destroy_function)mod_destroy,
94
-	0 /* per-child init function */
85
+	cmds,            /* cmd (cfg function) exports */
86
+	params,          /* param exports */
87
+	0,               /* RPC method exports */
88
+	0,               /* pseudo-variables exports */
89
+	0,               /* response handling function */
90
+	mod_init,        /* module init function */
91
+	0,               /* per-child init function */
92
+	mod_destroy      /* module destroy function */
95 93
 };
96 94
 
97 95
 
Browse code

keepalive: functions exported to kemi interface

Daniel-Constantin Mierla authored on 30/05/2017 08:34:16
Showing 1 changed files
... ...
@@ -35,6 +35,8 @@
35 35
 #include <unistd.h>
36 36
 
37 37
 #include "../../core/mod_fix.h"
38
+#include "../../core/kemi.h"
39
+
38 40
 #include "../tm/tm_load.h"
39 41
 #include "../dispatcher/api.h"
40 42
 
... ...
@@ -197,4 +199,28 @@ static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
197 199
 		return -1;
198 200
 	}
199 201
 	return ki_is_alive(msg, &dest);
202
+}
203
+
204
+/**
205
+ *
206
+ */
207
+/* clang-format off */
208
+static sr_kemi_t sr_kemi_keepalive_exports[] = {
209
+	{ str_init("keepalive"), str_init("is_alive"),
210
+		SR_KEMIP_INT, ki_is_alive,
211
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
212
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
213
+	},
214
+
215
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
216
+};
217
+/* clang-format on */
218
+
219
+/**
220
+ *
221
+ */
222
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
223
+{
224
+	sr_kemi_modules_add(sr_kemi_keepalive_exports);
225
+	return 0;
200 226
 }
201 227
\ No newline at end of file
Browse code

keepalive: allow variables in cfg function parameters

- use str* instead of str for api functions

Daniel-Constantin Mierla authored on 29/05/2017 05:18:14
Showing 1 changed files
... ...
@@ -34,6 +34,7 @@
34 34
 #include <sys/types.h>
35 35
 #include <unistd.h>
36 36
 
37
+#include "../../core/mod_fix.h"
37 38
 #include "../tm/tm_load.h"
38 39
 #include "../dispatcher/api.h"
39 40
 
... ...
@@ -50,7 +51,7 @@ int ka_init_rpc(void);
50 51
 int ka_alloc_destinations_list();
51 52
 extern void ka_check_timer(unsigned int ticks, void *param);
52 53
 
53
-static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
54
+static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
54 55
 
55 56
 extern struct tm_binds tmb;
56 57
 
... ...
@@ -59,8 +60,8 @@ ka_destinations_list_t *ka_destinations_list = NULL;
59 60
 
60 61
 
61 62
 static cmd_export_t cmds[] = {
62
-	{"is_alive", (cmd_function)cmd_is_alive, 1, 0, 0,
63
-			REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
63
+	{"is_alive", (cmd_function)w_cmd_is_alive, 1,
64
+			fixup_spve_null, 0, ANY_ROUTE},
64 65
 	// internal API
65 66
 	{"bind_keepalive", (cmd_function)bind_keepalive, 0, 0, 0, 0},
66 67
 	{0, 0, 0, 0, 0, 0}
... ...
@@ -151,7 +152,7 @@ static int ka_mod_add_destination(modparam_t type, void *val)
151 152
 	str owner = str_init("_params");
152 153
 	LM_DBG("adding destination %.*s\n", dest.len, dest.s);
153 154
 
154
-	return ka_add_dest(dest, owner, 0, 0, 0);
155
+	return ka_add_dest(&dest, &owner, 0, 0, 0);
155 156
 }
156 157
 
157 158
 /*
... ...
@@ -176,11 +177,8 @@ int ka_alloc_destinations_list()
176 177
 	return 0;
177 178
 }
178 179
 
179
-
180
-static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
180
+static int ki_is_alive(sip_msg_t *msg, str *dest)
181 181
 {
182
-	str dest = {str1, strlen(str1)};
183
-
184 182
 	ka_state state = ka_destination_state(dest);
185 183
 	// must not return 0, as it stops dialplan execution
186 184
 	if(state == KA_STATE_UNKNOWN) {
... ...
@@ -189,3 +187,14 @@ static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
189 187
 
190 188
 	return state;
191 189
 }
190
+
191
+static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
192
+{
193
+	str dest = STR_NULL;
194
+
195
+	if(fixup_get_svalue(msg, (gparam_t*)str1, &dest)!=0) {
196
+		LM_ERR("failed to get dest parameter\n");
197
+		return -1;
198
+	}
199
+	return ki_is_alive(msg, &dest);
200
+}
192 201
\ No newline at end of file
Browse code

keepalive: format to match default conding style and 80 chars lines

Daniel-Constantin Mierla authored on 05/04/2017 06:52:01
Showing 1 changed files
... ...
@@ -43,14 +43,14 @@
43 43
 MODULE_VERSION
44 44
 
45 45
 
46
-static int  mod_init(void);
46
+static int mod_init(void);
47 47
 static void mod_destroy(void);
48
-static int  ka_mod_add_destination(modparam_t type, void *val);
49
-int  ka_init_rpc(void);
50
-int         ka_alloc_destinations_list();
51
-extern void ka_check_timer(unsigned int ticks, void* param);
48
+static int ka_mod_add_destination(modparam_t type, void *val);
49
+int ka_init_rpc(void);
50
+int ka_alloc_destinations_list();
51
+extern void ka_check_timer(unsigned int ticks, void *param);
52 52
 
53
-static int cmd_is_alive(struct sip_msg* msg, char *str1, char *str2);
53
+static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
54 54
 
55 55
 extern struct tm_binds tmb;
56 56
 
... ...
@@ -58,36 +58,37 @@ int ka_ping_interval = 30;
58 58
 ka_destinations_list_t *ka_destinations_list = NULL;
59 59
 
60 60
 
61
-static cmd_export_t cmds[]={
62
-	{"is_alive",       (cmd_function)cmd_is_alive, 1, 0, 0,
63
-		REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE},
61
+static cmd_export_t cmds[] = {
62
+	{"is_alive", (cmd_function)cmd_is_alive, 1, 0, 0,
63
+			REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
64 64
 	// internal API
65 65
 	{"bind_keepalive", (cmd_function)bind_keepalive, 0, 0, 0, 0},
66
-	{0,0,0,0,0,0}
66
+	{0, 0, 0, 0, 0, 0}
67 67
 };
68 68
 
69 69
 
70
-static param_export_t params[]={
71
-	{"ping_interval", PARAM_INT                  , &ka_ping_interval},
72
-	{"destination"  , PARAM_STRING|USE_FUNC_PARAM, (void *)ka_mod_add_destination},
73
-	{0,0,0}
70
+static param_export_t params[] = {
71
+	{"ping_interval", PARAM_INT, &ka_ping_interval},
72
+	{"destination", PARAM_STRING | USE_FUNC_PARAM,
73
+				(void *)ka_mod_add_destination},
74
+	{0, 0, 0}
74 75
 };
75 76
 
76 77
 
77 78
 /** module exports */
78
-struct module_exports exports= {
79
+struct module_exports exports = {
79 80
 	"keepalive",
80 81
 	DEFAULT_DLFLAGS, /* dlopen flags */
81 82
 	cmds,
82 83
 	params,
83
-	0,          /* exported statistics */
84
-	0,          /* exported MI functions - no available anymore since 5.0 */
85
-	0,          /* exported pseudo-variables */
86
-	0,          /* extra processes */
87
-	mod_init,   /* module initialization function */
84
+	0,		  /* exported statistics */
85
+	0,		  /* exported MI functions - no available anymore since 5.0 */
86
+	0,		  /* exported pseudo-variables */
87
+	0,		  /* extra processes */
88
+	mod_init, /* module initialization function */
88 89
 	0,
89
-	(destroy_function) mod_destroy,
90
-	0           /* per-child init function */
90
+	(destroy_function)mod_destroy,
91
+	0 /* per-child init function */
91 92
 };
92 93
 
93 94
 
... ...
@@ -98,8 +99,7 @@ static int mod_init(void)
98 99
 {
99 100
 	LM_INFO("Initializing keepalive module\n");
100 101
 
101
-	if (load_tm_api( &tmb ) == -1)
102
-	{
102
+	if(load_tm_api(&tmb) == -1) {
103 103
 		LM_ERR("could not load the TM-functions - please load tm module\n");
104 104
 		return -1;
105 105
 	}
... ...
@@ -109,7 +109,7 @@ static int mod_init(void)
109 109
 		return -1;
110 110
 	}
111 111
 
112
-	if (ka_alloc_destinations_list() < 0)
112
+	if(ka_alloc_destinations_list() < 0)
113 113
 		return -1;
114 114
 
115 115
 	if(register_timer(ka_check_timer, NULL, ka_ping_interval) < 0) {
... ...
@@ -132,7 +132,7 @@ static void mod_destroy(void)
132 132
  * parses string to dispatcher dst flags set
133 133
  * returns <0 on failure or int with flag on success.
134 134
  */
135
-int ka_parse_flags( char* flag_str, int flag_len )
135
+int ka_parse_flags(char *flag_str, int flag_len)
136 136
 {
137 137
 	return 0;
138 138
 }
... ...
@@ -144,7 +144,7 @@ int ka_parse_flags( char* flag_str, int flag_len )
144 144
  */
145 145
 static int ka_mod_add_destination(modparam_t type, void *val)
146 146
 {
147
-	if (ka_alloc_destinations_list() < 0)
147
+	if(ka_alloc_destinations_list() < 0)
148 148
 		return -1;
149 149
 
150 150
 	str dest = {val, strlen(val)};
... ...
@@ -161,12 +161,13 @@ static int ka_mod_add_destination(modparam_t type, void *val)
161 161
  */
162 162
 int ka_alloc_destinations_list()
163 163
 {
164
-	if (ka_destinations_list != NULL) {
164
+	if(ka_destinations_list != NULL) {
165 165
 		LM_DBG("ka_destinations_list already allocated\n");
166 166
 		return 1;
167 167
 	}
168 168
 
169
-	ka_destinations_list = (ka_destinations_list_t *) shm_malloc(sizeof(ka_destinations_list_t));
169
+	ka_destinations_list = (ka_destinations_list_t *)shm_malloc(
170
+			sizeof(ka_destinations_list_t));
170 171
 	if(ka_destinations_list == NULL) {
171 172
 		LM_ERR("no more memory.\n");
172 173
 		return -1;
... ...
@@ -176,13 +177,13 @@ int ka_alloc_destinations_list()
176 177
 }
177 178
 
178 179
 
179
-static int cmd_is_alive(struct sip_msg* msg, char *str1, char *str2)
180
+static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
180 181
 {
181 182
 	str dest = {str1, strlen(str1)};
182 183
 
183 184
 	ka_state state = ka_destination_state(dest);
184 185
 	// must not return 0, as it stops dialplan execution
185
-	if (state == KA_STATE_UNKNOWN) {
186
+	if(state == KA_STATE_UNKNOWN) {
186 187
 		return KA_STATE_UP;
187 188
 	}
188 189
 
Browse code

keepalive: declare variables at the beginning of functions

- fix for C standard older than C99

Daniel-Constantin Mierla authored on 05/04/2017 06:45:33
Showing 1 changed files
... ...
@@ -142,7 +142,7 @@ int ka_parse_flags( char* flag_str, int flag_len )
142 142
  * Function callback executer per module param "destination".
143 143
  * Is just a wrapper to ka_add_dest() api function
144 144
  */
145
-static int ka_mod_add_destination(modparam_t type, void *val) 
145
+static int ka_mod_add_destination(modparam_t type, void *val)
146 146
 {
147 147
 	if (ka_alloc_destinations_list() < 0)
148 148
 		return -1;
... ...
@@ -156,7 +156,7 @@ static int ka_mod_add_destination(modparam_t type, void *val)
156 156
 
157 157
 /*
158 158
  * Allocate global variable *ka_destination_list* if not already done
159
- * WHY:  when specifying static destinations as module param, ka_mod_add_destination() is 
159
+ * WHY:  when specifying static destinations as module param, ka_mod_add_destination() is
160 160
  *       executed BEFORE mod_init()
161 161
  */
162 162
 int ka_alloc_destinations_list()
Browse code

keepalive: new module to monitor remote destinations

Guillaume Bour authored on 04/04/2017 14:00:15 • Daniel-Constantin Mierla committed on 04/04/2017 14:00:15
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,190 @@
1
+/**
2
+ * keepalive module - remote destinations probing
3
+ *
4
+ * Copyright (C) 2017 Guillaume Bour <guillaume@bour.cc>
5
+ *
6
+ * This file is part of Kamailio, a free SIP server.
7
+ *
8
+ * Kamailio is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * Kamailio is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
+ */
22
+
23
+/*! \file
24
+ * \ingroup keepalive
25
+ * \brief Keepalive :: Send keepalives
26
+ */
27
+
28
+/*! \defgroup keepalive Keepalive :: Probing remote gateways by sending keepalives
29
+ */
30
+
31
+#include <stdio.h>
32
+#include <string.h>
33
+#include <stdlib.h>
34
+#include <sys/types.h>
35
+#include <unistd.h>
36
+
37
+#include "../tm/tm_load.h"
38
+#include "../dispatcher/api.h"
39
+
40
+#include "keepalive.h"
41
+#include "api.h"
42
+
43
+MODULE_VERSION
44
+
45
+
46
+static int  mod_init(void);
47
+static void mod_destroy(void);
48
+static int  ka_mod_add_destination(modparam_t type, void *val);
49
+int  ka_init_rpc(void);
50
+int         ka_alloc_destinations_list();
51
+extern void ka_check_timer(unsigned int ticks, void* param);
52
+
53
+static int cmd_is_alive(struct sip_msg* msg, char *str1, char *str2);
54
+
55
+extern struct tm_binds tmb;
56
+
57
+int ka_ping_interval = 30;
58
+ka_destinations_list_t *ka_destinations_list = NULL;
59
+
60
+
61
+static cmd_export_t cmds[]={
62
+	{"is_alive",       (cmd_function)cmd_is_alive, 1, 0, 0,
63
+		REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE},
64
+	// internal API
65
+	{"bind_keepalive", (cmd_function)bind_keepalive, 0, 0, 0, 0},
66
+	{0,0,0,0,0,0}
67
+};
68
+
69
+
70
+static param_export_t params[]={
71
+	{"ping_interval", PARAM_INT                  , &ka_ping_interval},
72
+	{"destination"  , PARAM_STRING|USE_FUNC_PARAM, (void *)ka_mod_add_destination},
73
+	{0,0,0}
74
+};
75
+
76
+
77
+/** module exports */
78
+struct module_exports exports= {
79
+	"keepalive",
80
+	DEFAULT_DLFLAGS, /* dlopen flags */
81
+	cmds,
82
+	params,
83
+	0,          /* exported statistics */
84
+	0,          /* exported MI functions - no available anymore since 5.0 */
85
+	0,          /* exported pseudo-variables */
86
+	0,          /* extra processes */
87
+	mod_init,   /* module initialization function */
88
+	0,
89
+	(destroy_function) mod_destroy,
90
+	0           /* per-child init function */
91
+};
92
+
93
+
94
+/**
95
+ * Module initialization
96
+ */
97
+static int mod_init(void)
98
+{
99
+	LM_INFO("Initializing keepalive module\n");
100
+
101
+	if (load_tm_api( &tmb ) == -1)
102
+	{
103
+		LM_ERR("could not load the TM-functions - please load tm module\n");
104
+		return -1;
105
+	}
106
+
107
+	if(ka_init_rpc() < 0) {
108
+		LM_ERR("failed to register RPC commands\n");
109
+		return -1;
110
+	}
111
+
112
+	if (ka_alloc_destinations_list() < 0)
113
+		return -1;
114
+
115
+	if(register_timer(ka_check_timer, NULL, ka_ping_interval) < 0) {
116
+		LM_ERR("failed registering timer\n");
117
+		return -1;
118
+	}
119
+
120
+	return 0;
121
+}
122
+
123
+/*! \brief
124
+ * destroy function
125
+ */
126
+static void mod_destroy(void)
127
+{
128
+}
129
+
130
+
131
+/*! \brief
132
+ * parses string to dispatcher dst flags set
133
+ * returns <0 on failure or int with flag on success.
134
+ */
135
+int ka_parse_flags( char* flag_str, int flag_len )
136
+{
137
+	return 0;
138
+}
139
+
140
+
141
+/*
142
+ * Function callback executer per module param "destination".
143
+ * Is just a wrapper to ka_add_dest() api function
144
+ */
145
+static int ka_mod_add_destination(modparam_t type, void *val) 
146
+{
147
+	if (ka_alloc_destinations_list() < 0)
148
+		return -1;
149
+
150
+	str dest = {val, strlen(val)};
151
+	str owner = str_init("_params");
152
+	LM_DBG("adding destination %.*s\n", dest.len, dest.s);
153
+
154
+	return ka_add_dest(dest, owner, 0, 0, 0);
155
+}
156
+