Browse code

- fixed getting request uri from first line - generalized Subscribe message handling operations - exported more functions to handle subscribe info

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@2731 689a6050-402a-0410-94f2-e92a70836424

Anca Vamanu authored on 10/09/2007 12:56:47
Showing 11 changed files
... ...
@@ -381,8 +381,26 @@ Chapter 2. Developer's Guide
381 381
 typedef struct presence_api {
382 382
         add_event_t add_event;
383 383
         contains_event_t contains_event;
384
+        search_event_t search_event;
384 385
         get_event_list_t get_event_list;
386
+
385 387
         update_watchers_t update_watchers_status;
388
+
389
+        /* subs hash table handling functions */
390
+        new_shtable_t new_shtable;
391
+        destroy_shtable_t destroy_shtable;
392
+        insert_shtable_t insert_shtable;
393
+        search_shtable_t search_shtable;
394
+        delete_shtable_t delete_shtable;
395
+        update_shtable_t update_shtable;
396
+        /* function to duplicate a subs structure*/
397
+        mem_copy_subs_t  mem_copy_subs;
398
+        /* function used for update in database*/
399
+        update_db_subs_t update_db_subs;
400
+        /* function to extract dialog information from a
401
+        SUBSCRIBE message */
402
+        extract_sdialog_info_t extract_sdialog_info;
403
+
386 404
 }presence_api_t;
387 405
 ...
388 406
      __________________________________________________________
... ...
@@ -42,6 +42,7 @@ int bind_presence(presence_api_t* api)
42 42
 	
43 43
 	api->add_event = add_event;
44 44
 	api->contains_event= contains_event;
45
+	api->search_event= search_event;
45 46
 	api->get_event_list= get_event_list;
46 47
 	api->update_watchers_status= update_watchers_status;
47 48
 	api->new_shtable= new_shtable;
... ...
@@ -50,6 +51,9 @@ int bind_presence(presence_api_t* api)
50 50
     api->search_shtable= search_shtable;
51 51
     api->delete_shtable= delete_shtable;
52 52
     api->update_shtable= update_shtable;
53
+	api->mem_copy_subs= mem_copy_subs;
54
+	api->update_db_subs= update_db_subs;
55
+	api->extract_sdialog_info= extract_sdialog_info;
53 56
 	return 0;
54 57
 }
55 58
 
... ...
@@ -37,6 +37,7 @@ typedef int (*update_watchers_t)(str pres_uri, pres_ev_t* ev, str* rules_doc);
37 37
 typedef struct presence_api {
38 38
 	add_event_t add_event;
39 39
 	contains_event_t contains_event;
40
+	search_event_t search_event;
40 41
 	get_event_list_t get_event_list;
41 42
 	update_watchers_t update_watchers_status;
42 43
 	/* subs hash table functions */
... ...
@@ -46,6 +47,9 @@ typedef struct presence_api {
46 46
 	search_shtable_t search_shtable;
47 47
 	delete_shtable_t delete_shtable;
48 48
 	update_shtable_t update_shtable;
49
+	mem_copy_subs_t  mem_copy_subs;
50
+	update_db_subs_t update_db_subs;
51
+	extract_sdialog_info_t extract_sdialog_info;
49 52
 } presence_api_t;
50 53
 
51 54
 int bind_presence(presence_api_t* api);
... ...
@@ -30,8 +30,26 @@
30 30
 typedef struct presence_api {
31 31
 	add_event_t add_event;
32 32
 	contains_event_t contains_event;
33
+	search_event_t search_event;
33 34
 	get_event_list_t get_event_list;
35
+	
34 36
 	update_watchers_t update_watchers_status;
37
+	
38
+	/* subs hash table handling functions */
39
+	new_shtable_t new_shtable;
40
+	destroy_shtable_t destroy_shtable;
41
+	insert_shtable_t insert_shtable;
42
+	search_shtable_t search_shtable;
43
+	delete_shtable_t delete_shtable;
44
+	update_shtable_t update_shtable;
45
+	/* function to duplicate a subs structure*/
46
+	mem_copy_subs_t  mem_copy_subs;
47
+	/* function used for update in database*/
48
+	update_db_subs_t update_db_subs;
49
+	/* function to extract dialog information from a
50
+	SUBSCRIBE message */
51
+	extract_sdialog_info_t extract_sdialog_info;
52
+
35 53
 }presence_api_t;
36 54
 ...
37 55
 </programlisting>
... ...
@@ -126,6 +126,7 @@ void destroy_evlist(void);
126 126
 extern evlist_t* EvList;
127 127
 
128 128
 pres_ev_t* search_event(event_t* event);
129
+typedef pres_ev_t* (*search_event_t)(event_t* event);
129 130
 
130 131
 event_t* shm_copy_event(event_t* e);
131 132
 
... ...
@@ -212,8 +212,7 @@ error:
212 212
 	return -1;
213 213
 }
214 214
 
215
-int delete_shtable(shtable_t htable,unsigned int hash_code, 
216
-		str pres_uri, str ev_stored_name, str to_tag)
215
+int delete_shtable(shtable_t htable,unsigned int hash_code,str to_tag)
217 216
 {
218 217
 	subs_t* s= NULL, *ps= NULL;
219 218
 	int found= -1;
... ...
@@ -30,7 +30,6 @@
30 30
 #ifndef PS_HASH_H
31 31
 #define PS_HASH_H
32 32
 
33
-#include "subscribe.h"
34 33
 #include "../../lock_ops.h"
35 34
 
36 35
 #define REMOTE_TYPE   1<<1
... ...
@@ -52,10 +51,11 @@
52 52
 #define SHM_MEM_TYPE     1<< 2
53 53
 
54 54
 /* subscribe hash entry */
55
+struct subscription;
55 56
 
56 57
 typedef struct subs_entry
57 58
 {
58
-	subs_t* entries;
59
+	struct subscription* entries;
59 60
 	gen_lock_t lock;
60 61
 }subs_entry_t;	
61 62
 
... ...
@@ -63,40 +63,40 @@ typedef subs_entry_t* shtable_t;
63 63
 
64 64
 shtable_t new_shtable(int hash_size);
65 65
 
66
-subs_t* search_shtable(shtable_t htable, str callid,str to_tag,str from_tag,
66
+struct subscription* search_shtable(shtable_t htable, str callid,str to_tag,str from_tag,
67 67
 		unsigned int hash_code);
68 68
 
69
-int insert_shtable(shtable_t htable, unsigned int hash_code, subs_t* subs);
69
+int insert_shtable(shtable_t htable, unsigned int hash_code, struct subscription* subs);
70 70
 
71
-int delete_shtable(shtable_t htable, unsigned int hash_code, str pres_uri,
72
-		str ev_stored_name, str to_tag);
71
+int delete_shtable(shtable_t htable, unsigned int hash_code, str to_tag);
73 72
 
74
-int update_shtable(shtable_t htable, unsigned int hash_code, subs_t* subs,
73
+int update_shtable(shtable_t htable, unsigned int hash_code, struct subscription* subs,
75 74
 		int type);
76 75
 
77
-subs_t* mem_copy_subs(subs_t* s, int mem_type);
76
+struct subscription* mem_copy_subs(struct subscription* s, int mem_type);
78 77
 
79
-void free_subs_list(subs_t* s_array, int mem_type);
78
+void free_subs_list(struct subscription* s_array, int mem_type);
80 79
 
81 80
 void destroy_shtable(shtable_t htable, int hash_size);
82 81
 
83 82
 /* subs htable functions type definitions */
84 83
 typedef shtable_t (*new_shtable_t)(int hash_size);
85 84
 
86
-typedef subs_t* (*search_shtable_t)(shtable_t htable, str callid,str to_tag,
85
+typedef struct subscription* (*search_shtable_t)(shtable_t htable, str callid,str to_tag,
87 86
 		str from_tag, unsigned int hash_code);
88 87
 
89 88
 typedef int (*insert_shtable_t)(shtable_t htable, unsigned int hash_code,
90
-		subs_t* subs);
89
+		struct subscription* subs);
91 90
 
92 91
 typedef int (*delete_shtable_t)(shtable_t htable, unsigned int hash_code,
93
-		str pres_uri, str ev_stored_name, str to_tag);
92
+		str to_tag);
94 93
 
95 94
 typedef int (*update_shtable_t)(shtable_t htable, unsigned int hash_code,
96
-		subs_t* subs, int type);
95
+		struct subscription* subs, int type);
97 96
 
98 97
 typedef void (*destroy_shtable_t)(shtable_t htable, int hash_size);
99 98
 
99
+typedef struct subscription* (*mem_copy_subs_t)(struct subscription* s, int mem_type);
100 100
 
101 101
 
102 102
 /* presentity hash table */
... ...
@@ -1540,8 +1540,7 @@ void p_tm_callback( struct cell *t, int type, struct tmcb_params *ps)
1540 1540
 		c_back_param*  cb= (c_back_param*)(*ps->param);
1541 1541
 
1542 1542
 		hash_code= core_hash(&cb->pres_uri, &cb->ev_name, shtable_size);
1543
-		delete_shtable(subs_htable, hash_code,cb->pres_uri, cb->ev_name,
1544
-				cb->to_tag);
1543
+		delete_shtable(subs_htable, hash_code, cb->to_tag);
1545 1544
 
1546 1545
 		delete_db_subs(cb->pres_uri, cb->ev_name, cb->to_tag);
1547 1546
 
... ...
@@ -35,8 +35,7 @@
35 35
 
36 36
 #define FULL_STATE_FLAG (1<<0)
37 37
 #define PARTIAL_STATE_FLAG (1<<1)
38
-//extern char p_event[10] ;
39
-//extern char wi_event[16] ;
38
+
40 39
 #define PRES_LEN 8
41 40
 #define PWINFO_LEN 14
42 41
 #define BLA_LEN 10
... ...
@@ -53,7 +53,7 @@ static str pu_400_rpl  = str_init("Bad request");
53 53
 int send_202ok(struct sip_msg * msg, int lexpire, str *rtag, str* local_contact)
54 54
 {
55 55
 	static str hdr_append;
56
-
56
+	
57 57
 	hdr_append.s = (char *)pkg_malloc( sizeof(char)*(local_contact->len+ 128));
58 58
 	if(hdr_append.s == NULL)
59 59
 	{
... ...
@@ -312,7 +312,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, str *rtag,
312 312
 			/* delete record from hash table also */
313 313
 
314 314
 			subs->local_cseq= delete_shtable(subs_htable,hash_code,
315
-					subs->pres_uri, subs->event->name, subs->to_tag);
315
+					subs->to_tag);
316 316
 		
317 317
 			if(subs->event->type & PUBL_TYPE)
318 318
 			{	
... ...
@@ -494,30 +494,22 @@ void msg_watchers_clean(unsigned int ticks,void *param)
494 494
 
495 495
 int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
496 496
 {
497
-	struct to_body *pto, *pfrom = NULL, TO;
498
-	int lexpire;
499 497
 	int  to_tag_gen = 0;
500
-	str rtag_value;
501 498
 	subs_t subs;
502
-	static char buf[50];
503
-	str rec_route= {0, 0};
504
-	int error_ret = -1;
505
-	int rt  = 0;
506
-	str* contact= NULL;
507 499
 	pres_ev_t* event= NULL;
508 500
 	event_t* parsed_event= NULL;
509 501
 	param_t* ev_param= NULL;
510 502
 	int found;
511 503
 	str reason= {0, 0};
512 504
 	struct sip_uri uri;
513
-	contact_body_t *b;
505
+	int error_ret= -1;
514 506
 
515 507
 	/* ??? rename to avoid collisions with other symbols */
516 508
 	counter ++;
517 509
 
518 510
 	memset(&subs, 0, sizeof(subs_t));
519 511
 
520
-	if ( parse_headers(msg,HDR_EOH_F, 0)==-1 )
512
+	if( parse_headers(msg,HDR_EOH_F, 0)==-1 )
521 513
 	{
522 514
 		LM_ERR("parsing headers\n");
523 515
 
... ...
@@ -566,6 +558,184 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
566 566
 		ev_param= ev_param->next;
567 567
 	}		
568 568
 	
569
+	if(extract_sdialog_info(&subs, msg, &to_tag_gen)< 0)
570
+	{
571
+		LM_ERR("while extracting dialog information\n");
572
+		goto error;
573
+	}
574
+
575
+	/* getting presentity uri from Request-URI if initial subscribe - or else from database*/
576
+	if(to_tag_gen)
577
+	{
578
+		if(uandd_to_uri(msg->parsed_uri.user, msg->parsed_uri.host,
579
+					&subs.pres_uri)< 0)
580
+		{
581
+			LM_ERR("while constructing uri from user and domain\n");
582
+			goto error;
583
+		}
584
+	}
585
+	else
586
+	{
587
+		if(get_stored_info(msg, &subs, &error_ret )< 0)
588
+		{
589
+			LM_ERR("getting stored info\n");
590
+			goto error;
591
+		}
592
+		reason= subs.reason;
593
+	}	
594
+
595
+	/* call event specific subscription handling */
596
+	if(event->evs_subs_handl)
597
+	{
598
+		if(event->evs_subs_handl(msg)< 0)
599
+		{
600
+			LM_ERR("in event specific subscription handling\n");
601
+			goto error;
602
+		}
603
+	}	
604
+
605
+
606
+	/* if dialog initiation Subscribe - get subscription state */
607
+	if(to_tag_gen)
608
+	{
609
+		if( subs.expires== 0 )
610
+		{
611
+			subs.status= TERMINATED_STATUS;
612
+			subs.reason.s= "timeout";
613
+			subs.reason.len= 7;
614
+			goto after_status;	
615
+		}	
616
+
617
+		if(!event->req_auth) 
618
+			subs.status = ACTIVE_STATUS;
619
+		else   
620
+		{
621
+			/* query in watchers_table */
622
+			if(get_db_subs_auth(&subs, &found)< 0)
623
+			{
624
+				LM_ERR("getting subscription status from watchers table\n");
625
+				goto error;
626
+			}
627
+			if(found== 0)
628
+			{
629
+				/*default 'pending' status */
630
+				subs.status= PENDING_STATUS;
631
+				subs.reason.s= NULL;
632
+				subs.reason.len= 0;
633
+				/* here a query to xcap server must be done -> new process maybe */
634
+			
635
+				if(parse_uri(subs.pres_uri.s, subs.pres_uri.len, &uri)< 0)
636
+				{
637
+					LM_ERR("parsing uri\n");
638
+					goto error;
639
+
640
+				}
641
+				if(subs.event->get_rules_doc(&uri.user, &uri.host, &subs.auth_rules_doc)< 0)
642
+				{
643
+					LM_ERR("getting rules doc\n");
644
+					goto error;
645
+				}
646
+				
647
+				if(subs.event->get_auth_status(&subs)< 0)
648
+				{
649
+					LM_ERR("in event specific function is_watcher_allowed\n");
650
+					goto error;
651
+				}
652
+				
653
+				if(insert_db_subs_auth(&subs)< 0)
654
+				{
655
+					LM_ERR("while inserting record in watchers table\n");
656
+					goto error;
657
+				}
658
+			}
659
+			else
660
+			{
661
+				reason= subs.reason;
662
+			}
663
+		}
664
+	}
665
+
666
+after_status:
667
+	/* check if correct status */
668
+	if(get_status_str(subs.status)== NULL)
669
+	{
670
+		LM_ERR("wrong status\n");
671
+		goto error;
672
+	}
673
+	
674
+	if( update_subscription(msg, &subs, &subs.to_tag, to_tag_gen) <0 )
675
+	{	
676
+		LM_ERR("in update_subscription\n");
677
+		goto error;
678
+	}
679
+	if(subs.auth_rules_doc)
680
+	{
681
+		pkg_free(subs.auth_rules_doc->s);
682
+		pkg_free(subs.auth_rules_doc);
683
+	}
684
+	if(reason.s)
685
+		pkg_free(reason.s);
686
+	
687
+	if(subs.pres_uri.s)
688
+		pkg_free(subs.pres_uri.s);
689
+	
690
+	if((!server_address.s) || (server_address.len== 0))
691
+	{
692
+		pkg_free(subs.local_contact.s);
693
+	}
694
+
695
+	return 1;
696
+
697
+bad_event:
698
+
699
+	LM_ERR("Missing or unsupported event header field value\n");
700
+		
701
+	if(parsed_event)
702
+		LM_ERR("\tevent= %.*s\n",parsed_event->text.len,parsed_event->text.s);
703
+	
704
+	if(reply_bad_event(msg)< 0)
705
+		goto error;
706
+
707
+	error_ret = 0;
708
+
709
+error:
710
+
711
+	if(subs.pres_uri.s)
712
+		pkg_free(subs.pres_uri.s);
713
+	
714
+	if(subs.auth_rules_doc)
715
+	{
716
+		if(subs.auth_rules_doc->s)
717
+			pkg_free(subs.auth_rules_doc->s);
718
+		pkg_free(subs.auth_rules_doc);
719
+	}
720
+	if(reason.s)
721
+		pkg_free(reason.s);
722
+
723
+	if(((!server_address.s) ||(server_address.len== 0))&& subs.local_contact.s)
724
+	{
725
+		pkg_free(subs.local_contact.s);
726
+	}
727
+	if(subs.record_route.s)
728
+		pkg_free(subs.record_route.s);
729
+
730
+	return error_ret;
731
+
732
+}
733
+
734
+
735
+int extract_sdialog_info(subs_t* subs,struct sip_msg* msg, int* to_tag_gen)
736
+{
737
+	static char buf[50];
738
+	str rec_route= {0, 0};
739
+	int rt  = 0;
740
+	str* contact= NULL;
741
+	contact_body_t *b;
742
+	struct to_body *pto, *pfrom = NULL, TO;
743
+	int lexpire;
744
+	str rtag_value;
745
+	struct sip_uri uri;
746
+
569 747
 	/* examine the expire header field */
570 748
 	if(msg->expires && msg->expires->body.len > 0)
571 749
 	{
... ...
@@ -580,13 +750,13 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
580 580
 	}
581 581
 	else 
582 582
 	{
583
-		LM_DBG("'expires' not found; default=%d\n",event->default_expires);
584
-		lexpire = event->default_expires;
583
+		LM_DBG("'expires' not found; default=%d\n",subs->event->default_expires);
584
+		lexpire = subs->event->default_expires;
585 585
 	}
586 586
 	if(lexpire > max_expires)
587 587
 		lexpire = max_expires;
588 588
 
589
-	subs.expires = lexpire;
589
+	subs->expires = lexpire;
590 590
 
591 591
 	if( msg->to==NULL || msg->to->body.s==NULL)
592 592
 	{
... ...
@@ -613,8 +783,8 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
613 613
 	if( pto->parsed_uri.user.s && pto->parsed_uri.host.s &&
614 614
 		pto->parsed_uri.user.len && pto->parsed_uri.host.len)
615 615
 	{
616
-		subs.to_user = pto->parsed_uri.user;
617
-		subs.to_domain = pto->parsed_uri.host;
616
+		subs->to_user = pto->parsed_uri.user;
617
+		subs->to_domain = pto->parsed_uri.host;
618 618
 	}
619 619
 	else
620 620
 	{
... ...
@@ -623,8 +793,8 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
623 623
 			LM_ERR("while parsing uri\n");
624 624
 			goto error;
625 625
 		}
626
-		subs.to_user = uri.user;
627
-		subs.to_domain = uri.host;
626
+		subs->to_user = uri.user;
627
+		subs->to_domain = uri.host;
628 628
 	}
629 629
 
630 630
 	/* examine the from header */
... ...
@@ -648,8 +818,8 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
648 648
 	if( pfrom->parsed_uri.user.s && pfrom->parsed_uri.host.s && 
649 649
 		pfrom->parsed_uri.user.len && pfrom->parsed_uri.host.len)
650 650
 	{
651
-		subs.from_user = pfrom->parsed_uri.user;
652
-		subs.from_domain = pfrom->parsed_uri.host;
651
+		subs->from_user = pfrom->parsed_uri.user;
652
+		subs->from_domain = pfrom->parsed_uri.host;
653 653
 	}
654 654
 	else
655 655
 	{
... ...
@@ -658,8 +828,8 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
658 658
 			LM_ERR("while parsing uri\n");
659 659
 			goto error;
660 660
 		}
661
-		subs.from_user = uri.user;
662
-		subs.from_domain = uri.host;
661
+		subs->from_user = uri.user;
662
+		subs->from_domain = uri.host;
663 663
 	}
664 664
 
665 665
 
... ...
@@ -667,7 +837,7 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
667 667
 	if (pto->tag_value.s==NULL || pto->tag_value.len==0 )
668 668
 	{  
669 669
 		LM_DBG("generating to_tag\n");
670
-		to_tag_gen = 1;
670
+		*to_tag_gen = 1;
671 671
 		/*generate to_tag then insert it in avp*/
672 672
 		
673 673
 		rtag_value.s = buf;
... ...
@@ -681,23 +851,24 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
681 681
 	}
682 682
 	else
683 683
 	{
684
+		*to_tag_gen = 0;
684 685
 		rtag_value=pto->tag_value;
685 686
 	}
686
-	subs.to_tag = rtag_value;
687
+	subs->to_tag = rtag_value;
687 688
 
688 689
 	if( msg->callid==NULL || msg->callid->body.s==NULL)
689 690
 	{
690 691
 		LM_ERR("cannot parse callid header\n");
691 692
 		goto error;
692 693
 	}
693
-	subs.callid = msg->callid->body;
694
+	subs->callid = msg->callid->body;
694 695
 
695 696
 	if( msg->cseq==NULL || msg->cseq->body.s==NULL)
696 697
 	{
697 698
 		LM_ERR("cannot parse cseq header\n");
698 699
 		goto error;
699 700
 	}
700
-	if (str2int( &(get_cseq(msg)->number), &subs.remote_cseq)!=0 )
701
+	if (str2int( &(get_cseq(msg)->number), &subs->remote_cseq)!=0 )
701 702
 	{
702 703
 		LM_ERR("cannot parse cseq number\n");
703 704
 		goto error;
... ...
@@ -720,10 +891,10 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
720 720
 		LM_ERR("cannot parse contact header\n");
721 721
 		goto error;
722 722
 	}
723
-	subs.contact = b->contacts->uri;
723
+	subs->contact = b->contacts->uri;
724 724
 
725
-	LM_DBG("subs.contact= %.*s - len = %d\n",subs.contact.len,
726
-			subs.contact.s, subs.contact.len);	
725
+	LM_DBG("subs->contact= %.*s - len = %d\n",subs->contact.len,
726
+			subs->contact.s, subs->contact.len);	
727 727
 
728 728
 	/*process record route and add it to a string*/
729 729
 	if (msg->record_route!=NULL)
... ...
@@ -737,18 +908,18 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
737 737
 		//	goto error;
738 738
 		}
739 739
 	}
740
-	subs.record_route = rec_route;
740
+	subs->record_route = rec_route;
741 741
 			
742
-	subs.sockinfo_str= msg->rcv.bind_address->sock_str;
742
+	subs->sockinfo_str= msg->rcv.bind_address->sock_str;
743 743
 
744 744
 	if( pfrom->tag_value.s ==NULL || pfrom->tag_value.len == 0)
745 745
 	{
746 746
 		LM_ERR("no from tag value present\n");
747 747
 		goto error;
748 748
 	}
749
-	subs.from_tag = pfrom->tag_value;
749
+	subs->from_tag = pfrom->tag_value;
750 750
 
751
-	subs.version = 0;
751
+	subs->version = 0;
752 752
 	
753 753
 	if((!server_address.s) || (server_address.len== 0))
754 754
 	{
... ...
@@ -758,175 +929,19 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
758 758
 			LM_ERR("in function get_local_contact\n");
759 759
 			goto error;
760 760
 		}
761
-		subs.local_contact= *contact;
761
+		subs->local_contact= *contact;
762 762
 	}
763 763
 	else
764
-		subs.local_contact= server_address;
765
-
766
-	/* getting presentity uri from Request-URI if initial subscribe - or else from database*/
767
-	if(to_tag_gen)
768
-	{
769
-		subs.pres_uri= msg->first_line.u.request.uri;
770
-	}
771
-	else
772
-	{
773
-		if(get_stored_info(msg, &subs, &error_ret )< 0)
774
-		{
775
-			LM_ERR("getting stored info\n");
776
-			goto error;
777
-		}
778
-		reason= subs.reason;
779
-	}	
780
-
781
-	/* call event specific subscription handling */
782
-	if(event->evs_subs_handl)
783
-	{
784
-		if(event->evs_subs_handl(msg)< 0)
785
-		{
786
-			LM_ERR("in event specific subscription handling\n");
787
-			goto error;
788
-		}
789
-	}	
790
-
791
-
792
-	/* if dialog initiation Subscribe - get subscription state */
793
-	if(to_tag_gen)
794
-	{
795
-		if( subs.expires== 0 )
796
-		{
797
-			subs.status= TERMINATED_STATUS;
798
-			subs.reason.s= "timeout";
799
-			subs.reason.len= 7;
800
-			goto after_status;	
801
-		}	
802
-
803
-		if(!event->req_auth) 
804
-			subs.status = ACTIVE_STATUS;
805
-		else   
806
-		{
807
-			/* query in watchers_table */
808
-			if(get_db_subs_auth(&subs, &found)< 0)
809
-			{
810
-				LM_ERR("getting subscription status from watchers table\n");
811
-				goto error;
812
-			}
813
-			if(found== 0)
814
-			{
815
-				/*default 'pending' status */
816
-				subs.status= PENDING_STATUS;
817
-				subs.reason.s= NULL;
818
-				subs.reason.len= 0;
819
-				/* here a query to xcap server must be done -> new process maybe */
820
-			
821
-				if(parse_uri(subs.pres_uri.s, subs.pres_uri.len, &uri)< 0)
822
-				{
823
-					LM_ERR("parsing uri\n");
824
-					goto error;
825
-
826
-				}
827
-				if(subs.event->get_rules_doc(&uri.user, &uri.host, &subs.auth_rules_doc)< 0)
828
-				{
829
-					LM_ERR("getting rules doc\n");
830
-					goto error;
831
-				}
832
-				
833
-				if(subs.event->get_auth_status(&subs)< 0)
834
-				{
835
-					LM_ERR("in event specific function is_watcher_allowed\n");
836
-					goto error;
837
-				}
838
-				
839
-				if(insert_db_subs_auth(&subs)< 0)
840
-				{
841
-					LM_ERR("while inserting record in watchers table\n");
842
-					goto error;
843
-				}
844
-			}
845
-			else
846
-			{
847
-				reason= subs.reason;
848
-			}
849
-		}
850
-	}
851
-
852
-after_status:
853
-	/* check if correct status */
854
-	if(get_status_str(subs.status)== NULL)
855
-	{
856
-		LM_ERR("wrong status\n");
857
-		goto error;
858
-	}
859
-	
860
-	if( update_subscription(msg, &subs, &rtag_value, to_tag_gen) <0 )
861
-	{	
862
-		LM_ERR("in update_subscription\n");
863
-		goto error;
864
-	}
865
-	if(subs.auth_rules_doc)
866
-	{
867
-		pkg_free(subs.auth_rules_doc->s);
868
-		pkg_free(subs.auth_rules_doc);
869
-	}
870
-	if(reason.s)
871
-		pkg_free(reason.s);
872
-
873
-	if(subs.record_route.s)
874
-		pkg_free(subs.record_route.s);
764
+		subs->local_contact= server_address;
875 765
 	
876
-	if(to_tag_gen== 0)
877
-	{
878
-		if(subs.pres_uri.s)
879
-			pkg_free(subs.pres_uri.s);
880
-	}
881
-	if(contact)
882
-	{	
883
-		if(contact->s)
884
-			pkg_free(contact->s);
885
-		pkg_free(contact);
886
-	}
887
-	return 1;
888
-
889
-bad_event:
890
-
891
-	LM_ERR("Missing or unsupported event header field value\n");
892
-		
893
-	if(parsed_event)
894
-		LM_ERR("\tevent= %.*s\n",parsed_event->text.len,parsed_event->text.s);
766
+	return 0;
895 767
 	
896
-	if(reply_bad_event(msg)< 0)
897
-		goto error;
898
-
899
-	error_ret = 0;
900
-
901 768
 error:
902 769
 
903
-	if(subs.record_route.s)
904
-		pkg_free(subs.record_route.s);
905
-
906
-	if(to_tag_gen== 0)
907
-	{
908
-		if(subs.pres_uri.s)
909
-			pkg_free(subs.pres_uri.s);
910
-	}
911
-	if(contact)
912
-	{	
913
-		if(contact->s)
914
-			pkg_free(contact->s);
915
-		pkg_free(contact);
916
-	}
917
-	if(subs.auth_rules_doc)
918
-	{
919
-		if(subs.auth_rules_doc->s)
920
-			pkg_free(subs.auth_rules_doc->s);
921
-		pkg_free(subs.auth_rules_doc);
922
-	}
923
-	if(reason.s)
924
-		pkg_free(reason.s);
925
-
926
-	return error_ret;
927
-
770
+	return -1;
928 771
 }
929 772
 
773
+
930 774
 int get_stored_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
931 775
 {	
932 776
 	str pres_uri= {0, 0}, reason={0, 0};
... ...
@@ -1213,7 +1228,7 @@ error:
1213 1213
 }
1214 1214
 
1215 1215
 
1216
-int handle_expired_subs(subs_t* s, subs_t* prev_s)
1216
+int handle_expired_subs(subs_t* s)
1217 1217
 {
1218 1218
 	/* send Notify with state=terminated;reason=timeout */
1219 1219
 	
... ...
@@ -1233,7 +1248,19 @@ int handle_expired_subs(subs_t* s, subs_t* prev_s)
1233 1233
 }
1234 1234
 
1235 1235
 void timer_db_update(unsigned int ticks,void *param)
1236
-{
1236
+{	
1237
+	int no_lock;
1238
+
1239
+	if(ticks== 0 && param == NULL)
1240
+		no_lock= 1;
1241
+	
1242
+	update_db_subs(active_watchers_table, subs_htable, 
1243
+			shtable_size, no_lock, handle_expired_subs);
1244
+}
1245
+
1246
+void update_db_subs(char* db_table, shtable_t hash_table,
1247
+	int htable_size, int no_lock, handle_expired_func_t handle_expired_func)
1248
+{	
1237 1249
 	db_key_t query_cols[22], update_cols[7], result_cols[6];
1238 1250
 	db_val_t query_vals[22], update_vals[7];
1239 1251
 	db_op_t update_ops[1];
... ...
@@ -1245,14 +1272,11 @@ void timer_db_update(unsigned int ticks,void *param)
1245 1245
 		contact_col, local_contact_col, version_col,socket_info_col,reason_col;
1246 1246
 	int u_expires_col, u_local_cseq_col, u_remote_cseq_col, u_version_col, 
1247 1247
 		u_reason_col, u_status_col; 
1248
-	int no_lock= 0, i;
1248
+	int i;
1249 1249
 	subs_t* s= NULL, *prev_s= NULL;
1250 1250
 	int n_query_cols= 0, n_update_cols= 0;
1251 1251
 	int n_query_update;
1252 1252
 
1253
-	if(ticks== 0 && param == NULL)
1254
-		no_lock= 1;
1255
-
1256 1253
 	query_cols[pres_uri_col= n_query_cols] ="pres_uri";
1257 1254
 	query_vals[pres_uri_col].type = DB_STR;
1258 1255
 	query_vals[pres_uri_col].nul = 0;
... ...
@@ -1393,33 +1417,32 @@ void timer_db_update(unsigned int ticks,void *param)
1393 1393
 		LM_ERR("null database connection\n");
1394 1394
 		return;
1395 1395
 	}
1396
-	if(pa_dbf.use_table(pa_db, active_watchers_table)< 0)
1396
+	if(pa_dbf.use_table(pa_db, db_table)< 0)
1397 1397
 	{
1398 1398
 		LM_ERR("in use table\n");
1399 1399
 		return ;
1400 1400
 	}
1401
-
1402
-	for(i=0; i<shtable_size; i++) 
1401
+	for(i=0; i<htable_size; i++) 
1403 1402
 	{
1404 1403
 		if(!no_lock)
1405
-			lock_get(&subs_htable[i].lock);	
1404
+			lock_get(&hash_table[i].lock);	
1406 1405
 
1407
-		prev_s= subs_htable[i].entries;
1406
+		prev_s= hash_table[i].entries;
1408 1407
 		s= prev_s->next;
1409 1408
 	
1410 1409
 		while(s)
1411 1410
 		{
1412 1411
 			printf_subs(s);
1413
-			if(s->expires < ((int)time(NULL)- 50))	
1412
+			if(s->expires < (int)time(NULL)- 50)	
1414 1413
 			{
1415 1414
 				LM_DBG("Found expired record\n");
1416 1415
 				if(!no_lock)
1417 1416
 				{
1418
-					if(handle_expired_subs(s, prev_s)< 0)
1417
+					if(handle_expired_func(s)< 0)
1419 1418
 					{
1420 1419
 						LM_ERR("in function handle_expired_record\n");
1421 1420
 						if(!no_lock)
1422
-							lock_release(&subs_htable[i].lock);	
1421
+							lock_release(&hash_table[i].lock);	
1423 1422
 						return ;
1424 1423
 					}
1425 1424
 				}
... ...
@@ -1458,7 +1481,7 @@ void timer_db_update(unsigned int ticks,void *param)
1458 1458
 					{
1459 1459
 						LM_ERR("updating in database\n");
1460 1460
 						if(!no_lock)
1461
-							lock_release(&subs_htable[i].lock);	
1461
+							lock_release(&hash_table[i].lock);	
1462 1462
 						return ;
1463 1463
 					}
1464 1464
 					break;
... ...
@@ -1492,7 +1515,7 @@ void timer_db_update(unsigned int ticks,void *param)
1492 1492
 					{
1493 1493
 						LM_ERR("unsuccessful sql insert\n");
1494 1494
 						if(!no_lock)
1495
-							lock_release(&subs_htable[i].lock);
1495
+							lock_release(&hash_table[i].lock);
1496 1496
 						return ;
1497 1497
 					}
1498 1498
 					break;										
... ...
@@ -1504,7 +1527,7 @@ void timer_db_update(unsigned int ticks,void *param)
1504 1504
 			s= s->next;
1505 1505
 		}
1506 1506
 		if(!no_lock)
1507
-			lock_release(&subs_htable[i].lock);	
1507
+			lock_release(&hash_table[i].lock);	
1508 1508
 	}
1509 1509
 
1510 1510
 	update_vals[0].val.int_val= (int)time(NULL)- 10;
... ...
@@ -35,6 +35,7 @@
35 35
 struct pres_ev;
36 36
 
37 37
 #include "event_list.h"
38
+#include "hash.h"
38 39
 
39 40
 #define ACTIVE_STATUS        1
40 41
 #define PENDING_STATUS       2
... ...
@@ -80,8 +81,6 @@ int delete_db_subs(str pres_uri, str ev_stored_name, str to_tag);
80 80
 
81 81
 void timer_db_update(unsigned int ticks,void *param);
82 82
 
83
-int restore_db_subs(void);
84
-
85 83
 int update_subs_db(subs_t* subs, int type);
86 84
 
87 85
 int refresh_watcher(str* pres_uri, str* watcher_uri, str* event, 
... ...
@@ -89,4 +88,17 @@ int refresh_watcher(str* pres_uri, str* watcher_uri, str* event,
89 89
 
90 90
 typedef int (*refresh_watcher_t)(str*, str* , str* ,int , str* );
91 91
 
92
+int restore_db_subs(void);
93
+
94
+typedef int (*handle_expired_func_t)(subs_t* );
95
+void update_db_subs(char* db_table, shtable_t hash_table,int htable_size,
96
+		int no_lock, handle_expired_func_t handle_expired_subs);
97
+
98
+typedef void (*update_db_subs_t)(char* ,shtable_t ,int ,int ,
99
+		handle_expired_func_t);
100
+
101
+int extract_sdialog_info(subs_t* subs,struct sip_msg* msg, int* to_tag_gen);
102
+typedef int (*extract_sdialog_info_t)(subs_t* subs, struct sip_msg* msg,
103
+		int* to_tag_gen);
104
+
92 105
 #endif