Beispiel #1
0
/** @internal Create a handle for processing incoming request */
nua_handle_t *nua_stack_incoming_handle(nua_t *nua,
					nta_incoming_t *irq,
					sip_t const *sip,
					int create_dialog)
{
  nua_handle_t *nh;
  url_t const *url;
  sip_to_t to[1];
  sip_from_t from[1];

  assert(sip && sip->sip_from && sip->sip_to);

  if (sip->sip_contact)
    url = sip->sip_contact->m_url;
  else
    url = sip->sip_from->a_url;

  /* Strip away parameters */
  sip_from_init(from)->a_display = sip->sip_to->a_display;
  *from->a_url = *sip->sip_to->a_url;

  sip_to_init(to)->a_display = sip->sip_from->a_display;
  *to->a_url = *sip->sip_from->a_url;

  nh = nh_create(nua,
		 NUTAG_URL((url_string_t *)url), /* Remote target */
		 SIPTAG_TO(to), /* Local AoR */
		 SIPTAG_FROM(from), /* Remote AoR */
		 TAG_END());

  if (nh && nua_stack_init_handle(nua, nh, NULL) < 0)
    nh_destroy(nua, nh), nh = NULL;

  if (nh && create_dialog) {
    struct nua_dialog_state *ds = nh->nh_ds;

    nua_dialog_store_peer_info(nh, ds, sip);

    ds->ds_leg = nta_leg_tcreate(nua->nua_nta, nua_stack_process_request, nh,
				 SIPTAG_CALL_ID(sip->sip_call_id),
				 SIPTAG_FROM(sip->sip_to),
				 SIPTAG_TO(sip->sip_from),
				 NTATAG_REMOTE_CSEQ(sip->sip_cseq->cs_seq),
				 TAG_END());

    if (!ds->ds_leg || !nta_leg_tag(ds->ds_leg, nta_incoming_tag(irq, NULL)))
      nh_destroy(nua, nh), nh = NULL;
  }

  if (nh)
    nua_dialog_uas_route(nh, nh->nh_ds, sip, 1);

  return nh;
}
Beispiel #2
0
END_TEST

START_TEST(fetch_6_2_3)
{
    nua_handle_t *nh;
    struct message *subscribe;
    struct event *event;

    S2_CASE("6.2.3", "Event fetch - no NOTIFY",
            "NUA sends SUBSCRIBE with Expires 0, waits for NOTIFY, times out");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), SIPTAG_EXPIRES_STR("0"), TAG_END());
    subscribe = s2_sip_wait_for_request(SIP_METHOD_SUBSCRIBE);
    s2_sip_respond_to(subscribe, dialog, SIP_202_ACCEPTED,
                      SIPTAG_EXPIRES_STR("0"), TAG_END());
    s2_sip_free_message(subscribe);

    event = s2_wait_for_event(nua_r_subscribe, 202);
    fail_if(!event);
    fail_unless(s2_check_substate(event, nua_substate_embryonic));
    s2_free_event(event);

    s2_nua_fast_forward(600, s2base->root);

    event = s2_wait_for_event(nua_i_notify, 408);
    fail_if(!event);
    fail_unless(s2_check_substate(event, nua_substate_terminated));
    s2_free_event(event);

    nua_handle_destroy(nh);
}
Beispiel #3
0
END_TEST

START_TEST(subscribe_6_1_3)
{
    nua_handle_t *nh;
    struct message *response;
    struct event *notify, *event;

    S2_CASE("6.1.3", "Subscription terminated by notifier",
            "NUA sends SUBSCRIBE, waits for NOTIFY, "
            "gets NOTIFY terminating the subscription,");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), TAG_END());
    notify = subscription_by_nua(nh, nua_substate_embryonic, TAG_END());
    s2_free_event(notify);

    fail_if(s2_sip_request_to(dialog, SIP_METHOD_NOTIFY, NULL,
                              SIPTAG_EVENT_STR(event_type),
                              SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"),
                              TAG_END()));
    event = s2_wait_for_event(nua_i_notify, 200);
    fail_if(!event);
    fail_unless(s2_check_substate(event, nua_substate_terminated));
    s2_free_event(event);
    response = s2_sip_wait_for_response(200, SIP_METHOD_NOTIFY);
    fail_if(!response);
    s2_sip_free_message(response);

    nua_handle_destroy(nh);
}
/**
 * Create a new leg object for incoming request message.
 *
 * @param agent    agent object
 * @param callback function which is called for each 
 *                 incoming request belonging to this leg
 * @param magic    call leg context
 * @param msg      a request message
 *
 * @note The ownership of @a msg will pass back to NTA upon successful call
 * to the function nta_msg_leg(). In other words, if the call to @a
 * nta_msg_leg() is successful, the application may not do anything with @a
 * msg anymore.  Instead of that, NTA will create of a new incoming request
 * object for the @a msg and eventually return the request to application by
 * calling the @a callback function.
 *
 * @deprecated Use nta_leg_stateful() instead.
 */
nta_leg_t *nta_msg_leg(nta_agent_t *agent,
		       msg_t *msg,
		       nta_request_f *callback,
		       nta_leg_magic_t *magic,
		       ...)
{
  nta_leg_t *leg;
  sip_t *sip = sip_object(msg);

  SU_DEBUG_9(("\tnta_msg_leg(): called\n"));

  assert(msg && sip && sip->sip_request);

  if (!msg || !sip || !sip->sip_request || !callback)
    return NULL;

  leg = nta_leg_tcreate(agent, callback, magic,
			SIPTAG_CALL_ID(sip->sip_call_id),
			SIPTAG_FROM(sip->sip_to), /* local address */
			SIPTAG_TO(sip->sip_from), /* remote address */
			TAG_END());
  if (!leg) 
    /* xyzzy */;
  else if (nta_leg_server_route(leg, sip->sip_record_route, 
				sip->sip_contact) < 0)
    nta_leg_destroy(leg), leg = NULL;
  else if (nta_leg_stateful(leg, msg) < 0)
    nta_leg_destroy(leg), leg = NULL;

  SU_DEBUG_9(("\tnta_msg_leg(): returns %p\n", leg));

  return leg;
}
END_TEST

START_TEST(client_1_4_2)
{
    nua_handle_t *nh;
    struct message *message;
    struct event *response;

    S2_CASE("1.4.2", "Retry-After a delay",
            "Retry MESSAGE after a delay");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_message(nh,
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR("hello"),
                TAG_END());
    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL, SIP_503_SERVICE_UNAVAILABLE,
                      SIPTAG_RETRY_AFTER_STR("3"),
                      TAG_END());
    s2_sip_free_message(message);
    response = s2_wait_for_event(nua_r_message, 100);
    s2_free_event(response);

    s2_nua_fast_forward(32, s2base->root);

    /* Too long delay */
    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL, SIP_503_SERVICE_UNAVAILABLE,
                      SIPTAG_RETRY_AFTER_STR("32"),
                      TAG_END());
    s2_sip_free_message(message);
    response = s2_wait_for_event(nua_r_message, 503);
    s2_free_event(response);

    nua_set_params(nua, NUTAG_MAX_RETRY_AFTER(0), TAG_END());
    fail_unless_event(nua_r_set_params, 200);

    /* Disable feature */
    nua_message(nh,
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR("hello"),
                TAG_END());
    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL, SIP_503_SERVICE_UNAVAILABLE,
                      SIPTAG_RETRY_AFTER_STR("3"),
                      TAG_END());
    s2_sip_free_message(message);
    response = s2_wait_for_event(nua_r_message, 503);
    s2_free_event(response);

    nua_handle_destroy(nh);
}
END_TEST

START_TEST(SIP_CC_OE_CE_TI_011_012)
{
  nua_handle_t *nh;
  struct message *invite;

  S2_CASE("6.1.2", "SIP_CC_OE_CE_TI_011_012",
          "Ensure that the IUT, when an INVITE client transaction "
          "has been in the Terminated state, on receipt of a "
          "retransmitted Success (200 OK) responses sends an ACK "
          "request until 64*T1 duration expires, after this, "
          "on receipt of a retransmitted Success (200 OK) "
          "responses does not send an ACK request.");

  nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());

  invite = invite_sent_by_nua(nh, TAG_END());

  process_offer(invite);

  respond_with_sdp(invite, d1, SIP_200_OK, TAG_END());

  fail_unless_event(nua_r_invite, 200);
  fail_unless(s2_check_callstate(nua_callstate_ready));
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(5, s2base->root);
  respond_with_sdp(invite, d1, SIP_200_OK, TAG_END());
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(5, s2base->root);
  respond_with_sdp(invite, d1, SIP_200_OK, TAG_END());
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(20, s2base->root);
  respond_with_sdp(invite, d1, SIP_200_OK, TAG_END());
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  /* Stack times out the INVITE transaction */
  s2_nua_fast_forward(5, s2base->root);

  respond_with_sdp(invite, d1, SIP_200_OK,
		   SIPTAG_SUBJECT_STR("Stray 200 OK"),
		   TAG_END());
  s2_sip_free_message(invite);
  mark_point();
  fail_if(s2_sip_check_request_timeout(SIP_METHOD_ACK, 3));

  bye_by_nua(d1, nh, TAG_END());

  nua_handle_destroy(nh);
}
END_TEST

START_TEST(SIP_CC_OE_CE_TI_008)
{
  nua_handle_t *nh;
  struct message *invite;

  S2_CASE("6.1.2", "SIP_CC_OE_CE_TI_008",
          "If an unreliable transport is used, ensure that "
          "the IUT, when an INVITE client transaction is in "
          "the Completed state, on receipt of final responses "
          "that matches the transaction, still answer with an "
          "ACK request until timer D set to at least 32 second expires.");

  nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());

  invite = invite_sent_by_nua(nh, TAG_END());

  s2_sip_respond_to(invite, d1, 404, "First not found", TAG_END());
  fail_unless_event(nua_r_invite, 404);
  fail_unless(s2_check_callstate(nua_callstate_terminated));
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(5, s2base->root);

  s2_sip_respond_to(invite, d1, 404, "Not found after 5 seconds", TAG_END());
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(5, s2base->root);

  s2_sip_respond_to(invite, d1, 404, "Not found after 10 seconds", TAG_END());
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(21, s2base->root);

  s2_sip_respond_to(invite, d1, 404, "Not found after 31 seconds", TAG_END());
  fail_unless(s2_sip_check_request(SIP_METHOD_ACK));

  s2_nua_fast_forward(5, s2base->root);

  /* Wake up nua thread and let it time out INVITE transaction */
  nua_set_params(s2->nua, TAG_END());
  fail_unless_event(nua_r_set_params, 0);

  s2_sip_respond_to(invite, d1, 404, "Not found after 32 seconds", TAG_END());
  s2_sip_free_message(invite);
  fail_if(s2_sip_check_request_timeout(SIP_METHOD_ACK, 3));

  nua_handle_destroy(nh);
}
Beispiel #8
0
END_TEST

START_TEST(subscribe_6_1_2)
{
    nua_handle_t *nh;
    struct message *subscribe, *response;
    struct event *notify, *event;

    S2_CASE("6.1.2", "Basic subscription with refresh",
            "NUA sends SUBSCRIBE, waits for NOTIFY, "
            "sends re-SUBSCRIBE, waits for NOTIFY, "
            "sends un-SUBSCRIBE");

    send_notify_before_response = 1;

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), TAG_END());
    notify = subscription_by_nua(nh, nua_substate_embryonic, TAG_END());
    s2_free_event(notify);

    /* Wait for refresh */
    s2_nua_fast_forward(600, s2base->root);
    subscribe = s2_sip_wait_for_request(SIP_METHOD_SUBSCRIBE);
    s2_sip_respond_to(subscribe, dialog, SIP_200_OK,
                      SIPTAG_EXPIRES_STR("600"),
                      TAG_END());

    event = s2_wait_for_event(nua_r_subscribe, 200);
    fail_if(!event);
    fail_unless(s2_check_substate(event, nua_substate_active));
    s2_free_event(event);

    fail_if(s2_sip_request_to(dialog, SIP_METHOD_NOTIFY, NULL,
                              SIPTAG_EVENT(subscribe->sip->sip_event),
                              SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=600"),
                              SIPTAG_CONTENT_TYPE_STR(event_mime_type),
                              SIPTAG_PAYLOAD_STR(event_state),
                              TAG_END()));
    event = s2_wait_for_event(nua_i_notify, 200);
    fail_if(!event);
    fail_unless(s2_check_substate(event, nua_substate_active));
    s2_free_event(event);
    response = s2_sip_wait_for_response(200, SIP_METHOD_NOTIFY);
    fail_if(!response);
    s2_sip_free_message(response);

    unsubscribe_by_nua(nh, TAG_END());

    nua_handle_destroy(nh);
}
/**
 * Create a new leg object
 *
 * @param agent    agent object
 * @param callback function which is called for each
 *                 incoming request belonging to this leg
 * @param magic    call leg context
 * @param i        optional @CallID
 *                 (if @c NULL, an ID generated by @b NTA is used)
 * @param from     optional @From (local address)
 * @param to       optional @To (remote address)
 * @param extra    optional extra header
 * @param headers  va_list of optional extra headers
 *
 * @deprecated Use nta_leg_tcreate() instead.
 */
nta_leg_t *nta_leg_vcreate(nta_agent_t *agent,
			   nta_request_f *callback,
			   nta_leg_magic_t *magic,
			   sip_call_id_t const *i,
			   sip_from_t const *from,
			   sip_to_t const *to,
			   void const *extra, va_list headers)
{
  sip_route_t const *route = NULL;
  sip_cseq_t const *cseq = NULL;

  for (; extra ; extra = va_arg(headers, void *)) {
    sip_header_t const *h = (sip_header_t const *)extra;

    if (h == SIP_NONE)
      continue;
    else if (sip_call_id_p(h)) {
      if (i == NULL) i = h->sh_call_id;
    }
    else if (sip_from_p(h)) {
      if (from == NULL) from = h->sh_from;
    }
    else if (sip_to_p(h)) {
      if (to == NULL) to = h->sh_to;
    }
    else if (sip_route_p(h)) {
      route = h->sh_route;
    }
    else if (sip_cseq_p(h)) {
      cseq = h->sh_cseq;
    }
    else {
      SU_DEBUG_3(("nta_leg_create: extra header %s\n", 
		  sip_header_name(h, 0)));
    }
  }

  return nta_leg_tcreate(agent, callback, magic,
			 NTATAG_NO_DIALOG(i == SIP_NONE->sh_call_id),
			 TAG_IF(i != SIP_NONE->sh_call_id, SIPTAG_CALL_ID(i)),
			 TAG_IF(from != SIP_NONE->sh_from, SIPTAG_FROM(from)),
			 TAG_IF(to != SIP_NONE->sh_to, SIPTAG_TO(to)),
			 SIPTAG_ROUTE(route),
			 SIPTAG_CSEQ(cseq),
			 TAG_END());
}
Beispiel #10
0
END_TEST

START_TEST(subscribe_6_1_5)
{
    nua_handle_t *nh;
    struct event *notify;
    sip_via_t *vorig = s2_sip_tport_via(s2sip->udp.tport);
    sip_via_t via[2];
    char *v0_params[8] = {}, *v1_params[8] = {};
    char branch0[32], branch1[32];

    S2_CASE("6.1.5", "Via handling in response to NOTIFY",
            "NUA sends SUBSCRIBE, waits for NOTIFY, sends un-SUBSCRIBE");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), TAG_END());

    s2_sip_msg_flags = MSG_FLG_COMMA_LISTS|MSG_FLG_COMPACT;

    fail_if(vorig == NULL);

    via[0] = *vorig;
    via[0].v_host = "example.org";
    via[0].v_params = (void *)v0_params;
    snprintf(v0_params[0] = branch0, sizeof branch0,
             "branch=z9hG4bK%lx", ++s2sip->tid);

    fail_if(vorig == NULL);

    via[1] = *vorig;
    via[1].v_params = (void *)v1_params;
    snprintf(v1_params[0] = branch1, sizeof branch1,
             "branch=z9hG4bK%lx", ++s2sip->tid);

    notify = subscription_by_nua(nh, nua_substate_embryonic,
                                 SIPTAG_VIA(via + 1),
                                 SIPTAG_VIA(via + 0),
                                 TAG_END());

    s2_sip_msg_flags = 0;

    s2_free_event(notify);
    unsubscribe_by_nua(nh, TAG_END());
    nua_handle_destroy(nh);
}
Beispiel #11
0
END_TEST

START_TEST(subscribe_6_1_4)
{
    nua_handle_t *nh;
    struct message *response;
    struct event *notify, *event;

    S2_CASE("6.1.4", "Subscription terminated by notifier, re-established",
            "NUA sends SUBSCRIBE, waits for NOTIFY, "
            "gets NOTIFY terminating the subscription,");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), TAG_END());
    notify = subscription_by_nua(nh, nua_substate_embryonic, TAG_END());
    s2_free_event(notify);

    fail_if(s2_sip_request_to(dialog, SIP_METHOD_NOTIFY, NULL,
                              SIPTAG_EVENT_STR(event_type),
                              SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=deactivated"),
                              TAG_END()));
    event = s2_wait_for_event(nua_i_notify, 200);
    fail_if(!event);
    fail_unless(s2_check_substate(event, nua_substate_embryonic));
    s2_free_event(event);
    response = s2_sip_wait_for_response(200, SIP_METHOD_NOTIFY);
    fail_if(!response);
    s2_sip_free_message(response);

    su_home_unref((void *)dialog), dialog = su_home_new(sizeof *dialog);
    fail_if(!dialog);

    s2_nua_fast_forward(5, s2base->root);
    /* nua re-establishes the subscription */
    notify = subscription_by_nua(nh, nua_substate_embryonic, TAG_END());
    s2_free_event(notify);

    /* Unsubscribe with nua_subscribe() Expires: 0 */
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), SIPTAG_EXPIRES_STR("0"), TAG_END());
    notify = subscription_by_nua(nh, nua_substate_active, TAG_END());
    s2_free_event(notify);

    nua_handle_destroy(nh);
}
Beispiel #12
0
/** Send a BYE to an INVITE.
 *
 * @deprecated
 * This function should used only if application requires 
 * RFC2543 compatibility.
 */
nta_outgoing_t *nta_outgoing_tbye(nta_outgoing_t *orq,
				  nta_response_f *callback,
				  nta_outgoing_magic_t *magic,
				  url_string_t const *route_url,
				  tag_type_t tag, tag_value_t value, ...)
{
  msg_t *msg;
  sip_t *sip, *inv;
  sip_cseq_t *cs;
  sip_request_t *rq;
  su_home_t *home;
  url_string_t *url;

  if (orq == NULL || orq->orq_method != sip_method_invite)
    return NULL;

  inv = sip_object(orq->orq_request);
  msg = nta_msg_create(orq->orq_agent, 0);
  home = msg_home(msg);
  sip = sip_object(msg);

  if (inv == NULL || sip == NULL) {
    msg_destroy(msg);
    return NULL;
  }

  sip_add_tl(msg, sip,
	     SIPTAG_TO(inv->sip_to),
	     SIPTAG_FROM(inv->sip_from),
	     SIPTAG_CALL_ID(inv->sip_call_id),
	     SIPTAG_ROUTE(inv->sip_route),
	     TAG_END());

  url = (url_string_t *)inv->sip_request->rq_url;

  rq = sip_request_create(home, SIP_METHOD_BYE, url, NULL);
  sip_header_insert(msg, sip, (sip_header_t*)rq);

  cs = sip_cseq_create(home, inv->sip_cseq->cs_seq + 1, SIP_METHOD_BYE);
  sip_header_insert(msg, sip, (sip_header_t*)cs);

  return nta_outgoing_mcreate(orq->orq_agent, callback, magic, 
			      route_url, msg);
}
Beispiel #13
0
END_TEST

START_TEST(fetch_6_2_2)
{
    nua_handle_t *nh;
    struct event *notify;

    S2_CASE("6.2.2", "Event fetch - NOTIFY before 200",
            "NUA sends SUBSCRIBE with Expires 0, waits for NOTIFY");

    send_notify_before_response = 1;

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_subscribe(nh, SIPTAG_EVENT_STR(event_type), SIPTAG_EXPIRES_STR("0"), TAG_END());
    notify = subscription_by_nua(nh, nua_substate_embryonic, TAG_END());
    s2_check_substate(notify, nua_substate_terminated);
    s2_free_event(notify);
    nua_handle_destroy(nh);
}
Beispiel #14
0
END_TEST

START_TEST(message_6_4_3)
{
    nua_handle_t *nh;
    struct message *message;
    struct event *response;
    sip_call_id_t *i;

    S2_CASE("6.4.1", "SIMPLE MESSAGE",
            "Send MESSAGE");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());
    nua_message(nh,
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR("hello"),
                TAG_END());
    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL,
                      SIP_407_PROXY_AUTH_REQUIRED,
                      SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
                      TAG_END());
    i = sip_call_id_dup(NULL, message->sip->sip_call_id);
    fail_unless(i != NULL);
    s2_sip_free_message(message);
    fail_unless_event(nua_r_message, 407);

    nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());

    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL, SIP_202_ACCEPTED, TAG_END());
    fail_unless(su_strmatch(i->i_id, message->sip->sip_call_id->i_id));
    s2_sip_free_message(message);
    response = s2_wait_for_event(nua_r_message, 202);
    s2_free_event(response);

    nua_handle_destroy(nh);
}
int test_extension(struct context *ctx)
{
  BEGIN();

  struct endpoint *a = &ctx->a,  *b = &ctx->b;
  struct call *a_call = a->call, *b_call = b->call;
  struct event *e;
  sip_t const *sip;


/* Test for EXTENSION

   A			B
   |------EXTENSION---->|
   |<--------501--------| (method not recognized)
   |			|
   |------EXTENSION---->|
   |<-------200---------| (method allowed, responded)
   |			|
*/

  if (print_headings)
    printf("TEST NUA-13.1: EXTENSION\n");


  TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));

  /* Test first without NUTAG_METHOD() */
  METHOD(a, a_call, a_call->nh,
	 TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
	 TAG_END());

  run_ab_until(ctx, -1, save_until_final_response, -1, NULL);

  /* Client events:
     nua_method(), nua_r_method
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_method);
  TEST(e->data->e_status, 900);	/* Internal error */
  TEST_1(!e->data->e_msg);
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);
  nua_handle_destroy(a_call->nh), a_call->nh = NULL;

  TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));

  METHOD(a, a_call, a_call->nh,
	 TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
	 NUTAG_METHOD("EXTENSION"),
	 TAG_END());

  run_ab_until(ctx, -1, save_until_final_response, -1, NULL);

  /* Client events:
     nua_method(), nua_r_method
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_method);
  TEST(e->data->e_status, 501);
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);
  nua_handle_destroy(a_call->nh), a_call->nh = NULL;

  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  nua_set_params(b->nua, NUTAG_ALLOW("EXTENSION"), TAG_END());

  run_b_until(ctx, nua_r_set_params, until_final_response);

  TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));

  METHOD(a, a_call, a_call->nh,
	 TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
	 NUTAG_METHOD("EXTENSION"),
	 TAG_END());

  run_ab_until(ctx, -1, save_until_final_response, -1, respond_to_extension);

  /* Client events:
     nua_method(), nua_r_method
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_method);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(!e->next);

  /*
   Server events:
   nua_i_method
  */
  TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_method);
  TEST(e->data->e_status, 100);
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);
  nua_handle_destroy(a_call->nh), a_call->nh = NULL;

  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  nua_set_params(b->nua,
		 SIPTAG_ALLOW(b->allow),
		 NUTAG_APPL_METHOD(NULL),
		 NUTAG_APPL_METHOD(b->appl_method),
		 TAG_END());
  run_b_until(ctx, nua_r_set_params, until_final_response);

  if (print_headings)
    printf("TEST NUA-13.1: PASSED\n");
  END();
}
Beispiel #16
0
END_TEST

START_TEST(message_6_4_2)
{
    nua_handle_t *nh;
    struct message *message;
    struct event *response;

    S2_CASE("6.4.2", "MESSAGE with 302/305",
            "Send MESSAGE");

    nh = nua_handle(nua, NULL, SIPTAG_TO(s2sip->aor), TAG_END());

    nua_message(nh,
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR("hello"),
                TAG_END());
    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL, SIP_302_MOVED_TEMPORARILY,
                      SIPTAG_CONTACT_STR("<sip:[email protected]>"),
                      TAG_END());
    s2_sip_free_message(message);

    response = s2_wait_for_event(nua_r_message, 100);
    s2_free_event(response);

    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    fail_unless(message->sip->sip_request->rq_url->url_user != NULL);
    fail_if(strcmp(message->sip->sip_request->rq_url->url_user, "302ed"));
    s2_sip_respond_to(message, NULL, SIP_305_USE_PROXY,
                      SIPTAG_CONTACT_STR("<sip:[email protected];lr>"),
                      TAG_END());
    s2_sip_free_message(message);

    response = s2_wait_for_event(nua_r_message, 100);
    s2_free_event(response);

    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    fail_unless(message->sip->sip_route != NULL);
    fail_unless(message->sip->sip_route->r_url->url_user != NULL);
    fail_if(strcmp(message->sip->sip_route->r_url->url_user, "routed"));
    s2_sip_respond_to(message, NULL, SIP_200_OK, TAG_END());
    s2_sip_free_message(message);

    response = s2_wait_for_event(nua_r_message, 200);
    s2_free_event(response);

    /* ---------------------------------------------------------------------- */

    nua_message(nh,
                NUTAG_AUTO302(0),
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR("hello 2"),
                TAG_END());

    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    s2_sip_respond_to(message, NULL, SIP_302_MOVED_TEMPORARILY,
                      SIPTAG_CONTACT_STR("<sip:[email protected]>"),
                      TAG_END());
    s2_sip_free_message(message);

    response = s2_wait_for_event(nua_r_message, 302);

    fail_unless(response->sip && response->sip->sip_contact);
    nua_message(nh,
                NUTAG_URL(response->sip->sip_contact->m_url),
                NUTAG_AUTO305(0),
                SIPTAG_CONTENT_TYPE_STR("text/plain"),
                SIPTAG_PAYLOAD_STR("hello 2"),
                TAG_END());

    s2_free_event(response);

    message = s2_sip_wait_for_request(SIP_METHOD_MESSAGE);
    fail_unless(message->sip->sip_request->rq_url->url_user != NULL);
    fail_if(strcmp(message->sip->sip_request->rq_url->url_user, "not302ed"));
    s2_sip_respond_to(message, NULL, SIP_305_USE_PROXY,
                      SIPTAG_CONTACT_STR("<sip:[email protected]>"),
                      TAG_END());
    s2_sip_free_message(message);

    response = s2_wait_for_event(nua_r_message, 305);
    s2_free_event(response);

    nua_handle_destroy(nh);
}
    int DrachtioController::processRequestOutsideDialog( nta_leg_t* defaultLeg, nta_incoming_t* irq, sip_t const *sip) {
        DR_LOG(log_debug) << "processRequestOutsideDialog" << endl ;
        int rc = validateSipMessage( sip ) ;
        if( 0 != rc ) {
            return rc ;
        }

        switch (sip->sip_request->rq_method ) {
            case sip_method_invite:
            {
                /* TODO:  should support optional config to only allow invites from defined addresses */

                /* system-wide minimum session-expires is 90 seconds */
                if( sip->sip_session_expires && sip->sip_session_expires->x_delta < 90 ) {
                      nta_incoming_treply( irq, SIP_422_SESSION_TIMER_TOO_SMALL, 
                        SIPTAG_MIN_SE_STR("90"),
                        TAG_END() ) ; 
                      return 0;
                } 

                string transactionId ;
                generateUuid( transactionId ) ;

                nta_incoming_treply( irq, SIP_100_TRYING, TAG_END() ) ;                
 
                 if( !m_pClientController->route_request_outside_dialog( irq, sip, transactionId ) )  {
                    DR_LOG(log_error) << "No providers available for invite" << endl ;
                    return 503 ;
                }

                nta_leg_t* leg = nta_leg_tcreate(m_nta, legCallback, this,
                                                   SIPTAG_CALL_ID(sip->sip_call_id),
                                                   SIPTAG_CSEQ(sip->sip_cseq),
                                                   SIPTAG_TO(sip->sip_from),
                                                   SIPTAG_FROM(sip->sip_to),
                                                   TAG_END());
                if( NULL == leg ) {
                    DR_LOG(log_error) << "Error creating a leg for  origination" << endl ;
                    //TODO: we got a client out there with a dead INVITE now...
                    return 500 ;
                }
                boost::shared_ptr<SipDialog> dlg = boost::make_shared<SipDialog>( leg, irq, sip ) ;
                dlg->setTransactionId( transactionId ) ;

                string contactStr ;
                generateOutgoingContact( sip->sip_contact, contactStr ) ;
                nta_leg_server_route( leg, sip->sip_record_route, sip->sip_contact ) ;

                m_pDialogController->addIncomingInviteTransaction( leg, irq, sip, transactionId, dlg ) ;


            }
            break ;

            case sip_method_ack:

                /* success case: call has been established */
                nta_incoming_destroy( irq ) ;
                return 0 ;               
            case sip_method_register:
            case sip_method_message:
            case sip_method_options:
            case sip_method_notify:
            {
                string transactionId ;
                generateUuid( transactionId ) ;

                if( !m_pClientController->route_request_outside_dialog( irq, sip, transactionId ) )  {
                    DR_LOG(log_error) << "No providers available for register" << endl ;
                    return 503 ;
                }

                m_pDialogController->addIncomingRequestTransaction( irq, transactionId ) ;
                return 0 ;
            }
            
            case sip_method_bye:
            case sip_method_cancel:
                DR_LOG(log_error) << "Received BYE or CANCEL for unknown dialog: " << sip->sip_call_id->i_id << endl ;
                return 481 ;
                
            default:
                DR_LOG(log_error) << "DrachtioController::processRequestOutsideDialog - unsupported method type: " << sip->sip_request->rq_method_name << ": " << sip->sip_call_id->i_id << endl ;
                return 501 ;
                break ;
                
        }
        
        return 0 ;
    }
Beispiel #18
0
/** @internal Create a message template for keepalive. */
static int create_keepalive_message(outbound_t *ob, sip_t const *regsip)
{
  msg_t *msg = nta_msg_create(ob->ob_nta, MSG_FLG_COMPACT), *previous;
  sip_t *osip = sip_object(msg);
  sip_contact_t *m = ob->ob_rcontact;

  unsigned d = ob->ob_keepalive.interval;

  if (msg == NULL)
    return -1;

  assert(regsip); assert(regsip->sip_request);

  if (m && m->m_params) {
    sip_accept_contact_t *ac;
    size_t i;
    int features = 0;

    ac = sip_accept_contact_make(msg_home(msg), "*;require;explicit");

    for (i = 0; m->m_params[i]; i++) {
      char const *s = m->m_params[i];
      if (!sip_is_callerpref(s))
	continue;
      features++;
      s = su_strdup(msg_home(msg), s);
      msg_header_add_param(msg_home(msg), ac->cp_common, s);
    }

    if (features)
      msg_header_insert(msg, NULL, (void *)ac);
    else
      msg_header_free(msg_home(msg), (void *)ac);
  }

  if (0 >
      /* Duplicate essential headers from REGISTER request: */
      sip_add_tl(msg, osip,
		 SIPTAG_TO(regsip->sip_to),
		 SIPTAG_FROM(regsip->sip_from),
		 /* XXX - we should only use loose routing here */
		 /* XXX - if we used strict routing,
		    the route header/request_uri must be restored
		 */
		 SIPTAG_ROUTE(regsip->sip_route),
		 /* Add Max-Forwards 0 */
		 TAG_IF(d, SIPTAG_MAX_FORWARDS_STR("0")),
		 TAG_IF(d, SIPTAG_SUBJECT_STR("KEEPALIVE")),
		 SIPTAG_CALL_ID_STR(ob->ob_cookie),
		 SIPTAG_ACCEPT_STR(outbound_content_type),
		 TAG_END()) ||
      /* Create request-line, Call-ID, CSeq */
      nta_msg_request_complete(msg,
       			nta_default_leg(ob->ob_nta),
       			SIP_METHOD_OPTIONS,
       			(void *)regsip->sip_to->a_url) < 0 ||
      msg_serialize(msg, (void *)osip) < 0 ||
      msg_prepare(msg) < 0)
    return msg_destroy(msg), -1;

  previous = ob->ob_keepalive.msg;
  ob->ob_keepalive.msg = msg;
  msg_destroy(previous);

  return 0;
}
Beispiel #19
0
/**
 * Creates a new operation object and stores it the list of
 * active operations for 'cli'.
 */
ssc_oper_t *ssc_oper_create(ssc_t *ssc,
                            sip_method_t method,
                            char const *name,
                            char const *address,
                            tag_type_t tag, tag_value_t value, ...)
{
    ssc_oper_t *op, *old;

    ta_list ta;

    enter;

    for (old = ssc->ssc_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(ssc->ssc_home, address);

        if (to == NULL) {
            printf("%s: %s: invalid address: %s\n", ssc->ssc_name, name, address);
            return NULL;
        }

        /* Try to make sense out of the URL */
        if (url_sanitize(to->a_url) < 0) {
            printf("%s: %s: invalid address\n", ssc->ssc_name, name);
            return NULL;
        }

        if (!(op = su_zalloc(ssc->ssc_home, sizeof(*op)))) {
            printf("%s: %s: cannot create handle\n", ssc->ssc_name, name);
            return NULL;
        }

        op->op_next = ssc->ssc_operations;
        op->op_prev_state = -1;
        op->op_ssc = ssc;
        ssc->ssc_operations = op;

        if (method == sip_method_register)
            have_url = 0;

        ta_start(ta, tag, value);

        op->op_handle = nua_handle(ssc->ssc_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(ssc->ssc_home, (sip_header_t *)to);

        ssc_oper_assign(op, method, name);

        if (!op->op_persistent) {
            ssc_oper_t *old_next;
            for (; old; old = old_next) {      /* Clean old handles */
                old_next = old->op_next;
                if (!old->op_persistent && !old->op_callstate)
                    ssc_oper_destroy(ssc, old);
            }
        }

        su_free(ssc->ssc_home, to);
    }
    else if (method || name)
        ssc_oper_assign(op = old, method, name);
    else
        return old;

    if (!op) {
        if (address)
            printf("%s: %s: invalid destination\n", ssc->ssc_name, name);
        else
            printf("%s: %s: no destination\n", ssc->ssc_name, name);
        return NULL;
    }

    return op;
}
Beispiel #20
0
/**Send a request message.
 *
 * @retval 0 if request is pending
 * @retval >=1 if error event has been sent
 * @retval < 0 if no error event has been sent
 */
static
int nua_client_request_sendmsg(nua_client_request_t *cr)
{
  nua_handle_t *nh = cr->cr_owner;
  nua_dialog_state_t *ds = nh->nh_ds;
  sip_method_t method = cr->cr_method;
  char const *name = cr->cr_method_name;
  url_string_t const *url = (url_string_t *)cr->cr_target;
  nta_leg_t *leg;
  msg_t *msg;
  sip_t *sip;
  int error;

  assert(cr->cr_orq == NULL);

  cr->cr_offer_sent = cr->cr_answer_recv = 0;
  cr->cr_offer_recv = cr->cr_answer_sent = 0;

  if (!ds->ds_leg && cr->cr_dialog) {
    ds->ds_leg = nta_leg_tcreate(nh->nh_nua->nua_nta,
				 nua_stack_process_request, nh,
				 SIPTAG_CALL_ID(cr->cr_sip->sip_call_id),
				 SIPTAG_FROM(cr->cr_sip->sip_from),
				 SIPTAG_TO(cr->cr_sip->sip_to),
				 SIPTAG_CSEQ(cr->cr_sip->sip_cseq),
				 TAG_END());
    if (!ds->ds_leg)
      return -1;
  }

  if (cr->cr_sip->sip_from && ds->ds_leg) {
    if (cr->cr_sip->sip_from->a_tag == NULL) {
      if (sip_from_tag(msg_home(cr->cr_msg), cr->cr_sip->sip_from,
		       nta_leg_tag(ds->ds_leg, NULL)) < 0) {
	return -1;
      }
    }
  }

  cr->cr_retry_count++;

  if (ds->ds_leg)
    leg = ds->ds_leg;
  else
    leg = nh->nh_nua->nua_dhandle->nh_ds->ds_leg; /* Default leg */

  msg = msg_copy(cr->cr_msg), sip = sip_object(msg);

  if (msg == NULL)
    return -1;

  if (nua_dialog_is_established(ds)) {
    while (sip->sip_route)
      sip_route_remove(msg, sip);
  }
  else if (!ds->ds_route) {
    sip_route_t *initial_route = NH_PGET(nh, initial_route);

    if (initial_route) {
      initial_route = sip_route_dup(msg_home(msg), initial_route);
      if (!initial_route) return -1;
      msg_header_prepend(msg, (msg_pub_t*)sip,
			 /* This should be
			    (msg_header_t **)&sip->sip_route
			  * but directly casting pointer &sip->sip_route gives
			  * spurious type-punning warning */
			 (msg_header_t **)((char *)sip + offsetof(sip_t, sip_route)),
			 (msg_header_t *)initial_route);
    }
  }


  /**
   * For in-dialog requests, the request URI is taken from the @Contact
   * header received from the remote party during dialog establishment,
   * and the NUTAG_URL() is ignored.
   *
   * Also, the @CallID and @CSeq headers and @From and @To tags are
   * generated based on the dialog information and added to the request.
   * If the dialog has a route, it is added to the request, too.
   */
  if (nta_msg_request_complete(msg, leg, method, name, url) < 0) {
    msg_destroy(msg);
    return -1;
  }

  /**@MaxForwards header (with default value set by NTATAG_MAX_FORWARDS()) is
   * also added now, if it does not exist.
   */

  if (!ds->ds_remote)
    ds->ds_remote = sip_to_dup(nh->nh_home, sip->sip_to);
  if (!ds->ds_local)
    ds->ds_local = sip_from_dup(nh->nh_home, sip->sip_from);

  /**
   * Next, values previously set with nua_set_params() or nua_set_hparams()
   * are used: @Allow, @Supported, @Organization, @UserAgent and
   * @AllowEvents headers are added to the request if they are not already
   * set.
   */
  if (!sip->sip_allow)
    sip_add_dup(msg, sip, (sip_header_t*)NH_PGET(nh, allow));

  if (!sip->sip_supported && NH_PGET(nh, supported))
    sip_add_dup(msg, sip, (sip_header_t *)NH_PGET(nh, supported));

  if (method == sip_method_register && NH_PGET(nh, path_enable) &&
      !sip_has_feature(sip->sip_supported, "path") &&
      !sip_has_feature(sip->sip_require, "path"))
    sip_add_make(msg, sip, sip_supported_class, "path");

  if (!sip->sip_organization && NH_PGET(nh, organization))
    sip_add_make(msg, sip, sip_organization_class, NH_PGET(nh, organization));

  if (!sip->sip_user_agent && NH_PGET(nh, user_agent))
    sip_add_make(msg, sip, sip_user_agent_class, NH_PGET(nh, user_agent));

  /** Any node implementing one or more event packages SHOULD include an
   * appropriate @AllowEvents header indicating all supported events in
   * all methods which initiate dialogs and their responses (such as
   * INVITE) and OPTIONS responses.
   */
  if (!sip->sip_allow_events &&
      NH_PGET(nh, allow_events) &&
      (method == sip_method_notify || /* Always in NOTIFY */
       (!ds->ds_remote_tag &&	      /* And in initial requests */
	(method == sip_method_subscribe || method == sip_method_refer ||
	 method == sip_method_options ||
	 method == sip_method_invite))))
    sip_add_dup(msg, sip, (void *)NH_PGET(nh, allow_events));

  /**
   * Next, the stack generates a @Contact header for the request (unless
   * the application already gave a @Contact header or it does not want to
   * use @Contact and indicates that by including SIPTAG_CONTACT(NULL) or
   * SIPTAG_CONTACT(SIP_NONE) in the tagged parameters.) If the
   * application has registered the URI in @From header, the @Contact
   * header used with registration is used. Otherwise, the @Contact header
   * is generated from the local IP address and port number.
   */

  /**For the initial requests, @ServiceRoute set that was received from the
   * registrar is also added to the request message.
   */
  if (cr->cr_method != sip_method_register) {
    if (cr->cr_contactize && cr->cr_has_contact) {
      sip_contact_t *ltarget = sip_contact_dup(nh->nh_home, sip->sip_contact);
      if (ds->ds_ltarget)
	msg_header_free(nh->nh_home, (msg_header_t *)ds->ds_ltarget);
      ds->ds_ltarget = ltarget;
    }

    if (ds->ds_ltarget && !cr->cr_has_contact)
      sip_add_dup(msg, sip, (sip_header_t *)ds->ds_ltarget);

    if (nua_registration_add_contact_to_request(nh, msg, sip,
						cr->cr_contactize &&
						!cr->cr_has_contact &&
						!ds->ds_ltarget,
						!ds->ds_route) < 0) {
      msg_destroy(msg);
      return -1;
    }
  }

  cr->cr_wait_for_cred = 0;

  if (cr->cr_methods->crm_send)
    error = cr->cr_methods->crm_send(cr, msg, sip, NULL);
  else
    error = nua_base_client_request(cr, msg, sip, NULL);

  if (error == -1)
    msg_destroy(msg);

  return error;
}
Beispiel #21
0
int test_events(struct context *ctx)
{
  BEGIN();

  struct endpoint *a = &ctx->a,  *b = &ctx->b;
  struct call *a_call = a->call, *b_call = b->call;
  struct event *e, *en, *es;
  sip_t const *sip;
  tagi_t const *t, *n_tags, *r_tags;
  url_t b_url[1];
  enum nua_substate substate;
  nea_sub_t *sub = NULL;

  char const open[] =
    "<?xml version='1.0' encoding='UTF-8'?>\n"
    "<presence xmlns='urn:ietf:params:xml:ns:cpim-pidf' \n"
    "   entity='pres:[email protected]'>\n"
    "  <tuple id='ksac9udshce'>\n"
    "    <status><basic>open</basic></status>\n"
    "    <contact priority='1.0'>sip:[email protected]</contact>\n"
    "  </tuple>\n"
    "</presence>\n";

  char const closed[] =
    "<?xml version='1.0' encoding='UTF-8'?>\n"
    "<presence xmlns='urn:ietf:params:xml:ns:cpim-pidf' \n"
    "   entity='pres:[email protected]'>\n"
    "  <tuple id='ksac9udshce'>\n"
    "    <status><basic>closed</basic></status>\n"
    "  </tuple>\n"
    "</presence>\n";


/* SUBSCRIBE test

   A			B
   |------SUBSCRIBE---->|
   |<--------405--------|
   |			|

*/
  if (print_headings)
    printf("TEST NUA-12.1: SUBSCRIBE without notifier\n");

  nua_set_params(b->nua, SIPTAG_ALLOW_EVENTS(NULL), TAG_END());
  run_b_until(ctx, nua_r_set_params, until_final_response);

  TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));

  SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
	    SIPTAG_EVENT_STR("presence"),
	    TAG_END());

  run_ab_until(ctx, -1, save_until_final_response,
	       -1, NULL /* XXX save_until_received */);

  /* Client events:
     nua_subscribe(), nua_r_subscribe
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_subscribe);
  TEST(e->data->e_status, 489);
  TEST_1(!e->next);

#if 0				/* XXX */
  /*
   Server events:
   nua_i_subscribe
  */
  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_i_subscribe);
  TEST(e->data->e_status, 405);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event);
  TEST_S(sip->sip_event->o_type, "presence");
  TEST_1(!e->next);
#endif

  free_events_in_list(ctx, a->events);
  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  if (print_headings)
    printf("TEST NUA-12.1: PASSED\n");

  /* ---------------------------------------------------------------------- */

/* SUBSCRIBE test using notifier and establishing subscription

   A			B
   |                    |
   |------SUBSCRIBE---->|
   |<--------202--------|
   |<------NOTIFY-------|
   |-------200 OK------>|
   |                    |
*/

  if (print_headings)
    printf("TEST NUA-12.2: using notifier and establishing subscription\n");

  TEST_1(b_call->nh = nua_handle(b->nua, b_call, TAG_END()));

  *b_url = *b->contact->m_url;

  NOTIFIER(b, b_call, b_call->nh,
	   NUTAG_URL(b_url),
	   SIPTAG_EVENT_STR("presence"),
	   SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
	   SIPTAG_PAYLOAD_STR(closed),
	   NEATAG_THROTTLE(1),
	   TAG_END());
  run_b_until(ctx, nua_r_notifier, until_final_response);

  SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
	    SIPTAG_EVENT_STR("presence"),
	    SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
	    TAG_END());

  run_ab_until(ctx, -1, save_until_notified_and_responded,
	       -1, save_until_received);

  /* Client events:
     nua_subscribe(), nua_i_notify/nua_r_subscribe
  */
  TEST_1(en = event_by_type(a->events->head, nua_i_notify));
  TEST_1(es = event_by_type(a->events->head, nua_r_subscribe));

  TEST_1(e = es); TEST_E(e->data->e_event, nua_r_subscribe);
  r_tags = e->data->e_tags;
  TEST_1(tl_find(r_tags, nutag_substate));
  if (es->next == en) {
    TEST_1(200 <= e->data->e_status && e->data->e_status < 300);
    TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_embryonic);
  }
  else {
    TEST_1(200 <= e->data->e_status && e->data->e_status < 300);
    TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
  }

  TEST_1(e = en); TEST_E(e->data->e_event, nua_i_notify);
  TEST_1(sip = sip_object(e->data->e_msg));
  n_tags = e->data->e_tags;

  TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
  TEST_1(sip->sip_content_type);
  TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "active");
  TEST_1(sip->sip_subscription_state->ss_expires);
  TEST_1(tl_find(n_tags, nutag_substate));
  TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
  TEST_1(!en->next || !es->next);
  free_events_in_list(ctx, a->events);

  /* XXX --- Do not check server side events */
  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-12.2: PASSED\n");

  /* ---------------------------------------------------------------------- */

/* NOTIFY with updated content

   A			B
   |                    |
   |<------NOTIFY-------|
   |-------200 OK------>|
   |                    |
*/
  if (print_headings)
    printf("TEST NUA-12.3: update notifier\n");

  /* Update presence data */

  NOTIFIER(b, b_call, b_call->nh,
	   SIPTAG_EVENT_STR("presence"),
	   SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
	   SIPTAG_PAYLOAD_STR(open),
	   TAG_END());

  run_ab_until(ctx, -1, save_until_notified, -1, save_until_received);

  /* subscriber events:
     nua_i_notify
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_notify);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
  TEST_1(sip->sip_content_type);
  TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "active");
  TEST_1(sip->sip_subscription_state->ss_expires);
  n_tags = e->data->e_tags;
  TEST_1(tl_find(n_tags, nutag_substate));
  TEST(tl_find(n_tags, nutag_substate)->t_value,
       nua_substate_active);
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  /* XXX --- Do not check server side events */
  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-12.3: PASSED\n");

  /* ---------------------------------------------------------------------- */

/* un-SUBSCRIBE

   A			B
   |                    |
   |------SUBSCRIBE---->|
   |<--------202--------|
   |<------NOTIFY-------|
   |-------200 OK------>|
   |                    |
*/
  if (print_headings)
    printf("TEST NUA-12.5: un-SUBSCRIBE\n");

  memset(&a->flags, 0, sizeof a->flags);

  UNSUBSCRIBE(a, a_call, a_call->nh, TAG_END());

  run_ab_until(ctx, -1, save_until_notified_and_responded,
	       -1, save_until_subscription);

  /* Client events:
     nua_unsubscribe(), nua_i_notify/nua_r_unsubscribe
  */
  TEST_1(e = a->events->head);
  if (e->data->e_event == nua_i_notify) {
    TEST_E(e->data->e_event, nua_i_notify);
    TEST_1(sip = sip_object(e->data->e_msg));
    n_tags = e->data->e_tags;
    TEST_1(sip->sip_event);
    TEST_1(sip->sip_subscription_state);
    TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
    TEST_1(!sip->sip_subscription_state->ss_expires);
    TEST_1(tl_find(n_tags, nutag_substate));
    TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);

    TEST_1(e = e->next);
    TEST_E(e->data->e_event, nua_r_unsubscribe);
    TEST_1(tl_find(e->data->e_tags, nutag_substate));
    TEST(tl_find(e->data->e_tags, nutag_substate)->t_value,
       nua_substate_terminated);
  }
  else {
    TEST_E(e->data->e_event, nua_r_unsubscribe);
    TEST_1(tl_find(e->data->e_tags, nutag_substate));
    /* NOTIFY is no more dropped after successful response to unsubscribe */
    TEST(tl_find(e->data->e_tags, nutag_substate)->t_value,
       nua_substate_active);

    TEST_1(e = e->next);
    TEST_E(e->data->e_event, nua_i_notify);
    TEST_1(sip = sip_object(e->data->e_msg));
    n_tags = e->data->e_tags;
    TEST_1(sip->sip_event);
    TEST_1(sip->sip_subscription_state);
    TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
    TEST_1(!sip->sip_subscription_state->ss_expires);
    TEST_1(tl_find(n_tags, nutag_substate));
    TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
  }
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  /* Server events: nua_i_subscription with terminated status */
  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_i_subscription);
  TEST(tl_gets(e->data->e_tags,
               NEATAG_SUB_REF(sub),
               NUTAG_SUBSTATE_REF(substate),
               TAG_END()), 2);
  TEST_1(sub);
  TEST(substate, nua_substate_terminated);
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-12.5: PASSED\n");

  /* ---------------------------------------------------------------------- */
/* Fetch event, SUBSCRIBE with expires: 0

   A			B
   |                    |
   |------SUBSCRIBE---->|
   |<--------202--------|
   |<------NOTIFY-------|
   |-------200 OK------>|
   |                    |
*/
  if (print_headings)
    printf("TEST NUA-12.5.1: event fetch\n");

  SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
	    SIPTAG_EVENT_STR("presence"),
	    SIPTAG_ACCEPT_STR("application/pidf+xml"),
	    SIPTAG_EXPIRES_STR("0"),
	    TAG_END());

  run_ab_until(ctx, -1, save_until_notified_and_responded,
	       -1, save_until_subscription);

  /* Client events:
     nua_subscribe(), nua_i_notify/nua_r_subscribe
  */
  TEST_1(en = event_by_type(a->events->head, nua_i_notify));
  TEST_1(es = event_by_type(a->events->head, nua_r_subscribe));

  e = es; TEST_E(e->data->e_event, nua_r_subscribe);
  TEST_1(t = tl_find(e->data->e_tags, nutag_substate));
  TEST_1(t->t_value == nua_substate_pending ||
	 t->t_value == nua_substate_terminated ||
	 t->t_value == nua_substate_embryonic);

  e = en; TEST_E(e->data->e_event, nua_i_notify);
  TEST_1(sip = sip_object(e->data->e_msg));
  n_tags = e->data->e_tags;

  TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
  TEST_1(sip->sip_content_type);
  TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
  TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
  TEST_1(tl_find(n_tags, nutag_substate));
  TEST(tl_find(n_tags, nutag_substate)->t_value,
       nua_substate_terminated);
  TEST_1(!en->next || !es->next);
  free_events_in_list(ctx, a->events);

  /*
   Server events:
   nua_i_subscription
  */
  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_i_subscription);
  TEST(tl_gets(e->data->e_tags, NEATAG_SUB_REF(sub), TAG_END()), 1);
  TEST_1(sub);
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-12.4.1: PASSED\n");


  /* ---------------------------------------------------------------------- */
/* 2nd SUBSCRIBE with event id

   A			B
   |                    |
   |------SUBSCRIBE---->|
   |<--------202--------|
   |<------NOTIFY-------|
   |-------200 OK------>|
   |                    |
*/
  /* XXX - we should do this before unsubscribing first one */
  if (print_headings)
    printf("TEST NUA-12.4.2: establishing 2nd subscription\n");

   NOTIFIER(b, b_call, b_call->nh,
	    SIPTAG_EVENT_STR("presence"),
	    SIPTAG_CONTENT_TYPE_STR("application/xpidf+xml"),
	    SIPTAG_PAYLOAD_STR(open),
	    NEATAG_THROTTLE(1),
	    NUTAG_SUBSTATE(nua_substate_pending),
	    TAG_END());
  run_b_until(ctx, nua_r_notifier, until_final_response);

  NOTIFIER(b, b_call, b_call->nh,
	   SIPTAG_EVENT_STR("presence"),
	   SIPTAG_CONTENT_TYPE_STR("application/xpidf+xml"),
	   SIPTAG_PAYLOAD_STR(closed),
	   NEATAG_THROTTLE(1),
	   NEATAG_FAKE(1),
	   NUTAG_SUBSTATE(nua_substate_pending),
	   TAG_END());
  run_b_until(ctx, nua_r_notifier, until_final_response);

  SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
	    SIPTAG_EVENT_STR("presence;id=1"),
	    SIPTAG_ACCEPT_STR("application/xpidf+xml"),
	    TAG_END());

  run_ab_until(ctx, -1, save_until_notified_and_responded,
	       -1, save_until_subscription);

  /* Client events:
     nua_subscribe(), nua_i_notify/nua_r_subscribe
  */
  TEST_1(en = event_by_type(a->events->head, nua_i_notify));
  TEST_1(es = event_by_type(a->events->head, nua_r_subscribe));

  e = es; TEST_E(e->data->e_event, nua_r_subscribe);
  TEST_1(t = tl_find(e->data->e_tags, nutag_substate));
  TEST_1(t->t_value == nua_substate_pending ||
	 t->t_value == nua_substate_embryonic);

  e = en; TEST_E(e->data->e_event, nua_i_notify);
  TEST_1(sip = sip_object(e->data->e_msg));
  n_tags = e->data->e_tags;

  TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
  TEST_S(sip->sip_event->o_id, "1");
  TEST_1(sip->sip_content_type);
  TEST_S(sip->sip_content_type->c_type, "application/xpidf+xml");
  TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
  /* Check that we really got "fake" content */
  TEST_1(memmem(sip->sip_payload->pl_data, sip->sip_payload->pl_len,
		"<basic>closed</basic>", strlen("<basic>closed</basic>")));
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "pending");
  TEST_1(sip->sip_subscription_state->ss_expires);
  TEST_1(tl_find(n_tags, nutag_substate));
  TEST(tl_find(n_tags, nutag_substate)->t_value,
       nua_substate_pending);
  TEST_1(!en->next || !es->next);
  free_events_in_list(ctx, a->events);

  /*
   Server events:
   nua_i_subscription
  */
  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_i_subscription);
  TEST(tl_gets(e->data->e_tags, NEATAG_SUB_REF(sub), TAG_END()), 1);
  TEST_1(sub);
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);

  /* Authorize user A */
  AUTHORIZE(b, b_call, b_call->nh,
	    NUTAG_SUBSTATE(nua_substate_active),
	    NEATAG_SUB(sub),
	    NEATAG_FAKE(0),
	    TAG_END());

  run_ab_until(ctx, -1, save_until_notified,
	       -1, save_until_final_response);

  /* subscriber events:
     nua_i_notify with NUTAG_SUBSTATE(nua_substate_active)
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_notify);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
  TEST_1(sip->sip_content_type);
  TEST_S(sip->sip_content_type->c_type, "application/xpidf+xml");
  TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
  /* Check that we really got real content */
  TEST_1(memmem(sip->sip_payload->pl_data, sip->sip_payload->pl_len,
		"<basic>open</basic>", strlen("<basic>open</basic>")));
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "active");
  TEST_1(sip->sip_subscription_state->ss_expires);
  n_tags = e->data->e_tags;
  TEST_1(tl_find(n_tags, nutag_substate));
  TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  /*
   Server events:
   nua_r_authorize
  */
  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_r_authorize);
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-12.4: PASSED\n");

  /* ---------------------------------------------------------------------- */

/* NOTIFY terminating subscription

   A			B
   |                    |
   |<------NOTIFY-------|
   |-------200 OK------>|
   |                    |
*/

  if (print_headings)
    printf("TEST NUA-12.6: terminate notifier\n");

  TERMINATE(b, b_call, b_call->nh, TAG_END());

  run_ab_until(ctx, -1, save_until_notified, -1, until_final_response);

  /* Client events:
     nua_i_notify
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_notify);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
  TEST_S(sip->sip_event->o_id, "1");
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
  TEST_1(!sip->sip_subscription_state->ss_expires);
  n_tags = e->data->e_tags;
  TEST_1(tl_find(n_tags, nutag_substate));
  TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  if (print_headings)
    printf("TEST NUA-12.6: PASSED\n");

  /* ---------------------------------------------------------------------- */


  nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  END();			/* test_events */
}