-
Notifications
You must be signed in to change notification settings - Fork 910
/
save.c
1418 lines (1258 loc) · 58.1 KB
/
save.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$
*
* Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
* Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
*
* The initial version of this code was written by Dragos Vingarzan
* (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
* Fruanhofer Institute. It was and still is maintained in a separate
* branch of the original SER. We are therefore migrating it to
* Kamailio/SR and look forward to maintaining it from here on out.
* 2011/2012 Smile Communications, Pty. Ltd.
* ported/maintained/improved by
* Jason Penton (jason(dot)penton(at)smilecoms.com and
* Richard Good (richard(dot)good(at)smilecoms.com) as part of an
* effort to add full IMS support to Kamailio/SR using a new and
* improved architecture
*
* NB: Alot of this code was originally part of OpenIMSCore,
* FhG Fokus.
* Copyright (C) 2004-2006 FhG Fokus
* Thanks for great work! This is an effort to
* break apart the various CSCF functions into logically separate
* components. We hope this will drive wider use. We also feel
* that in this way the architecture is more complete and thereby easier
* to manage in the Kamailio/SR environment
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "../../str.h"
#include "../../socket_info.h"
#include "../../parser/parse_allow.h"
#include "../../parser/parse_methods.h"
#include "../../parser/msg_parser.h"
#include "../../parser/parse_to.h"
#include "../../parser/parse_uri.h"
#include "../../dprint.h"
#include "../../trim.h"
#include "../../ut.h"
#include "../../qvalue.h"
#include "../../dset.h"
#include "../../mod_fix.h"
#include "../../lib/kcore/cmpapi.h"
#include "../../lib/kcore/statistics.h"
#ifdef USE_TCP
#include "../../tcp_server.h"
#endif
#include "../ims_usrloc_scscf/usrloc.h"
#include "common.h"
#include "sip_msg.h"
#include "rerrno.h"
#include "reply.h"
#include "reg_mod.h"
#include "regtime.h"
#include "path.h"
#include "save.h"
#include "config.h"
#include "server_assignment.h"
#include "userdata_parser.h"
#include "../../lib/ims/ims_getters.h"
#include "registrar_notify.h"
#include "cxdx_sar.h"
extern struct tm_binds tmb;
extern int store_data_on_dereg; /**< should we store SAR user data on de-registration */
extern int ue_unsubscribe_on_dereg;
extern int user_data_always;
/* \brief
* Return randomized expires between expires-range% and expires.
* RFC allows only value less or equal to the one provided by UAC.
*/
static inline int randomize_expires(int expires, int range) {
/* if no range is given just return expires */
if (range == 0) return expires;
int range_min = expires - (float) range / 100 * expires;
return range_min + (float) (rand() % 100) / 100 * (expires - range_min);
}
/*! \brief
* Calculate absolute expires value per contact as follows:
* 1) If the contact has expires value, use the value. If it
* is not zero, add actual time to it
* 2) If the contact has no expires parameter, use expires
* header field in the same way
* 3) If the message contained no expires header field, use
* the default value
*/
static inline int calc_contact_expires(contact_t *c, unsigned int expires_hdr, int sos_reg) {
unsigned int r;
if (expires_hdr >= 0)
r = expires_hdr;
else {
r = (sos_reg > 0) ? default_registrar_cfg.em_default_expires : default_registrar_cfg.default_expires;
goto end;
}
if (c && c->expires)
str2int(&(c->expires->body), (unsigned int*) &r);
if (r > 0) {
if (!sos_reg && r < default_registrar_cfg.min_expires) {
r = default_registrar_cfg.min_expires;
goto end;
}
if (sos_reg && r < default_registrar_cfg.em_min_expires) {
r = default_registrar_cfg.em_min_expires;
goto end;
}
}
if (!sos_reg && r > default_registrar_cfg.max_expires) {
r = default_registrar_cfg.max_expires;
goto end;
}
if (sos_reg && r > default_registrar_cfg.em_max_expires)
r = default_registrar_cfg.em_min_expires;
end:
r = randomize_expires(r, default_registrar_cfg.default_expires_range);
LM_DBG("Calculated expires for contact is %d\n", r);
return time(NULL) + r;
}
/*! \brief
* Process request that contained a star, in that case,
* we will remove all bindings with the given impu
* from the usrloc and return 200 OK response
*/
static inline int star(udomain_t* _d, str* _a) {
impurecord_t* r;
ul.lock_udomain(_d, _a);
if (ul.delete_impurecord(_d, _a, 0) != 0) {
LM_ERR("failed to remove record from usrloc\n");
/* Delete failed, try to get corresponding
* record structure and send back all existing
* contacts
*/
rerrno = R_UL_DEL_R;
if (ul.get_impurecord(_d, _a, &r) == 0) {
contact_for_header_t** contact_header = 0;
build_contact(r, contact_header);
free_contact_buf(*contact_header);
ul.unlock_udomain(_d, _a);
}
return -1;
}
ul.unlock_udomain(_d, _a);
return 0;
}
/*! \brief
*/
static struct socket_info *get_sock_hdr(struct sip_msg *msg) {
struct socket_info *sock;
struct hdr_field *hf;
str socks;
str hosts;
int port;
int proto;
char c;
if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
LM_ERR("failed to parse message\n");
return 0;
}
for (hf = msg->headers; hf; hf = hf->next) {
if (cmp_hdrname_str(&hf->name, &sock_hdr_name) == 0)
break;
}
/* hdr found? */
if (hf == 0)
return 0;
trim_len(socks.len, socks.s, hf->body);
if (socks.len == 0)
return 0;
/*FIXME: This is a hack */
c = socks.s[socks.len];
socks.s[socks.len] = '\0';
if (parse_phostport(socks.s, &hosts.s, &hosts.len, &port, &proto) != 0) {
socks.s[socks.len] = c;
LM_ERR("bad socket <%.*s> in \n",
socks.len, socks.s);
return 0;
}
socks.s[socks.len] = c;
sock = grep_sock_info(&hosts, (unsigned short) port,
(unsigned short) proto);
if (sock == 0) {
LM_ERR("non-local socket <%.*s>\n", socks.len, socks.s);
return 0;
}
LM_DBG("%d:<%.*s>:%d -> p=%p\n", proto, socks.len, socks.s, port_no, sock);
return sock;
}
/*! \brief
* Fills the common part (for all contacts) of the info structure
*/
static inline ucontact_info_t* pack_ci(struct sip_msg* _m, contact_t* _c, unsigned int _e, unsigned int _f) {
static ucontact_info_t ci;
static str no_ua = str_init("n/a");
static str callid;
static str path_received = {0, 0};
static str path;
static str received = {0, 0};
static int received_found;
static unsigned int allowed, allow_parsed;
static struct sip_msg *m = 0;
int_str val;
if (_m != 0) {
memset(&ci, 0, sizeof (ucontact_info_t));
/* Get callid of the message */
callid = _m->callid->body;
trim_trailing(&callid);
if (callid.len > CALLID_MAX_SIZE) {
rerrno = R_CALLID_LEN;
LM_ERR("callid too long\n");
goto error;
}
ci.callid = &callid;
/* Get CSeq number of the message */
if (str2int(&get_cseq(_m)->number, (unsigned int*) &ci.cseq) < 0) {
rerrno = R_INV_CSEQ;
LM_ERR("failed to convert cseq number\n");
goto error;
}
/* set received socket */
if (_m->flags & sock_flag) {
ci.sock = get_sock_hdr(_m);
if (ci.sock == 0)
ci.sock = _m->rcv.bind_address;
} else {
ci.sock = _m->rcv.bind_address;
}
/* additional info from message */
if (parse_headers(_m, HDR_USERAGENT_F, 0) != -1 && _m->user_agent
&& _m->user_agent->body.len > 0 && _m->user_agent->body.len < MAX_UA_SIZE) {
ci.user_agent = &_m->user_agent->body;
} else {
ci.user_agent = &no_ua;
}
/* extract Path headers */
if (path_enabled) {
if (build_path_vector(_m, &path, &path_received) < 0) {
rerrno = R_PARSE_PATH;
goto error;
}
if (path.len && path.s) {
ci.path = &path;
if (path_mode != PATH_MODE_OFF) {
/* save in msg too for reply */
if (set_path_vector(_m, &path) < 0) {
rerrno = R_PARSE_PATH;
goto error;
}
}
}
}
if (_c->params) {
ci.params = _c->params;
}
/* set flags */
ci.flags = _f;
getbflagsval(0, &ci.cflags);
/* get received */
if (path_received.len && path_received.s) {
ci.cflags |= ul.nat_flag;
ci.received = path_received;
}
allow_parsed = 0; /* not parsed yet */
received_found = 0; /* not found yet */
m = _m; /* remember the message */
}
if (_c != 0) {
/* Calculate q value of the contact */
if (calc_contact_q(_c->q, &ci.q) < 0) {
rerrno = R_INV_Q;
LM_ERR("failed to calculate q\n");
goto error;
}
/* set expire time */
ci.expires = _e;
/* Get methods of contact */
if (_c->methods) {
if (parse_methods(&(_c->methods->body), &ci.methods) < 0) {
rerrno = R_PARSE;
LM_ERR("failed to parse contact methods\n");
goto error;
}
} else {
/* check on Allow hdr */
if (allow_parsed == 0) {
if (m && parse_allow(m) != -1) {
allowed = get_allow_methods(m);
} else {
allowed = ALL_METHODS;
}
allow_parsed = 1;
}
ci.methods = allowed;
}
/* get received */
if (ci.received.len == 0) {
if (_c->received) {
ci.received = _c->received->body;
} else {
if (received_found == 0) {
memset(&val, 0, sizeof (int_str));
if (rcv_avp_name.n != 0
&& search_first_avp(rcv_avp_type, rcv_avp_name,
&val, 0) && val.s.len > 0) {
if (val.s.len > RECEIVED_MAX_SIZE) {
rerrno = R_CONTACT_LEN;
LM_ERR("received too long\n");
goto error;
}
received = val.s;
} else {
received.s = 0;
received.len = 0;
}
received_found = 1;
}
ci.received = received;
}
}
}
return &ci;
error:
return 0;
}
/**
* Deallocates memory used by a subscription.
* \note Must be called with the lock got to avoid races
* @param s - the ims_subscription to free
*/
void free_ims_subscription_data(ims_subscription *s) {
int i, j, k;
if (!s)
return;
/* lock_get(s->lock); - must be called with the lock got */
for (i = 0; i < s->service_profiles_cnt; i++) {
for (j = 0; j < s->service_profiles[i].public_identities_cnt; j++) {
if (s->service_profiles[i].public_identities[j].public_identity.s)
shm_free(
s->service_profiles[i].public_identities[j].public_identity.s);
if (s->service_profiles[i].public_identities[j].wildcarded_psi.s)
shm_free(
s->service_profiles[i].public_identities[j].wildcarded_psi.s);
}
if (s->service_profiles[i].public_identities)
shm_free(s->service_profiles[i].public_identities);
for (j = 0; j < s->service_profiles[i].filter_criteria_cnt; j++) {
if (s->service_profiles[i].filter_criteria[j].trigger_point) {
for (k = 0;
k
< s->service_profiles[i].filter_criteria[j].trigger_point->spt_cnt;
k++) {
switch (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].type) {
case IFC_REQUEST_URI:
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].request_uri.s)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].request_uri.s);
break;
case IFC_METHOD:
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].method.s)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].method.s);
break;
case IFC_SIP_HEADER:
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].sip_header.header.s)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].sip_header.header.s);
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].sip_header.content.s)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].sip_header.content.s);
break;
case IFC_SESSION_CASE:
break;
case IFC_SESSION_DESC:
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].session_desc.line.s)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].session_desc.line.s);
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].session_desc.content.s)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt[k].session_desc.content.s);
break;
}
}
if (s->service_profiles[i].filter_criteria[j].trigger_point->spt)
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point->spt);
shm_free(
s->service_profiles[i].filter_criteria[j].trigger_point);
}
if (s->service_profiles[i].filter_criteria[j].application_server.server_name.s)
shm_free(
s->service_profiles[i].filter_criteria[j].application_server.server_name.s);
if (s->service_profiles[i].filter_criteria[j].application_server.service_info.s)
shm_free(
s->service_profiles[i].filter_criteria[j].application_server.service_info.s);
if (s->service_profiles[i].filter_criteria[j].profile_part_indicator)
shm_free(
s->service_profiles[i].filter_criteria[j].profile_part_indicator);
}
if (s->service_profiles[i].filter_criteria)
shm_free(s->service_profiles[i].filter_criteria);
if (s->service_profiles[i].cn_service_auth)
shm_free(s->service_profiles[i].cn_service_auth);
if (s->service_profiles[i].shared_ifc_set)
shm_free(s->service_profiles[i].shared_ifc_set);
}
if (s->service_profiles)
shm_free(s->service_profiles);
if (s->private_identity.s)
shm_free(s->private_identity.s);
ul.unlock_subscription(s);
#ifdef EXTRA_DEBUG
LM_DBG("SUBSCRIPTION LOCK %p destroyed\n", s->lock);
#endif
lock_destroy(s->lock);
lock_dealloc(s->lock);
shm_free(s);
}
/** Check if an impu record exists.
* 1. must be in registered state (impurecord)
* 2. must have at least one valid contact
*/
static inline int is_impu_registered(udomain_t* _d, str* public_identity) {
int res, ret = 1;
impurecord_t* impu;
ul.lock_udomain(_d, public_identity);
res = ul.get_impurecord(_d, public_identity, &impu);
if (res != 0) {
ul.unlock_udomain(_d, public_identity);
return 0;
} else {
//check reg status
if (impu->reg_state != IMPU_REGISTERED) {
LM_DBG("IMPU <%.*s> is not currently registered\n", public_identity->len, public_identity->s);
ret = 0;
}
//check valid contacts
if ((impu->num_contacts <= 0) || (impu->newcontacts[0] == 0)) {
LM_DBG("IMPU <%.*s> has no valid contacts\n", public_identity->len, public_identity->s);
ret = 0;
}
ul.unlock_udomain(_d, public_identity);
}
return ret;
}
struct sip_msg* get_request_from_reply(struct sip_msg *reply) {
struct cell *t;
t = tmb.t_gett();
if (!t || t == (void*) - 1) {
LM_ERR("get_request_from_reply: Reply without transaction\n");
return 0;
}
if (t)
return t->uas.request;
else
return 0;
}
/**
* update the contacts for a public identity. Make sure you have the lock on the domain before calling this
* returns 0 on success, -1 on failure
*/
static inline int update_contacts_helper(struct sip_msg* msg, impurecord_t* impu_rec, int assignment_type, unsigned int expires_hdr) {
struct hdr_field* h;
contact_t* chi; //contact header information
ucontact_info_t* ci; //ucontact info
qvalue_t qvalue;
int sos = 0, expires;
struct ucontact* ucontact;
int result, sl;
LM_DBG("updating the contacts for IMPU <%.*s>\n", impu_rec->public_identity.len, impu_rec->public_identity.s);
switch (assignment_type) {
case AVP_IMS_SAR_USER_DEREGISTRATION:
LM_DBG("update_contacts_helper: doing de-reg\n");
break;
case AVP_IMS_SAR_REGISTRATION:
case AVP_IMS_SAR_RE_REGISTRATION:
for (h = msg->contact; h; h = h->next) {
if (h->type == HDR_CONTACT_T && h->parsed) {
for (chi = ((contact_body_t*) h->parsed)->contacts; chi; chi =
chi->next) {
if (calc_contact_q(chi->q, &qvalue) != 0) {
LM_ERR("error on <%.*s>\n", chi->uri.len, chi->uri.s);
goto error;
}
sos = cscf_get_sos_uri_param(chi->uri);
if (sos < 0) {
LM_ERR("Error trying to determine if this is a sos contact <%.*s>\n", chi->uri.len, chi->uri.s);
goto error;
}
expires = calc_contact_expires(chi, expires_hdr, sos);
//TODO: this next line will fail if the expires is in the main body and not the contact body //FIXED
LM_DBG("Need to update contact: <%.*s>: "
"q_value [%d],"
"sos: [%d],"
"expires [%ld]\n", chi->uri.len, chi->uri.s, qvalue, sos, expires - time(NULL));
LM_DBG("packing contact information\n");
if ((ci = pack_ci(msg, chi, expires, 0)) == 0) {
LM_ERR("Failed to extract contact info\n");
goto error;
}
LM_DBG("adding/updating contact based on prior existence\n");
//stick the contacts into usrloc
//ul.lock_contact_slot(&chi->uri);
result = ul.get_ucontact(&chi->uri, ci->callid,
ci->path, ci->cseq, &ucontact);
if (result != 0) { //get_contact returns with lock
LM_DBG("inserting new contact\n");
if (ul.insert_ucontact(impu_rec, &chi->uri, ci,
&ucontact) != 0) {
LM_ERR("Error inserting contact <%.*s>\n", chi->uri.len, chi->uri.s);
// ul.unlock_contact_slot(&chi->uri);
goto error;
}
} else {
LM_DBG("Contact already exists - updating - it's currently in state [%d]\n", ucontact->state);
sl = ucontact->sl;
ul.lock_contact_slot_i(sl);
if (ucontact->state != CONTACT_VALID) {
LM_WARN("contact is not in state valid - this is a race between dereg and reg/re-reg");
ucontact->state = CONTACT_VALID; //TODO this should prob move into the contact info structure - ie pass state into update
}
if (ul.update_ucontact(impu_rec, ucontact, ci) != 0) {
LM_ERR("Error updating contact <%.*s>\n", chi->uri.len, chi->uri.s);
ul.unlock_contact_slot_i(sl);
ul.release_ucontact(ucontact);
// ul.unlock_contact_slot(&chi->uri);
goto error;
}
ul.unlock_contact_slot_i(sl);
ul.release_ucontact(ucontact);
}
// ul.unlock_contact_slot(&chi->uri);
}
}
}
break;
}
return 0;
error:
return -1;
}
/*NB remember to lock udomain prior to calling this*/
static inline int unregister_contact(contact_t* chi, contact_state_t state) {
struct ucontact* ucontact;
str callid = {0, 0};
str path = {0, 0};
// if (_impu_rec) {
// LM_DBG("already have impurecord....\n");
// impu_rec = _impu_rec;
// } else {
// if (ul.get_impurecord(_d, public_identity, &impu_rec) != 0) {
// LM_ERR("Error, no public identity exists for <%.*s>\n", public_identity->len, public_identity->s);
// goto error;
// }
// }
if (ul.get_ucontact(&chi->uri, &callid, &path, 0/*cseq*/, &ucontact) != 0) {
LM_ERR("Can't unregister contact that does not exist <%.*s>\n", chi->uri.len, chi->uri.s);
// ul.unlock_udomain(_d, public_identity);
goto error;
}
get_act_time();
if (ucontact->state == CONTACT_DELETED) {
LM_DBG("Contact is not valid (expired/deleted).... ignoring\n");
ul.release_ucontact(ucontact);
return 0;
}
//Richard added this - fix to remove subscribes that have presentity and watcher uri same as a contact aor that is being removed
//When UEs explicitly dereg - they don't unsubscribe, so we remove subscriptions for them
//only do this if ue_unsubscribe_on_dereg is set to 0
// if (!ue_unsubscribe_on_dereg) {
// s = impu_rec->shead;
// LM_DBG("Checking if there is a subscription to this IMPU that has same watcher contact as this contact");
// while (s) {
//
// LM_DBG("Subscription for this impurecord: watcher uri [%.*s] presentity uri [%.*s] watcher contact [%.*s] ", s->watcher_uri.len, s->watcher_uri.s,
// s->presentity_uri.len, s->presentity_uri.s, s->watcher_contact.len, s->watcher_contact.s);
// LM_DBG("Contact to be removed [%.*s] ", ucontact->c.len, ucontact->c.s);
// if (contact_port_ip_match(&s->watcher_contact, &ucontact->c)) {
// //if ((s->watcher_contact.len == ucontact->c.len) && (strncasecmp(s->watcher_contact.s, ucontact->c.s, ucontact->c.len) == 0)) {
// LM_DBG("This contact has a subscription to its own status - so going to delete the subscription");
// ul.external_delete_subscriber(s, _d, 0 /*domain is locked*/);
// }
// s = s->next;
// }
// }
// if (ul.delete_ucontact(impu_rec, ucontact) != 0) {
ul.lock_contact_slot_i(ucontact->sl);
ucontact->state = state;
// notify_subscribers(impu_rec);
// ucontact->state = CONTACT_DELETED;
// if (ul.unlink_contact_from_impu(impu_rec, ucontact, 1, 1/*explicit dereg of contact*/) != 0) {
// LM_ERR("Failed to delete ucontact <%.*s>\n", chi->uri.len, chi->uri.s);
// }
ul.unlock_contact_slot_i(ucontact->sl);
ul.release_ucontact(ucontact);
// LM_DBG("Contact unlinked successfully <%.*s>\n", chi->uri.len, chi->uri.s);
// ul.unlock_udomain(_d, public_identity);
return 0;
error:
return -1;
}
/**
* Get the number of valid contacts for an impu. Ie contacts not expired and not in deleted or delete_pending state
* @param impu
* @return
*/
int get_number_of_valid_contacts(impurecord_t* impu) {
int i;
int ret = 0;
get_act_time();
for (i = 0; i < impu->num_contacts; i++) {
if (VALID_CONTACT(impu->newcontacts[i], act_time)) {
ret++;
}
}
return ret;
}
/**
*
* @param msg
* @param _d
* @param public_identity
* @param assignment_type
* @param s
* @param ccf1
* @param ccf2
* @param ecf1
* @param ecf2
* @param contact_header
* @return 1 - success(contacts left) - unregistered contacts and remaining contacts in contact buffer for reply message
* 2 - success(no contacts left)
* <=0 - on failure
*/
int update_contacts(struct sip_msg* msg, udomain_t* _d,
str* public_identity, int assignment_type, ims_subscription** s,
str* ccf1, str* ccf2, str* ecf1, str* ecf2, contact_for_header_t** contact_header) {
int reg_state, i, j;
ims_public_identity* pi = 0;
impurecord_t* impu_rec, *tmp_impu_rec;
int expires_hdr = -1; //by default registration doesn't expire
struct hdr_field* h;
contact_t* chi; //contact header information
qvalue_t qvalue;
int sos = 0;
ims_subscription* subscription = 0;
int first_unbarred_impu = 1; //this is used to flag the IMPU as anchor for implicit set
int is_primary_impu = 0;
int ret = 1;
if (msg) {
expires_hdr = cscf_get_expires_hdr(msg, 0); //get the expires from the main body of the sip message (global)
}
switch (assignment_type) {
case AVP_IMS_SAR_REGISTRATION:
LM_DBG("updating contacts in REGISTRATION state\n");
reg_state = IMS_USER_REGISTERED;
if (!s) {
LM_ERR("no userdata supplied for AVP_IMS_SAR_REGISTRATION\n");
goto error;
}
for (i = 0; i < (*s)->service_profiles_cnt; i++) {
for (j = 0; j < (*s)->service_profiles[i].public_identities_cnt; j++) {
pi = &((*s)->service_profiles[i].public_identities[j]);
ul.lock_udomain(_d, &pi->public_identity);
if (first_unbarred_impu && !pi->barring) {
is_primary_impu = 1;
first_unbarred_impu = 0;
} else {
is_primary_impu = 0;
}
if (ul.update_impurecord(_d, &pi->public_identity, 0, reg_state, -1 /*do not change send sar on delete */,
pi->barring, is_primary_impu, s, ccf1, ccf2, ecf1, ecf2, &impu_rec) != 0) {
LM_ERR("Unable to update impurecord for <%.*s>\n", pi->public_identity.len, pi->public_identity.s);
ul.unlock_udomain(_d, &pi->public_identity);
goto error;
}
//here we can do something with impu_rec if we want but we must unlock when done
//lets update the contacts
if (update_contacts_helper(msg, impu_rec, assignment_type, expires_hdr) != 0) {
LM_ERR("Failed trying to update contacts\n");
ul.unlock_udomain(_d, &pi->public_identity);
goto error;
}
ul.unlock_udomain(_d, &pi->public_identity);
}
}
//if we were successful up to this point, then we need to copy the contacts from main impu record (asserted IMPU) into the register response
ul.lock_udomain(_d, public_identity);
if (ul.get_impurecord(_d, public_identity, &impu_rec) != 0) {
LM_ERR("Error, we should have a record after registraion\n");
ul.unlock_udomain(_d, public_identity);
goto error;
}
//now build the contact buffer to be include in the reply message and unlock
build_contact(impu_rec, contact_header);
notify_subscribers(impu_rec);
ul.unlock_udomain(_d, public_identity);
break;
case AVP_IMS_SAR_RE_REGISTRATION:
/* first update all the implicit IMPU based on the existing IMPUs subscription
* then, once done with the implicits, update the explicit with the new subscription data
*/
LM_DBG("updating contacts in RE-REGISTRATION state\n");
reg_state = IMS_USER_REGISTERED;
ul.lock_udomain(_d, public_identity);
if (ul.get_impurecord(_d, public_identity, &impu_rec) != 0) {
LM_ERR("No IMPU record fond for re-registration...aborting\n");
ul.unlock_udomain(_d, public_identity);
goto error;
}
if (update_contacts_helper(msg, impu_rec, assignment_type, expires_hdr) != 0) { //update the contacts for the explicit IMPU
LM_ERR("Failed trying to update contacts for re-registration\n");
ul.unlock_udomain(_d, public_identity);
goto error;
}
//build the contact buffer for all registered contacts on explicit IMPU
build_contact(impu_rec, contact_header);
subscription = impu_rec->s;
if (!subscription) {
LM_ERR("No subscriber info associated with <%.*s>, not doing any implicit re-registrations\n", impu_rec->public_identity.len, impu_rec->public_identity.s);
//update the new subscription infor for the explicit IMPU
if (ul.update_impurecord(_d, public_identity, 0, reg_state, -1 /*do not change send sar on delete */, 0 /*this is explicit so barring must be 0*/, 0, s, ccf1, ccf2,
ecf1, ecf2, &impu_rec) != 0) {
LM_ERR("Unable to update explicit impurecord for <%.*s>\n", public_identity->len, public_identity->s);
}
build_contact(impu_rec, contact_header);
ul.unlock_udomain(_d, public_identity);
break;
}
ul.lock_subscription(subscription);
subscription->ref_count++;
LM_DBG("ref count after add is now %d\n", subscription->ref_count);
ul.unlock_subscription(subscription);
ul.unlock_udomain(_d, public_identity);
//now update the implicit set
for (i = 0; i < subscription->service_profiles_cnt; i++) {
for (j = 0; j < subscription->service_profiles[i].public_identities_cnt; j++) {
pi = &((*s)->service_profiles[i].public_identities[j]);
if (memcmp(public_identity->s, pi->public_identity.s, public_identity->len) == 0) { //we don't need to update the explicit IMPU
LM_DBG("Ignoring explicit identity <%.*s>, updating later.....\n", public_identity->len, public_identity->s);
continue;
}
//update the implicit IMPU with the new data
if (ul.update_impurecord(_d, &pi->public_identity, 0,
reg_state, -1 /*do not change send sar on delete */, pi->barring, 0, s, ccf1, ccf2, ecf1, ecf2,
&impu_rec) != 0) {
LM_ERR("Unable to update implicit impurecord for <%.*s>.... continuing\n", pi->public_identity.len, pi->public_identity.s);
continue;
}
//update the contacts for the explicit IMPU
if (update_contacts_helper(msg, impu_rec, assignment_type, expires_hdr) != 0) {
LM_ERR("Failed trying to update contacts for re-registration of implicit IMPU <%.*s>.......continuing\n", pi->public_identity.len, pi->public_identity.s);
continue;
}
}
}
ul.lock_subscription(subscription);
subscription->ref_count--;
LM_DBG("ref count after sub is now %d\n", subscription->ref_count);
ul.unlock_subscription(subscription);
//finally we update the explicit IMPU record with the new data
if (ul.update_impurecord(_d, public_identity, 0, reg_state, -1 /*do not change send sar on delete */, 0 /*this is explicit so barring must be 0*/, 0, s, ccf1, ccf2, ecf1, ecf2, &impu_rec) != 0) {
LM_ERR("Unable to update explicit impurecord for <%.*s>\n", public_identity->len, public_identity->s);
}
notify_subscribers(impu_rec);
break;
case AVP_IMS_SAR_USER_DEREGISTRATION:
/*TODO: if its not a star lets find all the contact records and remove them*/
//first we update the state of the contact/s
for (h = msg->contact; h; h = h->next) {
if (h->type == HDR_CONTACT_T && h->parsed) {
for (chi = ((contact_body_t*) h->parsed)->contacts; chi; chi = chi->next) {
if (calc_contact_q(chi->q, &qvalue) != 0) {
LM_ERR("error on <%.*s>\n", chi->uri.len, chi->uri.s);
goto error;
}
sos = cscf_get_sos_uri_param(chi->uri);
if (sos < 0) {
LM_ERR("Error trying to determine if this is a sos contact <%.*s>\n", chi->uri.len, chi->uri.s);
goto error;
}
calc_contact_expires(chi, expires_hdr, sos);
if (unregister_contact(chi, CONTACT_DELETE_PENDING) != 0) {
LM_ERR("Unable to remove contact <%.*s\n", chi->uri.len, chi->uri.s);
}
//add this contact to the successful unregistered in the 200OK so the PCSCF can also see what is de-registered
build_expired_contact(chi, contact_header);
}
}
}
//now, we get the subscription
ul.lock_udomain(_d, public_identity);
if (ul.get_impurecord(_d, public_identity, &impu_rec) != 0) {
LM_ERR("Error retrieving impu record on explicit de-reg nothing we can do from here on... aborting..\n");
ul.unlock_udomain(_d, public_identity);
goto error;
}
int num_contacts = get_number_of_valid_contacts(impu_rec);
if (num_contacts > 0) {
LM_DBG("contacts still available\n");
//TODO: add all other remaining contacts to reply message (contacts still registered for this IMPU)
ret = 1;
} else {
LM_DBG("no more contacts available\n");
ret = 2;
}
ims_subscription* subscription = impu_rec->s;
if (!subscription) {
LM_WARN("subscription is null..... continuing without de-registering implicit set\n");
} else {
ul.lock_subscription(subscription);
subscription->ref_count++; //this is so we can de-reg the implicit set just now without holding the lock on the current IMPU
ul.unlock_subscription(subscription);
}
ul.unlock_udomain(_d, public_identity);
if (subscription) {
for (i = 0; i < subscription->service_profiles_cnt; i++) {
for (j = 0; j < subscription->service_profiles[i].public_identities_cnt; j++) {
pi = &(subscription->service_profiles[i].public_identities[j]);
// if (memcmp(public_identity->s, pi->public_identity.s, public_identity->len) == 0) { //we don't need to update the explicit IMPU
// LM_DBG("Ignoring explicit identity <%.*s>, already de-reg/updated\n", public_identity->len, public_identity->s);
// continue;
// }
ul.lock_udomain(_d, &pi->public_identity);
if (ul.get_impurecord(_d, &pi->public_identity, &tmp_impu_rec) != 0) {
LM_ERR("Can't find IMPU for implicit de-registration update....continuing\n");
ul.unlock_udomain(_d, &pi->public_identity);
continue;
}
LM_DBG("Implicit deregistration of IMPU <%.*s>\n", pi->public_identity.len, pi->public_identity.s);
//TODO_LATEST: need to add back the following functionality
// if (!ue_unsubscribe_on_dereg) {
// subscriber = tmp_impu_rec->shead;
// LM_DBG("Checking if there is a subscription to this IMPU that has same watcher contact as this contact");
// while (s) {
//
// LM_DBG("Subscription for this impurecord: watcher uri [%.*s] presentity uri [%.*s] watcher contact [%.*s] ", subscriber->watcher_uri.len, subscriber->watcher_uri.s,
// subscriber->presentity_uri.len, subscriber->presentity_uri.s, subscriber->watcher_contact.len, subscriber->watcher_contact.s);
// LM_DBG("Contact to be removed [%.*s] ", ucontact->c.len, ucontact->c.s);
// if (contact_port_ip_match(&subscriber->watcher_contact, &ucontact->c)) {
// //if ((s->watcher_contact.len == ucontact->c.len) && (strncasecmp(s->watcher_contact.s, ucontact->c.s, ucontact->c.len) == 0)) {
// LM_DBG("This contact has a subscription to its own status - so going to delete the subscription");
// ul.external_delete_subscriber(subscriber, _d, 0 /*domain is locked*/);
// }
// s = s->next;
// }
// }
notify_subscribers(tmp_impu_rec);
for (h = msg->contact; h; h = h->next) {
if (h->type == HDR_CONTACT_T && h->parsed) {
for (chi = ((contact_body_t*) h->parsed)->contacts; chi; chi = chi->next) {
if (calc_contact_q(chi->q, &qvalue) != 0) {
LM_ERR("error on <%.*s>\n", chi->uri.len, chi->uri.s);
ul.unlock_udomain(_d, &pi->public_identity);
LM_ERR("no q value of implicit de-reg....continuing\n");
continue;
}
sos = cscf_get_sos_uri_param(chi->uri);
if (sos < 0) {
LM_ERR("Error trying to determine if this is a sos contact <%.*s>\n", chi->uri.len, chi->uri.s);
ul.unlock_udomain(_d, public_identity);
goto error;
}
calc_contact_expires(chi, expires_hdr, sos);
if (unregister_contact(chi, CONTACT_DELETED) != 0) {
LM_ERR("Unable to remove contact <%.*s>\n", chi->uri.len, chi->uri.s);
}
}
}
}
/*now lets see if we still have any contacts left to decide on return value*/
int num_valid_contacts = get_number_of_valid_contacts(tmp_impu_rec);
if (num_valid_contacts)
LM_DBG("contacts still available after implicit dereg for IMPU: <%.*s>\n", pi->public_identity.len, pi->public_identity.s);
else {
LM_DBG("no contacts left after implicit dereg for IMPU: <%.*s>\n", pi->public_identity.len, pi->public_identity.s);
LM_DBG("Updating impu record to not send SAR on delete as this is explicit dereg");
reg_state = IMS_USER_REGISTERED; //keep reg_state as it is
if (ul.update_impurecord(_d, 0/*&pi->public_identity*/, tmp_impu_rec, reg_state, 0 /*do not send sar on delete */, -1 /*do not change barring*/, 0, 0, 0, 0, 0, 0, &tmp_impu_rec) != 0) {
LM_ERR("Unable to update explicit impurecord for <%.*s>\n", pi->public_identity.len, pi->public_identity.s);
}
}
ul.unlock_udomain(_d, &pi->public_identity);
}
}
ul.lock_subscription(subscription);
subscription->ref_count--;
ul.unlock_subscription(subscription);
}
//TODO: clean here too - maybe do earlier with the lock on the domain already held...
if (ret == 2) {
LM_DBG("no contacts left after explicit dereg for IMPU: <%.*s>\n", public_identity->len, public_identity->s);
LM_DBG("Updating impu record to not send SAR on delete as this is explicit dereg");
reg_state = IMS_USER_REGISTERED; //keep reg_state as it is
ul.lock_udomain(_d, public_identity);