test_proxy.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE test_proxy.c
  25. * @brief Extremely simple proxy and registrar for testing nua
  26. *
  27. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  28. *
  29. * @date Created: Thu Nov 3 22:49:46 EET 2005
  30. */
  31. #include "config.h"
  32. #include <string.h>
  33. struct proxy;
  34. struct domain;
  35. union proxy_or_domain;
  36. struct proxy_tr;
  37. struct client_tr;
  38. struct registration_entry;
  39. struct binding;
  40. #define SU_ROOT_MAGIC_T struct proxy
  41. #define NTA_LEG_MAGIC_T union proxy_or_domain
  42. #define NTA_OUTGOING_MAGIC_T struct client_tr
  43. #define NTA_INCOMING_MAGIC_T struct proxy_tr
  44. #define SU_TIMER_ARG_T struct proxy_tr
  45. #include <sofia-sip/su_wait.h>
  46. #include <sofia-sip/nta.h>
  47. #include <sofia-sip/sip_header.h>
  48. #include <sofia-sip/sip_status.h>
  49. #include <sofia-sip/sip_util.h>
  50. #include <sofia-sip/auth_module.h>
  51. #include <sofia-sip/su_tagarg.h>
  52. #include <sofia-sip/msg_addr.h>
  53. #include <sofia-sip/hostdomain.h>
  54. #include <sofia-sip/tport.h>
  55. #include <sofia-sip/nta_tport.h>
  56. #include <stdlib.h>
  57. #include <assert.h>
  58. #define LIST_PROTOS(STORAGE, PREFIX, T) \
  59. STORAGE void PREFIX ##_insert(T **list, T *node), \
  60. PREFIX ##_remove(T *node)
  61. #define LIST_BODIES(STORAGE, PREFIX, T, NEXT, PREV) \
  62. STORAGE void PREFIX ##_insert(T **list, T *node) \
  63. { \
  64. if ((node->NEXT = *list)) { \
  65. node->PREV = node->NEXT->PREV; \
  66. node->NEXT->PREV = &node->NEXT; \
  67. } \
  68. else \
  69. node->PREV = list; \
  70. *list = node; \
  71. } \
  72. STORAGE void PREFIX ##_remove(T *node) \
  73. { \
  74. if (node->PREV) \
  75. if ((*node->PREV = node->NEXT)) \
  76. node->NEXT->PREV = node->PREV; \
  77. node->PREV = NULL; \
  78. } \
  79. extern int LIST_DUMMY_VARIABLE
  80. #include "test_proxy.h"
  81. #include <sofia-sip/auth_module.h>
  82. struct proxy {
  83. su_home_t home[1];
  84. void *magic;
  85. su_root_t *parent;
  86. su_clone_r clone;
  87. tagi_t *tags;
  88. su_root_t *root;
  89. nta_agent_t *agent;
  90. url_t const *uri;
  91. sip_route_t *lr;
  92. char const *lr_str;
  93. url_t const *rr_uri;
  94. nta_leg_t *defleg;
  95. sip_contact_t *transport_contacts;
  96. struct proxy_tr *stateless;
  97. struct proxy_tr *transactions;
  98. struct proxy_tr *invite_waiting;
  99. struct domain *domains;
  100. struct {
  101. unsigned t1x64;
  102. sip_time_t session_expires, min_se;
  103. } prefs;
  104. };
  105. struct domain {
  106. su_home_t home[1];
  107. void *magic;
  108. struct proxy *proxy;
  109. struct domain *next, **prev;
  110. url_t *uri;
  111. nta_leg_t *rleg, *uleg;
  112. auth_mod_t *auth;
  113. struct registration_entry *entries;
  114. struct {
  115. sip_time_t min_expires, expires, max_expires;
  116. int outbound_tcp; /**< Use inbound TCP connection as outbound */
  117. char const *authorize; /**< Authorization realm to use */
  118. int record_route;
  119. } prefs;
  120. tagi_t *tags;
  121. };
  122. LIST_PROTOS(static, domain, struct domain);
  123. static int _domain_init(void *_d);
  124. static int domain_init(struct domain *domain);
  125. static void domain_destroy(struct domain *domain);
  126. LIST_BODIES(static, domain, struct domain, next, prev);
  127. LIST_PROTOS(static, registration_entry, struct registration_entry);
  128. static struct registration_entry *registration_entry_new(struct domain *,
  129. url_t const *);
  130. static void registration_entry_destroy(struct registration_entry *e);
  131. struct registration_entry
  132. {
  133. struct registration_entry *next, **prev;
  134. struct domain *domain; /* backpointer */
  135. url_t *aor; /* address-of-record */
  136. struct binding *bindings; /* list of bindings */
  137. sip_contact_t *contacts;
  138. };
  139. struct binding
  140. {
  141. struct binding *next, **prev;
  142. sip_contact_t *contact; /* binding */
  143. sip_time_t registered, expires; /* When registered and when expires */
  144. sip_call_id_t *call_id;
  145. uint32_t cseq;
  146. tport_t *tport; /**< Reference to tport */
  147. };
  148. static struct binding *binding_new(su_home_t *home,
  149. sip_contact_t *contact,
  150. tport_t *tport,
  151. sip_call_id_t const *call_id,
  152. uint32_t cseq,
  153. sip_time_t registered,
  154. sip_time_t expires);
  155. static void binding_destroy(su_home_t *home, struct binding *b);
  156. static int binding_is_active(struct binding const *b)
  157. {
  158. return
  159. b->expires > sip_now() &&
  160. (b->tport == NULL || tport_is_clear_to_send(b->tport));
  161. }
  162. LIST_PROTOS(static, proxy_tr, struct proxy_tr);
  163. struct proxy_tr *proxy_tr_new(struct proxy *);
  164. static void proxy_tr_timeout(struct proxy_tr *t);
  165. static void proxy_tr_destroy(struct proxy_tr *t);
  166. struct proxy_tr
  167. {
  168. struct proxy_tr *next, **prev;
  169. struct proxy *proxy; /**< Backpointer to proxy */
  170. struct domain *origin; /**< Originating domain */
  171. struct domain *domain; /**< Destination domain */
  172. sip_time_t now; /**< When received */
  173. nta_incoming_t *server; /**< server transaction */
  174. msg_t *msg; /**< request message */
  175. sip_t *sip; /**< request headers */
  176. sip_method_t method; /**< request method */
  177. char const *method_name;
  178. int status; /**< best status */
  179. url_t *target; /**< request-URI */
  180. struct client_tr *clients; /**< Client transactions */
  181. struct registration_entry *entry;
  182. /**< Registration entry */
  183. auth_mod_t *am; /**< Authentication module */
  184. auth_status_t *as; /**< Authentication status */
  185. char const *realm; /**< Authentication realm to use */
  186. unsigned use_auth; /**< Authentication method (401/407) to use */
  187. su_timer_t *timer; /**< Timer */
  188. unsigned rr:1;
  189. };
  190. LIST_PROTOS(static, client_tr, struct client_tr);
  191. struct client_tr
  192. {
  193. struct client_tr *next, **prev;
  194. struct proxy_tr *t;
  195. int status; /* response status */
  196. sip_request_t *rq; /* request line */
  197. msg_t *msg; /* request message */
  198. sip_t *sip; /* request headers */
  199. nta_outgoing_t *client; /* transaction */
  200. };
  201. LIST_BODIES(static, client_tr, struct client_tr, next, prev);
  202. static sip_contact_t *create_transport_contacts(struct proxy *p);
  203. union proxy_or_domain { struct proxy proxy[1]; struct domain domain[1]; };
  204. static int proxy_request(union proxy_or_domain *proxy,
  205. nta_leg_t *leg,
  206. nta_incoming_t *irq,
  207. sip_t const *sip);
  208. static int domain_request(union proxy_or_domain *domain,
  209. nta_leg_t *leg,
  210. nta_incoming_t *irq,
  211. sip_t const *sip);
  212. static int proxy_response(struct client_tr *client,
  213. nta_outgoing_t *orq,
  214. sip_t const *sip);
  215. static int close_tports(void *proxy);
  216. static auth_challenger_t registrar_challenger[1];
  217. static auth_challenger_t proxy_challenger[1];
  218. /* Proxy entry point */
  219. static int
  220. test_proxy_init(su_root_t *root, struct proxy *proxy)
  221. {
  222. struct proxy_tr *t;
  223. struct client_tr *c;
  224. auth_challenger_t _proxy_challenger[1] =
  225. {{
  226. SIP_407_PROXY_AUTH_REQUIRED,
  227. sip_proxy_authenticate_class,
  228. sip_proxy_authentication_info_class
  229. }};
  230. auth_challenger_t _registrar_challenger[1] =
  231. {{
  232. SIP_401_UNAUTHORIZED,
  233. sip_www_authenticate_class,
  234. sip_authentication_info_class
  235. }};
  236. *proxy_challenger = *_proxy_challenger;
  237. *registrar_challenger = *_registrar_challenger;
  238. proxy->root = root;
  239. proxy->agent = nta_agent_create(root,
  240. URL_STRING_MAKE("sip:0.0.0.0:*"),
  241. NULL, NULL,
  242. NTATAG_UA(0),
  243. NTATAG_CANCEL_487(0),
  244. NTATAG_SERVER_RPORT(1),
  245. NTATAG_CLIENT_RPORT(1),
  246. TAG_NEXT(proxy->tags));
  247. if (!proxy->agent)
  248. return -1;
  249. proxy->transport_contacts = create_transport_contacts(proxy);
  250. proxy->defleg = nta_leg_tcreate(proxy->agent,
  251. proxy_request,
  252. (union proxy_or_domain *)proxy,
  253. NTATAG_NO_DIALOG(1),
  254. TAG_END());
  255. proxy->prefs.session_expires = 180;
  256. proxy->prefs.min_se = 90;
  257. proxy->prefs.t1x64 = 64 * 500;
  258. nta_agent_get_params(proxy->agent,
  259. NTATAG_SIP_T1X64_REF(proxy->prefs.t1x64),
  260. TAG_END());
  261. if (!proxy->defleg)
  262. return -1;
  263. /* if (!proxy->example_net || !proxy->example_org || !proxy->example_com)
  264. return -1; */
  265. /* Create stateless client */
  266. t = su_zalloc(proxy->home, sizeof *t);
  267. c = su_zalloc(proxy->home, sizeof *c);
  268. if (!t || !c)
  269. return -1;
  270. proxy->stateless = t;
  271. t->proxy = proxy;
  272. c->t = t, client_tr_insert(&t->clients, c);
  273. t->server = nta_incoming_default(proxy->agent);
  274. c->client = nta_outgoing_default(proxy->agent, proxy_response, c);
  275. if (!c->client || !t->server)
  276. return -1;
  277. proxy->uri = nta_agent_contact(proxy->agent)->m_url;
  278. proxy->lr_str = su_sprintf(proxy->home, "<" URL_PRINT_FORMAT ";lr>", URL_PRINT_ARGS(proxy->uri));
  279. proxy->lr = sip_route_make(proxy->home, proxy->lr_str);
  280. if (!proxy->lr)
  281. return -1;
  282. return 0;
  283. }
  284. static void
  285. test_proxy_deinit(su_root_t *root, struct proxy *proxy)
  286. {
  287. struct proxy_tr *t;
  288. while (proxy->transactions)
  289. proxy_tr_destroy(proxy->transactions);
  290. if ((t = proxy->stateless)) {
  291. proxy->stateless = NULL;
  292. proxy_tr_destroy(t);
  293. }
  294. while (proxy->domains)
  295. domain_destroy(proxy->domains);
  296. nta_agent_destroy(proxy->agent);
  297. free(proxy->tags);
  298. }
  299. /* Create test proxy object */
  300. struct proxy *test_proxy_create(su_root_t *root,
  301. tag_type_t tag, tag_value_t value, ...)
  302. {
  303. struct proxy *p = su_home_new(sizeof *p);
  304. if (p) {
  305. ta_list ta;
  306. p->magic = test_proxy_create;
  307. p->parent = root;
  308. ta_start(ta, tag, value);
  309. p->tags = tl_llist(ta_tags(ta));
  310. ta_end(ta);
  311. if (su_clone_start(root,
  312. p->clone,
  313. p,
  314. test_proxy_init,
  315. test_proxy_deinit) == -1)
  316. su_home_unref(p->home), p = NULL;
  317. }
  318. return p;
  319. }
  320. /* Destroy the proxy object */
  321. void test_proxy_destroy(struct proxy *p)
  322. {
  323. if (p) {
  324. su_clone_wait(p->parent, p->clone);
  325. su_home_unref(p->home);
  326. }
  327. }
  328. /* Return the proxy URI */
  329. url_t const *test_proxy_uri(struct proxy const *p)
  330. {
  331. return p ? p->uri : NULL;
  332. }
  333. /* Return the proxy route URI */
  334. char const *test_proxy_route_uri(struct proxy const *p,
  335. sip_route_t const **return_route)
  336. {
  337. if (p == NULL)
  338. return NULL;
  339. if (return_route)
  340. *return_route = p->lr;
  341. return p->lr_str;
  342. }
  343. struct _set_logging {
  344. struct proxy *p;
  345. int logging;
  346. };
  347. static int _set_logging(void *_a)
  348. {
  349. struct _set_logging *a = _a;
  350. return nta_agent_set_params(a->p->agent, TPTAG_LOG(a->logging), TAG_END());
  351. }
  352. void test_proxy_set_logging(struct proxy *p, int logging)
  353. {
  354. if (p) {
  355. struct _set_logging a[1] = {{ p, logging }};
  356. su_task_execute(su_clone_task(p->clone), _set_logging, a, NULL);
  357. }
  358. }
  359. void test_proxy_domain_set_expiration(struct domain *d,
  360. sip_time_t min_expires,
  361. sip_time_t expires,
  362. sip_time_t max_expires)
  363. {
  364. if (d) {
  365. d->prefs.min_expires = min_expires;
  366. d->prefs.expires = expires;
  367. d->prefs.max_expires = max_expires;
  368. }
  369. }
  370. void test_proxy_domain_get_expiration(struct domain *d,
  371. sip_time_t *return_min_expires,
  372. sip_time_t *return_expires,
  373. sip_time_t *return_max_expires)
  374. {
  375. if (d) {
  376. if (return_min_expires) *return_min_expires = d->prefs.min_expires;
  377. if (return_expires) *return_expires = d->prefs.expires;
  378. if (return_max_expires) *return_max_expires = d->prefs.max_expires;
  379. }
  380. }
  381. void test_proxy_set_session_timer(struct proxy *p,
  382. sip_time_t session_expires,
  383. sip_time_t min_se)
  384. {
  385. if (p) {
  386. p->prefs.session_expires = session_expires;
  387. p->prefs.min_se = min_se;
  388. }
  389. }
  390. void test_proxy_get_session_timer(struct proxy *p,
  391. sip_time_t *return_session_expires,
  392. sip_time_t *return_min_se)
  393. {
  394. if (p) {
  395. if (return_session_expires)
  396. *return_session_expires = p->prefs.session_expires;
  397. if (return_min_se) *return_min_se = p->prefs.min_se;
  398. }
  399. }
  400. void test_proxy_domain_set_outbound(struct domain *d,
  401. int use_outbound)
  402. {
  403. if (d) {
  404. d->prefs.outbound_tcp = use_outbound;
  405. }
  406. }
  407. void test_proxy_domain_get_outbound(struct domain *d,
  408. int *return_use_outbound)
  409. {
  410. if (d) {
  411. if (return_use_outbound)
  412. *return_use_outbound = d->prefs.outbound_tcp;
  413. }
  414. }
  415. void test_proxy_domain_set_record_route(struct domain *d,
  416. int use_record_route)
  417. {
  418. if (d) {
  419. d->prefs.record_route = use_record_route;
  420. }
  421. }
  422. void test_proxy_domain_get_record_route(struct domain *d,
  423. int *return_use_record_route)
  424. {
  425. if (d) {
  426. if (return_use_record_route)
  427. *return_use_record_route = d->prefs.record_route;
  428. }
  429. }
  430. int test_proxy_domain_set_authorize(struct domain *d,
  431. char const *realm)
  432. {
  433. if (d) {
  434. if (realm) {
  435. realm = su_strdup(d->home, realm);
  436. if (!realm)
  437. return -1;
  438. }
  439. d->prefs.authorize = realm;
  440. return 0;
  441. }
  442. return -1;
  443. }
  444. int test_proxy_domain_get_authorize(struct domain *d,
  445. char const **return_realm)
  446. {
  447. if (d) {
  448. if (return_realm) {
  449. *return_realm = d->prefs.authorize;
  450. return 0;
  451. }
  452. }
  453. return -1;
  454. }
  455. int test_proxy_close_tports(struct proxy *p)
  456. {
  457. if (p) {
  458. int retval = -EPROTO;
  459. su_task_execute(su_clone_task(p->clone), close_tports, p, &retval);
  460. if (retval < 0)
  461. return errno = -retval, -1;
  462. else
  463. return 0;
  464. }
  465. return errno = EFAULT, -1;
  466. }
  467. /* ---------------------------------------------------------------------- */
  468. struct domain *test_proxy_add_domain(struct proxy *p,
  469. url_t const *uri,
  470. tag_type_t tag, tag_value_t value, ...)
  471. {
  472. struct domain *d;
  473. if (p == NULL || uri == NULL)
  474. return NULL;
  475. d = su_home_clone(p->home, sizeof *d);
  476. if (d) {
  477. ta_list ta;
  478. int init = 0;
  479. ta_start(ta, tag, value);
  480. d->magic = domain_init;
  481. d->proxy = p;
  482. d->uri = url_hdup(d->home, uri);
  483. d->tags = tl_adup(d->home, ta_args(ta));
  484. d->prefs.min_expires = 300;
  485. d->prefs.expires = 3600;
  486. d->prefs.max_expires = 36000;
  487. d->prefs.outbound_tcp = 0;
  488. d->prefs.authorize = NULL;
  489. if (d->uri && d->tags &&
  490. !su_task_execute(su_clone_task(p->clone), _domain_init, d, &init)) {
  491. if (init == 0)
  492. /* OK */;
  493. else
  494. d = NULL;
  495. }
  496. else
  497. su_home_unref(d->home);
  498. }
  499. return d;
  500. }
  501. static int _domain_init(void *_d)
  502. {
  503. return domain_init(_d);
  504. }
  505. static int domain_init(struct domain *d)
  506. {
  507. struct proxy *p = d->proxy;
  508. url_t uri[1];
  509. *uri = *d->uri;
  510. d->auth = auth_mod_create(p->root, TAG_NEXT(d->tags));
  511. /* Leg for URIs without userpart */
  512. d->rleg = nta_leg_tcreate(d->proxy->agent,
  513. domain_request,
  514. (union proxy_or_domain *)d,
  515. NTATAG_NO_DIALOG(1),
  516. URLTAG_URL(uri),
  517. TAG_END());
  518. /* Leg for URIs with wildcard userpart */
  519. uri->url_user = "%";
  520. d->uleg = nta_leg_tcreate(d->proxy->agent,
  521. domain_request,
  522. (union proxy_or_domain *)d,
  523. NTATAG_NO_DIALOG(1),
  524. URLTAG_URL(uri),
  525. TAG_END());
  526. if (d->auth && d->rleg && d->uleg) {
  527. domain_insert(&p->domains, d);
  528. return 0;
  529. }
  530. domain_destroy(d);
  531. return -1;
  532. }
  533. static void domain_destroy(struct domain *d)
  534. {
  535. while (d->entries)
  536. registration_entry_destroy(d->entries);
  537. nta_leg_destroy(d->rleg), d->rleg = NULL;
  538. nta_leg_destroy(d->uleg), d->uleg = NULL;
  539. auth_mod_destroy(d->auth), d->auth = NULL;
  540. domain_remove(d);
  541. su_home_unref(d->home);
  542. }
  543. /* ---------------------------------------------------------------------- */
  544. static sip_contact_t *create_transport_contacts(struct proxy *p)
  545. {
  546. su_home_t *home = p->home;
  547. sip_via_t *v;
  548. sip_contact_t *retval = NULL, **mm = &retval;
  549. if (!p->agent)
  550. return NULL;
  551. for (v = nta_agent_via(p->agent); v; v = v->v_next) {
  552. char const *proto = v->v_protocol;
  553. if (v->v_next &&
  554. su_casematch(v->v_host, v->v_next->v_host) &&
  555. str0cmp(v->v_port, v->v_next->v_port) == 0 &&
  556. ((proto == sip_transport_udp &&
  557. v->v_next->v_protocol == sip_transport_tcp) ||
  558. (proto == sip_transport_tcp &&
  559. v->v_next->v_protocol == sip_transport_udp)))
  560. /* We have udp/tcp pair, insert URL without tport parameter */
  561. *mm = sip_contact_create_from_via_with_transport(home, v, NULL, NULL);
  562. if (*mm) mm = &(*mm)->m_next;
  563. *mm = sip_contact_create_from_via_with_transport(home, v, NULL, proto);
  564. if (*mm) mm = &(*mm)->m_next;
  565. }
  566. return retval;
  567. }
  568. /* ---------------------------------------------------------------------- */
  569. static int proxy_tr_with(struct proxy *proxy,
  570. struct domain *domain,
  571. nta_incoming_t *irq,
  572. sip_t const *sip,
  573. int (*process)(struct proxy_tr *));
  574. static int proxy_transaction(struct proxy_tr *t);
  575. static int respond_transaction(struct proxy_tr *t,
  576. int status, char const *phrase,
  577. tag_type_t tag, tag_value_t value,
  578. ...);
  579. static int validate_transaction(struct proxy_tr *t);
  580. static int originating_transaction(struct proxy_tr *t);
  581. static int challenge_transaction(struct proxy_tr *t);
  582. static int session_timers(struct proxy_tr *t);
  583. static int incoming_transaction(struct proxy_tr *t);
  584. static int target_transaction(struct proxy_tr *t,
  585. url_t const *target,
  586. tport_t *tport);
  587. static int process_register(struct proxy_tr *t);
  588. static int process_options(struct proxy_tr *t);
  589. static int proxy_ack_cancel(struct proxy_tr *t,
  590. nta_incoming_t *irq,
  591. sip_t const *sip);
  592. static struct registration_entry *
  593. registration_entry_find(struct domain const *domain, url_t const *uri);
  594. static int proxy_request(union proxy_or_domain *pod,
  595. nta_leg_t *leg,
  596. nta_incoming_t *irq,
  597. sip_t const *sip)
  598. {
  599. assert(pod->proxy->magic = test_proxy_init);
  600. return proxy_tr_with(pod->proxy, NULL, irq, sip, proxy_transaction);
  601. }
  602. static int domain_request(union proxy_or_domain *pod,
  603. nta_leg_t *leg,
  604. nta_incoming_t *irq,
  605. sip_t const *sip)
  606. {
  607. int (*process)(struct proxy_tr *) = NULL;
  608. sip_method_t method = sip->sip_request->rq_method;
  609. assert(pod->domain->magic = domain_init);
  610. if (leg == pod->domain->uleg)
  611. process = proxy_transaction;
  612. else if (method == sip_method_register)
  613. process = process_register;
  614. else if (method == sip_method_options)
  615. process = process_options;
  616. if (process == NULL)
  617. return 501; /* Not implemented */
  618. return proxy_tr_with(pod->domain->proxy, pod->domain, irq, sip, process);
  619. }
  620. static int proxy_tr_with(struct proxy *proxy,
  621. struct domain *domain,
  622. nta_incoming_t *irq,
  623. sip_t const *sip,
  624. int (*process)(struct proxy_tr *))
  625. {
  626. struct proxy_tr *t = NULL;
  627. int status = 500;
  628. assert(proxy->magic = test_proxy_init);
  629. t = proxy_tr_new(proxy);
  630. if (t) {
  631. t->proxy = proxy, t->domain = domain, t->server = irq;
  632. t->msg = nta_incoming_getrequest(irq);
  633. t->sip = sip_object(t->msg);
  634. t->method = sip->sip_request->rq_method;
  635. t->method_name = sip->sip_request->rq_method_name;
  636. t->target = sip->sip_request->rq_url;
  637. t->now = nta_incoming_received(irq, NULL);
  638. if (t->method != sip_method_ack && t->method != sip_method_cancel)
  639. nta_incoming_bind(irq, proxy_ack_cancel, t);
  640. if (domain && domain->prefs.record_route)
  641. t->rr = 1;
  642. if (process(t) < 200)
  643. return 0;
  644. proxy_tr_destroy(t);
  645. }
  646. else {
  647. nta_incoming_treply(irq, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
  648. }
  649. return status;
  650. }
  651. /** Forward request */
  652. static int proxy_transaction(struct proxy_tr *t)
  653. {
  654. if (originating_transaction(t))
  655. return t->status;
  656. if (validate_transaction(t))
  657. return t->status;
  658. if (session_timers(t))
  659. return t->status;
  660. if (t->domain)
  661. return incoming_transaction(t);
  662. return target_transaction(t, t->target, NULL);
  663. }
  664. static int respond_transaction(struct proxy_tr *t,
  665. int status, char const *phrase,
  666. tag_type_t tag, tag_value_t value,
  667. ...)
  668. {
  669. ta_list ta;
  670. void *info = NULL, *response = NULL;
  671. ta_start(ta, tag, value);
  672. if (t->as)
  673. info = t->as->as_info, response = t->as->as_response;
  674. if (nta_incoming_treply(t->server, t->status = status, phrase,
  675. SIPTAG_HEADER(info),
  676. SIPTAG_HEADER(response),
  677. ta_tags(ta)) < 0)
  678. t->status = status = 500;
  679. ta_end(ta);
  680. return status;
  681. }
  682. static int originating_transaction(struct proxy_tr *t)
  683. {
  684. struct domain *o;
  685. char const *host;
  686. host = t->sip->sip_from->a_url->url_host;
  687. if (!host)
  688. return 0;
  689. for (o = t->proxy->domains; o; o = o->next)
  690. if (su_casematch(host, o->uri->url_host))
  691. break;
  692. t->origin = o;
  693. if (o && o->auth && o->prefs.authorize) {
  694. t->am = o->auth;
  695. t->realm = o->prefs.authorize;
  696. t->use_auth = 407;
  697. }
  698. if (o && o->prefs.record_route)
  699. t->rr = 1;
  700. return 0;
  701. }
  702. static int validate_transaction(struct proxy_tr *t)
  703. {
  704. sip_max_forwards_t *mf;
  705. mf = t->sip->sip_max_forwards;
  706. if (mf && mf->mf_count <= 1) {
  707. if (t->method == sip_method_options)
  708. return process_options(t);
  709. return respond_transaction(t, SIP_483_TOO_MANY_HOPS, TAG_END());
  710. }
  711. /* Remove our routes */
  712. while (t->sip->sip_route &&
  713. url_has_param(t->sip->sip_route->r_url, "lr") &&
  714. (url_cmp(t->proxy->lr->r_url, t->sip->sip_route->r_url) == 0 ||
  715. url_cmp(t->proxy->rr_uri, t->sip->sip_route->r_url) == 0)) {
  716. sip_route_remove(t->msg, t->sip);
  717. /* add record-route also to the forwarded request */
  718. }
  719. if (t->use_auth)
  720. return challenge_transaction(t);
  721. return 0;
  722. }
  723. static int session_timers(struct proxy_tr *t)
  724. {
  725. sip_t *sip = t->sip;
  726. sip_session_expires_t *x = NULL, x0[1];
  727. sip_min_se_t *min_se = NULL, min_se0[1];
  728. char const *require = NULL;
  729. if (t->method == sip_method_invite) {
  730. if (t->proxy->prefs.min_se) {
  731. if (!sip->sip_min_se ||
  732. sip->sip_min_se->min_delta < t->proxy->prefs.min_se) {
  733. min_se = sip_min_se_init(min_se0);
  734. min_se->min_delta = t->proxy->prefs.min_se;
  735. }
  736. if (sip->sip_session_expires
  737. && sip->sip_session_expires->x_delta < t->proxy->prefs.min_se
  738. && sip_has_supported(sip->sip_supported, "timer")) {
  739. if (min_se == NULL)
  740. min_se = sip->sip_min_se; assert(min_se);
  741. return respond_transaction(t, SIP_422_SESSION_TIMER_TOO_SMALL,
  742. SIPTAG_MIN_SE(min_se),
  743. TAG_END());
  744. }
  745. }
  746. if (t->proxy->prefs.session_expires) {
  747. if (!sip->sip_session_expires ||
  748. sip->sip_session_expires->x_delta > t->proxy->prefs.session_expires) {
  749. x = sip_session_expires_init(x0);
  750. x->x_delta = t->proxy->prefs.session_expires;
  751. if (!sip_has_supported(sip->sip_supported, "timer"))
  752. require = "timer";
  753. }
  754. }
  755. if (x || min_se || require)
  756. sip_add_tl(t->msg, t->sip,
  757. SIPTAG_REQUIRE_STR(require),
  758. SIPTAG_MIN_SE(min_se),
  759. SIPTAG_SESSION_EXPIRES(x),
  760. TAG_END());
  761. }
  762. return 0;
  763. }
  764. static int incoming_transaction(struct proxy_tr *t)
  765. {
  766. struct registration_entry *e;
  767. struct binding *b;
  768. #if 0
  769. if (sip->sip_request->rq_method == sip_method_register)
  770. return process_register(proxy, irq, sip);
  771. #endif
  772. t->entry = e = registration_entry_find(t->domain, t->target);
  773. if (e == NULL)
  774. return respond_transaction(t, SIP_404_NOT_FOUND, TAG_END());
  775. for (b = e->bindings; b; b = b->next) {
  776. if (binding_is_active(b))
  777. target_transaction(t, b->contact->m_url, b->tport);
  778. if (t->clients) /* XXX - enable forking */
  779. break;
  780. }
  781. if (t->clients != NULL)
  782. return 0;
  783. return respond_transaction(t, SIP_480_TEMPORARILY_UNAVAILABLE, TAG_END());
  784. }
  785. static int target_transaction(struct proxy_tr *t,
  786. url_t const *target,
  787. tport_t *tport)
  788. {
  789. struct client_tr *c = su_zalloc(t->proxy->home, sizeof *c);
  790. int stateless = t->method == sip_method_ack;
  791. if (c == NULL)
  792. return 500;
  793. c->t = t;
  794. c->msg = msg_copy(t->msg);
  795. c->sip = sip_object(c->msg);
  796. if (c->msg)
  797. c->rq = sip_request_create(msg_home(c->msg),
  798. t->method, t->method_name,
  799. (url_string_t *)target,
  800. NULL);
  801. msg_header_insert(c->msg, (msg_pub_t *)c->sip, (msg_header_t *)c->rq);
  802. if (t->rr) {
  803. sip_record_route_t rr[1];
  804. if (t->proxy->rr_uri) {
  805. *sip_record_route_init(rr)->r_url = *t->proxy->rr_uri;
  806. msg_header_add_dup(c->msg, (msg_pub_t *)c->sip, (msg_header_t *)rr);
  807. }
  808. else if (t->proxy->lr) {
  809. *sip_record_route_init(rr)->r_url = *t->proxy->lr->r_url;
  810. msg_header_add_dup(c->msg, (msg_pub_t *)c->sip, (msg_header_t *)rr);
  811. }
  812. }
  813. if (c->rq)
  814. /* Forward request */
  815. c->client = nta_outgoing_mcreate(t->proxy->agent,
  816. proxy_response, c,
  817. NULL,
  818. msg_ref_create(c->msg),
  819. NTATAG_TPORT(tport),
  820. NTATAG_STATELESS(stateless),
  821. TAG_END());
  822. if (!c->client) {
  823. msg_destroy(c->msg);
  824. su_free(t->proxy->home, c);
  825. return 500;
  826. }
  827. client_tr_insert(&t->clients, c);
  828. return stateless ? 200 : 0;
  829. }
  830. static int challenge_transaction(struct proxy_tr *t)
  831. {
  832. auth_status_t *as;
  833. sip_t *sip = t->sip;
  834. assert(t->am);
  835. t->as = as = auth_status_new(t->proxy->home);
  836. if (!as)
  837. return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
  838. as->as_method = sip->sip_request->rq_method_name;
  839. as->as_source = msg_addrinfo(t->msg);
  840. as->as_realm = t->realm;
  841. as->as_user_uri = sip->sip_from->a_url;
  842. as->as_display = sip->sip_from->a_display;
  843. if (sip->sip_payload)
  844. as->as_body = sip->sip_payload->pl_data,
  845. as->as_bodylen = sip->sip_payload->pl_len;
  846. if (t->use_auth == 401)
  847. auth_mod_check_client(t->am, as, sip->sip_authorization,
  848. registrar_challenger);
  849. else
  850. auth_mod_check_client(t->am, as, sip->sip_proxy_authorization,
  851. proxy_challenger);
  852. if (as->as_status)
  853. return respond_transaction(t, as->as_status, as->as_phrase, TAG_END());
  854. if (as->as_match)
  855. msg_header_remove(t->msg, (msg_pub_t *)sip, as->as_match);
  856. return 0;
  857. }
  858. int proxy_ack_cancel(struct proxy_tr *t,
  859. nta_incoming_t *irq,
  860. sip_t const *sip)
  861. {
  862. struct client_tr *c;
  863. int status;
  864. if (sip == NULL) { /* timeout */
  865. proxy_tr_destroy(t);
  866. return 0;
  867. }
  868. if (sip->sip_request->rq_method != sip_method_cancel)
  869. return 500;
  870. status = 200;
  871. for (c = t->clients; c; c = c->next) {
  872. if (c->client && c->status < 200)
  873. /*
  874. * We don't care about response to CANCEL (or ACK)
  875. * so we give NULL as callback pointer (and nta immediately
  876. * destroys transaction object or marks it disposable)
  877. */
  878. if (nta_outgoing_tcancel(c->client, NULL, NULL, TAG_END()) == NULL)
  879. status = 500;
  880. }
  881. return status;
  882. }
  883. int proxy_response(struct client_tr *c,
  884. nta_outgoing_t *client,
  885. sip_t const *sip)
  886. {
  887. int final, timeout = 0;
  888. assert(c->t);
  889. if (sip) {
  890. msg_t *response = nta_outgoing_getresponse(client);
  891. if (c->t->method == sip_method_invite)
  892. final = sip->sip_status->st_status >= 300,
  893. timeout = sip->sip_status->st_status >= 200;
  894. else
  895. final = sip->sip_status->st_status >= 200;
  896. sip_via_remove(response, sip_object(response));
  897. nta_incoming_mreply(c->t->server, response);
  898. }
  899. else {
  900. int status = nta_outgoing_status(c->client);
  901. char const *phrase;
  902. if (status < 300 || status > 699)
  903. status = 500;
  904. phrase = sip_status_phrase(status);
  905. respond_transaction(c->t, status, phrase, TAG_END());
  906. final = 1;
  907. }
  908. if (final)
  909. proxy_tr_destroy(c->t);
  910. else if (timeout)
  911. proxy_tr_timeout(c->t);
  912. return 0;
  913. }
  914. int proxy_late_response(struct client_tr *c,
  915. nta_outgoing_t *client,
  916. sip_t const *sip)
  917. {
  918. assert(c->t);
  919. if (sip &&
  920. sip->sip_status->st_status >= 200 &&
  921. sip->sip_status->st_status < 300) {
  922. msg_t *response = nta_outgoing_getresponse(client);
  923. sip_via_remove(response, sip_object(response));
  924. nta_incoming_mreply(c->t->server, response);
  925. }
  926. return 0;
  927. }
  928. static void proxy_tr_remove_late(su_root_magic_t *magic,
  929. su_timer_t *timer,
  930. struct proxy_tr *t)
  931. {
  932. proxy_tr_destroy(t);
  933. }
  934. /** Proxy only late responses
  935. *
  936. * Keeping the invite transactions live
  937. */
  938. static void proxy_tr_timeout(struct proxy_tr *t)
  939. {
  940. struct client_tr *c;
  941. if (t == t->proxy->stateless)
  942. return;
  943. for (c = t->clients; c; c = c->next) {
  944. if (c->client && c->status < 300) {
  945. nta_outgoing_bind(c->client, proxy_late_response, c);
  946. if (c->status < 200) {
  947. nta_outgoing_tcancel(c->client, NULL, NULL, TAG_END());
  948. }
  949. }
  950. }
  951. t->timer = su_timer_create(su_root_task(t->proxy->root), t->proxy->prefs.t1x64);
  952. if (su_timer_set(t->timer, proxy_tr_remove_late, t) < 0) {
  953. proxy_tr_destroy(t);
  954. }
  955. }
  956. struct proxy_tr *
  957. proxy_tr_new(struct proxy *proxy)
  958. {
  959. struct proxy_tr *t;
  960. t = su_zalloc(proxy->home, sizeof *t);
  961. if (t) {
  962. t->proxy = proxy;
  963. proxy_tr_insert(&proxy->transactions, t);
  964. }
  965. return t;
  966. }
  967. static
  968. void proxy_tr_destroy(struct proxy_tr *t)
  969. {
  970. struct client_tr *c;
  971. if (t == t->proxy->stateless)
  972. return;
  973. proxy_tr_remove(t);
  974. if (t->as)
  975. su_home_unref(t->as->as_home), t->as = NULL;
  976. while (t->clients) {
  977. client_tr_remove(c = t->clients);
  978. nta_outgoing_destroy(c->client), c->client = NULL;
  979. msg_destroy(c->msg), c->msg = NULL;
  980. su_free(t->proxy->home, c);
  981. }
  982. su_timer_destroy(t->timer), t->timer = NULL;
  983. msg_destroy(t->msg);
  984. nta_incoming_destroy(t->server);
  985. su_free(t->proxy->home, t);
  986. }
  987. LIST_BODIES(static, proxy_tr, struct proxy_tr, next, prev);
  988. /* ---------------------------------------------------------------------- */
  989. static int process_options(struct proxy_tr *t)
  990. {
  991. return respond_transaction(t, SIP_200_OK,
  992. SIPTAG_CONTACT(t->proxy->transport_contacts),
  993. TAG_END());
  994. }
  995. /* ---------------------------------------------------------------------- */
  996. static int check_received_contact(struct proxy_tr *t);
  997. static int validate_contacts(struct proxy_tr *t);
  998. static int check_out_of_order(struct proxy_tr *t);
  999. static int update_bindings(struct proxy_tr *t);
  1000. int process_register(struct proxy_tr *t)
  1001. {
  1002. /* This is before authentication because we want to be bug-compatible */
  1003. if (check_received_contact(t))
  1004. return t->status;
  1005. if (t->domain->auth) {
  1006. t->am = t->domain->auth, t->use_auth = 401;
  1007. if (t->domain->prefs.authorize)
  1008. t->realm = t->domain->prefs.authorize;
  1009. if (challenge_transaction(t))
  1010. return t->status;
  1011. }
  1012. if (validate_contacts(t))
  1013. return t->status;
  1014. t->entry = registration_entry_find(t->domain, t->sip->sip_to->a_url);
  1015. if (check_out_of_order(t))
  1016. return t->status;
  1017. return update_bindings(t);
  1018. }
  1019. static int check_received_contact(struct proxy_tr *t)
  1020. {
  1021. sip_t *sip = t->sip;
  1022. sip_contact_t *m = sip->sip_contact;
  1023. sip_via_t *v = sip->sip_via;
  1024. if (m && v && v->v_received && m->m_url->url_host
  1025. && !su_casematch(v->v_received, m->m_url->url_host)
  1026. && host_is_ip_address(m->m_url->url_host))
  1027. return respond_transaction(t, 406, "Unacceptable Contact", TAG_END());
  1028. return 0;
  1029. }
  1030. /* Validate expiration times */
  1031. static int validate_contacts(struct proxy_tr *t)
  1032. {
  1033. sip_contact_t const *m = t->sip->sip_contact;
  1034. sip_expires_t const *ex = t->sip->sip_expires;
  1035. sip_date_t const *date = t->sip->sip_date;
  1036. sip_time_t expires;
  1037. if (m && m->m_url->url_type == url_any) {
  1038. if (!ex || ex->ex_delta || ex->ex_time || m->m_next)
  1039. return respond_transaction(t, SIP_400_BAD_REQUEST, TAG_END());
  1040. return 0;
  1041. }
  1042. for (; m; m = m->m_next) {
  1043. expires = sip_contact_expires(m, ex, date, t->domain->prefs.expires, t->now);
  1044. if (expires > 0 && expires < t->domain->prefs.min_expires) {
  1045. sip_min_expires_t me[1];
  1046. sip_min_expires_init(me)->me_delta = t->domain->prefs.min_expires;
  1047. return respond_transaction(t, SIP_423_INTERVAL_TOO_BRIEF,
  1048. SIPTAG_MIN_EXPIRES(me),
  1049. TAG_END());
  1050. }
  1051. }
  1052. return 0;
  1053. }
  1054. /** Check for out-of-order register request */
  1055. static int check_out_of_order(struct proxy_tr *t)
  1056. {
  1057. struct binding const *b;
  1058. sip_call_id_t const *id = t->sip->sip_call_id;
  1059. uint32_t cseq = t->sip->sip_cseq->cs_seq;
  1060. sip_contact_t *m;
  1061. if (t->entry == NULL || !t->sip->sip_contact)
  1062. return 0;
  1063. /* RFC 3261 subsection 10.3 step 6 and step 7 (p. 66): */
  1064. /* Check for reordered register requests */
  1065. for (b = t->entry->bindings; b; b = b->next) {
  1066. if (binding_is_active(b) &&
  1067. strcmp(id->i_id, b->call_id->i_id) == 0 &&
  1068. cseq <= b->cseq) {
  1069. for (m = t->sip->sip_contact; m; m = m->m_next) {
  1070. if (m->m_url->url_type == url_any ||
  1071. url_cmp_all(m->m_url, b->contact->m_url) == 0)
  1072. return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR,
  1073. TAG_END());
  1074. }
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. static struct registration_entry *
  1080. registration_entry_find(struct domain const *d, url_t const *uri)
  1081. {
  1082. struct registration_entry *e;
  1083. /* Our routing table */
  1084. for (e = d->entries; e; e = e->next) {
  1085. if (url_cmp(uri, e->aor) == 0)
  1086. return e;
  1087. }
  1088. return NULL;
  1089. }
  1090. static struct registration_entry *
  1091. registration_entry_new(struct domain *d, url_t const *aor)
  1092. {
  1093. struct registration_entry *e;
  1094. if (d == NULL)
  1095. return NULL;
  1096. e = su_zalloc(d->home, sizeof *e);
  1097. if (!e)
  1098. return NULL;
  1099. e->domain = d;
  1100. e->aor = url_hdup(d->home, aor);
  1101. if (!e->aor) {
  1102. su_free(d->home, e);
  1103. return NULL;
  1104. }
  1105. registration_entry_insert(&d->entries, e);
  1106. return e;
  1107. }
  1108. static void
  1109. registration_entry_destroy(struct registration_entry *e)
  1110. {
  1111. if (e) {
  1112. registration_entry_remove(e);
  1113. su_free(e->domain->home, e->aor);
  1114. while (e->bindings)
  1115. binding_destroy(e->domain->home, e->bindings);
  1116. msg_header_free(e->domain->home, (void *)e->contacts);
  1117. su_free(e->domain->home, e);
  1118. }
  1119. }
  1120. sip_contact_t *entry_contacts(struct registration_entry *entry)
  1121. {
  1122. return entry ? entry->contacts : NULL;
  1123. }
  1124. LIST_BODIES(static, registration_entry, struct registration_entry, next, prev);
  1125. /* ---------------------------------------------------------------------- */
  1126. /* Bindings */
  1127. static
  1128. struct binding *binding_new(su_home_t *home,
  1129. sip_contact_t *contact,
  1130. tport_t *tport,
  1131. sip_call_id_t const *call_id,
  1132. uint32_t cseq,
  1133. sip_time_t registered,
  1134. sip_time_t expires)
  1135. {
  1136. struct binding *b;
  1137. b = su_zalloc(home, sizeof *b);
  1138. if (b) {
  1139. sip_contact_t m[1];
  1140. *m = *contact; m->m_next = NULL;
  1141. b->contact = sip_contact_dup(home, m);
  1142. b->tport = tport_ref(tport);
  1143. b->call_id = sip_call_id_dup(home, call_id);
  1144. b->cseq = cseq;
  1145. b->registered = registered;
  1146. b->expires = expires;
  1147. if (!b->contact || !b->call_id)
  1148. binding_destroy(home, b), b = NULL;
  1149. if (b)
  1150. msg_header_remove_param(b->contact->m_common, "expires");
  1151. }
  1152. return b;
  1153. }
  1154. static
  1155. void binding_destroy(su_home_t *home, struct binding *b)
  1156. {
  1157. if (b->prev) {
  1158. if ((*b->prev = b->next))
  1159. b->next->prev = b->prev;
  1160. }
  1161. msg_header_free(home, (void *)b->contact);
  1162. msg_header_free(home, (void *)b->call_id);
  1163. tport_unref(b->tport);
  1164. su_free(home, b);
  1165. }
  1166. static int update_bindings(struct proxy_tr *t)
  1167. {
  1168. struct domain *d = t->domain;
  1169. struct binding *b, *old, *next, *last, *bindings = NULL, **bb = &bindings;
  1170. sip_contact_t *m;
  1171. sip_call_id_t const *id = t->sip->sip_call_id;
  1172. uint32_t cseq = t->sip->sip_cseq->cs_seq;
  1173. sip_expires_t *ex = t->sip->sip_expires;
  1174. sip_date_t *date = t->sip->sip_date;
  1175. sip_time_t expires;
  1176. tport_t *tport = NULL;
  1177. sip_contact_t *contacts = NULL, **mm = &contacts;
  1178. void *tbf;
  1179. if (t->sip->sip_contact == NULL) {
  1180. if (t->entry)
  1181. contacts = t->entry->contacts;
  1182. goto ok200;
  1183. }
  1184. if (t->entry == NULL)
  1185. t->entry = registration_entry_new(d, t->sip->sip_to->a_url);
  1186. if (t->entry == NULL)
  1187. return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
  1188. if (d->prefs.outbound_tcp &&
  1189. str0casecmp(t->sip->sip_via->v_protocol, sip_transport_tcp) == 0)
  1190. tport = nta_incoming_transport(t->proxy->agent, t->server, NULL);
  1191. /* Create new bindings */
  1192. for (m = t->sip->sip_contact; m; m = m->m_next) {
  1193. if (m->m_url->url_type == url_any)
  1194. break;
  1195. expires = sip_contact_expires(m, ex, date, d->prefs.expires, t->now);
  1196. if (expires > d->prefs.max_expires)
  1197. expires = d->prefs.max_expires;
  1198. msg_header_remove_param(m->m_common, "expires");
  1199. b = binding_new(d->home, m, tport, id, cseq, t->now, t->now + expires);
  1200. if (!b)
  1201. break;
  1202. *bb = b, b->prev = bb, bb = &b->next;
  1203. }
  1204. tport_unref(tport);
  1205. last = NULL;
  1206. if (m == NULL) {
  1207. /* Merge new bindings with old ones */
  1208. for (old = t->entry->bindings; old; old = next) {
  1209. next = old->next;
  1210. for (b = bindings; b != last; b = b->next) {
  1211. if (url_cmp_all(old->contact->m_url, b->contact->m_url) != 0)
  1212. continue;
  1213. if (strcmp(old->call_id->i_id, b->call_id->i_id) == 0) {
  1214. b->registered = old->registered;
  1215. }
  1216. binding_destroy(d->home, old);
  1217. break;
  1218. }
  1219. }
  1220. for (bb = &t->entry->bindings; *bb; bb = &(*bb)->next)
  1221. ;
  1222. if ((*bb = bindings))
  1223. bindings->prev = bb;
  1224. }
  1225. else if (m->m_url->url_type == url_any) {
  1226. /* Unregister all */
  1227. for (b = t->entry->bindings; b; b = b->next) {
  1228. b->expires = t->now;
  1229. }
  1230. }
  1231. else {
  1232. /* Infernal error */
  1233. for (old = bindings; old; old = next) {
  1234. next = old->next;
  1235. binding_destroy(d->home, old);
  1236. }
  1237. return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
  1238. }
  1239. for (b = t->entry->bindings; b; b = b->next) {
  1240. char const *expires;
  1241. if (b->expires <= t->now)
  1242. continue;
  1243. *mm = sip_contact_copy(d->home, b->contact);
  1244. if (*mm) {
  1245. expires = su_sprintf(d->home, "expires=%u",
  1246. (unsigned)(b->expires - t->now));
  1247. msg_header_add_param(d->home, (*mm)->m_common, expires);
  1248. mm = &(*mm)->m_next;
  1249. }
  1250. }
  1251. tbf = t->entry->contacts;
  1252. t->entry->contacts = contacts;
  1253. msg_header_free(d->home, tbf);
  1254. ok200:
  1255. return respond_transaction(t, SIP_200_OK,
  1256. SIPTAG_CONTACT(contacts),
  1257. TAG_END());
  1258. }
  1259. /* ---------------------------------------------------------------------- */
  1260. static int close_tports(void *_proxy)
  1261. {
  1262. struct proxy *p = _proxy;
  1263. struct domain *d;
  1264. struct registration_entry *e;
  1265. struct binding *b;
  1266. /* Close all outbound transports */
  1267. for (d = p->domains; d; d = d->next) {
  1268. for (e = d->entries; e; e = e->next) {
  1269. for (b = e->bindings; b; b = b->next) {
  1270. if (b->tport) {
  1271. tport_shutdown(b->tport, 1);
  1272. tport_unref(b->tport);
  1273. b->tport = NULL;
  1274. }
  1275. }
  1276. }
  1277. }
  1278. return 0;
  1279. }