forked from freeswitch/sofia-sip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_proxy.c
1604 lines (1279 loc) · 37.7 KB
/
test_proxy.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
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
*
* Contact: Pekka Pessi <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**@CFILE test_proxy.c
* @brief Extremely simple proxy and registrar for testing nua
*
* @author Pekka Pessi <[email protected]>
*
* @date Created: Thu Nov 3 22:49:46 EET 2005
*/
#include "config.h"
#include <string.h>
struct proxy;
struct domain;
union proxy_or_domain;
struct proxy_tr;
struct client_tr;
struct registration_entry;
struct binding;
#define SU_ROOT_MAGIC_T struct proxy
#define NTA_LEG_MAGIC_T union proxy_or_domain
#define NTA_OUTGOING_MAGIC_T struct client_tr
#define NTA_INCOMING_MAGIC_T struct proxy_tr
#define SU_TIMER_ARG_T struct proxy_tr
#include <sofia-sip/su_wait.h>
#include <sofia-sip/nta.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/auth_module.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/msg_addr.h>
#include <sofia-sip/hostdomain.h>
#include <sofia-sip/tport.h>
#include <sofia-sip/nta_tport.h>
#include <stdlib.h>
#include <assert.h>
#define LIST_PROTOS(STORAGE, PREFIX, T) \
STORAGE void PREFIX ##_insert(T **list, T *node), \
PREFIX ##_remove(T *node)
#define LIST_BODIES(STORAGE, PREFIX, T, NEXT, PREV) \
STORAGE void PREFIX ##_insert(T **list, T *node) \
{ \
if ((node->NEXT = *list)) { \
node->PREV = node->NEXT->PREV; \
node->NEXT->PREV = &node->NEXT; \
} \
else \
node->PREV = list; \
*list = node; \
} \
STORAGE void PREFIX ##_remove(T *node) \
{ \
if (node->PREV) \
if ((*node->PREV = node->NEXT)) \
node->NEXT->PREV = node->PREV; \
node->PREV = NULL; \
} \
extern int LIST_DUMMY_VARIABLE
#include "test_proxy.h"
#include <sofia-sip/auth_module.h>
struct proxy {
su_home_t home[1];
void *magic;
su_root_t *parent;
su_clone_r clone;
tagi_t *tags;
su_root_t *root;
nta_agent_t *agent;
url_t const *uri;
sip_route_t *lr;
char const *lr_str;
url_t const *rr_uri;
nta_leg_t *defleg;
sip_contact_t *transport_contacts;
struct proxy_tr *stateless;
struct proxy_tr *transactions;
struct proxy_tr *invite_waiting;
struct domain *domains;
struct {
unsigned t1x64;
sip_time_t session_expires, min_se;
} prefs;
};
struct domain {
su_home_t home[1];
void *magic;
struct proxy *proxy;
struct domain *next, **prev;
url_t *uri;
nta_leg_t *rleg, *uleg;
auth_mod_t *auth;
struct registration_entry *entries;
struct {
sip_time_t min_expires, expires, max_expires;
int outbound_tcp; /**< Use inbound TCP connection as outbound */
char const *authorize; /**< Authorization realm to use */
int record_route;
} prefs;
tagi_t *tags;
};
LIST_PROTOS(static, domain, struct domain);
static int _domain_init(void *_d);
static int domain_init(struct domain *domain);
static void domain_destroy(struct domain *domain);
LIST_BODIES(static, domain, struct domain, next, prev);
LIST_PROTOS(static, registration_entry, struct registration_entry);
static struct registration_entry *registration_entry_new(struct domain *,
url_t const *);
static void registration_entry_destroy(struct registration_entry *e);
struct registration_entry
{
struct registration_entry *next, **prev;
struct domain *domain; /* backpointer */
url_t *aor; /* address-of-record */
struct binding *bindings; /* list of bindings */
sip_contact_t *contacts;
};
struct binding
{
struct binding *next, **prev;
sip_contact_t *contact; /* binding */
sip_time_t registered, expires; /* When registered and when expires */
sip_call_id_t *call_id;
uint32_t cseq;
tport_t *tport; /**< Reference to tport */
};
static struct binding *binding_new(su_home_t *home,
sip_contact_t *contact,
tport_t *tport,
sip_call_id_t const *call_id,
uint32_t cseq,
sip_time_t registered,
sip_time_t expires);
static void binding_destroy(su_home_t *home, struct binding *b);
static int binding_is_active(struct binding const *b)
{
return
b->expires > sip_now() &&
(b->tport == NULL || tport_is_clear_to_send(b->tport));
}
LIST_PROTOS(static, proxy_tr, struct proxy_tr);
struct proxy_tr *proxy_tr_new(struct proxy *);
static void proxy_tr_timeout(struct proxy_tr *t);
static void proxy_tr_destroy(struct proxy_tr *t);
struct proxy_tr
{
struct proxy_tr *next, **prev;
struct proxy *proxy; /**< Backpointer to proxy */
struct domain *origin; /**< Originating domain */
struct domain *domain; /**< Destination domain */
sip_time_t now; /**< When received */
nta_incoming_t *server; /**< server transaction */
msg_t *msg; /**< request message */
sip_t *sip; /**< request headers */
sip_method_t method; /**< request method */
char const *method_name;
int status; /**< best status */
url_t *target; /**< request-URI */
struct client_tr *clients; /**< Client transactions */
struct registration_entry *entry;
/**< Registration entry */
auth_mod_t *am; /**< Authentication module */
auth_status_t *as; /**< Authentication status */
char const *realm; /**< Authentication realm to use */
unsigned use_auth; /**< Authentication method (401/407) to use */
su_timer_t *timer; /**< Timer */
unsigned rr:1;
};
LIST_PROTOS(static, client_tr, struct client_tr);
struct client_tr
{
struct client_tr *next, **prev;
struct proxy_tr *t;
int status; /* response status */
sip_request_t *rq; /* request line */
msg_t *msg; /* request message */
sip_t *sip; /* request headers */
nta_outgoing_t *client; /* transaction */
};
LIST_BODIES(static, client_tr, struct client_tr, next, prev);
static sip_contact_t *create_transport_contacts(struct proxy *p);
union proxy_or_domain { struct proxy proxy[1]; struct domain domain[1]; };
static int proxy_request(union proxy_or_domain *proxy,
nta_leg_t *leg,
nta_incoming_t *irq,
sip_t const *sip);
static int domain_request(union proxy_or_domain *domain,
nta_leg_t *leg,
nta_incoming_t *irq,
sip_t const *sip);
static int proxy_response(struct client_tr *client,
nta_outgoing_t *orq,
sip_t const *sip);
static int close_tports(void *proxy);
static auth_challenger_t registrar_challenger[1];
static auth_challenger_t proxy_challenger[1];
/* Proxy entry point */
static int
test_proxy_init(su_root_t *root, struct proxy *proxy)
{
struct proxy_tr *t;
struct client_tr *c;
auth_challenger_t _proxy_challenger[1] =
{{
SIP_407_PROXY_AUTH_REQUIRED,
sip_proxy_authenticate_class,
sip_proxy_authentication_info_class
}};
auth_challenger_t _registrar_challenger[1] =
{{
SIP_401_UNAUTHORIZED,
sip_www_authenticate_class,
sip_authentication_info_class
}};
*proxy_challenger = *_proxy_challenger;
*registrar_challenger = *_registrar_challenger;
proxy->root = root;
proxy->agent = nta_agent_create(root,
URL_STRING_MAKE("sip:0.0.0.0:*"),
NULL, NULL,
NTATAG_UA(0),
NTATAG_CANCEL_487(0),
NTATAG_SERVER_RPORT(1),
NTATAG_CLIENT_RPORT(1),
TAG_NEXT(proxy->tags));
if (!proxy->agent)
return -1;
proxy->transport_contacts = create_transport_contacts(proxy);
proxy->defleg = nta_leg_tcreate(proxy->agent,
proxy_request,
(union proxy_or_domain *)proxy,
NTATAG_NO_DIALOG(1),
TAG_END());
proxy->prefs.session_expires = 180;
proxy->prefs.min_se = 90;
proxy->prefs.t1x64 = 64 * 500;
nta_agent_get_params(proxy->agent,
NTATAG_SIP_T1X64_REF(proxy->prefs.t1x64),
TAG_END());
if (!proxy->defleg)
return -1;
/* if (!proxy->example_net || !proxy->example_org || !proxy->example_com)
return -1; */
/* Create stateless client */
t = su_zalloc(proxy->home, sizeof *t);
c = su_zalloc(proxy->home, sizeof *c);
if (!t || !c)
return -1;
proxy->stateless = t;
t->proxy = proxy;
c->t = t, client_tr_insert(&t->clients, c);
t->server = nta_incoming_default(proxy->agent);
c->client = nta_outgoing_default(proxy->agent, proxy_response, c);
if (!c->client || !t->server)
return -1;
proxy->uri = nta_agent_contact(proxy->agent)->m_url;
proxy->lr_str = su_sprintf(proxy->home, "<" URL_PRINT_FORMAT ";lr>", URL_PRINT_ARGS(proxy->uri));
proxy->lr = sip_route_make(proxy->home, proxy->lr_str);
if (!proxy->lr)
return -1;
return 0;
}
static void
test_proxy_deinit(su_root_t *root, struct proxy *proxy)
{
struct proxy_tr *t;
while (proxy->transactions)
proxy_tr_destroy(proxy->transactions);
if ((t = proxy->stateless)) {
proxy->stateless = NULL;
proxy_tr_destroy(t);
}
while (proxy->domains)
domain_destroy(proxy->domains);
nta_agent_destroy(proxy->agent);
free(proxy->tags);
}
/* Create test proxy object */
struct proxy *test_proxy_create(su_root_t *root,
tag_type_t tag, tag_value_t value, ...)
{
struct proxy *p = su_home_new(sizeof *p);
if (p) {
ta_list ta;
p->magic = test_proxy_create;
p->parent = root;
ta_start(ta, tag, value);
p->tags = tl_llist(ta_tags(ta));
ta_end(ta);
if (su_clone_start(root,
p->clone,
p,
test_proxy_init,
test_proxy_deinit) == -1)
su_home_unref(p->home), p = NULL;
}
return p;
}
/* Destroy the proxy object */
void test_proxy_destroy(struct proxy *p)
{
if (p) {
su_clone_wait(p->parent, p->clone);
su_home_unref(p->home);
}
}
/* Return the proxy URI */
url_t const *test_proxy_uri(struct proxy const *p)
{
return p ? p->uri : NULL;
}
/* Return the proxy route URI */
char const *test_proxy_route_uri(struct proxy const *p,
sip_route_t const **return_route)
{
if (p == NULL)
return NULL;
if (return_route)
*return_route = p->lr;
return p->lr_str;
}
struct _set_logging {
struct proxy *p;
int logging;
};
static int _set_logging(void *_a)
{
struct _set_logging *a = _a;
return nta_agent_set_params(a->p->agent, TPTAG_LOG(a->logging), TAG_END());
}
void test_proxy_set_logging(struct proxy *p, int logging)
{
if (p) {
struct _set_logging a[1] = {{ p, logging }};
su_task_execute(su_clone_task(p->clone), _set_logging, a, NULL);
}
}
void test_proxy_domain_set_expiration(struct domain *d,
sip_time_t min_expires,
sip_time_t expires,
sip_time_t max_expires)
{
if (d) {
d->prefs.min_expires = min_expires;
d->prefs.expires = expires;
d->prefs.max_expires = max_expires;
}
}
void test_proxy_domain_get_expiration(struct domain *d,
sip_time_t *return_min_expires,
sip_time_t *return_expires,
sip_time_t *return_max_expires)
{
if (d) {
if (return_min_expires) *return_min_expires = d->prefs.min_expires;
if (return_expires) *return_expires = d->prefs.expires;
if (return_max_expires) *return_max_expires = d->prefs.max_expires;
}
}
void test_proxy_set_session_timer(struct proxy *p,
sip_time_t session_expires,
sip_time_t min_se)
{
if (p) {
p->prefs.session_expires = session_expires;
p->prefs.min_se = min_se;
}
}
void test_proxy_get_session_timer(struct proxy *p,
sip_time_t *return_session_expires,
sip_time_t *return_min_se)
{
if (p) {
if (return_session_expires)
*return_session_expires = p->prefs.session_expires;
if (return_min_se) *return_min_se = p->prefs.min_se;
}
}
void test_proxy_domain_set_outbound(struct domain *d,
int use_outbound)
{
if (d) {
d->prefs.outbound_tcp = use_outbound;
}
}
void test_proxy_domain_get_outbound(struct domain *d,
int *return_use_outbound)
{
if (d) {
if (return_use_outbound)
*return_use_outbound = d->prefs.outbound_tcp;
}
}
void test_proxy_domain_set_record_route(struct domain *d,
int use_record_route)
{
if (d) {
d->prefs.record_route = use_record_route;
}
}
void test_proxy_domain_get_record_route(struct domain *d,
int *return_use_record_route)
{
if (d) {
if (return_use_record_route)
*return_use_record_route = d->prefs.record_route;
}
}
int test_proxy_domain_set_authorize(struct domain *d,
char const *realm)
{
if (d) {
if (realm) {
realm = su_strdup(d->home, realm);
if (!realm)
return -1;
}
d->prefs.authorize = realm;
return 0;
}
return -1;
}
int test_proxy_domain_get_authorize(struct domain *d,
char const **return_realm)
{
if (d) {
if (return_realm) {
*return_realm = d->prefs.authorize;
return 0;
}
}
return -1;
}
int test_proxy_close_tports(struct proxy *p)
{
if (p) {
int retval = -EPROTO;
su_task_execute(su_clone_task(p->clone), close_tports, p, &retval);
if (retval < 0)
return errno = -retval, -1;
else
return 0;
}
return errno = EFAULT, -1;
}
/* ---------------------------------------------------------------------- */
struct domain *test_proxy_add_domain(struct proxy *p,
url_t const *uri,
tag_type_t tag, tag_value_t value, ...)
{
struct domain *d;
if (p == NULL || uri == NULL)
return NULL;
d = su_home_clone(p->home, sizeof *d);
if (d) {
ta_list ta;
int init = 0;
ta_start(ta, tag, value);
d->magic = domain_init;
d->proxy = p;
d->uri = url_hdup(d->home, uri);
d->tags = tl_adup(d->home, ta_args(ta));
d->prefs.min_expires = 300;
d->prefs.expires = 3600;
d->prefs.max_expires = 36000;
d->prefs.outbound_tcp = 0;
d->prefs.authorize = NULL;
if (d->uri && d->tags &&
!su_task_execute(su_clone_task(p->clone), _domain_init, d, &init)) {
if (init == 0)
/* OK */;
else
d = NULL;
}
else
su_home_unref(d->home);
}
return d;
}
static int _domain_init(void *_d)
{
return domain_init(_d);
}
static int domain_init(struct domain *d)
{
struct proxy *p = d->proxy;
url_t uri[1];
*uri = *d->uri;
d->auth = auth_mod_create(p->root, TAG_NEXT(d->tags));
/* Leg for URIs without userpart */
d->rleg = nta_leg_tcreate(d->proxy->agent,
domain_request,
(union proxy_or_domain *)d,
NTATAG_NO_DIALOG(1),
URLTAG_URL(uri),
TAG_END());
/* Leg for URIs with wildcard userpart */
uri->url_user = "%";
d->uleg = nta_leg_tcreate(d->proxy->agent,
domain_request,
(union proxy_or_domain *)d,
NTATAG_NO_DIALOG(1),
URLTAG_URL(uri),
TAG_END());
if (d->auth && d->rleg && d->uleg) {
domain_insert(&p->domains, d);
return 0;
}
domain_destroy(d);
return -1;
}
static void domain_destroy(struct domain *d)
{
while (d->entries)
registration_entry_destroy(d->entries);
nta_leg_destroy(d->rleg), d->rleg = NULL;
nta_leg_destroy(d->uleg), d->uleg = NULL;
auth_mod_destroy(d->auth), d->auth = NULL;
domain_remove(d);
su_home_unref(d->home);
}
/* ---------------------------------------------------------------------- */
static sip_contact_t *create_transport_contacts(struct proxy *p)
{
su_home_t *home = p->home;
sip_via_t *v;
sip_contact_t *retval = NULL, **mm = &retval;
if (!p->agent)
return NULL;
for (v = nta_agent_via(p->agent); v; v = v->v_next) {
char const *proto = v->v_protocol;
if (v->v_next &&
su_casematch(v->v_host, v->v_next->v_host) &&
str0cmp(v->v_port, v->v_next->v_port) == 0 &&
((proto == sip_transport_udp &&
v->v_next->v_protocol == sip_transport_tcp) ||
(proto == sip_transport_tcp &&
v->v_next->v_protocol == sip_transport_udp)))
/* We have udp/tcp pair, insert URL without tport parameter */
*mm = sip_contact_create_from_via_with_transport(home, v, NULL, NULL);
if (*mm) mm = &(*mm)->m_next;
*mm = sip_contact_create_from_via_with_transport(home, v, NULL, proto);
if (*mm) mm = &(*mm)->m_next;
}
return retval;
}
/* ---------------------------------------------------------------------- */
static int proxy_tr_with(struct proxy *proxy,
struct domain *domain,
nta_incoming_t *irq,
sip_t const *sip,
int (*process)(struct proxy_tr *));
static int proxy_transaction(struct proxy_tr *t);
static int respond_transaction(struct proxy_tr *t,
int status, char const *phrase,
tag_type_t tag, tag_value_t value,
...);
static int validate_transaction(struct proxy_tr *t);
static int originating_transaction(struct proxy_tr *t);
static int challenge_transaction(struct proxy_tr *t);
static int session_timers(struct proxy_tr *t);
static int incoming_transaction(struct proxy_tr *t);
static int target_transaction(struct proxy_tr *t,
url_t const *target,
tport_t *tport);
static int process_register(struct proxy_tr *t);
static int process_options(struct proxy_tr *t);
static int proxy_ack_cancel(struct proxy_tr *t,
nta_incoming_t *irq,
sip_t const *sip);
static struct registration_entry *
registration_entry_find(struct domain const *domain, url_t const *uri);
static int proxy_request(union proxy_or_domain *pod,
nta_leg_t *leg,
nta_incoming_t *irq,
sip_t const *sip)
{
assert(pod->proxy->magic = test_proxy_init);
return proxy_tr_with(pod->proxy, NULL, irq, sip, proxy_transaction);
}
static int domain_request(union proxy_or_domain *pod,
nta_leg_t *leg,
nta_incoming_t *irq,
sip_t const *sip)
{
int (*process)(struct proxy_tr *) = NULL;
sip_method_t method = sip->sip_request->rq_method;
assert(pod->domain->magic = domain_init);
if (leg == pod->domain->uleg)
process = proxy_transaction;
else if (method == sip_method_register)
process = process_register;
else if (method == sip_method_options)
process = process_options;
if (process == NULL)
return 501; /* Not implemented */
return proxy_tr_with(pod->domain->proxy, pod->domain, irq, sip, process);
}
static int proxy_tr_with(struct proxy *proxy,
struct domain *domain,
nta_incoming_t *irq,
sip_t const *sip,
int (*process)(struct proxy_tr *))
{
struct proxy_tr *t = NULL;
int status = 500;
assert(proxy->magic = test_proxy_init);
t = proxy_tr_new(proxy);
if (t) {
t->proxy = proxy, t->domain = domain, t->server = irq;
t->msg = nta_incoming_getrequest(irq);
t->sip = sip_object(t->msg);
t->method = sip->sip_request->rq_method;
t->method_name = sip->sip_request->rq_method_name;
t->target = sip->sip_request->rq_url;
t->now = nta_incoming_received(irq, NULL);
if (t->method != sip_method_ack && t->method != sip_method_cancel)
nta_incoming_bind(irq, proxy_ack_cancel, t);
if (domain && domain->prefs.record_route)
t->rr = 1;
if (process(t) < 200)
return 0;
proxy_tr_destroy(t);
}
else {
nta_incoming_treply(irq, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
}
return status;
}
/** Forward request */
static int proxy_transaction(struct proxy_tr *t)
{
if (originating_transaction(t))
return t->status;
if (validate_transaction(t))
return t->status;
if (session_timers(t))
return t->status;
if (t->domain)
return incoming_transaction(t);
return target_transaction(t, t->target, NULL);
}
static int respond_transaction(struct proxy_tr *t,
int status, char const *phrase,
tag_type_t tag, tag_value_t value,
...)
{
ta_list ta;
void *info = NULL, *response = NULL;
ta_start(ta, tag, value);
if (t->as)
info = t->as->as_info, response = t->as->as_response;
if (nta_incoming_treply(t->server, t->status = status, phrase,
SIPTAG_HEADER(info),
SIPTAG_HEADER(response),
ta_tags(ta)) < 0)
t->status = status = 500;
ta_end(ta);
return status;
}
static int originating_transaction(struct proxy_tr *t)
{
struct domain *o;
char const *host;
host = t->sip->sip_from->a_url->url_host;
if (!host)
return 0;
for (o = t->proxy->domains; o; o = o->next)
if (su_casematch(host, o->uri->url_host))
break;
t->origin = o;
if (o && o->auth && o->prefs.authorize) {
t->am = o->auth;
t->realm = o->prefs.authorize;
t->use_auth = 407;
}
if (o && o->prefs.record_route)
t->rr = 1;
return 0;
}
static int validate_transaction(struct proxy_tr *t)
{
sip_max_forwards_t *mf;
mf = t->sip->sip_max_forwards;
if (mf && mf->mf_count <= 1) {
if (t->method == sip_method_options)
return process_options(t);
return respond_transaction(t, SIP_483_TOO_MANY_HOPS, TAG_END());
}
/* Remove our routes */
while (t->sip->sip_route &&
url_has_param(t->sip->sip_route->r_url, "lr") &&
(url_cmp(t->proxy->lr->r_url, t->sip->sip_route->r_url) == 0 ||
url_cmp(t->proxy->rr_uri, t->sip->sip_route->r_url) == 0)) {
sip_route_remove(t->msg, t->sip);
/* add record-route also to the forwarded request */
}
if (t->use_auth)
return challenge_transaction(t);
return 0;
}
static int session_timers(struct proxy_tr *t)
{
sip_t *sip = t->sip;
sip_session_expires_t *x = NULL, x0[1];
sip_min_se_t *min_se = NULL, min_se0[1];
char const *require = NULL;
if (t->method == sip_method_invite) {
if (t->proxy->prefs.min_se) {
if (!sip->sip_min_se ||
sip->sip_min_se->min_delta < t->proxy->prefs.min_se) {
min_se = sip_min_se_init(min_se0);
min_se->min_delta = t->proxy->prefs.min_se;
}
if (sip->sip_session_expires
&& sip->sip_session_expires->x_delta < t->proxy->prefs.min_se
&& sip_has_supported(sip->sip_supported, "timer")) {
if (min_se == NULL)
min_se = sip->sip_min_se; assert(min_se);
return respond_transaction(t, SIP_422_SESSION_TIMER_TOO_SMALL,
SIPTAG_MIN_SE(min_se),
TAG_END());
}
}
if (t->proxy->prefs.session_expires) {
if (!sip->sip_session_expires ||
sip->sip_session_expires->x_delta > t->proxy->prefs.session_expires) {
x = sip_session_expires_init(x0);
x->x_delta = t->proxy->prefs.session_expires;
if (!sip_has_supported(sip->sip_supported, "timer"))
require = "timer";
}
}
if (x || min_se || require)
sip_add_tl(t->msg, t->sip,
SIPTAG_REQUIRE_STR(require),
SIPTAG_MIN_SE(min_se),
SIPTAG_SESSION_EXPIRES(x),
TAG_END());
}
return 0;
}
static int incoming_transaction(struct proxy_tr *t)
{
struct registration_entry *e;
struct binding *b;
#if 0
if (sip->sip_request->rq_method == sip_method_register)
return process_register(proxy, irq, sip);
#endif
t->entry = e = registration_entry_find(t->domain, t->target);
if (e == NULL)
return respond_transaction(t, SIP_404_NOT_FOUND, TAG_END());
for (b = e->bindings; b; b = b->next) {
if (binding_is_active(b))
target_transaction(t, b->contact->m_url, b->tport);
if (t->clients) /* XXX - enable forking */
break;
}
if (t->clients != NULL)
return 0;
return respond_transaction(t, SIP_480_TEMPORARILY_UNAVAILABLE, TAG_END());
}
static int target_transaction(struct proxy_tr *t,
url_t const *target,
tport_t *tport)
{
struct client_tr *c = su_zalloc(t->proxy->home, sizeof *c);
int stateless = t->method == sip_method_ack;
if (c == NULL)
return 500;
c->t = t;
c->msg = msg_copy(t->msg);
c->sip = sip_object(c->msg);
if (c->msg)
c->rq = sip_request_create(msg_home(c->msg),
t->method, t->method_name,