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

📄 test_nta.c

📁 Sofia SIP is an open-source SIP User-Agent library, compliant with the IETF RFC3261 specification.
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * 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.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;typedef struct client_t client_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 client_t#define NTA_OUTGOING_MAGIC_T0 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/sofia_features.h>#include <sofia-sip/hostdomain.h>#include <sofia-sip/tport.h>#include <sofia-sip/string0.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <stdio.h>#include <assert.h>#include <unistd.h>#if HAVE_OPEN_C#include <sys/param.h>#endifSOFIAPUBVAR su_log_t nta_log[];SOFIAPUBVAR su_log_t tport_log[];int tstflags = 0;#define TSTFLAGS tstflags#include <sofia-sip/tstdef.h>#if HAVE_FUNC#elif HAVE_FUNCTION#define __func__ __FUNCTION__#else#define __func__ name#endif#define NONE ((void *)-1)int expensive_checks;#define EXPENSIVE_CHECKS (expensive_checks)struct sigcomp_compartment;char const name[] = "test_nta";typedef struct invite_client_t invite_client_t;typedef int client_check_f(client_t *, nta_outgoing_t *, sip_t const *);typedef int client_deinit_f(client_t *);struct client_t {  agent_t *c_ag;  char const *c_name;  client_check_f * c_check;  client_check_f * const * c_checks;  client_deinit_f * c_deinit;  void *c_extra;  nta_outgoing_t *c_orq;  int c_status;  int c_final;  int c_errors;};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;  url_string_t   *ag_obp;	/**< Outbound proxy. */  nta_leg_t      *ag_server_leg; /**< Leg for sip:%@% */  nta_leg_t      *ag_default_leg; /**< Leg for rest */  unsigned        ag_drop;  nta_outgoing_t *ag_orq;  unsigned        ag_running :1, ag_canceled:1, ag_acked:1, :0;  char const     *ag_comp;  struct sigcomp_compartment *ag_client_compartment;  /* Server side */  int             ag_response;	/**< What we answer by default */  nta_incoming_t *ag_irq;  struct sigcomp_compartment *ag_server_compartment;  char const     *ag_m;  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.				  */  nta_reliable_t *ag_reliable;  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;  su_sockaddr_t   ag_su_nta[1];  socklen_t       ag_su_nta_len;  /* Dummy servers */  char const     *ag_sink_port;  su_socket_t     ag_sink_socket, ag_down_socket;  su_wait_t       ag_sink_wait[1];};static int test_init(agent_t *ag, char const *resolv_conf);static int test_deinit(agent_t *ag);static int test_bad_messages(agent_t *ag);static int test_routing(agent_t *ag);static int test_tports(agent_t *ag);static int test_resolv(agent_t *ag, char const *resolv_conf);static int test_dialog(agent_t *ag);static int test_call(agent_t *ag);static int test_prack(agent_t *ag);static int test_fix_467(agent_t *ag);static int test_for_ack(agent_t *ag,			nta_incoming_t *irq, 			sip_t const *sip);static int test_for_ack_or_timeout(agent_t *ag,				   nta_incoming_t *irq, 				   sip_t const *sip);static int wait_for_ack_or_cancel(agent_t *ag,				  nta_incoming_t *irq, 				  sip_t const *sip);int agent_callback(agent_t *ag,		   nta_agent_t *nta,		   msg_t *msg,		   sip_t *sip){  if (tstflags & tst_verbatim) {    if (sip->sip_request) {      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);    }    else {      printf("%s: %s: %s %03d %s\n", name, __func__, 	     sip->sip_status->st_version, 	     sip->sip_status->st_status, 	     sip->sip_status->st_phrase);    }  }  msg_destroy(msg);  return 0;}staticvoid leg_match(agent_t *ag, nta_leg_t *leg, int always, char const *func){  char const *match = "unknown leg";  if (!always && (tstflags & tst_verbatim) != tst_verbatim)    return;  if (leg == ag->ag_default_leg)    match = "ag_default_leg";  else if (leg == ag->ag_server_leg)     match = "ag_server_leg";  else if (leg == ag->ag_alice_leg)    match = "ag_alice_leg";  else if (leg == ag->ag_bob_leg)    match = "ag_bob_leg";  printf("%s: %s: %smatched with %s\n", name, func, 	 always ? "mis" : "", match);}staticvoid leg_zap(agent_t *ag, nta_leg_t *leg){  if (leg == ag->ag_default_leg)    ag->ag_default_leg = NULL;  else if (leg == ag->ag_server_leg)     ag->ag_server_leg = NULL;  else if (leg == ag->ag_alice_leg)    ag->ag_alice_leg = NULL;  else if (leg == ag->ag_bob_leg)    ag->ag_bob_leg = NULL;  else     printf("%s:%u: %s: did not exist\n", 	   __FILE__, __LINE__, __func__);  nta_leg_destroy(leg);}int leg_callback_200(agent_t *ag,		     nta_leg_t *leg,		     nta_incoming_t *irq,		     sip_t const *sip){  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);  }  if (!sip->sip_content_length ||      !sip->sip_via ||      !sip->sip_from || !sip->sip_from->a_tag)    return 500;  if (ag->ag_in_via == NULL)    ag->ag_in_via = sip_via_dup(ag->ag_home, sip->sip_via);  if (ag->ag_request == NULL)    ag->ag_request = nta_incoming_getrequest(irq);  ag->ag_latest_leg = leg;  if (ag->ag_expect_leg && leg != ag->ag_expect_leg) {    leg_match(ag, leg, 1, __func__);    return 500;  }  leg_match(ag, leg, 0, __func__);  if (sip->sip_request->rq_method == sip_method_bye) {    leg_zap(ag, leg);  }  return 200;}int leg_callback_500(agent_t *ag,		     nta_leg_t *leg,		     nta_incoming_t *irq,		     sip_t const *sip){  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);  }  return 500;}int new_leg_callback_200(agent_t *ag,			 nta_leg_t *leg,			 nta_incoming_t *irq,			 sip_t const *sip){  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);  }  if (!sip->sip_content_length ||      !sip->sip_via ||      !sip->sip_from || !sip->sip_from->a_tag)    return 500;  ag->ag_latest_leg = leg;  if (ag->ag_expect_leg && leg != ag->ag_expect_leg) {    leg_match(ag, leg, 1, __func__);    return 500;  }  leg_match(ag, leg, 0, __func__);  ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,				   leg_callback_200,				   ag,				   URLTAG_URL(sip->sip_request->rq_url),				   SIPTAG_CALL_ID(sip->sip_call_id),				   SIPTAG_FROM(sip->sip_to),				   SIPTAG_TO(sip->sip_from),				   TAG_END());  if (!ag->ag_bob_leg ||      !nta_leg_tag(ag->ag_bob_leg, NULL) ||      !nta_leg_get_tag(ag->ag_bob_leg) ||      !nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)))    return 500;  return 200;}int new_leg_callback_180(agent_t *ag,			 nta_leg_t *leg,			 nta_incoming_t *irq,			 sip_t const *sip){  int status = new_leg_callback_200(ag, leg, irq, sip);  if (status == 200) {    ag->ag_irq = irq;    status = 180;  }  return status;}static client_check_f client_check_to_tag;static client_check_f * const default_checks[] = {  client_check_to_tag,  NULL};static client_check_f * const no_default_checks[] = {  NULL};int outgoing_callback(client_t *ctx,		      nta_outgoing_t *orq,		      sip_t const *sip){  agent_t *ag = ctx->c_ag;  int status = nta_outgoing_status(orq);  client_check_f * const *checks;  if (tstflags & tst_verbatim) {    if (sip)      printf("%s: %s: response %s %03d %s\n", name, ctx->c_name, 	     sip->sip_status->st_version, 	     sip->sip_status->st_status, 	     sip->sip_status->st_phrase);    else      printf("%s: %s: callback %03d\n", name, ctx->c_name,	     status);  }  if (status >= 200 && ag->ag_comp) {		/* XXX */    nta_compartment_decref(&ag->ag_client_compartment);    ag->ag_client_compartment = nta_outgoing_compartment(ctx->c_orq);  }  if (status > ctx->c_status)    ctx->c_status = status;  if (status >= 200)    ctx->c_final = 1;  if (ctx->c_check && ctx->c_check(ctx, orq, sip))    ctx->c_errors++;  checks = ctx->c_checks;  for (checks = checks ? checks : default_checks; *checks; checks++)    if ((*checks)(ctx, ctx->c_orq, sip))      ctx->c_errors++;  return 0;}/** Deinit client. Return nonzero if client checks failed. */static int client_deinit(client_t *c){  int errors = c->c_errors;  if (c->c_deinit && c->c_deinit(c))    errors++;  if (c->c_orq) nta_outgoing_destroy(c->c_orq), c->c_orq = NULL;  c->c_errors = 0;   c->c_status = 0;  return errors;}static void nta_test_run(agent_t *ag){  for (ag->ag_running = 1; ag->ag_running;) {    if (tstflags & tst_verbatim) {      fputs(".", stdout); fflush(stdout);    }    su_root_step(ag->ag_root, 500L);  }}/** Run client test. Return nonzero if client checks failed. */static int client_run_with(client_t *c, int expected, void (*runner)(client_t *c)){  int resulting;  TEST_1(c->c_orq != NULL);  runner(c);  resulting = c->c_status;  if (client_deinit(c))    return 1;  if (expected)    TEST(resulting, expected);  return 0;}static void until_final_received(client_t *c){  for (c->c_final = 0; !c->c_final; ) {    if (tstflags & tst_verbatim) {      fputs(".", stdout); fflush(stdout);    }    su_root_step(c->c_ag->ag_root, 500L);  }}static int client_run(client_t *c, int expected){  return client_run_with(c, expected, until_final_received);}staticvoid until_server_acked(client_t *c){

⌨️ 快捷键说明

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