![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: sip.cpp
- Revision:
- 113:db67ae00550e
- Parent:
- 112:6ae726539ab9
- Child:
- 114:472502b31a12
--- a/sip.cpp Mon Apr 13 12:16:21 2015 +0000 +++ b/sip.cpp Thu Apr 16 12:57:13 2015 +0000 @@ -11,48 +11,9 @@ } void Sip::__reconnect__( void ){ - sock.close(); - sock.init(); - sock.bind( this->my_port ); + __end_sock__(); + __init_sock__(); } -/* -Sip::Sip( char * server_ip, int server_port, char * my_ip, int my_port, int my_ext, int peer_ext, int id ){ - this->id = id; - strncpy( this->server_ip, server_ip, 20 ); - this->server_ip[19] = 0; - this->server_port = server_port; - strncpy( this->my_ip, my_ip, 20 ); - this->my_ip[19] = 0; - this->my_port = my_port; - //this->my_rtp_port = fill_random_rtp_port(); - this->my_rtp_port = 0; - if( debug_rtp ) debug_msg("new with rtp port %d ", this->my_rtp_port ); - - this->my_ext = my_ext; - itoa( this->my_ext, this->my_display, 10 ); - this->peer_ext = peer_ext; - - strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" ); - strcpy( this->last_invite_tag, "" ); - strcpy( this->last_invite_callid, ""); - strcpy( this->SVNREV, "COLA" ); - status = sip_idle; - - __init_sock__(); - muted = false; - - this->invite_pkg_sent = 0; - - call = NULL; - - ok_sent = 0; - waiting = false; - length_muted = 0; - listen_SIP_server_return = 0; - - sip_new_counter++; -} -*/ Sip::Sip( int id, uint16_t my_port ){ FILE * fp = NULL; @@ -175,15 +136,18 @@ int Sip::registry(){ build_registry_package( buffer ); - int sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400 + int send = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400 + if( send != strlen( buffer ) ) + { + if( debug_reconnect ) send_msg("Reconnect SIP -- Registry"); + if( debug_reconnect ) send_msg("Sent %i of %i bytes", send, strlen( buffer ) ); + __reconnect__(); + miss_sip_registry_send_pkg++; + } - if( debug_sip ) send_msg("Registry ( %d, %d ) - Sent %i of %i bytes", this->my_ext, this->my_port , sent, strlen( buffer ) ); + if( debug_sip ) send_msg("Registry ( %d, %d ) -- Sent %i of %i bytes", this->my_ext, this->my_port, send, strlen( buffer ) ); - if( sent != strlen( buffer ) ){ - sip_socket_send_failure++; - } - - return( sent ); + return( send ); } VZ_call * Sip::invite(){ @@ -201,19 +165,21 @@ this->invite_pkg_sent = 0; - if ( drop_invite_to_ast_pkg ) - { - debug_msg("Droped invite pkg to ast"); - } - else + do{ + this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); + }while( this->invite_pkg_sent == 0 ); + + //FIXME verificar se posso nao usar invite_pkg_sent + int send = this->invite_pkg_sent; + if( send != strlen( buffer ) ) { - do{ - this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); - }while( this->invite_pkg_sent == 0 ); - - if( debug_invite ) debug_msg("Return value for invite pkg %d", invite_pkg_sent ); + if( debug_reconnect ) send_msg("Reconnect SIP -- Invite"); + __reconnect__(); + miss_sip_invite_send_pkg++; } + if( debug_invite || debug_reconnect ) send_msg("Return value for invite pkg %d", invite_pkg_sent ); + invite_timer.stop(); invite_timer.reset(); invite_timer.start(); @@ -344,10 +310,6 @@ fill_random16h( branch ); fill_random( tag,18 ); fill_random16h( callid ); - - debug_msg("branch :: %s", branch ); - debug_msg("tag :: %s", tag ); - debug_msg("callid :: %s", callid ); strcpy( last_invite_tag,tag ); strcpy( last_invite_callid, callid ); @@ -373,7 +335,7 @@ strcat( header, "." ); itoa( 0, itoa_buffer, 10 ); strcat( header, itoa_buffer ); - strcat( header, "/UDP " ); // isso que dizer qu eue sempre coloco /UDP:192, quando deveria ser /UDP<0x20>192... + strcat( header, "/UDP " ); strcat( header, this->my_ip ); strcat( header, ":" ); itoa( this->my_port, itoa_buffer, 10 ); @@ -468,37 +430,14 @@ strcat( body, itoa_buffer ); strcat( body, "\r\n" ); - this->my_rtp_port = fill_random_rtp_port(); sprintf( itoa_buffer,"i=UDP %d\r\n", this->my_rtp_port ); strcat( body, itoa_buffer ); + strcat( body, "m=audio " ); itoa( this->my_rtp_port, itoa_buffer, 10 ); strcat( body, itoa_buffer ); - - if( debug_rtp ) debug_msg("Invite PKG with rtp poOort ( %d - %s )", this->my_rtp_port, itoa_buffer ); - - /* - if ( chicago ) - { - chicago = false; - itoa( ( this->my_rtp_port ) + 19, itoa_buffer, 10 ); - debug_msg("my_rtp_port( %d )", my_rtp_port + 19 ); - strcat( body, itoa_buffer ); - } - else - { - if ( canada ) - { - canada = false; - strcat( body, " " ); - } - else - { - strcat( body, itoa_buffer ); - } - } - */ + if( debug_rtp ) debug_msg("Invite PKG with rtp port ( %d - %s )", this->my_rtp_port, itoa_buffer ); strcat( body, " RTP/AVP " ); itoa( 8, itoa_buffer, 10 ); strcat( body, itoa_buffer ); @@ -933,10 +872,11 @@ return this->my_rtp_port; } int Sip::fill_random_rtp_port () -{ +{ static uint16_t port = 0; port += 2; port &= 0x7fe; // 0 to 2046, always even + return port + 16384; } @@ -990,31 +930,30 @@ } void Sip::send_bye( void ){ - - int ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); + int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); if( debug_sip ) send_msg( "sizeof( bye pkg ) :: %d", strlen( buffer ) ); - if( ret == -1 ){ - muted = true; - }else{ - muted = false; + if( send != strlen( this->buffer ) ) + { + if( debug_reconnect ) send_msg("Reconnect SIP -- BYE, %d, %d", send, strlen( this->buffer ) ); + __reconnect__(); + miss_sip_bye_send_pkg++; } } void Sip::send_unregistry_pkg(){ build_unregistry_package( this->buffer ); - int ret = 0; - - ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); + int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); + if( send != strlen( this->buffer ) ) + { + if( debug_reconnect ) send_msg("Reconnect SIP -- Unregistry"); + __reconnect__(); + miss_sip_unregistry_send_pkg++; + } + if( debug_sip ) send_msg("sizeof( unregistry_pkg ) :: %d", strlen( buffer ) ); - - if( ret == -1 ){ - muted = true; - }else{ - muted = false; - } } char * Sip::build_unregistry_package( char * buffer ){ @@ -1113,9 +1052,15 @@ int length = sock.receiveFrom( from, buffer, sizeof( buffer ) ); + //checa se o pacote foi recebido do antigo host conhecido. if( memcmp( ( ( u8_t * )( &(from._remoteHost) ) ) + 2, ( ( u8_t * )( &(sip_server._remoteHost) ) ) + 2, 6 ) != 0 ) return 0; - if( length == -1 ){ length_muted++; } + if( length == -1 ) + { + if( debug_reconnect ) send_msg("Reconnect SIP -- UDP Incomming"); + __reconnect__(); + miss_sip_inc_pkg++; + } if( length > 0 ){ if( sizeof( buffer ) > length ) buffer[ length ] = 0; @@ -1128,44 +1073,36 @@ } if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) ){ - if( sip_response == 200 ){ // Ok + //if( !( strncasecmp( buffer + 7, " 200 ", 5 ) ) ){ + if( sip_response == 200 ){ // Ok char *ref = strstr( buffer, "audio" ); if( debug_invite ) debug_msg("ok"); if( ref != NULL ){ - if ( drop_ok_pkg ) - { - debug_msg("Droped ok pkg received from ast"); - return -71; - } - ref += 6; // audio ref = strtok( ref, " "); if( call == NULL ){ call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); + if( debug_rtp ) debug_msg("Ok invite ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref ); if( call == NULL ) memory_is_over = true; } char ans[ 1024 ]; build_ack_package( ans, ( unsigned char *)buffer ); + int send = sock.sendTo( sip_server, ans, strlen( ans ) ); - if ( drop_ack_pkg ) + if( send != strlen( ans ) ) { - debug_msg("Droped ack pkt to ast"); - //return -73; - // nao faz sentido ter esse return por que na logica o resto eh processado - } - else - { - ok_sent = sock.sendTo( sip_server, ans, strlen( ans ) ); + if( debug_reconnect ) send_msg("Reconnect SIP -- Ok -- Call mode on"); + __reconnect__(); + miss_sip_ok_send_pkg++; } - debug_msg("sizeof( ack ) :: %d - strlen( ans ):: %d", ok_sent, strlen( ans ) ); if( debug_invite ) debug_msg("Call alocada -- Sip::status::%d :: ", status ); set_status( status, sip_on_call ); } } } - if( ( status == sip_trying ) || ( status == sip_ringing ) ){ + if( ( status == sip_trying ) || ( status == sip_ringing ) ){ //if( !( strncmp( buffer + 7, " 4", 2 ) ) ){ if( sip_response >= 400 && sip_response < 700 ){ if( debug_invite ) { @@ -1184,6 +1121,7 @@ ref = strtok( ref, " "); if( call == NULL ){ this->call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); + if( debug_rtp ) debug_msg("Session in Progress ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref ); if( call == NULL ) memory_is_over = true; } waiting = true; @@ -1198,7 +1136,6 @@ } } if( status == sip_waiting_trying ){ - //if( !( strncasecmp( buffer + 7, " 100 ", 5 ) ) ){ if( sip_response == 100 ){ if( debug_invite ) debug_msg("trying") set_status( status, sip_trying ); @@ -1209,16 +1146,22 @@ char ans[ 2000 ]; build_reply_package( ans, (unsigned char*)buffer ); - int bye_sent = sock.sendTo( sip_server, ans, strlen( ans ) ); - if( debug_invite ) debug_msg("sizeof( bye reply pkg ) :: %d", strlen( ans ) ); - if( debug_invite ) debug_msg("Server request BYE to %d ext", my_ext ); - if( debug_invite ) debug_msg("Bye request received from *"); + int send = sock.sendTo( sip_server, ans, strlen( ans ) ); + if( send == strlen( ans ) ) + { + if( debug_reconnect ) send_msg("Reconnect SIP -- RCV BYE from *"); + __reconnect__(); + miss_sip_rcv_bye_send_pkg++; + } + + if( debug_invite ) debug_msg("sizeof( bye reply pkg ) :: %d\nServer request BYE to %d ext\nBye request received from *", strlen( ans ), my_ext ); + + set_sip_rtp_port( 0 ); listen_SIP_server_return = my_ext; } } } - return length; } @@ -1235,7 +1178,7 @@ void Sip::set_sip_rtp_port ( int new_my_rtp_port ) { this->my_rtp_port = new_my_rtp_port; - debug_msg( "this->my_rtp_port ( %d ) = new_my_rtp_port( %d )", this->my_rtp_port, new_my_rtp_port ); + if( debug_rtp ) debug_msg( "this->my_rtp_port ( %d ) = new_my_rtp_port( %d )", this->my_rtp_port, new_my_rtp_port ); } int Sip::print_yourself ( void )