1 /**
2 * @file src/ua.c User-Agent
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6 #include <string.h>
7 #include <re.h>
8 #include <baresip.h>
9 #include "core.h"
10 #include <ctype.h>
11
12
13 /** Magic number */
14 #define MAGIC 0x0a0a0a0a
15 #include "magic.h"
16
17
18 /** Defines a SIP User Agent object */
19 struct ua {
20 MAGIC_DECL /**< Magic number for struct ua */
21 struct ua **uap; /**< Pointer to application's ua */
22 struct le le; /**< Linked list element */
23 struct account *acc; /**< Account Parameters */
24 struct list regl; /**< List of Register clients */
25 struct list calls; /**< List of active calls (struct call) */
26 struct pl extensionv[8]; /**< Vector of SIP extensions */
27 size_t extensionc; /**< Number of SIP extensions */
28 char *cuser; /**< SIP Contact username */
29 char *pub_gruu; /**< SIP Public GRUU */
30 int af; /**< Preferred Address Family */
31 int af_media; /**< Preferred Address Family for media */
32 enum presence_status my_status; /**< Presence Status */
33 };
34
35 struct ua_eh {
36 struct le le;
37 ua_event_h *h;
38 void *arg;
39 };
40
41 static struct {
42 struct config_sip *cfg; /**< SIP configuration */
43 struct list ual; /**< List of User-Agents (struct ua) */
44 struct list ehl; /**< Event handlers (struct ua_eh) */
45 struct sip *sip; /**< SIP Stack */
46 struct sip_lsnr *lsnr; /**< SIP Listener */
47 struct sipsess_sock *sock; /**< SIP Session socket */
48 struct sipevent_sock *evsock; /**< SIP Event socket */
49 struct ua *ua_cur; /**< Current User-Agent */
50 bool use_udp; /**< Use UDP transport */
51 bool use_tcp; /**< Use TCP transport */
52 bool use_tls; /**< Use TLS transport */
53 bool prefer_ipv6; /**< Force IPv6 transport */
54 sip_msg_h *subh; /**< Subscribe handler */
55 ua_exit_h *exith; /**< UA Exit handler */
56 void *arg; /**< UA Exit handler argument */
57 char *eprm; /**< Extra UA parameters */
58 #ifdef USE_TLS
59 struct tls *tls; /**< TLS Context */
60 #endif
61 } uag = {
62 NULL,
63 LIST_INIT,
64 LIST_INIT,
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 NULL,
70 true,
71 true,
72 true,
73 false,
74 NULL,
75 NULL,
76 NULL,
77 NULL,
78 #ifdef USE_TLS
79 NULL,
80 #endif
81 };
82
83
84 /* prototypes */
85 static int ua_call_alloc(struct call **callp, struct ua *ua,
86 enum vidmode vidmode, const struct sip_msg *msg,
87 struct call *xcall, const char *local_uri,
88 bool use_rtp);
89
90
91 /* This function is called when all SIP transactions are done */
exit_handler(void * arg)92 static void exit_handler(void *arg)
93 {
94 (void)arg;
95
96 ua_event(NULL, UA_EVENT_EXIT, NULL, NULL);
97
98 debug("ua: sip-stack exit\n");
99
100 if (uag.exith)
101 uag.exith(uag.arg);
102 }
103
104
ua_printf(const struct ua * ua,const char * fmt,...)105 void ua_printf(const struct ua *ua, const char *fmt, ...)
106 {
107 va_list ap;
108
109 if (!ua)
110 return;
111
112 va_start(ap, fmt);
113 info("%r@%r: %v", &ua->acc->luri.user, &ua->acc->luri.host, fmt, &ap);
114 va_end(ap);
115 }
116
117
ua_event(struct ua * ua,enum ua_event ev,struct call * call,const char * fmt,...)118 void ua_event(struct ua *ua, enum ua_event ev, struct call *call,
119 const char *fmt, ...)
120 {
121 struct le *le;
122 char buf[256];
123 va_list ap;
124
125 va_start(ap, fmt);
126 (void)re_vsnprintf(buf, sizeof(buf), fmt, ap);
127 va_end(ap);
128
129 /* send event to all clients */
130 le = uag.ehl.head;
131 while (le) {
132 struct ua_eh *eh = le->data;
133 le = le->next;
134
135 eh->h(ua, ev, call, buf, eh->arg);
136 }
137 }
138
139
140 /**
141 * Start registration of a User-Agent
142 *
143 * @param ua User-Agent
144 *
145 * @return 0 if success, otherwise errorcode
146 */
ua_register(struct ua * ua)147 int ua_register(struct ua *ua)
148 {
149 struct account *acc;
150 struct le *le;
151 struct uri uri;
152 char *reg_uri = NULL;
153 char params[256] = "";
154 unsigned i;
155 int err;
156
157 if (!ua)
158 return EINVAL;
159
160 acc = ua->acc;
161 uri = ua->acc->luri;
162 uri.user = uri.password = pl_null;
163
164 err = re_sdprintf(®_uri, "%H", uri_encode, &uri);
165 if (err)
166 goto out;
167
168 if (uag.cfg && str_isset(uag.cfg->uuid)) {
169 if (re_snprintf(params, sizeof(params),
170 ";+sip.instance=\"<urn:uuid:%s>\"",
171 uag.cfg->uuid) < 0) {
172 err = ENOMEM;
173 goto out;
174 }
175 }
176
177 if (acc->regq) {
178 if (re_snprintf(¶ms[strlen(params)],
179 sizeof(params) - strlen(params),
180 ";q=%s", acc->regq) < 0) {
181 err = ENOMEM;
182 goto out;
183 }
184 }
185
186 if (acc->mnat && acc->mnat->ftag) {
187 if (re_snprintf(¶ms[strlen(params)],
188 sizeof(params) - strlen(params),
189 ";%s", acc->mnat->ftag) < 0) {
190 err = ENOMEM;
191 goto out;
192 }
193 }
194
195 ua_event(ua, UA_EVENT_REGISTERING, NULL, NULL);
196
197 for (le = ua->regl.head, i=0; le; le = le->next, i++) {
198 struct reg *reg = le->data;
199
200 err = reg_register(reg, reg_uri, params,
201 acc->regint, acc->outboundv[i]);
202 if (err) {
203 warning("ua: SIP register failed: %m\n", err);
204 goto out;
205 }
206 }
207
208 out:
209 mem_deref(reg_uri);
210
211 return err;
212 }
213
214
215 /**
216 * Unregister all Register clients of a User-Agent
217 *
218 * @param ua User-Agent
219 */
ua_unregister(struct ua * ua)220 void ua_unregister(struct ua *ua)
221 {
222 struct le *le;
223
224 if (!ua)
225 return;
226
227 if (!list_isempty(&ua->regl))
228 ua_event(ua, UA_EVENT_UNREGISTERING, NULL, NULL);
229
230 for (le = ua->regl.head; le; le = le->next) {
231 struct reg *reg = le->data;
232
233 reg_unregister(reg);
234 }
235 }
236
237
ua_isregistered(const struct ua * ua)238 bool ua_isregistered(const struct ua *ua)
239 {
240 struct le *le;
241
242 if (!ua)
243 return false;
244
245 for (le = ua->regl.head; le; le = le->next) {
246
247 const struct reg *reg = le->data;
248
249 /* it is enough if one of the registrations work */
250 if (reg_isok(reg))
251 return true;
252 }
253
254 return false;
255 }
256
257
ua_find_call_onhold(const struct ua * ua)258 static struct call *ua_find_call_onhold(const struct ua *ua)
259 {
260 struct le *le;
261
262 if (!ua)
263 return NULL;
264
265 for (le = ua->calls.tail; le; le = le->prev) {
266
267 struct call *call = le->data;
268
269 if (call_is_onhold(call))
270 return call;
271 }
272
273 return NULL;
274 }
275
276
resume_call(struct ua * ua)277 static void resume_call(struct ua *ua)
278 {
279 struct call *call;
280
281 call = ua_find_call_onhold(ua);
282 if (call) {
283 ua_printf(ua, "resuming previous call with '%s'\n",
284 call_peeruri(call));
285 call_hold(call, false);
286 }
287 }
288
289
call_event_handler(struct call * call,enum call_event ev,const char * str,void * arg)290 static void call_event_handler(struct call *call, enum call_event ev,
291 const char *str, void *arg)
292 {
293 struct ua *ua = arg;
294 const char *peeruri;
295 struct call *call2 = NULL;
296 int err;
297
298 MAGIC_CHECK(ua);
299
300 peeruri = call_peeruri(call);
301
302 switch (ev) {
303
304 case CALL_EVENT_INCOMING:
305
306 if (contact_block_access(baresip_contacts(),
307 peeruri)) {
308
309 info("ua: blocked access: \"%s\"\n", peeruri);
310
311 ua_event(ua, UA_EVENT_CALL_CLOSED, call, str);
312 mem_deref(call);
313 break;
314 }
315
316 switch (ua->acc->answermode) {
317
318 case ANSWERMODE_EARLY:
319 (void)call_progress(call);
320 break;
321
322 case ANSWERMODE_AUTO:
323 (void)call_answer(call, 200);
324 break;
325
326 case ANSWERMODE_MANUAL:
327 default:
328 ua_event(ua, UA_EVENT_CALL_INCOMING, call, peeruri);
329 break;
330 }
331 break;
332
333 case CALL_EVENT_RINGING:
334 ua_event(ua, UA_EVENT_CALL_RINGING, call, peeruri);
335 break;
336
337 case CALL_EVENT_PROGRESS:
338 ua_printf(ua, "Call in-progress: %s\n", peeruri);
339 ua_event(ua, UA_EVENT_CALL_PROGRESS, call, peeruri);
340 break;
341
342 case CALL_EVENT_ESTABLISHED:
343 ua_printf(ua, "Call established: %s\n", peeruri);
344 ua_event(ua, UA_EVENT_CALL_ESTABLISHED, call, peeruri);
345 break;
346
347 case CALL_EVENT_CLOSED:
348 ua_event(ua, UA_EVENT_CALL_CLOSED, call, str);
349 mem_deref(call);
350
351 resume_call(ua);
352 break;
353
354 case CALL_EVENT_TRANSFER:
355
356 /*
357 * Create a new call to transfer target.
358 *
359 * NOTE: we will automatically connect a new call to the
360 * transfer target
361 */
362
363 ua_printf(ua, "transferring call to %s\n", str);
364
365 err = ua_call_alloc(&call2, ua, VIDMODE_ON, NULL, call,
366 call_localuri(call), true);
367 if (!err) {
368 struct pl pl;
369
370 pl_set_str(&pl, str);
371
372 err = call_connect(call2, &pl);
373 if (err) {
374 warning("ua: transfer: connect error: %m\n",
375 err);
376 }
377 }
378
379 if (err) {
380 (void)call_notify_sipfrag(call, 500, "Call Error");
381 mem_deref(call2);
382 }
383 break;
384
385 case CALL_EVENT_TRANSFER_FAILED:
386 ua_event(ua, UA_EVENT_CALL_TRANSFER_FAILED, call, str);
387 break;
388 }
389 }
390
391
call_dtmf_handler(struct call * call,char key,void * arg)392 static void call_dtmf_handler(struct call *call, char key, void *arg)
393 {
394 struct ua *ua = arg;
395 char key_str[2];
396
397 MAGIC_CHECK(ua);
398
399 if (key != '\0') {
400
401 key_str[0] = key;
402 key_str[1] = '\0';
403
404 ua_event(ua, UA_EVENT_CALL_DTMF_START, call, key_str);
405 }
406 else {
407 ua_event(ua, UA_EVENT_CALL_DTMF_END, call, NULL);
408 }
409 }
410
411
ua_call_alloc(struct call ** callp,struct ua * ua,enum vidmode vidmode,const struct sip_msg * msg,struct call * xcall,const char * local_uri,bool use_rtp)412 static int ua_call_alloc(struct call **callp, struct ua *ua,
413 enum vidmode vidmode, const struct sip_msg *msg,
414 struct call *xcall, const char *local_uri,
415 bool use_rtp)
416 {
417 const struct network *net = baresip_network();
418 struct call_prm cprm;
419 int af = AF_UNSPEC;
420 int err;
421
422 if (*callp) {
423 warning("ua: call_alloc: call is already allocated\n");
424 return EALREADY;
425 }
426
427 /* 1. if AF_MEDIA is set, we prefer it
428 * 2. otherwise fall back to SIP AF
429 */
430 if (ua->af_media) {
431 af = ua->af_media;
432 }
433 else if (ua->af) {
434 af = ua->af;
435 }
436
437 memset(&cprm, 0, sizeof(cprm));
438
439 sa_cpy(&cprm.laddr, net_laddr_af(net, af));
440 cprm.vidmode = vidmode;
441 cprm.af = af;
442 cprm.use_rtp = use_rtp;
443
444 err = call_alloc(callp, conf_config(), &ua->calls,
445 ua->acc->dispname,
446 local_uri ? local_uri : ua->acc->aor,
447 ua->acc, ua, &cprm,
448 msg, xcall,
449 net_dnsc(net),
450 call_event_handler, ua);
451 if (err)
452 return err;
453
454 call_set_handlers(*callp, NULL, call_dtmf_handler, ua);
455
456 return 0;
457 }
458
459
handle_options(struct ua * ua,const struct sip_msg * msg)460 static void handle_options(struct ua *ua, const struct sip_msg *msg)
461 {
462 struct sip_contact contact;
463 struct call *call = NULL;
464 struct mbuf *desc = NULL;
465 const struct sip_hdr *hdr;
466 bool accept_sdp = true;
467 int err;
468
469 debug("ua: incoming OPTIONS message from %r (%J)\n",
470 &msg->from.auri, &msg->src);
471
472 /* application/sdp is the default if the
473 Accept header field is not present */
474 hdr = sip_msg_hdr(msg, SIP_HDR_ACCEPT);
475 if (hdr) {
476 accept_sdp = 0==pl_strcasecmp(&hdr->val, "application/sdp");
477 }
478
479 if (accept_sdp) {
480
481 err = ua_call_alloc(&call, ua, VIDMODE_ON, NULL, NULL, NULL,
482 false);
483 if (err) {
484 (void)sip_treply(NULL, uag.sip, msg,
485 500, "Call Error");
486 return;
487 }
488
489 err = call_sdp_get(call, &desc, true);
490 if (err)
491 goto out;
492 }
493
494 sip_contact_set(&contact, ua_cuser(ua), &msg->dst, msg->tp);
495
496 err = sip_treplyf(NULL, NULL, uag.sip,
497 msg, true, 200, "OK",
498 "Allow: %s\r\n"
499 "%H"
500 "%H"
501 "%s"
502 "Content-Length: %zu\r\n"
503 "\r\n"
504 "%b",
505 uag_allowed_methods(),
506 ua_print_supported, ua,
507 sip_contact_print, &contact,
508 desc ? "Content-Type: application/sdp\r\n" : "",
509 desc ? mbuf_get_left(desc) : (size_t)0,
510 desc ? mbuf_buf(desc) : NULL,
511 desc ? mbuf_get_left(desc) : (size_t)0);
512 if (err) {
513 warning("ua: options: sip_treplyf: %m\n", err);
514 }
515
516 out:
517 mem_deref(desc);
518 mem_deref(call);
519 }
520
521
ua_destructor(void * arg)522 static void ua_destructor(void *arg)
523 {
524 struct ua *ua = arg;
525
526 if (ua->uap) {
527 *ua->uap = NULL;
528 ua->uap = NULL;
529 }
530
531 list_unlink(&ua->le);
532
533 if (!list_isempty(&ua->regl))
534 ua_event(ua, UA_EVENT_UNREGISTERING, NULL, NULL);
535
536 list_flush(&ua->calls);
537 list_flush(&ua->regl);
538 mem_deref(ua->cuser);
539 mem_deref(ua->pub_gruu);
540 mem_deref(ua->acc);
541
542 if (list_isempty(&uag.ual)) {
543 sip_close(uag.sip, false);
544 }
545 }
546
547
request_handler(const struct sip_msg * msg,void * arg)548 static bool request_handler(const struct sip_msg *msg, void *arg)
549 {
550 struct ua *ua;
551
552 (void)arg;
553
554 if (pl_strcmp(&msg->met, "OPTIONS"))
555 return false;
556
557 ua = uag_find(&msg->uri.user);
558 if (!ua) {
559 (void)sip_treply(NULL, uag_sip(), msg, 404, "Not Found");
560 return true;
561 }
562
563 handle_options(ua, msg);
564
565 return true;
566 }
567
568
add_extension(struct ua * ua,const char * extension)569 static void add_extension(struct ua *ua, const char *extension)
570 {
571 struct pl e;
572
573 if (ua->extensionc >= ARRAY_SIZE(ua->extensionv)) {
574 warning("ua: maximum %u number of SIP extensions\n");
575 return;
576 }
577
578 pl_set_str(&e, extension);
579
580 ua->extensionv[ua->extensionc++] = e;
581 }
582
583
584 /**
585 * Allocate a SIP User-Agent
586 *
587 * @param uap Pointer to allocated User-Agent object
588 * @param aor SIP Address-of-Record (AOR)
589 *
590 * @return 0 if success, otherwise errorcode
591 */
ua_alloc(struct ua ** uap,const char * aor)592 int ua_alloc(struct ua **uap, const char *aor)
593 {
594 struct ua *ua;
595 char *buf = NULL;
596 int err;
597
598 if (!aor)
599 return EINVAL;
600
601 ua = mem_zalloc(sizeof(*ua), ua_destructor);
602 if (!ua)
603 return ENOMEM;
604
605 MAGIC_INIT(ua);
606
607 list_init(&ua->calls);
608
609 #if HAVE_INET6
610 ua->af = uag.prefer_ipv6 ? AF_INET6 : AF_INET;
611 #else
612 ua->af = AF_INET;
613 #endif
614
615 /* Decode SIP address */
616 if (uag.eprm) {
617 err = re_sdprintf(&buf, "%s;%s", aor, uag.eprm);
618 if (err)
619 goto out;
620 aor = buf;
621 }
622
623 err = account_alloc(&ua->acc, aor);
624 if (err)
625 goto out;
626
627 /* generate a unique contact-user, this is needed to route
628 incoming requests when using multiple useragents */
629 err = re_sdprintf(&ua->cuser, "%r-%p", &ua->acc->luri.user, ua);
630 if (err)
631 goto out;
632
633 if (ua->acc->sipnat) {
634 ua_printf(ua, "Using sipnat: `%s'\n", ua->acc->sipnat);
635 }
636
637 if (ua->acc->mnat) {
638 ua_printf(ua, "Using medianat `%s'\n",
639 ua->acc->mnat->id);
640
641 if (0 == str_casecmp(ua->acc->mnat->id, "ice"))
642 add_extension(ua, "ice");
643 }
644
645 if (ua->acc->menc) {
646 ua_printf(ua, "Using media encryption `%s'\n",
647 ua->acc->menc->id);
648 }
649
650 /* Register clients */
651 if (uag.cfg && str_isset(uag.cfg->uuid))
652 add_extension(ua, "gruu");
653
654 if (0 == str_casecmp(ua->acc->sipnat, "outbound")) {
655
656 size_t i;
657
658 add_extension(ua, "path");
659 add_extension(ua, "outbound");
660
661 if (!str_isset(uag.cfg->uuid)) {
662
663 warning("ua: outbound requires valid UUID!\n");
664 err = ENOSYS;
665 goto out;
666 }
667
668 for (i=0; i<ARRAY_SIZE(ua->acc->outboundv); i++) {
669
670 if (ua->acc->outboundv[i] && ua->acc->regint) {
671 err = reg_add(&ua->regl, ua, (int)i+1);
672 if (err)
673 break;
674 }
675 }
676 }
677 else if (ua->acc->regint) {
678 err = reg_add(&ua->regl, ua, 0);
679 }
680 if (err)
681 goto out;
682
683 list_append(&uag.ual, &ua->le, ua);
684
685 if (ua->acc->regint) {
686 err = ua_register(ua);
687 }
688
689 if (!uag_current())
690 uag_current_set(ua);
691
692 out:
693 mem_deref(buf);
694 if (err)
695 mem_deref(ua);
696 else if (uap) {
697 *uap = ua;
698
699 ua->uap = uap;
700 }
701
702 return err;
703 }
704
705
uri_complete(struct ua * ua,struct mbuf * buf,const char * uri)706 static int uri_complete(struct ua *ua, struct mbuf *buf, const char *uri)
707 {
708 size_t len;
709 int err = 0;
710
711 /* Skip initial whitespace */
712 while (isspace(*uri))
713 ++uri;
714
715 len = str_len(uri);
716
717 /* Append sip: scheme if missing */
718 if (0 != re_regex(uri, len, "sip:"))
719 err |= mbuf_printf(buf, "sip:");
720
721 err |= mbuf_write_str(buf, uri);
722
723 /* Append domain if missing */
724 if (0 != re_regex(uri, len, "[^@]+@[^]+", NULL, NULL)) {
725 #if HAVE_INET6
726 if (AF_INET6 == ua->acc->luri.af)
727 err |= mbuf_printf(buf, "@[%r]",
728 &ua->acc->luri.host);
729 else
730 #endif
731 err |= mbuf_printf(buf, "@%r",
732 &ua->acc->luri.host);
733
734 /* Also append port if specified and not 5060 */
735 switch (ua->acc->luri.port) {
736
737 case 0:
738 case SIP_PORT:
739 break;
740
741 default:
742 err |= mbuf_printf(buf, ":%u", ua->acc->luri.port);
743 break;
744 }
745 }
746
747 return err;
748 }
749
750
751 /**
752 * Connect an outgoing call to a given SIP uri
753 *
754 * @param ua User-Agent
755 * @param callp Optional pointer to allocated call object
756 * @param from_uri Optional From uri, or NULL for default AOR
757 * @param uri SIP uri to connect to
758 * @param params Optional URI parameters
759 * @param vmode Video mode
760 *
761 * @return 0 if success, otherwise errorcode
762 */
ua_connect(struct ua * ua,struct call ** callp,const char * from_uri,const char * uri,const char * params,enum vidmode vmode)763 int ua_connect(struct ua *ua, struct call **callp,
764 const char *from_uri, const char *uri,
765 const char *params, enum vidmode vmode)
766 {
767 struct call *call = NULL;
768 struct mbuf *dialbuf;
769 struct pl pl;
770 int err = 0;
771
772 if (!ua || !str_isset(uri))
773 return EINVAL;
774
775 dialbuf = mbuf_alloc(64);
776 if (!dialbuf)
777 return ENOMEM;
778
779 if (params)
780 err |= mbuf_printf(dialbuf, "<");
781
782 err |= uri_complete(ua, dialbuf, uri);
783
784 if (params) {
785 err |= mbuf_printf(dialbuf, ";%s", params);
786 }
787
788 /* Append any optional URI parameters */
789 err |= mbuf_write_pl(dialbuf, &ua->acc->luri.params);
790
791 if (params)
792 err |= mbuf_printf(dialbuf, ">");
793
794 if (err)
795 goto out;
796
797 err = ua_call_alloc(&call, ua, vmode, NULL, NULL, from_uri, true);
798 if (err)
799 goto out;
800
801 pl.p = (char *)dialbuf->buf;
802 pl.l = dialbuf->end;
803
804 err = call_connect(call, &pl);
805
806 if (err)
807 mem_deref(call);
808 else if (callp)
809 *callp = call;
810
811 out:
812 mem_deref(dialbuf);
813
814 return err;
815 }
816
817
818 /**
819 * Hangup the current call
820 *
821 * @param ua User-Agent
822 * @param call Call to hangup, or NULL for current call
823 * @param scode Optional status code
824 * @param reason Optional reason
825 */
ua_hangup(struct ua * ua,struct call * call,uint16_t scode,const char * reason)826 void ua_hangup(struct ua *ua, struct call *call,
827 uint16_t scode, const char *reason)
828 {
829 if (!ua)
830 return;
831
832 if (!call) {
833 call = ua_call(ua);
834 if (!call)
835 return;
836 }
837
838 (void)call_hangup(call, scode, reason);
839
840 ua_event(ua, UA_EVENT_CALL_CLOSED, call,
841 reason ? reason : "Connection reset by user");
842
843 mem_deref(call);
844
845 resume_call(ua);
846 }
847
848
849 /**
850 * Answer an incoming call
851 *
852 * @param ua User-Agent
853 * @param call Call to answer, or NULL for current call
854 *
855 * @return 0 if success, otherwise errorcode
856 */
ua_answer(struct ua * ua,struct call * call)857 int ua_answer(struct ua *ua, struct call *call)
858 {
859 if (!ua)
860 return EINVAL;
861
862 if (!call) {
863 call = ua_call(ua);
864 if (!call)
865 return ENOENT;
866 }
867
868 return call_answer(call, 200);
869 }
870
871
ua_progress(struct ua * ua,struct call * call)872 int ua_progress(struct ua *ua, struct call *call)
873 {
874 if (!ua)
875 return EINVAL;
876
877 if (!call) {
878 call = ua_call(ua);
879 if (!call)
880 return ENOENT;
881 }
882
883 return call_progress(call);
884 }
885
886
887 /**
888 * Put the current call on hold and answer the incoming call
889 *
890 * @param ua User-Agent
891 * @param call Call to answer, or NULL for current call
892 *
893 * @return 0 if success, otherwise errorcode
894 */
ua_hold_answer(struct ua * ua,struct call * call)895 int ua_hold_answer(struct ua *ua, struct call *call)
896 {
897 struct call *pcall;
898 int err;
899
900 if (!ua)
901 return EINVAL;
902
903 if (!call) {
904 call = ua_call(ua);
905 if (!call)
906 return ENOENT;
907 }
908
909 /* put previous call on-hold */
910 pcall = ua_prev_call(ua);
911 if (pcall) {
912 ua_printf(ua, "putting call with '%s' on hold\n",
913 call_peeruri(pcall));
914
915 err = call_hold(pcall, true);
916 if (err)
917 return err;
918 }
919
920 return ua_answer(ua, call);
921 }
922
923
ua_print_status(struct re_printf * pf,const struct ua * ua)924 int ua_print_status(struct re_printf *pf, const struct ua *ua)
925 {
926 struct le *le;
927 int err;
928
929 if (!ua)
930 return 0;
931
932 err = re_hprintf(pf, "%-42s", ua->acc->aor);
933
934 for (le = ua->regl.head; le; le = le->next)
935 err |= reg_status(pf, le->data);
936
937 err |= re_hprintf(pf, "\n");
938
939 return err;
940 }
941
942
943 /**
944 * Send SIP OPTIONS message to a peer
945 *
946 * @param ua User-Agent object
947 * @param uri Peer SIP Address
948 * @param resph Response handler
949 * @param arg Handler argument
950 *
951 * @return 0 if success, otherwise errorcode
952 */
ua_options_send(struct ua * ua,const char * uri,options_resp_h * resph,void * arg)953 int ua_options_send(struct ua *ua, const char *uri,
954 options_resp_h *resph, void *arg)
955 {
956 struct mbuf *dialbuf;
957 int err = 0;
958
959 if (!ua || !str_isset(uri))
960 return EINVAL;
961
962 dialbuf = mbuf_alloc(64);
963 if (!dialbuf)
964 return ENOMEM;
965
966 err = uri_complete(ua, dialbuf, uri);
967 if (err)
968 goto out;
969
970 dialbuf->buf[dialbuf->end] = '\0';
971
972 err = sip_req_send(ua, "OPTIONS", (char *)dialbuf->buf, resph, arg,
973 "Accept: application/sdp\r\n"
974 "Content-Length: 0\r\n"
975 "\r\n");
976 if (err) {
977 warning("ua: send options: (%m)\n", err);
978 }
979
980 out:
981 mem_deref(dialbuf);
982
983 return err;
984 }
985
986
987 /**
988 * Get the AOR of a User-Agent
989 *
990 * @param ua User-Agent object
991 *
992 * @return AOR
993 */
ua_aor(const struct ua * ua)994 const char *ua_aor(const struct ua *ua)
995 {
996 return ua ? account_aor(ua->acc) : NULL;
997 }
998
999
1000 /**
1001 * Get presence status of a User-Agent
1002 *
1003 * @param ua User-Agent object
1004 *
1005 * @return presence status
1006 */
ua_presence_status(const struct ua * ua)1007 enum presence_status ua_presence_status(const struct ua *ua)
1008 {
1009 return ua ? ua->my_status : PRESENCE_UNKNOWN;
1010 }
1011
1012
1013 /**
1014 * Set presence status of a User-Agent
1015 *
1016 * @param ua User-Agent object
1017 * @param status Presence status
1018 */
ua_presence_status_set(struct ua * ua,const enum presence_status status)1019 void ua_presence_status_set(struct ua *ua, const enum presence_status status)
1020 {
1021 if (!ua)
1022 return;
1023
1024 ua->my_status = status;
1025 }
1026
1027
1028 /**
1029 * Get the outbound SIP proxy of a User-Agent
1030 *
1031 * @param ua User-Agent object
1032 *
1033 * @return Outbound SIP proxy uri
1034 */
ua_outbound(const struct ua * ua)1035 const char *ua_outbound(const struct ua *ua)
1036 {
1037 /* NOTE: we pick the first outbound server, should be rotated? */
1038 return ua ? ua->acc->outboundv[0] : NULL;
1039 }
1040
1041
1042 /**
1043 * Get the current call object of a User-Agent
1044 *
1045 * @param ua User-Agent object
1046 *
1047 * @return Current call, NULL if no active calls
1048 *
1049 *
1050 * Current call strategy:
1051 *
1052 * We can only have 1 current call. The current call is the one that was
1053 * added last (end of the list).
1054 */
ua_call(const struct ua * ua)1055 struct call *ua_call(const struct ua *ua)
1056 {
1057 if (!ua)
1058 return NULL;
1059
1060 return list_ledata(list_tail(&ua->calls));
1061 }
1062
1063
ua_prev_call(const struct ua * ua)1064 struct call *ua_prev_call(const struct ua *ua)
1065 {
1066 struct le *le;
1067 int prev = 0;
1068
1069 if (!ua)
1070 return NULL;
1071
1072 for (le = ua->calls.tail; le; le = le->prev) {
1073 if ( prev == 1) {
1074 struct call *call = le->data;
1075 return call;
1076 }
1077 if ( prev == 0)
1078 prev = 1;
1079 }
1080
1081 return NULL;
1082 }
1083
1084
ua_debug(struct re_printf * pf,const struct ua * ua)1085 int ua_debug(struct re_printf *pf, const struct ua *ua)
1086 {
1087 struct le *le;
1088 int err;
1089
1090 if (!ua)
1091 return 0;
1092
1093 err = re_hprintf(pf, "--- %s ---\n", ua->acc->aor);
1094 err |= re_hprintf(pf, " nrefs: %u\n", mem_nrefs(ua));
1095 err |= re_hprintf(pf, " cuser: %s\n", ua->cuser);
1096 err |= re_hprintf(pf, " pub-gruu: %s\n", ua->pub_gruu);
1097 err |= re_hprintf(pf, " af: %s\n", net_af2name(ua->af));
1098 err |= re_hprintf(pf, " %H", ua_print_supported, ua);
1099
1100 err |= account_debug(pf, ua->acc);
1101
1102 for (le = ua->regl.head; le; le = le->next)
1103 err |= reg_debug(pf, le->data);
1104
1105 return err;
1106 }
1107
1108
1109 /* One instance */
1110
1111
add_transp_af(const struct sa * laddr)1112 static int add_transp_af(const struct sa *laddr)
1113 {
1114 struct sa local;
1115 int err = 0;
1116
1117 if (str_isset(uag.cfg->local)) {
1118 err = sa_decode(&local, uag.cfg->local,
1119 str_len(uag.cfg->local));
1120 if (err) {
1121 err = sa_set_str(&local, uag.cfg->local, 0);
1122 if (err) {
1123 warning("ua: decode failed: '%s'\n",
1124 uag.cfg->local);
1125 return err;
1126 }
1127 }
1128
1129 if (!sa_isset(&local, SA_ADDR)) {
1130 uint16_t port = sa_port(&local);
1131 (void)sa_set_sa(&local, &laddr->u.sa);
1132 sa_set_port(&local, port);
1133 }
1134
1135 if (sa_af(laddr) != sa_af(&local))
1136 return 0;
1137 }
1138 else {
1139 sa_cpy(&local, laddr);
1140 sa_set_port(&local, 0);
1141 }
1142
1143 if (uag.use_udp)
1144 err |= sip_transp_add(uag.sip, SIP_TRANSP_UDP, &local);
1145 if (uag.use_tcp)
1146 err |= sip_transp_add(uag.sip, SIP_TRANSP_TCP, &local);
1147 if (err) {
1148 warning("ua: SIP Transport failed: %m\n", err);
1149 return err;
1150 }
1151
1152 #ifdef USE_TLS
1153 if (uag.use_tls) {
1154 /* Build our SSL context*/
1155 if (!uag.tls) {
1156 const char *cert = NULL;
1157
1158 if (str_isset(uag.cfg->cert)) {
1159 cert = uag.cfg->cert;
1160 info("SIP Certificate: %s\n", cert);
1161 }
1162
1163 err = tls_alloc(&uag.tls, TLS_METHOD_SSLV23,
1164 cert, NULL);
1165 if (err) {
1166 warning("ua: tls_alloc() failed: %m\n", err);
1167 return err;
1168 }
1169 }
1170
1171 if (sa_isset(&local, SA_PORT))
1172 sa_set_port(&local, sa_port(&local) + 1);
1173
1174 err = sip_transp_add(uag.sip, SIP_TRANSP_TLS, &local, uag.tls);
1175 if (err) {
1176 warning("ua: SIP/TLS transport failed: %m\n", err);
1177 return err;
1178 }
1179 }
1180 #endif
1181
1182 return err;
1183 }
1184
1185
ua_add_transp(struct network * net)1186 static int ua_add_transp(struct network *net)
1187 {
1188 int err = 0;
1189
1190 if (!uag.prefer_ipv6) {
1191 if (sa_isset(net_laddr_af(net, AF_INET), SA_ADDR))
1192 err |= add_transp_af(net_laddr_af(net, AF_INET));
1193 }
1194
1195 #if HAVE_INET6
1196 if (sa_isset(net_laddr_af(net, AF_INET6), SA_ADDR))
1197 err |= add_transp_af(net_laddr_af(net, AF_INET6));
1198 #endif
1199
1200 return err;
1201 }
1202
1203
require_handler(const struct sip_hdr * hdr,const struct sip_msg * msg,void * arg)1204 static bool require_handler(const struct sip_hdr *hdr,
1205 const struct sip_msg *msg, void *arg)
1206 {
1207 struct ua *ua = arg;
1208 bool supported = false;
1209 size_t i;
1210 (void)msg;
1211
1212 for (i=0; i<ua->extensionc; i++) {
1213
1214 if (!pl_casecmp(&hdr->val, &ua->extensionv[i])) {
1215 supported = true;
1216 break;
1217 }
1218 }
1219
1220 return !supported;
1221 }
1222
1223
1224 /* Handle incoming calls */
sipsess_conn_handler(const struct sip_msg * msg,void * arg)1225 static void sipsess_conn_handler(const struct sip_msg *msg, void *arg)
1226 {
1227 struct config *config = conf_config();
1228 const struct sip_hdr *hdr;
1229 struct ua *ua;
1230 struct call *call = NULL;
1231 char to_uri[256];
1232 int err;
1233
1234 (void)arg;
1235
1236 ua = uag_find(&msg->uri.user);
1237 if (!ua) {
1238 warning("ua: %r: UA not found: %r\n",
1239 &msg->from.auri, &msg->uri.user);
1240 (void)sip_treply(NULL, uag_sip(), msg, 404, "Not Found");
1241 return;
1242 }
1243
1244 /* handle multiple calls */
1245 if (config->call.max_calls &&
1246 list_count(&ua->calls) + 1 > config->call.max_calls) {
1247
1248 info("ua: rejected call from %r (maximum %d calls)\n",
1249 &msg->from.auri, config->call.max_calls);
1250 (void)sip_treply(NULL, uag.sip, msg, 486, "Max Calls");
1251 return;
1252 }
1253
1254 /* Handle Require: header, check for any required extensions */
1255 hdr = sip_msg_hdr_apply(msg, true, SIP_HDR_REQUIRE,
1256 require_handler, ua);
1257 if (hdr) {
1258 info("ua: call from %r rejected with 420"
1259 " -- option-tag '%r' not supported\n",
1260 &msg->from.auri, &hdr->val);
1261
1262 (void)sip_treplyf(NULL, NULL, uag.sip, msg, false,
1263 420, "Bad Extension",
1264 "Unsupported: %r\r\n"
1265 "Content-Length: 0\r\n\r\n",
1266 &hdr->val);
1267 return;
1268 }
1269
1270 (void)pl_strcpy(&msg->to.auri, to_uri, sizeof(to_uri));
1271
1272 err = ua_call_alloc(&call, ua, VIDMODE_ON, msg, NULL, to_uri, true);
1273 if (err) {
1274 warning("ua: call_alloc: %m\n", err);
1275 goto error;
1276 }
1277
1278 err = call_accept(call, uag.sock, msg);
1279 if (err)
1280 goto error;
1281
1282 return;
1283
1284 error:
1285 mem_deref(call);
1286 (void)sip_treply(NULL, uag.sip, msg, 500, "Call Error");
1287 }
1288
1289
net_change_handler(void * arg)1290 static void net_change_handler(void *arg)
1291 {
1292 (void)arg;
1293
1294 info("IP-address changed: %j\n",
1295 net_laddr_af(baresip_network(), AF_INET));
1296
1297 (void)uag_reset_transp(true, true);
1298 }
1299
1300
sub_handler(const struct sip_msg * msg,void * arg)1301 static bool sub_handler(const struct sip_msg *msg, void *arg)
1302 {
1303 struct ua *ua;
1304
1305 (void)arg;
1306
1307 ua = uag_find(&msg->uri.user);
1308 if (!ua) {
1309 warning("subscribe: no UA found for %r\n", &msg->uri.user);
1310 (void)sip_treply(NULL, uag_sip(), msg, 404, "Not Found");
1311 return true;
1312 }
1313
1314 if (uag.subh)
1315 uag.subh(msg, ua);
1316
1317 return true;
1318 }
1319
1320
1321 /**
1322 * Initialise the User-Agents
1323 *
1324 * @param software SIP User-Agent string
1325 * @param udp Enable UDP transport
1326 * @param tcp Enable TCP transport
1327 * @param tls Enable TLS transport
1328 * @param prefer_ipv6 Prefer IPv6 flag
1329 *
1330 * @return 0 if success, otherwise errorcode
1331 */
ua_init(const char * software,bool udp,bool tcp,bool tls,bool prefer_ipv6)1332 int ua_init(const char *software, bool udp, bool tcp, bool tls,
1333 bool prefer_ipv6)
1334 {
1335 struct config *cfg = conf_config();
1336 struct network *net = baresip_network();
1337 uint32_t bsize;
1338 int err;
1339
1340 if (!net) {
1341 warning("ua: no network\n");
1342 return EINVAL;
1343 }
1344
1345 uag.cfg = &cfg->sip;
1346 bsize = cfg->sip.trans_bsize;
1347
1348 uag.use_udp = udp;
1349 uag.use_tcp = tcp;
1350 uag.use_tls = tls;
1351 uag.prefer_ipv6 = prefer_ipv6;
1352
1353 list_init(&uag.ual);
1354
1355 err = sip_alloc(&uag.sip, net_dnsc(net), bsize, bsize, bsize,
1356 software, exit_handler, NULL);
1357 if (err) {
1358 warning("ua: sip stack failed: %m\n", err);
1359 goto out;
1360 }
1361
1362 err = ua_add_transp(net);
1363 if (err)
1364 goto out;
1365
1366 err = sip_listen(&uag.lsnr, uag.sip, true, request_handler, NULL);
1367 if (err)
1368 goto out;
1369
1370 err = sipsess_listen(&uag.sock, uag.sip, bsize,
1371 sipsess_conn_handler, NULL);
1372 if (err)
1373 goto out;
1374
1375 err = sipevent_listen(&uag.evsock, uag.sip, bsize, bsize,
1376 sub_handler, NULL);
1377 if (err)
1378 goto out;
1379
1380 net_change(net, 60, net_change_handler, NULL);
1381
1382 out:
1383 if (err) {
1384 warning("ua: init failed (%m)\n", err);
1385 ua_close();
1386 }
1387 return err;
1388 }
1389
1390
1391 /**
1392 * Close all active User-Agents
1393 */
ua_close(void)1394 void ua_close(void)
1395 {
1396 uag.evsock = mem_deref(uag.evsock);
1397 uag.sock = mem_deref(uag.sock);
1398 uag.lsnr = mem_deref(uag.lsnr);
1399 uag.sip = mem_deref(uag.sip);
1400 uag.eprm = mem_deref(uag.eprm);
1401
1402 #ifdef USE_TLS
1403 uag.tls = mem_deref(uag.tls);
1404 #endif
1405
1406 list_flush(&uag.ual);
1407 list_flush(&uag.ehl);
1408
1409 /* note: must be done before mod_close() */
1410 module_app_unload();
1411 }
1412
1413
1414 /**
1415 * Stop all User-Agents
1416 *
1417 * @param forced True to force, otherwise false
1418 */
ua_stop_all(bool forced)1419 void ua_stop_all(bool forced)
1420 {
1421 struct le *le;
1422 bool ext_ref = false;
1423
1424 info("ua: stop all (forced=%d)\n", forced);
1425
1426 /* check if someone else has grabbed a ref to ua */
1427 le = uag.ual.head;
1428 while (le) {
1429
1430 struct ua *ua = le->data;
1431 le = le->next;
1432
1433 if (mem_nrefs(ua) > 1) {
1434
1435 list_unlink(&ua->le);
1436 list_flush(&ua->calls);
1437 mem_deref(ua);
1438
1439 ext_ref = true;
1440 }
1441
1442 ua_event(ua, UA_EVENT_SHUTDOWN, NULL, NULL);
1443 }
1444
1445 if (ext_ref) {
1446 info("ua: ext_ref -> cannot unload mods\n");
1447 return;
1448 }
1449 else {
1450 module_app_unload();
1451 }
1452
1453 if (!list_isempty(&uag.ual)) {
1454 const uint32_t n = list_count(&uag.ual);
1455 info("Stopping %u useragent%s.. %s\n",
1456 n, n==1 ? "" : "s", forced ? "(Forced)" : "");
1457 }
1458
1459 if (forced)
1460 sipsess_close_all(uag.sock);
1461 else
1462 list_flush(&uag.ual);
1463
1464 sip_close(uag.sip, forced);
1465 }
1466
1467
1468 /**
1469 * Set the global UA exit handler. The exit handler will be called
1470 * asyncronously when the SIP stack has exited.
1471 *
1472 * @param exith Exit handler
1473 * @param arg Handler argument
1474 */
uag_set_exit_handler(ua_exit_h * exith,void * arg)1475 void uag_set_exit_handler(ua_exit_h *exith, void *arg)
1476 {
1477 uag.exith = exith;
1478 uag.arg = arg;
1479 }
1480
1481
1482 /**
1483 * Reset the SIP transports for all User-Agents
1484 *
1485 * @param reg True to reset registration
1486 * @param reinvite True to update active calls
1487 *
1488 * @return 0 if success, otherwise errorcode
1489 */
uag_reset_transp(bool reg,bool reinvite)1490 int uag_reset_transp(bool reg, bool reinvite)
1491 {
1492 struct network *net = baresip_network();
1493 struct le *le;
1494 int err;
1495
1496 /* Update SIP transports */
1497 sip_transp_flush(uag.sip);
1498
1499 (void)net_check(net);
1500 err = ua_add_transp(net);
1501 if (err)
1502 return err;
1503
1504 /* Re-REGISTER all User-Agents */
1505 for (le = uag.ual.head; le; le = le->next) {
1506 struct ua *ua = le->data;
1507
1508 if (reg && ua->acc->regint) {
1509 err |= ua_register(ua);
1510 }
1511
1512 /* update all active calls */
1513 if (reinvite) {
1514 struct le *lec;
1515
1516 for (lec = ua->calls.head; lec; lec = lec->next) {
1517 struct call *call = lec->data;
1518 const struct sa *laddr;
1519
1520 laddr = net_laddr_af(net, call_af(call));
1521
1522 err |= call_reset_transp(call, laddr);
1523 }
1524 }
1525 }
1526
1527 return err;
1528 }
1529
1530
1531 /**
1532 * Print the SIP Status for all User-Agents
1533 *
1534 * @param pf Print handler for debug output
1535 * @param unused Unused parameter
1536 *
1537 * @return 0 if success, otherwise errorcode
1538 */
ua_print_sip_status(struct re_printf * pf,void * unused)1539 int ua_print_sip_status(struct re_printf *pf, void *unused)
1540 {
1541 (void)unused;
1542 return sip_debug(pf, uag.sip);
1543 }
1544
1545
1546 /**
1547 * Print all calls for a given User-Agent
1548 *
1549 * @param pf Print handler for debug output
1550 * @param ua User-Agent
1551 *
1552 * @return 0 if success, otherwise errorcode
1553 */
ua_print_calls(struct re_printf * pf,const struct ua * ua)1554 int ua_print_calls(struct re_printf *pf, const struct ua *ua)
1555 {
1556 uint32_t n, count=0;
1557 uint32_t linenum;
1558 int err = 0;
1559
1560 if (!ua) {
1561 err |= re_hprintf(pf, "\n--- No active calls ---\n");
1562 return err;
1563 }
1564
1565 n = list_count(&ua->calls);
1566
1567 err |= re_hprintf(pf, "\n--- List of active calls (%u): ---\n",
1568 n);
1569
1570 for (linenum=CALL_LINENUM_MIN; linenum<CALL_LINENUM_MAX; linenum++) {
1571
1572 const struct call *call;
1573
1574 call = call_find_linenum(&ua->calls, linenum);
1575 if (call) {
1576 ++count;
1577
1578 err |= re_hprintf(pf, " %c %H\n",
1579 call == ua_call(ua) ? '>' : ' ',
1580 call_info, call);
1581 }
1582
1583 if (count >= n)
1584 break;
1585 }
1586
1587 err |= re_hprintf(pf, "\n");
1588
1589 return err;
1590 }
1591
1592
1593 /**
1594 * Get the global SIP Stack
1595 *
1596 * @return SIP Stack
1597 */
uag_sip(void)1598 struct sip *uag_sip(void)
1599 {
1600 return uag.sip;
1601 }
1602
1603
1604 /**
1605 * Get the global SIP Session socket
1606 *
1607 * @return SIP Session socket
1608 */
uag_sipsess_sock(void)1609 struct sipsess_sock *uag_sipsess_sock(void)
1610 {
1611 return uag.sock;
1612 }
1613
1614
1615 /**
1616 * Get the global SIP Event socket
1617 *
1618 * @return SIP Event socket
1619 */
uag_sipevent_sock(void)1620 struct sipevent_sock *uag_sipevent_sock(void)
1621 {
1622 return uag.evsock;
1623 }
1624
1625
uag_tls(void)1626 struct tls *uag_tls(void)
1627 {
1628 #ifdef USE_TLS
1629 return uag.tls;
1630 #else
1631 return NULL;
1632 #endif
1633 }
1634
1635
1636 /**
1637 * Find the correct UA from the contact user
1638 *
1639 * @param cuser Contact username
1640 *
1641 * @return Matching UA if found, NULL if not found
1642 */
uag_find(const struct pl * cuser)1643 struct ua *uag_find(const struct pl *cuser)
1644 {
1645 struct le *le;
1646
1647 for (le = uag.ual.head; le; le = le->next) {
1648 struct ua *ua = le->data;
1649
1650 if (0 == pl_strcasecmp(cuser, ua->cuser))
1651 return ua;
1652 }
1653
1654 /* Try also matching by AOR, for better interop */
1655 for (le = uag.ual.head; le; le = le->next) {
1656 struct ua *ua = le->data;
1657
1658 if (0 == pl_casecmp(cuser, &ua->acc->luri.user))
1659 return ua;
1660 }
1661
1662 return NULL;
1663 }
1664
1665
1666 /**
1667 * Find a User-Agent (UA) from an Address-of-Record (AOR)
1668 *
1669 * @param aor Address-of-Record string
1670 *
1671 * @return User-Agent (UA) if found, otherwise NULL
1672 */
uag_find_aor(const char * aor)1673 struct ua *uag_find_aor(const char *aor)
1674 {
1675 struct le *le;
1676
1677 for (le = uag.ual.head; le; le = le->next) {
1678 struct ua *ua = le->data;
1679
1680 if (str_isset(aor) && str_cmp(ua->acc->aor, aor))
1681 continue;
1682
1683 return ua;
1684 }
1685
1686 return NULL;
1687 }
1688
1689
1690 /**
1691 * Find a User-Agent (UA) which has certain address parameter and/or value
1692 *
1693 * @param name SIP Address parameter name
1694 * @param value SIP Address parameter value (optional)
1695 *
1696 * @return User-Agent (UA) if found, otherwise NULL
1697 */
uag_find_param(const char * name,const char * value)1698 struct ua *uag_find_param(const char *name, const char *value)
1699 {
1700 struct le *le;
1701
1702 for (le = uag.ual.head; le; le = le->next) {
1703 struct ua *ua = le->data;
1704 struct sip_addr *laddr = account_laddr(ua->acc);
1705 struct pl val;
1706
1707 if (value) {
1708
1709 if (0 == msg_param_decode(&laddr->params, name, &val)
1710 &&
1711 0 == pl_strcasecmp(&val, value)) {
1712 return ua;
1713 }
1714 }
1715 else {
1716 if (0 == msg_param_exists(&laddr->params, name, &val))
1717 return ua;
1718 }
1719 }
1720
1721 return NULL;
1722 }
1723
1724
1725 /**
1726 * Get the contact user/uri of a User-Agent (UA)
1727 *
1728 * If the Public GRUU is set, it will be returned.
1729 * Otherwise the local contact-user (cuser) will be returned.
1730 *
1731 * @param ua User-Agent
1732 *
1733 * @return Contact user
1734 */
ua_cuser(const struct ua * ua)1735 const char *ua_cuser(const struct ua *ua)
1736 {
1737 if (!ua)
1738 return NULL;
1739
1740 if (str_isset(ua->pub_gruu))
1741 return ua->pub_gruu;
1742
1743 return ua->cuser;
1744 }
1745
1746
ua_local_cuser(const struct ua * ua)1747 const char *ua_local_cuser(const struct ua *ua)
1748 {
1749 return ua ? ua->cuser : NULL;
1750 }
1751
1752
1753 /**
1754 * Get Account of a User-Agent
1755 *
1756 * @param ua User-Agent
1757 *
1758 * @return Pointer to UA's account
1759 */
ua_account(const struct ua * ua)1760 struct account *ua_account(const struct ua *ua)
1761 {
1762 return ua ? ua->acc : NULL;
1763 }
1764
1765
1766 /**
1767 * Set Public GRUU of a User-Agent (UA)
1768 *
1769 * @param ua User-Agent
1770 * @param pval Public GRUU
1771 */
ua_pub_gruu_set(struct ua * ua,const struct pl * pval)1772 void ua_pub_gruu_set(struct ua *ua, const struct pl *pval)
1773 {
1774 if (!ua)
1775 return;
1776
1777 ua->pub_gruu = mem_deref(ua->pub_gruu);
1778 (void)pl_strdup(&ua->pub_gruu, pval);
1779 }
1780
1781
uag_list(void)1782 struct list *uag_list(void)
1783 {
1784 return &uag.ual;
1785 }
1786
1787
1788 /**
1789 * Return list of methods supported by the UA
1790 *
1791 * @return String of supported methods
1792 */
uag_allowed_methods(void)1793 const char *uag_allowed_methods(void)
1794 {
1795 return "INVITE,ACK,BYE,CANCEL,OPTIONS,REFER,"
1796 "NOTIFY,SUBSCRIBE,INFO,MESSAGE";
1797 }
1798
1799
ua_print_supported(struct re_printf * pf,const struct ua * ua)1800 int ua_print_supported(struct re_printf *pf, const struct ua *ua)
1801 {
1802 size_t i;
1803 int err;
1804
1805 err = re_hprintf(pf, "Supported:");
1806
1807 for (i=0; i<ua->extensionc; i++) {
1808 err |= re_hprintf(pf, "%s%r",
1809 i==0 ? " " : ",", &ua->extensionv[i]);
1810 }
1811
1812 err |= re_hprintf(pf, "\r\n");
1813
1814 return err;
1815 }
1816
1817
ua_calls(const struct ua * ua)1818 struct list *ua_calls(const struct ua *ua)
1819 {
1820 return ua ? (struct list *)&ua->calls : NULL;
1821 }
1822
1823
eh_destructor(void * arg)1824 static void eh_destructor(void *arg)
1825 {
1826 struct ua_eh *eh = arg;
1827 list_unlink(&eh->le);
1828 }
1829
1830
uag_event_register(ua_event_h * h,void * arg)1831 int uag_event_register(ua_event_h *h, void *arg)
1832 {
1833 struct ua_eh *eh;
1834
1835 if (!h)
1836 return EINVAL;
1837
1838 uag_event_unregister(h);
1839
1840 eh = mem_zalloc(sizeof(*eh), eh_destructor);
1841 if (!eh)
1842 return ENOMEM;
1843
1844 eh->h = h;
1845 eh->arg = arg;
1846
1847 list_append(&uag.ehl, &eh->le, eh);
1848
1849 return 0;
1850 }
1851
1852
uag_event_unregister(ua_event_h * h)1853 void uag_event_unregister(ua_event_h *h)
1854 {
1855 struct le *le;
1856
1857 for (le = uag.ehl.head; le; le = le->next) {
1858
1859 struct ua_eh *eh = le->data;
1860
1861 if (eh->h == h) {
1862 mem_deref(eh);
1863 break;
1864 }
1865 }
1866 }
1867
1868
uag_set_sub_handler(sip_msg_h * subh)1869 void uag_set_sub_handler(sip_msg_h *subh)
1870 {
1871 uag.subh = subh;
1872 }
1873
1874
uag_current_set(struct ua * ua)1875 void uag_current_set(struct ua *ua)
1876 {
1877 uag.ua_cur = ua;
1878 }
1879
1880
uag_current(void)1881 struct ua *uag_current(void)
1882 {
1883 if (list_isempty(uag_list()))
1884 return NULL;
1885
1886 return uag.ua_cur;
1887 }
1888
1889
ua_set_media_af(struct ua * ua,int af_media)1890 void ua_set_media_af(struct ua *ua, int af_media)
1891 {
1892 if (!ua)
1893 return;
1894
1895 ua->af_media = af_media;
1896 }
1897
1898
uag_set_extra_params(const char * eprm)1899 int uag_set_extra_params(const char *eprm)
1900 {
1901 uag.eprm = mem_deref(uag.eprm);
1902
1903 if (eprm)
1904 return str_dup(&uag.eprm, eprm);
1905
1906 return 0;
1907 }
1908