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

📄 sipp.cpp

📁 sipp is sip protocal testing tool.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program 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 General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * *  Author : Richard GAYRAUD - 04 Nov 2003 *           Marc LAMBERTON *           Olivier JACQUES *           Herve PELLAN *           David MANSUTTI *           Francois-Xavier Kowalski *           Gerard Lyonnaz *           From Hewlett Packard Company. *           F. Tarek Rogers *           Peter Higginson *           Vincent Luba *           Shriram Natarajan *           Guillaume Teissier from FTR&D *           Clement Chen *           Wolfgang Beck *           Charles P Wright from IBM Research */#define GLOBALS_FULL_DEFINITION#include "sipp.hpp"#include "assert.h"#ifdef _USE_OPENSSLSSL_CTX  *sip_trp_ssl_ctx = NULL; /* For SSL cserver context */SSL_CTX  *sip_trp_ssl_ctx_client = NULL; /* For SSL cserver context */SSL_CTX  *twinSipp_sip_trp_ssl_ctx_client = NULL; /* For SSL cserver context */enum ssl_init_status {  SSL_INIT_NORMAL, /* 0   Normal completion    */  SSL_INIT_ERROR   /* 1   Unspecified error    */};#define CALL_BACK_USER_DATA "ksgr"int passwd_call_back_routine(char  *buf , int size , int flag, void *passwd){  strncpy(buf, (char *)(passwd), size);  buf[size - 1] = '\0';  return(strlen(buf));}#endif/* These could be local to main, but for the option processing table. */static int argiFileName;static int argiInputFile;/***************** Option Handling Table *****************/struct sipp_option {	const char *option;	const char *help;	int type;	void *data;};#define SIPP_OPTION_HELP	1#define SIPP_OPTION_INT		2#define SIPP_OPTION_SETFLAG	3#define SIPP_OPTION_UNSETFLAG	4#define SIPP_OPTION_STRING	5#define SIPP_OPTION_ARGI	6#define SIPP_OPTION_INT_TO_MS	7#define SIPP_OPTION_FLOAT	8#define SIPP_OPTION_FLOAT_TO_SEC 9#define SIPP_OPTION_BOOL	10#define SIPP_OPTION_VERSION	11#define SIPP_OPTION_TRANSPORT	12#define SIPP_OPTION_NEED_SSL	13#define SIPP_OPTION_IP		14#define SIPP_OPTION_MAX_SOCKET	15#define SIPP_OPTION_CSEQ	16#define SIPP_OPTION_SCENARIO	17#define SIPP_OPTION_RSA		18#define SIPP_OPTION_LIMIT	19#define SIPP_OPTION_USERS	20#define SIPP_OPTION_KEY		21#define SIPP_OPTION_3PCC	22#define SIPP_OPTION_TDMMAP	23/* Put Each option, its help text, and type in this table. */struct sipp_option options_table[] = {	{"v", "Display version and copyright information.", SIPP_OPTION_VERSION, NULL},	{"h", NULL, SIPP_OPTION_HELP, NULL},	{"help", NULL, SIPP_OPTION_HELP, NULL},	{"aa", "Enable automatic 200 OK answer for INFO and NOTIFY messages.", SIPP_OPTION_SETFLAG, &auto_answer},#ifdef _USE_OPENSSL	{"auth_uri", "Force the value of the URI for authentication.\n"                     "By default, the URI is composed of remote_ip:remote_port.", SIPP_OPTION_STRING, &auth_uri},#else	{"auth_uri", NULL, SIPP_OPTION_NEED_SSL, NULL},#endif	{"base_cseq", "Start value of [cseq] for each call.", SIPP_OPTION_CSEQ, NULL},	{"bg", "Launch SIPp in background mode.", SIPP_OPTION_SETFLAG, &backgroundMode},	{"bind_local", "Bind socket to local IP address, i.e. the local IP address is used as the source IP address.  If SIPp runs in server mode it will only listen on the local IP address instead of all IP addresses.", SIPP_OPTION_SETFLAG, &bind_local},	{"buff_size", "Set the send and receive buffer size.", SIPP_OPTION_INT, &buff_size},	{"cid_str", "Call ID string (default %u-%p@%s).  %u=call_number, %s=ip_address, %p=process_number, %%=% (in any order).", SIPP_OPTION_STRING, &call_id_string},	{"d", "Controls the length (in milliseconds) of calls. More precisely, this controls the duration of 'pause' instructions in the scenario, if they do not have a 'milliseconds' section. Default value is 0.", SIPP_OPTION_INT, &duration},	{"f", "Set the statistics report frequency on screen (in seconds). Default is 1.", SIPP_OPTION_INT, &report_freq},	{"fd", "Set the statistics dump log report frequency (in seconds). Default is 60.", SIPP_OPTION_INT_TO_MS, &report_freq_dumpLog},	{"i", "Set the local IP address for 'Contact:','Via:', and 'From:' headers. Default is primary host IP address.\n", SIPP_OPTION_IP, local_ip},	{"inf", "Inject values from an external CSV file during calls into the scenarios.\n"                "First line of this file say whether the data is to be read in sequence (SEQUENTIAL) or random (RANDOM) order.\n"		"Each line corresponds to one call and has one or more ';' delimited data fields. Those fields can be referred as [field0], [field1], ... in the xml scenario file.", SIPP_OPTION_ARGI, &argiInputFile},	{"ip_field", "Set which field from the injection file contains the IP address from which the client will send its messages.\n"                     "If this option is omitted and the '-t ui' option is present, then field 0 is assumed.\n"		     "Use this option together with '-t ui'", SIPP_OPTION_INT, &peripfield},	{"l", "Set the maximum number of simultaneous calls. Once this limit is reached, traffic is decreased until the number of open calls goes down. Default:\n"	      "  (3 * call_duration (s) * rate).", SIPP_OPTION_LIMIT, NULL},	{"m", "Stop the test and exit when 'calls' calls are processed", SIPP_OPTION_INT, &stop_after},	{"mi", "Set the local media IP address", SIPP_OPTION_IP, media_ip},	{"max_recv_loops", "Set the maximum number of messages received read per cycle. Increase this value for high traffic level.  The default value is 1000.", SIPP_OPTION_INT, &max_recv_loops},	{"max_reconnect", "Set the the maximum number of reconnection.", SIPP_OPTION_INT, &reset_number},	{"max_retrans", "Maximum number of UDP retransmissions before call ends on timeout.  Default is 5 for INVITE transactions and 7 for others.", SIPP_OPTION_INT, &max_udp_retrans},	{"max_invite_retrans", "Maximum number of UDP retransmissions for invite transactions before call ends on timeout.", SIPP_OPTION_INT, &max_invite_retrans},	{"max_non_invite_retrans", "Maximum number of UDP retransmissions for non-invite transactions before call ends on timeout.", SIPP_OPTION_INT, &max_non_invite_retrans},	{"max_socket", "Set the max number of sockets to open simultaneously. This option is significant if you use one socket per call. Once this limit is reached, traffic is distributed over the sockets already opened. Default value is 50000", SIPP_OPTION_MAX_SOCKET, NULL},	{"mb", "Set the RTP echo buffer size (default: 2048).", SIPP_OPTION_INT, &media_bufsize},	{"mp", "Set the local RTP echo port number. Default is 6000.", SIPP_OPTION_INT, &user_media_port},	{"nd", "No Default. Disable all default behavior of SIPp which are the following:\n"	        "- On UDP retransmission timeout, abort the call by sending a BYE or a CANCEL\n"	        "- On receive timeout with no ontimeout attribute, abort the call by sending a BYE or a CANCEL\n"	        "- On unexpected BYE send a 200 OK and close the call\n"	        "- On unexpected CANCEL send a 200 OK and close the call\n"	        "- On unexpected PING send a 200 OK and continue the call\n"	        "- On any other unexpected message, abort the call by sending a BYE or a CANCEL\n",		SIPP_OPTION_UNSETFLAG, &default_behavior},	{"nr", "Disable retransmission in UDP mode.", SIPP_OPTION_UNSETFLAG, &retrans_enabled},	{"p", "Set the local port number.  Default is a random free port chosen by the system.", SIPP_OPTION_INT, &user_port},	{"pause_msg_ign", "Ignore the messages received during a pause defined in the scenario ", SIPP_OPTION_SETFLAG, &pause_msg_ign},	{"r", "Set the call rate (in calls per seconds).  This value can be"	      "changed during test by pressing '+','_','*' or '/'. Default is 10.\n"	      "pressing '+' key to increase call rate by 1,\n"              "pressing '-' key to decrease call rate by 1,\n"              "pressing '*' key to increase call rate by 10,\n"              "pressing '/' key to decrease call rate by 10.\n"              "If the -rp option is used, the call rate is calculated with the period in ms given by the user.", SIPP_OPTION_FLOAT, &rate},	{"rp", "Specify the rate period in milliseconds for the call rate.  Default is 1 second.  This allows you to have n calls every m milliseconds (by using -r n -rp m).\n"               "Example: -r 7 -rp 2000 ==> 7 calls every 2 seconds.", SIPP_OPTION_FLOAT_TO_SEC, &rate_period_s},	{"rate_increase", "Specify the rate increase every -fd seconds.  This allows you to increase the load for each independent logging period.\n"                      "Example: -rate_increase 10 -fd 10\n"                      "  ==> increase calls by 10 every 10 seconds.", SIPP_OPTION_INT, &rate_increase},	{"rate_max", "If -rate_increase is set, then quit after the rate reaches this value.\n"                      "Example: -rate_increase 10 -max_rate 100\n"                      "  ==> increase calls by 10 until 100 cps is hit.", SIPP_OPTION_INT, &rate_max},	{"recv_timeout", "Global receive timeout in milliseconds.  If the expected message is not received, the call times out and is aborted.", SIPP_OPTION_INT, &defl_recv_timeout},	{"reconnect_close", "Should calls be closed on reconnect?", SIPP_OPTION_BOOL, &reset_close},	{"reconnect_sleep", "How long to sleep between the close and reconnect?", SIPP_OPTION_INT, &reset_sleep},	{"rsa", "Set the remote sending address to host:port for sending the messages.", SIPP_OPTION_RSA, NULL},	{"rtp_echo", "Enable RTP echo. RTP/UDP packets received on port defined by -mp are echoed to their sender.\n"                     "RTP/UDP packets coming on this port + 2 are also echoed to their sender (used for sound and video echo).",		     SIPP_OPTION_SETFLAG, &rtp_echo_enabled},	{"rtt_freq", "freq is mandatory. Dump response times every freq calls in the log file defined by -trace_rtt. Default value is 200.",		     SIPP_OPTION_SETFLAG, &report_freq_dumpRtt},	{"s", "Set the username part of the resquest URI. Default is 'service'.", SIPP_OPTION_STRING, &service},	{"sd", "Dumps a default scenario (embeded in the sipp executable)", SIPP_OPTION_SCENARIO, NULL},	{"sf", "Loads an alternate xml scenario file.  To learn more about XML scenario syntax, use the -sd option to dump embedded scenarios. They contain all the necessary help.", SIPP_OPTION_SCENARIO, NULL},	{"sn", "Use a default scenario (embedded in the sipp executable). If this option is omitted, the Standard SipStone UAC scenario is loaded.\n"               "Available values in this version:\n\n"               "- 'uac'      : Standard SipStone UAC (default).\n"               "- 'uas'      : Simple UAS responder.\n"               "- 'regexp'   : Standard SipStone UAC - with regexp and variables.\n"               "- 'branchc'  : Branching and conditional branching in scenarios - client.\n"               "- 'branchs'  : Branching and conditional branching in scenarios - server.\n\n"               "Default 3pcc scenarios (see -3pcc option):\n\n"               "- '3pcc-C-A' : Controller A side (must be started after all other 3pcc scenarios)\n"               "- '3pcc-C-B' : Controller B side.\n"               "- '3pcc-A'   : A side.\n"               "- '3pcc-B'   : B side.\n", SIPP_OPTION_SCENARIO, NULL},	{"stat_delimiter", "Set the delimiter for the statistics file", SIPP_OPTION_STRING, &stat_delimiter},	{"stf", "Set the file name to use to dump statistics", SIPP_OPTION_ARGI, &argiFileName},	{"t", "Set the transport mode:\n"              "- u1: UDP with one socket (default),\n"              "- un: UDP with one socket per call,\n"              "- ui: UDP with one socket per IP address The IP addresses must be defined in the injection file.\n"              "- t1: TCP with one socket,\n"              "- tn: TCP with one socket per call,\n"              "- l1: TLS with one socket,\n"              "- ln: TLS with one socket per call,\n"              "- c1: u1 + compression (only if compression plugin loaded),\n"              "- cn: un + compression (only if compression plugin loaded).\n", SIPP_OPTION_TRANSPORT, NULL},	{"timeout", "Global timeout in seconds.  If this option is set, SIPp quits after nb seconds.", SIPP_OPTION_INT, &global_timeout},	{"timer_resol", "Set the timer resolution in milliseconds.  This option has an impact on timers precision."                      "Small values allow more precise scheduling but impacts CPU usage."                      "If the compression is on, the value is set to 50ms. The default value is 10ms.", SIPP_OPTION_INT, &timer_resolution},	{"trace_msg", "Displays sent and received SIP messages in <scenario file name>_<pid>_messages.log", SIPP_OPTION_SETFLAG, &useMessagef},	{"trace_screen", "Dump statistic screens in the <scenario_name>_<pid>_screens.log file when quitting SIPp. Useful to get a final status report in background mode (-bg option).", SIPP_OPTION_SETFLAG, &useScreenf},	{"trace_err", "Trace all unexpected messages in <scenario file name>_<pid>_errors.log.", SIPP_OPTION_SETFLAG, &print_all_responses},	{"trace_timeout", "Displays call ids for calls with timeouts in <scenario file name>_<pid>_timeout.log", SIPP_OPTION_SETFLAG, &useTimeoutf},	{"trace_stat", "Dumps all statistics in <scenario_name>_<pid>.csv file. Use the '-h stat' option for a detailed description of the statistics file content.", SIPP_OPTION_SETFLAG, &dumpInFile},	{"trace_rtt", "Allow tracing of all response times in <scenario file name>_<pid>_rtt.csv.", SIPP_OPTION_SETFLAG, &dumpInRtt},	{"trace_logs", "Allow tracing of <log> actions in <scenario file name>_<pid>_logs.log.", SIPP_OPTION_SETFLAG, &useLogf},	{"up_nb", "Set the number of updates of the internal clock during the reading of received messages.  Default value is 1.", SIPP_OPTION_INT, &update_nb},#ifdef _USE_OPENSSL	{"ap", "Set the password for authentication challenges. Default is 'password", SIPP_OPTION_STRING, &auth_password},	{"tls_cert", "Set the name for TLS Certificate file. Default is 'cacert.pem", SIPP_OPTION_STRING, &tls_cert_name},	{"tls_key", "Set the name for TLS Private Key file. Default is 'cakey.pem'", SIPP_OPTION_STRING, &tls_key_name},	{"tls_crl", "Set the name for Certificate Revocation List file. If not specified, X509 CRL is not activated.", SIPP_OPTION_STRING, &tls_crl_name},#else	{"ap", NULL, SIPP_OPTION_NEED_SSL, NULL},	{"tls_cert", NULL, SIPP_OPTION_NEED_SSL, NULL},	{"tls_key", NULL, SIPP_OPTION_NEED_SSL, NULL},	{"tls_crl", NULL, SIPP_OPTION_NEED_SSL, NULL},#endif#ifdef __3PCC__	{"3pcc", "Launch the tool in 3pcc mode (\"Third Party call control\"). The passed ip address is depending on the 3PCC role.\n"                 "- When the first twin command is 'sendCmd' then this is the address of the remote twin socket.  SIPp will try to connect to this address:port to send the twin command (This instance must be started after all other 3PCC scenarii).\n"                 "    Example: 3PCC-C-A scenario.\n"                 "- When the first twin command is 'recvCmd' then this is the address of the local twin socket. SIPp will open this address:port to listen for twin command.\n"		 "    Example: 3PCC-C-B scenario.", SIPP_OPTION_3PCC, NULL},#endif	{"tdmmap", "Generate and handle a table of TDM circuits.\n"                   "A circuit must be available for the call to be placed.\n"                   "Format: -tdmmap {0-3}{99}{5-8}{1-31}", SIPP_OPTION_TDMMAP, NULL},	{"key", "key value\nSet the generic parameter named \"key\" to \"value\".", SIPP_OPTION_KEY, NULL},};struct sipp_option *find_option(const char *option) {	int i;	int max = sizeof(options_table)/sizeof(options_table[0]);	/* Allow options to start with '-' or '--' */	if (option[0] != '-') {	  return NULL;	}	option++;	if (option[0] == '-') {	  option++;	}	for (i = 0; i < max; i++) {	  if (!strcmp(options_table[i].option, option)) {	    return &(options_table[i]);	  }	}	return NULL;};/***************** System Portability Features *****************/unsigned int getmilliseconds(){  struct timeval LS_system_time;  unsigned long long int VI_milli;  static unsigned long long int VI_milli_base = 0;    gettimeofday(&LS_system_time, NULL);  VI_milli = ((unsigned long long) LS_system_time.tv_sec)     * 1000LL + (LS_system_time.tv_usec / 1000LL);  if (!VI_milli_base) VI_milli_base = VI_milli - 1;  VI_milli = VI_milli - VI_milli_base;    return (unsigned int) VI_milli;}#ifdef _USE_OPENSSL/****** SSL error handling                         *************/void sip_tls_error_handling(SSL *ssl, int size) {  int err;  err=SSL_get_error(ssl, size);  switch(err) {    case SSL_ERROR_NONE:      break;    case SSL_ERROR_WANT_WRITE:      WARNING("SSL_read returned SSL_ERROR_WANT_WRITE");      break;    case SSL_ERROR_WANT_READ:      WARNING("SSL_read returned SSL_ERROR_WANT_READ");      break;    case SSL_ERROR_WANT_X509_LOOKUP:      WARNING("SSL_read returned SSL_ERROR_WANT_X509_LOOKUP");      break;    case SSL_ERROR_SYSCALL:      if(size<0) { /* not EOF */        switch(errno) {          case EINTR:            WARNING("SSL_read interrupted by a signal");            break;          case EAGAIN:            WARNING("SSL_read returned EAGAIN");            break;           default:            WARNING("SSL_read (ERROR_SYSCALL)");        }      } else { /* EOF */        WARNING("SSL socket closed on SSL_read");      }      break;  }}/****** Certificate Verification Callback FACILITY *************/int sip_tls_verify_callback(int ok , X509_STORE_CTX *store){  char data[512];    if (!ok) {     X509 *cert = X509_STORE_CTX_get_current_cert(store);     int  depth = X509_STORE_CTX_get_error_depth(store);     int  err   = X509_STORE_CTX_get_error(store);     X509_NAME_oneline(X509_get_issuer_name(cert),                                   data,512);     WARNING_P1("TLS verification error for issuer: '%s'", data);     X509_NAME_oneline(X509_get_subject_name(cert),                                   data,512);     WARNING_P1("TLS verification error for subject: '%s'", data);  }  return ok;}/***********  Load the CRL's into SSL_CTX **********************/int sip_tls_load_crls( SSL_CTX *ctx , char *crlfile){  X509_STORE          *store;  X509_LOOKUP         *lookup;  /*  Get the X509_STORE from SSL context */  if (!(store = SSL_CTX_get_cert_store(ctx))) {    return (-1);  }  /* Add lookup file to X509_STORE */  if (!(lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file()))) {    return (-1);  }  /* Add the CRLS to the lookpup object */  if (X509_load_crl_file(lookup,crlfile,X509_FILETYPE_PEM) != 1) {    return (-1);  }  /* Set the flags of the store so that CRLS's are consulted */#if OPENSSL_VERSION_NUMBER >= 0x00907000L  X509_STORE_set_flags( store,X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);#else#warning This version of OpenSSL (<0.9.7) cannot handle CRL files in capath  ERROR("This version of OpenSSL (<0.9.7) cannot handle CRL files in capath");#endif  return (1);}/************* Prepare the SSL context ************************/static ssl_init_status FI_init_ssl_context (void){  sip_trp_ssl_ctx = SSL_CTX_new( SSLv23_method() );   if ( sip_trp_ssl_ctx == NULL ) {    ERROR("FI_init_ssl_context: SSL_CTX_new with SSLv23_method failed");    return SSL_INIT_ERROR;  }  sip_trp_ssl_ctx_client = SSL_CTX_new( TLSv1_method() );  if ( sip_trp_ssl_ctx_client == NULL)  {    ERROR("FI_init_ssl_context: SSL_CTX_new with TLSv1_method failed");    return SSL_INIT_ERROR;  }  /*  Load the trusted CA's */  SSL_CTX_load_verify_locations(sip_trp_ssl_ctx, tls_cert_name, NULL);  SSL_CTX_load_verify_locations(sip_trp_ssl_ctx_client, tls_cert_name, NULL);    /*  CRL load from application specified only if specified on the command line */  if (strlen(tls_crl_name) != 0) {    if(sip_tls_load_crls(sip_trp_ssl_ctx,tls_crl_name) == -1) {      ERROR_P1("FI_init_ssl_context: Unable to load CRL file (%s)", tls_crl_name);      return SSL_INIT_ERROR;    }      if(sip_tls_load_crls(sip_trp_ssl_ctx_client,tls_crl_name) == -1) {      ERROR_P1("FI_init_ssl_context: Unable to load CRL (client) file (%s)", tls_crl_name);      return SSL_INIT_ERROR;    }    /* The following call forces to process the certificates with the */    /* initialised SSL_CTX                                            */

⌨️ 快捷键说明

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