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 * Charles P. Wright from IBM Research 19 * Andy Aicken 20 */ 21 22 #ifndef __CALL__ 23 #define __CALL__ 24 25 #include <map> 26 #include <list> 27 #include <sys/types.h> 28 #include <sys/socket.h> 29 #include <string.h> 30 #include "scenario.hpp" 31 #include "stat.hpp" 32 #ifdef USE_OPENSSL 33 #include "sslcommon.h" 34 #endif 35 #ifdef PCAPPLAY 36 #include "send_packets.h" 37 #endif 38 #ifdef RTP_STREAM 39 #include "rtpstream.hpp" 40 #endif 41 42 #ifndef MAX 43 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 44 #endif 45 #include "sip_parser.hpp" 46 47 #define UDP_MAX_RETRANS_INVITE_TRANSACTION 5 48 #define UDP_MAX_RETRANS_NON_INVITE_TRANSACTION 9 49 #define UDP_MAX_RETRANS MAX(UDP_MAX_RETRANS_INVITE_TRANSACTION, UDP_MAX_RETRANS_NON_INVITE_TRANSACTION) 50 #define MAX_SUB_MESSAGE_LENGTH 2049 51 #define DEFAULT_T2_TIMER_VALUE 4000 52 #define SIP_TRANSACTION_TIMEOUT 32000 53 54 /* Retransmission check methods. */ 55 #define RTCHECK_FULL 1 56 #define RTCHECK_LOOSE 2 57 58 59 struct txnInstanceInfo { 60 char *txnID; 61 unsigned long txnResp; 62 int ackIndex; 63 }; 64 65 class call : virtual public task, virtual public listener, public virtual socketowner 66 { 67 public: 68 /* These are wrappers for various circumstances, (private) init does the real work. */ 69 //call(char * p_id, int userId, bool ipv6, bool isAutomatic); 70 call(const char *p_id, bool use_ipv6, int userId, struct sockaddr_storage *dest); 71 call(const char *p_id, struct sipp_socket *socket, struct sockaddr_storage *dest); 72 static call *add_call(int userId, bool ipv6, struct sockaddr_storage *dest); 73 call(scenario * call_scenario, struct sipp_socket *socket, struct sockaddr_storage *dest, const char * p_id, int userId, bool ipv6, bool isAutomatic, bool isInitCall); 74 75 virtual ~call(); 76 77 virtual bool process_incoming(char * msg, struct sockaddr_storage *src = NULL); 78 virtual bool process_twinSippCom(char * msg); 79 80 virtual bool run(); 81 /* Terminate this call, depending on action results and timewait. */ 82 virtual void terminate(CStat::E_Action reason); 83 virtual void tcpClose(); 84 85 /* When should this call wake up? */ 86 virtual unsigned int wake(); 87 virtual bool abortCall(bool writeLog); // call aborted with BYE or CANCEL 88 virtual void abort(); 89 90 /* Dump call info to error log. */ 91 virtual void dump(); 92 93 /* Automatic */ 94 enum T_AutoMode { 95 E_AM_DEFAULT, 96 E_AM_UNEXP_BYE, 97 E_AM_UNEXP_CANCEL, 98 E_AM_PING, 99 E_AM_AA, 100 E_AM_OOCALL 101 }; 102 103 void setLastMsg(const char *msg); 104 bool automaticResponseMode(T_AutoMode P_case, char* P_recv); getLastReceived()105 const char *getLastReceived() { 106 return last_recv_msg; 107 }; 108 109 private: 110 /* This is the core constructor function. */ 111 void init(scenario * call_scenario, struct sipp_socket *socket, struct sockaddr_storage *dest, const char * p_id, int userId, bool ipv6, bool isAutomatic, bool isInitCall); 112 /* This this call for initialization? */ 113 bool initCall; 114 115 struct sockaddr_storage call_peer; 116 117 scenario *call_scenario; 118 unsigned int number; 119 120 public: 121 static int maxDynamicId; // max value for dynamicId; this value is reached ! 122 static int startDynamicId; // offset for first dynamicId FIXME:in CmdLine 123 static int stepDynamicId; // step of increment for dynamicId 124 static int dynamicId; // a counter for general use, incrementing by stepDynamicId starting at startDynamicId wrapping at maxDynamicId GLOBALY 125 private: 126 127 128 unsigned int tdm_map_number; 129 130 int msg_index; 131 int zombie; 132 char * realloc_ptr; 133 134 /* Last message sent from scenario step (retransmitions do not 135 * change this index. Only message sent from the scenario 136 * are kept in this index.) */ 137 int last_send_index; 138 char * last_send_msg; 139 int last_send_len; 140 141 /* How long until sending this message times out. */ 142 unsigned int send_timeout; 143 144 /* Last received message (expected, not optional, and not 145 * retransmitted) and the associated hash. Stills setted until a new 146 * scenario steps sends a message */ 147 unsigned long last_recv_hash; 148 int last_recv_index; 149 char * last_recv_msg; 150 151 /* Recv message characteristics when we sent a valid message 152 * (scneario, no retrans) just after a valid reception. This was 153 * a cause relationship, so the next time this cookie will be recvd, 154 * we will retransmit the same message we sent this time */ 155 unsigned long recv_retrans_hash; 156 int recv_retrans_recv_index; 157 int recv_retrans_send_index; 158 unsigned int recv_timeout; 159 160 /* holds the route set */ 161 char * dialog_route_set; 162 char * next_req_url; 163 164 /* cseq value for [cseq] keyword */ 165 unsigned int cseq; 166 167 #ifdef PCAPPLAY 168 int hasMediaInformation; 169 pthread_t media_thread; 170 play_args_t play_args_a; 171 play_args_t play_args_i; 172 play_args_t play_args_v; 173 #endif 174 175 #ifdef RTP_STREAM 176 rtpstream_callinfo_t rtpstream_callinfo; 177 #endif 178 179 /* holds the auth header and if the challenge was 401 or 407 */ 180 char * dialog_authentication; 181 int dialog_challenge_type; 182 183 unsigned int next_retrans; 184 int nb_retrans; 185 unsigned int nb_last_delay; 186 187 unsigned int paused_until; 188 189 unsigned long start_time; 190 unsigned long long *start_time_rtd; 191 bool *rtd_done; 192 193 char *peer_tag; 194 195 struct sipp_socket *call_remote_socket; 196 int call_port; 197 198 void * comp_state; 199 200 int deleted; 201 202 bool call_established; // == true when the call is established 203 // ie ACK received or sent 204 // => init to false 205 bool ack_is_pending; // == true if an ACK is pending 206 // Needed to avoid abortCall sending a 207 // CANCEL instead of BYE in some extreme 208 // cases for 3PCC scenario. 209 // => init to false 210 211 /* Call Variable Table */ 212 VariableTable *M_callVariableTable; 213 214 /* Our transaction IDs. */ 215 struct txnInstanceInfo *transactions; 216 217 /* result of execute action */ 218 enum T_ActionResult { 219 E_AR_NO_ERROR = 0, 220 E_AR_REGEXP_DOESNT_MATCH, 221 E_AR_REGEXP_SHOULDNT_MATCH, 222 E_AR_STOP_CALL, 223 E_AR_CONNECT_FAILED, 224 E_AR_HDR_NOT_FOUND 225 }; 226 227 /* Store the last action result to allow */ 228 /* call to continue and mark it as failed */ 229 T_ActionResult last_action_result; 230 231 /* rc == true means call not deleted by processing */ 232 void formatNextReqUrl (char* next_req_url); 233 void computeRouteSetAndRemoteTargetUri (char* rrList, char* contact, bool bRequestIncoming); 234 bool matches_scenario(unsigned int index, int reply_code, char * request, char * responsecseqmethod, char *txn); 235 236 bool executeMessage(message *curmsg); 237 T_ActionResult executeAction(char * msg, message *message); 238 void extractSubMessage(char * msg, char * matchingString, char* result, bool case_indep, 239 int occurrence, bool headers); 240 bool rejectCall(); 241 double get_rhs(CAction *currentAction); 242 243 // P_index use for message index in scenario and ctrl of CRLF 244 // P_index = -2 No ctrl of CRLF 245 // P_index = -1 Add crlf to end of message 246 char* createSendingMessage(SendingMessage *src, int P_index, int *msgLen=NULL); 247 char* createSendingMessage(char * src, int P_index, bool skip_sanity = false); 248 char* createSendingMessage(SendingMessage *src, int P_index, char *msg_buffer, int buflen, int *msgLen=NULL); 249 250 // method for the management of unexpected messages 251 bool checkInternalCmd(char* cmd); // check of specific internal command 252 // received from the twin socket 253 // used for example to cancel the call 254 // of the third party 255 bool check_peer_src(char* msg, 256 int search_index); // 3pcc extended mode:check if 257 // the twin message received 258 // comes from the expected sender 259 void sendBuffer(char *buf, int len = 0); // send a message out of a scenario 260 // execution 261 262 T_AutoMode checkAutomaticResponseMode(char * P_recv); 263 264 int sendCmdMessage(message *curmsg); // 3PCC 265 266 int sendCmdBuffer(char* cmd); // for 3PCC, send a command out of a 267 // scenario execution 268 269 static void readInputFileContents(const char* fileName); 270 static void dumpFileContents(void); 271 272 void getFieldFromInputFile(const char* fileName, int field, SendingMessage *line, char*& dest); 273 274 /* Associate a user with this call. */ 275 void setUser(int userId); 276 277 /* Is this call just around for final retransmissions. */ 278 bool timewait; 279 280 /* rc == true means call not deleted by processing */ 281 bool next(); 282 bool process_unexpected(char * msg); 283 void do_bookkeeping(message *curmsg); 284 285 void extract_cseq_method (char* responseCseq, char* msg); 286 void extract_transaction (char* txn, char* msg); 287 288 int send_raw(const char * msg, int index, int len); 289 char * send_scene(int index, int *send_status, int *msgLen); 290 bool connect_socket_if_needed(); 291 292 char * get_header_field_code(const char * msg, const char * code); 293 char * get_last_header(const char * name); 294 char * get_last_request_uri(); 295 unsigned long hash(const char * msg); 296 297 typedef std::map <std::string, int> file_line_map; 298 file_line_map *m_lineNumber; 299 int userId; 300 301 bool use_ipv6; 302 303 void get_remote_media_addr(char * message); 304 305 #ifdef RTP_STREAM 306 void extract_rtp_remote_addr (char * message); 307 #endif 308 309 bool lost(int index); 310 311 void computeStat (CStat::E_Action P_action); 312 void computeStat (CStat::E_Action P_action, unsigned long P_value); 313 void computeStat (CStat::E_Action P_action, unsigned long P_value, int which); 314 315 316 void queue_up(char *msg); 317 char *queued_msg; 318 319 320 #ifdef USE_OPENSSL 321 SSL_CTX *m_ctx_ssl; 322 BIO *m_bio; 323 #endif 324 325 int _callDebug(const char *fmt, ...) __attribute__((format(printf, 2, 3))); 326 char *debugBuffer; 327 int debugLength; 328 }; 329 330 331 /* Default Message Functions. */ 332 void init_default_messages(); 333 void free_default_messages(); 334 SendingMessage *get_default_message(const char *which); 335 void set_default_message(const char *which, char *message); 336 337 #endif 338