3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17
* Author : Richard GAYRAUD - 04 Nov 2003
18
* From Hewlett Packard Company.
30
#include <sys/types.h>
31
#include <sys/socket.h>
32
#include <netinet/in.h>
33
#include <netinet/tcp.h>
36
#include <sys/resource.h>
39
#include <arpa/inet.h>
57
#if defined(__HPUX) || defined(__SUNOS)
63
#include "xp_parser.h"
64
#include "scenario.hpp"
67
#include "listener.hpp"
68
#include "socketowner.hpp"
71
#include "variables.hpp"
73
#include "actions.hpp"
75
#include "opentask.hpp"
76
#include "reporttask.hpp"
77
#include "watchdog.hpp"
80
#include "sslcommon.h"
86
#define FD_SETSIZE 65000
90
#define FD_SETSIZE 1024
95
* If this files is included in the Main, then extern definitions
96
* are removed, and the _DEFAULT macro becomes '= value;'. Else
97
* extern definition does not contain default values assignment
100
#ifdef GLOBALS_FULL_DEFINITION
102
#define _DEFVAL(value) = value
104
#define _DEFVAL(value)
107
/************************** Constants **************************/
110
# ifdef LOCAL_VERSION_EXTRA
111
# define SIPP_VERSION SVN_VERSION LOCAL_VERSION_EXTRA
113
# define SIPP_VERSION SVN_VERSION
116
# define SIPP_VERSION "unknown"
124
#define DEFAULT_TLS_CERT ((char *)"cacert.pem")
125
#define DEFAULT_TLS_KEY ((char *)"cakey.pem")
126
#define DEFAULT_TLS_CRL ((char *)"")
129
#define TRANSPORT_TO_STRING(p) ((p==T_TCP) ? "TCP" : ((p==T_TLS)? "TLS" :"UDP"))
131
#define SIPP_MAXFDS 65536
132
#define SIPP_MAX_MSG_SIZE 65536
134
#define MSG_RETRANS_FIRST 0
135
#define MSG_RETRANS_RETRANSMISSION 1
136
#define MSG_RETRANS_NEVER 2
138
#define DISPLAY_STAT_SCREEN 1
139
#define DISPLAY_REPARTITION_SCREEN 2
140
#define DISPLAY_SCENARIO_SCREEN 3
141
#define DISPLAY_VARIABLE_SCREEN 4
142
#define DISPLAY_TDM_MAP_SCREEN 5
143
#define DISPLAY_SECONDARY_REPARTITION_SCREEN 6
145
#define MAX_RECV_LOOPS_PER_CYCLE 1000
146
#define MAX_SCHED_LOOPS_PER_CYCLE 1000
147
#define NB_UPDATE_PER_CYCLE 1
151
#define MAX_PEER_SIZE 4096 /* 3pcc extended mode: max size of peer names */
152
#define MAX_LOCAL_TWIN_SOCKETS 10 /*3pcc extended mode:max number of peers from which
153
cmd messages are received */
155
/******************** Default parameters ***********************/
157
#define DEFAULT_RATE 10.0
158
#define DEFAULT_RATE_SCALE 1.0
159
#define DEFAULT_RATE_PERIOD_MS 1000
160
#define DEFAULT_TRANSPORT T_UDP
161
#define DEFAULT_PORT 5060
162
#define DEFAULT_MEDIA_PORT 6000
163
#define DEFAULT_3PCC_PORT 6060
164
#define DEFAULT_SERVICE ((char *)"service")
165
#define DEFAULT_AUTH_PASSWORD ((char *)"password")
166
#define DEFAULT_REPORT_FREQ 1000
167
#define DEFAULT_REPORT_FREQ_DUMP_LOG 60000
168
#define DEFAULT_TIMER_RESOLUTION 1
169
#define DEFAULT_FREQ_DUMP_RTT 200
170
#define DEFAULT_MAX_MULTI_SOCKET 50000
171
#define DEFAULT_CTRL_SOCKET_PORT 8888
172
#define DEFAULT_DEADCALL_WAIT 33000
174
#define DEFAULT_BEHAVIOR_NONE 0
175
#define DEFAULT_BEHAVIOR_BYE 1
176
#define DEFAULT_BEHAVIOR_ABORTUNEXP 2
177
#define DEFAULT_BEHAVIOR_PINGREPLY 4
179
#define DEFAULT_BEHAVIOR_ALL (DEFAULT_BEHAVIOR_BYE | DEFAULT_BEHAVIOR_ABORTUNEXP | DEFAULT_BEHAVIOR_PINGREPLY)
181
/************ User controls and command line options ***********/
183
extern int duration _DEFVAL(0);
184
extern double rate _DEFVAL(DEFAULT_RATE);
185
extern double rate_scale _DEFVAL(DEFAULT_RATE_SCALE);
186
extern int rate_increase _DEFVAL(0);
187
extern int rate_max _DEFVAL(0);
188
extern bool rate_quit _DEFVAL(true);
189
extern int users _DEFVAL(-1);
190
extern int rate_period_ms _DEFVAL(DEFAULT_RATE_PERIOD_MS);
191
extern int sleeptime _DEFVAL(0);
192
extern unsigned long defl_recv_timeout _DEFVAL(0);
193
extern unsigned long defl_send_timeout _DEFVAL(0);
194
extern unsigned long global_timeout _DEFVAL(0);
195
extern int transport _DEFVAL(DEFAULT_TRANSPORT);
196
extern bool retrans_enabled _DEFVAL(1);
197
extern int rtcheck _DEFVAL(RTCHECK_FULL);
198
extern int max_udp_retrans _DEFVAL(UDP_MAX_RETRANS);
199
extern int max_invite_retrans _DEFVAL(UDP_MAX_RETRANS_INVITE_TRANSACTION);
200
extern int max_non_invite_retrans _DEFVAL(UDP_MAX_RETRANS_NON_INVITE_TRANSACTION);
201
extern unsigned long default_behaviors _DEFVAL(DEFAULT_BEHAVIOR_ALL);
202
extern unsigned long deadcall_wait _DEFVAL(DEFAULT_DEADCALL_WAIT);
203
extern bool pause_msg_ign _DEFVAL(0);
204
extern bool auto_answer _DEFVAL(false);
205
extern int multisocket _DEFVAL(0);
206
extern int compression _DEFVAL(0);
207
extern int peripsocket _DEFVAL(0);
208
extern int peripfield _DEFVAL(0);
209
extern bool bind_local _DEFVAL(false);
210
extern void * monosocket_comp_state _DEFVAL(0);
211
extern char * service _DEFVAL(DEFAULT_SERVICE);
212
extern char * auth_password _DEFVAL(DEFAULT_AUTH_PASSWORD);
213
extern unsigned long report_freq _DEFVAL(DEFAULT_REPORT_FREQ);
214
extern unsigned long report_freq_dumpLog _DEFVAL
215
(DEFAULT_REPORT_FREQ_DUMP_LOG);
216
extern bool periodic_rtd _DEFVAL(false);
217
extern char * stat_delimiter _DEFVAL(";");
219
extern bool timeout_exit _DEFVAL(false);
220
extern bool timeout_error _DEFVAL(false);
222
extern unsigned long report_freq_dumpRtt _DEFVAL
223
(DEFAULT_FREQ_DUMP_RTT);
225
extern int max_multi_socket _DEFVAL
226
(DEFAULT_MAX_MULTI_SOCKET);
227
extern bool skip_rlimit _DEFVAL(false);
229
extern unsigned int timer_resolution _DEFVAL(DEFAULT_TIMER_RESOLUTION);
230
extern int max_recv_loops _DEFVAL(MAX_RECV_LOOPS_PER_CYCLE);
231
extern int max_sched_loops _DEFVAL(MAX_SCHED_LOOPS_PER_CYCLE);
233
extern unsigned int global_t2 _DEFVAL(DEFAULT_T2_TIMER_VALUE);
235
extern char local_ip[40];
236
extern char local_ip_escaped[42];
237
extern bool local_ip_is_ipv6;
238
extern int local_port _DEFVAL(0);
239
extern char control_ip[40];
240
extern int control_port _DEFVAL(0);
241
extern int buff_size _DEFVAL(65535);
242
extern int tcp_readsize _DEFVAL(65535);
244
extern int hasMedia _DEFVAL(0);
246
extern bool rtp_echo_enabled _DEFVAL(0);
247
extern char media_ip[40];
248
extern char media_ip_escaped[42];
249
extern int user_media_port _DEFVAL(0);
250
extern int media_port _DEFVAL(0);
251
extern size_t media_bufsize _DEFVAL(2048);
252
extern bool media_ip_is_ipv6;
253
extern char remote_ip[40];
254
extern char remote_ip_escaped[42];
255
extern int remote_port _DEFVAL(DEFAULT_PORT);
256
extern unsigned int pid _DEFVAL(0);
257
extern bool print_all_responses _DEFVAL(false);
258
extern unsigned long stop_after _DEFVAL(0xffffffff);
259
extern int quitting _DEFVAL(0);
260
extern int interrupt _DEFVAL(0);
261
extern bool paused _DEFVAL(false);
262
extern int lose_packets _DEFVAL(0);
263
extern double global_lost _DEFVAL(0.0);
264
extern char remote_host[255];
265
extern char twinSippHost[255];
266
extern char twinSippIp[40];
267
extern char * master_name;
268
extern char * slave_number;
269
extern int twinSippPort _DEFVAL(DEFAULT_3PCC_PORT);
270
extern bool twinSippMode _DEFVAL(false);
271
extern bool extendedTwinSippMode _DEFVAL(false);
273
extern bool nostdin _DEFVAL(false);
274
extern bool backgroundMode _DEFVAL(false);
275
extern bool signalDump _DEFVAL(false);
277
extern int currentScreenToDisplay _DEFVAL
278
(DISPLAY_SCENARIO_SCREEN);
279
extern int currentRepartitionToDisplay _DEFVAL(1);
280
extern unsigned int base_cseq _DEFVAL(0);
281
extern char * auth_uri _DEFVAL(0);
282
extern char * call_id_string _DEFVAL("%u-%p@%s");
283
extern char **generic[100];
286
extern bool use_tdmmap _DEFVAL(false);
287
extern unsigned int tdm_map_a _DEFVAL(0);
288
extern unsigned int tdm_map_b _DEFVAL(0);
289
extern unsigned int tdm_map_c _DEFVAL(0);
290
extern unsigned int tdm_map_x _DEFVAL(0);
291
extern unsigned int tdm_map_y _DEFVAL(0);
292
extern unsigned int tdm_map_z _DEFVAL(0);
293
extern unsigned int tdm_map_h _DEFVAL(0);
294
extern bool tdm_map[1024];
297
extern BIO *twinSipp_bio ;
298
extern SSL *twinSipp_ssl ;
299
extern char *tls_cert_name _DEFVAL(DEFAULT_TLS_CERT) ;
300
extern char *tls_key_name _DEFVAL(DEFAULT_TLS_KEY) ;
301
extern char *tls_crl_name _DEFVAL(DEFAULT_TLS_CRL) ;
305
// extern field file management
306
typedef std::map<string, FileContents *> file_map;
307
extern file_map inFiles;
308
typedef std::map<string, str_int_map *> file_index;
309
extern char *ip_file _DEFVAL(NULL);
310
extern char *default_file _DEFVAL(NULL);
313
extern list<int> freeUsers;
314
extern list<int> retiredUsers;
315
extern AllocVariableTable *globalVariables _DEFVAL(NULL);
316
extern AllocVariableTable *userVariables _DEFVAL(NULL);
317
typedef std::map<int, VariableTable *> int_vt_map;
318
extern int_vt_map userVarMap;
320
//extern int new_socket(bool P_use_ipv6, int P_type_socket, int * P_status);
321
extern struct sipp_socket *new_sipp_socket(bool use_ipv6, int transport);
322
struct sipp_socket *new_sipp_call_socket(bool use_ipv6, int transport, bool *existing);
323
struct sipp_socket *sipp_accept_socket(struct sipp_socket *accept_socket);
324
extern int sipp_bind_socket(struct sipp_socket *socket, struct sockaddr_storage *saddr, int *port);
325
extern int sipp_connect_socket(struct sipp_socket *socket, struct sockaddr_storage *dest);
326
extern int sipp_reconnect_socket(struct sipp_socket *socket);
327
extern void sipp_customize_socket(struct sipp_socket *socket);
328
extern int delete_socket(int P_socket);
329
extern int min_socket _DEFVAL(65535);
330
extern int select_socket _DEFVAL(0);
331
extern bool socket_close _DEFVAL(true);
332
extern bool test_socket _DEFVAL(true);
333
extern bool maxSocketPresent _DEFVAL(false);
335
extern unsigned long getmilliseconds();
336
extern unsigned long long getmicroseconds();
338
/************************ Statistics **************************/
340
extern unsigned long last_report_calls _DEFVAL(0);
341
extern unsigned long nb_net_send_errors _DEFVAL(0);
342
extern unsigned long nb_net_cong _DEFVAL(0);
343
extern unsigned long nb_net_recv_errors _DEFVAL(0);
344
extern bool cpu_max _DEFVAL(false);
345
extern bool outbound_congestion _DEFVAL(false);
346
extern int open_calls_user_setting _DEFVAL(0);
347
extern int resynch_send _DEFVAL(0);
348
extern int resynch_recv _DEFVAL(0);
349
extern unsigned long rtp_pckts _DEFVAL(0);
350
extern unsigned long rtp_bytes _DEFVAL(0);
351
extern unsigned long rtp_pckts_pcap _DEFVAL(0);
352
extern unsigned long rtp_bytes_pcap _DEFVAL(0);
353
extern unsigned long rtp2_pckts _DEFVAL(0);
354
extern unsigned long rtp2_bytes _DEFVAL(0);
355
extern unsigned long rtp2_pckts_pcap _DEFVAL(0);
356
extern unsigned long rtp2_bytes_pcap _DEFVAL(0);
358
/************* Rate Control & Contexts variables **************/
360
extern int last_running_calls _DEFVAL(0);
361
extern int last_woken_calls _DEFVAL(0);
362
extern int last_paused_calls _DEFVAL(0);
363
extern unsigned int open_calls_allowed _DEFVAL(0);
364
extern unsigned long last_report_time _DEFVAL(0);
365
extern unsigned long last_dump_time _DEFVAL(0);
367
/********************** Clock variables ***********************/
369
extern unsigned long clock_tick _DEFVAL(0);
370
extern unsigned long scheduling_loops _DEFVAL(0);
371
extern unsigned long last_timer_cycle _DEFVAL(0);
373
extern unsigned long watchdog_interval _DEFVAL(400);
374
extern unsigned long watchdog_minor_threshold _DEFVAL(500);
375
extern unsigned long watchdog_minor_maxtriggers _DEFVAL(120);
376
extern unsigned long watchdog_major_threshold _DEFVAL(3000);
377
extern unsigned long watchdog_major_maxtriggers _DEFVAL(10);
378
extern unsigned long watchdog_reset _DEFVAL(600000);
381
/********************* dynamic Id ************************* */
382
extern int maxDynamicId _DEFVAL(12000); // max value for dynamicId; this value is reached
383
extern int startDynamicId _DEFVAL(10000); // offset for first dynamicId FIXME:in CmdLine
384
extern int stepDynamicId _DEFVAL(4); // step of increment for dynamicId
388
#define GET_TIME(clock) \
390
struct timezone tzp; \
391
gettimeofday (clock, &tzp); \
394
/*********************** Global Sockets **********************/
396
extern struct sipp_socket *main_socket _DEFVAL(NULL);
397
extern struct sipp_socket *main_remote_socket _DEFVAL(NULL);
398
extern struct sipp_socket *tcp_multiplex _DEFVAL(NULL);
399
extern int media_socket _DEFVAL(0);
400
extern int media_socket_video _DEFVAL(0);
402
extern struct sockaddr_storage local_sockaddr;
403
extern struct sockaddr_storage localTwin_sockaddr;
404
extern int user_port _DEFVAL(0);
405
extern char hostname[80];
406
extern bool is_ipv6 _DEFVAL(false);
408
extern int reset_number _DEFVAL(0);
409
extern bool reset_close _DEFVAL(true);
410
extern int reset_sleep _DEFVAL(1000);
411
extern bool sendbuffer_warn _DEFVAL(false);
412
/* A list of sockets pending reset. */
413
extern set<struct sipp_socket *> sockets_pending_reset;
415
extern struct addrinfo * local_addr_storage;
417
extern struct sipp_socket *twinSippSocket _DEFVAL(NULL);
418
extern struct sipp_socket *localTwinSippSocket _DEFVAL(NULL);
419
extern struct sockaddr_storage twinSipp_sockaddr;
421
/* 3pcc extended mode */
422
typedef struct _T_peer_infos {
425
struct sockaddr_storage peer_sockaddr;
427
struct sipp_socket *peer_socket ;
430
typedef std::map<std::string, char * > peer_addr_map;
431
extern peer_addr_map peer_addrs;
432
typedef std::map<std::string, T_peer_infos> peer_map;
433
extern peer_map peers;
434
typedef std::map<struct sipp_socket *, std::string > peer_socket_map;
435
extern peer_socket_map peer_sockets;
436
extern struct sipp_socket *local_sockets[MAX_LOCAL_TWIN_SOCKETS];
437
extern int local_nb _DEFVAL(0);
438
extern int peers_connected _DEFVAL(0);
440
extern struct sockaddr_storage remote_sockaddr;
441
extern short use_remote_sending_addr _DEFVAL(0);
442
extern struct sockaddr_storage remote_sending_sockaddr;
450
/************************** Trace Files ***********************/
452
extern FILE * screenf _DEFVAL(0);
453
extern FILE * countf _DEFVAL(0);
454
// extern FILE * timeoutf _DEFVAL(0);
455
extern bool useMessagef _DEFVAL(0);
456
extern bool useCallDebugf _DEFVAL(0);
457
extern bool useShortMessagef _DEFVAL(0);
458
extern bool useScreenf _DEFVAL(0);
459
extern bool useLogf _DEFVAL(0);
460
//extern bool useTimeoutf _DEFVAL(0);
461
extern bool dumpInFile _DEFVAL(0);
462
extern bool dumpInRtt _DEFVAL(0);
463
extern bool useCountf _DEFVAL(0);
464
extern char * scenario_file;
465
extern char * slave_cfg_file;
467
extern unsigned long long max_log_size _DEFVAL(0);
468
extern unsigned long long ringbuffer_size _DEFVAL(0);
469
extern int ringbuffer_files _DEFVAL(0);
471
extern char screen_last_error[32768];
472
extern char screen_logfile[MAX_PATH] _DEFVAL("");
474
/* Log Rotation Functions. */
480
struct logfile_info {
485
struct logfile_id *ftimes;
486
char file_name[MAX_PATH];
493
#ifdef GLOBALS_FULL_DEFINITION
494
#define LOGFILE(name, s, check) \
495
struct logfile_info name = { s, check, NULL, 0, NULL, "", true, false, 0, 0};
497
#define LOGFILE(name, s, check) \
498
extern struct logfile_info name;
500
LOGFILE(calldebug_lfi, "calldebug", true);
501
LOGFILE(message_lfi, "messages", true);
502
LOGFILE(shortmessage_lfi, "shortmessages", true);
503
LOGFILE(log_lfi, "logs", true);
504
LOGFILE(error_lfi, "errors", false);
506
void rotate_errorf();
508
/* Screen/Statistics Printing Functions. */
509
void print_statistics(int last);
510
void print_count_file(FILE *f, int header);
513
/********************* Mini-Parser Routines *******************/
515
int get_method(char *msg);
516
char * get_peer_tag(char *msg);
517
unsigned long int get_cseq_value(char *msg);
518
unsigned long get_reply_code(char *msg);
520
/********************** Network Interfaces ********************/
522
int send_message(int s, void ** comp_state, char * msg);
524
int send_message_tls(SSL *s, void ** comp_state, char * msg);
527
/* Socket Buffer Management. */
530
struct socketbuf *alloc_socketbuf(char *buffer, size_t size, int copy);
531
void free_socketbuf(struct socketbuf *socketbuf);
533
/* These buffers lets us read past the end of the message, and then split it if
534
* required. This eliminates the need for reading a message octet by octet and
535
* performing a second read for the content length. */
540
struct sockaddr_storage addr;
541
struct socketbuf *next;
544
/* This is an abstraction of a socket, which provides buffers for input and
547
int ss_count; /* How many users are there of this socket? */
549
int ss_transport; /* T_TCP, T_UDP, or T_TLS. */
551
bool ss_control; /* Is this a control socket? */
552
bool ss_call_socket; /* Is this a call socket? */
553
bool ss_changed_dest; /* Has the destination changed from default. */
555
int ss_fd; /* The underlying file descriptor for this socket. */
556
void *ss_comp_state; /* The compression state. */
558
SSL *ss_ssl; /* The underlying SSL descriptor for this socket. */
559
BIO *ss_bio; /* The underlying BIO descriptor for this socket. */
561
struct sockaddr_storage ss_remote_sockaddr; /* Who we are talking to. */
562
struct sockaddr_storage ss_dest; /* Who we are talking to. */
565
int ss_pollidx; /* The index of this socket in our poll structures. */
566
bool ss_congested; /* Is this socket congested? */
567
bool ss_invalid; /* Has this socket been closed remotely? */
569
struct socketbuf *ss_in; /* Buffered input. */
570
size_t ss_msglen; /* Is there a complete SIP message waiting, and if so how big? */
571
struct socketbuf *ss_out; /* Buffered output. */
574
/* Write data to a socket. */
575
int write_socket(struct sipp_socket *socket, char *buffer, ssize_t len, int flags, struct sockaddr_storage *dest);
576
/* Mark a socket as "bad". */
577
void sipp_socket_invalidate(struct sipp_socket *socket);
578
/* Actually free the socket. */
579
void sipp_close_socket(struct sipp_socket *socket);
581
#define WS_EAGAIN 1 /* Return EAGAIN if there is no room for writing the message. */
582
#define WS_BUFFER 2 /* Buffer the message if there is no room for writing the message. */
585
#if defined (__hpux) || defined (__alpha) && !defined (__FreeBSD__)
586
#define sipp_socklen_t int
588
#define sipp_socklen_t socklen_t
591
#define SOCK_ADDR_SIZE(a) \
592
(((a)->ss_family == AF_INET) ? sizeof(struct sockaddr_in) \
593
: sizeof(struct sockaddr_in6))
595
#if defined(__cplusplus) && defined (__hpux)
596
#define _RCAST(type, val) (reinterpret_cast<type> (val))
598
#define _RCAST(type, val) ((type)(val))
601
/********************* Utilities functions *******************/
603
char *strcasestr2 ( char *__haystack, char *__needle);
604
char *get_peer_addr(char *);
605
int get_decimal_from_hex(char hex);
607
bool reconnect_allowed();
608
void reset_connection(struct sipp_socket *);
609
void close_calls(struct sipp_socket *);
610
int close_connections();
611
int open_connections();
612
void timeout_alarm(int);
614
/* extended 3PCC mode */
615
struct sipp_socket **get_peer_socket(char *);
616
bool is_a_peer_socket(struct sipp_socket *);
617
bool is_a_local_socket(struct sipp_socket *);
618
void connect_to_peer (char *, int , sockaddr_storage *, char *, struct sipp_socket **);
619
void connect_to_all_peers ();
620
void connect_local_twin_socket(char *);
621
void close_peer_sockets();
622
void close_local_sockets();
623
void free_peer_addr_map();
625
/********************* Reset global kludge *******************/
627
#ifdef GLOBALS_FULL_DEFINITION
631
/* THis must go after the GLOBALS_FULL_DEFINITION, because we need the extern keyword. */
635
int TRACE_MSG(char *fmt, ...);
636
int TRACE_CALLDEBUG(char *fmt, ...);
637
int TRACE_SHORTMSG(char *fmt, ...);
638
int LOG_MSG(char *fmt, ...);