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 5 changed files
... ...
@@ -36,10 +36,10 @@ typedef int ka_state;
36 36
 #define KA_STATE_UP 1
37 37
 #define KA_STATE_DOWN 2
38 38
 
39
-typedef void (*ka_statechanged_f)(str uri, int state, void *user_attr);
40
-typedef int (*ka_add_dest_f)(str uri, str owner, int flags,
39
+typedef void (*ka_statechanged_f)(str *uri, int state, void *user_attr);
40
+typedef int (*ka_add_dest_f)(str *uri, str *owner, int flags,
41 41
 		ka_statechanged_f callback, void *user_attr);
42
-typedef ka_state (*ka_dest_state_f)(str uri);
42
+typedef ka_state (*ka_dest_state_f)(str *uri);
43 43
 
44 44
 typedef struct keepalive_api
45 45
 {
... ...
@@ -70,4 +70,4 @@ static inline int keepalive_load_api(keepalive_api_t *api)
70 70
 	return 0;
71 71
 }
72 72
 
73
-#endif /* __KEEPALIVE_API_H__ */
73
+#endif /* __KEEPALIVE_API_H__ */
74 74
\ No newline at end of file
... ...
@@ -45,7 +45,7 @@
45 45
 #define KA_PROBE_INACTIVE 2
46 46
 #define KA_PROBE_ONLYFLAGGED 3
47 47
 
48
-typedef void (*ka_statechanged_f)(str uri, int state, void *user_attr);
48
+typedef void (*ka_statechanged_f)(str *uri, int state, void *user_attr);
49 49
 
50 50
 typedef struct _ka_dest
51 51
 {
... ...
@@ -73,9 +73,9 @@ typedef struct _ka_destinations_list
73 73
 
74 74
 extern ka_destinations_list_t *ka_destinations_list;
75 75
 
76
-int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback,
76
+int ka_add_dest(str *uri, str *owner, int flags, ka_statechanged_f callback,
77 77
 		void *user_attr);
78
-int ka_destination_state(str uri);
79
-int ka_str_copy(str src, str *dest, char *prefix);
78
+int ka_destination_state(str *uri);
79
+int ka_str_copy(str *src, str *dest, char *prefix);
80 80
 
81 81
 #endif
... ...
@@ -59,13 +59,13 @@ int bind_keepalive(keepalive_api_t *api)
59 59
 /*
60 60
  * Add a new destination in keepalive pool
61 61
  */
62
-int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback,
62
+int ka_add_dest(str *uri, str *owner, int flags, ka_statechanged_f callback,
63 63
 		void *user_attr)
64 64
 {
65 65
 	struct sip_uri _uri;
66 66
 	ka_dest_t *dest;
67 67
 
68
-	LM_INFO("adding destination: %.*s\n", uri.len, uri.s);
68
+	LM_INFO("adding destination: %.*s\n", uri->len, uri->s);
69 69
 
70 70
 	dest = (ka_dest_t *)shm_malloc(sizeof(ka_dest_t));
71 71
 	if(dest == NULL) {
... ...
@@ -74,8 +74,8 @@ int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback,
74 74
 	}
75 75
 	memset(dest, 0, sizeof(ka_dest_t));
76 76
 
77
-	if(uri.len >= 4 && (!strncasecmp("sip:", uri.s, 4)
78
-							   || !strncasecmp("sips:", uri.s, 5))) {
77
+	if(uri->len >= 4 && (!strncasecmp("sip:", uri->s, 4)
78
+							   || !strncasecmp("sips:", uri->s, 5))) {
79 79
 		// protocol found
80 80
 		if(ka_str_copy(uri, &(dest->uri), NULL) < 0)
81 81
 			goto err;
... ...
@@ -123,14 +123,15 @@ int ka_rm_dest()
123 123
 /*
124 124
  *
125 125
  */
126
-ka_state ka_destination_state(str destination)
126
+ka_state ka_destination_state(str *destination)
127 127
 {
128 128
 	ka_dest_t *ka_dest = NULL;
129 129
 
130 130
 	for(ka_dest = ka_destinations_list->first; ka_dest != NULL;
131 131
 			ka_dest = ka_dest->next) {
132
-		if(strncmp(ka_dest->uri.s + 4, destination.s, ka_dest->uri.len - 4)
133
-				== 0) {
132
+		if((destination->len == ka_dest->uri.len - 4)
133
+				&& (strncmp(ka_dest->uri.s + 4, destination->s, ka_dest->uri.len - 4)
134
+						== 0)) {
134 135
 			break;
135 136
 		}
136 137
 	}
... ...
@@ -122,7 +122,7 @@ static void ka_options_callback(
122 122
 		ka_run_route(msg, &uri, state_routes[state]);
123 123
 
124 124
 		if(ka_dest->statechanged_clb != NULL) {
125
-			ka_dest->statechanged_clb(ka_dest->uri, state, ka_dest->user_attr);
125
+			ka_dest->statechanged_clb(&ka_dest->uri, state, ka_dest->user_attr);
126 126
 		}
127 127
 
128 128
 		ka_dest->state = state;
... ...
@@ -174,11 +174,11 @@ static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
174 174
 /*
175 175
  * copy str into dynamically allocated shm memory
176 176
  */
177
-int ka_str_copy(str src, str *dest, char *prefix)
177
+int ka_str_copy(str *src, str *dest, char *prefix)
178 178
 {
179 179
 	int lp = prefix ? strlen(prefix) : 0;
180 180
 
181
-	dest->s = (char *)shm_malloc((src.len + 1 + lp) * sizeof(char));
181
+	dest->s = (char *)shm_malloc((src->len + 1 + lp) * sizeof(char));
182 182
 	if(dest->s == NULL) {
183 183
 		LM_ERR("no more memory!\n");
184 184
 		return -1;
... ...
@@ -186,9 +186,9 @@ int ka_str_copy(str src, str *dest, char *prefix)
186 186
 
187 187
 	if(prefix)
188 188
 		strncpy(dest->s, prefix, lp);
189
-	strncpy(dest->s + lp, src.s, src.len);
190
-	dest->s[src.len + lp] = '\0';
191
-	dest->len = src.len + lp;
189
+	strncpy(dest->s + lp, src->s, src->len);
190
+	dest->s[src->len + lp] = '\0';
191
+	dest->len = src->len + lp;
192 192
 
193 193
 	return 0;
194 194
 }
... ...
@@ -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