-
Notifications
You must be signed in to change notification settings - Fork 564
/
presentity.c
1580 lines (1367 loc) · 36.1 KB
/
presentity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* presence module - presence server implementation
*
* Copyright (C) 2006 Voice Sistem S.R.L.
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2006-08-15 initial version (Anca Vamanu)
* 2010-10-19 support for extra headers (osas)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../../db/db.h"
#include "../../dprint.h"
#include "../../mem/shm_mem.h"
#include "../../str.h"
#include "../../receive.h"
#include "../../usr_avp.h"
#include "../alias_db/alias_db.h"
#include "../../data_lump_rpl.h"
#include "presentity.h"
#include "presence.h"
#include "notify.h"
#include "publish.h"
#include "hash.h"
#include "utils_func.h"
#define DLG_STATES_NO 4
char* dialog_states[]= { "trying",
"early",
"confirmed",
"terminated"};
char* presence_notes[]={ "Calling",
"Calling",
"On the phone",
""};
unsigned char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name);
xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
const char *ns);
static str pu_200_rpl = str_init("OK");
static str pu_412_rpl = str_init("Conditional request failed");
static char etag_buf[ETAG_LEN];
int generate_ETag(int publ_count, str* etag)
{
etag->s = etag_buf;
memset(etag_buf, 0, ETAG_LEN);
etag->len = sprintf (etag_buf, "%c.%d.%d.%d.%d",
prefix, (int)startup_time, pid, counter, publ_count);
if( etag->len <0 )
{
LM_ERR("unsuccessfull sprintf\n ");
return -1;
}
if(etag->len > ETAG_LEN)
{
LM_ERR("buffer size overflown\n");
return -1;
}
LM_DBG("etag= %.*s\n",etag->len, etag->s);
return 0;
}
int publ_send200ok(struct sip_msg *msg, int lexpire, str etag)
{
char buf[128];
int buf_len= 128, size;
str hdr_append= {0, 0}, hdr_append2= {0, 0} ;
LM_DBG("send 200OK reply, etag= %.*s\n", etag.len, etag.s);
hdr_append.s = buf;
hdr_append.s[0]='\0';
hdr_append.len = sprintf(hdr_append.s, "Expires: %d\r\n",((lexpire< expires_offset)?0:
(lexpire - expires_offset)));
if(hdr_append.len < 0)
{
LM_ERR("unsuccessful sprintf\n");
goto error;
}
if(hdr_append.len > buf_len)
{
LM_ERR("buffer size overflown\n");
goto error;
}
hdr_append.s[hdr_append.len]= '\0';
if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
{
LM_ERR("unable to add lump_rl\n");
goto error;
}
size= 20 + etag.len;
hdr_append2.s = (char *)pkg_malloc(size);
if(hdr_append2.s == NULL)
{
ERR_MEM(PKG_MEM_STR);
}
hdr_append2.s[0]='\0';
hdr_append2.len = sprintf(hdr_append2.s, "SIP-ETag: %.*s\r\n", etag.len, etag.s);
if(hdr_append2.len < 0)
{
LM_ERR("unsuccessful sprintf\n ");
goto error;
}
if(hdr_append2.len+1 > size)
{
LM_ERR("buffer size overflown\n");
goto error;
}
hdr_append2.s[hdr_append2.len]= '\0';
if (add_lump_rpl(msg, hdr_append2.s, hdr_append2.len, LUMP_RPL_HDR)==0 )
{
LM_ERR("unable to add lump_rl\n");
goto error;
}
if( sigb.reply( msg, 200, &pu_200_rpl, 0)== -1)
{
LM_ERR("sending reply\n");
goto error;
}
pkg_free(hdr_append2.s);
return 0;
error:
if(hdr_append2.s)
pkg_free(hdr_append2.s);
return -1;
}
xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
{
xmlAttrPtr attr = node->properties;
while (attr) {
if (xmlStrcasecmp(attr->name, (unsigned char*)name) == 0)
return attr;
attr = attr->next;
}
return NULL;
}
unsigned char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
{
xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
if (attr)
return xmlNodeGetContent(attr->children);
else
return NULL;
}
xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
{
xmlNodePtr cur = node->children;
while (cur) {
if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0)
return cur;
cur = cur->next;
}
return NULL;
}
#define bla_extract_dlginfo(node, callid, fromtag, totag) \
do {\
callid = xmlNodeGetAttrContentByName(node, "call-id");\
dir = xmlNodeGetAttrContentByName(node, "direction");\
if(dir == NULL) {\
LM_ERR("Dialog direction not specified\n");\
goto error;\
}\
if(xmlStrcasecmp(dir, (unsigned char*)"initiator") == 0) {\
fromtag = xmlNodeGetAttrContentByName(node, "local-tag");\
totag = xmlNodeGetAttrContentByName(node, "remote-tag");\
} else {\
totag = xmlNodeGetAttrContentByName(node, "local-tag");\
fromtag = xmlNodeGetAttrContentByName(node, "remote-tag");\
}\
xmlFree(dir);\
dir = NULL;\
}while(0)
int bla_same_dialog(unsigned char* n_callid, unsigned char* n_fromtag, unsigned char* n_totag,
unsigned char* o_callid, unsigned char* o_fromtag, unsigned char* o_totag)
{
if(n_callid && o_callid && xmlStrcasecmp(n_callid, o_callid))
return 0;
if(n_fromtag && o_fromtag && xmlStrcasecmp(n_fromtag, o_fromtag))
return 0;
if(n_totag && o_totag && xmlStrcasecmp(n_totag, o_totag))
return 0;
return 1;
}
int dialog_fix_remote_target(str *body, str *fixed_body)
{
xmlDocPtr doc = NULL;
xmlNodePtr n_dlg_node;
xmlNodePtr remote_node;
xmlNodePtr identity_node;
xmlNodePtr node;
xmlErrorPtr xml_error;
unsigned char* attr;
if (!fixed_body)
{
LM_ERR("invalid NULL fixed_body pointer\n");
goto error;
}
doc = xmlParseMemory(body->s, body->len);
if (!doc)
{
xml_error = xmlGetLastError();
LM_ERR("Failed to parse xml dialog body: %s\n",
xml_error ? xml_error->message : "unknown error");
goto error;
}
n_dlg_node = xmlNodeGetChildByName(doc->children, "dialog");
for(; n_dlg_node; n_dlg_node=n_dlg_node->next)
{
if(xmlStrcasecmp(n_dlg_node->name, (unsigned char*)"dialog")!= 0)
continue;
/* change the remote target - don't let it pass contact on the other side */
remote_node = xmlNodeGetChildByName(n_dlg_node, "remote");
if(remote_node)
{
node = xmlNodeGetChildByName(remote_node, "target");
if(node)
{
xmlUnlinkNode(node);
xmlFreeNode(node);
/* add another target node */
identity_node = xmlNodeGetChildByName(remote_node, "identity");
if(identity_node == NULL)
{
LM_ERR("No remote identity node found\n");
goto error;
}
attr = xmlNodeGetContent(identity_node);
if(attr == NULL)
{
LM_ERR("No identity node content\n");
goto error;
}
node = xmlNewChild(remote_node, 0, (unsigned char*)"target", 0);
if(node == NULL)
{
LM_ERR("Failed to add new node target\n");
xmlFree(attr);
goto error;
}
xmlNewProp(node, BAD_CAST "uri", attr);
xmlFree(attr);
}
}
}
xmlDocDumpMemory(doc, (xmlChar **)(void *)&fixed_body->s,
&fixed_body->len);
xmlFreeDoc(doc);
return 0;
error:
if (doc)
xmlFreeDoc(doc);
return -1;
}
int get_dialog_state(str body, int *dialog_state)
{
xmlDocPtr doc;
xmlNodePtr node;
unsigned char* state = NULL;
int i;
doc = xmlParseMemory(body.s, body.len);
if(doc== NULL)
{
LM_ERR("failed to parse xml document\n");
return -1;
}
node = doc->children;
node = xmlNodeGetChildByName(node, "dialog");
if(node == NULL)
{
*dialog_state = DLG_DESTROYED;
xmlFreeDoc(doc);
return 0;
}
node = xmlNodeGetChildByName(node, "state");
if(node == NULL)
{
LM_ERR("Malformed document - no state found\n");
goto error;
}
state = xmlNodeGetContent(node);
if(state == NULL)
{
LM_ERR("Malformed document - null state\n");
goto error;
}
LM_DBG("state = %s\n", state);
for(i = 0; i< DLG_STATES_NO; i++)
{
if(xmlStrcasecmp(state, BAD_CAST dialog_states[i])==0)
{
break;
}
}
xmlFree(state);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
if(i == DLG_STATES_NO)
{
LM_ERR("Wrong dialog state\n");
return -1;
}
*dialog_state = i;
return 0;
error:
xmlFreeDoc(doc);
return -1;
}
int check_if_dialog(str body, int *is_dialog)
{
xmlDocPtr doc;
xmlNodePtr node;
doc = xmlParseMemory(body.s, body.len);
if(doc== NULL)
{
LM_ERR("failed to parse xml document\n");
return -1;
}
node = doc->children;
node = xmlNodeGetChildByName(node, "dialog");
if(node == NULL)
*is_dialog = 0;
else
*is_dialog = 1;
xmlFreeDoc(doc);
return 0;
}
int update_presentity(struct sip_msg* msg, presentity_t* presentity, int* sent_reply)
{
// static db_ps_t my_ps_insert = NULL, my_ps_update_no_body = NULL,
// my_ps_update_body = NULL;
// static db_ps_t my_ps_delete = NULL, my_ps_query = NULL;
db_key_t query_cols[13], update_keys[8], result_cols[1];
db_op_t query_ops[13];
db_val_t query_vals[13], update_vals[8];
int n_query_cols = 0;
int n_update_cols = 0;
str etag= {NULL, 0};
str notify_body = {NULL, 0};
str cur_etag= {NULL, 0};
str* rules_doc= NULL;
str pres_uri= {NULL, 0};
pres_entry_t* p= NULL;
unsigned int hash_code;
unsigned int turn;
str body = presentity->body;
str *extra_hdrs = presentity->extra_hdrs;
db_res_t *result= NULL;
*sent_reply= 0;
if(presentity->event->req_auth)
{
/* get rules_document */
if(presentity->event->get_rules_doc(&presentity->user,
&presentity->domain, &rules_doc))
{
LM_ERR("getting rules doc\n");
goto error;
}
}
if(uandd_to_uri(presentity->user, presentity->domain, &pres_uri)< 0)
{
LM_ERR("constructing uri from user and domain\n");
goto error;
}
query_cols[n_query_cols] = &str_domain_col;
query_ops[n_query_cols] = OP_EQ;
query_vals[n_query_cols].type = DB_STR;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.str_val = presentity->domain;
n_query_cols++;
query_cols[n_query_cols] = &str_username_col;
query_ops[n_query_cols] = OP_EQ;
query_vals[n_query_cols].type = DB_STR;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.str_val = presentity->user;
n_query_cols++;
query_cols[n_query_cols] = &str_event_col;
query_ops[n_query_cols] = OP_EQ;
query_vals[n_query_cols].type = DB_STR;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.str_val = presentity->event->name;
n_query_cols++;
query_cols[n_query_cols] = &str_etag_col;
query_ops[n_query_cols] = OP_EQ;
query_vals[n_query_cols].type = DB_STR;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.str_val = presentity->etag;
n_query_cols++;
result_cols[0] = &str_etag_col;
hash_code= core_hash(&pres_uri, NULL, phtable_size);
if(presentity->etag_new)
{
if( publ_send200ok(msg, presentity->expires, presentity->etag)< 0)
{
LM_ERR("sending 200OK\n");
goto error;
}
*sent_reply= 1;
/* insert new record in hash_table */
p = insert_phtable(&pres_uri, presentity->event->evp->parsed,
&presentity->etag, presentity->sphere, 1);
if (p==NULL)
{
LM_ERR("inserting record in hash table\n");
goto error;
}
/* insert new record into database */
query_cols[n_query_cols] = &str_expires_col;
query_vals[n_query_cols].type = DB_INT;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.int_val = presentity->expires+
(int)time(NULL);
n_query_cols++;
query_cols[n_query_cols] = &str_sender_col;
query_vals[n_query_cols].type = DB_STR;
query_vals[n_query_cols].nul = 0;
if( presentity->sender)
{
query_vals[n_query_cols].val.str_val = *presentity->sender;
}
else
{
query_vals[n_query_cols].val.str_val.s = "";
query_vals[n_query_cols].val.str_val.len = 0;
}
n_query_cols++;
query_cols[n_query_cols] = &str_body_col;
query_vals[n_query_cols].type = DB_BLOB;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.str_val = body;
n_query_cols++;
query_cols[n_query_cols] = &str_received_time_col;
query_vals[n_query_cols].type = DB_INT;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.int_val = presentity->received_time;
n_query_cols++;
if(extra_hdrs)
{
query_cols[n_query_cols] = &str_extra_hdrs_col;
query_vals[n_query_cols].type = DB_BLOB;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.str_val = *extra_hdrs;
n_query_cols++;
}
if (pa_dbf.use_table(pa_db, &presentity_table) < 0)
{
LM_ERR("unsuccessful use_table\n");
goto error;
}
LM_DBG("inserting %d cols into table\n",n_query_cols);
//CON_PS_REFERENCE(pa_db) = &my_ps_insert;
if (pa_dbf.insert(pa_db, query_cols, query_vals, n_query_cols) < 0)
{
LM_ERR("inserting new record in database\n");
goto error;
}
goto send_notify;
}
else
{
lock_get(&pres_htable[hash_code].lock);
p = search_phtable_etag(&pres_uri, presentity->event->evp->parsed,
&presentity->etag, hash_code);
if (p) {
turn = p->last_turn++;
LM_DBG("xXx - %.*s my turn is %d, current turn is %d\n",pres_uri.len, pres_uri.s, turn, p->current_turn);
/* wait to get our turn as order of handling pubishs
(need to wait the ongoing published to terminate
before starting */
while (p && turn!=p->current_turn) {
lock_release(&pres_htable[hash_code].lock);
sleep_us(100);
lock_get(&pres_htable[hash_code].lock);
p = search_phtable_etag(&pres_uri, presentity->event->evp->parsed,
&presentity->etag, hash_code);
}
} else {
lock_release(&pres_htable[hash_code].lock);
/* search also in db */
if (pa_dbf.use_table(pa_db, &presentity_table) < 0)
{
LM_ERR("unsuccessful sql use table\n");
goto error;
}
if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals,
result_cols, n_query_cols, 1, 0, &result) < 0)
{
LM_ERR("unsuccessful sql query\n");
goto error;
}
if(result== NULL)
goto error;
if (result->n <= 0)
{
pa_dbf.free_result(pa_db, result);
LM_ERR("No E_Tag match [%.*s]\n", presentity->etag.len,
presentity->etag.s);
if (sigb.reply(msg, 412, &pu_412_rpl, 0) == -1)
{
LM_ERR("sending '412 Conditional request failed' reply\n");
goto error;
}
*sent_reply= 1;
goto done;
}
pa_dbf.free_result(pa_db, result);
LM_INFO("*** found in db but not in htable [%.*s]\n",
presentity->etag.len, presentity->etag.s);
}
/* record found */
if(presentity->expires == 0)
{
/* delete from hash table */
if(p && delete_phtable(p, hash_code)< 0)
{
LM_ERR("deleting record from hash table failed\n");
}
lock_release(&pres_htable[hash_code].lock);
if( publ_send200ok(msg, presentity->expires, presentity->etag)< 0)
{
LM_ERR("sending 200OK reply\n");
goto error;
}
*sent_reply= 1;
if(publ_notify(presentity, pres_uri, body.s ? &body : 0,
&presentity->etag, rules_doc, NULL) < 0)
{
LM_ERR("while sending notify\n");
goto error;
}
if (pa_dbf.use_table(pa_db, &presentity_table) < 0)
{
LM_ERR("unsuccessful sql use table\n");
goto error;
}
// CON_PS_REFERENCE(pa_db) = &my_ps_delete;
if(pa_dbf.delete(pa_db,query_cols,0,query_vals,n_query_cols)<0)
{
LM_ERR("unsuccessful sql delete operation");
goto error;
}
LM_DBG("Expires=0, deleted from db %.*s\n",
presentity->user.len,presentity->user.s);
goto send_mxd_notify;
}
if(presentity->event->etag_not_new== 0)
{
if(generate_ETag(p?p->etag_count:0, &etag) < 0)
{
LM_ERR("while generating etag\n");
lock_release(&pres_htable[hash_code].lock);
goto error;
}
cur_etag= etag;
if(p)
{
update_pres_etag(p, &etag);
lock_release(&pres_htable[hash_code].lock);
}
else
{
lock_release(&pres_htable[hash_code].lock);
p = insert_phtable(&pres_uri, presentity->event->evp->parsed,
&presentity->etag, presentity->sphere, 1);
if ( p==NULL )
{
LM_ERR("inserting record in hash table\n");
goto error;
}
}
}
else
{
lock_release(&pres_htable[hash_code].lock);
cur_etag= presentity->etag;
}
n_update_cols= 0;
update_keys[n_update_cols] = &str_etag_col;
update_vals[n_update_cols].type = DB_STR;
update_vals[n_update_cols].nul = 0;
update_vals[n_update_cols].val.str_val = cur_etag;
n_update_cols++;
update_keys[n_update_cols] = &str_expires_col;
update_vals[n_update_cols].type = DB_INT;
update_vals[n_update_cols].nul = 0;
update_vals[n_update_cols].val.int_val= presentity->expires +
(int)time(NULL);
n_update_cols++;
update_keys[n_update_cols] = &str_received_time_col;
update_vals[n_update_cols].type = DB_INT;
update_vals[n_update_cols].nul = 0;
update_vals[n_update_cols].val.int_val= presentity->received_time;
n_update_cols++;
update_keys[n_update_cols] = &str_sender_col;
update_vals[n_update_cols].type = DB_STR;
update_vals[n_update_cols].nul = 0;
if( presentity->sender)
{
update_vals[n_update_cols].val.str_val = *presentity->sender;
}
else
{
update_vals[n_update_cols].val.str_val.s = "";
update_vals[n_update_cols].val.str_val.len = 0;
}
n_update_cols++;
if(extra_hdrs)
{
update_keys[n_update_cols] = &str_extra_hdrs_col;
update_vals[n_update_cols].type = DB_BLOB;
update_vals[n_update_cols].nul = 0;
update_vals[n_update_cols].val.str_val = *extra_hdrs;
n_update_cols++;
}
if(body.s)
{
if (fix_remote_target)
{
if (dialog_fix_remote_target(&body, ¬ify_body)== 0)
{
body.s = notify_body.s;
body.len = notify_body.len;
}
else
{
LM_ERR("Failed to fix remote target\n");
}
}
update_keys[n_update_cols] = &str_body_col;
update_vals[n_update_cols].type = DB_BLOB;
update_vals[n_update_cols].nul = 0;
update_vals[n_update_cols].val.str_val = body;
n_update_cols++;
/* updated stored sphere */
if(sphere_enable &&
presentity->event->evp->parsed== EVENT_PRESENCE)
{
if(update_phtable(presentity, pres_uri, body)< 0)
{
LM_ERR("failed to update sphere for presentity\n");
goto error;
}
}
// CON_PS_REFERENCE(pa_db) = &my_ps_update_body;
}
else
{
// CON_PS_REFERENCE(pa_db) = &my_ps_update_no_body;
}
if (pa_dbf.use_table(pa_db, &presentity_table) < 0)
{
LM_ERR("unsuccessful sql use table\n");
goto error;
}
if( pa_dbf.update( pa_db,query_cols, query_ops, query_vals,
update_keys, update_vals, n_query_cols, n_update_cols )<0)
{
LM_ERR("updating published info in database\n");
goto error;
}
/* send 200OK */
if( publ_send200ok(msg, presentity->expires, cur_etag)< 0)
{
LM_ERR("sending 200OK reply\n");
goto error;
}
*sent_reply= 1;
if(!body.s && !extra_hdrs)
goto done;
}
send_notify:
if (publ_notify(presentity, pres_uri, body.s?&body:0,
NULL, rules_doc, NULL)<0)
{
LM_ERR("while sending Notify requests to watchers\n");
goto error;
}
send_mxd_notify:
/* if event dialog -> send Notify for presence also */
if(mix_dialog_presence && *pres_event_p &&
presentity->event->evp->parsed == EVENT_DIALOG)
{
str* dialog_body= NULL;
LM_DBG("Publish for event dialog - try to send Notify for presence\n");
dialog_body = xml_dialog2presence(&pres_uri, &body);
if(dialog_body)
{
/* send Notify for presence */
presentity->event = *pres_event_p;
if (publ_notify(presentity, pres_uri, 0, NULL, 0, dialog_body)<0)
{
LM_ERR("while sending Notify requests to watchers\n");
if(dialog_body && dialog_body!=FAKED_BODY)
{
xmlFree(dialog_body->s);
pkg_free(dialog_body);
}
goto error;
}
if(dialog_body && dialog_body!=FAKED_BODY)
{
xmlFree(dialog_body->s);
pkg_free(dialog_body);
}
}
}
done:
/* allow next publish to be handled */
if (p)
next_turn_phtable( p, hash_code);
if (notify_body.s)
xmlFree(notify_body.s);
if(rules_doc)
{
if(rules_doc->s)
pkg_free(rules_doc->s);
pkg_free(rules_doc);
}
if(pres_uri.s)
pkg_free(pres_uri.s);
return 0;
error:
/* allow next publish to be handled */
if (p)
next_turn_phtable( p, hash_code);
if(result)
pa_dbf.free_result(pa_db, result);
if(etag.s)
pkg_free(etag.s);
if (notify_body.s)
xmlFree(notify_body.s);
if(rules_doc)
{
if(rules_doc->s)
pkg_free(rules_doc->s);
pkg_free(rules_doc);
}
if(pres_uri.s)
pkg_free(pres_uri.s);
return -1;
}
int pres_htable_restore(void)
{
/* query all records from presentity table and insert records
* in presentity table */
db_key_t result_cols[6];
db_res_t *result= NULL;
db_row_t *rows= NULL ;
db_val_t *row_vals;
int i;
str user, domain, ev_str, uri, body;
int n_result_cols= 0;
int user_col, domain_col, event_col, expires_col, body_col = 0, etag_col;
int event;
event_t ev;
char* sphere= NULL;
int nr_rows;
str etag;
int no_rows = 10;
result_cols[user_col= n_result_cols++]= &str_username_col;
result_cols[domain_col= n_result_cols++]= &str_domain_col;
result_cols[event_col= n_result_cols++]= &str_event_col;
result_cols[expires_col= n_result_cols++]= &str_expires_col;
result_cols[etag_col= n_result_cols++]= &str_etag_col;
if(sphere_enable)
result_cols[body_col= n_result_cols++]= &str_body_col;
if (pa_dbf.use_table(pa_db, &presentity_table) < 0)
{
LM_ERR("unsuccessful use table sql operation\n");
goto error;
}
/* select the whole tabel and all the columns */
if (DB_CAPABILITY(pa_dbf, DB_CAP_FETCH))
{
if(pa_dbf.query(pa_db,0,0,0,result_cols, 0,
n_result_cols, result_cols[user_col], 0) < 0)
{
LM_ERR("Error while querying (fetch) database\n");
return -1;
}
no_rows = estimate_available_rows( 32+32+32+4+64, n_result_cols);
if (no_rows==0) no_rows = 10;
if(pa_dbf.fetch_result(pa_db,&result, no_rows)<0)
{
LM_ERR("fetching rows failed\n");
return -1;
}
} else
{
if (pa_dbf.query (pa_db, 0, 0, 0,result_cols,0, n_result_cols,
result_cols[user_col], &result) < 0)
{
LM_ERR("querying presentity\n");
goto error;
}
}
nr_rows = RES_ROW_N(result);
do {
LM_DBG("loading information from database for %i records\n", nr_rows);
rows = RES_ROWS(result);
/* for every row */
for(i=0; i<nr_rows; i++)
{
row_vals = ROW_VALUES(rows + i);
if (VAL_NULL(row_vals) || VAL_NULL(row_vals+1))
{
LM_ERR("columns %.*s or/and %.*s cannot be null -> skipping\n",
str_username_col.len, str_username_col.s,
str_domain_col.len, str_domain_col.s);
continue;
}
if (VAL_NULL(row_vals+2) || VAL_NULL(row_vals+3))
{
LM_ERR("columns %.*s or/and %.*s cannot be null -> skipping\n",
str_event_col.len, str_event_col.s,
str_domain_col.len, str_domain_col.s);
continue;
}
if(row_vals[expires_col].val.int_val< (int)time(NULL))
continue;
sphere= NULL;
user.s= (char*)row_vals[user_col].val.string_val;
user.len= strlen(user.s);
domain.s= (char*)row_vals[domain_col].val.string_val;
domain.len= strlen(domain.s);
ev_str.s= (char*)row_vals[event_col].val.string_val;
ev_str.len= strlen(ev_str.s);
etag.s= (char*)row_vals[etag_col].val.string_val;
etag.len= strlen(etag.s);
if(event_parser(ev_str.s, ev_str.len, &ev)< 0)
{
LM_ERR("parsing event\n");
free_event_params(ev.params, PKG_MEM_TYPE);
goto error;
}
event= ev.parsed;
free_event_params(ev.params, PKG_MEM_TYPE);
if(uandd_to_uri(user, domain, &uri)< 0)
{
LM_ERR("constructing uri\n");
goto error;
}
/* insert in hash_table*/
if(sphere_enable && event== EVENT_PRESENCE )
{
body.s= (char*)row_vals[body_col].val.string_val;
body.len= strlen(body.s);
sphere= extract_sphere(body);
}
if(insert_phtable(&uri, event, &etag, sphere, 0)== NULL)
{
LM_ERR("inserting record in presentity hash table");
pkg_free(uri.s);
if(sphere)
pkg_free(sphere);
goto error;
}
if(sphere)
pkg_free(sphere);
pkg_free(uri.s);
}