-
Notifications
You must be signed in to change notification settings - Fork 909
/
nat_traversal.c
2136 lines (1729 loc) · 50.2 KB
/
nat_traversal.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) 2007-2009 Dan Pascu
*
* 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
*
*/
/*!
* \file
* \brief Module interface and functions
* \ingroup nat_traversal
* Module: \ref nat_traversal
*/
/**
* @defgroup nat_traversal Nat
* @brief Kamailio nat_traversal module
The nat_traversal module provides support for handling far-end NAT
traversal for SIP signaling.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <inttypes.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include "../../core/sr_module.h"
#include "../../core/mem/shm_mem.h"
#include "../../core/mem/mem.h"
#include "../../core/lock_ops.h"
#include "../../core/dprint.h"
#include "../../core/str.h"
#include "../../core/ut.h"
#include "../../core/pvar.h"
#include "../../core/error.h"
#include "../../core/timer.h"
#include "../../core/resolve.h"
#include "../../core/data_lump.h"
#include "../../core/mod_fix.h"
#include "../../core/script_cb.h"
#include "../../core/strutils.h"
#include "../../core/timer_proc.h"
#include "../../core/parser/msg_parser.h"
#include "../../core/parser/parse_from.h"
#include "../../core/parser/parse_uri.h"
#include "../../core/parser/parse_expires.h"
#include "../../core/parser/contact/parse_contact.h"
#include "../../core/counters.h"
#include "../../core/rand/kam_rand.h"
#include "../../core/kemi.h"
#include "../dialog/dlg_load.h"
#include "../../modules/tm/tm_load.h"
#include "../../modules/sl/sl.h"
MODULE_VERSION
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define INLINE inline
#else
#define INLINE
#endif
/* WARNING: Keep this aligned with parser/msg_parser.h! */
#define FL_DO_KEEPALIVE (1u << 31)
#define HASH_SIZE 512
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define STR_MATCH(str, buf) \
((str).len == strlen(buf) && memcmp(buf, (str).s, (str).len) == 0)
#define STR_IMATCH(str, buf) \
((str).len == strlen(buf) && strncasecmp(buf, (str).s, (str).len) == 0)
#define STR_MATCH_STR(str, str2) \
((str).len == (str2).len && memcmp((str).s, (str2).s, (str).len) == 0)
#define STR_IMATCH_STR(str, str2) \
((str).len == (str2).len && strncasecmp((str).s, (str2).s, (str).len) == 0)
#define STR_HAS_PREFIX(str, prefix) \
((str).len > (prefix).len && memcmp((prefix).s, (str).s, (prefix).len) == 0)
#define STR_HAS_IPREFIX(str, prefix) \
((str).len > (prefix).len \
&& strncasecmp((prefix).s, (str).s, (prefix).len) == 0)
typedef bool (*NatTestFunction)(struct sip_msg *msg);
typedef enum
{
NTNone = 0,
NTPrivateContact = 1,
NTSourceAddress = 2,
NTPrivateVia = 4
} NatTestType;
typedef struct
{
NatTestType test;
NatTestFunction proc;
} NatTest;
typedef struct
{
const char *name;
uint32_t address;
uint32_t mask;
} NetInfo;
typedef struct SIP_Dialog
{
struct dlg_cell *dlg;
time_t expire;
struct SIP_Dialog *next;
} SIP_Dialog;
typedef struct NAT_Contact
{
char *uri;
struct socket_info *socket;
time_t registration_expire;
time_t subscription_expire;
SIP_Dialog *dialogs;
struct NAT_Contact *next;
} NAT_Contact;
typedef struct HashSlot
{
NAT_Contact
*head; // pointer to the head of the linked list stored in this slot
gen_lock_t lock;
} HashSlot;
typedef struct HashTable
{
HashSlot *slots;
unsigned size; // table size (number of slots)
} HashTable;
#define URI_LIST_INITIAL_SIZE 8
#define URI_LIST_RESIZE_INCREMENT 8
typedef struct Dialog_Param
{
char *caller_uri;
char *callee_uri;
time_t expire;
bool confirmed;
gen_lock_t lock;
struct
{
char **uri;
int count;
int size;
} callee_candidates;
} Dialog_Param;
// Module parameters
//
typedef struct Keepalive_Params
{
// user specified
char *method;
char *from;
char *extra_headers;
// internally generated
char callid_prefix[20];
unsigned callid_counter;
unsigned from_tag;
char *event_header; // this will be set if method is NOTIFY
} Keepalive_Params;
// Function prototypes
//
static int w_NAT_Keepalive(struct sip_msg *msg, char *p1, char *p2);
static int w_FixContact(struct sip_msg *msg, char *p1, char *p2);
static int w_ClientNatTest(struct sip_msg *msg, char *ptests, char *p2);
static bool test_private_contact(struct sip_msg *msg);
static bool test_source_address(struct sip_msg *msg);
static bool test_private_via(struct sip_msg *msg);
static int mod_init(void);
static int child_init(int rank);
static void mod_destroy(void);
static int preprocess_request(
struct sip_msg *msg, unsigned int flags, void *param);
static int reply_filter(struct sip_msg *reply);
static int pv_parse_nat_contact_name(pv_spec_p sp, str *in);
static int pv_get_keepalive_socket(
struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
static int pv_get_source_uri(
struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
// Module global variables and state
//
static HashTable *nat_table = NULL;
static bool keepalive_disabled = false;
static unsigned int keepalive_interval = 60;
static char *keepalive_state_file = "keepalive_state";
static Keepalive_Params keepalive_params = {"NOTIFY", NULL, "", "", 0, 0, ""};
struct tm_binds tm_api;
struct dlg_binds dlg_api;
bool have_dlg_api = false;
static int dialog_flag = -1;
static unsigned dialog_default_timeout = 12 * 3600; // 12 hours
static int natt_contact_match = 0;
stat_var *keepalive_endpoints = 0;
stat_var *registered_endpoints = 0;
stat_var *subscribed_endpoints = 0;
stat_var *dialog_endpoints = 0;
static NetInfo rfc1918nets[] = {{"10.0.0.0", 0x0a000000UL, 0xff000000UL},
{"172.16.0.0", 0xac100000UL, 0xfff00000UL},
{"192.168.0.0", 0xc0a80000UL, 0xffff0000UL},
{"100.64.0.0", 0x64400000UL,
0xffc00000UL}, // include rfc6598 shared address space as technically the same for our purpose
{"192.0.0.0", 0xc0000000UL,
0xfffffff8UL}, // include rfc7335 IPv4 Service Continuity Prefix
{NULL, 0UL, 0UL}};
static NatTest NAT_Tests[] = {{NTPrivateContact, test_private_contact},
{NTSourceAddress, test_source_address},
{NTPrivateVia, test_private_via}, {NTNone, NULL}};
/** SL API structure */
sl_api_t slb;
static cmd_export_t commands[] = {
{"nat_keepalive", (cmd_function)w_NAT_Keepalive, 0, NULL, 0,
REQUEST_ROUTE},
{"fix_contact", (cmd_function)w_FixContact, 0, NULL, 0, ANY_ROUTE},
{"client_nat_test", (cmd_function)w_ClientNatTest, 1, fixup_igp_null, 0,
ANY_ROUTE},
{0, 0, 0, 0, 0, 0}};
static param_export_t parameters[] = {
{"keepalive_interval", INT_PARAM, &keepalive_interval},
{"keepalive_method", PARAM_STRING, &keepalive_params.method},
{"keepalive_from", PARAM_STRING, &keepalive_params.from},
{"keepalive_extra_headers", PARAM_STRING,
&keepalive_params.extra_headers},
{"keepalive_state_file", PARAM_STRING, &keepalive_state_file},
{"contact_match", PARAM_INT, &natt_contact_match},
{0, 0, 0}};
static pv_export_t pvars[] = {
{str_init("keepalive.socket"), PVT_OTHER, pv_get_keepalive_socket, NULL,
pv_parse_nat_contact_name, NULL, NULL, 0},
{str_init("source_uri"), PVT_OTHER, pv_get_source_uri, NULL, NULL, NULL,
NULL, 0},
{{0, 0}, 0, 0, 0, 0, 0, 0, 0}};
#ifdef STATISTICS
static stat_export_t statistics[] = {
{"keepalive_endpoints", STAT_NO_RESET, &keepalive_endpoints},
{"registered_endpoints", STAT_NO_RESET, ®istered_endpoints},
{"subscribed_endpoints", STAT_NO_RESET, &subscribed_endpoints},
{"dialog_endpoints", STAT_NO_RESET, &dialog_endpoints}, {0, 0, 0}};
#endif
struct module_exports exports = {
"nat_traversal", // module name
DEFAULT_DLFLAGS, // dlopen flags
commands, // exported functions
parameters, // exported parameters
0, // exported RPC methods
pvars, // exported pseudo-variables
reply_filter, // reply processing function
mod_init, // module init function (before fork. kids will inherit)
child_init, // child init function
mod_destroy // destroy function
};
// SIP_Dialog structure handling functions
//
static SIP_Dialog *SIP_Dialog_new(struct dlg_cell *dlg, time_t expire)
{
SIP_Dialog *dialog;
dialog = (SIP_Dialog *)shm_malloc(sizeof(SIP_Dialog));
if(!dialog) {
LM_ERR("out of memory while creating new SIP_Dialog structure\n");
return NULL;
}
dialog->dlg = dlg;
dialog->expire = expire;
dialog->next = NULL;
// we assume expire is always strictly positive on new dialogs
update_stat(dialog_endpoints, 1);
return dialog;
}
static void SIP_Dialog_del(SIP_Dialog *dialog)
{
if(!dialog)
return;
if(dialog->expire > 0)
update_stat(dialog_endpoints, -1);
shm_free(dialog);
}
// Purge expired dialogs from the linked list pointed by dialog
//
static SIP_Dialog *SIP_Dialog_purge_expired(SIP_Dialog *dialog, time_t now)
{
SIP_Dialog *next;
if(dialog == NULL)
return NULL;
dialog->next = SIP_Dialog_purge_expired(dialog->next, now);
if(now > dialog->expire) {
next = dialog->next;
SIP_Dialog_del(dialog);
return next;
}
return dialog;
}
static INLINE void SIP_Dialog_end(SIP_Dialog *dialog)
{
if(dialog->expire > 0) {
dialog->expire = 0;
update_stat(dialog_endpoints, -1);
}
}
// Helpers to handle registration and subscription timeouts for NAT_Contacts
//
static INLINE void SIP_Registration_update(NAT_Contact *contact, time_t expire)
{
if(expire > contact->registration_expire) {
if(contact->registration_expire == 0)
update_stat(registered_endpoints, 1);
contact->registration_expire = expire;
}
}
static INLINE void SIP_Registration_expire(NAT_Contact *contact, time_t now)
{
if(contact->registration_expire && now > contact->registration_expire) {
update_stat(registered_endpoints, -1);
contact->registration_expire = 0;
}
}
static INLINE void SIP_Subscription_update(NAT_Contact *contact, time_t expire)
{
if(expire > contact->subscription_expire) {
if(contact->subscription_expire == 0)
update_stat(subscribed_endpoints, 1);
contact->subscription_expire = expire;
}
}
static INLINE void SIP_Subscription_expire(NAT_Contact *contact, time_t now)
{
if(contact->subscription_expire && now > contact->subscription_expire) {
update_stat(subscribed_endpoints, -1);
contact->subscription_expire = 0;
}
}
// NAT_Contact structure handling functions
//
static NAT_Contact *NAT_Contact_new(char *uri, struct socket_info *socket)
{
NAT_Contact *contact;
contact = (NAT_Contact *)shm_malloc(sizeof(NAT_Contact));
if(!contact) {
LM_ERR("out of memory while creating new NAT_Contact structure\n");
return NULL;
}
memset(contact, 0, sizeof(NAT_Contact));
contact->uri = shm_char_dup(uri);
if(!contact->uri) {
LM_ERR("out of memory while creating new NAT_Contact structure\n");
shm_free(contact);
return NULL;
}
contact->socket = socket;
update_stat(keepalive_endpoints, 1);
return contact;
}
static void NAT_Contact_del(NAT_Contact *contact)
{
SIP_Dialog *dialog, *next;
if(!contact)
return;
dialog = contact->dialogs;
while(dialog) {
next = dialog->next;
SIP_Dialog_del(dialog);
dialog = next;
}
if(contact->registration_expire > 0)
update_stat(registered_endpoints, -1);
if(contact->subscription_expire > 0)
update_stat(subscribed_endpoints, -1);
update_stat(keepalive_endpoints, -1);
shm_free(contact->uri);
shm_free(contact);
}
static bool NAT_Contact_match(NAT_Contact *contact, const char *uri)
{
return strcmp(contact->uri, uri) == 0;
}
static SIP_Dialog *NAT_Contact_get_dialog(
NAT_Contact *contact, struct dlg_cell *dlg)
{
SIP_Dialog *dialog;
dialog = contact->dialogs;
while(dialog) {
if(dialog->dlg == dlg)
break;
dialog = dialog->next;
}
return dialog;
}
static NAT_Contact *NAT_Contact_purge_expired(NAT_Contact *contact, time_t now)
{
NAT_Contact *next;
if(contact == NULL)
return NULL;
contact->next = NAT_Contact_purge_expired(contact->next, now);
SIP_Registration_expire(contact, now);
SIP_Subscription_expire(contact, now);
contact->dialogs = SIP_Dialog_purge_expired(contact->dialogs, now);
if(!contact->registration_expire && !contact->subscription_expire
&& !contact->dialogs) {
next = contact->next;
NAT_Contact_del(contact);
return next;
}
return contact;
}
// HashTable structure manipulation
//
#define HASH(table, key) (hash_string(key) % (table)->size)
static INLINE unsigned hash_string(const char *key)
{
register unsigned ret = 0;
register unsigned ctr = 0;
while(*key) {
ret ^= *(char *)key++ << ctr;
ctr = (ctr + 1) % sizeof(char *);
}
return ret;
}
static HashTable *HashTable_new(void)
{
HashTable *table;
int i, j;
table = shm_malloc(sizeof(HashTable));
if(!table) {
LM_ERR("cannot allocate shared memory for hash table\n");
return NULL;
}
memset(table, 0, sizeof(HashTable));
table->size = HASH_SIZE;
table->slots = shm_malloc(sizeof(HashSlot) * table->size);
if(!table->slots) {
LM_ERR("cannot allocate shared memory for hash table\n");
shm_free(table);
return NULL;
}
memset(table->slots, 0, sizeof(HashSlot) * table->size);
for(i = 0; i < table->size; i++) {
if(!lock_init(&table->slots[i].lock)) {
LM_ERR("cannot initialize hash table locks\n");
for(j = 0; j < i; j++)
lock_destroy(&table->slots[j].lock);
shm_free(table->slots);
shm_free(table);
return NULL;
}
}
return table;
}
static void HashTable_del(HashTable *table)
{
NAT_Contact *contact, *next;
int i;
for(i = 0; i < table->size; i++) {
lock_get(&table->slots[i].lock);
contact = table->slots[i].head;
while(contact) {
next = contact->next;
NAT_Contact_del(contact);
contact = next;
}
table->slots[i].head = NULL;
lock_release(&table->slots[i].lock);
}
shm_free(table->slots);
shm_free(table);
}
// This function assumes that the caller has locked the slot already
//
static NAT_Contact *HashTable_search(HashTable *table, char *uri, unsigned slot)
{
NAT_Contact *contact;
contact = table->slots[slot].head;
while(contact) {
if(NAT_Contact_match(contact, uri))
break;
contact = contact->next;
}
return contact;
}
// Dialog_Param structure handling functions
//
static Dialog_Param *Dialog_Param_new(void)
{
Dialog_Param *param;
param = shm_malloc(sizeof(Dialog_Param));
if(!param) {
LM_ERR("cannot allocate shared memory for dialog callback param\n");
return NULL;
}
memset(param, 0, sizeof(Dialog_Param));
param->callee_candidates.uri =
shm_malloc(sizeof(char *) * URI_LIST_INITIAL_SIZE);
if(!param->callee_candidates.uri) {
LM_ERR("cannot allocate shared memory for callee_candidates uri "
"list\n");
shm_free(param);
return NULL;
}
memset(param->callee_candidates.uri, 0,
sizeof(char *) * URI_LIST_INITIAL_SIZE);
param->callee_candidates.size = URI_LIST_INITIAL_SIZE;
param->expire = time(NULL) + dialog_default_timeout;
if(!lock_init(¶m->lock)) {
LM_ERR("cannot initialize dialog param structure lock\n");
shm_free(param->callee_candidates.uri);
shm_free(param);
return NULL;
}
return param;
}
static void Dialog_Param_del(Dialog_Param *param)
{
int i;
if(!param)
return;
lock_destroy(¶m->lock);
if(param->caller_uri)
shm_free(param->caller_uri);
if(param->callee_uri)
shm_free(param->callee_uri);
for(i = 0; i < param->callee_candidates.count; i++)
shm_free(param->callee_candidates.uri[i]);
shm_free(param->callee_candidates.uri);
shm_free(param);
}
// This function assumes the caller has locked the Dialog_Param while operating on it
//
static bool Dialog_Param_has_candidate(Dialog_Param *param, char *candidate)
{
int i;
for(i = 0; i < param->callee_candidates.count; i++) {
if(strcmp(candidate, param->callee_candidates.uri[i]) == 0) {
return true;
}
}
return false;
}
// This function assumes the caller has locked the Dialog_Param while operating on it
//
static bool Dialog_Param_add_candidate(Dialog_Param *param, char *candidate)
{
char **new_uri, *new_candidate;
int new_size;
if(param->callee_candidates.count == param->callee_candidates.size) {
new_size = param->callee_candidates.size + URI_LIST_RESIZE_INCREMENT;
LM_DBG("growing callee_candidates list size from %d to %d entries\n",
param->callee_candidates.size, new_size);
new_uri = shm_realloc(
param->callee_candidates.uri, new_size * sizeof(char *));
if(!new_uri) {
LM_ERR("failed to grow callee_candidates uri list\n");
return false;
}
param->callee_candidates.uri = new_uri;
param->callee_candidates.size = new_size;
}
new_candidate = shm_char_dup(candidate);
if(!new_candidate) {
LM_ERR("cannot allocate shared memory for new candidate uri\n");
return false;
}
param->callee_candidates.uri[param->callee_candidates.count] =
new_candidate;
param->callee_candidates.count++;
return true;
}
// Miscellaneous helper functions
//
static bool get_contact_uri(
struct sip_msg *msg, struct sip_uri *uri, contact_t **_c)
{
if((parse_headers(msg, HDR_CONTACT_F, 0) == -1) || !msg->contact)
return false;
if(!msg->contact->parsed && parse_contact(msg->contact) < 0) {
LM_ERR("cannot parse the Contact header\n");
return false;
}
*_c = ((contact_body_t *)msg->contact->parsed)->contacts;
if(*_c == NULL) {
return false;
}
if(parse_uri((*_c)->uri.s, (*_c)->uri.len, uri) < 0 || uri->host.len <= 0) {
LM_ERR("cannot parse the Contact URI\n");
return false;
}
return true;
}
#define is_private_address(x) (rfc1918address(x) == 1 ? 1 : 0)
// Test if IP in `address' belongs to a RFC1918 network
static INLINE int rfc1918address(str *address)
{
struct ip_addr *ip;
uint32_t netaddr;
int i;
ip = str2ip(address);
if(ip == NULL)
return -1; // invalid address to test
netaddr = ntohl(ip->u.addr32[0]);
for(i = 0; rfc1918nets[i].name != NULL; i++) {
if((netaddr & rfc1918nets[i].mask) == rfc1918nets[i].address) {
return 1;
}
}
return 0;
}
// Test if address of signaling is different from address in 1st Via field
static bool test_source_address(struct sip_msg *msg)
{
bool different_ip, different_port;
int via1_port;
different_ip = received_via_test(msg);
via1_port = (msg->via1->port ? msg->via1->port : SIP_PORT);
different_port = (msg->rcv.src_port != via1_port);
return (different_ip || different_port);
}
// Test if Contact field contains a private IP address as defined in RFC1918
static bool test_private_contact(struct sip_msg *msg)
{
struct sip_uri uri;
contact_t *contact;
if(!get_contact_uri(msg, &uri, &contact))
return false;
return is_private_address(&(uri.host));
}
// Test if top Via field contains a private IP address as defined in RFC1918
static bool test_private_via(struct sip_msg *msg)
{
return is_private_address(&(msg->via1->host));
}
// return the Expires header value (converted to a UNIX timestamp if > 0)
static int get_expires(struct sip_msg *msg)
{
exp_body_t *expires;
if(parse_headers(msg, HDR_EXPIRES_F, 0) < 0) {
LM_ERR("failed to parse the Expires header\n");
return 0;
}
if(!msg->expires)
return 0;
if(parse_expires(msg->expires) < 0) {
LM_ERR("failed to parse the Expires header body\n");
return 0;
}
expires = (exp_body_t *)msg->expires->parsed;
return ((expires->valid && expires->val) ? expires->val + time(NULL) : 0);
}
// return the highest expire value from all registered contacts in the request
static time_t get_register_expire(
struct sip_msg *request, struct sip_msg *reply)
{
struct hdr_field contact_hdr, *hdr, *r_hdr;
contact_body_t *contact_body, *r_contact_body;
contact_t *contact, *r_contact;
param_t *expires_param;
time_t now, expire = 0;
unsigned exp;
bool matched;
if(!request->contact)
return 0;
if(parse_headers(reply, HDR_EOH_F, 0) < 0) {
LM_ERR("failed to parse headers for REGISTER reply\n");
return 0;
}
if(!reply->contact)
return 0;
now = time(NULL);
// request may be R/O (if we are called from the TM callback),
// thus we copy the hdr_field structures before parsing them
for(hdr = request->contact; hdr; hdr = next_sibling_hdr(hdr)) {
if(!hdr->parsed) {
memcpy(&contact_hdr, hdr, sizeof(struct hdr_field));
if(parse_contact(&contact_hdr) < 0) {
LM_ERR("failed to parse the Contact header body\n");
continue;
}
contact_body = (contact_body_t *)contact_hdr.parsed;
} else {
contact_body = (contact_body_t *)hdr->parsed;
}
if(contact_body->star) {
if(!hdr->parsed)
clean_hdr_field(&contact_hdr);
return 0;
}
for(contact = contact_body->contacts; contact;
contact = contact->next) {
for(r_hdr = reply->contact, matched = false; r_hdr && !matched;
r_hdr = next_sibling_hdr(r_hdr)) {
if(!r_hdr->parsed && parse_contact(r_hdr) < 0) {
LM_ERR("failed to parse the Contact header body in "
"reply\n");
continue;
}
r_contact_body = (contact_body_t *)r_hdr->parsed;
for(r_contact = r_contact_body->contacts; r_contact;
r_contact = r_contact->next) {
if((natt_contact_match == 0
&& STR_MATCH_STR(contact->uri, r_contact->uri))
|| (natt_contact_match == 1
&& cmp_uri_light_str(
&contact->uri, &r_contact->uri)
== 0)) {
expires_param = r_contact->expires;
if(expires_param && expires_param->body.len
&& str2int(&expires_param->body, &exp) == 0)
expire = max(expire, exp);
matched = true;
break;
}
}
}
}
if(!hdr->parsed) {
clean_hdr_field(&contact_hdr);
}
}
LM_DBG("maximum expire for all contacts: %" PRIu64 "\n", (uint64_t)expire);
return (expire ? expire + now : 0);
}
static char *get_source_uri(struct sip_msg *msg)
{
static char uri[64];
snprintf(uri, 64, "sip:%s:%d", ip_addr2a(&msg->rcv.src_ip),
msg->rcv.src_port);
return uri;
}
static void keepalive_registration(struct sip_msg *request, time_t expire)
{
NAT_Contact *contact;
unsigned h;
char *uri;
uri = get_source_uri(request);
h = HASH(nat_table, uri);
lock_get(&nat_table->slots[h].lock);
contact = HashTable_search(nat_table, uri, h);
if(contact) {
SIP_Registration_update(contact, expire);
} else {
contact = NAT_Contact_new(uri, request->rcv.bind_address);
if(contact) {
SIP_Registration_update(contact, expire);
contact->next = nat_table->slots[h].head;
nat_table->slots[h].head = contact;
} else {
LM_ERR("cannot allocate shared memory for new NAT contact\n");
}
}
lock_release(&nat_table->slots[h].lock);
}
static void keepalive_subscription(struct sip_msg *request, time_t expire)
{
NAT_Contact *contact;
unsigned h;
char *uri;
uri = get_source_uri(request);
h = HASH(nat_table, uri);
lock_get(&nat_table->slots[h].lock);
contact = HashTable_search(nat_table, uri, h);
if(contact) {
SIP_Subscription_update(contact, expire);
} else {
contact = NAT_Contact_new(uri, request->rcv.bind_address);
if(contact) {
SIP_Subscription_update(contact, expire);
contact->next = nat_table->slots[h].head;
nat_table->slots[h].head = contact;
} else {
LM_ERR("cannot allocate shared memory for new NAT contact\n");
}
}
lock_release(&nat_table->slots[h].lock);
}
static void __dialog_early(
struct dlg_cell *dlg, int type, struct dlg_cb_params *_params)
{
Dialog_Param *param = (Dialog_Param *)*_params->param;
NAT_Contact *contact;