1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA
15  *
16  *  Author : Richard GAYRAUD - 04 Nov 2003
17  *           From Hewlett Packard Company.
18  */
19 
20 #ifndef __SIPP__
21 #define __SIPP__
22 
23 /* Std C includes */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #ifdef USE_SCTP
34 #ifndef __DARWIN
35 #include <netinet/sctp.h>
36 #endif
37 #endif
38 #include <sys/time.h>
39 #include <sys/poll.h>
40 #ifdef HAVE_EPOLL
41 #include <sys/epoll.h>
42 #endif
43 #include <sys/resource.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <arpa/inet.h>
47 #include <errno.h>
48 #include <netdb.h>
49 #include <ctype.h>
50 #include <signal.h>
51 #include <time.h>
52 #include <limits.h>
53 #include <vector>
54 #include <string>
55 #include <map>
56 #include <set>
57 #include <math.h>
58 #ifndef __SUNOS
59 #include <curses.h>
60 #else
61 #include <stdarg.h>
62 #endif
63 
64 #if defined(__HPUX) || defined(__SUNOS)
65 #include <alloca.h>
66 #endif
67 
68 /* Sipp includes */
69 
70 #include "xp_parser.h"
71 #include "scenario.hpp"
72 #include "screen.hpp"
73 #include "task.hpp"
74 #include "listener.hpp"
75 #include "socketowner.hpp"
76 #include "socket.hpp"
77 #include "call.hpp"
78 #include "comp.h"
79 #include "variables.hpp"
80 #include "stat.hpp"
81 #include "actions.hpp"
82 #include "infile.hpp"
83 #include "call_generation_task.hpp"
84 #include "reporttask.hpp"
85 #include "ratetask.hpp"
86 #include "watchdog.hpp"
87 /* Open SSL stuff */
88 #ifdef USE_OPENSSL
89 #include "sslcommon.h"
90 #endif
91 
92 /*
93  * If this files is included in the Main, then extern definitions
94  * are removed, and the _DEFAULT macro becomes '= value;'. Else
95  * extern definition does not contain default values assignment
96  */
97 
98 #ifdef GLOBALS_FULL_DEFINITION
99 #define extern
100 #define _DEFVAL(value) = value
101 #define extern_c
102 #else
103 #define _DEFVAL(value)
104 #ifdef __cplusplus
105 #define extern_c extern "C"
106 #else
107 #define extern_c
108 #endif
109 #endif
110 
111 /************************** Constants **************************/
112 
113 #define T_UDP                      0
114 #define T_TCP                      1
115 #define T_TLS                      2
116 #define T_SCTP                     3
117 
118 #ifdef USE_OPENSSL
119 #define DEFAULT_TLS_CERT           "cacert.pem"
120 #define DEFAULT_TLS_KEY            "cakey.pem"
121 #define DEFAULT_TLS_CRL            ""
122 #endif
123 
124 #define TRANSPORT_TO_STRING(p)     ((p==T_TCP) ? "TCP" : ((p==T_TLS)? "TLS" : ((p==T_UDP)? "UDP" : "SCTP")))
125 
126 #define SIPP_MAXFDS                65536
127 #define SIPP_MAX_MSG_SIZE          65536
128 
129 #define MSG_RETRANS_FIRST          0
130 #define MSG_RETRANS_RETRANSMISSION 1
131 #define MSG_RETRANS_NEVER          2
132 
133 #define DISPLAY_STAT_SCREEN        1
134 #define DISPLAY_REPARTITION_SCREEN 2
135 #define DISPLAY_SCENARIO_SCREEN    3
136 #define DISPLAY_VARIABLE_SCREEN    4
137 #define DISPLAY_TDM_MAP_SCREEN     5
138 #define DISPLAY_SECONDARY_REPARTITION_SCREEN 6
139 
140 #define MAX_RECV_LOOPS_PER_CYCLE   1000
141 #define MAX_SCHED_LOOPS_PER_CYCLE  1000
142 #define NB_UPDATE_PER_CYCLE        1
143 
144 #define MAX_PEER_SIZE              4096  /* 3pcc extended mode: max size of peer names */
145 #define MAX_LOCAL_TWIN_SOCKETS     10    /*3pcc extended mode:max number of peers from which
146 cmd messages are received */
147 
148 /******************** Default parameters ***********************/
149 
150 #define DEFAULT_RATE                 10.0
151 #define DEFAULT_RATE_SCALE           1.0
152 #define DEFAULT_RATE_PERIOD_MS       1000
153 #define DEFAULT_TRANSPORT            T_UDP
154 #define DEFAULT_PORT                 5060
155 #define DEFAULT_MEDIA_PORT           6000
156 #define DEFAULT_3PCC_PORT            6060
157 #define DEFAULT_SERVICE              "service"
158 #define DEFAULT_AUTH_PASSWORD        "password"
159 #define DEFAULT_REPORT_FREQ          1000
160 #define DEFAULT_RATE_INCR_FREQ       0
161 #define DEFAULT_REPORT_FREQ_DUMP_LOG 60000
162 #define DEFAULT_TIMER_RESOLUTION     1
163 #define DEFAULT_FREQ_DUMP_RTT        200
164 #define DEFAULT_MAX_MULTI_SOCKET     50000
165 #define DEFAULT_CTRL_SOCKET_PORT     8888
166 #define DEFAULT_DEADCALL_WAIT        33000
167 
168 #define DEFAULT_BEHAVIOR_NONE        0
169 #define DEFAULT_BEHAVIOR_BYE         1
170 #define DEFAULT_BEHAVIOR_ABORTUNEXP  2
171 #define DEFAULT_BEHAVIOR_PINGREPLY   4
172 
173 #define DEFAULT_BEHAVIOR_ALL         (DEFAULT_BEHAVIOR_BYE | DEFAULT_BEHAVIOR_ABORTUNEXP | DEFAULT_BEHAVIOR_PINGREPLY)
174 
175 #ifdef RTP_STREAM
176 #define DEFAULT_MIN_RTP_PORT         8192
177 #define DEFAULT_MAX_RTP_PORT         65535
178 #define DEFAULT_RTP_PAYLOAD          8
179 #define DEFAULT_RTP_THREADTASKS      20
180 #endif
181 
182 /************ User controls and command line options ***********/
183 
184 extern int                duration                _DEFVAL(0);
185 extern double             rate                    _DEFVAL(DEFAULT_RATE);
186 extern double             rate_scale              _DEFVAL(DEFAULT_RATE_SCALE);
187 extern int                rate_increase           _DEFVAL(0);
188 extern int                rate_max                _DEFVAL(0);
189 extern unsigned long      rate_increase_freq      _DEFVAL(DEFAULT_RATE_INCR_FREQ);
190 extern bool               rate_quit               _DEFVAL(true);
191 extern int                users                   _DEFVAL(-1);
192 extern int                rate_period_ms          _DEFVAL(DEFAULT_RATE_PERIOD_MS);
193 extern int                sleeptime               _DEFVAL(0);
194 extern unsigned long      defl_recv_timeout       _DEFVAL(0);
195 extern unsigned long      defl_send_timeout       _DEFVAL(0);
196 extern unsigned long      global_timeout          _DEFVAL(0);
197 extern int                transport               _DEFVAL(DEFAULT_TRANSPORT);
198 extern bool               retrans_enabled         _DEFVAL(1);
199 extern int                rtcheck                 _DEFVAL(RTCHECK_FULL);
200 extern int                max_udp_retrans         _DEFVAL(UDP_MAX_RETRANS);
201 extern int                max_invite_retrans      _DEFVAL(UDP_MAX_RETRANS_INVITE_TRANSACTION);
202 extern int                max_non_invite_retrans  _DEFVAL(UDP_MAX_RETRANS_NON_INVITE_TRANSACTION);
203 extern unsigned long      default_behaviors       _DEFVAL(DEFAULT_BEHAVIOR_ALL);
204 extern unsigned long      deadcall_wait           _DEFVAL(DEFAULT_DEADCALL_WAIT);
205 extern bool               pause_msg_ign           _DEFVAL(0);
206 extern bool               auto_answer             _DEFVAL(false);
207 extern int                multisocket             _DEFVAL(0);
208 extern int                compression             _DEFVAL(0);
209 extern int                peripsocket             _DEFVAL(0);
210 extern int                peripfield              _DEFVAL(0);
211 extern bool               bind_local              _DEFVAL(false);
212 extern void             * monosocket_comp_state   _DEFVAL(0);
213 extern const char       * service                 _DEFVAL(DEFAULT_SERVICE);
214 extern const char       * auth_password           _DEFVAL(DEFAULT_AUTH_PASSWORD);
215 extern const char       * auth_username           _DEFVAL(0);
216 extern unsigned long      report_freq             _DEFVAL(DEFAULT_REPORT_FREQ);
217 extern unsigned long      report_freq_dumpLog     _DEFVAL
218 (DEFAULT_REPORT_FREQ_DUMP_LOG);
219 extern bool               periodic_rtd            _DEFVAL(false);
220 extern const char       * stat_delimiter          _DEFVAL(";");
221 
222 extern bool               timeout_exit            _DEFVAL(false);
223 extern bool               timeout_error           _DEFVAL(false);
224 
225 extern unsigned long      report_freq_dumpRtt     _DEFVAL
226 (DEFAULT_FREQ_DUMP_RTT);
227 
228 extern int                max_multi_socket        _DEFVAL
229 (DEFAULT_MAX_MULTI_SOCKET);
230 extern bool               skip_rlimit             _DEFVAL(false);
231 
232 extern unsigned int       timer_resolution        _DEFVAL(DEFAULT_TIMER_RESOLUTION);
233 extern int                max_recv_loops          _DEFVAL(MAX_RECV_LOOPS_PER_CYCLE);
234 extern int                max_sched_loops         _DEFVAL(MAX_SCHED_LOOPS_PER_CYCLE);
235 
236 extern unsigned int       global_t2               _DEFVAL(DEFAULT_T2_TIMER_VALUE);
237 
238 extern char               local_ip[40];
239 extern char               local_ip_escaped[42];
240 extern bool               local_ip_is_ipv6;
241 extern int                local_port              _DEFVAL(0);
242 #ifdef USE_SCTP
243 extern char               multihome_ip[40];
244 extern int                heartbeat               _DEFVAL(0);
245 extern int                assocmaxret             _DEFVAL(0);
246 extern int                pathmaxret              _DEFVAL(0);
247 extern int                pmtu                    _DEFVAL(0);
248 extern bool               gracefulclose           _DEFVAL(true);
249 #endif
250 extern char               control_ip[40];
251 extern int                control_port            _DEFVAL(0);
252 extern int                buff_size               _DEFVAL(65535);
253 extern int                tcp_readsize            _DEFVAL(65535);
254 #if defined(PCAPPLAY) || defined(RTP_STREAM)
255 extern int                hasMedia                _DEFVAL(0);
256 #endif
257 #ifdef RTP_STREAM
258 extern int                min_rtp_port            _DEFVAL(DEFAULT_MIN_RTP_PORT);
259 extern int                max_rtp_port            _DEFVAL(DEFAULT_MAX_RTP_PORT);
260 extern int                rtp_default_payload     _DEFVAL(DEFAULT_RTP_PAYLOAD);
261 extern int                rtp_tasks_per_thread    _DEFVAL(DEFAULT_RTP_THREADTASKS);
262 extern int                rtp_buffsize            _DEFVAL(65535);
263 #endif
264 
265 extern bool               rtp_echo_enabled        _DEFVAL(0);
266 extern char               media_ip[40];
267 extern char               media_ip_escaped[42];
268 extern int                user_media_port         _DEFVAL(0);
269 extern int                media_port              _DEFVAL(0);
270 extern size_t             media_bufsize           _DEFVAL(2048);
271 extern_c bool             media_ip_is_ipv6        _DEFVAL(false);
272 extern char               remote_ip[40];
273 extern char               remote_ip_escaped[42];
274 extern int                remote_port             _DEFVAL(DEFAULT_PORT);
275 extern unsigned int       pid                     _DEFVAL(0);
276 extern bool               print_all_responses     _DEFVAL(false);
277 extern unsigned long      stop_after              _DEFVAL(0xffffffff);
278 extern int                quitting                _DEFVAL(0);
279 extern int                interrupt               _DEFVAL(0);
280 extern bool               paused                  _DEFVAL(false);
281 extern int                lose_packets            _DEFVAL(0);
282 extern double             global_lost             _DEFVAL(0.0);
283 extern char               remote_host[255];
284 extern char               twinSippHost[255];
285 extern char               twinSippIp[40];
286 extern char             * master_name;
287 extern char             * slave_number;
288 extern int                twinSippPort            _DEFVAL(DEFAULT_3PCC_PORT);
289 extern bool               twinSippMode            _DEFVAL(false);
290 extern bool               extendedTwinSippMode    _DEFVAL(false);
291 
292 extern bool               nostdin                 _DEFVAL(false);
293 extern bool               use_curses              _DEFVAL(true);
294 extern bool               backgroundMode          _DEFVAL(false);
295 extern bool               signalDump              _DEFVAL(false);
296 
297 extern int                currentScreenToDisplay  _DEFVAL
298 (DISPLAY_SCENARIO_SCREEN);
299 extern int                currentRepartitionToDisplay  _DEFVAL(1);
300 extern unsigned int       base_cseq               _DEFVAL(0);
301 extern char             * auth_uri                _DEFVAL(0);
302 extern const char       * call_id_string          _DEFVAL("%u-%p@%s");
303 extern char             **generic[100];
304 
305 extern bool               callidSlash             _DEFVAL(false);
306 
307 /* TDM map */
308 extern bool               use_tdmmap              _DEFVAL(false);
309 extern unsigned int       tdm_map_a               _DEFVAL(0);
310 extern unsigned int       tdm_map_b               _DEFVAL(0);
311 extern unsigned int       tdm_map_c               _DEFVAL(0);
312 extern unsigned int       tdm_map_x               _DEFVAL(0);
313 extern unsigned int       tdm_map_y               _DEFVAL(0);
314 extern unsigned int       tdm_map_z               _DEFVAL(0);
315 extern unsigned int       tdm_map_h               _DEFVAL(0);
316 extern bool               tdm_map[1024];
317 
318 #ifdef USE_OPENSSL
319 extern BIO              * twinSipp_bio;
320 extern SSL              * twinSipp_ssl;
321 extern const char       * tls_cert_name           _DEFVAL(DEFAULT_TLS_CERT);
322 extern const char       * tls_key_name            _DEFVAL(DEFAULT_TLS_KEY);
323 extern const char       * tls_crl_name            _DEFVAL(DEFAULT_TLS_CRL);
324 
325 #endif
326 
327 extern char*              scenario_file           _DEFVAL(NULL);
328 extern_c char*              scenario_path         _DEFVAL(NULL);
329 
330 // extern field file management
331 typedef std::map<string, FileContents *> file_map;
332 extern file_map inFiles;
333 typedef std::map<string, str_int_map *> file_index;
334 extern char *ip_file _DEFVAL(NULL);
335 extern char *default_file _DEFVAL(NULL);
336 
337 // free user id list
338 extern list<int> freeUsers;
339 extern list<int> retiredUsers;
340 extern AllocVariableTable *globalVariables        _DEFVAL(NULL);
341 extern AllocVariableTable *userVariables          _DEFVAL(NULL);
342 typedef std::map<int, VariableTable *> int_vt_map;
343 extern int_vt_map         userVarMap;
344 
345 //extern int      new_socket(bool P_use_ipv6, int P_type_socket, int * P_status);
346 extern struct   sipp_socket *new_sipp_socket(bool use_ipv6, int transport);
347 struct sipp_socket *new_sipp_call_socket(bool use_ipv6, int transport, bool *existing);
348 struct sipp_socket *sipp_accept_socket(struct sipp_socket *accept_socket);
349 extern int      sipp_bind_socket(struct sipp_socket *socket, struct sockaddr_storage *saddr, int *port);
350 extern int      sipp_connect_socket(struct sipp_socket *socket, struct sockaddr_storage *dest);
351 extern int      sipp_reconnect_socket(struct sipp_socket *socket);
352 extern void     sipp_customize_socket(struct sipp_socket *socket);
353 extern int      delete_socket(int P_socket);
354 extern int      min_socket          _DEFVAL(65535);
355 extern int      select_socket       _DEFVAL(0);
356 extern bool     socket_close        _DEFVAL(true);
357 extern bool     test_socket         _DEFVAL(true);
358 extern bool     maxSocketPresent    _DEFVAL(false);
359 
360 #include "time.hpp"
361 
362 /************************ Statistics **************************/
363 
364 extern unsigned long last_report_calls            _DEFVAL(0);
365 extern unsigned long nb_net_send_errors           _DEFVAL(0);
366 extern unsigned long nb_net_cong                  _DEFVAL(0);
367 extern unsigned long nb_net_recv_errors           _DEFVAL(0);
368 extern bool          cpu_max                      _DEFVAL(false);
369 extern bool          outbound_congestion          _DEFVAL(false);
370 extern int           open_calls_user_setting      _DEFVAL(0);
371 extern int           resynch_send                 _DEFVAL(0);
372 extern int           resynch_recv                 _DEFVAL(0);
373 extern unsigned long rtp_pckts                    _DEFVAL(0);
374 extern unsigned long rtp_bytes                    _DEFVAL(0);
375 extern_c unsigned long rtp_pckts_pcap             _DEFVAL(0);
376 extern_c unsigned long rtp_bytes_pcap             _DEFVAL(0);
377 extern unsigned long rtp2_pckts                   _DEFVAL(0);
378 extern unsigned long rtp2_bytes                   _DEFVAL(0);
379 extern unsigned long rtp2_pckts_pcap              _DEFVAL(0);
380 extern unsigned long rtp2_bytes_pcap              _DEFVAL(0);
381 #ifdef RTP_STREAM
382 extern volatile unsigned long rtpstream_numthreads _DEFVAL(0);
383 extern volatile unsigned long rtpstream_bytes_in  _DEFVAL(0);
384 extern volatile unsigned long rtpstream_bytes_out _DEFVAL(0);
385 extern volatile unsigned long rtpstream_pckts     _DEFVAL(0);
386 #endif
387 
388 
389 /************* Rate Control & Contexts variables **************/
390 
391 extern int           last_running_calls           _DEFVAL(0);
392 extern int           last_woken_calls             _DEFVAL(0);
393 extern int           last_paused_calls            _DEFVAL(0);
394 extern unsigned int  open_calls_allowed           _DEFVAL(0);
395 extern unsigned long last_report_time             _DEFVAL(0);
396 extern unsigned long last_dump_time               _DEFVAL(0);
397 extern unsigned long last_rate_increase_time      _DEFVAL(0);
398 
399 /********************** Clock variables ***********************/
400 
401 extern unsigned long clock_tick                   _DEFVAL(0);
402 extern unsigned long scheduling_loops             _DEFVAL(0);
403 extern unsigned long last_timer_cycle             _DEFVAL(0);
404 
405 extern unsigned long watchdog_interval            _DEFVAL(400);
406 extern unsigned long watchdog_minor_threshold     _DEFVAL(500);
407 extern unsigned long watchdog_minor_maxtriggers   _DEFVAL(120);
408 extern unsigned long watchdog_major_threshold     _DEFVAL(3000);
409 extern unsigned long watchdog_major_maxtriggers   _DEFVAL(10);
410 extern unsigned long watchdog_reset               _DEFVAL(600000);
411 
412 
413 /********************* dynamic Id ************************* */
414 extern  int maxDynamicId    _DEFVAL(12000);  // max value for dynamicId; this value is reached
415 extern  int startDynamicId  _DEFVAL(10000);  // offset for first dynamicId  FIXME:in CmdLine
416 extern  int stepDynamicId   _DEFVAL(4);      // step of increment for dynamicId
417 
418 
419 
420 #define GET_TIME(clock)       \
421 {                             \
422   struct timezone tzp;        \
423   gettimeofday (clock, &tzp); \
424 }
425 
426 /*********************** Global Sockets  **********************/
427 
428 extern struct sipp_socket *main_socket            _DEFVAL(NULL);
429 extern struct sipp_socket *main_remote_socket     _DEFVAL(NULL);
430 extern struct sipp_socket *tcp_multiplex          _DEFVAL(NULL);
431 extern int           media_socket                 _DEFVAL(0);
432 extern int           media_socket_video           _DEFVAL(0);
433 
434 extern struct sockaddr_storage  local_sockaddr;
435 extern struct sockaddr_storage  localTwin_sockaddr;
436 extern int           user_port                    _DEFVAL(0);
437 extern char          hostname[80];
438 extern bool          is_ipv6                      _DEFVAL(false);
439 
440 extern int           reset_number                 _DEFVAL(0);
441 extern bool          reset_close                  _DEFVAL(true);
442 extern int           reset_sleep                  _DEFVAL(1000);
443 extern bool          sendbuffer_warn              _DEFVAL(false);
444 /* A list of sockets pending reset. */
445 extern set<struct sipp_socket *> sockets_pending_reset;
446 
447 extern struct addrinfo *local_addr_storage;
448 
449 extern struct sipp_socket *twinSippSocket         _DEFVAL(NULL);
450 extern struct sipp_socket *localTwinSippSocket    _DEFVAL(NULL);
451 extern struct sockaddr_storage twinSipp_sockaddr;
452 
453 /* 3pcc extended mode */
454 typedef struct _T_peer_infos {
455     char                      peer_host[40];
456     int                       peer_port;
457     struct sockaddr_storage   peer_sockaddr;
458     char                      peer_ip[40];
459     struct sipp_socket        *peer_socket;
460 } T_peer_infos;
461 
462 typedef std::map<std::string, char * > peer_addr_map;
463 extern peer_addr_map peer_addrs;
464 typedef std::map<std::string, T_peer_infos> peer_map;
465 extern peer_map      peers;
466 typedef std::map<struct sipp_socket *, std::string > peer_socket_map;
467 extern peer_socket_map peer_sockets;
468 extern struct sipp_socket *local_sockets[MAX_LOCAL_TWIN_SOCKETS];
469 extern int           local_nb                    _DEFVAL(0);
470 extern int           peers_connected             _DEFVAL(0);
471 
472 extern struct sockaddr_storage remote_sockaddr;
473 extern short         use_remote_sending_addr      _DEFVAL(0);
474 extern struct sockaddr_storage remote_sending_sockaddr;
475 
476 enum E_Alter_YesNo {
477     E_ALTER_YES=0,
478     E_ALTER_NO
479 };
480 
481 #include "logger.hpp"
482 
483 /********************* Utilities functions  *******************/
484 
485 #include "strings.hpp"
486 
487 void sipp_exit(int rc);
488 
489 char *get_peer_addr(char *);
490 
491 bool reconnect_allowed();
492 void reset_connection(struct sipp_socket *);
493 void close_calls(struct sipp_socket *);
494 int close_connections();
495 int open_connections();
496 void timeout_alarm(int);
497 
498 /* extended 3PCC mode */
499 struct sipp_socket **get_peer_socket(char *);
500 bool is_a_peer_socket(struct sipp_socket *);
501 bool is_a_local_socket(struct sipp_socket *);
502 void connect_to_peer (char *, int , sockaddr_storage *, char *, struct sipp_socket **);
503 void connect_to_all_peers ();
504 void connect_local_twin_socket(char *);
505 void close_peer_sockets();
506 void close_local_sockets();
507 void free_peer_addr_map();
508 
509 /********************* Reset global kludge  *******************/
510 
511 #ifdef GLOBALS_FULL_DEFINITION
512 #undef extern
513 #endif
514 
515 #endif // __SIPP__
516