/
ims_getters.c
1858 lines (1700 loc) · 43 KB
/
ims_getters.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
/*
* 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 "../../core/parser/msg_parser.h"
#include "../../core/parser/digest/digest.h"
#include "../../core/parser/parse_to.h"
#include "../../core/parser/parse_expires.h"
#include "../../core/parser/contact/parse_contact.h"
#include "../../core/parser/parse_uri.h"
#include "../../core/parser/parse_rr.h"
#include "../../core/parser/parse_nameaddr.h"
#include "../../core/data_lump.h"
#include "../../core/data_lump_rpl.h"
#include "../../core/parser/parse_from.h"
#include "../../core/parser/parse_content.h"
#include "ims_getters.h"
#include "../../core/parser/parse_ppi_pai.h"
/**
* Delete parameters and stuff from uri.
* @param uri - the string to operate on
*/
static inline void cscf_strip_uri(str *uri)
{
int i;
/* Strip the ending */
i = 0;
while(i < uri->len && uri->s[i] != '@')
i++;
while(i < uri->len && uri->s[i] != ':' && uri->s[i] != '/'
&& uri->s[i] != '&')
i++;
uri->len = i;
}
/**
* Parses all the contact headers.
* @param msg - the SIP message
* @returns the first contact_body
*/
contact_body_t *cscf_parse_contacts(struct sip_msg *msg)
{
struct hdr_field *ptr;
if(!msg)
return 0;
if(parse_headers(msg, HDR_EOH_F, 0) < 0) {
LM_ERR("Error parsing headers \n");
return 0;
}
if(msg->contact) {
ptr = msg->contact;
while(ptr) {
if(ptr->type == HDR_CONTACT_T) {
if(ptr->parsed == 0) {
if(parse_contact(ptr) < 0) {
LM_DBG("error parsing contacts [%.*s]\n", ptr->body.len,
ptr->body.s);
}
}
}
ptr = ptr->next;
}
}
if(!msg->contact)
return 0;
return msg->contact->parsed;
}
/**
* Returns the Private Identity extracted from the Authorization header.
* If none found there takes the SIP URI in To without the "sip:" prefix
* \todo - remove the fallback case to the To header
* @param msg - the SIP message
* @param realm - the realm to match in an Authorization header
* @returns the str containing the private id, no mem dup
*/
str cscf_get_private_identity(struct sip_msg *msg, str realm)
{
str pi = {0, 0};
struct hdr_field *h = 0;
int ret, i, res;
if((parse_headers(msg, HDR_AUTHORIZATION_F, 0) != 0)
&& (parse_headers(msg, HDR_PROXYAUTH_F, 0) != 0)) {
return pi;
}
if(!msg->authorization) {
goto fallback;
}
h = msg->authorization;
if(realm.len && realm.s) {
ret = find_credentials(msg, &realm, HDR_AUTHORIZATION_T, &h);
if(ret < 0) {
ret = find_credentials(msg, &realm, HDR_PROXYAUTH_T, &h);
if(ret < 0) {
goto fallback;
} else {
if(ret > 0) {
goto fallback;
}
h = msg->proxy_auth;
}
} else {
if(ret > 0) {
goto fallback;
}
}
}
if(!h)
goto fallback;
res = parse_credentials(h);
if(res != 0) {
LOG(L_ERR, "Error while parsing credentials\n");
return pi;
}
pi = ((auth_body_t *)h->parsed)->digest.username.whole;
if(memchr(pi.s, '@', pi.len) == 0) {
LM_DBG("no domain in username - required for IMPI - falling back "
"to IMPU\n");
goto fallback;
}
goto done;
fallback:
pi = cscf_get_public_identity(msg);
if(pi.len > 4 && strncasecmp(pi.s, "sip:", 4) == 0) {
pi.s += 4;
pi.len -= 4;
}
for(i = 0; i < pi.len; i++)
if(pi.s[i] == ';') {
pi.len = i;
break;
}
done:
return pi;
}
/**
* Returns the Private Identity extracted from the Authorization header.
* If none found there takes the SIP URI in To without the "sip:" prefix
* \todo - remove the fallback case to the To header
* @param msg - the SIP message
* @param realm - the realm to match in an Authorization header
* @returns the str containing the private id, no mem dup
*/
str cscf_get_private_identity_from(struct sip_msg *msg, str realm)
{
str pi = {0, 0};
struct hdr_field *h = 0;
int ret, i, res;
if(parse_headers(msg, HDR_AUTHORIZATION_F, 0) != 0) {
return pi;
}
if(!msg->authorization) {
goto fallback;
}
h = msg->authorization;
if(realm.len && realm.s) {
ret = find_credentials(msg, &realm, HDR_AUTHORIZATION_T, &h);
if(ret < 0) {
goto fallback;
} else if(ret > 0) {
goto fallback;
}
}
res = parse_credentials(h);
if(res != 0) {
LOG(L_ERR, "Error while parsing credentials\n");
return pi;
}
pi = ((auth_body_t *)h->parsed)->digest.username.whole;
goto done;
fallback:
pi = cscf_get_public_identity_from(msg);
if(pi.len > 4 && strncasecmp(pi.s, "sip:", 4) == 0) {
pi.s += 4;
pi.len -= 4;
}
for(i = 0; i < pi.len; i++)
if(pi.s[i] == ';') {
pi.len = i;
break;
}
done:
return pi;
}
/**
* Returns the Private Identity extracted from the Authorization header.
* If none found there takes the SIP URI in To without the "sip:" prefix
* \todo - remove the fallback case to the To header
* @param msg - the SIP message
* @param realm - the realm to match in an Authorization header
* @returns the str containing the private id, no mem dup
*/
str cscf_get_private_identity_no_realm(struct sip_msg *msg, str realm)
{
str pi = {0, 0};
struct hdr_field *h = 0;
int i;
if(parse_headers(msg, HDR_AUTHORIZATION_F, 0) != 0) {
return pi;
}
if(!msg->authorization) {
goto fallback;
}
h = msg->authorization;
if(h)
pi = ((auth_body_t *)h->parsed)->digest.username.whole;
goto done;
fallback:
pi = cscf_get_public_identity(msg);
if(pi.len > 4 && strncasecmp(pi.s, "sip:", 4) == 0) {
pi.s += 4;
pi.len -= 4;
}
for(i = 0; i < pi.len; i++)
if(pi.s[i] == ';') {
pi.len = i;
break;
}
done:
return pi;
}
/**
* Returns the Public Identity extracted from the To header
* @param msg - the SIP message
* @returns the str containing the public id, no mem dup
*/
str cscf_get_public_identity(struct sip_msg *msg)
{
str pu = {0, 0};
struct to_body *to;
int i;
if(parse_headers(msg, HDR_TO_F, 0) != 0) {
return pu;
}
if(get_to(msg) == NULL) {
to = (struct to_body *)pkg_malloc(sizeof(struct to_body));
if(to == NULL) {
PKG_MEM_ERROR;
return pu;
}
parse_to(msg->to->body.s, msg->to->body.s + msg->to->body.len, to);
msg->to->parsed = to;
} else
to = (struct to_body *)msg->to->parsed;
pu = to->uri;
/* truncate to sip:username@host or tel:number */
for(i = 4; i < pu.len; i++)
if(pu.s[i] == ';' || pu.s[i] == '?' || pu.s[i] == ':') {
pu.len = i;
}
return pu;
}
/**
* Returns the Public Identity extracted from the From header
* @param msg - the SIP message
* @returns the str containing the public id, no mem dup
*/
str cscf_get_public_identity_from(struct sip_msg *msg)
{
str pu = {0, 0};
struct to_body *from;
int i;
if(parse_headers(msg, HDR_FROM_F, 0) != 0) {
return pu;
}
if(get_from(msg) == NULL) {
from = (struct to_body *)pkg_malloc(sizeof(struct to_body));
if(from == NULL) {
PKG_MEM_ERROR;
return pu;
}
parse_to(msg->from->body.s, msg->from->body.s + msg->from->body.len,
from);
msg->from->parsed = from;
} else
from = (struct to_body *)msg->from->parsed;
pu = from->uri;
/* truncate to sip:username@host or tel:number */
for(i = 4; i < pu.len; i++)
if(pu.s[i] == ';' || pu.s[i] == '?' || pu.s[i] == ':') {
pu.len = i;
}
return pu;
}
/**
* Returns the expires value from the Expires header in the message.
* It searches into the Expires header and if not found returns -1
* @param msg - the SIP message, if available
* @is_shm - msg from from shared memory
* @returns the value of the expire or -1 if not found
*/
int cscf_get_expires_hdr(struct sip_msg *msg, int is_shm)
{
exp_body_t *exp;
int expires;
if(!msg)
return -1;
/*first search in Expires header */
if(parse_headers(msg, HDR_EXPIRES_F, 0) != 0) {
return -1;
}
if(msg->expires) {
if(!msg->expires->parsed) {
if(parse_expires(msg->expires) < 0) {
LM_ERR("failed to parse expires header\n");
}
}
if(msg->expires->parsed) {
exp = (exp_body_t *)msg->expires->parsed;
if(exp->valid) {
expires = exp->val;
if(is_shm) {
free_expires((exp_body_t **)&exp);
msg->expires->parsed = 0;
}
return expires;
}
}
}
return -1;
}
/**
* Returns the expires value from the message.
* First it searches into the Expires header and if not found it also looks
* into the expires parameter in the contact header
* @param msg - the SIP message
* @param is_shm - msg from shared memory
* @returns the value of the expire or the default 3600 if none found
*/
int cscf_get_max_expires(struct sip_msg *msg, int is_shm)
{
unsigned int exp;
int max_expires = -1;
struct hdr_field *h;
contact_t *c;
/*first search in Expires header */
max_expires = cscf_get_expires_hdr(msg, is_shm);
cscf_parse_contacts(msg);
for(h = msg->contact; h; h = h->next) {
if(h->type == HDR_CONTACT_T && h->parsed) {
for(c = ((contact_body_t *)h->parsed)->contacts; c; c = c->next) {
if(c->expires) {
if(!str2int(&(c->expires->body), (unsigned int *)&exp)
&& (int)exp > max_expires)
max_expires = exp;
}
}
}
}
if(is_shm) {
for(h = msg->contact; h; h = h->next) {
if(h->type == HDR_CONTACT_T && h->parsed) {
free_contact((contact_body_t **)&(h->parsed));
h->parsed = 0;
}
}
}
return max_expires;
}
/**
* Get the Public Identity from the Request URI of the message
* NB: free returned result str when done from shm
* @param msg - the SIP message
* @returns the public identity (don't forget to free from shm)
*/
str cscf_get_public_identity_from_requri(struct sip_msg *msg)
{
str pu = {0, 0};
if(msg->first_line.type != SIP_REQUEST) {
return pu;
}
if(parse_sip_msg_uri(msg) < 0) {
return pu;
}
if(msg->parsed_uri.type == TEL_URI_T) {
pu.len = 4 + msg->parsed_uri.user.len;
pu.s = shm_malloc(pu.len + 1);
if(!pu.s) {
SHM_MEM_ERROR;
pu.len = 0;
goto done;
}
sprintf(pu.s, "tel:%.*s", msg->parsed_uri.user.len,
msg->parsed_uri.user.s);
} else {
pu.len = 4 + msg->parsed_uri.user.len + 1 + msg->parsed_uri.host.len;
pu.s = shm_malloc(pu.len + 1);
if(!pu.s) {
SHM_MEM_ERROR;
pu.len = 0;
goto done;
}
sprintf(pu.s, "sip:%.*s@%.*s", msg->parsed_uri.user.len,
msg->parsed_uri.user.s, msg->parsed_uri.host.len,
msg->parsed_uri.host.s);
}
done:
return pu;
}
/**
* Get the contact from the Request URI of the message
* NB: free returned result str when done from shm
* @param msg - the SIP message
* @returns the contact (don't forget to free from shm)
*
* NOTE: should only be called when REQ URI has been converted sip:user@IP_ADDRESS:PORT or tel:IP_ADDRESS:PORT
*/
str cscf_get_contact_from_requri(struct sip_msg *msg)
{
str pu = {0, 0};
if(msg->first_line.type != SIP_REQUEST) {
return pu;
}
if(parse_sip_msg_uri(msg) < 0) {
return pu;
}
if(!msg->parsed_uri.port.len) {
return pu;
}
if(msg->parsed_uri.type == TEL_URI_T) {
pu.len = 4 + msg->parsed_uri.user.len + msg->parsed_uri.port.len
+ 1 /*for colon before port*/;
pu.s = shm_malloc(pu.len + 1);
if(!pu.s) {
SHM_MEM_ERROR;
pu.len = 0;
goto done;
}
sprintf(pu.s, "tel:%.*s:%.*s", msg->parsed_uri.user.len,
msg->parsed_uri.user.s, msg->parsed_uri.port.len,
msg->parsed_uri.port.s);
} else {
pu.len = 4 + msg->parsed_uri.user.len
+ 1 /*for @*/ + msg->parsed_uri.host.len
+ msg->parsed_uri.port.len + 1 /*for colon before port*/;
pu.s = shm_malloc(pu.len + 1);
if(!pu.s) {
SHM_MEM_ERROR;
pu.len = 0;
goto done;
}
sprintf(pu.s, "sip:%.*s@%.*s:%.*s", msg->parsed_uri.user.len,
msg->parsed_uri.user.s, msg->parsed_uri.host.len,
msg->parsed_uri.host.s, msg->parsed_uri.port.len,
msg->parsed_uri.port.s);
}
done:
return pu;
}
/**
* Finds if the message contains the orig parameter in the first Route header
* @param msg - the SIP message
* @param str1 - not used
* @param str2 - not used
* @returns #CSCF_RETURN_TRUE if yes, else #CSCF_RETURN_FALSE
*/
int cscf_has_originating(struct sip_msg *msg, char *str1, char *str2)
{
//int ret=CSCF_RETURN_FALSE;
struct hdr_field *h;
str *uri;
rr_t *r;
if(parse_headers(msg, HDR_ROUTE_F, 0) < 0) {
LM_DBG("I_originating: error parsing headers\n");
return CSCF_RETURN_FALSE;
}
h = msg->route;
if(!h) {
LM_DBG("I_originating: Header Route not found\n");
return CSCF_RETURN_FALSE;
}
if(parse_rr(h) < 0) {
LM_DBG("I_originating: Error parsing as Route header\n");
return CSCF_RETURN_FALSE;
}
r = (rr_t *)h->parsed;
uri = &r->nameaddr.uri;
struct sip_uri puri;
if(parse_uri(uri->s, uri->len, &puri) < 0) {
LM_DBG("I_originating: Error while parsing the first route URI\n");
return -1;
}
if(puri.params.len < 4)
return CSCF_RETURN_FALSE;
int c = 0;
int state = 0;
while(c < puri.params.len) {
switch(puri.params.s[c]) {
case 'o':
if(state == 0)
state = 1;
break;
case 'r':
if(state == 1)
state = 2;
break;
case 'i':
if(state == 2)
state = 3;
break;
case 'g':
if(state == 3)
state = 4;
break;
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
case ';':
if(state == 4)
return CSCF_RETURN_TRUE;
state = 0;
break;
case '=':
if(state == 4)
return CSCF_RETURN_TRUE;
state = -1;
break;
default:
state = -1;
}
c++;
}
return state == 4 ? CSCF_RETURN_TRUE : CSCF_RETURN_FALSE;
}
str s_asserted_identity = {"P-Asserted-Identity", 19};
/**
* Looks for the P-Asserted-Identity header and extracts its content
* @param msg - the sip message
* @returns the asserted identity
*/
str cscf_get_asserted_identity(struct sip_msg *msg, int is_shm)
{
int len;
str uri = {0, 0};
if(!msg || !msg->pai)
return uri;
if((parse_pai_header(msg) == 0) && (msg->pai) && (msg->pai->parsed)) {
to_body_t *pai = get_pai(msg)->id;
if(!is_shm)
return pai->uri;
//make a pkg malloc str to return to consuming function
len = pai->uri.len + 1;
uri.s = (char *)pkg_malloc(pai->uri.len + 1);
if(!uri.s) {
PKG_MEM_ERROR;
return uri;
}
memset(uri.s, 0, len);
memcpy(uri.s, pai->uri.s, pai->uri.len);
uri.len = pai->uri.len;
p_id_body_t *ptr = (p_id_body_t *)msg->pai->parsed;
msg->pai->parsed = 0;
free_pai_ppi_body(ptr);
}
return uri;
}
static str phone_context_s = {";phone-context=", 15};
/**
* Extracts the realm from a SIP/TEL URI.
* - SIP - the hostname
* - TEL - the phone-context parameter
* @param msg - the SIP message
* @returns the realm
*/
str cscf_get_realm_from_uri(str uri)
{
str realm = {0, 0};
int i;
if(uri.len < 5) {
LM_DBG("cscf_get_realm_from_uri: Error trying to extra realm from too "
"short URI <%.*s>.\n",
uri.len, uri.s);
return realm;
}
if(strncasecmp(uri.s, "sip:", 4) == 0
|| strncasecmp(uri.s, "sips:", 5) == 0) {
/* SIP URI */
realm = uri;
for(i = 0; i < realm.len; i++)
if(realm.s[i] == '@') {
realm.s = realm.s + i + 1;
realm.len = realm.len - i - 1;
break;
}
if(!realm.len)
realm = uri;
for(i = 0; i < realm.len; i++)
if(realm.s[i] == ';' || realm.s[i] == '&' || realm.s[i] == ':') {
realm.len = i;
break;
}
} else if(strncasecmp(uri.s, "tel:", 4) == 0) {
/* TEL URI */
realm = uri;
while(realm.s[0] != ';' && realm.len > 0) {
realm.s++;
realm.len--;
}
if(realm.len < 1) {
realm.len = 0;
return realm;
} else {
while(realm.len > phone_context_s.len) {
if(strncasecmp(realm.s, phone_context_s.s, phone_context_s.len)
== 0) {
realm.s += phone_context_s.len;
realm.len -= phone_context_s.len;
for(i = 0; i < realm.len; i++)
if(realm.s[i] == ';' || realm.s[i] == '&') {
realm.len = i;
break;
}
break;
}
realm.s++;
realm.len--;
}
}
} else {
/* unknown... just extract between @ and ;? */
realm = uri;
for(i = 0; i < realm.len; i++)
if(realm.s[i] == '@') {
realm.s = realm.s + i + 1;
realm.len = realm.len - i - 1;
break;
}
if(!realm.len)
realm = uri;
for(i = 0; i < realm.len; i++)
if(realm.s[i] == ';' || realm.s[i] == '&' || realm.s[i] == ':') {
realm.len = i;
break;
}
}
LM_DBG("cscf_get_realm_from_uri: realm <%.*s>.\n", realm.len, realm.s);
return realm;
}
/**
* Delivers the Realm from request URI
* @param msg sip message
* @returns realm as String on success 0 on fail
*/
str cscf_get_realm_from_ruri(struct sip_msg *msg)
{
str realm = {0, 0};
if(!msg || msg->first_line.type != SIP_REQUEST) {
LM_DBG("cscf_get_realm_from_ruri: This is not a request!!!\n");
return realm;
}
if(!msg->parsed_orig_ruri_ok)
if(parse_orig_ruri(msg) < 0)
return realm;
realm = msg->parsed_orig_ruri.host;
return realm;
}
/**
* Looks for the Call-ID header
* @param msg - the sip message
* @param hr - ptr to return the found hdr_field
* @returns the callid value
*/
str cscf_get_call_id(struct sip_msg *msg, struct hdr_field **hr)
{
struct hdr_field *h;
str call_id = {0, 0};
if(hr)
*hr = 0;
if(!msg)
return call_id;
if(parse_headers(msg, HDR_CALLID_F, 0) < 0) {
LM_DBG("cscf_get_call_id: error parsing headers\n");
return call_id;
}
h = msg->callid;
if(!h) {
LM_DBG("cscf_get_call_id: Header Call-ID not found\n");
return call_id;
}
if(hr)
*hr = h;
call_id = h->body;
return call_id;
}
static str sos_uri_par = {"sos", 3};
/**
* Check if the contact has an URI parameter with the value "sos",
* used for detecting an Emergency Registration
* http://tools.ietf.org/html/draft-patel-ecrit-sos-parameter-0x
* @param uri - contact uri to be checked
* @return 1 if found, 0 if not, -1 on error
*/
int cscf_get_sos_uri_param(str uri)
{
struct sip_uri puri;
param_hooks_t h;
param_t *p = 0, *crt;
enum pclass p_class = CLASS_URI;
int ret;
ret = 0;
p = NULL;
if(parse_uri(uri.s, uri.len, &puri) < 0) {
LM_DBG("cscf_get_sos_uri_param: failed to parse %.*s\n", uri.len,
uri.s);
return -1;
}
if(puri.params.len <= 0)
return 0;
LM_DBG("cscf_get_sos_uri_param: searching through the uri "
"parameters:%.*s\n",
puri.params.len, puri.params.s);
if(parse_params(&(puri.params), p_class, &h, &p)) {
LM_DBG("cscf_get_sos_uri_param:error while parsing uri parameters\n");
ret = -1;
goto end;
}
for(crt = p; crt; crt = crt->next) {
LM_DBG("cscf_get_sos_uri_param:name: %.*s body: %.*s\n", crt->name.len,
crt->name.s, crt->body.len, crt->body.s);
if((crt->name.len == sos_uri_par.len)
&& (strncmp(crt->name.s, sos_uri_par.s, sos_uri_par.len)
== 0)) {
ret = 1;
goto end;
}
}
end:
if(p)
free_params(p);
return ret;
}
str cscf_p_visited_network_id = {"P-Visited-Network-ID", 20};
/**
* Return the P-Visited-Network-ID header
* @param msg - the SIP message
* @returns the str with the header's body
*/
str cscf_get_visited_network_id(struct sip_msg *msg, struct hdr_field **h)
{
str vnid = {0, 0};
struct hdr_field *hdr;
if(h)
*h = 0;
if(parse_headers(msg, HDR_EOH_F, 0) != 0) {
LM_DBG("cscf_get_visited_network_id: Error parsing until header EOH: "
"\n");
return vnid;
}
hdr = msg->headers;
while(hdr) {
if(hdr->name.len == cscf_p_visited_network_id.len
&& strncasecmp(hdr->name.s, cscf_p_visited_network_id.s,
hdr->name.len)
== 0) {
if(h)
*h = hdr;
vnid = hdr->body;
goto done;
}
hdr = hdr->next;
}
LM_DBG("cscf_get_visited_network_id: P-Visited-Network-ID header not found "
"\n");
done:
LM_DBG("cscf_get_visited_network_id: <%.*s> \n", vnid.len, vnid.s);
return vnid;
}
/**
* Adds a header to the message as the first one in the message
* @param msg - the message to add a header to
* @param content - the str containing the new header
* @returns 1 on success, 0 on failure
*/
int cscf_add_header_first(struct sip_msg *msg, str *hdr, int type)
{
struct hdr_field *first;
struct lump *anchor, *l;
first = msg->headers;
anchor = anchor_lump(msg, first->name.s - msg->buf, 0, 0);
if(anchor == NULL) {
LM_DBG("cscf_add_header_first: anchor_lump failed\n");
return 0;
}
if(!(l = insert_new_lump_before(anchor, hdr->s, hdr->len, type))) {
LM_ERR("cscf_add_header_first: error creating lump for header\n");
return 0;
}
return 1;
}
/**
* Returns the next header structure for a given header name.
* @param msg - the SIP message to look into
* @param header_name - the name of the header to search for
* @param last_header - last header to ignore in the search, or NULL if to start from the first one
* @returns the hdr_field on success or NULL if not found
*/
struct hdr_field *cscf_get_next_header(
struct sip_msg *msg, str header_name, struct hdr_field *last_header)
{
struct hdr_field *h;
if(parse_headers(msg, HDR_EOH_F, 0) < 0) {
LM_ERR("cscf_get_next_header_field: error parsing headers\n");
return NULL;
}
if(last_header)
h = last_header->next;
else
h = msg->headers;
while(h) {
if(h->name.len == header_name.len
&& strncasecmp(h->name.s, header_name.s, header_name.len) == 0)
break;
h = h->next;
}
return h;
}
/**
* Looks for the First Via header and returns its body.
* @param msg - the SIP message
* @param h - the hdr_field to fill with the result
* @returns the first via_body
*/
struct via_body *cscf_get_first_via(struct sip_msg *msg, struct hdr_field **h)
{
if(h)
*h = 0;
if(!msg->h_via1 && parse_headers(msg, HDR_VIA_F, 0) != 0) {
LM_ERR("cscf_get_first_via: Error parsing until header Via: \n");
return msg->h_via1->parsed;
}
if(!msg->via1) {
LM_ERR("cscf_get_first_via: Message does not contain Via header.\n");
return msg->h_via1->parsed;
}
return msg->h_via1->parsed;
}
/**
* Looks for the UE Via in First Via header if it is a request
* or in the last if it is a response and returns its body
* @param msg - the SIP message
* @returns the via of the UE
*/
struct via_body *cscf_get_ue_via(struct sip_msg *msg)
{
struct via_body *vb = 0;
if(msg->first_line.type == SIP_REQUEST)
vb = cscf_get_first_via(msg, 0);
else
vb = cscf_get_last_via(msg);
if(!vb)
return 0;
if(vb->port == 0)
vb->port = 5060;
return vb;
}
/**
* Looks for the Last Via header and returns it.
* @param msg - the SIP message
* @returns the last via body body
*/
struct via_body *cscf_get_last_via(struct sip_msg *msg)
{
struct hdr_field *h = 0, *i;
struct via_body *vb;
if(parse_headers(msg, HDR_EOH_F, 0) != 0) {
LM_ERR("cscf_get_last_via: Error parsing until last header\n");
return 0;
}