Browse code

*** empty log message ***

Daniel-Constantin Mierla authored on 16/10/2002 13:58:30
Showing 15 changed files
1 1
deleted file mode 100644
... ...
@@ -1,464 +0,0 @@
1
-/*
2
- * $Id$
3
- *
4
- * JABBER module
5
- *
6
- *
7
- * Copyright (C) 2001-2003 Fhg Fokus
8
- *
9
- * This file is part of ser, a free SIP server.
10
- *
11
- * ser is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version
15
- *
16
- * For a license to use the ser software under conditions
17
- * other than those described here, or to purchase support for this
18
- * software, please contact iptel.org by e-mail at the following addresses:
19
- *    info@iptel.org
20
- *
21
- * ser is distributed in the hope that it will be useful,
22
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
- * GNU General Public License for more details.
25
- *
26
- * You should have received a copy of the GNU General Public License 
27
- * along with this program; if not, write to the Free Software 
28
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
- */
30
-
31
-
32
-#include <stdio.h>
33
-#include <string.h>
34
-#include <stdlib.h>
35
-#include <sys/types.h>
36
-#include <sys/ipc.h>
37
-#include <unistd.h>
38
-#include <fcntl.h>
39
-
40
-#include "../../sr_module.h"
41
-#include "../../error.h"
42
-#include "../../ut.h"
43
-#include "../im/im_funcs.h"
44
-#include "../../mem/shm_mem.h"
45
-#include "../../mem/mem.h"
46
-#include "../../globals.h"
47
-#include "../../parser/parse_uri.h"
48
-
49
-#include "sip2jabber.h"
50
-#include "jc_pool.h"
51
-#include "../../db/db.h"
52
-
53
-/** TM bind */
54
-struct tm_binds tmb;
55
-
56
-/** workers list */
57
-jab_wlist jwl = NULL;
58
-
59
-/** Structure that represents database connection */
60
-db_con_t** db_con;
61
-
62
-/** parameters */
63
-
64
-char *db_url   = "sql://root@127.0.0.1/sip_jab";
65
-char *db_table = "jusers";
66
-
67
-int nrw = 2;
68
-int max_jobs = 10;
69
-
70
-char *contact = "-";
71
-char *jaddress = "127.0.0.1";
72
-int jport = 5222;
73
-
74
-int delay_time = 10;
75
-int sleep_time = 20;
76
-int cache_time = 600;
77
-
78
-int **pipes = NULL;
79
-
80
-static int mod_init(void);
81
-static int child_init(int rank);
82
-static int jab_send_message(struct sip_msg*, char*, char* );
83
-
84
-void destroy(void);
85
-
86
-struct module_exports exports= {
87
-	"jabber",
88
-	(char*[]){
89
-		"jab_send_message"
90
-	},
91
-	(cmd_function[]){
92
-		jab_send_message
93
-	},
94
-	(int[]){
95
-		0
96
-	},
97
-	(fixup_function[]){
98
-		0
99
-	},
100
-	1,
101
-
102
-	(char*[]) {   /* Module parameter names */
103
-		"contact",
104
-		"db_url",
105
-		"jaddress",
106
-		"jport",
107
-		"workers",
108
-		"max_jobs",
109
-		"cache_time",
110
-		"delay_time",
111
-		"sleep_time"
112
-	},
113
-	(modparam_t[]) {   /* Module parameter types */
114
-		STR_PARAM,
115
-		STR_PARAM,
116
-		STR_PARAM,
117
-		INT_PARAM,
118
-		INT_PARAM,
119
-		INT_PARAM,
120
-		INT_PARAM,
121
-		INT_PARAM,
122
-		INT_PARAM
123
-	},
124
-	(void*[]) {   /* Module parameter variable pointers */
125
-		&contact,
126
-		&db_url,
127
-		&jaddress,
128
-		&jport,
129
-		&nrw,
130
-		&max_jobs,
131
-		&cache_time,
132
-		&delay_time,
133
-		&sleep_time
134
-	},
135
-	9,      /* Number of module paramers */
136
-	
137
-	mod_init,   /* module initialization function */
138
-	(response_function) 0,
139
-	(destroy_function) destroy,
140
-	0,
141
-	child_init  /* per-child init function */
142
-};
143
-
144
-/**
145
- * init module function
146
- */
147
-static int mod_init(void)
148
-{
149
-	load_tm_f load_tm;
150
-	int  i;
151
-
152
-	DBG("JABBER: initializing ...\n");
153
-
154
-	/* import mysql functions */
155
-	if (bind_dbmod())
156
-	{
157
-		DBG("JABBER: ERROR: Database module not found\n");
158
-		return -1;
159
-	}
160
-	db_con = (db_con_t**)shm_malloc(nrw*sizeof(db_con_t*));
161
-	if (db_con == NULL)
162
-	{
163
-		DBG("JABBER: Error while allocating db_con's\n");
164
-		return -3;
165
-	}
166
-
167
-	/* import the TM auto-loading function */
168
-	if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT))) {
169
-		LOG(L_ERR, "ERROR: jabber: mod_init: can't import load_tm\n");
170
-		return -1;
171
-	}
172
-	/* let the auto-loading function load all TM stuff */
173
-	if (load_tm( &tmb )==-1)
174
-		return -1;
175
-
176
-	pipes = (int**)pkg_malloc(nrw*sizeof(int*));
177
-	if (pipes == NULL)
178
-	{
179
-		DBG("JABBER: Error while allocating pipes\n");
180
-		return -3;
181
-	}
182
-	
183
-	for(i=0; i<nrw; i++)
184
-	{
185
-		pipes[i] = (int*)pkg_malloc(2*sizeof(int));
186
-		if (!pipes[i])
187
-		{
188
-			DBG("JABBER: Error while allocating pipes\n");
189
-			return -3;
190
-		}
191
-	}
192
-	
193
-	for(i=0; i<nrw; i++)
194
-	{
195
-		db_con[i] = db_init(db_url);
196
-		if (!db_con[i])
197
-		{
198
-			DBG("JABBER: Error while connecting database\n");
199
-			return -3;
200
-		}
201
-		else
202
-		{
203
-			db_use_table(db_con[i], db_table);
204
-			DBG("JABBER: Database connection opened successfuly\n");
205
-		}
206
-	}
207
-
208
-	
209
-	/** creating the pipees */
210
-	
211
-	for(i=0;i<nrw;i++)
212
-	{
213
-		/* create the pipe*/
214
-		if (pipe(pipes[i])==-1) {
215
-			DBG("JABBER: ERROR: mod_init: cannot create pipe!\n");
216
-			return -4;
217
-		}
218
-		DBG("JABBER: INIT: pipe[%d] = <%d>-<%d>\n", i, pipes[i][0],
219
-			pipes[i][1]);
220
-	}
221
-	
222
-	if((jwl = jab_wlist_init(pipes, nrw, max_jobs)) == NULL)
223
-	{
224
-		DBG("JABBER: mod_init: error initializing workers list\n");
225
-		return -1;
226
-	}
227
-	
228
-	if(jab_wlist_init_contact(jwl, contact) < 0)
229
-	{
230
-		DBG("JABBER: mod_init: error workers list properties\n");
231
-		return -1;
232
-	}
233
-	
234
-	DBG("JABBER: mod_init: initialized ...\n");	
235
-	return 0;
236
-}
237
-
238
-/*
239
- * Initialize childs
240
- */
241
-static int child_init(int rank)
242
-{
243
-	int i;
244
-	int *pids = NULL;
245
-	
246
-	DBG("JABBER: Initializing child %d\n", rank);
247
-	if(rank == 0)
248
-	{
249
-		pids = (int*)pkg_malloc(nrw*sizeof(int));
250
-		if (pids == NULL)
251
-		{
252
-			DBG("JABBER: Error while allocating pid's\n");
253
-			return -1;
254
-		}
255
-		/** launching the workers */
256
-		for(i=0;i<nrw;i++)
257
-		{
258
-			if ( (pids[i]=fork())<0 )
259
-			{
260
-				DBG("JABBER: ERROR: mod_init cannot launch worker\n");
261
-				return -1;
262
-			}
263
-			if (pids[i] == 0)
264
-			{
265
-				close(pipes[i][1]);
266
-				worker_process(jwl, jaddress, jport, pipes[i][0], max_jobs,
267
-					cache_time,	sleep_time, delay_time, db_con[i]);
268
-				exit(0);
269
-			}
270
-		}
271
-	
272
-		if(jab_wlist_set_pids(jwl, pids, nrw) < 0)
273
-		{
274
-			DBG("JABBER: mod_init: error setting pid's\n");
275
-			return -1;
276
-		}
277
-		if(pids)
278
-			pkg_free(pids);
279
-	}
280
-	
281
-	if(pipes)
282
-	{
283
-		for(i=0;i<nrw;i++)
284
-			close(pipes[i][0]);
285
-	}
286
-	return 0;
287
-}
288
-
289
-/**
290
- * send the SIP message through Jabber
291
- */
292
-static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
293
-{
294
-	str body, dst, /*host, user,*/ *p;
295
-	jab_sipmsg jsmsg;
296
-	struct to_body to, from;
297
-	struct sip_uri _uri;
298
-	int pipe, fl;
299
-
300
-	// extract message body - after that whole SIP MESSAGE is parsed
301
-	if ( !im_extract_body(msg,&body) )
302
-	{
303
-		DBG("JABBER: jab_send_message: cannot extract body from sip msg!\n");
304
-		goto error;
305
-	}
306
-	// check for FROM header
307
-	if(msg->from != NULL)
308
-	{
309
-		memset( &from , 0, sizeof(from) );
310
-		parse_to(msg->from->body.s, msg->from->body.s + msg->from->body.len+1,
311
-						&from);
312
-		if(from.error == PARSE_OK)
313
-			DBG("JABBER: jab_send_message: From parsed OK.\n");
314
-		else
315
-		{
316
-			DBG("JABER: jab_send_message: From NOT parsed\n");
317
-			goto error;
318
-		}
319
-	}
320
-	else
321
-	{
322
-		DBG("JABBER: jab_send_message: cannot find FROM HEADER!\n");
323
-		goto error;
324
-	}
325
-	// get the communication pipe with the worker
326
-	if((pipe = jab_wlist_get(jwl, &from.uri, &p)) < 0)
327
-	{
328
-		DBG("JABBER: jab_send_message: cannot find pipe of the worker!\n");
329
-		goto error;
330
-	}
331
-	
332
-	// determination of destination
333
-	dst.len = 0;
334
-	if( msg->new_uri.len > 0 )
335
-	{
336
-		DBG("JABBER: jab_send_message: using NEW URI for destination\n");
337
-		dst.s = msg->new_uri.s;
338
-		dst.len = msg->new_uri.len;
339
-	} else if ( msg->first_line.u.request.uri.len > 0 )
340
-	{
341
-		DBG("JABBER: jab_send_message: parsing URI from first line\n");
342
-		if(parse_uri(msg->first_line.u.request.uri.s,
343
-					msg->first_line.u.request.uri.len, &_uri) < 0)
344
-		{
345
-			DBG("JABBER:jab_send_message:ERROR parsing URI from first line\n");
346
-			goto error;
347
-		}
348
-		if(_uri.user.len > 0)
349
-		{
350
-			DBG("JABBER: jab_send_message: using URI for destination\n");
351
-			dst.s = msg->first_line.u.request.uri.s;
352
-			dst.len = msg->first_line.u.request.uri.len;
353
-		}
354
-		free_uri(&_uri);
355
-	}
356
-	if(dst.len == 0 && msg->to != NULL)
357
-	{
358
-		memset( &to , 0, sizeof(to) );
359
-		parse_to(msg->to->body.s, msg->to->body.s + msg->to->body.len + 1,
360
-				&to);
361
-		if(to.uri.len > 0) // to.error == PARSE_OK)
362
-		{
363
-			DBG("JABBER: jab_send_message: TO parsed OK <%.*s>.\n",
364
-				to.uri.len, to.uri.s);
365
-			dst.s = to.uri.s;
366
-			dst.len = to.uri.len;
367
-		}
368
-		else
369
-		{
370
-			DBG("JABER: jab_send_message: TO NOT parsed\n");
371
-			goto error;
372
-		}
373
-	}
374
-	if(dst.len == 0)
375
-	{
376
-		DBG("JABBER:jab_send_message: destination not found in SIP message\n");
377
-		goto error;
378
-	}
379
-	
380
-	/** skip 'sip:' in destination address */
381
-	if(dst.s[0]=='s' && dst.s[1]=='i' && dst.s[2]=='p')
382
-	{
383
-		dst.s += 3;
384
-		dst.len -= 3;
385
-		fl = 1;
386
-		while(*dst.s == ' ' || *dst.s == '\t' || *dst.s == ':')
387
-		{
388
-			dst.s++;
389
-			dst.len--;
390
-			fl = 0;
391
-		}
392
-		if(fl)
393
-		{
394
-			dst.s -= 3;
395
-			dst.len += 3;
396
-		}
397
-		
398
-		DBG("JABBER: DESTINATION corrected <%.*s>.\n", dst.len, dst.s);
399
-	}
400
-	
401
-	//putting the SIP message parts in share memory to be accessible by workers
402
-    jsmsg = (jab_sipmsg)shm_malloc(sizeof(t_jab_sipmsg));
403
-    if(jsmsg == NULL)
404
-    	return -1;
405
-	jsmsg->to.len = dst.len;
406
-	jsmsg->to.s = (char*)shm_malloc(jsmsg->to.len+1);
407
-	if(jsmsg->to.s == NULL)
408
-	{
409
-		shm_free(jsmsg);
410
-		goto error;
411
-	}
412
-	strncpy(jsmsg->to.s, dst.s, jsmsg->to.len);
413
-	
414
-	jsmsg->msg.len = body.len;
415
-	jsmsg->msg.s = (char*)shm_malloc(jsmsg->msg.len+1);
416
-	if(jsmsg->msg.s == NULL)
417
-	{
418
-		shm_free(jsmsg->to.s);
419
-		shm_free(jsmsg);
420
-		goto error;
421
-	}
422
-	strncpy(jsmsg->msg.s, body.s, jsmsg->msg.len);
423
-	
424
-	jsmsg->from = p;
425
-	
426
-	DBG("JABBER: jab_send_message[%d]: sending <%p> to worker through <%d>\n",
427
-			getpid(), jsmsg, pipe);
428
-	// sending the SHM pointer of SIP message to the worker
429
-	if(write(pipe, &jsmsg, sizeof(jsmsg)) != sizeof(jsmsg))
430
-	{
431
-		DBG("JABBER: jab_send_message: error when writting to worker pipe!\n");
432
-		goto error;
433
-	}
434
-	
435
-	return 1;
436
-error:
437
-	return -1;
438
-}
439
-
440
-/**
441
- * destroy function of module
442
- */
443
-void destroy(void)
444
-{
445
-	int i;
446
-	DBG("JABBER: Unloading module ...\n");
447
-	if(pipes)
448
-	{
449
-		for(i = 0; i < nrw; i++)
450
-			pkg_free(pipes[i]);
451
-		pkg_free(pipes);
452
-	}
453
-	// cleaning MySQL connections
454
-	if(db_con != NULL)
455
-	{
456
-		for(i = 0; i<nrw; i++)
457
-			db_close(db_con[i]);
458
-		shm_free(db_con);
459
-	}
460
-			
461
-	jab_wlist_free(jwl);
462
-	DBG("JABBER: Unloaded\n");
463
-}
464
-
465 0
deleted file mode 100644
... ...
@@ -1,1052 +0,0 @@
1
-/*
2
- * $Id$
3
- *
4
- * JABBER module - Jabber connections pool
5
- *
6
- *
7
- * Copyright (C) 2001-2003 Fhg Fokus
8
- *
9
- * This file is part of ser, a free SIP server.
10
- *
11
- * ser is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version
15
- *
16
- * For a license to use the ser software under conditions
17
- * other than those described here, or to purchase support for this
18
- * software, please contact iptel.org by e-mail at the following addresses:
19
- *    info@iptel.org
20
- *
21
- * ser is distributed in the hope that it will be useful,
22
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
- * GNU General Public License for more details.
25
- *
26
- * You should have received a copy of the GNU General Public License 
27
- * along with this program; if not, write to the Free Software 
28
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
- */
30
-
31
-
32
-#include <string.h>
33
-#include <unistd.h>
34
-#include <stdio.h>
35
-#include <sys/time.h>
36
-#include <sys/types.h>
37
-
38
-#include "../../dprint.h"
39
-#include "../../timer.h"
40
-#include "../../mem/mem.h"
41
-#include "../../mem/shm_mem.h"
42
-#include "../tm/t_funcs.h"
43
-#include "../tm/uac.h"
44
-
45
-#include "jc_pool.h"
46
-#include "sip2jabber.h"
47
-#include "../im/im_funcs.h"
48
-#include "xml_jab.h"
49
-#include "mdefines.h"
50
-
51
-/** TM bind */
52
-struct tm_binds tmb;
53
-
54
-/**
55
- * function used to compare two elements in B-Tree
56
- */
57
-int k_cmp(void *a, void *b)
58
-{
59
-	int n;
60
-	if(a == NULL)
61
-	    return -1;
62
-	if(a == NULL)
63
-	    return 1;
64
-	// DBG("JABBER: k_kmp: comparing <%.*s> / <%.*s>\n", ((str *)a)->len,
65
-	// 		((str *)a)->s, ((str *)b)->len, ((str *)b)->s);
66
-	if(((str *)a)->len != ((str *)b)->len)
67
-		return -1;
68
-	n = strncmp(((str *)a)->s, ((str *)b)->s, ((str *)a)->len);
69
-	if(n<0)
70
-		return -1;
71
-	if(n>0)
72
-		return 1;
73
-	return 0;
74
-}
75
-
76
-/**
77
- * free the information from a B-Tree node
78
- */
79
-void free_str_p(void *p)
80
-{
81
-	if(p == NULL)
82
-		return;
83
-	if(((str*)p)->s != NULL)
84
-		_M_SHM_FREE(((str*)p)->s);
85
-	_M_SHM_FREE(p);
86
-}
87
-
88
-/**
89
- * free a pointer to a t_jab_sipmsg structure
90
- * > element where points 'from' MUST be eliberated separated
91
- */
92
-void jab_sipmsg_free(jab_sipmsg jsmsg)
93
-{
94
-	if(jsmsg == NULL)
95
-		return;
96
-	if(jsmsg->to.s != NULL)
97
-		_M_SHM_FREE(jsmsg->to.s);
98
-//	if(jsmsg->from.s != NULL)
99
-//		_M_SHM_FREE(jsmsg->from.s);
100
-	if(jsmsg->msg.s != NULL)
101
-		_M_SHM_FREE(jsmsg->msg.s);
102
-	_M_SHM_FREE(jsmsg);
103
-}
104
-
105
-/**
106
- * init a workers list
107
- * - pipes : communication pipes
108
- * - size : size of list - number of workers
109
- * - max : maximum number of jobs per worker
110
- * #return : pointer to workers list or NULL on error
111
- */
112
-jab_wlist jab_wlist_init(int **pipes, int size, int max)
113
-{
114
-	int i;
115
-	jab_wlist jwl = NULL;
116
-	
117
-	if(pipes == NULL || size <= 0 || max <= 0)
118
-		return NULL;
119
-	
120
-	DBG("JABBER: JC_WLIST_INIT: --- === ---\n");
121
-	
122
-	jwl = (jab_wlist)_M_SHM_MALLOC(sizeof(t_jab_wlist));
123
-	if(jwl == NULL)
124
-		return NULL;
125
-	jwl->len = size;
126
-	jwl->maxj = max;
127
-	jwl->contact_h = NULL;
128
-	if((jwl->sems = create_semaphores(size)) == NULL)
129
-	{
130
-		_M_SHM_FREE(jwl);
131
-		return NULL;
132
-	}
133
-	jwl->workers = (jab_worker)_M_SHM_MALLOC(size*sizeof(t_jab_worker));
134
-	if(jwl->workers == NULL)
135
-	{
136
-		_M_SHM_FREE(jwl);
137
-		return NULL;
138
-	}
139
-	
140
-	for(i = 0; i < size; i++)
141
-	{
142
-		jwl->workers[i].nr = 0;
143
-		jwl->workers[i].pipe = pipes[i][1];
144
-		if((jwl->workers[i].sip_ids = newtree234(k_cmp)) == NULL)
145
-		{
146
-			_M_SHM_FREE(jwl);
147
-			return NULL;
148
-		}
149
-	}	
150
-	
151
-	return jwl;
152
-}
153
-
154
-/**
155
- * init contact address for SIP messages that will be sent by workers
156
- * - jwl - pointer to workers list
157
- * - ch - string representation of the contact, e.g. 'sip:100.100.100.100:5060'
158
- * #return : 0 on success or <0 on error
159
- * info: still has 0 at the end of string
160
- */
161
-int jab_wlist_init_contact(jab_wlist jwl, char *ch)
162
-{
163
-	int f = 0; // correction flag: 1 -> must be converted to <sip: ... > 
164
-	if(ch == NULL)
165
-		return -1;
166
-	if((jwl->contact_h = (str*)_M_SHM_MALLOC(sizeof(str))) == NULL)
167
-		return -1;
168
-	jwl->contact_h->len = strlen(ch);
169
-
170
-	if(jwl->contact_h->len > 2 && strstr(ch, "sip:") == NULL)
171
-	{
172
-		// contact correction
173
-		jwl->contact_h->len += 6;
174
-		f = 1;
175
-	}
176
-
177
-	if((jwl->contact_h->s=(char*)_M_SHM_MALLOC(jwl->contact_h->len+1))==NULL)
178
-	{
179
-		_M_SHM_FREE(jwl->contact_h);
180
-		return -2;
181
-	}
182
-	
183
-	if(f)
184
-	{
185
-		strncpy(jwl->contact_h->s, "<sip:", 5);
186
-		strcpy(jwl->contact_h->s+5, ch);
187
-		jwl->contact_h->s[jwl->contact_h->len-1] = '>';
188
-		jwl->contact_h->s[jwl->contact_h->len] = 0;
189
-	}
190
-	else
191
-		strcpy(jwl->contact_h->s, ch);
192
-
193
-	return 0;
194
-}
195
-
196
-
197
-/**
198
- * set the p.id's of the workers
199
- * - jwl : pointer to the workers list
200
- * - pids : p.id's array
201
- * - size : number of pids
202
- * #return : 0 on success or <0 on error
203
- */
204
-int jab_wlist_set_pids(jab_wlist jwl, int *pids, int size)
205
-{
206
-	int i;
207
-	
208
-	if(jwl == NULL || pids == NULL || size <= 0)
209
-		return -1;
210
-	for(i = 0; i < size; i++)
211
-		jwl->workers[i].pid = pids[i];
212
-	return 0;
213
-}
214
-
215
-/**
216
- * free jab_wlist
217
- * - jwl : pointer to the workers list
218
- */
219
-void jab_wlist_free(jab_wlist jwl)
220
-{
221
-	int i;
222
-	DBG("JABBER: jab_wlist_free : freeing 'jab_wlist' memory ...\n");
223
-	if(jwl == NULL)
224
-		return;
225
-	
226
-	if(jwl->contact_h != NULL && jwl->contact_h->s != NULL)
227
-		_M_SHM_FREE(jwl->contact_h->s);
228
-	if(jwl->contact_h != NULL)
229
-		_M_SHM_FREE(jwl->contact_h);
230
-		
231
-	if(jwl->workers != NULL)
232
-	{
233
-		for(i=0; i<jwl->len; i++)
234
-			free2tree234(jwl->workers[i].sip_ids, free_str_p);
235
-		_M_SHM_FREE(jwl->workers);
236
-	}
237
-	
238
-	//rm_sem(jwl->semid);
239
-	if(jwl->sems != NULL)
240
-		destroy_semaphores(jwl->sems);
241
-	
242
-	_M_SHM_FREE(jwl);
243
-}
244
-
245
-/**
246
- * return communication pipe with the worker that will process the message for
247
- * 		the id 'sid', or -1 if error
248
- * - jwl : pointer to the workers list
249
- * - sid : id of the entity (connection to Jabber - usually SHOULD be FROM 
250
- *   header of the incomming SIP message)
251
- * - p : will point to the SHM location of the 'sid' in jwl
252
- */
253
-int jab_wlist_get(jab_wlist jwl, str *sid, str **p)
254
-{
255
-	int i = 0, pos = -1, min = 100000;
256
-	str *msid = NULL;
257
-	
258
-	if(jwl == NULL)
259
-		return -1;
260
-	DBG("JABBER: JC_WLIST_GET: --- === ---\n");
261
-	//mutex_lock(jwl->semid);
262
-	
263
-	*p = NULL;
264
-	while(i < jwl->len)
265
-	{
266
-		s_lock_at(jwl->sems, i);
267
-		if((*p = find234(jwl->workers[i].sip_ids, (void*)sid, NULL)) != NULL)
268
-		{
269
-			//mutex_unlock(jwl->semid);
270
-			if(pos >= 0)
271
-				s_unlock_at(jwl->sems, pos);
272
-			s_unlock_at(jwl->sems, i);
273
-			DBG("JABBER: JC_WLIST_GET: entry already exists for <%.*s> in the" 
274
-				" pool of <%d> [%d]\n",sid->len, sid->s,jwl->workers[i].pid,i);
275
-			return jwl->workers[i].pipe;
276
-		}
277
-		if(min > jwl->workers[i].nr)
278
-		{
279
-			if(pos >= 0)
280
-				s_unlock_at(jwl->sems, pos);
281
-			pos = i;
282
-			min = jwl->workers[i].nr;
283
-		}
284
-		else
285
-			s_unlock_at(jwl->sems, i);
286
-		i++;
287
-	}
288
-	if(pos >= 0 && jwl->workers[pos].nr < jwl->maxj)
289
-	{
290
-		jwl->workers[pos].nr++;
291
-		
292
-		msid = (str*)_M_SHM_MALLOC(sizeof(str));
293
-		if((msid != NULL) && 
294
-			(*p = add234(jwl->workers[pos].sip_ids, msid)) != NULL)
295
-		{
296
-			msid->s = (char*)_M_SHM_MALLOC(sid->len);
297
-			msid->len = sid->len;
298
-			memcpy(msid->s, sid->s, sid->len);
299
-			s_unlock_at(jwl->sems, pos);
300
-			//mutex_unlock(jwl->semid);
301
-			DBG("JABBER: JC_WLIST_GET: new entry for <%.*s> in the pool of"
302
-				" <%d> - [%d]\n", sid->len, sid->s,
303
-				jwl->workers[pos].pid, pos);
304
-			return jwl->workers[pos].pipe;
305
-		}
306
-	}
307
-	
308
-	if(pos >= 0)
309
-		s_unlock_at(jwl->sems, pos);
310
-	//mutex_unlock(jwl->semid);
311
-	
312
-	return -1;
313
-}
314
-
315
-/**
316
- * delete an entity from working list of a worker
317
- * - jwl : pointer to the workers list
318
- * - sid : id of the entity (connection to Jabber - usually SHOULD be FROM
319
- *   header of the incomming SIP message
320
- * - _pid : process id of the worker
321
- */
322
-void jab_wlist_del(jab_wlist jwl, str *sid, int _pid)
323
-{
324
-	int i;
325
-	void *p;
326
-	if(jwl == NULL || sid == NULL)
327
-		return;
328
-	for(i=0; i < jwl->len; i++)
329
-		if(jwl->workers[i].pid == _pid)
330
-			break;
331
-	if(i >= jwl->len)
332
-	{
333
-		DBG("JABBER: jab_wlist_del:%d: key <%.*s> not found in [%d]...\n", 
334
-			_pid, sid->len, sid->s, i);		
335
-		return;
336
-	}
337
-	DBG("JABBER: jab_wlist_del:%d: trying to delete entry for <%.*s>...\n", 
338
-		_pid, sid->len, sid->s);
339
-	
340
-	s_lock_at(jwl->sems, i);
341
-	p = del234(jwl->workers[i].sip_ids, (void*)sid);	
342
-	
343
-	if(p != NULL)
344
-	{
345
-		jwl->workers[i].nr--;
346
-		
347
-		DBG("JABBER: jab_wlist_del:%d: sip id <%.*s> deleted\n", _pid, 
348
-			sid->len, sid->s);
349
-		free_str_p(p);
350
-	}
351
-		
352
-	s_unlock_at(jwl->sems, i);
353
-}
354
-
355
-/**
356
- * send a SIP MESSAGE message
357
- * - to : destination
358
- * - from : origin
359
- * - contact : contact header
360
- * - msg : body of the message
361
- * #return : 0 on success or <0 on error
362
- */
363
-int jab_send_sip_msg(str *to, str *from, str *contact, str *msg)
364
-{
365
-	str  msg_type = { "MESSAGE", 7};
366
-	char buf[512];
367
-	str  tfrom;
368
-	str  str_hdr;
369
-	char buf1[1024];
370
-
371
-	// from correction
372
-	strcpy(buf, "<sip:");
373
-	strncat(buf, from->s, from->len);
374
-	tfrom.len = from->len;
375
-	if(strstr(buf+5, "sip:") == NULL)
376
-	{
377
-		tfrom.len += 5;
378
-		buf[tfrom.len++] = '>';
379
-		tfrom.s = buf;
380
-	} else
381
-		tfrom.s = buf+5;
382
-	// building Contact and Content-Type
383
-	strcpy(buf1,"Content-Type: text/plain"CRLF"Contact: ");
384
-	str_hdr.len = 24 + CRLF_LEN + 9;
385
-	if(contact != NULL && contact->len > 2) {
386
-		strncat(buf1,contact->s,contact->len);
387
-		str_hdr.len += contact->len;
388
-	} else {
389
-		strncat(buf1,tfrom.s,tfrom.len);
390
-		str_hdr.len += tfrom.len;
391
-	}
392
-	str_hdr.s = buf1;
393
-
394
-	return tmb.t_uac( &msg_type, to, &str_hdr , msg, &tfrom, 0 , 0, 0);
395
-}
396
-
397
-/**
398
- * send a SIP MESSAGE message
399
- * - to : destination
400
- * - from : origin
401
- * - contact : contact header
402
- * - msg : body of the message, string terminated by zero
403
- * #return : 0 on success or <0 on error
404
- */
405
-int jab_send_sip_msgz(str *to, str *from, str *contact, char *msg)
406
-{
407
-	str tstr;
408
-	int n;
409
-	tstr.s = msg;
410
-	tstr.len = strlen(msg);
411
-	if((n = jab_send_sip_msg(to, from, contact, &tstr)) < 0)
412
-		DBG("JABBER: jab_send_sip_msgz: ERROR SIP MESSAGE wasn't sent to"
413
-			" [%.*s]...\n", tstr.len, tstr.s);
414
-	else
415
-		DBG("JABBER: jab_send_sip_msgz: SIP MESSAGE was sent to [%.*s]...\n", 
416
-			tstr.len, tstr.s);
417
-	return n;
418
-}
419
-
420
-/**
421
- * worker implementation
422
- * - jwl : pointer to the workers list
423
- * - jaddress : address of the jabber server
424
- * - jport : port of the jabber server
425
- * - pipe : communication pipe with SER
426
- * - size : maximun number of jobs - open connections to Jabber server
427
- * - ctime : cache time for a connection to Jabber
428
- * - wtime : wait time between cache checking
429
- * - dtime : delay time for first message
430
- * - db_con : connection to database
431
- * #return : 0 on success or <0 on error
432
- */
433
-int worker_process(jab_wlist jwl, char* jaddress, int jport, int pipe,
434
-		int size, int ctime, int wtime, int dtime, db_con_t* db_con)
435
-{
436
-	int ret, i, n, maxfd, error, cseq;
437
-	jc_pool jcp;
438
-	struct timeval tmv;
439
-	fd_set set, mset;
440
-	jab_sipmsg jsmsg;
441
-	t_jab_jmsg tjmsg;
442
-	jbconnection jbc;
443
-	open_jc ojc;
444
-	char buff[1024], tbuff[1024], recv_buff[4096];
445
-	int ltime = 0;
446
-	int _pid = getpid();
447
-	str tstr;
448
-	
449
-	db_key_t keys[] = {"sip_id"};
450
-	db_val_t vals[] = {{DB_STRING, 0, {.string_val = buff}}};
451
-	db_key_t col[] = {"jab_id", "jab_passwd"};
452
-	db_res_t* res = NULL;
453
-	
454
-	DBG("JABBER: WORKER_PROCESS:%d: started - pipe=<%d> : 1st message delay"
455
-		" <%d>\n", _pid, pipe, dtime);
456
-	
457
-	if((jcp = jc_pool_init(size, 10)) == NULL)
458
-	{
459
-		DBG("JABBER: WORKER_PROCESS: cannot allocate the pool\n");
460
-		return -1;		
461
-	}
462
-	
463
-	maxfd = pipe;
464
-	tmv.tv_sec = wtime;
465
-	tmv.tv_usec = 0;
466
-	cseq = 1;
467
-	
468
-	FD_ZERO(&set);
469
-	FD_SET(pipe, &set);
470
-	while(1)
471
-	{
472
-		mset = set;
473
-			
474
-		tmv.tv_sec = (jcp->jmqueue.size == 0)?wtime:1;
475
-		DBG("JABBER: worker_process:%d: select waiting %dsec\n", _pid, 
476
-						(int)tmv.tv_sec);
477
-		tmv.tv_usec = 0;
478
-		
479
-		ret = select(maxfd+1, &mset, NULL, NULL, &tmv);
480
-		/** check the queue AND conecction of head element is ready */
481
-		while(jcp->jmqueue.size != 0 
482
-			&& jcp->jmqueue.ojc[jcp->jmqueue.head]->ready < get_ticks())
483
-		{
484
-			/** send message from queue */
485
-			DBG("JABBER: worker_process:%d: SENDING AS JABBER MESSAGE FROM "
486
-				" LOCAL QUEUE ...\n", _pid);
487
-			jb_send_msg(jcp->jmqueue.ojc[jcp->jmqueue.head]->jbc, 
488
-						jcp->jmqueue.jsm[jcp->jmqueue.head]->to.s, 
489
-						jcp->jmqueue.jsm[jcp->jmqueue.head]->to.len,
490
-						jcp->jmqueue.jsm[jcp->jmqueue.head]->msg.s, 
491
-						jcp->jmqueue.jsm[jcp->jmqueue.head]->msg.len);
492
-			
493
-			jab_sipmsg_free(jcp->jmqueue.jsm[jcp->jmqueue.head]);
494
-			
495
-			/** delete message from queue */
496
-			jc_pool_del_jmsg(jcp);
497
-		}
498
-		
499
-		error = 0;
500
-		if(ret > 0)
501
-		{
502
-			DBG("JABBER: worker_process:%d: something is coming\n", _pid);
503
-			if(FD_ISSET(pipe, &mset))
504
-			{ // new job from ser
505
-				read(pipe, &jsmsg, sizeof(jsmsg));
506
-
507
-				DBG("JABBER: worker_process <%d>: job <%p> from SER\n", 
508
-								_pid, jsmsg);
509
-				if( jsmsg == NULL || jsmsg->from == NULL)
510
-					continue;
511
-				
512
-				strncpy(buff, jsmsg->from->s, jsmsg->from->len);
513
-				buff[jsmsg->from->len] = 0;
514
-				
515
-				ojc = jc_pool_get(jcp, jsmsg->from);
516
-				if(ojc == NULL)
517
-				{ // NO OPEN CONNECTION FOR THIS SIP ID
518
-					DBG("JABBER:worker_process:%d: new connection for <%s>.\n",
519
-									_pid, buff);
520
-					if(db_query(db_con, keys, vals, col, 1, 2, NULL, &res)==0)
521
-					{
522
-
523
-						if (RES_ROW_N(res) != 0)
524
-						{
525
-						
526
-							jbc = jb_init_jbconnection(jaddress, jport);
527
-    	        		
528
-							if(!jb_connect_to_server(jbc))
529
-                            {
530
-								DBG("JABBER: auth to jabber as: [%s] / [%s]\n",
531
-									(char*)(ROW_VALUES(RES_ROWS(res))[0].val.string_val), 
532
-									(char*)(ROW_VALUES(RES_ROWS(res))[1].val.string_val));
533
-								if(jb_user_auth_to_server(jbc,
534
-									(char*)(ROW_VALUES(RES_ROWS(res))[0].val.string_val),
535
-									(char*)(ROW_VALUES(RES_ROWS(res))[1].val.string_val), "jbcl") == 0)
536
-	        		            {
537
-									ojc = open_jc_create(jsmsg->from, jbc, 
538
-													ctime, dtime);
539
-									if((ojc != NULL) 
540
-										&& (jc_pool_add( jcp, ojc) == 0))
541
-		                            {
542
-										/** add socket descriptor to select */
543
-										DBG("JABBER: worker_process <%d>: add"
544
-											" connection on <%d> \n", _pid, 
545
-											jbc->sock);
546
-										if(jbc->sock > maxfd)
547
-											maxfd = jbc->sock;
548
-										FD_SET(jbc->sock, &set);
549
-										
550
-										jb_get_roster(jbc);
551
-										jb_send_presence(jbc, NULL,
552
-											"Online", "9");
553
-										/** wait for a while - SER is tired */
554
-										//sleep(3);
555
-									}
556
-									else
557
-									{
558
-										DBG("JABBER:worker_process:%d: Keeping"
559
-											" connection to Jabber server"
560
-											" failed! Not enough memory ...\n", 
561
-											_pid);
562
-										jb_disconnect(jbc);
563
-										jb_free_jbconnection(jbc);
564
-										if(ojc != NULL)
565
-											open_jc_free(ojc);
566
-										jab_send_sip_msgz(jsmsg->from, 
567
-											&jsmsg->to,	jwl->contact_h,	
568
-											"ERROR:Your message was	not"
569
-											" sent. SIP-2-JABBER"
570
-											" gateway is full.");
571
-										error = 1;
572
-									}	
573
-								}
574
-								else
575
-								{
576
-									DBG("JABBER:worker_process:%d: "
577
-										" Authentication to the Jabber server"
578
-										" failed ...\n", _pid);
579
-									jb_disconnect(jbc);
580
-									jb_free_jbconnection(jbc);
581
-									jab_send_sip_msgz(jsmsg->from, &jsmsg->to,
582
-										jwl->contact_h, "ERROR: Your message"
583
-										" was not sent. Authentication to the"
584
-										" Jabber server failed.");
585
-									error = 1;
586
-								}
587
-
588
-							}
589
-							else
590
-							{
591
-								DBG("JABBER:worker_process:%d: Cannot connect"
592
-									" to the Jabber server ...\n", _pid);
593
-								jab_send_sip_msgz(jsmsg->from, &jsmsg->to, 
594
-									jwl->contact_h, "ERROR: Your message was"
595
-									" not sent. Cannot connect to the Jabber"
596
-									" server.");
597
-								error = 1;
598
-							}
599
-
600
-						}
601
-						else
602
-						{
603
-							DBG("JABBER:worker_process:%d: no database"
604
-								" result\n", _pid);
605
-							jab_send_sip_msgz(jsmsg->from, &jsmsg->to, 
606
-								jwl->contact_h, "ERROR: Your message was not"
607
-								" sent. You do not have permision to use the"
608
-								" gateway.");
609
-							error = 1;
610
-						}
611
-						if ((res != NULL) && (db_free_query(db_con,res) < 0))
612
-						{
613
-							DBG("JABBER:worker_process:%d:Error while freeing"
614
-								" SQL result\n", _pid);
615
-							return -1;
616
-						}
617
-						else
618
-							res = NULL;
619
-					}
620
-				}
621
-				else
622
-				{
623
-					DBG("JABBER:worker_process:%d: connection already exists"
624
-						" for <%s> ...\n", _pid, buff);
625
-					open_jc_update(ojc, ctime);
626
-				}
627
-				
628
-				if(!error)
629
-				{
630
-					if(ojc->ready < get_ticks())
631
-					{
632
-						DBG("JABBER: worker_process:%d: SENDING AS JABBER"
633
-							" MESSAGE ...\n", _pid);
634
-						if(jb_send_msg(ojc->jbc, jsmsg->to.s, jsmsg->to.len,
635
-								jsmsg->msg.s, jsmsg->msg.len)<0)
636
-							jab_send_sip_msgz(jsmsg->from, &jsmsg->to,
637
-								jwl->contact_h, "ERROR: Your message was not"
638
-								" sent. Something wrong during transmition to"
639
-								" Jabber.");
640
-						
641
-						jab_sipmsg_free(jsmsg);
642
-					}
643
-					else
644
-					{
645
-						DBG("JABBER:worker_process:%d:SCHEDULING THE MESSAGE."
646
-							"\n", _pid);
647
-						if(jc_pool_add_jmsg(jcp, jsmsg, ojc) < 0)
648
-						{
649
-							DBG("JABBER: worker_process:%d: SCHEDULING THE"
650
-								" MESSAGE FAILED. Message was droped.\n",_pid);
651
-							jab_sipmsg_free(jsmsg);
652
-						}
653
-					}
654
-				}
655
-				else
656
-					jab_sipmsg_free(jsmsg);
657
-			}
658
-			else
659
-			{ // new message from ... JABBER
660
-				for(i = 0; i < jcp->len; i++)
661
-				{
662
-					if(jcp->ojc[i] != NULL && jcp->ojc[i]->jbc != NULL)
663
-					{
664
-						DBG("JABBER: worker_process:%d: checking socket <%d>"
665
-							" ...\n", _pid, jcp->ojc[i]->jbc->sock);
666
-						if(FD_ISSET(jcp->ojc[i]->jbc->sock, &mset))
667
-						{
668
-							if((n = read(jcp->ojc[i]->jbc->sock, recv_buff,
669
-										sizeof(recv_buff))) > 0)
670
-							{
671
-								open_jc_update(jcp->ojc[i], ctime);
672
-								
673
-								DBG("JABBER: JMSG START ----------\n%.*s\n"
674
-									" JABBER: JMSG END ----------\n", 
675
-									n, recv_buff);
676
-								
677
-								recv_buff[n] = 0;
678
-								if(strstr(recv_buff, "<message ") != NULL)
679
-								{
680
-									if(j2s_parse_jmsgx(recv_buff,n,&tjmsg)>=0)
681
-									{
682
-										DBG("JABBER: worker_process:%d:"
683
-											" sending as SIP ...\n", _pid);
684
-										buff[0] = 0;
685
-										if(tjmsg.error.len > 0)
686
-										{
687
-											strcpy(buff, "{Error: ");
688
-											if(tjmsg.errcode.len > 0)
689
-											{
690
-												strncat(buff, tjmsg.errcode.s, 
691
-													tjmsg.errcode.len);
692
-												strncat(buff, " - ", 3);
693
-											}
694
-											strncat(buff, tjmsg.error.s, 
695
-												tjmsg.error.len);
696
-											strcat(buff, ". The following"
697
-												" message was NOT sent}: ");
698
-										}
699
-										strncat(buff, tjmsg.body.s, 
700
-											tjmsg.body.len);
701
-										if((n=xml_unescape(buff, strlen(buff),
702
-													tbuff, 1024)) > 0)
703
-										{
704
-											tstr.s = tbuff;
705
-											tstr.len = n;
706
-											if(jab_send_sip_msg(jcp->ojc[i]->id,
707
-													&tjmsg.from,jwl->contact_h,
708
-													&tstr) < 0)
709
-												DBG("JABBER:worker_process:%d:"
710
-													" ERROR SIP MESSAGE was not"
711
-													" sent ...\n", _pid);
712
-											else
713
-												DBG("JABBER:worker_process:%d:"
714
-													" SIP MESSAGE was sent.\n",
715
-													_pid);
716
-										}
717
-										else
718
-										{
719
-											DBG("JABBER: worker_process:%d:"
720
-												" ERROR sending as sip: output"
721
-												" buffer too small.\n", _pid);
722
-										}
723
-									}
724
-									else
725
-										DBG("JABBER: worker_process:%d: ERROR"
726
-											" parsing jabber message ...\n",
727
-											_pid);
728
-								}
729
-							}
730
-							else
731
-							{
732
-								DBG("JABBER: worker_process:%d: ERROR -"
733
-									" connection to jabber lost on socket <%d>"
734
-									" ...\n", _pid, jcp->ojc[i]->jbc->sock);
735
-								jcp->ojc[i]->expire = ltime = 0;
736
-							}
737
-						}
738
-					}
739
-				}
740
-			}			
741
-		} // END IF ret>0
742
-		
743
-		if(ret < 0)
744
-		{
745
-			DBG("JABBER: worker_process:%d: SIGNAL received!!!!!!!!\n", _pid);
746
-			maxfd = pipe;
747
-			FD_ZERO(&set);
748
-			FD_SET(pipe, &set);
749
-			for(i = 0; i < jcp->len; i++)
750
-			{
751
-				if(jcp->ojc[i] != NULL && jcp->ojc[i]->jbc != NULL)
752
-				{
753
-					FD_SET(jcp->ojc[i]->jbc->sock, &set);
754
-					if( jcp->ojc[i]->jbc->sock > maxfd )
755
-						maxfd = jcp->ojc[i]->jbc->sock;
756
-				}
757
-			}
758
-		}
759
-		
760
-		if(ltime + wtime <= get_ticks())
761
-		{
762
-			ltime = get_ticks();
763
-			DBG("JABBER: worker_process:%d: scanning for expired connection\n",
764
-				_pid);
765
-			for(i = 0; i < jcp->len; i++)
766
-			{
767
-				if((jcp->ojc[i] != NULL) && (jcp->ojc[i]->expire <= ltime))
768
-				{
769
-					DBG("JABBER: worker_process:%d: connection expired for"
770
-						" <%.*s>\n", _pid, jcp->ojc[i]->id->len, 
771
-						jcp->ojc[i]->id->s);
772
-					
773
-					// CLEAN JAB_WLIST
774
-					jab_wlist_del(jwl, jcp->ojc[i]->id, _pid);
775
-					
776
-					FD_CLR(jcp->ojc[i]->jbc->sock, &set);
777
-					jb_disconnect(jcp->ojc[i]->jbc);
778
-					open_jc_free(jcp->ojc[i]);
779
-					jcp->ojc[i] = NULL;
780
-				}
781
-			}
782
-		}
783
-				
784
-	} // END while
785
-	
786
-	return 0;
787
-}
788
-
789
-/*****************************     ****************************************/
790
-
791
-/**
792
- * init a jc_pool structure
793
- * - size : maximum number of the open connection to Jabber
794
- * - jlen : maximun size of messages queue
795
- * #return : pointer to the structure or NULL on error
796
- */
797
-jc_pool jc_pool_init(int size, int jlen)
798
-{
799
-	jc_pool jcp = (jc_pool)_M_MALLOC(sizeof(t_jc_pool));
800
-	if(jcp == NULL)
801
-		return NULL;
802
-	jcp->len = size;
803
-	jcp->ojc = (open_jc*)_M_MALLOC(size*sizeof(open_jc));
804
-	if(jcp->ojc == NULL)
805
-	{
806
-		_M_FREE(jcp);
807
-		return NULL;
808
-	}
809
-	memset( jcp->ojc , 0, size*sizeof(open_jc) );
810
-	jcp->jmqueue.len = jlen;
811
-	jcp->jmqueue.size = 0;
812
-	jcp->jmqueue.head = 1;
813
-	jcp->jmqueue.tail = 0;
814
-	jcp->jmqueue.jsm = (jab_sipmsg*)_M_MALLOC(jlen*sizeof(jab_sipmsg));
815
-	if(jcp->jmqueue.jsm == NULL)
816
-	{
817
-		_M_FREE(jcp->ojc);
818
-		_M_FREE(jcp);
819
-		return NULL;
820
-	}
821
-	memset( jcp->jmqueue.jsm , 0, jlen*sizeof(jab_sipmsg) );
822
-	jcp->jmqueue.ojc = (open_jc*)_M_MALLOC(jlen*sizeof(open_jc));
823
-	if(jcp->jmqueue.ojc == NULL)
824
-	{
825
-		_M_FREE(jcp->jmqueue.jsm);
826
-		_M_FREE(jcp->ojc);
827
-		_M_FREE(jcp);
828
-		return NULL;
829
-	}
830
-	memset( jcp->jmqueue.ojc , 0, jlen*sizeof(open_jc) );
831
-	return jcp;
832
-}
833
-
834
-/**
835
- * add a new element in messages queue
836
- * - jcp : pointer to the Jabber connections pool structure
837
- * - _jsm : pointer to the message
838
- * - _ojc : pointer to the Jabber connection that will be used for this message
839
- * #return : 0 on success or <0 on error
840
- */
841
-int jc_pool_add_jmsg(jc_pool jcp, jab_sipmsg _jsm, open_jc _ojc)
842
-{
843
-	
844
-	if(jcp == NULL)
845
-		return -1;
846
-	if(jcp->jmqueue.size == jcp->jmqueue.len)
847
-		return -2;
848
-		
849
-	DBG("JABBER: JC_POOL_ADD: add connection into the pool\n");
850
-	if(++jcp->jmqueue.tail == jcp->jmqueue.len)
851
-		jcp->jmqueue.tail = 0;
852
-	jcp->jmqueue.size++;
853
-	
854
-	jcp->jmqueue.jsm[jcp->jmqueue.tail] = _jsm;
855
-	jcp->jmqueue.ojc[jcp->jmqueue.tail] = _ojc;
856
-	
857
-	return 0;
858
-}
859
-
860
-/**
861
- * delete first element from messages queue
862
- * - jcp : pointer to the Jabber connections pool structure
863
- * #return : 0 on success or <0 on error
864
- */
865
-int jc_pool_del_jmsg(jc_pool jcp)
866
-{
867
-	if(jcp == NULL)
868
-		return -1;
869
-	if(jcp->jmqueue.size == 0)
870
-		return -2;
871
-	jcp->jmqueue.size--;
872
-	jcp->jmqueue.jsm[jcp->jmqueue.head] = NULL;
873
-	jcp->jmqueue.ojc[jcp->jmqueue.head] = NULL;
874
-	if(++jcp->jmqueue.head == jcp->jmqueue.len)
875
-		jcp->jmqueue.head = 0;
876
-	
877
-	return 0;
878
-}
879
-
880
-/**
881
- * add a new connection in pool
882
- * - jcp : pointer to the Jabber connections pool structure
883
- * #return : 0 on success or <0 on error
884
- */
885
-int jc_pool_add(jc_pool jcp, t_open_jc *jc)
886
-{
887
-	int i = 0;
888
-	
889
-	if(jcp == NULL)
890
-		return -1;
891
-	DBG("JABBER: JC_POOL_ADD: add connection into the pool\n");
892
-	
893
-	while(i < jcp->len && jcp->ojc[i] != NULL)
894
-		i++;
895
-	if(i >= jcp->len)
896
-		return -1;
897
-	jcp->ojc[i] = jc;
898
-	
899
-	return 0;
900
-}
901
-
902
-/**
903
- * get the jabber connection associated with 'id'
904
- * - jcp : pointer to the Jabber connections pool structure
905
- * - id : id of the Jabber connection
906
- * #return : pointer to the open connection to Jabber structure or NULL on error
907
- */
908
-open_jc  jc_pool_get(jc_pool jcp, str *id)
909
-{
910
-	int i = 0;
911
-	open_jc _ojc;
912
-	
913
-	if(jcp == NULL || id == NULL)
914
-		return NULL;
915
-	
916
-	DBG("JABBER: JC_POOL_GET: looking for the connection of <%.*s>"
917
-		" into the pool\n", id->len, id->s);
918
-	while(i < jcp->len)
919
-	{
920
-	 	if((jcp->ojc[i]!=NULL) && (!strncmp(jcp->ojc[i]->id->s, id->s, 
921
-				id->len)))
922
-	 	{
923
-	 		_ojc = jcp->ojc[i];
924
-	 		//jcp->ojc[i] = NULL;
925
-	 		return _ojc;
926
-	 	}
927
-		i++;
928
-	}
929
-	
930
-	return NULL;
931
-}
932
-
933
-/**
934
- * remove the connection associated with 'id' from pool
935
- * - jcp : pointer to the Jabber connections pool structure
936
- * - id : id of the Jabber connection
937
- * #return : 0 on success or <0 on error
938
- */
939
-int jc_pool_del(jc_pool jcp, str *id)
940
-{
941
-	int i = 0;
942
-	
943
-	if(jcp == NULL)
944
-		return -1;
945
-	
946
-	DBG("JABBER: JC_POOL_DEL: removing a connection from the pool\n");
947
-	
948
-	while(i < jcp->len)
949
-	{
950
-	 	if((jcp->ojc[i]!=NULL) && (!strncmp(jcp->ojc[i]->id->s, id->s,