⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_nta_api.c

📁 this is simple sip stack.
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * This file is part of the Sofia-SIP package * * Copyright (C) 2005 Nokia Corporation. * * Contact: Pekka Pessi <pekka.pessi@nokia.com> * * 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 * *//**@internal * @CFILE test_nta_api.c * * Test functions for NTA. * * @author Pekka Pessi <Pekka.Pessi@nokia.com> * * @date Created: Tue Aug 21 15:18:26 2001 ppessi */#include "config.h"typedef struct agent_t agent_t;#define SU_ROOT_MAGIC_T      agent_t#include <sofia-sip/su_wait.h>#include <msg_internal.h>#define NTA_AGENT_MAGIC_T    agent_t#define NTA_LEG_MAGIC_T      agent_t#define NTA_OUTGOING_MAGIC_T agent_t#define NTA_INCOMING_MAGIC_T agent_t#define NTA_RELIABLE_MAGIC_T agent_t#include "sofia-sip/nta.h"#include "nta_internal.h"#include <sofia-sip/sip_header.h>#include <sofia-sip/sip_tag.h>#include <sofia-sip/sip_status.h>#include <sofia-sip/tport.h>#include <sofia-sip/htable.h>#include <sofia-sip/sresolv.h>#include <sofia-sip/su_log.h>#include <sofia-sip/msg_mclass.h>#include <sofia-sip/sofia_features.h>#include <sofia-sip/hostdomain.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <stdio.h>#include <assert.h>#include <time.h>#include "sofia-sip/string0.h"extern su_log_t nta_log[];extern su_log_t tport_log[];int tstflags = 0;#define TSTFLAGS tstflagschar const name[] = "test_nta_api";#include <sofia-sip/tstdef.h>#if HAVE_FUNC#elif HAVE_FUNCTION#define __func__ __FUNCTION__#else#define __func__ name#endif#define NONE ((void *)-1)struct sigcomp_compartment;struct agent_t {  su_home_t       ag_home[1];  int             ag_flags;  su_root_t      *ag_root;  msg_mclass_t   *ag_mclass;  nta_agent_t    *ag_agent;  nta_leg_t      *ag_default_leg; /**< Leg for rest */  nta_leg_t      *ag_server_leg;  /**< Leg for <sip:%@%>;methods=<PUBLISH>;events=<presence> */  unsigned        ag_drop;  nta_outgoing_t *ag_orq;  int             ag_status;  msg_t          *ag_response;  /* Server side */  nta_incoming_t *ag_irq;  sip_contact_t const *ag_contact;  sip_from_t     *ag_alice;  sip_to_t       *ag_bob;  sip_contact_t  *ag_m_alice;  sip_contact_t  *ag_m_bob;  sip_contact_t  *ag_aliases;  nta_leg_t      *ag_alice_leg;  nta_leg_t      *ag_bob_leg;  msg_t          *ag_request;  nta_leg_t      *ag_expect_leg;  nta_leg_t      *ag_latest_leg;  nta_leg_t      *ag_call_leg;  nta_leg_t      *ag_tag_remote; /**< If this is set, outgoing_callback()				  *   tags it with the tag from remote.				  */  int             ag_tag_status; /**< Which response established dialog */  msg_param_t     ag_call_tag;	 /**< Tag used to establish dialog */  nta_reliable_t *ag_reliable;  sip_via_t      *ag_out_via;	/**< Outgoing via */  sip_via_t      *ag_in_via;	/**< Incoming via */  sip_content_type_t *ag_content_type;  sip_payload_t  *ag_payload;  msg_t          *ag_probe_msg;};static int incoming_callback_1(agent_t *ag,			       nta_incoming_t *irq, 			       sip_t const *sip){  return 0;}static int incoming_callback_2(agent_t *ag,			       nta_incoming_t *irq, 			       sip_t const *sip){  return 0;}int agent_callback(agent_t *ag,		   nta_agent_t *nta,		   msg_t *msg,		   sip_t *sip){  msg_destroy(msg);  return 0;}int leg_callback(agent_t *ag,		 nta_leg_t *leg,		 nta_incoming_t *irq,		 sip_t const *sip){  BEGIN();  msg_t *msg;  char const *tag;    if (tstflags & tst_verbatim) {    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",	   name, __func__, sip->sip_request->rq_method_name, 	   URL_PRINT_ARGS(sip->sip_request->rq_url),	   sip->sip_request->rq_version);  }  TEST_1(sip->sip_content_length);  TEST_1(sip->sip_via);  TEST_1(sip->sip_from && sip->sip_from->a_tag);  TEST_VOID(nta_incoming_bind(irq, incoming_callback_1, ag));  TEST(nta_incoming_magic(irq, incoming_callback_1), ag);  TEST(nta_incoming_magic(irq, incoming_callback_2), 0);    TEST_1(tag = nta_incoming_tag(irq, "tag=foofaa"));  TEST_S(nta_incoming_gettag(irq), tag);  TEST_S(tag, "foofaa");  TEST_1(tag = nta_incoming_tag(irq, "foofaa"));  TEST(nta_incoming_status(irq), 0);  TEST(nta_incoming_method(irq), sip_method_message);  TEST_S(nta_incoming_method_name(irq), "MESSAGE");  TEST_1(nta_incoming_url(irq) != NULL);  TEST_1(nta_incoming_cseq(irq) != 0);    TEST(nta_incoming_set_params(irq, TAG_END()), 0);    TEST_1(msg = nta_incoming_getrequest(irq)); msg_destroy(msg);  TEST(nta_incoming_getrequest_ackcancel(irq), NULL);  TEST(nta_incoming_getresponse(irq), NULL);  TEST(nta_incoming_treply(irq, SIP_100_TRYING, TAG_END()), 0);  TEST_1(msg = nta_incoming_getresponse(irq)); msg_destroy(msg);  msg = nta_msg_create(ag->ag_agent, 0);  TEST(nta_incoming_complete_response(irq, msg, SIP_200_OK, TAG_END()), 0);  TEST(nta_incoming_mreply(irq, msg), 0);  END();}int outgoing_callback(agent_t *ag,		      nta_outgoing_t *orq,		      sip_t const *sip){  BEGIN();  msg_t *msg;  int status = sip->sip_status->st_status;  if (tstflags & tst_verbatim) {    printf("%s: %s: %s %03d %s\n", name, __func__, 	   sip->sip_status->st_version, 	   sip->sip_status->st_status, 	   sip->sip_status->st_phrase);  }  ag->ag_status = status;  if (status < 200)    return 0;  TEST_1(sip->sip_to && sip->sip_to->a_tag);  /* Test API functions */  TEST(nta_outgoing_status(orq), status);  TEST_1(nta_outgoing_request_uri(orq));  TEST_1(!nta_outgoing_route_uri(orq));  TEST(nta_outgoing_method(orq), sip_method_message);  TEST_S(nta_outgoing_method_name(orq), "MESSAGE");  TEST(nta_outgoing_cseq(orq), sip->sip_cseq->cs_seq);  TEST_1(nta_outgoing_delay(orq) < UINT_MAX);    TEST_1(msg = nta_outgoing_getresponse(orq));  msg_destroy(msg);    TEST_1(msg = nta_outgoing_getrequest(orq));  msg_destroy(msg);  nta_outgoing_destroy(orq);	  /* Call it twice */  nta_outgoing_destroy(orq);  ag->ag_orq = NULL;  END();}void nta_test_run(agent_t *ag){  time_t now = time(NULL);  for (ag->ag_status = 0; ag->ag_status < 200;) {    if (tstflags & tst_verbatim) {      fputs(".", stdout); fflush(stdout);    }    su_root_step(ag->ag_root, 500L);    if (!getenv("NTA_TEST_DEBUG") && time(NULL) > now + 5) {      fprintf(stderr, "nta_test_run: timeout\n");      return;    }  }}int api_test_init(agent_t *ag){  BEGIN();  char const *contact = NULL;  if (getenv("SIPCONTACT"))    contact = getenv("SIPCONTACT");  if (contact == NULL || contact[0] == '\0')    contact = "sip:0.0.0.0:*;comp=sigcomp";  TEST_1(ag->ag_root = su_root_create(ag));  TEST_1(ag->ag_mclass = msg_mclass_clone(sip_default_mclass(), 0, 0));  /* Create agent */  TEST_1(ag->ag_agent = nta_agent_create(ag->ag_root,					 (url_string_t *)contact,					 NULL,					 NULL,					 NTATAG_MCLASS(ag->ag_mclass),					 NTATAG_USE_TIMESTAMP(1),					 NTATAG_USE_NAPTR(0),					 NTATAG_USE_SRV(0),					 NTATAG_PRELOAD(2048),					 TAG_END()));  /* Create a default leg */  TEST_1(ag->ag_default_leg = nta_leg_tcreate(ag->ag_agent, 					     leg_callback,					     ag,					     NTATAG_NO_DIALOG(1),					     TAG_END()));  {    /* Initialize our headers */    sip_from_t from[1];    sip_to_t to[1];    sip_contact_t m[1];    sip_from_init(from);    sip_to_init(to);    sip_contact_init(m);    TEST_1(ag->ag_contact = nta_agent_contact(ag->ag_agent));    *m->m_url = *ag->ag_contact->m_url;    m->m_url->url_user = "bob";    TEST_1(ag->ag_m_bob = sip_contact_dup(ag->ag_home, m));    to->a_display = "Bob";    *to->a_url = *ag->ag_contact->m_url;    to->a_url->url_user = "bob";    to->a_url->url_port = NULL;    TEST_1(ag->ag_bob = sip_to_dup(ag->ag_home, to));    url_strip_transport(ag->ag_bob->a_url);    *m->m_url = *ag->ag_contact->m_url;    m->m_url->url_user = "alice";    TEST_1(ag->ag_m_alice = sip_contact_dup(ag->ag_home, m));    from->a_display = "Alice";    *from->a_url = *ag->ag_contact->m_url;    from->a_url->url_user = "alice";    from->a_url->url_port = NULL;    TEST_1(ag->ag_alice = sip_from_dup(ag->ag_home, from));    url_strip_transport(ag->ag_alice->a_url);  }  {    char const data[] =       "v=0\r\n"      "o=- 425432 423412 IN IP4 127.0.0.1\r\n"      "s= \r\n"      "c=IN IP4 127.0.0.1\r\n"      "m=5004 audio 8 0\r\n";    ag->ag_content_type = sip_content_type_make(ag->ag_home, "application/sdp");    ag->ag_payload = sip_payload_make(ag->ag_home, data);  }  {    sip_contact_t *m;    ag->ag_aliases =       sip_contact_make(ag->ag_home, "sip:127.0.0.1, sip:localhost, sip:[::1]");    TEST_1(ag->ag_aliases);    TEST_1(ag->ag_aliases->m_next);    TEST_1(ag->ag_aliases->m_next->m_next);    TEST(ag->ag_aliases->m_next->m_next->m_next, NULL);    for (m = ag->ag_aliases; m; m = m->m_next)      m->m_url->url_port = ag->ag_contact->m_url->url_port;    TEST_1(m = sip_contact_dup(ag->ag_home, ag->ag_contact));    m->m_next = ag->ag_aliases;    ag->ag_aliases = m;    TEST(nta_agent_set_params(ag->ag_agent, 			      NTATAG_ALIASES(ag->ag_aliases),			      NTATAG_REL100(1),			      NTATAG_UA(1), 			      NTATAG_USE_NAPTR(1),			      NTATAG_USE_SRV(1),			      TAG_END()),	 5);    TEST(nta_agent_set_params(ag->ag_agent, 			      NTATAG_ALIASES(ag->ag_aliases),			      NTATAG_DEFAULT_PROXY("sip:127.0.0.1"),			      TAG_END()), 2);    TEST(nta_agent_set_params(ag->ag_agent, 			      NTATAG_ALIASES(ag->ag_aliases),			      NTATAG_DEFAULT_PROXY(NULL),			      TAG_END()), 2);    TEST(nta_agent_set_params(ag->ag_agent, 			      NTATAG_DEFAULT_PROXY("tel:+35878008000"),			      TAG_END()), -1);  }    {    url_t url[1];    /* Create the server leg */    *url = *ag->ag_aliases->m_url;    url->url_user = "%";    TEST_1(ag->ag_server_leg = nta_leg_tcreate(ag->ag_agent, 					       leg_callback,					       ag,					       NTATAG_NO_DIALOG(1),					       URLTAG_URL(url),					       TAG_END()));  }  END();}  int api_test_deinit(agent_t *ag){  BEGIN();  if (ag->ag_request) msg_destroy(ag->ag_request), ag->ag_request = NULL;  if (ag->ag_response) msg_destroy(ag->ag_response), ag->ag_response = NULL;  su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;  nta_leg_destroy(ag->ag_alice_leg);  nta_leg_destroy(ag->ag_bob_leg);  nta_leg_destroy(ag->ag_default_leg);  nta_leg_destroy(ag->ag_server_leg);  nta_agent_destroy(ag->ag_agent);  su_root_destroy(ag->ag_root);  free(ag->ag_mclass), ag->ag_mclass = NULL;  END();}  /* Get and check parameters */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -