Browse code

dmq: many safety checks for mem mallocs and function return codes

- added license header in the files

Daniel-Constantin Mierla authored on 26/05/2013 08:43:24
Showing 16 changed files
... ...
@@ -1,8 +1,35 @@
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
1 25
 #include "dmq.h"
2 26
 #include "bind_dmq.h"
3 27
 #include "peer.h"
4 28
 #include "dmq_funcs.h"
5 29
 
30
+/**
31
+ * @brief bind dmq module api
32
+ */
6 33
 int bind_dmq(dmq_api_t* api) {
7 34
 	api->register_dmq_peer = register_dmq_peer;
8 35
 	api->send_message = dmq_send_message;
... ...
@@ -1,12 +1,39 @@
1
-#ifndef BIND_DMQ_H
2
-#define BIND_DMQ_H
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+
26
+#ifndef _BIND_DMQ_H_
27
+#define _BIND_DMQ_H_
3 28
 
4 29
 #include "peer.h"
5 30
 #include "dmqnode.h"
6 31
 #include "dmq_funcs.h"
7 32
 
8
-typedef int (*bcast_message_t)(dmq_peer_t* peer, str* body, dmq_node_t* except, dmq_resp_cback_t* resp_cback, int max_forwards);
9
-typedef int (*send_message_t)(dmq_peer_t* peer, str* body, dmq_node_t* node, dmq_resp_cback_t* resp_cback, int max_forwards);
33
+typedef int (*bcast_message_t)(dmq_peer_t* peer, str* body, dmq_node_t* except,
34
+		dmq_resp_cback_t* resp_cback, int max_forwards);
35
+typedef int (*send_message_t)(dmq_peer_t* peer, str* body, dmq_node_t* node,
36
+		dmq_resp_cback_t* resp_cback, int max_forwards);
10 37
 
11 38
 typedef struct dmq_api {
12 39
 	register_dmq_peer_t register_dmq_peer;
... ...
@@ -21,10 +21,8 @@
21 21
  * along with this program; if not, write to the Free Software 
22 22
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 23
  *
24
- * History:
25
- * --------
26
- *  2010-03-29  initial version (mariusbucur)
27 24
  */
25
+
28 26
 #include <stdio.h>
29 27
 #include <string.h>
30 28
 #include <stdlib.h>
... ...
@@ -135,7 +133,8 @@ struct module_exports exports = {
135 133
 /**
136 134
  * init module function
137 135
  */
138
-static int mod_init(void) {
136
+static int mod_init(void)
137
+{
139 138
 	
140 139
 	if(register_mi_mod(exports.name, mi_cmds)!=0) {
141 140
 		LM_ERR("failed to register MI commands\n");
... ...
@@ -153,12 +152,20 @@ static int mod_init(void) {
153 152
 		LM_ERR("can't load tm functions. TM module probably not loaded\n");
154 153
 		return -1;
155 154
 	}
155
+
156 156
 	/* load peer list - the list containing the module callbacks for dmq */
157
-	
158 157
 	peer_list = init_peer_list();
158
+	if(peer_list==NULL) {
159
+		LM_ERR("cannot initialize peer list\n");
160
+		return -1;
161
+	}
159 162
 	
160 163
 	/* load the dmq node list - the list containing the dmq servers */
161 164
 	node_list = init_dmq_node_list();
165
+	if(node_list==NULL) {
166
+		LM_ERR("cannot initialize node list\n");
167
+		return -1;
168
+	}
162 169
 	
163 170
 	/* register worker processes - add one because of the ping process */
164 171
 	register_procs(num_workers);
... ...
@@ -169,7 +176,8 @@ static int mod_init(void) {
169 176
 		return -1;
170 177
 	}
171 178
 	
172
-	if(parse_server_address(&dmq_notification_address, &dmq_notification_uri) < 0) {
179
+	if(parse_server_address(&dmq_notification_address,
180
+				&dmq_notification_uri) < 0) {
173 181
 		LM_ERR("notification address invalid\n");
174 182
 		return -1;
175 183
 	}
... ...
@@ -182,10 +190,13 @@ static int mod_init(void) {
182 190
 	}
183 191
 	
184 192
 	/**
185
-         * add the dmq notification peer.
193
+	 * add the dmq notification peer.
186 194
 	 * the dmq is a peer itself so that it can receive node notifications
187 195
 	 */
188
-	add_notification_peer();
196
+	if(add_notification_peer()<0) {
197
+		LM_ERR("cannot add notification peer\n");
198
+		return -1;
199
+	}
189 200
 	
190 201
 	startup_time = (int) time(NULL);
191 202
 	
... ...
@@ -196,7 +207,10 @@ static int mod_init(void) {
196 207
 	if(ping_interval < MIN_PING_INTERVAL) {
197 208
 		ping_interval = MIN_PING_INTERVAL;
198 209
 	}
199
-	register_timer(ping_servers, 0, ping_interval);
210
+	if(register_timer(ping_servers, 0, ping_interval)<0) {
211
+		LM_ERR("cannot register timer callback\n");
212
+		return -1;
213
+	}
200 214
 	
201 215
 	return 0;
202 216
 }
... ...
@@ -204,7 +218,8 @@ static int mod_init(void) {
204 218
 /**
205 219
  * initialize children
206 220
  */
207
-static int child_init(int rank) {
221
+static int child_init(int rank)
222
+{
208 223
   	int i, newpid;
209 224
 	if (rank == PROC_MAIN) {
210 225
 		/* fork worker processes */
... ...
@@ -216,7 +231,7 @@ static int child_init(int rank) {
216 231
 				LM_ERR("failed to form process\n");
217 232
 				return -1;
218 233
 			} else if(newpid == 0) {
219
-				// child - this will loop forever
234
+				/* child - this will loop forever */
220 235
 				worker_loop(i);
221 236
 			} else {
222 237
 				workers[i].pid = newpid;
... ...
@@ -259,15 +274,18 @@ static void destroy(void) {
259 274
 	}
260 275
 }
261 276
 
262
-static int handle_dmq_fixup(void** param, int param_no) {
277
+static int handle_dmq_fixup(void** param, int param_no)
278
+{
263 279
  	return 0;
264 280
 }
265 281
 
266
-static int send_dmq_fixup(void** param, int param_no) {
282
+static int send_dmq_fixup(void** param, int param_no)
283
+{
267 284
 	return fixup_spve_null(param, 1);
268 285
 }
269 286
 
270
-static int parse_server_address(str* uri, struct sip_uri* parsed_uri) {
287
+static int parse_server_address(str* uri, struct sip_uri* parsed_uri)
288
+{
271 289
 	if(!uri->s) {
272 290
 		goto empty;
273 291
 	}
... ...
@@ -1,5 +1,30 @@
1
-#ifndef DMQ_H
2
-#define DMQ_H
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+
26
+#ifndef _DMQ_H_
27
+#define _DMQ_H_
3 28
 
4 29
 #include "../../dprint.h"
5 30
 #include "../../error.h"
... ...
@@ -1,7 +1,35 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
1 25
 #include "dmq_funcs.h"
2 26
 #include "notification_peer.h"
3 27
 
4
-dmq_peer_t* register_dmq_peer(dmq_peer_t* peer) {
28
+/**
29
+ * @brief register a DMQ peer
30
+ */
31
+dmq_peer_t* register_dmq_peer(dmq_peer_t* peer)
32
+{
5 33
 	dmq_peer_t* new_peer;
6 34
 	lock_get(&peer_list->lock);
7 35
 	if(search_peer_list(peer_list, peer)) {
... ...
@@ -15,8 +43,18 @@ dmq_peer_t* register_dmq_peer(dmq_peer_t* peer) {
15 43
 	return new_peer;
16 44
 }
17 45
 
18
-void dmq_tm_callback(struct cell *t, int type, struct tmcb_params *ps) {
19
-	dmq_cback_param_t* cb_param = (dmq_cback_param_t*)(*ps->param);
46
+/**
47
+ * @brief dmq tm callback
48
+ */
49
+void dmq_tm_callback(struct cell *t, int type, struct tmcb_params *ps)
50
+{
51
+	dmq_cback_param_t* cb_param;
52
+	
53
+	cb_param = (dmq_cback_param_t*)(*ps->param);
54
+
55
+	if(cb_param==NULL)
56
+		return;
57
+
20 58
 	LM_DBG("dmq_tm_callback start\n");
21 59
 	if(cb_param->resp_cback.f) {
22 60
 		if(cb_param->resp_cback.f(ps->rpl, ps->code, cb_param->node, cb_param->resp_cback.param) < 0) {
... ...
@@ -26,11 +64,15 @@ void dmq_tm_callback(struct cell *t, int type, struct tmcb_params *ps) {
26 64
 	LM_DBG("dmq_tm_callback done\n");
27 65
 	shm_free_node(cb_param->node);
28 66
 	shm_free(cb_param);
67
+	*ps->param = NULL;
29 68
 }
30 69
 
31
-int build_uri_str(str* username, struct sip_uri* uri, str* from) {
70
+int build_uri_str(str* username, struct sip_uri* uri, str* from)
71
+{
32 72
 	/* sip:user@host:port */
33
-	int from_len = username->len + uri->host.len + uri->port.len + 10;
73
+	int from_len;
74
+	
75
+	from_len = username->len + uri->host.len + uri->port.len + 10;
34 76
 	if(!uri->host.s || !uri->host.len) {
35 77
 		LM_ERR("no host in uri\n");
36 78
 		return -1;
... ...
@@ -40,6 +82,10 @@ int build_uri_str(str* username, struct sip_uri* uri, str* from) {
40 82
 		return -1;
41 83
 	}
42 84
 	from->s = pkg_malloc(from_len);
85
+	if(from->s==NULL) {
86
+		LM_ERR("no more pkg\n");
87
+		return -1;
88
+	}
43 89
 	from->len = 0;
44 90
 	
45 91
 	memcpy(from->s, "sip:", 4);
... ...
@@ -63,13 +109,17 @@ int build_uri_str(str* username, struct sip_uri* uri, str* from) {
63 109
 	return 0;
64 110
 }
65 111
 
66
-/* broadcast a dmq message
112
+/**
113
+ * @brief broadcast a dmq message
114
+ *
67 115
  * peer - the peer structure on behalf of which we are sending
68 116
  * body - the body of the message
69 117
  * except - we do not send the message to this node
70 118
  * resp_cback - a response callback that gets called when the transaction is complete
71 119
  */
72
-int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except, dmq_resp_cback_t* resp_cback, int max_forwards) {
120
+int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except,
121
+		dmq_resp_cback_t* resp_cback, int max_forwards)
122
+{
73 123
 	dmq_node_t* node;
74 124
 	
75 125
 	lock_get(&node_list->lock);
... ...
@@ -80,7 +130,8 @@ int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except, dmq_resp_
80 130
 		 *   - itself
81 131
 		 *   - any inactive nodes
82 132
 		 */
83
-		if((except && cmp_dmq_node(node, except)) || node->local || node->status != DMQ_NODE_ACTIVE) {
133
+		if((except && cmp_dmq_node(node, except)) || node->local
134
+				|| node->status != DMQ_NODE_ACTIVE) {
84 135
 			LM_DBG("skipping node %.*s\n", STR_FMT(&node->orig_uri));
85 136
 			node = node->next;
86 137
 			continue;
... ...
@@ -98,13 +149,17 @@ error:
98 149
 	return -1;
99 150
 }
100 151
 
101
-/* send a dmq message
152
+/**
153
+ * @brief send a dmq message
154
+ *
102 155
  * peer - the peer structure on behalf of which we are sending
103 156
  * body - the body of the message
104 157
  * node - we send the message to this node
105 158
  * resp_cback - a response callback that gets called when the transaction is complete
106 159
  */
107
-int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node, dmq_resp_cback_t* resp_cback, int max_forwards) {
160
+int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node,
161
+		dmq_resp_cback_t* resp_cback, int max_forwards)
162
+{
108 163
 	uac_req_t uac_r;
109 164
 	str str_hdr = {0, 0};
110 165
 	str from, to, req_uri;
... ...
@@ -113,8 +168,12 @@ int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node, dmq_resp_cba
113 168
 	int len = 0;
114 169
 	
115 170
 	/* Max-Forwards */
116
-	str_hdr.len = 18 + CRLF_LEN;
171
+	str_hdr.len = 20 + CRLF_LEN;
117 172
 	str_hdr.s = pkg_malloc(str_hdr.len);
173
+	if(str_hdr.s==NULL) {
174
+		LM_ERR("no more pkg\n");
175
+		return -1;
176
+	}
118 177
 	len += sprintf(str_hdr.s, "Max-Forwards: %d%s", max_forwards, CRLF);
119 178
 	str_hdr.len = len;
120 179
 	
... ...
@@ -124,7 +183,8 @@ int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node, dmq_resp_cba
124 183
 	cb_param->node = shm_dup_node(node);
125 184
 	
126 185
 	if(build_uri_str(&peer->peer_id, &dmq_server_uri, &from) < 0) {
127
-		LM_ERR("error building from string [username %.*s]\n", STR_FMT(&peer->peer_id));
186
+		LM_ERR("error building from string [username %.*s]\n",
187
+				STR_FMT(&peer->peer_id));
128 188
 		goto error;
129 189
 	}
130 190
 	if(build_uri_str(&peer->peer_id, &node->uri, &to) < 0) {
... ...
@@ -133,8 +193,8 @@ int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node, dmq_resp_cba
133 193
 	}
134 194
 	req_uri = to;
135 195
 	
136
-	set_uac_req(&uac_r, &dmq_request_method, &str_hdr, body, NULL, TMCB_LOCAL_COMPLETED,
137
-			dmq_tm_callback, (void*)cb_param);
196
+	set_uac_req(&uac_r, &dmq_request_method, &str_hdr, body, NULL,
197
+			TMCB_LOCAL_COMPLETED, dmq_tm_callback, (void*)cb_param);
138 198
 	result = tmb.t_request(&uac_r, &req_uri,
139 199
 			       &to, &from,
140 200
 			       NULL);
... ...
@@ -149,13 +209,28 @@ error:
149 209
 	return -1;
150 210
 }
151 211
 
152
-int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body) {
212
+/**
213
+ * @brief config file function for sending dmq message
214
+ */
215
+int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body)
216
+{
153 217
 	str peer_str;
154
-	get_str_fparam(&peer_str, msg, (fparam_t*)peer);
155 218
 	str to_str;
156
-	get_str_fparam(&to_str, msg, (fparam_t*)to);
157 219
 	str body_str;
158
-	get_str_fparam(&body_str, msg, (fparam_t*)body);
220
+	
221
+	if(get_str_fparam(&peer_str, msg, (fparam_t*)peer)<0) {
222
+		LM_ERR("cannot get peer value\n");
223
+		return -1;
224
+	}
225
+	if(get_str_fparam(&to_str, msg, (fparam_t*)to)<0) {
226
+		LM_ERR("cannot get dst value\n");
227
+		return -1;
228
+	}
229
+	if(get_str_fparam(&body_str, msg, (fparam_t*)body)<0) {
230
+		LM_ERR("cannot get body value\n");
231
+		return -1;
232
+	}
233
+	
159 234
 	LM_INFO("cfg_dmq_send_message: %.*s - %.*s - %.*s\n",
160 235
 		peer_str.len, peer_str.s,
161 236
 		to_str.len, to_str.s,
... ...
@@ -180,27 +255,32 @@ int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body)
180 255
 		LM_ERR("cannot find dmq_node: %.*s\n", to_str.len, to_str.s);
181 256
 		goto error;
182 257
 	}
183
-	if(dmq_send_message(destination_peer, &body_str, to_dmq_node, &notification_callback, 1) < 0) {
258
+	if(dmq_send_message(destination_peer, &body_str, to_dmq_node,
259
+				&notification_callback, 1) < 0) {
184 260
 		LM_ERR("cannot send dmq message\n");
185 261
 		goto error;
186 262
 	}
187
-	return 0;
263
+	return 1;
188 264
 error:
189 265
 	return -1;
190 266
 }
191 267
 
192
-/* pings the servers in the nodelist
268
+/**
269
+ * @brief pings the servers in the nodelist
270
+ *
193 271
  * if the server does not reply to the ping, it is removed from the list
194 272
  * the ping messages are actualy notification requests
195 273
  * this way the ping will have two uses:
196 274
  *   - checks if the servers in the list are up and running
197 275
  *   - updates the list of servers from the other nodes
198 276
  */
199
-void ping_servers(unsigned int ticks,void *param) {
200
-	str* body = build_notification_body();
277
+void ping_servers(unsigned int ticks, void *param) {
278
+	str* body;
201 279
 	int ret;
202 280
 	LM_DBG("ping_servers\n");
203
-	ret = bcast_dmq_message(dmq_notification_peer, body, notification_node, &notification_callback, 1);
281
+	body = build_notification_body();
282
+	ret = bcast_dmq_message(dmq_notification_peer, body, notification_node,
283
+			&notification_callback, 1);
204 284
 	pkg_free(body->s);
205 285
 	pkg_free(body);
206 286
 	if(ret < 0) {
... ...
@@ -1,5 +1,30 @@
1
-#ifndef DMQ_FUNCS_H
2
-#define DMQ_FUNCS_H
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+
26
+#ifndef _DMQ_FUNCS_H_
27
+#define _DMQ_FUNCS_H_
3 28
 
4 29
 #include "../../str.h"
5 30
 #include "../../modules/tm/dlg.h"
... ...
@@ -21,10 +46,13 @@ typedef struct dmq_cback_param {
21 46
 	dmq_node_t* node;
22 47
 } dmq_cback_param_t;
23 48
 
24
-int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body);
49
+int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to,
50
+		char* body);
25 51
 dmq_peer_t* register_dmq_peer(dmq_peer_t* peer);
26
-int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node, dmq_resp_cback_t* resp_cback, int max_forwards);
27
-int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except, dmq_resp_cback_t* resp_cback, int max_forwards);
52
+int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node,
53
+		dmq_resp_cback_t* resp_cback, int max_forwards);
54
+int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except,
55
+		dmq_resp_cback_t* resp_cback, int max_forwards);
28 56
 
29 57
 #endif
30 58
 
... ...
@@ -1,3 +1,28 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ *
24
+ */
25
+
1 26
 #include "../../ut.h"
2 27
 #include "dmqnode.h"
3 28
 #include "dmq.h"
... ...
@@ -12,7 +37,11 @@ str dmq_node_active_str = str_init("active");
12 37
 str dmq_node_disabled_str = str_init("disabled");
13 38
 str dmq_node_timeout_str = str_init("timeout");
14 39
 
15
-str* get_status_str(int status) {
40
+/**
41
+ * @brief get the string status of the node
42
+ */
43
+str* get_status_str(int status)
44
+{
16 45
 	switch(status) {
17 46
 		case DMQ_NODE_ACTIVE: {
18 47
 			return &dmq_node_active_str;
... ...
@@ -29,14 +58,27 @@ str* get_status_str(int status) {
29 58
 	}
30 59
 }
31 60
 
32
-dmq_node_list_t* init_dmq_node_list() {
33
-	dmq_node_list_t* node_list = shm_malloc(sizeof(dmq_node_list_t));
61
+/**
62
+ * @brief initialize dmg node list
63
+ */
64
+dmq_node_list_t* init_dmq_node_list()
65
+{
66
+	dmq_node_list_t* node_list;
67
+	node_list = shm_malloc(sizeof(dmq_node_list_t));
68
+	if(node_list==NULL) {
69
+		LM_ERR("no more shm\n");
70
+		return NULL;
71
+	}
34 72
 	memset(node_list, 0, sizeof(dmq_node_list_t));
35 73
 	lock_init(&node_list->lock);
36 74
 	return node_list;
37 75
 }
38 76
 
39
-int cmp_dmq_node(dmq_node_t* node, dmq_node_t* cmpnode) {
77
+/**
78
+ * @brief compare dmq node addresses
79
+ */
80
+int cmp_dmq_node(dmq_node_t* node, dmq_node_t* cmpnode)
81
+{
40 82
 	if(!node || !cmpnode) {
41 83
 		LM_ERR("cmp_dmq_node - null node received\n");
42 84
 		return -1;
... ...
@@ -45,7 +87,11 @@ int cmp_dmq_node(dmq_node_t* node, dmq_node_t* cmpnode) {
45 87
 	       STR_EQ(node->uri.port, cmpnode->uri.port);
46 88
 }
47 89
 
48
-str* get_param_value(param_t* params, str* param) {
90
+/**
91
+ * @brief get the value of a parameter
92
+ */
93
+str* get_param_value(param_t* params, str* param)
94
+{
49 95
 	while (params) {
50 96
 		if ((params->name.len == param->len) &&
51 97
 		    (strncmp(params->name.s, param->s, param->len) == 0)) {
... ...
@@ -56,7 +102,11 @@ str* get_param_value(param_t* params, str* param) {
56 102
 	return NULL;
57 103
 }
58 104
 
59
-int set_dmq_node_params(dmq_node_t* node, param_t* params) {
105
+/**
106
+ * @brief set the parameters for the node
107
+ */
108
+int set_dmq_node_params(dmq_node_t* node, param_t* params)
109
+{
60 110
 	str* status;
61 111
 	if(!params) {
62 112
 		LM_DBG("no parameters given\n");
... ...
@@ -80,26 +130,45 @@ error:
80 130
 	return -1;
81 131
 }
82 132
 
83
-int set_default_dmq_node_params(dmq_node_t* node) {
133
+/**
134
+ * @brief set default node params
135
+ */
136
+int set_default_dmq_node_params(dmq_node_t* node)
137
+{
84 138
 	node->status = DMQ_NODE_ACTIVE;
85 139
 	return 0;
86 140
 }
87 141
 
142
+/**
143
+ * @brief build a dmq node
144
+ */
88 145
 dmq_node_t* build_dmq_node(str* uri, int shm) {
89
-	dmq_node_t* ret;
146
+	dmq_node_t* ret = NULL;
90 147
 	param_hooks_t hooks;
91 148
 	param_t* params;
92 149
 	
93 150
 	LM_DBG("build_dmq_node %.*s with %s memory\n", STR_FMT(uri), shm?"shm":"private");
94 151
 	
95 152
 	if(shm) {
96
-		ret = shm_malloc(sizeof(*ret));
97
-		memset(ret, 0, sizeof(*ret));
98
-		shm_str_dup(&ret->orig_uri, uri);
153
+		ret = shm_malloc(sizeof(dmq_node_t));
154
+		if(ret==NULL) {
155
+			LM_ERR("no more shm\n");
156
+			goto error;
157
+		}
158
+		memset(ret, 0, sizeof(dmq_node_t));
159
+		if(shm_str_dup(&ret->orig_uri, uri)<0) {
160
+			goto error;
161
+		}
99 162
 	} else {
100
-		ret = pkg_malloc(sizeof(*ret));
101
-		memset(ret, 0, sizeof(*ret));
102
-		pkg_str_dup(&ret->orig_uri, uri);
163
+		ret = pkg_malloc(sizeof(dmq_node_t));
164
+		if(ret==NULL) {
165
+			LM_ERR("no more pkg\n");
166
+			goto error;
167
+		}
168
+		memset(ret, 0, sizeof(dmq_node_t));
169
+		if(pkg_str_dup(&ret->orig_uri, uri)<0) {
170
+			goto error;
171
+		}
103 172
 	}
104 173
 	set_default_dmq_node_params(ret);
105 174
 	if(parse_uri(ret->orig_uri.s, ret->orig_uri.len, &ret->uri) < 0) {
... ...
@@ -131,19 +200,39 @@ dmq_node_t* build_dmq_node(str* uri, int shm) {
131 200
 		LM_DBG("no dmqnode params found\n");		
132 201
 	}
133 202
 	return ret;
203
+
134 204
 error:
205
+	if(ret!=NULL) {
206
+		/* tbd: free uri and params */
207
+		if(shm) {
208
+			shm_free(ret);
209
+		} else {
210
+			pkg_free(ret);
211
+		}
212
+	}
135 213
 	return NULL;
136 214
 }
137 215
 
138
-dmq_node_t* find_dmq_node_uri(dmq_node_list_t* list, str* uri) {
216
+/**
217
+ * @brief find dmq node by uri
218
+ */
219
+dmq_node_t* find_dmq_node_uri(dmq_node_list_t* list, str* uri)
220
+{
139 221
 	dmq_node_t *ret, *find;
140 222
 	find =  build_dmq_node(uri, 0);
223
+	if(find==NULL)
224
+		return NULL;
141 225
 	ret = find_dmq_node(list, find);
142 226
 	destroy_dmq_node(find, 0);
143 227
 	return ret;
144 228
 }
145 229
 
146
-void destroy_dmq_node(dmq_node_t* node, int shm) {
230
+/**
231
+ * @brief destroy dmq node
232
+ */
233
+void destroy_dmq_node(dmq_node_t* node, int shm)
234
+{
235
+	/* tbd: check inner fields */
147 236
 	if(shm) {
148 237
 		shm_free_node(node);
149 238
 	} else {
... ...
@@ -151,7 +240,11 @@ void destroy_dmq_node(dmq_node_t* node, int shm) {
151 240
 	}
152 241
 }
153 242
 
154
-dmq_node_t* find_dmq_node(dmq_node_list_t* list, dmq_node_t* node) {
243
+/**
244
+ * @brief find dmq node
245
+ */
246
+dmq_node_t* find_dmq_node(dmq_node_list_t* list, dmq_node_t* node)
247
+{
155 248
 	dmq_node_t* cur = list->nodes;
156 249
 	while(cur) {
157 250
 		if(cmp_dmq_node(cur, node)) {
... ...
@@ -162,32 +255,58 @@ dmq_node_t* find_dmq_node(dmq_node_list_t* list, dmq_node_t* node) {
162 255
 	return NULL;
163 256
 }
164 257
 
165
-dmq_node_t* shm_dup_node(dmq_node_t* node) {
166
-	dmq_node_t* newnode = shm_malloc(sizeof(dmq_node_t));
258
+/**
259
+ * @brief duplicate dmq node
260
+ */
261
+dmq_node_t* shm_dup_node(dmq_node_t* node)
262
+{
263
+	dmq_node_t* newnode;
264
+	newnode = shm_malloc(sizeof(dmq_node_t));
265
+	if(newnode==NULL) {
266
+		LM_ERR("no more shm\n");
267
+		return NULL;
268
+	}
167 269
 	memcpy(newnode, node, sizeof(dmq_node_t));
168
-	shm_str_dup(&newnode->orig_uri, &node->orig_uri);
169
-	if(parse_uri(newnode->orig_uri.s, newnode->orig_uri.len, &newnode->uri) < 0) {
270
+	newnode->orig_uri.s = NULL;
271
+	if(shm_str_dup(&newnode->orig_uri, &node->orig_uri)<0) {
272
+		goto error;
273
+	}
274
+	if(parse_uri(newnode->orig_uri.s, newnode->orig_uri.len,
275
+				&newnode->uri) < 0) {
170 276
 		LM_ERR("error in parsing node uri\n");
171 277
 		goto error;
172 278
 	}
173 279
 	return newnode;
174 280
 error:
175
-	shm_free(newnode->orig_uri.s);
281
+	if(newnode->orig_uri.s!=NULL)
282
+		shm_free(newnode->orig_uri.s);
176 283
 	shm_free(newnode);
177 284
 	return NULL;
178 285
 }
179 286
 
180
-void shm_free_node(dmq_node_t* node) {
287
+/**
288
+ * @brief free shm dmq node
289
+ */
290
+void shm_free_node(dmq_node_t* node)
291
+{
181 292
 	shm_free(node->orig_uri.s);
182 293
 	shm_free(node);
183 294
 }
184 295
 
185
-void pkg_free_node(dmq_node_t* node) {
296
+/**
297
+ * @brief free pkg dmq node
298
+ */
299
+void pkg_free_node(dmq_node_t* node)
300
+{
186 301
 	pkg_free(node->orig_uri.s);
187 302
 	pkg_free(node);
188 303
 }
189 304
 
190
-int del_dmq_node(dmq_node_list_t* list, dmq_node_t* node) {
305
+/**
306
+ * @brief delete dmq node
307
+ */
308
+int del_dmq_node(dmq_node_list_t* list, dmq_node_t* node)
309
+{
191 310
 	dmq_node_t *cur, **prev;
192 311
 	lock_get(&list->lock);
193 312
 	cur = list->nodes;
... ...
@@ -206,8 +325,14 @@ int del_dmq_node(dmq_node_list_t* list, dmq_node_t* node) {
206 325
 	return 0;
207 326
 }
208 327
 
209
-dmq_node_t* add_dmq_node(dmq_node_list_t* list, str* uri) {
210
-	dmq_node_t* newnode = build_dmq_node(uri, 1);
328
+/**
329
+ * @brief add dmq node
330
+ */
331
+dmq_node_t* add_dmq_node(dmq_node_list_t* list, str* uri)
332
+{
333
+	dmq_node_t* newnode;
334
+	
335
+	newnode = build_dmq_node(uri, 1);
211 336
 	if(!newnode) {
212 337
 		LM_ERR("error creating node\n");
213 338
 		goto error;
... ...
@@ -223,6 +348,9 @@ error:
223 348
 	return NULL;
224 349
 }
225 350
 
351
+/**
352
+ * @brief build dmq node string
353
+ */
226 354
 int build_node_str(dmq_node_t* node, char* buf, int buflen) {
227 355
 	/* sip:host:port;status=[status] */
228 356
 	int len = 0;
... ...
@@ -242,7 +370,8 @@ int build_node_str(dmq_node_t* node, char* buf, int buflen) {
242 370
 	len += 1;
243 371
 	memcpy(buf + len, "status=", 7);
244 372
 	len += 7;
245
-	memcpy(buf + len, get_status_str(node->status)->s, get_status_str(node->status)->len);
373
+	memcpy(buf + len, get_status_str(node->status)->s,
374
+			get_status_str(node->status)->len);
246 375
 	len += get_status_str(node->status)->len;
247 376
 	return len;
248 377
 }
... ...
@@ -1,5 +1,30 @@
1
-#ifndef DMQNODE_H
2
-#define DMQNODE_H
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+
26
+#ifndef _DMQNODE_H_
27
+#define _DMQNODE_H_
3 28
 
4 29
 #include <string.h>
5 30
 #include <stdlib.h>
... ...
@@ -1,6 +1,32 @@
1 1
 #include "../../parser/parse_to.h"
2 2
 #include "../../parser/parse_uri.h"
3 3
 #include "../../sip_msg_clone.h"
4
+/*
5
+ * $Id$
6
+ *
7
+ * dmq module - distributed message queue
8
+ *
9
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
10
+ *
11
+ * This file is part of Kamailio, a free SIP server.
12
+ *
13
+ * Kamailio is free software; you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation; either version 2 of the License, or
16
+ * (at your option) any later version
17
+ *
18
+ * Kamailio is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License 
24
+ * along with this program; if not, write to the Free Software 
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ *
27
+ */
28
+
29
+
4 30
 #include "../../parser/parse_content.h"
5 31
 #include "../../parser/parse_from.h"
6 32
 #include "../../ut.h"
... ...
@@ -14,7 +40,11 @@ str dmq_400_rpl  = str_init("Bad request");
14 40
 str dmq_500_rpl  = str_init("Server Internal Error");
15 41
 str dmq_404_rpl  = str_init("User Not Found");
16 42
 
17
-int dmq_handle_message(struct sip_msg* msg, char* str1, char* str2) {
43
+/**
44
+ * @brief config function to handle dmq messages
45
+ */
46
+int dmq_handle_message(struct sip_msg* msg, char* str1, char* str2)
47
+{
18 48
 	dmq_peer_t* peer;
19 49
 	struct sip_msg* cloned_msg = NULL;
20 50
 	int cloned_msg_len;
... ...
@@ -43,7 +73,10 @@ int dmq_handle_message(struct sip_msg* msg, char* str1, char* str2) {
43 73
 		LM_ERR("error cloning sip message\n");
44 74
 		goto error;
45 75
 	}
46
-	add_dmq_job(cloned_msg, peer);
76
+	if(add_dmq_job(cloned_msg, peer)<0) {
77
+		LM_ERR("failed to add dmq job\n");
78
+		goto error;
79
+	}
47 80
 	return 0;
48 81
 error:
49 82
 	return -1;
... ...
@@ -1,3 +1,31 @@
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+
26
+#ifndef _MESSAGE_H_
27
+#define _MESSAGE_H_
1 28
 
2 29
 int dmq_handle_message(struct sip_msg*, char*, char*);
3 30
 
31
+#endif
... ...
@@ -1,9 +1,39 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ *
24
+ */
25
+
26
+
1 27
 #include "notification_peer.h"
2 28
 
3 29
 static str notification_content_type = str_init("text/plain");
4 30
 dmq_resp_cback_t notification_callback = {&notification_resp_callback_f, 0};
5 31
 
6
-int add_notification_peer() {
32
+/**
33
+ * @brief add notification peer
34
+ */
35
+int add_notification_peer()
36
+{
7 37
 	dmq_peer_t not_peer;
8 38
 	not_peer.callback = dmq_notification_callback;
9 39
 	not_peer.description.s = "notification_peer";
... ...
@@ -28,9 +58,15 @@ error:
28 58
 	return -1;
29 59
 }
30 60
 
31
-dmq_node_t* add_server_and_notify(str* server_address) {
61
+/**
62
+ * @brief add a server node and notify it
63
+ */
64
+dmq_node_t* add_server_and_notify(str* server_address)
65
+{
32 66
 	/* add the notification server to the node list - if any */
33
-	dmq_node_t* node = add_dmq_node(node_list, server_address);
67
+	dmq_node_t* node;
68
+	
69
+	node = add_dmq_node(node_list, server_address);
34 70
 	if(!node) {
35 71
 		LM_ERR("error adding notification node\n");
36 72
 		goto error;
... ...
@@ -58,7 +94,8 @@ error:
58 94
  * 	sip:host2:port2;param2=value2
59 95
  * 	...
60 96
  */
61
-int extract_node_list(dmq_node_list_t* update_list, struct sip_msg* msg) {
97
+int extract_node_list(dmq_node_list_t* update_list, struct sip_msg* msg)
98
+{
62 99
 	int content_length, total_nodes = 0;
63 100
 	str body;
64 101
 	str tmp_uri;
... ...
@@ -115,7 +152,11 @@ error:
115 152
 	return -1;
116 153
 }
117 154
 
118
-int dmq_notification_callback(struct sip_msg* msg, peer_reponse_t* resp) {
155
+/**
156
+ * @brief dmq notification callback
157
+ */
158
+int dmq_notification_callback(struct sip_msg* msg, peer_reponse_t* resp)
159
+{
119 160
 	int nodes_recv;
120 161
 	str* response_body = NULL;
121 162
 	unsigned int maxforwards = 1;
... ...
@@ -137,6 +178,10 @@ int dmq_notification_callback(struct sip_msg* msg, peer_reponse_t* resp) {
137 178
 	nodes_recv = extract_node_list(node_list, msg);
138 179
 	LM_DBG("received %d new nodes\n", nodes_recv);
139 180
 	response_body = build_notification_body();
181
+	if(response_body==NULL) {
182
+		LM_ERR("no response body\n");
183
+		goto error;
184
+	}
140 185
 	resp->content_type = notification_content_type;
141 186
 	resp->reason = dmq_200_rpl;
142 187
 	resp->body = *response_body;
... ...
@@ -145,7 +190,8 @@ int dmq_notification_callback(struct sip_msg* msg, peer_reponse_t* resp) {
145 190
 	/* if we received any new nodes tell about them to the others */
146 191
 	if(nodes_recv > 0 && maxforwards > 0) {
147 192
 		/* maxforwards is set to 0 so that the message is will not be in a spiral */
148
-		bcast_dmq_message(dmq_notification_peer, response_body, 0, &notification_callback, maxforwards);
193
+		bcast_dmq_message(dmq_notification_peer, response_body, 0,
194
+				&notification_callback, maxforwards);
149 195
 	}
150 196
 	LM_DBG("broadcasted message\n");
151 197
 	pkg_free(response_body);
... ...
@@ -161,7 +207,8 @@ error:
161 207
  * sip:host2:port2;param2=value2\r\n
162 208
  * sip:host3:port3;param3=value3
163 209
  */
164
-str* build_notification_body() {
210
+str* build_notification_body()
211
+{
165 212
 	/* the length of the current line describing the server */
166 213
 	int slen;
167 214
 	/* the current length of the body */
... ...
@@ -169,10 +216,19 @@ str* build_notification_body() {
169 216
 	dmq_node_t* cur_node = NULL;
170 217
 	str* body;
171 218
 	body = pkg_malloc(sizeof(str));
219
+	if(body==NULL) {
220
+		LM_ERR("no more pkg\n");
221
+		return NULL;
222
+	}
172 223
 	memset(body, 0, sizeof(str));
173 224
 	/* we allocate a chunk of data for the body */
174 225
 	body->len = NBODY_LEN;
175 226
 	body->s = pkg_malloc(body->len);
227
+	if(body->s==NULL) {
228
+		LM_ERR("no more pkg\n");
229
+		pkg_free(body);
230
+		return NULL;
231
+	}
176 232
 	/* we add each server to the body - each on a different line */
177 233
 	lock_get(&node_list->lock);
178 234
 	cur_node = node_list->nodes;
... ...
@@ -198,16 +254,31 @@ error:
198 254
 	return NULL;
199 255
 }
200 256
 
201
-int request_nodelist(dmq_node_t* node, int forward) {
202
-	str* body = build_notification_body();
257
+/**
258
+ * @brief request node list
259
+ */
260
+int request_nodelist(dmq_node_t* node, int forward)
261
+{
262
+	str* body;
203 263
 	int ret;
204
-	ret = dmq_send_message(dmq_notification_peer, body, node, &notification_callback, forward);
264
+	body = build_notification_body();
265
+	if(body==NULL) {
266
+		LM_ERR("no notification body\n");
267
+		return -1;
268
+	}
269
+	ret = dmq_send_message(dmq_notification_peer, body, node,
270
+			&notification_callback, forward);
205 271
 	pkg_free(body->s);
206 272
 	pkg_free(body);
207 273
 	return ret;
208 274
 }
209 275
 
210
-int notification_resp_callback_f(struct sip_msg* msg, int code, dmq_node_t* node, void* param) {
276
+/**
277
+ * @brief notification response callback
278
+ */
279
+int notification_resp_callback_f(struct sip_msg* msg, int code,
280
+		dmq_node_t* node, void* param)
281
+{
211 282
 	int ret;
212 283
 	LM_DBG("notification_callback_f triggered [%p %d %p]\n", msg, code, param);
213 284
 	if(code == 408) {
... ...
@@ -1,3 +1,30 @@
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+#ifndef _NOTIFICATION_PEER_H_
26
+#define _NOTIFICATION_PEER_H_
27
+
1 28
 #include "../../parser/msg_parser.h"
2 29
 #include "../../parser/parse_content.h"
3 30
 #include "../../ut.h"
... ...
@@ -22,6 +49,8 @@ int request_nodelist(dmq_node_t* node, int forward);
22 49
 dmq_node_t* add_server_and_notify(str* server_address);
23 50
 
24 51
 /* helper functions */
25
-extern int notification_resp_callback_f(struct sip_msg* msg, int code, dmq_node_t* node, void* param);
52
+extern int notification_resp_callback_f(struct sip_msg* msg, int code,
53
+		dmq_node_t* node, void* param);
26 54
 extern dmq_resp_cback_t notification_callback;
27 55
 
56
+#endif
... ...
@@ -1,49 +1,121 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ *
24
+ */
25
+
1 26
 #include "peer.h"
2 27
 #include "dmq.h"
3 28
 
4
-dmq_peer_list_t* init_peer_list() {
5
-	dmq_peer_list_t* peer_list = shm_malloc(sizeof(dmq_peer_list_t));
29
+/**
30
+ * @brief init peer list
31
+ */
32
+dmq_peer_list_t* init_peer_list()
33
+{
34
+	dmq_peer_list_t* peer_list;
35
+	peer_list = shm_malloc(sizeof(dmq_peer_list_t));
36
+	if(peer_list==NULL) {
37
+		LM_ERR("no more shm\n");
38
+		return NULL;
39
+	}
6 40
 	memset(peer_list, 0, sizeof(dmq_peer_list_t));
7 41
 	lock_init(&peer_list->lock);
8 42
 	return peer_list;
9 43
 }
10 44
 
11
-dmq_peer_t* search_peer_list(dmq_peer_list_t* peer_list, dmq_peer_t* peer) {
12
-	dmq_peer_t* cur = peer_list->peers;
45
+/**
46
+ * @brief search peer list
47
+ */
48
+dmq_peer_t* search_peer_list(dmq_peer_list_t* peer_list, dmq_peer_t* peer)
49
+{
50
+	dmq_peer_t* crt;
13 51
 	int len;
14
-	while(cur) {
52
+	crt = peer_list->peers;
53
+	while(crt) {
15 54
 		/* len - the minimum length of the two strings */
16
-		len = cur->peer_id.len < peer->peer_id.len ? cur->peer_id.len:peer->peer_id.len;
17
-		if(strncasecmp(cur->peer_id.s, peer->peer_id.s, len) == 0) {
18
-			return cur;
55
+		len = (crt->peer_id.len < peer->peer_id.len)
56
+			? crt->peer_id.len:peer->peer_id.len;
57
+		if(strncasecmp(crt->peer_id.s, peer->peer_id.s, len) == 0) {
58
+			return crt;
19 59
 		}
20
-		cur = cur->next;
60
+		crt = crt->next;
21 61
 	}
22 62
 	return 0;
23 63
 }
24 64
 
25
-dmq_peer_t* add_peer(dmq_peer_list_t* peer_list, dmq_peer_t* peer) {
26
-	dmq_peer_t* new_peer = shm_malloc(sizeof(dmq_peer_t));
65
+/**
66
+ * @brief add peer
67
+ */
68
+dmq_peer_t* add_peer(dmq_peer_list_t* peer_list, dmq_peer_t* peer)
69
+{
70
+	dmq_peer_t* new_peer = NULL;
71
+	
72
+	new_peer = shm_malloc(sizeof(dmq_peer_t));
73
+	if(new_peer==NULL) {
74
+		LM_ERR("no more shm\n");
75
+		return NULL;
76
+	}
27 77
 	*new_peer = *peer;
28 78
 	
29 79
 	/* copy the str's */
30 80
 	new_peer->peer_id.s = shm_malloc(peer->peer_id.len);
81
+	if(new_peer->peer_id.s==NULL) {
82
+		LM_ERR("no more shm\n");
83
+		shm_free(new_peer);
84
+		return NULL;
85
+	}
31 86
 	memcpy(new_peer->peer_id.s, peer->peer_id.s, peer->peer_id.len);
87
+	new_peer->peer_id.len = peer->peer_id.len;
88
+
32 89
 	new_peer->description.s = shm_malloc(peer->description.len);
90
+	if(new_peer->description.s==NULL) {
91
+		LM_ERR("no more shm\n");
92
+		shm_free(new_peer->peer_id.s);
93
+		shm_free(new_peer);
94
+		return NULL;
95
+	}
33 96
 	memcpy(new_peer->peer_id.s, peer->peer_id.s, peer->peer_id.len);
97
+	new_peer->peer_id.len = peer->peer_id.len;
34 98
 	
35 99
 	new_peer->next = peer_list->peers;
36 100
 	peer_list->peers = new_peer;
37 101
 	return new_peer;
38 102
 }
39 103
 
40
-dmq_peer_t* find_peer(str peer_id) {
104
+/**
105
+ * @brief find peer by id
106
+ */
107
+dmq_peer_t* find_peer(str peer_id)
108
+{
41 109
 	dmq_peer_t foo_peer;
42 110
 	foo_peer.peer_id = peer_id;
43 111
 	return search_peer_list(peer_list, &foo_peer);
44 112
 }
45 113
 
46
-int empty_peer_callback(struct sip_msg* msg, peer_reponse_t* resp) {
114
+/**
115
+ * @empty callback
116
+ */
117
+int empty_peer_callback(struct sip_msg* msg, peer_reponse_t* resp)
118
+{
47 119
 	return 0;
48 120
 }
49 121
 
... ...
@@ -1,5 +1,30 @@
1
-#ifndef PEER_H
2
-#define PEER_H
1
+/**
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ */
24
+
25
+
26
+#ifndef _PEER_H_
27
+#define _PEER_H_
3 28
 
4 29
 #include <string.h>
5 30
 #include <stdlib.h>
... ...
@@ -1,10 +1,37 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * dmq module - distributed message queue
5
+ *
6
+ * Copyright (C) 2011 Bucur Marius - Ovidiu
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License 
21
+ * along with this program; if not, write to the Free Software 
22
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
+ *
24
+ */
25
+
1 26
 #include "dmq.h"
2 27
 #include "peer.h"
3 28
 #include "worker.h"
4 29
 #include "../../data_lump_rpl.h"
5 30
 #include "../../mod_fix.h"
6 31
 
7
-/* set the body of a response */
32
+/**
33
+ * @brief set the body of a response
34
+ */
8 35
 static int set_reply_body(struct sip_msg* msg, str* body, str* content_type)
9 36
 {
10 37
 	char* buf;
... ...
@@ -41,16 +68,23 @@ static int set_reply_body(struct sip_msg* msg, str* body, str* content_type)
41 68
 	return 1;
42 69
 }
43 70
 
44
-void worker_loop(int id) {
45
-	dmq_worker_t* worker = &workers[id];
71
+/**
72
+ * @brief dmq worker loop
73
+ */
74
+void worker_loop(int id)
75
+{
76
+	dmq_worker_t* worker;
46 77
 	dmq_job_t* current_job;
47 78
 	peer_reponse_t peer_response;
48 79
 	int ret_value;
80
+
81
+	worker = &workers[id];
49 82
 	for(;;) {
50 83
 		LM_DBG("dmq_worker [%d %d] getting lock\n", id, my_pid());
51 84
 		lock_get(&worker->lock);
52 85
 		LM_DBG("dmq_worker [%d %d] lock acquired\n", id, my_pid());
53
-		/* multiple lock_release calls might be performed, so remove from queue until empty */
86
+		/* multiple lock_release calls might be performed, so remove
87
+		 * from queue until empty */
54 88
 		do {
55 89
 			/* fill the response with 0's */
56 90
 			memset(&peer_response, 0, sizeof(peer_response));
... ...
@@ -64,13 +98,15 @@ void worker_loop(int id) {
64 98
 				}
65 99
 				/* add the body to the reply */
66 100
 				if(peer_response.body.s) {
67
-					if(set_reply_body(current_job->msg, &peer_response.body, &peer_response.content_type) < 0) {
101
+					if(set_reply_body(current_job->msg, &peer_response.body,
102
+								&peer_response.content_type) < 0) {
68 103
 						LM_ERR("error adding lumps\n");
69 104
 						continue;
70 105
 					}
71 106
 				}
72 107
 				/* send the reply */
73
-				if(slb.freply(current_job->msg, peer_response.resp_code, &peer_response.reason) < 0)
108
+				if(slb.freply(current_job->msg, peer_response.resp_code,
109
+							&peer_response.reason) < 0)
74 110
 				{
75 111
 					LM_ERR("error sending reply\n");
76 112
 				}
... ...
@@ -89,10 +125,17 @@ void worker_loop(int id) {
89 125
 	}
90 126
 }
91 127
 
92
-int add_dmq_job(struct sip_msg* msg, dmq_peer_t* peer) {
128