Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Committer:
klauss
Date:
Wed May 13 21:18:12 2015 +0000
Revision:
125:8ff4dc96ad58
Parent:
124:c1b6c893e1c3
Child:
132:05cd37f7e007
Implementado; - dtx [ on | off ]; - drx [ on | off ]; - drxshow [ on | off ]; - dtxshow [ on | off ]; - end_call <ext>; - request_clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
klauss 0:4d17cd9c8f9d 1 #include "sip.h"
klauss 0:4d17cd9c8f9d 2
klauss 121:ee02790d00b7 3 //*[ begin ] ------------------------------------ Funcoes de uso interno ---------------------------------------------- *//
klauss 121:ee02790d00b7 4 void Sip::__init_sock__ ( void )
klauss 121:ee02790d00b7 5 {
klauss 121:ee02790d00b7 6 sip_server.set_address ( this->server_ip , this->server_port );
klauss 121:ee02790d00b7 7 sock.set_blocking ( false, 0 );
klauss 121:ee02790d00b7 8 sock.bind ( this->my_port );
klauss 0:4d17cd9c8f9d 9 }
klauss 0:4d17cd9c8f9d 10
klauss 121:ee02790d00b7 11 void Sip::__end_sock__ ( void )
klauss 121:ee02790d00b7 12 {
klauss 121:ee02790d00b7 13 sock.close ();
klauss 0:4d17cd9c8f9d 14 }
klauss 0:4d17cd9c8f9d 15
klauss 121:ee02790d00b7 16 void Sip::__reconnect__ ( void )
klauss 121:ee02790d00b7 17 {
klauss 119:ee6a53069455 18 __end_sock__();
klauss 114:472502b31a12 19 sock.init();
klauss 114:472502b31a12 20 sock.bind( this->my_port );
klauss 114:472502b31a12 21 }
klauss 121:ee02790d00b7 22 //*[ end ] -------------------------------------- Funcoes de uso interno ---------------------------------------------- *//
klauss 121:ee02790d00b7 23
klauss 29:7246460b73f8 24
klauss 121:ee02790d00b7 25 Sip::Sip ( const int new_ext, const uint16_t my_port )
klauss 121:ee02790d00b7 26 {
klauss 121:ee02790d00b7 27 char inner_tmp [ 20 ];
klauss 28:94aec56c6329 28
klauss 121:ee02790d00b7 29 if ( debug_sip ) debug_msg ("[%d, %d] Building SIP", new_ext, my_port );
klauss 27:98f824719d1c 30
klauss 119:ee6a53069455 31 this->my_ext = new_ext;
klauss 112:6ae726539ab9 32 this->my_port = my_port;
klauss 29:7246460b73f8 33
klauss 125:8ff4dc96ad58 34 FILE * fip = fopen ( "/qspi/serverip.txt", "r");
klauss 125:8ff4dc96ad58 35 if ( fip == NULL)
klauss 121:ee02790d00b7 36 {
klauss 121:ee02790d00b7 37 if ( debug_sip ) debug_msg("[%d] Failed to open /qspi/serverip.txt", this->my_ext );
klauss 121:ee02790d00b7 38 strncpy ( this->server_ip, SERVER_IP, 20 );
klauss 121:ee02790d00b7 39 } else {
klauss 121:ee02790d00b7 40 char tmp [ 16 ] = "\0";
klauss 125:8ff4dc96ad58 41 int read = fread ( tmp, 1, 15, fip );
klauss 121:ee02790d00b7 42 if ( read > 0 ){
klauss 121:ee02790d00b7 43 for ( int i = 0; i < read; i++ )
klauss 121:ee02790d00b7 44 if ( tmp [ i ] == '\n' || tmp[ i ] == '\r' ){
klauss 121:ee02790d00b7 45 tmp [ i ] = '\0';
klauss 121:ee02790d00b7 46 break;
klauss 121:ee02790d00b7 47 }
klauss 121:ee02790d00b7 48 strcpy ( this->server_ip, tmp );
klauss 121:ee02790d00b7 49 if ( debug_sip ) debug_msg ("[%d] ip server%s( read %d chars ), valid %d", this->my_ext, this->server_ip, read, strlen( this->server_ip ) );
klauss 121:ee02790d00b7 50 }else{
klauss 121:ee02790d00b7 51 if ( debug_sip ) debug_msg ("[%d] Failed to read /qspi/serverip.txt", this->my_ext );
klauss 121:ee02790d00b7 52 strncpy ( this->server_ip, SERVER_IP, 20 );
klauss 121:ee02790d00b7 53 }
klauss 121:ee02790d00b7 54 }
klauss 125:8ff4dc96ad58 55 if ( fip ) fclose ( fip );
klauss 121:ee02790d00b7 56 this->server_ip [ 15 ] = '\0';
klauss 121:ee02790d00b7 57
klauss 125:8ff4dc96ad58 58 FILE * sp = fopen ( "/qspi/serverport.txt", "r");
klauss 125:8ff4dc96ad58 59 if ( sp == NULL )
klauss 121:ee02790d00b7 60 {
klauss 121:ee02790d00b7 61 if ( debug_sip ) debug_msg ("[%d] Failed to open /qspi/serverport.txt", this->my_ext );
klauss 121:ee02790d00b7 62 this->server_port = SERVER_PORT;
klauss 121:ee02790d00b7 63 } else {
klauss 125:8ff4dc96ad58 64 if ( fread ( (void *)inner_tmp, 1, 20, sp ) > 0 )
klauss 121:ee02790d00b7 65 {
klauss 121:ee02790d00b7 66 server_port = atoi ( inner_tmp );
klauss 121:ee02790d00b7 67 if ( debug_sip ) debug_msg ("[%d] server port %d", this->my_ext, this->server_port );
klauss 121:ee02790d00b7 68 } else {
klauss 121:ee02790d00b7 69 if ( debug_sip ) debug_msg ("F[%d] failed to read /qspi/serverport.txt", this->my_ext );
klauss 121:ee02790d00b7 70 this->server_port = SERVER_PORT;
klauss 121:ee02790d00b7 71 }
klauss 121:ee02790d00b7 72 }
klauss 125:8ff4dc96ad58 73 if ( sp ) fclose ( sp );
klauss 121:ee02790d00b7 74
klauss 125:8ff4dc96ad58 75 FILE * fhip = fopen ( "/qspi/myip.txt", "r");
klauss 125:8ff4dc96ad58 76 if ( fhip == NULL )
klauss 121:ee02790d00b7 77 {
klauss 121:ee02790d00b7 78 if ( debug_sip ) debug_msg ("[%d] Failed to open /qspi/myip.txt", this->my_ext );
klauss 121:ee02790d00b7 79 strncpy ( this->my_ip, MY_IP, 20 );
klauss 121:ee02790d00b7 80 } else {
klauss 50:d9b6577a70f5 81 char tmp[ 16 ] = "\0";
klauss 125:8ff4dc96ad58 82 int read = fread( tmp, 1, 15, fhip );
klauss 46:a670f187a704 83 if( read > 0 ){
klauss 46:a670f187a704 84 for( int i = 0; i < read; i++ )
klauss 46:a670f187a704 85 if( tmp[ i ] == '\n' || tmp[ i ] == '\r' ){
klauss 46:a670f187a704 86 tmp[ i ] = '\0';
klauss 46:a670f187a704 87 break;
klauss 46:a670f187a704 88 }
klauss 121:ee02790d00b7 89 strcpy ( this->my_ip, tmp );
klauss 121:ee02790d00b7 90 if ( debug_sip ) debug_msg ("[%d] ip %s( lidos %d chars ), valid %d", this->my_ext, this->my_ip, read, strlen( this->my_ip ) );
klauss 28:94aec56c6329 91 }else{
klauss 121:ee02790d00b7 92 if ( debug_sip ) debug_msg ("[%d] Failed to read /qspi/myip.txt", this->my_ext );
klauss 121:ee02790d00b7 93 strncpy ( this->my_ip, MY_IP, 20 );
klauss 28:94aec56c6329 94 }
klauss 28:94aec56c6329 95 }
klauss 125:8ff4dc96ad58 96 if ( fhip ) fclose ( fhip );
klauss 46:a670f187a704 97
klauss 50:d9b6577a70f5 98 this->my_ip[ 15 ] = '\0';
klauss 28:94aec56c6329 99
klauss 121:ee02790d00b7 100 if ( debug_sip ) debug_msg ("[%d] port %d", this->my_ext, this->my_port );
klauss 97:8985817e8847 101
klauss 121:ee02790d00b7 102 snprintf ( this->my_display, sizeof ( this->my_display ), "%i", this->my_ext );
klauss 112:6ae726539ab9 103
klauss 112:6ae726539ab9 104 this->my_rtp_port = 0;
klauss 121:ee02790d00b7 105 if ( debug_rtp ) debug_msg ("[%d] rtp port %d ", this->my_ext, this->my_rtp_port );
klauss 46:a670f187a704 106
klauss 125:8ff4dc96ad58 107 FILE * serext = fopen ( "/qspi/peerext.txt", "r");
klauss 125:8ff4dc96ad58 108 if ( serext == NULL )
klauss 121:ee02790d00b7 109 {
klauss 121:ee02790d00b7 110 if( debug_sip ) debug_msg ("[%d] Failed to open /qspi/peerext.txt", this->my_ext );
klauss 119:ee6a53069455 111 this->peer_ext = PEER_EXT;
klauss 29:7246460b73f8 112 } else {
klauss 125:8ff4dc96ad58 113 if( fread ( (void *)inner_tmp, 1, 32, serext ) > 0 ) {
klauss 121:ee02790d00b7 114 this->peer_ext = atoi ( inner_tmp );
klauss 121:ee02790d00b7 115 if ( debug_sip ) debug_msg("[%d] server ext %d", this->my_ext, this->peer_ext );
klauss 29:7246460b73f8 116 } else {
klauss 121:ee02790d00b7 117 if ( debug_sip ) debug_msg ("Failed to read /qspi/peerext.txt" );
klauss 119:ee6a53069455 118 this->peer_ext = PEER_EXT;
klauss 28:94aec56c6329 119 }
klauss 28:94aec56c6329 120 }
klauss 125:8ff4dc96ad58 121 if ( serext ) fclose ( serext );
klauss 119:ee6a53069455 122
klauss 0:4d17cd9c8f9d 123 strcpy( this->last_invite_tag, "" );
klauss 0:4d17cd9c8f9d 124 strcpy( this->last_invite_callid, "");
klauss 119:ee6a53069455 125 strcpy( this->last_branch, "" );
klauss 0:4d17cd9c8f9d 126 strcpy( this->SVNREV, "COLA" );
klauss 121:ee02790d00b7 127
klauss 0:4d17cd9c8f9d 128 status = sip_idle;
klauss 0:4d17cd9c8f9d 129
klauss 121:ee02790d00b7 130 __init_sock__ ();
klauss 85:b6f2dc1d0f4f 131
klauss 91:c2a86b1f8aaa 132 call = NULL;
klauss 91:c2a86b1f8aaa 133
klauss 91:c2a86b1f8aaa 134 waiting = false;
klauss 119:ee6a53069455 135
klauss 91:c2a86b1f8aaa 136 listen_SIP_server_return = 0;
klauss 91:c2a86b1f8aaa 137
klauss 119:ee6a53069455 138 last_cseq = 0;
klauss 119:ee6a53069455 139
klauss 85:b6f2dc1d0f4f 140 sip_new_counter++;
klauss 0:4d17cd9c8f9d 141 }
klauss 29:7246460b73f8 142
klauss 121:ee02790d00b7 143 Sip::~Sip ( void )
klauss 121:ee02790d00b7 144 {
klauss 121:ee02790d00b7 145 __end_sock__ ();
klauss 85:b6f2dc1d0f4f 146 sip_delete_counter++;
klauss 0:4d17cd9c8f9d 147 }
klauss 0:4d17cd9c8f9d 148
klauss 121:ee02790d00b7 149 int Sip::registry ( void )
klauss 119:ee6a53069455 150 {
klauss 121:ee02790d00b7 151 build_registry_package ();
klauss 81:3656f00ab3db 152
klauss 121:ee02790d00b7 153 int send = sock.sendTo ( sip_server, buffer, strlen ( buffer ) ); // > 400
klauss 121:ee02790d00b7 154 if ( send != strlen ( buffer ) )
klauss 119:ee6a53069455 155 {
klauss 121:ee02790d00b7 156 if ( debug_reconnect ) debug_msg ("[%d] Reconnect SIP -- Registry -- Sent %i of %i bytes", this->my_ext, send, strlen ( buffer ) );
klauss 121:ee02790d00b7 157 __reconnect__ ();
klauss 119:ee6a53069455 158 miss_sip_registry_send_pkg++;
klauss 119:ee6a53069455 159 }
klauss 114:472502b31a12 160
klauss 121:ee02790d00b7 161 if ( debug_sip ) debug_msg ("[%d %d] Registry -- Sent %i of %i bytes", this->my_ext, this->my_port, send, strlen ( buffer ) );
klauss 114:472502b31a12 162
klauss 121:ee02790d00b7 163 return ( send );
klauss 0:4d17cd9c8f9d 164 }
klauss 0:4d17cd9c8f9d 165
klauss 121:ee02790d00b7 166 VZ_call * Sip::invite ( void )
klauss 121:ee02790d00b7 167 {
klauss 121:ee02790d00b7 168 if ( status == sip_on_call ) return call;
klauss 91:c2a86b1f8aaa 169
klauss 121:ee02790d00b7 170 else if ( status == sip_idle )
klauss 121:ee02790d00b7 171 {
klauss 91:c2a86b1f8aaa 172 int cseq = 0;
klauss 91:c2a86b1f8aaa 173
klauss 91:c2a86b1f8aaa 174 call = NULL;
klauss 91:c2a86b1f8aaa 175
klauss 121:ee02790d00b7 176 build_invite_package( &cseq );
klauss 91:c2a86b1f8aaa 177
klauss 124:c1b6c893e1c3 178 if ( debug_invite ) debug_msg ("cseq::%d", cseq );
klauss 91:c2a86b1f8aaa 179
klauss 114:472502b31a12 180 if ( drop_invite_to_ast_pkg )
klauss 114:472502b31a12 181 {
klauss 119:ee6a53069455 182 debug_msg("[%d] Droped invite pkg to ast", this->my_ext );
klauss 114:472502b31a12 183 }
klauss 114:472502b31a12 184 else
klauss 104:62646ef786a3 185 {
klauss 121:ee02790d00b7 186 int send = sock.sendTo ( sip_server, buffer, strlen ( buffer ) );
klauss 114:472502b31a12 187
klauss 121:ee02790d00b7 188 if ( send != strlen ( buffer ) )
klauss 119:ee6a53069455 189 {
klauss 121:ee02790d00b7 190 if ( debug_reconnect ) debug_msg ("[%d] Reconnect SIP -- Invite", this->my_ext );
klauss 121:ee02790d00b7 191 __reconnect__ ();
klauss 119:ee6a53069455 192 miss_sip_invite_send_pkg++;
klauss 119:ee6a53069455 193 }
klauss 119:ee6a53069455 194
klauss 121:ee02790d00b7 195 if ( debug_invite || debug_reconnect ) debug_msg ("[%d] Return value for invite pkg %d", this->my_ext, send );
klauss 104:62646ef786a3 196 }
klauss 104:62646ef786a3 197
klauss 121:ee02790d00b7 198 invite_timer.stop ();
klauss 121:ee02790d00b7 199 invite_timer.reset ();
klauss 121:ee02790d00b7 200 invite_timer.start ();
klauss 91:c2a86b1f8aaa 201
klauss 121:ee02790d00b7 202 sip_set_status ( sip_waiting_trying );
klauss 91:c2a86b1f8aaa 203
klauss 91:c2a86b1f8aaa 204 waiting = false;
klauss 78:1353744f01e1 205 }
klauss 0:4d17cd9c8f9d 206
klauss 121:ee02790d00b7 207 if ( invite_timer.read() > INVITE_MAX_WAITING_TIME )
klauss 121:ee02790d00b7 208 {
klauss 121:ee02790d00b7 209 if ( debug_invite ) debug_msg ("[%d] Invite call timeout :(", this->my_ext );
klauss 121:ee02790d00b7 210 invite_timer.stop ();
klauss 121:ee02790d00b7 211 invite_timer.reset ();
klauss 119:ee6a53069455 212 sip_set_status ( sip_denied );
klauss 121:ee02790d00b7 213 send_bye ();
klauss 91:c2a86b1f8aaa 214
klauss 91:c2a86b1f8aaa 215 call = NULL;
klauss 121:ee02790d00b7 216 return ( NULL );
klauss 78:1353744f01e1 217 }
klauss 77:e8c0253b57bc 218
klauss 121:ee02790d00b7 219 if ( waiting == true ) { return call; }
klauss 91:c2a86b1f8aaa 220
klauss 121:ee02790d00b7 221 else return ( NULL );
klauss 0:4d17cd9c8f9d 222 }
klauss 0:4d17cd9c8f9d 223
klauss 121:ee02790d00b7 224 char * Sip::build_registry_package ( void )
klauss 121:ee02790d00b7 225 {
klauss 0:4d17cd9c8f9d 226 char branch[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 227 char tag[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 228 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 229 fill_random16h( branch );
klauss 0:4d17cd9c8f9d 230 fill_random( tag, 18 );
klauss 0:4d17cd9c8f9d 231 fill_random16h( callid );
klauss 119:ee6a53069455 232
klauss 121:ee02790d00b7 233 snprintf( buffer, sizeof( buffer ) - 1,
klauss 121:ee02790d00b7 234 "REGISTER sip:%s:%d SIP/2.0\r\nVia: SIP/2.0/UDP %s:%d;branch=%s\r\n"
klauss 121:ee02790d00b7 235 "From: %d <sip:%d@%s:%d>;tag=%s\r\n"
klauss 121:ee02790d00b7 236 "To: %d <sip:%d@%s:%d>\r\n"
klauss 121:ee02790d00b7 237 "Call-ID: %s\r\n"
klauss 119:ee6a53069455 238 "CSeq: %d REGISTER\r\n"
klauss 119:ee6a53069455 239 "Max-Forwards: 70\r\n"
klauss 121:ee02790d00b7 240 "Contact: %d <sip:%d@%s:%d>;expires=270\r\n"
klauss 119:ee6a53069455 241 "User-Agent: VZtech/pabxdriver-%s\r\n"
klauss 119:ee6a53069455 242 "Content-Length: 0\r\n"
klauss 121:ee02790d00b7 243 "%s\r\n\r\n",
klauss 121:ee02790d00b7 244 server_ip, server_port, my_ip, my_port, branch,
klauss 121:ee02790d00b7 245 my_ext, my_ext, server_ip, server_port, tag,
klauss 121:ee02790d00b7 246 my_ext, my_ext, server_ip, server_port,
klauss 121:ee02790d00b7 247 callid,
klauss 121:ee02790d00b7 248 get_cseq(),
klauss 121:ee02790d00b7 249 my_ext, my_ext, my_ip, server_port,
klauss 121:ee02790d00b7 250 SVNREV,
klauss 121:ee02790d00b7 251 SIP_ALLOW
klauss 119:ee6a53069455 252 );
klauss 121:ee02790d00b7 253 return ( buffer );
klauss 121:ee02790d00b7 254 }
klauss 121:ee02790d00b7 255
klauss 121:ee02790d00b7 256 char * Sip::build_invite_package ( int * cseq, const bool retry )
klauss 121:ee02790d00b7 257 {
klauss 121:ee02790d00b7 258 char header [ SIP_MAXMSGSIZE ];
klauss 121:ee02790d00b7 259 char body [ SIP_MAXMSGSIZE ];
klauss 121:ee02790d00b7 260 char branch [ SIP_MAXFIELDSIZE ];
klauss 121:ee02790d00b7 261 char tag [ SIP_MAXFIELDSIZE ];
klauss 121:ee02790d00b7 262 char callid [ SIP_MAXFIELDSIZE ];
klauss 121:ee02790d00b7 263 char callbox_string[ CALLBOX_STRING_SIZE ];
klauss 119:ee6a53069455 264
klauss 121:ee02790d00b7 265 int snprintf_ret = snprintf ( callbox_string, CALLBOX_STRING_SIZE - 1, "%i", this->my_ext );
klauss 0:4d17cd9c8f9d 266
klauss 121:ee02790d00b7 267 if ( snprintf_ret == CALLBOX_STRING_SIZE ) callbox_string [ CALLBOX_STRING_SIZE - 1 ] = '\0';
klauss 114:472502b31a12 268
klauss 119:ee6a53069455 269 if ( !retry )
klauss 114:472502b31a12 270 {
klauss 119:ee6a53069455 271 *cseq = get_cseq();
klauss 119:ee6a53069455 272 last_cseq = *cseq;
klauss 119:ee6a53069455 273
klauss 121:ee02790d00b7 274 fill_random16h ( branch );
klauss 121:ee02790d00b7 275 strcpy ( last_branch, branch );
klauss 119:ee6a53069455 276
klauss 121:ee02790d00b7 277 fill_random ( tag, 18 );
klauss 121:ee02790d00b7 278 strcpy ( last_invite_tag, tag );
klauss 119:ee6a53069455 279
klauss 121:ee02790d00b7 280 fill_random16h ( callid );
klauss 121:ee02790d00b7 281 strcpy ( last_invite_callid, callid );
klauss 119:ee6a53069455 282
klauss 119:ee6a53069455 283 this->my_rtp_port = fill_random_rtp_port ();
klauss 114:472502b31a12 284 }
klauss 114:472502b31a12 285 else
klauss 114:472502b31a12 286 {
klauss 119:ee6a53069455 287 *cseq = last_cseq;
klauss 119:ee6a53069455 288
klauss 121:ee02790d00b7 289 strcpy ( branch, last_branch );
klauss 119:ee6a53069455 290
klauss 121:ee02790d00b7 291 strcpy ( tag, last_invite_tag );
klauss 119:ee6a53069455 292
klauss 121:ee02790d00b7 293 strcpy ( callid, last_invite_callid );
klauss 114:472502b31a12 294 }
klauss 119:ee6a53069455 295
klauss 121:ee02790d00b7 296 if ( debug_invite ) debug_msg ("[%d] \r\nbranch :: %s\r\ntag :: %s\r\ncallid :: %s", this->my_ext, branch, tag, callid );
klauss 119:ee6a53069455 297
klauss 121:ee02790d00b7 298 if ( debug_rtp || debug_invite ) debug_msg ("[%d] Invite PKG rtp port ( %d )", this->my_ext, this->my_rtp_port );
klauss 119:ee6a53069455 299
klauss 119:ee6a53069455 300 snprintf( header, SIP_MAXMSGSIZE,
klauss 119:ee6a53069455 301 "INVITE sip:%i@%s:%i SIP/2.0\r\n"
klauss 119:ee6a53069455 302 "Via: SIP/2.0/UDP %s:%i;branch=%s\r\n"
klauss 119:ee6a53069455 303 "From: %s <sip:%i@%s:%i>;tag=%s\r\n"
klauss 119:ee6a53069455 304 "To: <sip:%i@%s:%i>\r\n"
klauss 119:ee6a53069455 305 "Call-ID: %s@%s\r\n"
klauss 119:ee6a53069455 306 "CSeq: %i INVITE\r\n"
klauss 119:ee6a53069455 307 "Contact: %i <sip:%i@%s:%i>\r\n"
klauss 119:ee6a53069455 308 "Max-Forwards: 20\r\n"
klauss 119:ee6a53069455 309 "User-Agent: VZtech/pabxdriver-%s\r\n"
klauss 119:ee6a53069455 310 "Expires: 71\r\n"
klauss 119:ee6a53069455 311 "%s\r\n"
klauss 119:ee6a53069455 312 "Content-Type: application/sdp\r\n",
klauss 119:ee6a53069455 313 this->peer_ext, this->server_ip, this->server_port,
klauss 119:ee6a53069455 314 this->my_ip, this->my_port, branch,
klauss 119:ee6a53069455 315 callbox_string, this->my_ext, this->server_ip, this->server_port, tag,
klauss 119:ee6a53069455 316 this->peer_ext, this->server_ip, this->server_port,
klauss 119:ee6a53069455 317 callid, this->my_ip,
klauss 119:ee6a53069455 318 *cseq,
klauss 119:ee6a53069455 319 this->my_ext, this->my_ext, this->my_ip, this->my_port,
klauss 119:ee6a53069455 320 SVNREV,
klauss 119:ee6a53069455 321 SIP_ALLOW
klauss 119:ee6a53069455 322 );
klauss 119:ee6a53069455 323
klauss 119:ee6a53069455 324 snprintf( body, SIP_MAXMSGSIZE,
klauss 119:ee6a53069455 325 "v=0\r\no=- 7377 18176 IN IP4 %s\r\n"
klauss 119:ee6a53069455 326 "s=-\r\n"
klauss 119:ee6a53069455 327 "c=IN IP4 %s\r\n"
klauss 119:ee6a53069455 328 "t=0 0\r\n"
klauss 119:ee6a53069455 329 "i=UDP %i\r\n"
klauss 119:ee6a53069455 330 "m=audio %i RTP/AVP 8 101\r\n"
klauss 119:ee6a53069455 331 "a=rtpmap:8 PCMA/8000/1\r\n"
klauss 119:ee6a53069455 332 "a=rtpmap:101 telephone-event/8000\r\n"
klauss 119:ee6a53069455 333 "a=fmtp: 101 0-11",
klauss 119:ee6a53069455 334 this->my_ip,
klauss 119:ee6a53069455 335 this->my_ip,
klauss 119:ee6a53069455 336 this->my_rtp_port,
klauss 119:ee6a53069455 337 this->my_rtp_port
klauss 119:ee6a53069455 338 );
klauss 119:ee6a53069455 339
klauss 121:ee02790d00b7 340 strcpy( this->buffer, header );
klauss 121:ee02790d00b7 341
klauss 121:ee02790d00b7 342 char content_Length_msg [ CONTENT_LENGTH_MSG_SIZE ];
klauss 119:ee6a53069455 343
klauss 121:ee02790d00b7 344 snprintf_ret = snprintf ( content_Length_msg, CONTENT_LENGTH_MSG_SIZE, "Content-Length: %d\r\n\r\n", strlen ( body ) );
klauss 121:ee02790d00b7 345 if ( snprintf_ret == CONTENT_LENGTH_MSG_SIZE ) content_Length_msg [ CONTENT_LENGTH_MSG_SIZE - 1 ] = '\0';
klauss 121:ee02790d00b7 346 strcat( this->buffer, content_Length_msg );
klauss 121:ee02790d00b7 347
klauss 121:ee02790d00b7 348 strcat( this->buffer, body );
klauss 121:ee02790d00b7 349
klauss 121:ee02790d00b7 350 if ( debug_invite ) debug_msg ("[%d] strlen( header [%d] ) | strlen( body [%d] ) | ( h + b [%d])", this->my_ext, strlen( header ),strlen( body ), strlen( header ) + strlen( body ) );
klauss 121:ee02790d00b7 351
klauss 121:ee02790d00b7 352 return( this->buffer );
klauss 0:4d17cd9c8f9d 353 }
klauss 0:4d17cd9c8f9d 354
klauss 121:ee02790d00b7 355 char * Sip::build_bye_package ( void )
klauss 121:ee02790d00b7 356 {
klauss 0:4d17cd9c8f9d 357 char branch[SIP_MAXFIELDSIZE];
klauss 0:4d17cd9c8f9d 358 char tag[SIP_MAXFIELDSIZE];
klauss 0:4d17cd9c8f9d 359 fill_random16h(branch);
klauss 0:4d17cd9c8f9d 360 fill_random(tag,18);
klauss 0:4d17cd9c8f9d 361
klauss 121:ee02790d00b7 362 snprintf(
klauss 121:ee02790d00b7 363 this->buffer, sizeof ( this->buffer ) - 1,
klauss 121:ee02790d00b7 364 "BYE sip:%i@%s:%i SIP/2.0\r\n"
klauss 121:ee02790d00b7 365 "Via: SIP/2.0/UDP %s:%i;branch=%s\r\n"
klauss 121:ee02790d00b7 366 "From: %i <sip:%i@%s:%i>;tag=%s\r\n"
klauss 121:ee02790d00b7 367 "To: <sip:%i@%s:%i>;tag=%s\r\n"
klauss 121:ee02790d00b7 368 "Call-ID: %s@%s\r\n"
klauss 121:ee02790d00b7 369 "CSeq: %i BYE\r\n"
klauss 121:ee02790d00b7 370 "Max-Forwards: 70\r\n"
klauss 121:ee02790d00b7 371 "User-Agent: VZtech/pabxdriver-%s\r\n"
klauss 121:ee02790d00b7 372 "Content-Length: 0\r\n\r\n",
klauss 121:ee02790d00b7 373 this->peer_ext, this->server_ip, this->server_port,
klauss 121:ee02790d00b7 374 this->my_ip, this->my_port, branch,
klauss 121:ee02790d00b7 375 this->my_ext, this->my_ext, this->server_ip, this->server_port, last_invite_tag,
klauss 121:ee02790d00b7 376 this->peer_ext, this->server_ip, this->server_port, tag,
klauss 121:ee02790d00b7 377 last_invite_callid, this->my_ip,
klauss 121:ee02790d00b7 378 get_cseq(),
klauss 121:ee02790d00b7 379 SVNREV
klauss 121:ee02790d00b7 380 );
klauss 121:ee02790d00b7 381
klauss 0:4d17cd9c8f9d 382 return( buffer );
klauss 0:4d17cd9c8f9d 383 }
klauss 121:ee02790d00b7 384 char * Sip::build_ack_package ( char * buffer, const unsigned char * orig )
klauss 121:ee02790d00b7 385 {
klauss 0:4d17cd9c8f9d 386 char to[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 387 char from[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 388 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 389 char via[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 390 char branch[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 391 char cseq[ SIP_MAXFIELDSIZE ];
klauss 121:ee02790d00b7 392
klauss 0:4d17cd9c8f9d 393 if( decode_gettag( orig, "to: ", to ) == 0) {
klauss 0:4d17cd9c8f9d 394 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 395 return NULL;
klauss 0:4d17cd9c8f9d 396 }
klauss 0:4d17cd9c8f9d 397 if( decode_gettag( orig, "from: ", from ) == 0) {
klauss 0:4d17cd9c8f9d 398 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 399 return NULL;
klauss 0:4d17cd9c8f9d 400 }
klauss 0:4d17cd9c8f9d 401 if( decode_gettag( orig, "call-id: ", callid ) == 0) {
klauss 0:4d17cd9c8f9d 402 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 403 return NULL;
klauss 0:4d17cd9c8f9d 404 }
klauss 0:4d17cd9c8f9d 405 decode_branch( orig, branch );
klauss 0:4d17cd9c8f9d 406 decode_cseq( orig, cseq );
klauss 121:ee02790d00b7 407
klauss 0:4d17cd9c8f9d 408 if( strlen( via ) > 6 ) {
klauss 0:4d17cd9c8f9d 409 if( strcasecmp( ";rport", via+strlen( via )-6) == 0) {
klauss 0:4d17cd9c8f9d 410 via[strlen(via)-6] = 0;
klauss 0:4d17cd9c8f9d 411 }
klauss 0:4d17cd9c8f9d 412 }
klauss 121:ee02790d00b7 413
klauss 121:ee02790d00b7 414
klauss 121:ee02790d00b7 415 snprintf ( buffer, SIP_MAXMSGSIZE -1,
klauss 121:ee02790d00b7 416 "ACK sip:%i@%s:%i SIP/2.0\r\n"
klauss 121:ee02790d00b7 417 "Via: SIP/2.0/UDP %s:%i;branch=%s\r\n"
klauss 121:ee02790d00b7 418 "From: %s\r\n"
klauss 121:ee02790d00b7 419 "To: %s\r\n"
klauss 121:ee02790d00b7 420 "Call-ID: %s\r\n"
klauss 121:ee02790d00b7 421 "CSeq: %s ACK\r\n"
klauss 121:ee02790d00b7 422 "Max-Forwards: 70\r\n"
klauss 121:ee02790d00b7 423 "Contact: %i <sip:%i@%s:%i>\r\n"
klauss 121:ee02790d00b7 424 "User-Agent: VZtech/pabxdriver-%s\r\n"
klauss 121:ee02790d00b7 425 "Content-Length: 0\r\n\r\n",
klauss 121:ee02790d00b7 426 this->peer_ext, this->server_ip, this->server_port,
klauss 121:ee02790d00b7 427 this->my_ip, this->my_port, branch,
klauss 121:ee02790d00b7 428 from,
klauss 121:ee02790d00b7 429 to,
klauss 121:ee02790d00b7 430 callid,
klauss 121:ee02790d00b7 431 cseq,
klauss 121:ee02790d00b7 432 this->my_ext, this->my_ext, this->my_ip, this->my_port,
klauss 121:ee02790d00b7 433 SVNREV
klauss 121:ee02790d00b7 434 );
klauss 121:ee02790d00b7 435
klauss 0:4d17cd9c8f9d 436 return( buffer );
klauss 0:4d17cd9c8f9d 437 }
klauss 121:ee02790d00b7 438
klauss 121:ee02790d00b7 439 char * Sip::build_reply_package ( char * buffer, const unsigned char * orig )
klauss 121:ee02790d00b7 440 {
klauss 0:4d17cd9c8f9d 441 char to[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 442 char from[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 443 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 444 char cseq[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 445 char via[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 446 char tag[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 447
klauss 0:4d17cd9c8f9d 448 if( decode_gettag( orig, "to: ", to ) == 0) {
klauss 0:4d17cd9c8f9d 449 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 450 return NULL;
klauss 0:4d17cd9c8f9d 451 }
klauss 0:4d17cd9c8f9d 452 if( decode_gettag( orig, "from: ", from ) == 0) {
klauss 0:4d17cd9c8f9d 453 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 454 return NULL;
klauss 0:4d17cd9c8f9d 455 }
klauss 0:4d17cd9c8f9d 456 if( decode_gettag( orig, "call-id: ", callid ) == 0 ) {
klauss 0:4d17cd9c8f9d 457 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 458 return NULL;
klauss 0:4d17cd9c8f9d 459 }
klauss 0:4d17cd9c8f9d 460 if( decode_gettag( orig, "cseq: ", cseq) == 0 ) {
klauss 0:4d17cd9c8f9d 461 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 462 return NULL;
klauss 0:4d17cd9c8f9d 463 }
klauss 0:4d17cd9c8f9d 464 if( decode_gettag( orig, "via: ", via) == 0 ) {
klauss 0:4d17cd9c8f9d 465 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 466 return NULL;
klauss 0:4d17cd9c8f9d 467 }
klauss 0:4d17cd9c8f9d 468
klauss 0:4d17cd9c8f9d 469 if (strlen(via) > 6) {
klauss 0:4d17cd9c8f9d 470 if (strcasecmp(";rport",via+strlen(via)-6)==0) {
klauss 0:4d17cd9c8f9d 471 via[strlen(via)-6] = 0;
klauss 0:4d17cd9c8f9d 472 }
klauss 0:4d17cd9c8f9d 473 }
klauss 0:4d17cd9c8f9d 474
klauss 121:ee02790d00b7 475 fill_random ( tag, 18 );
klauss 0:4d17cd9c8f9d 476
klauss 121:ee02790d00b7 477 snprintf(
klauss 121:ee02790d00b7 478 buffer, SIP_MAXMSGSIZE -1,
klauss 121:ee02790d00b7 479 "SIP/2.0 200 OK\r\n"
klauss 121:ee02790d00b7 480 "To: %s;tag=%s\r\n"
klauss 121:ee02790d00b7 481 "From: %s\r\n"
klauss 121:ee02790d00b7 482 "Call-ID: %s\r\n"
klauss 121:ee02790d00b7 483 "CSeq: %s\r\n"
klauss 121:ee02790d00b7 484 "Via: %s\r\n"
klauss 121:ee02790d00b7 485 "Server: VZtech/pabxdriver-%s\r\n"
klauss 121:ee02790d00b7 486 "Content-Length: 0\r\n"
klauss 121:ee02790d00b7 487 "%s\r\n\r\n",
klauss 121:ee02790d00b7 488 to, tag,
klauss 121:ee02790d00b7 489 from,
klauss 121:ee02790d00b7 490 callid,
klauss 121:ee02790d00b7 491 cseq,
klauss 121:ee02790d00b7 492 via,
klauss 121:ee02790d00b7 493 SVNREV,
klauss 121:ee02790d00b7 494 SIP_ALLOW
klauss 121:ee02790d00b7 495 );
klauss 0:4d17cd9c8f9d 496
klauss 0:4d17cd9c8f9d 497 return( buffer );
klauss 0:4d17cd9c8f9d 498 }
klauss 121:ee02790d00b7 499
klauss 121:ee02790d00b7 500 char * Sip::fill_random16h ( char * buffer )
klauss 121:ee02790d00b7 501 {
klauss 0:4d17cd9c8f9d 502 fill_random( buffer, 16 );
klauss 0:4d17cd9c8f9d 503 buffer[ 7 ] = '-';
klauss 0:4d17cd9c8f9d 504 return( buffer );
klauss 0:4d17cd9c8f9d 505 }
klauss 0:4d17cd9c8f9d 506
klauss 121:ee02790d00b7 507 char * Sip::fill_random ( char * buffer, const int size )
klauss 121:ee02790d00b7 508 {
klauss 0:4d17cd9c8f9d 509 static uint16_t seed = time( NULL );
klauss 0:4d17cd9c8f9d 510 seed += 1;
klauss 0:4d17cd9c8f9d 511 srand( seed );
klauss 0:4d17cd9c8f9d 512
klauss 121:ee02790d00b7 513 for( int i = 0; i < size - 1; i++ )
klauss 121:ee02790d00b7 514 {
klauss 0:4d17cd9c8f9d 515 buffer[ i ] = fill_random_aux[ rand() & 0x3f ];
klauss 0:4d17cd9c8f9d 516 }
klauss 100:09a23fcd3bdf 517
klauss 100:09a23fcd3bdf 518 buffer[ size - 1 ] = 0;
klauss 0:4d17cd9c8f9d 519 return( buffer );
klauss 0:4d17cd9c8f9d 520 }
klauss 0:4d17cd9c8f9d 521
klauss 121:ee02790d00b7 522 int Sip::get_cseq ( void )
klauss 121:ee02790d00b7 523 {
klauss 0:4d17cd9c8f9d 524 static unsigned int cseq = 0;
klauss 121:ee02790d00b7 525 if( cseq == 0 )
klauss 121:ee02790d00b7 526 {
klauss 0:4d17cd9c8f9d 527 /* initial value, random number */
klauss 121:ee02790d00b7 528 cseq = rand ();
klauss 0:4d17cd9c8f9d 529 cseq &= 0x7fff;
klauss 0:4d17cd9c8f9d 530 }
klauss 0:4d17cd9c8f9d 531 cseq++;
klauss 0:4d17cd9c8f9d 532 cseq &= 0x7fff;
klauss 121:ee02790d00b7 533 if ( cseq == 0 ) cseq++;
klauss 0:4d17cd9c8f9d 534 return cseq;
klauss 0:4d17cd9c8f9d 535 }
klauss 0:4d17cd9c8f9d 536
klauss 121:ee02790d00b7 537 char * Sip::decode_cseq ( const unsigned char * package, char * cseq )
klauss 121:ee02790d00b7 538 {
klauss 121:ee02790d00b7 539 char pkg[ SIP_MAXMSGSIZE ], cs[ SIP_MAXMSGSIZE ];
klauss 121:ee02790d00b7 540
klauss 121:ee02790d00b7 541 if( decode_gettag ( package, "cseq: ", pkg ) != 0)
klauss 121:ee02790d00b7 542 {
klauss 0:4d17cd9c8f9d 543 int i = 0;
klauss 121:ee02790d00b7 544 strcpy ( cs, pkg );
klauss 121:ee02790d00b7 545 while ( ( cs [ i ] != 0 ) && ( cs [ i ] != ' ' ) ) i++;
klauss 121:ee02790d00b7 546 cs [ i ] = 0;
klauss 121:ee02790d00b7 547 strcpy ( cseq, cs );
klauss 0:4d17cd9c8f9d 548 } else {
klauss 121:ee02790d00b7 549 cseq [ 0 ] = 0;
klauss 0:4d17cd9c8f9d 550 }
klauss 121:ee02790d00b7 551 return ( cseq );
klauss 0:4d17cd9c8f9d 552 }
klauss 0:4d17cd9c8f9d 553
klauss 121:ee02790d00b7 554 char * Sip::decode_branch ( const unsigned char * package, char * branch )
klauss 121:ee02790d00b7 555 {
klauss 121:ee02790d00b7 556 char pkg[ SIP_MAXMSGSIZE ];
klauss 121:ee02790d00b7 557
klauss 121:ee02790d00b7 558 if ( decode_gettag ( package, "branch=", pkg ) != 0 )
klauss 121:ee02790d00b7 559 {
klauss 0:4d17cd9c8f9d 560 int i = 0;
klauss 121:ee02790d00b7 561 strcpy ( branch, pkg );
klauss 121:ee02790d00b7 562 while ( ( branch [ i ] != 0 ) && ( branch [ i ] != ';' ) ) i++;
klauss 121:ee02790d00b7 563 branch [ i ] = 0;
klauss 0:4d17cd9c8f9d 564 } else {
klauss 121:ee02790d00b7 565 branch [ 0 ] = 0;
klauss 0:4d17cd9c8f9d 566 }
klauss 121:ee02790d00b7 567
klauss 121:ee02790d00b7 568 return ( branch );
klauss 0:4d17cd9c8f9d 569 }
klauss 121:ee02790d00b7 570
klauss 121:ee02790d00b7 571 int Sip::decode_gettag ( const unsigned char * package, const char * tag, char * out )
klauss 121:ee02790d00b7 572 {
klauss 121:ee02790d00b7 573 int cmpsize = strlen ( tag );
klauss 121:ee02790d00b7 574 int size = strlen ( ( char * ) package ) - cmpsize;
klauss 121:ee02790d00b7 575
klauss 121:ee02790d00b7 576 for ( int i = 0; i < size; i++ )
klauss 121:ee02790d00b7 577 {
klauss 121:ee02790d00b7 578 if ( strncasecmp( tag, ( char * ) package + i, cmpsize ) == 0 )
klauss 121:ee02790d00b7 579 {
klauss 0:4d17cd9c8f9d 580 char * s;
klauss 121:ee02790d00b7 581 s = ( char * ) package + i + cmpsize;
klauss 0:4d17cd9c8f9d 582 register int j = 0;
klauss 121:ee02790d00b7 583 while ( ( s [ j ] != '\r' ) && ( s [ j ] != '\n' ) ) j++;
klauss 121:ee02790d00b7 584 strncpy ( out, s, j ); // copy string to output
klauss 121:ee02790d00b7 585 out [ j ] = 0; // terminate string
klauss 0:4d17cd9c8f9d 586 return 1; // found
klauss 0:4d17cd9c8f9d 587 }
klauss 0:4d17cd9c8f9d 588 }
klauss 121:ee02790d00b7 589
klauss 0:4d17cd9c8f9d 590 return 0; // not found
klauss 0:4d17cd9c8f9d 591 }
klauss 121:ee02790d00b7 592
klauss 121:ee02790d00b7 593 int Sip::get_my_rtp_port ( void ) { return this->my_rtp_port; }
klauss 121:ee02790d00b7 594
klauss 121:ee02790d00b7 595 int Sip::fill_random_rtp_port ( void )
klauss 114:472502b31a12 596 {
klauss 112:6ae726539ab9 597 static uint16_t port = 0;
klauss 112:6ae726539ab9 598 port += 2;
klauss 112:6ae726539ab9 599 port &= 0x7fe; // 0 to 2046, always even
klauss 112:6ae726539ab9 600 return port + 16384;
klauss 0:4d17cd9c8f9d 601 }
klauss 119:ee6a53069455 602
klauss 121:ee02790d00b7 603 void Sip::set_server_port ( const int new_server_port ) { this->server_port = new_server_port; }
klauss 121:ee02790d00b7 604
klauss 121:ee02790d00b7 605 void Sip::set_server_ext ( const int new_server_ext ) { this->peer_ext = new_server_ext; }
klauss 121:ee02790d00b7 606
klauss 121:ee02790d00b7 607 void Sip::set_server_ip ( const char * new_server_ip )
klauss 121:ee02790d00b7 608 {
klauss 121:ee02790d00b7 609 strncpy( this->server_ip, new_server_ip, 20 );
klauss 0:4d17cd9c8f9d 610 this->server_ip[19] = 0;
klauss 0:4d17cd9c8f9d 611 }
klauss 0:4d17cd9c8f9d 612
klauss 0:4d17cd9c8f9d 613 /* Retorna
klauss 0:4d17cd9c8f9d 614 = 0 :: ok
klauss 0:4d17cd9c8f9d 615 < 0 :: tive problemas
klauss 0:4d17cd9c8f9d 616 > 0 :: devo remover essa call do vetor de calls
klauss 0:4d17cd9c8f9d 617 */
klauss 121:ee02790d00b7 618 int Sip::listen_SIP_server ( void )
klauss 121:ee02790d00b7 619 {
klauss 91:c2a86b1f8aaa 620 int ret = listen_SIP_server_return;
klauss 91:c2a86b1f8aaa 621 listen_SIP_server_return = 0;
klauss 91:c2a86b1f8aaa 622 return ret;
klauss 0:4d17cd9c8f9d 623 }
klauss 0:4d17cd9c8f9d 624
klauss 121:ee02790d00b7 625 void Sip::set_ext ( const int ext ) { my_ext = ext; }
klauss 0:4d17cd9c8f9d 626
klauss 121:ee02790d00b7 627 void Sip::set_port ( const int port ) { my_port = port; }
klauss 0:4d17cd9c8f9d 628
klauss 119:ee6a53069455 629 void Sip::send_bye ( void )
klauss 119:ee6a53069455 630 {
klauss 121:ee02790d00b7 631 int send = sock.sendTo( sip_server, build_bye_package (), strlen( this->buffer ) );
klauss 114:472502b31a12 632
klauss 121:ee02790d00b7 633 if( debug_sip ) debug_msg( "[%d] sizeof( bye pkg ) :: %d", this->my_ext, strlen( this->buffer ) );
klauss 81:3656f00ab3db 634
klauss 119:ee6a53069455 635 if( send != strlen( this->buffer ) )
klauss 119:ee6a53069455 636 {
klauss 119:ee6a53069455 637 if( debug_reconnect ) debug_msg("[%d] Reconnect SIP -- BYE, %d, %d", this->my_ext, send, strlen( this->buffer ) );
klauss 119:ee6a53069455 638 __reconnect__();
klauss 119:ee6a53069455 639 miss_sip_bye_send_pkg++;
klauss 113:db67ae00550e 640 }
klauss 0:4d17cd9c8f9d 641 }
klauss 0:4d17cd9c8f9d 642
klauss 121:ee02790d00b7 643 int Sip::get_status ( void ) { return this->status; }
klauss 121:ee02790d00b7 644
klauss 121:ee02790d00b7 645 void Sip::sip_set_status ( const uint8_t status ) { this->status = status; }
klauss 78:1353744f01e1 646
klauss 121:ee02790d00b7 647 int Sip::get_socket_fd ( void ) { return sock.get_fd (); }
klauss 81:3656f00ab3db 648
klauss 121:ee02790d00b7 649 int Sip::udp_incomming_pkg ( void )
klauss 121:ee02790d00b7 650 {
klauss 91:c2a86b1f8aaa 651 Endpoint from;
klauss 91:c2a86b1f8aaa 652
klauss 121:ee02790d00b7 653 int length = sock.receiveFrom ( from, buffer, sizeof( buffer ) );
klauss 91:c2a86b1f8aaa 654
klauss 121:ee02790d00b7 655 if ( memcmp( ( ( u8_t * )( &(from._remoteHost) ) ) + 2, ( ( u8_t * )( &(sip_server._remoteHost) ) ) + 2, 6 ) != 0 ) return 0;
klauss 91:c2a86b1f8aaa 656
klauss 121:ee02790d00b7 657 if ( length == -1 )
klauss 119:ee6a53069455 658 {
klauss 121:ee02790d00b7 659 if ( debug_reconnect ) debug_msg ("[%d] Reconnect SIP -- UDP Incomming", get_ext () );
klauss 119:ee6a53069455 660 __reconnect__();
klauss 119:ee6a53069455 661 miss_sip_inc_pkg++;
klauss 119:ee6a53069455 662 }
klauss 91:c2a86b1f8aaa 663
klauss 121:ee02790d00b7 664 if ( length > 0 )
klauss 121:ee02790d00b7 665 {
klauss 121:ee02790d00b7 666 if ( sizeof( buffer ) > length ) buffer[ length ] = 0;
klauss 91:c2a86b1f8aaa 667
klauss 91:c2a86b1f8aaa 668 int sip_response = -1;
klauss 91:c2a86b1f8aaa 669
klauss 91:c2a86b1f8aaa 670 // faster than atoi() (:
klauss 121:ee02790d00b7 671 if ( buffer[ 7 ] == ' ' || buffer[ 11 ] == ' ' ){
klauss 91:c2a86b1f8aaa 672 sip_response = ( buffer[ 8 ] - '0' ) * 100 + ( buffer[ 9 ] - '0' ) * 10 + ( buffer[ 10 ] - '0' );
klauss 91:c2a86b1f8aaa 673 }
klauss 91:c2a86b1f8aaa 674
klauss 121:ee02790d00b7 675 if ( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) || ( status == sip_on_call ) )
klauss 119:ee6a53069455 676 {
klauss 121:ee02790d00b7 677 if ( sip_response == 200 ){ // Ok
klauss 91:c2a86b1f8aaa 678 char *ref = strstr( buffer, "audio" );
klauss 121:ee02790d00b7 679 if ( debug_invite ) debug_msg("[%d] udp_incomming_pkg -- ok", this->my_ext );
klauss 121:ee02790d00b7 680 if ( ref != NULL ){
klauss 121:ee02790d00b7 681 if ( drop_this_amount_of_ack_to_ast ) drop_this_amount_of_ack_to_ast--;
klauss 119:ee6a53069455 682
klauss 121:ee02790d00b7 683 if ( drop_this_amount_of_ack_to_ast )
klauss 114:472502b31a12 684 {
klauss 121:ee02790d00b7 685 debug_msg ("[%d] Droped ok pkg received from ast (%d)", this->my_ext, drop_this_amount_of_ack_to_ast );
klauss 114:472502b31a12 686 return -71;
klauss 114:472502b31a12 687 }
klauss 114:472502b31a12 688
klauss 91:c2a86b1f8aaa 689 ref += 6; // audio
klauss 91:c2a86b1f8aaa 690 ref = strtok( ref, " ");
klauss 121:ee02790d00b7 691 if ( call == NULL ){
klauss 121:ee02790d00b7 692 call = new VZ_call ( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
klauss 121:ee02790d00b7 693 if ( debug_rtp ) debug_msg ("[%d] Ok new call ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref );
klauss 121:ee02790d00b7 694 if ( call == NULL ) memory_is_over = true;
klauss 91:c2a86b1f8aaa 695 }
klauss 121:ee02790d00b7 696 char ans[ SIP_MAXMSGSIZE ];
klauss 91:c2a86b1f8aaa 697 build_ack_package( ans, ( unsigned char *)buffer );
klauss 91:c2a86b1f8aaa 698
klauss 119:ee6a53069455 699 int send = 0;
klauss 104:62646ef786a3 700
klauss 114:472502b31a12 701 if ( drop_ack_pkg )
klauss 104:62646ef786a3 702 {
klauss 119:ee6a53069455 703 debug_msg("[%d] Droped ack pkt to ast", this->my_ext );
klauss 114:472502b31a12 704 }
klauss 114:472502b31a12 705 else
klauss 114:472502b31a12 706 {
klauss 119:ee6a53069455 707 send = sock.sendTo( sip_server, ans, strlen( ans ) );
klauss 119:ee6a53069455 708
klauss 121:ee02790d00b7 709 if ( send != strlen( ans ) )
klauss 119:ee6a53069455 710 {
klauss 121:ee02790d00b7 711 if ( debug_reconnect ) debug_msg ("[%d] Reconnect SIP -- Ok -- Call mode on", this->my_ext );
klauss 119:ee6a53069455 712 __reconnect__();
klauss 119:ee6a53069455 713 miss_sip_ok_send_pkg++;
klauss 119:ee6a53069455 714 }
klauss 104:62646ef786a3 715 }
klauss 91:c2a86b1f8aaa 716
klauss 121:ee02790d00b7 717 if ( debug_invite ) debug_msg ("[%d] Call alocada -- sizeof( ack ) :: %d - strlen( ans ):: %d", this->my_ext, send, strlen( ans ) );
klauss 119:ee6a53069455 718 sip_set_status ( sip_on_call );
klauss 91:c2a86b1f8aaa 719 }
klauss 91:c2a86b1f8aaa 720 }
klauss 91:c2a86b1f8aaa 721 }
klauss 121:ee02790d00b7 722 if ( ( status == sip_trying ) || ( status == sip_ringing ) )
klauss 121:ee02790d00b7 723 {
klauss 121:ee02790d00b7 724 if ( sip_response >= 400 && sip_response < 700 )
klauss 121:ee02790d00b7 725 {
klauss 121:ee02790d00b7 726 if ( debug_invite ) {
klauss 91:c2a86b1f8aaa 727 buffer[ 11 ] = 0;
klauss 121:ee02790d00b7 728 if ( debug_invite ) debug_msg ("[%d] Busy Here :: %s", this->my_ext, buffer + 8 );
klauss 91:c2a86b1f8aaa 729 }
klauss 91:c2a86b1f8aaa 730 send_bye();
klauss 119:ee6a53069455 731 sip_set_status ( sip_busy );
klauss 121:ee02790d00b7 732 return ( NULL );
klauss 121:ee02790d00b7 733 }
klauss 121:ee02790d00b7 734 else if ( sip_response == 183 ) // Session in Progress
klauss 121:ee02790d00b7 735 {
klauss 121:ee02790d00b7 736 char *ref = strstr ( buffer, "audio" );
klauss 121:ee02790d00b7 737 if ( ref != NULL )
klauss 121:ee02790d00b7 738 {
klauss 91:c2a86b1f8aaa 739 ref += 6; // strlen( "audio" ) == 6
klauss 91:c2a86b1f8aaa 740 ref = strtok( ref, " ");
klauss 121:ee02790d00b7 741 if ( call == NULL )
klauss 121:ee02790d00b7 742 {
klauss 121:ee02790d00b7 743 this->call = new VZ_call ( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
klauss 121:ee02790d00b7 744 if ( debug_rtp ) debug_msg ("[%d] Session in Progress ( %d, %d, %d, %d ( %s ) ) ",this->my_ext, this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref );
klauss 121:ee02790d00b7 745 if ( call == NULL ) memory_is_over = true;
klauss 91:c2a86b1f8aaa 746 }
klauss 91:c2a86b1f8aaa 747 waiting = true;
klauss 91:c2a86b1f8aaa 748 }
klauss 91:c2a86b1f8aaa 749 }
klauss 91:c2a86b1f8aaa 750 }
klauss 121:ee02790d00b7 751
klauss 121:ee02790d00b7 752 if ( status == sip_trying ){
klauss 121:ee02790d00b7 753 if ( sip_response == 180 ){
klauss 121:ee02790d00b7 754 if ( debug_invite ) debug_msg ("[%d] ringing", this->my_ext );
klauss 119:ee6a53069455 755 sip_set_status ( sip_ringing );
klauss 91:c2a86b1f8aaa 756 }
klauss 91:c2a86b1f8aaa 757 }
klauss 121:ee02790d00b7 758
klauss 121:ee02790d00b7 759 if ( status == sip_waiting_trying ){
klauss 121:ee02790d00b7 760 if ( sip_response == 100 ){
klauss 121:ee02790d00b7 761 if ( debug_invite ) debug_msg ("[%d] trying", this->my_ext )
klauss 119:ee6a53069455 762 sip_set_status ( sip_trying );
klauss 91:c2a86b1f8aaa 763 }
klauss 91:c2a86b1f8aaa 764 }
klauss 121:ee02790d00b7 765
klauss 121:ee02790d00b7 766 if ( status == sip_on_call )
klauss 121:ee02790d00b7 767 {
klauss 121:ee02790d00b7 768 if ( !( strncasecmp( buffer, "bye ", 4 ) ) )
klauss 121:ee02790d00b7 769 {
klauss 91:c2a86b1f8aaa 770 char ans[ 2000 ];
klauss 121:ee02790d00b7 771 build_reply_package ( ans, ( unsigned char * ) buffer );
klauss 91:c2a86b1f8aaa 772
klauss 121:ee02790d00b7 773 int send = sock.sendTo ( sip_server, ans, strlen ( ans ) );
klauss 121:ee02790d00b7 774 if ( send != strlen( ans ) )
klauss 119:ee6a53069455 775 {
klauss 121:ee02790d00b7 776 if ( debug_reconnect ) debug_msg ("[%d] Reconnect SIP -- RCV BYE from * -- ( expected, realized ) ( %d, %d )", this->my_ext, strlen ( ans ), send );
klauss 121:ee02790d00b7 777 __reconnect__ ();
klauss 119:ee6a53069455 778 miss_sip_rcv_bye_send_pkg++;
klauss 119:ee6a53069455 779 }
klauss 119:ee6a53069455 780
klauss 121:ee02790d00b7 781 if ( debug_invite ) debug_msg ("[%d] Bye request received from * - sizeof( bye reply pkg ) :: %d", this->my_ext, strlen ( ans ) );
klauss 119:ee6a53069455 782
klauss 119:ee6a53069455 783
klauss 121:ee02790d00b7 784 set_sip_rtp_port ( 0 );
klauss 108:18a3702650f3 785
klauss 91:c2a86b1f8aaa 786 listen_SIP_server_return = my_ext;
klauss 91:c2a86b1f8aaa 787 }
klauss 91:c2a86b1f8aaa 788 }
klauss 91:c2a86b1f8aaa 789 }
klauss 114:472502b31a12 790
klauss 91:c2a86b1f8aaa 791 return length;
klauss 91:c2a86b1f8aaa 792 }
klauss 91:c2a86b1f8aaa 793
klauss 121:ee02790d00b7 794 void Sip::reset_call ( void ) { if( call != NULL ) call = NULL; }
klauss 97:8985817e8847 795
klauss 121:ee02790d00b7 796 int Sip::get_ext ( void ) { return my_ext; }
klauss 97:8985817e8847 797
klauss 121:ee02790d00b7 798 int Sip::get_port ( void ) { return my_port; }
klauss 108:18a3702650f3 799
klauss 121:ee02790d00b7 800 int Sip::get_sip_rtp_port ( void ) { return my_rtp_port; }
klauss 108:18a3702650f3 801
klauss 121:ee02790d00b7 802 void Sip::set_sip_rtp_port ( const int new_my_rtp_port )
klauss 109:a5b8264ffbbc 803 {
klauss 109:a5b8264ffbbc 804 this->my_rtp_port = new_my_rtp_port;
klauss 121:ee02790d00b7 805 if ( debug_rtp ) debug_msg ( "[%d] this->my_rtp_port ( %d ) = new_my_rtp_port( %d )", this->my_ext, this->my_rtp_port, new_my_rtp_port );
klauss 109:a5b8264ffbbc 806 }
klauss 109:a5b8264ffbbc 807
klauss 109:a5b8264ffbbc 808 int Sip::print_yourself ( void )
klauss 108:18a3702650f3 809 {
klauss 109:a5b8264ffbbc 810 send_msg ("");
klauss 109:a5b8264ffbbc 811 send_msg ("Values ::\r\n");
klauss 121:ee02790d00b7 812 send_msg ("server_ip :: %s", server_ip );
klauss 121:ee02790d00b7 813 send_msg ("server_port :: %d", server_port );
klauss 121:ee02790d00b7 814 send_msg ("my_ip :: %s", my_ip );
klauss 121:ee02790d00b7 815 send_msg ("my_port :: %d", my_port );
klauss 121:ee02790d00b7 816 send_msg ("my_ext :: %d", my_ext );
klauss 121:ee02790d00b7 817 send_msg ("my_rtp_port :: %d", my_rtp_port );
klauss 121:ee02790d00b7 818 send_msg ("my_display :: %s", my_display );
klauss 121:ee02790d00b7 819 send_msg ("peer_ext :: %d", peer_ext );
klauss 121:ee02790d00b7 820 send_msg ("fill_random_aux :: %s", fill_random_aux );
klauss 121:ee02790d00b7 821 send_msg ("last_invite_tag :: %s", last_invite_tag );
klauss 121:ee02790d00b7 822 send_msg ("last_invite_callid :: %s", last_invite_callid );
klauss 121:ee02790d00b7 823 send_msg ("SVNREV :: %s", SVNREV );
klauss 121:ee02790d00b7 824 send_msg ("char buffer[ 1024 ] :: %p", ( void * ) buffer );
klauss 109:a5b8264ffbbc 825
klauss 109:a5b8264ffbbc 826 //FIXME esse tipo de tag existe como metalinguagem ???
klauss 109:a5b8264ffbbc 827 //TOTHINK o que fazer pra entender melhor o estado dos sockets
klauss 121:ee02790d00b7 828 send_msg ("UDPSocket sock :: %p", ( void * ) &sock );
klauss 121:ee02790d00b7 829 send_msg ("Endpoint sip_server :: %p", ( void * ) &sip_server );
klauss 109:a5b8264ffbbc 830
klauss 121:ee02790d00b7 831 send_msg ("call :: %p", ( void * ) call );
klauss 121:ee02790d00b7 832 send_msg ("invite_timer :: %d", ( int ) invite_timer.read () );
klauss 121:ee02790d00b7 833 send_msg ("waiting :: %s", ( waiting ) ? "true" : "false" );
klauss 121:ee02790d00b7 834 send_msg ("listen_SIP_server_return :: %d", listen_SIP_server_return );
klauss 109:a5b8264ffbbc 835 send_msg ("");
klauss 109:a5b8264ffbbc 836
klauss 121:ee02790d00b7 837 return ( sizeof( Sip ) );
klauss 119:ee6a53069455 838 }
klauss 119:ee6a53069455 839
klauss 119:ee6a53069455 840 int Sip::retry_send_last_invite_pkg_to_ast ( void )
klauss 119:ee6a53069455 841 {
klauss 121:ee02790d00b7 842 char callbox_string [ 32 ];
klauss 121:ee02790d00b7 843 snprintf ( callbox_string, sizeof ( callbox_string ) -1, "%i", my_ext );
klauss 119:ee6a53069455 844
klauss 119:ee6a53069455 845 int cseq = 0;
klauss 119:ee6a53069455 846
klauss 119:ee6a53069455 847 call = NULL;
klauss 119:ee6a53069455 848
klauss 121:ee02790d00b7 849 build_invite_package ( &cseq, true );
klauss 119:ee6a53069455 850
klauss 121:ee02790d00b7 851 if ( debug_invite ) debug_msg ("cseq::%d", cseq );
klauss 119:ee6a53069455 852
klauss 119:ee6a53069455 853 int send = 0;
klauss 119:ee6a53069455 854
klauss 119:ee6a53069455 855 if ( drop_invite_to_ast_pkg )
klauss 119:ee6a53069455 856 {
klauss 121:ee02790d00b7 857 debug_msg ("[%d] Droped invite pkg to ast", this->my_ext );
klauss 119:ee6a53069455 858 }
klauss 119:ee6a53069455 859 else
klauss 119:ee6a53069455 860 {
klauss 121:ee02790d00b7 861
klauss 121:ee02790d00b7 862 send = sock.sendTo ( sip_server, buffer, strlen ( buffer ) );
klauss 119:ee6a53069455 863
klauss 121:ee02790d00b7 864 if ( send != strlen( buffer ) )
klauss 119:ee6a53069455 865 {
klauss 121:ee02790d00b7 866 if ( debug_reconnect ) debug_msg ("[%d] Reconnect SIP -- Invite", this->my_ext );
klauss 121:ee02790d00b7 867 __reconnect__ ();
klauss 119:ee6a53069455 868 miss_sip_invite_send_pkg++;
klauss 119:ee6a53069455 869 }
klauss 119:ee6a53069455 870
klauss 121:ee02790d00b7 871 if ( debug_invite || debug_reconnect ) debug_msg ("[%d] Return value for invite pkg %d", this->my_ext, send );
klauss 119:ee6a53069455 872 }
klauss 119:ee6a53069455 873
klauss 119:ee6a53069455 874 return ( send );
klauss 108:18a3702650f3 875 }