nua_glib.c 67.2 KB
Newer Older
1 2 3 4 5 6
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005-2006 Nokia Corporation.
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
7 8
 * Copyright (C) 2005 Collabora Ltd.
 *
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@file nua_glib.c Interface towards libsofia-sip-ua.
 * 
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
 * @author Rob Taylor <rob.taylor@collabora.co.uk>
 * @author Pekka Pessi <pekka.pessi@nokia.com>
 */

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

#include <glib.h>

#include "sofia-sip/nua_glib.h"
#include "nua_glib_priv.h"
#include "nua_glib_marshal.h"

/*=============== Class and Object init ===============*/
47
G_DEFINE_TYPE(NuaGlib, nua_glib, G_TYPE_OBJECT);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  
/*signal enum*/
enum
{
  CALL_FORKED,
  INCOMING_INVITE,
  INCOMING_REINVITE,
  CALL_STATE_CHANGED,
  INCOMING_ACTIVE,
  CALL_TERMINATED,
  INCOMING_PRACK,
  INCOMING_BYE,
  INCOMING_CANCEL,
  INCOMING_MESSAGE,
  INCOMING_INFO,
  INCOMING_REFER,
  INCOMING_NOTIFY,

  ERROR,
  SHUTDOWN,

  REGISTER_ANSWERED,
  UNREGISTER_ANSWERED,
  PUBLISH_ANSWERED,
  INVITE_ANSWERED,
  BYE_ANSWERED,
  MESSAGE_ANSWERED,
  INFO_ANSWERED,
  REFER_ANSWERED,
  SUBSCRIBE_ANSWERED,
  UNSUBSCRIBE_ANSWERED,
  NOTIFY_ANSWERED,
  OPTIONS_ANSWERED,
  MEDIA_ANSWERED,
  GET_ANSWERED,

  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};

enum
{
91 92 93
  PROP_ADDRESS = 1,
  PROP_PASSWORD,
  PROP_CONTACT,
94 95
  PROP_PROXY,
  PROP_REGISTRAR,
96
  PROP_STUN_SERVER,
97 98 99 100 101 102 103 104 105 106 107
  LAST_PROPERTY
};

void final_shutdown(NuaGlib *self)
{
  g_object_unref(self);
}

static GObjectClass *parent_class=NULL;  

static int sof_init(NuaGlibPrivate *priv, const char *contact);
108
static void priv_submit_authlist(NuaGlibOp *op);
109 110 111

static GObject *
nua_glib_constructor (GType                  type,
112 113
		      guint                  n_construct_properties,
		      GObjectConstructParam *construct_properties)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
  GObject *obj;
  int res = 0;
  GSource *gsource;

  {
    /* Invoke parent constructor.
     * this calls our init, and then set_property with any
     * CONSTRUCT params
     */
    NuaGlibClass *klass;
    klass = NUA_GLIB_CLASS (g_type_class_peek (NUA_GLIB_TYPE));
    parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
    obj = parent_class->constructor (type,
                                     n_construct_properties,
                                     construct_properties);
  }
  
  NuaGlib *self = NUA_GLIB(obj);

  /* create a su event loop and connect it to glib */
  self->priv->root = su_root_source_create(self);
  assert(self->priv->root);
  gsource = su_root_gsource(self->priv->root);
  assert(gsource);
  g_source_attach(gsource, NULL);

  /*check address has been set*/
  g_assert(strcmp(self->priv->address, "no-address-set")!=0);

144
  res = sof_init(self->priv, self->priv->contact);
145 146 147

  if (res != -1) {
    self->priv->nua = nua_create(self->priv->root, 
148 149 150
				 sof_callback, self,
				 NUTAG_SOA_NAME("default"),
				 NUTAG_MEDIA_ENABLE(1),
151 152
				 TAG_IF(self->priv->stun_server,
					STUNTAG_SERVER(self->priv->stun_server)),
153 154 155 156
				 TAG_IF(self->priv->contact,
					NUTAG_URL(self->priv->contact)),
				 /* SOATAG_USER_SDP_STR(local_caps), */
				 TAG_NULL());
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    if (self->priv->nua) {
      int min_se = 0;
      int s_e = 0;

      if (getenv("MIN_SE")) min_se = atoi(getenv("MIN_SE"));
      if (getenv("SESSION_EXPIRES")) s_e = atoi(getenv("SESSION_EXPIRES"));
      
      /* XXX: fix getenvs */
      
      nua_set_params(self->priv->nua,
                     TAG_IF(self->priv->proxy, NUTAG_PROXY(self->priv->proxy)),
                     TAG_IF(self->priv->registrar, NUTAG_REGISTRAR(self->priv->registrar)),
                     NUTAG_ENABLEMESSAGE(1),
                     NUTAG_ENABLEINVITE(1),
                     NUTAG_SESSION_TIMER(s_e),
                     NUTAG_MIN_SE(min_se),
                     SOATAG_AF(SOA_AF_IP4_IP6),
                     SIPTAG_FROM_STR(self->priv->address),
                     /*NUTAG_CERTIFICATE_DIR(getenv("SIPCERTDIR")),*/
                     TAG_NULL());
      nua_get_params(self->priv->nua, TAG_ANY(), TAG_NULL());

      self->priv->init=TRUE;
      g_signal_connect_after(self, "shutdown", (GCallback)final_shutdown, NULL);
    }
  }
  return obj;
}

static void
nua_glib_init(NuaGlib *self)
{
  self->priv =  g_new0(NuaGlibPrivate, 1);
 /* initialize sofia su OS abstraction layer */
  su_init();
  su_home_init(self->priv->home);
}

static void
nua_glib_dispose(GObject *obj)
{
  NuaGlib *self = NUA_GLIB(obj);

  if (self->priv->init)
  {
    g_free((gpointer)self->priv->contact);

    nua_shutdown(self->priv->nua);

    self->priv->init = FALSE;
    /*now hold a ref to ourselves that we drop when distruction is complete*/
    g_object_ref (obj);
  /* TODO:some start/stop nua funtions to do su_init/de_init?  su_deinit();*/
  }

  /* Chain up to the parent class */
  G_OBJECT_CLASS (parent_class)->dispose (obj);
}

static void
nua_glib_finalize (GObject *obj)
{
  NuaGlib *self = NUA_GLIB(obj);
  su_root_break(self->priv->root);
  nua_destroy(self->priv->nua);
  su_root_destroy(self->priv->root), self->priv->root = NULL;
  su_home_deinit(self->priv->home);
  g_free(self->priv);

  /* Chain up to the parent class */
  G_OBJECT_CLASS (parent_class)->finalize (obj); 
}

230 231 232 233 234
static
void nua_glib_set_property(GObject      *object,
			   guint         property_id,
			   const GValue *value,
			   GParamSpec   *pspec)
235 236 237
{
  NuaGlib *self = (NuaGlib*) object;

238 239 240 241
#define STORE_PARAM(s, x)			\
  g_free ((gpointer)(s)->priv->x);		\
  (s)->priv->x = g_value_dup_string (value)

242 243 244 245 246
  switch (property_id) {
  case PROP_ADDRESS: {
    if (self->priv->nua)
    {
      nua_set_params(self->priv->nua,
247
                     SIPTAG_FROM_STR(self->priv->address),
248 249 250 251 252 253 254 255 256
                     TAG_NULL());
      nua_get_params(self->priv->nua, TAG_ANY(), TAG_NULL());
    }
    else /*setting in constructor*/
    {
      self->priv->address = su_strdup(self->priv->home, g_value_get_string (value));
    }
    break;
  }
257 258 259 260 261 262 263 264
  case PROP_PASSWORD: {
    STORE_PARAM(self, password);
    break;
  }
  case PROP_CONTACT: {
    STORE_PARAM(self, contact);
    break;
  }
265
  case PROP_PROXY: {
266
    STORE_PARAM(self, proxy);
267 268 269 270 271 272 273 274 275
    if (self->priv->nua)
    {
      nua_set_params(self->priv->nua,
                     NUTAG_PROXY(self->priv->proxy),
                     TAG_NULL());
    }
    break;
  }
  case PROP_REGISTRAR: {
276
    STORE_PARAM(self, registrar);
277 278 279 280 281 282 283 284
    if (self->priv->nua)
    {
      nua_set_params(self->priv->nua,
                     NUTAG_REGISTRAR(self->priv->registrar),
                     TAG_NULL());
    }
    break;
  }
285 286
  case PROP_STUN_SERVER: {
    STORE_PARAM(self, stun_server);
287 288
    break;
  }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
 default:
    /* We don't have any other property... */
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object,property_id,pspec);
    break;
  }
}

static void
nua_glib_get_property (GObject      *object,
                        guint         property_id,
                        GValue       *value,
                        GParamSpec   *pspec)
{
  NuaGlib *self = (NuaGlib *) object;

  switch (property_id) {
  case PROP_ADDRESS: {
    g_value_set_string (value, self->priv->address);
    break;
  }
309 310 311 312 313 314 315 316
  case PROP_PASSWORD: {
    g_value_set_string (value, self->priv->password);
    break;
  }
  case PROP_CONTACT: {
    g_value_set_string (value, self->priv->contact);
    break;
  }
317 318 319 320 321 322 323 324
  case PROP_PROXY: {
    g_value_set_string (value, self->priv->proxy);
    break;
  }
  case PROP_REGISTRAR: {
    g_value_set_string (value, self->priv->registrar);
    break;
  }
325 326
  case PROP_STUN_SERVER: {
    g_value_set_string (value, self->priv->stun_server);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    break;
  }
  default:
    /* We don't have any other property... */
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object,property_id,pspec);
    break;
  }
}


static void
nua_glib_class_init (NuaGlibClass *nua_glib_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS(nua_glib_class);
  GParamSpec *param_spec;
 
  gobject_class->constructor = nua_glib_constructor;
  gobject_class->dispose = nua_glib_dispose;
  gobject_class->finalize = nua_glib_finalize;

  gobject_class->set_property = nua_glib_set_property;
  gobject_class->get_property = nua_glib_get_property;
  
  param_spec = g_param_spec_string("address",
                                   "NuaGlib construction property",
352
                                   "The address-of-record for this UA (e.g. 'sip:first.surname@myprovider.com')",
353 354 355 356 357 358
                                   "no-address-set", /*default value*/
                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  g_object_class_install_property (gobject_class,
                                   PROP_ADDRESS,
                                   param_spec);

359
  param_spec = g_param_spec_string("password",
360
                                   "NuaGlib construction property",
361
                                   "SIP account password",
362
                                   NULL, /*default value*/
363
                                   G_PARAM_READWRITE);
364
  g_object_class_install_property (gobject_class,
365
                                   PROP_PASSWORD,
366 367
                                   param_spec);

368
  param_spec = g_param_spec_string("contact",
369
                                   "NuaGlib construction property",
370
                                   "local bind interface (e.g. 'sip:0.0.0.0:*') [optional]",
371 372 373
                                   NULL, /*default value*/
                                   G_PARAM_READWRITE );
  g_object_class_install_property (gobject_class,
374
                                   PROP_CONTACT,
375 376
                                   param_spec);

377
  param_spec = g_param_spec_string("proxy",
378
                                   "NuaGlib construction property",
379
                                   "SIP outgoing proxy URI (e.g. 'sip:sipproxy.myprovider.com') [optional]",
380 381 382
                                   NULL, /*default value*/
                                   G_PARAM_READWRITE );
  g_object_class_install_property (gobject_class,
383
                                   PROP_PROXY,
384 385
                                   param_spec);

386 387 388 389 390 391 392 393
  param_spec = g_param_spec_string("registrar",
                                   "NuaGlib construction property",
                                   "SIP registrar URI (e.g. 'sip:sip.myprovider.com') [optional]",
                                   NULL, /*default value*/
                                   G_PARAM_READWRITE );
  g_object_class_install_property (gobject_class,
                                   PROP_REGISTRAR,
                                   param_spec);
394

395
  param_spec = g_param_spec_string("stun-server",
396
                                   "NuaGlib construction property",
397
                                   "STUN server address (FQDN or dotted-decimal) [optional]",
398 399 400
                                   "", /*default value*/
                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
  g_object_class_install_property (gobject_class,
401
                                   PROP_STUN_SERVER,
402 403
                                   param_spec);

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
  /**
   * NuaGlib::call-forked:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the call that was forked
   * @status: SIP status of fork (see SIP RFC)
   * @phrase: Reason for fork
   *
   * Emitted when an outgoing call has been forked.
   * This is when an INVITE request is answered with multiple 200 responses.
   */
  signals[CALL_FORKED] =
   g_signal_new("call-forked",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);


  /**
   * NuaGlib::incoming-invite:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call
   * @display: the display name of the invite recipient
   * @url: the url of the invite recipient
   * @subject: the subject of the invite (can be NULL)
   *
   * Emitted when an call invite is received
   * Should be answered with nua_glib_answer or nua_glib_decline
   */
  signals[INCOMING_INVITE] =
   g_signal_new("incoming-invite",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING,
    G_TYPE_NONE, 4, G_TYPE_POINTER , G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::incoming-reinvite:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   *
   * Emitted when an call invite is received for a call already in progress
   * Usually represents
   */
  signals[INCOMING_REINVITE] =
   g_signal_new("incoming-reinvite",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::call-state-changed:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @audio: #NuaGlibMediaActive describing audio availiablity
   * @video: #NuaGlibMediaActive describing video availiablity
   * @image: #NuaGlibMediaActive describing image availiablity
   * @chat: #NuaGlibMediaActive describing chat availiablity
   * @l_sdp: String containing any new local caps as SDP, can be NULL
   * @r_sdp: String containing any new remote cap as SDP, can be NULL
   *
   * Emitted when call state changes.
   */
 
  signals[CALL_STATE_CHANGED] =
   g_signal_new("call-state-changed",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_INT_INT_INT_STRING_STRING,
    G_TYPE_NONE, 7, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
                    G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::incoming-active:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   *
   * Emitted when a call goes active.
   */
 
  signals[INCOMING_ACTIVE] =
   g_signal_new("incoming-active",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::call-terminated:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing callA
   * @status: SIP status of termination (see SIP RFC)
   *
   * This will be emitted after a call has been terminated.
   * 
   * A call is terminated, when
   * 1) an error response (300..599) is sent to an incoming initial INVITE 
   * 2) a reliable response (200..299 or reliable preliminary response) to an 
   * incoming initial INVITE is not acknowledged with ACK or PRACK 
   * 3) BYE is received or sent
   *
   * Any references you hold to @op should be dropped at this point
   */
 
  signals[CALL_TERMINATED] =
   g_signal_new("call-terminated",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT,
    G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_INT);

  /**
   * NuaGlib::incoming-prack:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @rack: pointer to RAck that was PRACKed FIXME!!!
   *
   * Emitted when a PRACK is received
   */
 
  signals[INCOMING_PRACK] =
   g_signal_new("incoming-prack",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_POINTER,
    G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-bye:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   *
   */
  signals[INCOMING_BYE] =
   g_signal_new("incoming-bye",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-cancel:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of cancel (see SIP RFC)
   * @phrase: Reason for cancel 
   *
   * Emitted when incoming INVITE has been cancelled or no ACK has 
   * been received for an accepted call.
   */
 
  signals[INCOMING_CANCEL] =
   g_signal_new("incoming-cancel",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__POINTER,
    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-message:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call
   * @display: the display name of the invite recipient
   * @url: the url of the invite recipient
   * @subject: the subject of the invite (can be NULL)
   *
   * Emitted when a message is received
   */
  signals[INCOMING_MESSAGE] =
   g_signal_new("incoming-message",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
587 588
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING_STRING,
    G_TYPE_NONE, 5, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

  /**
   * NuaGlib::incoming-info:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call
   * @display_name: the display name of the info sender
   * @address: the address of the info sender
   * @subject: the subject of the invite (can be NULL)
   *
   * Emitted when an INFO message is received
   */
  signals[INCOMING_INFO] =
   g_signal_new("incoming-info",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING,
    G_TYPE_NONE, 4, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::incoming-refer:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation for the call that originated this refer
   * @display: the display name of the REFERER 
   * @address: the address of the REFERER 
   * @refer_address: the address you have been requested to refer to
   * @op2: a pointer to an operation to use to respond to this refer, NULL if 
   *       REFER url was not a SIP uri.
   *
   * Emitted when a REFER message is received
   * You should reply to this with #nua_glib_notify to cancel it or 
   * #nua_glib_follow_refer, and probably close * the call that referred 
   * you to follow it.
   */
  signals[INCOMING_REFER] =
   g_signal_new("incoming-refer",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING_POINTER,
    G_TYPE_NONE, 5, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
                    G_TYPE_POINTER);

  /**
   * NuaGlib::incoming-notify:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation created to represent this call, NULL if 
   *      this was a rogue notify
   * @event: name of event, can be NULL
   * @content-type: content type of event, can be NULL
   * @payload: payload of event
   *
   * Emitted when an NOTIFY message is received
   */
 
  signals[INCOMING_NOTIFY] =
   g_signal_new("incoming-notify",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_STRING_STRING_STRING,
    G_TYPE_NONE, 4, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

  /**
   * NuaGlib::error:
   * @nua_glib: the object that received the signal
   * @status: SIP status for error (see SIP RFC)
   * @phrase: Reason for error
   *
   * If you get this, run around waving your hands and screaming.
   */
 
  signals[ERROR] =
   g_signal_new("error",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__INT_STRING,
    G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::register-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of REGISTER answer (see SIP RFC)
   * @phrase: Reason for REGISTER answer 
   *
   * if status >=300 and not 401 or 407, you should drop any references you hold 
   * on @op
   */
  signals[REGISTER_ANSWERED] =
   g_signal_new("register-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::unregister-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of REGISTER answer (see SIP RFC)
   * @phrase: Reason for REGISTER answer 
   *
   * if status >=300 and not 401 or 407, you should drop any references you hold 
   * on @op
   */
 
  signals[UNREGISTER_ANSWERED] =
   g_signal_new("unregister-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::publish-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of PUBLISH answer (see SIP RFC)
   * @phrase: Reason for PUBLISH answer 
   *
   * if status >=300 and not 401 or 407, you should drop any references you hold 
   * on @op
   */
 
  signals[PUBLISH_ANSWERED] =
   g_signal_new("publish-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::invite-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of INVITE answer (see SIP RFC)
   * @phrase: Reason for INVITE answer 
   *
   */
  signals[INVITE_ANSWERED] =
   g_signal_new("invite-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::bye-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of bye answer (see SIP RFC)
   * @phrase: Reason for bye answer 
   *
   */
  signals[BYE_ANSWERED] =
   g_signal_new("bye-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::message-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of message answer (see SIP RFC)
   * @phrase: Reason for message answer
   *
   */
  signals[MESSAGE_ANSWERED] =
   g_signal_new("message-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::info-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of INFO answer (see SIP RFC)
   * @phrase: Reason for INFO answer
   *
   */
  signals[INFO_ANSWERED] =
   g_signal_new("info-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::refer-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of REFER answer (see SIP RFC)
   * @phrase: Reason for REFER answer 
   *
   */
  signals[REFER_ANSWERED] =
   g_signal_new("refer-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::subscribe-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of SUBSCRIBE answer (see SIP RFC)
   * @phrase: Reason for SUBSCRIBE answer
   *
   */
  signals[SUBSCRIBE_ANSWERED] =
   g_signal_new("subscribe-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::unsubscribe-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of UNSUBSCRIBE answer (see SIP RFC)
   * @phrase: Reason for UNSUBSCRIBE answer
   *
   *You should remove any references you have to @op after receiving this signal
   */
  signals[UNSUBSCRIBE_ANSWERED] =
   g_signal_new("unsubscribe-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::notify-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of NOTIFY answer (see SIP RFC)
   * @phrase: Reason for NOTIFY answer 
   *
   */
  signals[NOTIFY_ANSWERED] =
   g_signal_new("notify-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::options-answered:
   * @nua_glib: the object that received the signal
   * @op: pointer to the operation representing the existing call
   * @status: SIP status of options answer (see SIP RFC)
   * @phrase: Reason for options answer 
   *
   */
  signals[OPTIONS_ANSWERED] =
   g_signal_new("options-answered",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    nua_glib_marshal_VOID__POINTER_INT_STRING,
    G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_INT, G_TYPE_STRING);

  /**
   * NuaGlib::shutdown:
   * @nua_glib: the object that received the signal
   *
   * The sip stack is shutting down, 
   * drop all #NuaGlibOp references you hold
   * and unref the object it was emited on
   */
 signals[SHUTDOWN] =
   g_signal_new("shutdown",
    G_OBJECT_CLASS_TYPE (nua_glib_class),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0, NULL, NULL,
    g_cclosure_marshal_VOID__VOID,
    G_TYPE_NONE, 0, NULL);


}

/* ====================================================================== */
int sof_init(NuaGlibPrivate *priv, const char *contact)
{
  priv->name = "UA";
  priv->contact = g_strdup(contact);

  su_root_threading(priv->root, 0);

  return 0;
}


static void 
sof_callback(nua_event_t event,
                    int status, char const *phrase,
                    nua_t *nua, NuaGlib *self,
                    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
                    tagi_t tags[])
{
  g_return_if_fail(self);

  switch (event) {
  case nua_r_shutdown:    
    sof_r_shutdown(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_get_params:    
    sof_r_get_params(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_register:
    sof_r_register(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_unregister:
    sof_r_unregister(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_options:
    sof_r_options(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_invite:
    sof_r_invite(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_fork:
    sof_i_fork(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_i_invite:
    sof_i_invite(nua, self, nh, op, sip, tags);
    return;

  case nua_i_prack:
    sof_i_prack(nua, self, nh, op, sip, tags);
    return;

  case nua_i_active:
    sof_i_active(nua, self, nh, op, sip, tags);
    return;

  case nua_i_state:
    sof_i_state(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_i_terminated:
    sof_i_terminated(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_bye:
    sof_r_bye(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_bye:
    sof_i_bye(nua, self, nh, op, sip, tags);
    return;

  case nua_r_message:
    sof_r_message(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_message:
    sof_i_message(nua, self, nh, op, sip, tags);
    return;

  case nua_r_info:
    sof_r_info(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_info:
    sof_i_info(nua, self, nh, op, sip, tags);
    return;

  case nua_r_refer:
    sof_r_refer(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_i_refer:
    sof_i_refer(nua, self, nh, op, sip, tags);
    return;
     
  case nua_r_subscribe:
    sof_r_subscribe(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_unsubscribe:
    sof_r_unsubscribe(status, phrase, nua, self, nh, op, sip, tags);
    return;

  case nua_r_publish:
    sof_r_publish(status, phrase, nua, self, nh, op, sip, tags);
    return;
    
  case nua_r_notify:
    sof_r_notify(status, phrase, nua, self, nh, op, sip, tags);
    return;
     
  case nua_i_notify:
    sof_i_notify(nua, self, nh, op, sip, tags);
    return;

  case nua_i_cancel:
    sof_i_cancel(nua, self, nh, op, sip, tags);
    return;

  case nua_i_error:
    sof_i_error(nua, self, nh, op, status, phrase, tags);
    return;

  default:
    break;
  }

  if (status > 100)
    g_warning("%s: unknown event %d: %03d %s\n", 
           self->priv->name, event, status, phrase);
  else
    g_warning("%s: unknown event %d\n", self->priv->name, event);

  tl_print(stderr, "", tags);
}

/* ====================================================================== */
static inline
void oper_assign(NuaGlibOp *op, sip_method_t method, char const *name);

static void nua_glib_op_destroy(NuaGlib *self, NuaGlibOp *op);

static NuaGlibOp *
nua_glib_op_create(NuaGlib *self, 
                     sip_method_t method,
                     char const *name,
                     const char *address,
                     tag_type_t tag, tag_value_t value, ...)
{
  NuaGlibOp *op, *old;

  ta_list ta;
   
  enter;

  for (old = self->priv->operations; old; old = old->op_next)
    if (!old->op_persistent)
      break;

  if (address) {
    int have_url = 1;
    sip_to_t *to;

    to = sip_to_make(self->priv->home, address);

    if (to == NULL) {
      /*TODO, error returns*/
      g_warning("%s: %s: invalid address: %s\n", self->priv->name, name, address);
      return NULL;
    }

    /* Try to make sense out of the URL */
    if (url_sanitize(to->a_url) < 0) {
      /*TODO, error returns*/
      g_warning("%s: %s: invalid address\n", self->priv->name, name);
      return NULL;
    }

    if (!(op = su_zalloc(self->priv->home, sizeof(*op)))) {
      /*TODO, error returns*/
      g_warning("%s: %s: cannot create handle\n", self->priv->name, name);
      return NULL;
    }

    op->op_parent = self;
    op->op_next = self->priv->operations;
    op->op_prev_state = -1;
    self->priv->operations = op;      

    if (method == sip_method_register)
      have_url = 0;
    
    ta_start(ta, tag, value); 
     
    op->op_handle = nua_handle(self->priv->nua, op, 
                               TAG_IF(have_url, NUTAG_URL(to->a_url)), 
                               SIPTAG_TO(to),
                               ta_tags(ta));

    ta_end(ta);  
     
    op->op_ident = sip_header_as_string(self->priv->home, (sip_header_t *)to);

    oper_assign(op, method, name);
    
    if (!op->op_persistent) {
      NuaGlibOp *old_next;
      for (; old; old = old_next) {      /* Clean old handles */
        old_next = old->op_next;
        if (!old->op_persistent && !old->op_callstate)
          nua_glib_op_destroy(self, old);
      }
    }
    
    su_free(self->priv->home, to);
  }
  else if (method || name) 
    oper_assign(op = old, method, name);
  else
    return old;

  if (!op) {
    if (address)
      /*TODO, error returns*/
      g_warning("%s: %s: invalid destination\n", self->priv->name, name);
    else
      /*TODO, error returns*/
      g_warning("%s: %s: no destination\n", self->priv->name, name);
    return NULL;
  }

  return op;
}

static NuaGlibOp *
nua_glib_op_create2(NuaGlib *self, 
                      sip_method_t method,
                      char const *name,
                      nua_handle_t *nh,
                      sip_from_t const *from)
{
  NuaGlibOp *op;

  enter;

  if ((op = su_zalloc(self->priv->home, sizeof(*op)))) {
    op->op_parent = self;
    op->op_next = self->priv->operations;
    self->priv->operations = op;      

    oper_assign(op, method, name);
    nua_handle_bind(op->op_handle = nh, op);
    op->op_ident = sip_header_as_string(self->priv->home, (sip_header_t*)from);
  }
  else {
    printf("%s: cannot create operation object for %s\n", 
           self->priv->name, name);
  }

  return op;
}

/** Delete operation and attached handles and identities */
static void 
nua_glib_op_destroy(NuaGlib *self, NuaGlibOp *op)
{
  NuaGlibOp **prev;

  if (!op)
    return;

  g_assert(op->data == NULL);

  /* Remove from queue */
  for (prev = &self->priv->operations; 
       *prev && *prev != op; 
       prev = &(*prev)->op_next)
    ;
  if (*prev)
    *prev = op->op_next, op->op_next = NULL;

1177 1178 1179
  if (op->op_authlist)
    nua_glib_auth_clear(self, op);

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  if (op->op_handle)
    nua_handle_destroy(op->op_handle), op->op_handle = NULL;

  su_free(self->priv->home, op);
}

/* ====================================================================== */
static void 
oper_assign(NuaGlibOp *op, sip_method_t method, char const *name)
{
  if (!op)
    return;

  op->op_method = method, op->op_method_name = name;

  op->op_persistent = 
    method == sip_method_subscribe ||
    method == sip_method_register ||
    method == sip_method_publish;
}

1201 1202 1203
/**
 * Handles authentication challenge for operation 'op'.
 */
1204 1205 1206 1207 1208
static void 
oper_set_auth (NuaGlib *self, NuaGlibOp *op, sip_t const *sip, tagi_t *tags)
{
  sip_www_authenticate_t const *wa = sip->sip_www_authenticate;
  sip_proxy_authenticate_t const *pa = sip->sip_proxy_authenticate;
1209 1210 1211
  sip_from_t const *sipfrom = sip->sip_from;
  const char *realm = NULL;

1212 1213 1214 1215 1216 1217 1218
  enter;

  tl_gets(tags, 
          SIPTAG_WWW_AUTHENTICATE_REF(wa),
          SIPTAG_PROXY_AUTHENTICATE_REF(pa),
          TAG_NULL());

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  SU_DEBUG_3(("%s: %s was unauthorized\n", self->priv->name, op->op_method_name));

  if (wa) {
    sl_header_print(stdout, "Server auth: %s\n", (sip_header_t *)wa);
    realm = msg_params_find(wa->au_params, "realm=");  
    nua_glib_auth_add(self, op, wa->au_scheme, realm, sipfrom->a_url->url_user, self->priv->password);
  }
  if (pa) {
    sl_header_print(stdout, "Proxy auth: %s\n", (sip_header_t *)pa);
    realm = msg_params_find(pa->au_params, "realm=");  
    nua_glib_auth_add(self, op, pa->au_scheme, realm, sipfrom->a_url->url_user, self->priv->password);
  }
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
 
  if (op->op_tried_auth)
  {
    g_message("Failed auth for %s by %s",
         op->op_method_name, self->priv->name);
    op->op_tried_auth =0;
    op->op_auth_failed =1;
    return;
  }

1241
  priv_submit_authlist(op);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776

  op->op_tried_auth =1;
}


/**
 * nua_glib_op_owner:
 *
 * get the owning NuaGlib for a given NuaGlibOp
 */
NuaGlib* 
nua_glib_op_owner(NuaGlibOp *op)
{
  return op->op_parent;
}

sip_method_t
nua_glib_op_method_type(NuaGlibOp *op)
{
  return op->op_method;
}
/**
 * nua_glib_op_set_data:
 * @op: op to attach data to
 * @data: data to attach
 *
 * Attach an applciation specific blob of data to a NuaGlibOp
 * The application is in charge of deleting the data when it 
 * removes any internal references to the @op. When it has done 
 * so, it should call this function with @data set to NULL
 * Failing to do so will cause an assertion.
 */
void
nua_glib_op_set_data(NuaGlibOp *op, gpointer data)
{
  op->data = data;
}

/**
 * nua_glib_op_get_data:
 * @op: op to get data from
 *
 * Get an application specific blob of data from a NuaGlibOp
 * Returns: attached data
 */
gpointer
nua_glib_op_get_data(NuaGlibOp *op)
{
  return op->data ;
}

/**
 * nua_glib_op_get_identity:
 * @op: op to get data from
 *
 * Get the identity of an operation
 * This is the contents of To: when initiating, From: when receiving.
 * Returns: the identity
 */
const gchar *
nua_glib_op_get_identity(NuaGlibOp *op)
{
  return op->op_ident;
}


static void 
sof_i_error(nua_t *nua, NuaGlib *self, nua_handle_t *nh, NuaGlibOp *op, 
            int status, char const *phrase,
            tagi_t tags[])
{
  g_signal_emit(self, signals[ERROR], status, phrase);
}


#if 0
shouldnt be necessary in glib bindings
/**
 *  List active calls
 *
 */
GList*
nua_glib_list(NuaGlib *self)
{
  NuaGlibOp *op;

  for (op = self->priv->operations; op; op = op->op_next) {
    if (op->op_ident) {

             op->op_method, op->op_method_name, op->op_ident);
    }
  }

}

#endif

/**
 * nua_glib_invite:
 * @destination sip address to invite
 * @local_sdp an SDP blob describing local capabilites
 * 
 * Issue an INVITE
 * Return value: operation descriptor for this operation, NULL if failure
 *
 */

NuaGlibOp *
nua_glib_invite(NuaGlib *self, const char *destination, const char *local_sdp)
{
  NuaGlibOp *op;

  op = nua_glib_op_create(self, SIP_METHOD_INVITE, destination, TAG_END());

  /* SDP O/A note: 
   *  - pass media information to nua_invite() in the 
   *    SOATAG_USER_SDP_STR() tag
   *  - see also: sof_i_state() and nua_glib_answer()
   */ 

  if (op) {
    nua_invite(op->op_handle,
               SOATAG_USER_SDP_STR(local_sdp),
#if 0
               SIPTAG_SUBJECT_STR("Call!"),
               SIPTAG_CALL_INFO_STR("<http://127.1/my_face.jpg>"
                                    ";purpose=icon"),
               NUTAG_MEDIA_ENABLE(0),
#endif
               TAG_END());

    op->op_callstate |= opc_sent;
    return op;
  }
  return NULL;
}


/*
 * invite response handler
 */
static void 
sof_r_invite(int status, char const *phrase, 
             nua_t *nua, NuaGlib *self,
             nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
             tagi_t tags[])
{
  if (status >= 300) {
    op->op_callstate &= ~opc_sent;
    if (status == 401 || status == 407)
      oper_set_auth(self, op, sip, tags);
  }
  g_signal_emit(self, signals[INVITE_ANSWERED],0, op, status, phrase);
}

/*
 * incoming call-forked handler
 *
 * Releases forked calls
 */
static void 
sof_i_fork(int status, char const *phrase,
           nua_t *nua, NuaGlib *self,
           nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
           tagi_t tags[])
{
  nua_handle_t *nh2 = NULL;

  g_signal_emit(self, signals[CALL_FORKED], 0, status, phrase, op);
  
  g_warning("%s: call fork: %03d %s\n", self->priv->name, status, phrase);

  /* We just release forked calls. */
  tl_gets(tags, NUTAG_HANDLE_REF(nh2), TAG_END());
  g_return_if_fail(nh2);

  nua_bye(nh2, TAG_END());
  nua_handle_destroy(nh2);
}

/*
 * incoming invite handler
 */
static void 
sof_i_invite(nua_t *nua, NuaGlib *self,
             nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
             tagi_t tags[])
{
  /* Incoming call */
  sip_from_t const *from;
  sip_to_t const *to;
  sip_subject_t const *subject;

  g_return_if_fail(sip);

  from = sip->sip_from;
  to = sip->sip_to;
  subject = sip->sip_subject;
  char *url;

  g_return_if_fail(from && to);

  if (op) {
    op->op_callstate |= opc_recv;
  }
  else if ((op = nua_glib_op_create2(self, SIP_METHOD_INVITE, nh, from))) {
    op->op_callstate = opc_recv;
  }
  else {
    nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
    nua_handle_destroy(nh);
  }

  if (op) {
    if (op->op_callstate == opc_recv) {
      url = url_as_string(self->priv->home, to->a_url);
      g_signal_emit(self, signals[INCOMING_INVITE], 0, op, to->a_display, url, subject?subject->g_value:NULL); 
      su_free(self->priv->home, url);
    }
    else {
      g_signal_emit(self, signals[INCOMING_REINVITE], 0, op); 
    }
  }

}

/**
 * nua_glib_redirect:
 * @op: call to redirect
 * @contact: contact to redirect to
 * Redirect a call
 */
void
nua_glib_redirect(NuaGlib *self, 
                    NuaGlibOp *op, 
                    const char *contact)

{
  nua_respond(op->op_handle, SIP_302_MOVED_TEMPORARILY, 
              SIPTAG_CONTACT_STR(contact),
              TAG_END());
}

/** 
 * nua_glib_answer:
 * @op: operation returned from the incoming-invite signal
 * @status: SIP response status (see RFCs of SIP)
 * @phrase: Reponse text (default response phrase used if NULL)
 * @sdp: SDP description of local media capabilites
 *
 * Answer a call. 
 */
void 
nua_glib_answer(NuaGlib *self, 
                  NuaGlibOp *op,                         
                  int status, 
                  const char *phrase, 
                  const char *sdp)
{
  /* SDP O/A note: 
   *  - pass SDP information to nua_respond() in
   *    the SOATAG_USER_SDP_STR() tag
   *  - see also: sof_i_state() and nua_glib_invite()
   */ 
  
  if (status >= 200 && status < 300)
    op->op_callstate |= opc_sent;
  else
    op->op_callstate = opc_none;

  nua_respond(op->op_handle, status, phrase, 
              SOATAG_USER_SDP_STR(sdp),
              TAG_END());
}

static void 
sof_i_prack(nua_t *nua, NuaGlib *self,
            nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
            tagi_t tags[])
{
  /* Incoming PRACK */
  sip_rack_t const *rack;

  g_return_if_fail(sip);

  rack = sip->sip_rack;

  g_signal_emit(self, signals[INCOMING_PRACK], 0, op, rack ? rack->ra_response : 0);

  if (op == NULL)
    nua_handle_destroy(nh);
}

static void 
sof_i_state(int status, char const *phrase, 
            nua_t *nua, NuaGlib *self,
            nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
            tagi_t tags[])
{
  char const *l_sdp = NULL, *r_sdp = NULL;
  int audio = NUA_GLIB_MEDIA_INACTIVE;
  int video = NUA_GLIB_MEDIA_INACTIVE;
  int image = NUA_GLIB_MEDIA_INACTIVE;
  int chat = NUA_GLIB_MEDIA_INACTIVE;
  int offer_recv = 0, answer_recv = 0, offer_sent = 0, answer_sent = 0;
  int ss_state = nua_callstate_init;

  g_return_if_fail(op);

  tl_gets(tags, 
          NUTAG_CALLSTATE_REF(ss_state),
          NUTAG_ACTIVE_AUDIO_REF(audio), 
          NUTAG_ACTIVE_VIDEO_REF(video), 
          NUTAG_ACTIVE_IMAGE_REF(image), 
          NUTAG_ACTIVE_CHAT_REF(chat), 
          NUTAG_OFFER_RECV_REF(offer_recv),
          NUTAG_ANSWER_RECV_REF(answer_recv),
          NUTAG_OFFER_SENT_REF(offer_sent),
          NUTAG_ANSWER_SENT_REF(answer_sent),
          SOATAG_LOCAL_SDP_STR_REF(l_sdp),
          SOATAG_REMOTE_SDP_STR_REF(r_sdp),
          TAG_END());

  /* SDP O/A note: 
   *  - check the O/A state and whether local and/or remote SDP 
   *    is available (and whether it is updated)
   *  - inform media subsystem of the changes in configuration
   *  - see also: sof_i_state() and nua_glib_invite()
   */ 

  if (l_sdp) {
    g_return_if_fail(answer_sent || offer_sent);
  }

  if (r_sdp) {
    g_return_if_fail(answer_recv || offer_recv);
  }

  if (op->op_prev_state != ss_state) {
    /* note: only emit if state has changed */
    g_signal_emit(self, signals[CALL_STATE_CHANGED], 0, op, audio, video, image, chat, l_sdp, r_sdp); 
    op->op_prev_state = ss_state;
  }
}

static void 
sof_i_active(nua_t *nua, NuaGlib *self,
             nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
             tagi_t tags[])
{
  assert(op);

  op->op_callstate = opc_active;
  g_signal_emit(self, signals[INCOMING_ACTIVE], 0, op);
}

static gboolean 
idle_kill_op (gpointer data)
{
  NuaGlibOp* op = (NuaGlibOp *)data;

  nua_glib_op_destroy(op->op_parent, op);
  return FALSE;
}

static void 
sof_i_terminated(int status, char const *phrase, 
                 nua_t *nua, NuaGlib *self,
                 nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
                 tagi_t tags[])
{
  if (op) {
    g_signal_emit(self, signals[CALL_TERMINATED], 0, op, status);
    op->op_callstate = 0;
    g_idle_add(idle_kill_op, op);
    
  }

}

/**
 * nua_glib_bye:
 * @op the call to bye
 *
 * Initiate a BYE 
 * a bye-received signal will be emitted with the response to the bye
 */
void nua_glib_bye(NuaGlib *self, NuaGlibOp *op)
{
  nua_bye(op->op_handle, TAG_END());
  op->op_callstate = 0;
}

void sof_r_bye(int status, char const *phrase, 
               nua_t *nua, NuaGlib *self,
               nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
               tagi_t tags[])
{
  assert(op); assert(op->op_handle == nh);

  g_signal_emit(self, signals[BYE_ANSWERED], 0, op, status, phrase);
}

static void 
sof_i_bye(nua_t *nua, NuaGlib *self,
          nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
          tagi_t tags[])
{
  assert(op); assert(op->op_handle == nh);

  g_signal_emit(self, signals[INCOMING_BYE], 0, op);
}

/**
 * nua_glib_cancel:
 * @op the call to cancel
 *
 * Cancel a call
 * A cancel-received signal will be emitted with the response to the cancel 
 */
void nua_glib_cancel(NuaGlib *self, NuaGlibOp *op)
{
  nua_cancel(op->op_handle, TAG_END());
}

void sof_i_cancel(nua_t *nua, NuaGlib *self,
                    nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
                    tagi_t tags[])
{
  assert(op); assert(op->op_handle == nh);
  
  g_signal_emit(self, signals[INCOMING_CANCEL], 0, op);
}

/**
 * nua_glib_options:
 * @destination: URI to set options for
 * make an options request to the destination
 *
 * Return value: operation created for request
 */
NuaGlibOp *
nua_glib_options(NuaGlib *self, const char *destination)
{
  NuaGlibOp *op = nua_glib_op_create(self, SIP_METHOD_OPTIONS, destination,
                                   TAG_END());

  if (op) {
    nua_options(op->op_handle, TAG_END());
    return op;
  }
  else
    return NULL; 
}

static void 
sof_r_options(int status, char const *phrase,
              nua_t *nua, NuaGlib *self,
              nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
              tagi_t tags[])
{
  g_signal_emit(self, signals[OPTIONS_ANSWERED], 0, op, status, phrase);

  if (status == 401 || status == 407)
    oper_set_auth(self, op, sip, tags);

}

/**
 * nua_glib_message:
 * @destination destination address
 * @message: message to send
 * 
 * Send a message to a given destination address
 * Return value: operation created for the message, NULL cif failure
 */
NuaGlibOp * 
nua_glib_message(NuaGlib *self, const char *destination, const char *message)
{
  NuaGlibOp *op = nua_glib_op_create(self, SIP_METHOD_MESSAGE, destination, 
                                   TAG_END());

  if (op) {
    nua_message(op->op_handle,
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR(message),
                TAG_END());
    return op;
  }
  else
    return NULL;
}

static void 
sof_r_message(int status, char const *phrase,
              nua_t *nua, NuaGlib *self,
              nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
              tagi_t tags[])
{
  g_signal_emit(self, signals[MESSAGE_ANSWERED], 0, op, status, phrase);
  if (status < 200)
    return;

  if (status == 401 || status == 407)
    oper_set_auth(self, op, sip, tags);

}

static void 
sof_i_message(nua_t *nua, NuaGlib *self,
              nua_handle_t *nh, NuaGlibOp *op, sip_t const *sip,
              tagi_t tags[])
{
  /* Incoming message */
  sip_from_t const *from;
  sip_to_t const *to;
  sip_subject_t const *subject;
  GString *message;
  char *url;

  assert(sip);

  from = sip->sip_from;
  to = sip->sip_to;
  subject = sip->sip_subject;

  assert(from && to);

  if (sip->sip_payload)
    message=g_string_new_len(sip->sip_payload->pl_data, sip->sip_payload->pl_len);
  else
    message=NULL;

  
  url = url_as_string(self->priv->home, to->a_url);
1777 1778 1779 1780
  g_signal_emit(self, signals[INCOMING_MESSAGE], 0, op, to->a_display, url,
		subject ? subject->g_value : NULL,
		message ? message->str : NULL); 

1781
  su_free(self->priv->home, url);
1782 1783 1784

  if (message)
    g_string_free(message, TRUE);
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 <