Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
119:ee6a53069455
Parent:
114:472502b31a12
Child:
121:ee02790d00b7
--- a/sip.cpp	Fri Apr 24 13:54:24 2015 +0000
+++ b/sip.cpp	Thu Apr 30 15:42:41 2015 +0000
@@ -11,63 +11,24 @@
 }
 
 void Sip::__reconnect__( void ){
-    sock.close(); 
+    __end_sock__(); 
     sock.init();
     sock.bind( this->my_port );
 }
-/*
-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 ){
+Sip::Sip( int new_ext, uint16_t my_port ){
     FILE * fp = NULL;
     char inner_tmp[ 20 ];
     
-    if( debug_sip ) debug_msg("Building SIP ext :: %d | Port :: %d", id, my_port );
+    if( debug_sip ) debug_msg("[%d, %d] Building SIP", new_ext, my_port );
     
-    this->id = id;
-    this->my_ext = id;
+    this->my_ext = new_ext;
     this->my_port = my_port;
         
     fp = fopen( "/qspi/serverip.txt", "r");
     if( fp == NULL){
-        if( debug_sip ) debug_msg("Failed to open /qspi/serverip.txt" );
-        strncpy( this->server_ip, __SERVER_IP__, 20 );    
+        if( debug_sip ) debug_msg("[%d] Failed to open /qspi/serverip.txt", this->my_ext );
+        strncpy( this->server_ip, SERVER_IP, 20 );    
     }else{
         char tmp[ 16 ] = "\0";
         int read = fread( tmp, 1, 15, fp );
@@ -78,10 +39,10 @@
                     break;
                 }
             strcpy( this->server_ip, tmp );
-            if( debug_sip ) debug_msg("Objeto SIP fixado no server ip %s( read %d chars ), valid %d", this->server_ip, read, strlen( this->server_ip )  );
+            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 )  );
         }else{
-            if( debug_sip ) debug_msg("Failed to read /qspi/serverip.txt" );
-            strncpy( this->server_ip, __SERVER_IP__, 20 );
+            if( debug_sip ) debug_msg("[%d] Failed to read /qspi/serverip.txt", this->my_ext );
+            strncpy( this->server_ip, SERVER_IP, 20 );
         }
     }
     fclose( fp );
@@ -89,23 +50,23 @@
     
     fp = fopen( "/qspi/serverport.txt", "r");
     if( fp == NULL ){
-        if( debug_sip ) debug_msg("Failed to open /qspi/serverport.txt" );
-        this->server_port = __SERVER_PORT__;
+        if( debug_sip ) debug_msg("[%d] Failed to open /qspi/serverport.txt", this->my_ext );
+        this->server_port = SERVER_PORT;
     }else{
         if( fread( (void *)inner_tmp, 1, 20, fp ) > 0 ){
             server_port = atoi( inner_tmp );
-            if( debug_sip ) debug_msg("Objeto SIP fixado no server port %d", this->server_port );
+            if( debug_sip ) debug_msg("[%d] server port %d", this->my_ext, this->server_port );
         }else{
-            if( debug_sip ) debug_msg("Failed to read /qspi/serverport.txt" );
-            this->server_port = __SERVER_PORT__;
+            if( debug_sip ) debug_msg("F[%d] failed to read /qspi/serverport.txt", this->my_ext );
+            this->server_port = SERVER_PORT;
         }
     }
     fclose( fp );
     
     fp = fopen( "/qspi/myip.txt", "r");
     if (fp == NULL){
-        if( debug_sip ) debug_msg("Failed to open /qspi/myip.txt" );
-        strncpy( this->my_ip, __MY_IP__, 20 );    
+        if( debug_sip ) debug_msg("[%d] Failed to open /qspi/myip.txt", this->my_ext );
+        strncpy( this->my_ip, MY_IP, 20 );    
     }else{
         char tmp[ 16 ] = "\0";
         int read = fread( tmp, 1, 15, fp );
@@ -116,54 +77,54 @@
                     break;
                 }
             strcpy( this->my_ip, tmp );
-            if( debug_sip ) debug_msg("Objeto SIP fixado no ip %s( lidos %d chars ), valid %d", this->my_ip, read, strlen( this->my_ip ) );
+            if( debug_sip ) debug_msg("[%d] ip %s( lidos %d chars ), valid %d", this->my_ext, this->my_ip, read, strlen( this->my_ip ) );
         }else{
-            if( debug_sip ) debug_msg("Failed to read /qspi/myip.txt" );
-            strncpy( this->my_ip, __MY_IP__, 20 );
+            if( debug_sip ) debug_msg("[%d] Failed to read /qspi/myip.txt", this->my_ext );
+            strncpy( this->my_ip, MY_IP, 20 );
         }
     }
     fclose( fp );
     
     this->my_ip[ 15 ] = '\0';
     
-    if( debug_sip ) debug_msg("Fixado na porta %d", this->my_port );
+    if( debug_sip ) debug_msg("[%d] port %d", this->my_ext, this->my_port );
     
     itoa( this->my_ext, this->my_display, 10 );
     
     this->my_rtp_port = 0;
-    if( debug_rtp ) debug_msg("new with rtp port %d ", this->my_rtp_port );
+    if( debug_rtp ) debug_msg("[%d] rtp port %d ", this->my_ext, this->my_rtp_port );
 
     fp = fopen( "/qspi/peerext.txt", "r");
     if( fp == NULL ) {
-        if( debug_sip ) debug_msg("Failed to open /qspi/peerext.txt" );
-        this->peer_ext = __PEER_EXT__;
+        if( debug_sip ) debug_msg("[%d] Failed to open /qspi/peerext.txt", this->my_ext );
+        this->peer_ext = PEER_EXT;
     } else {
         if( fread( (void *)inner_tmp, 1, 32, fp ) > 0 ) {
             this->peer_ext = atoi( inner_tmp );
-            if( debug_sip ) debug_msg("Objeto SIP fixado no server ext %d", this->peer_ext );
+            if( debug_sip ) debug_msg("[%d] server ext %d", this->my_ext, this->peer_ext );
         } else {
             if( debug_sip ) debug_msg("Failed to read /qspi/peerext.txt" );
-            this->peer_ext = __PEER_EXT__;
+            this->peer_ext = PEER_EXT;
         }
     }
     fclose( fp );
-    strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
+    
     strcpy( this->last_invite_tag, "" );
     strcpy( this->last_invite_callid, "");
+    strcpy( this->last_branch, "" );
     strcpy( this->SVNREV, "COLA" );
     status = sip_idle;
     
     __init_sock__();
-    muted = false;
     
-    invite_pkg_sent = 0;
     call = NULL;
     
-    ok_sent = 0;
     waiting = false;
-    length_muted = 0;
+    
     listen_SIP_server_return = 0;
     
+    last_cseq = 0;
+    
     sip_new_counter++;
 }
 
@@ -172,23 +133,26 @@
     sip_delete_counter++;
 }
  
-int Sip::registry(){
+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 ) debug_msg("[%d] Reconnect SIP -- Registry -- Sent %i of %i bytes", this->my_ext, 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 ) debug_msg("[%d %d] Registry -- 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(){
     char callbox_string[ 32 ];
-    itoa( get_id(), callbox_string, 10 );
+    snprintf ( callbox_string, sizeof ( callbox_string ) -1, "%i", get_id () );
     
     if( status == sip_on_call ) return call;
     
@@ -199,54 +163,46 @@
 
         build_invite_package( buffer, callbox_string, &cseq );
         
-        this->invite_pkg_sent = 0;
+        debug_msg ("cseq::%d", cseq );
         
         if ( drop_invite_to_ast_pkg )
         {
-                debug_msg("Droped invite pkg to ast");
+                debug_msg("[%d] Droped invite pkg to ast", this->my_ext );
         }
             else
         {
+            int send = 0;
             do{
-                this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) );
-            }while( this->invite_pkg_sent == 0 );
+                send = sock.sendTo( sip_server, buffer, strlen( buffer ) );
+            }while( send == 0 );
             
-            if( debug_invite ) debug_msg("Return value for invite pkg %d", invite_pkg_sent );
+            //FIXME verificar se posso nao usar invite_pkg_sent
+            if( send != strlen( buffer ) )
+            {
+                if( debug_reconnect ) debug_msg("[%d] Reconnect SIP -- Invite", this->my_ext );
+                __reconnect__();
+                miss_sip_invite_send_pkg++;
+            }
+         
+            if( debug_invite || debug_reconnect ) debug_msg("[%d] Return value for invite pkg %d", this->my_ext, send );
         }
         
         invite_timer.stop();
         invite_timer.reset();
         invite_timer.start();
         
-        set_status( status, sip_waiting_trying );
+        sip_set_status( sip_waiting_trying );
         
         waiting = false;
-        
-        ok_sent = 0;
-    
-        length_muted = 0;
     }
     
-    if( invite_timer.read() > __INVITE_MAX_WAITING_TIME__ ){
-        if( debug_invite == true ) debug_msg("Invite call timeout :(");
-        
-        if( ( this->invite_pkg_sent == -1 ) || length_muted || ok_sent == -1 ){
-            muted = true;
-            if( debug_invite || debug_muted ) debug_msg("RX from eth having situation, length_muted( %d ) - ok_sent( %d )", length_muted, ok_sent );
-            ok_sent = 0;
-            length_muted = 0;
-            __reconnect__();
-        }else{
-            muted = false;    
-        }
-        
-        if( debug_invite ) debug_msg("Timeout do pedido");
+    if( invite_timer.read() > INVITE_MAX_WAITING_TIME ){
+        if( debug_invite ) debug_msg("[%d] Invite call timeout :(", this->my_ext );
         invite_timer.stop();
         invite_timer.reset();
-        set_status( status, sip_denied );
+        sip_set_status ( sip_denied );
         send_bye();
         
-        //if( call != NULL ) delete( call );
         call = NULL;
         return( NULL );
     }
@@ -263,6 +219,33 @@
     fill_random16h( branch );
     fill_random( tag, 18 );
     fill_random16h( callid );
+    
+    
+    /*
+    snprintf(s, SIP_MAXMSGSIZE,
+        "REGISTER sip:%s:%d SIP/2.0\r\n"  //sip_server_ip, sip_server_port,
+        "Via: SIP/2.0/UDP %s:%d;branch=%s\r\n" //sip_myself_ip, sip_myself_port, branch,
+        "From: %s <sip:%d@%s:%d>;tag=%s\r\n"  // sip_myself_display, sip_myself_ext, sip_server_ip, sip_server_port, tag,
+        "To: %s <sip:%d@%s:%d>\r\n" //  sip_myself_display, sip_myself_ext, sip_server_ip, sip_server_port,
+        "Call-ID: %s@%s\r\n"  // callid, sip_myself_ip,
+        "CSeq: %d REGISTER\r\n"
+        "Max-Forwards: 70\r\n"
+        "Contact: %s <sip:%d@%s:%d>;expires=%i\r\n"
+        "User-Agent: VZtech/pabxdriver-%s\r\n"
+        "Content-Length: 0\r\n"
+        SIP_ALLOW "\r\n\r\n",
+        sip_server_ip, sip_server_port,
+        sip_myself_ip, sip_myself_port, branch,
+        sip_myself_display, sip_myself_ext, sip_server_ip, sip_server_port, tag,
+        sip_myself_display, sip_myself_ext, sip_server_ip, sip_server_port,
+        callid, sip_myself_ip,
+        get_cseq(),
+        sip_myself_display, sip_myself_ext, sip_myself_ip, sip_myself_port, SIP_REGISTER_EXPIRES,
+        SVNREV
+    );
+    s[SIP_MAXMSGSIZE-1] = 0;
+    */
+    
     char itoa_buffer[ 65 ];
     strcpy( buffer, "REGISTER sip:" );
     strcat( buffer, server_ip );
@@ -334,207 +317,85 @@
     return buffer;
 }
  
-char * Sip::build_invite_package( char * pkg, char * callbox_string, int * cseq ){
+char * Sip::build_invite_package( char * pkg, char * callbox_string, int * cseq, bool retry ){
     char header[ SIP_MAXMSGSIZE ], body[ SIP_MAXMSGSIZE ];
     char branch[ SIP_MAXFIELDSIZE ];
     char tag[ SIP_MAXFIELDSIZE ];
     char callid[ SIP_MAXFIELDSIZE ];
-    char itoa_buffer[ 65 ];
-    *cseq = get_cseq();
-    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 );
-    
-    strcpy( header, "INVITE sip:" );
-    itoa( this->peer_ext, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "@" );
-    strcat( header, this->server_ip );
-    strcat( header, ":" );
-    itoa( this->server_port, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, " SIP/" );
-    itoa( 2, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "." );
-    itoa( 0, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "\r\n" );
-    strcat( header, "Via: SIP/" );
-    itoa( 2, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    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, this->my_ip );
-    strcat( header, ":" );
-    itoa( this->my_port, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, ";branch="  );
-    strcat( header, branch );
-    strcat( header, "\r\n" );
-    strcat( header, "From: " );
-    strcat( header, callbox_string );
-    strcat( header, " <sip:" );
-    itoa( this->my_ext, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "@" );
-    strcat( header, this->server_ip );
-    strcat( header, ":" );
-    itoa( this->server_port, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, ">;tag=" );
-    strcat( header, tag );
-    strcat( header, "\r\n" );
-    strcat( header, "To: <sip:" );
-    itoa( this->peer_ext, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "@" );
-    strcat( header, this->server_ip );
-    strcat( header, ":" );
-    itoa( this->server_port, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, ">\r\n" );
-    strcat( header, "Call-ID: " );
-    strcat( header, callid );
-    strcat( header, "@" );
-    strcat( header, this->my_ip );
-    strcat( header, "\r\n" );
-    strcat( header, "CSeq: " );
-    itoa( *cseq, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, " INVITE\r\n" );
-    strcat( header, "Contact: " );
-    itoa( this->my_ext, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, " <sip:" );
-    itoa( this->my_ext, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "@" );
-    strcat( header, this->my_ip );
-    strcat( header, ":" );
-    itoa( this->my_port, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, ">\r\n" );
-    strcat( header, "Max-Forwards: " );
-    itoa( 20, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "\r\n" );
-    strcat( header, "User-Agent: VZtech/pabxdriver-" );
-    strcat( header, SVNREV );
-    strcat( header, "\r\n" );
-    strcat( header, "Expires: " );
-    itoa( 71, itoa_buffer, 10 );
-    strcat( header, itoa_buffer );
-    strcat( header, "\r\n" );
-    strcat( header, SIP_ALLOW );
-    strcat( header, "\r\n" );
-    strcat( header, "Content-Type: application/sdp\r\n" );
-    //header[ SIP_MAXMSGSIZE - 1 ] = 0;
-    
-    strcpy( body, "v=0\r\n" );
-    strcat( body, "o=- " );
-    itoa( 7377, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " " );
-    itoa( 18176, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " IN IP" );
-    itoa( 4, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " " );
-    strcat( body, this->my_ip );
-    strcat( body, "\r\n" );
-    strcat( body, "s=-\r\n" );
-    strcat( body, "c=IN IP" );
-    itoa( 4, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " " );
-    strcat( body, this->my_ip );
-    strcat( body, "\r\n" );
-    strcat( body, "t=" );
-    itoa( 0, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " " );
-    itoa( 0, itoa_buffer, 10 );
-    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 )
+    if ( !retry )
     {
-        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 );
+        *cseq = get_cseq();
+        last_cseq = *cseq;
+        
+        fill_random16h( branch );
+        strcpy( last_branch, branch );
+        
+        fill_random( tag,18 );
+        strcpy( last_invite_tag, tag );
+        
+        fill_random16h( callid );
+        strcpy( last_invite_callid, callid );
+        
+        this->my_rtp_port = fill_random_rtp_port ();
     }
         else
     {
-        if ( canada )
-        {
-            canada = false;
-            strcat( body, " " );
-        }
-            else
-        {
-            strcat( body, itoa_buffer );
-        }
+        *cseq = last_cseq;
+        
+        strcpy( branch, last_branch  );
+        
+        strcpy( tag, last_invite_tag );
+        
+        strcpy( callid, last_invite_callid );
     }
-    */
-    strcat( body, " RTP/AVP " );
-    itoa( 8, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " " );
-    itoa( 101, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, "\r\n" );
-    strcat( body, "a=rtpmap:" );
-    itoa( 8, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " PCMA/" );
-    itoa( 8000, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, "/" );
-    itoa( 1, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, "\r\n" );
-    strcat( body, "a=rtpmap:" );
-    itoa( 101, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " telephone-event/" );
-    itoa( 8000, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, "\r\n" );
-    strcat( body, "a=fmtp: " );
-    itoa( 101, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, " " );
-    itoa( 0, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    strcat( body, "-" );
-    itoa( 11, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    //body[ SIP_MAXMSGSIZE - 1 ] = 0;
- 
+    
+    if ( debug_invite ) debug_msg("[%d] \r\nbranch :: %s\r\ntag :: %s\r\ncallid :: %s", this->my_ext, branch, tag, callid );
+    
+    if( debug_rtp || debug_invite ) debug_msg("[%d] Invite PKG rtp port ( %d )", this->my_ext, this->my_rtp_port );
+
+    snprintf( header, SIP_MAXMSGSIZE,
+        "INVITE sip:%i@%s:%i SIP/2.0\r\n"
+        "Via: SIP/2.0/UDP %s:%i;branch=%s\r\n"
+        "From: %s <sip:%i@%s:%i>;tag=%s\r\n"
+        "To: <sip:%i@%s:%i>\r\n"
+        "Call-ID: %s@%s\r\n"
+        "CSeq: %i INVITE\r\n"
+        "Contact: %i <sip:%i@%s:%i>\r\n"
+        "Max-Forwards: 20\r\n"
+        "User-Agent: VZtech/pabxdriver-%s\r\n"
+        "Expires: 71\r\n"
+        "%s\r\n"
+        "Content-Type: application/sdp\r\n",
+        this->peer_ext, this->server_ip, this->server_port, 
+        this->my_ip, this->my_port, branch, 
+        callbox_string, this->my_ext, this->server_ip, this->server_port, tag,
+        this->peer_ext, this->server_ip, this->server_port, 
+        callid, this->my_ip, 
+        *cseq,
+        this->my_ext, this->my_ext, this->my_ip, this->my_port, 
+        SVNREV, 
+        SIP_ALLOW
+    );
+    
+    snprintf( body, SIP_MAXMSGSIZE,
+        "v=0\r\no=- 7377 18176 IN IP4 %s\r\n"
+        "s=-\r\n"
+        "c=IN IP4 %s\r\n"
+        "t=0 0\r\n"
+        "i=UDP %i\r\n"
+        "m=audio %i RTP/AVP 8 101\r\n"
+        "a=rtpmap:8 PCMA/8000/1\r\n"
+        "a=rtpmap:101 telephone-event/8000\r\n"
+        "a=fmtp: 101 0-11",
+        this->my_ip,
+        this->my_ip,
+        this->my_rtp_port,
+        this->my_rtp_port
+    );
+    
     make_content_length( header, body, pkg );
+    
     return( pkg );
 }
  
@@ -927,7 +788,7 @@
 }
  
 int Sip::get_id( void ){
-    return this->id;
+    return this->my_ext;
 }
 int Sip::get_my_rtp_port( void ){
     return this->my_rtp_port;
@@ -939,26 +800,7 @@
     port &= 0x7fe; // 0 to 2046, always even
     return port + 16384;
 }
- 
-void Sip::change_sip_server(){
-    // just in case sip server don't answer, invite another server
-    // mudar para os sets
-    set_server_ip( get_next_server_ip( this->buffer) );
-    set_server_ext( get_next_server_ext() );
-    set_server_port( get_next_server_port() );
-    registry();
-}
- 
-char * Sip::get_next_server_ip( char * server_ip ){
-    strcpy( server_ip, this->server_ip );
-    return server_ip;
-}
-int Sip::get_next_server_port(){
-    return this->server_port;
-}
-int Sip::get_next_server_ext(){
-    return this->peer_ext;
-}
+
 void Sip::set_server_port( int new_server_port ){
     this->server_port = new_server_port;
 }
@@ -989,108 +831,20 @@
     my_port = port;
 }
 
-void Sip::send_bye( void ){
+void Sip::send_bye ( void )
+{
+    int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) );
     
-    int ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) );
-    
-    if( debug_sip ) send_msg( "sizeof( bye pkg ) :: %d", strlen( buffer ) );
+    if( debug_sip ) debug_msg( "[%d] sizeof( bye pkg ) :: %d", this->my_ext, strlen( buffer ) );
     
-    if( ret == -1 ){
-        muted = true;
-    }else{
-        muted = false;    
-    }
-}
-
-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 ) );
-    if( debug_sip ) send_msg("sizeof( unregistry_pkg ) :: %d", strlen( buffer ) );
-    
-    if( ret == -1 ){
-        muted = true;
-    }else{
-        muted = false;    
+    if( send != strlen( this->buffer ) )
+    {
+        if( debug_reconnect ) debug_msg("[%d] Reconnect SIP -- BYE, %d, %d", this->my_ext, send, strlen( this->buffer ) );
+        __reconnect__();
+        miss_sip_bye_send_pkg++;
     }
 }
 
-char * Sip::build_unregistry_package( char * buffer ){
-    char branch[ SIP_MAXFIELDSIZE ];
-    char tag[ SIP_MAXFIELDSIZE ];
-    char callid[ SIP_MAXFIELDSIZE ];
-    fill_random16h( branch );
-    fill_random( tag, 18 );
-    fill_random16h( callid );
-    char itoa_buffer[ 65 ];
-    strcpy( buffer, "REGISTER sip:" );
-    strcat( buffer, server_ip );
-    strcat( buffer, ":" );
-    itoa( server_port, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, " SIP/2.0\r\nVia: SIP/2.0/UDP " );
-    strcat( buffer, my_ip );
-    strcat( buffer, ":" );
-    itoa( my_port, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, ";branch=" );
-    strcat( buffer, branch );
-    strcat( buffer, "\r\nFrom: " );
-    itoa( my_ext, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, " <sip:" );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, "@" ); 
-    strcat( buffer, server_ip );
-    strcat( buffer, ":" );
-    itoa( server_port, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, ">;tag=" );
-    strcat( buffer, tag );
-    strcat( buffer, "\r\nTo: " );
-    itoa( my_ext, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, " <sip:" );
-    itoa( my_ext, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, "@" );
-    strcat( buffer, server_ip );
-    strcat( buffer, ":" );
-    itoa( server_port, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, ">\r\nCall-ID: " );
-    strcat( buffer, callid );
-    strcat( buffer, "\r\nCSeq: " );
-    itoa( get_cseq(), itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, " REGISTER\r\n" );
-    strcat( buffer, "Max-Forwards: " );
-    itoa( 70, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, "\r\n" );
-    
-    strcat( buffer, "Contact: " );
-    itoa( my_ext, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, " <sip:" );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, "@" );
-    strcat( buffer, my_ip );
-    strcat( buffer, ":" );
-    itoa( server_port, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, ">;expires=0\r\n" );
-    strcat( buffer, "User-Agent: VZtech/pabxdriver-" );
-    strcat( buffer, SVNREV );
-    strcat( buffer, "\r\nContent-Length: 0\r\n" );
-    strcat( buffer, SIP_ALLOW );
-    strcat( buffer, "\r\n\r\n" );              
-    //buffer[ SIP_MAXMSGSIZE - 1 ] = 0;
-    return buffer;
-}
-
 int Sip::get_status( void ){
     return this->status;    
 }
@@ -1099,11 +853,6 @@
     this->status = status;    
 }
 
-void Sip::sip_check_muted( void ){
-    if( muted == true ) __reconnect__();
-    if( debug_muted ) send_msg("For some reason sip muted, reconnected");
-}
-
 int Sip::get_socket_fd( void ){
     return sock.get_fd();
 }
@@ -1115,7 +864,12 @@
     
     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 ) debug_msg("[%d] Reconnect SIP -- UDP Incomming", get_ext () );
+        __reconnect__();
+        miss_sip_inc_pkg++;
+    }
     
     if( length > 0 ){
         if( sizeof( buffer ) > length ) buffer[ length ] = 0;
@@ -1127,14 +881,19 @@
             sip_response = ( buffer[ 8 ] - '0' ) * 100 + ( buffer[ 9 ] - '0' ) * 10 + ( buffer[ 10 ] - '0' );
         }
         
-        if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) ){ 
+        if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) || ( status == sip_on_call ) )
+        { 
             if( sip_response == 200 ){ // Ok    
                 char *ref = strstr( buffer, "audio" );
-                if( debug_invite ) debug_msg("ok");
+                if( debug_invite ) debug_msg("[%d] udp_incomming_pkg -- ok", this->my_ext );
                 if( ref != NULL ){
-                    if ( drop_ok_pkg )
+                    
+                    static uint8_t count = 0;
+                    if ( ( count++ % 3 ) == 0 ) drop_ok_pkg = !drop_ok_pkg;
+                        
+                    if ( drop_this_amount_of_ack_to_ast-- )
                     {
-                        debug_msg("Droped ok pkg received from ast");
+                        debug_msg("[%d] Droped ok pkg received from ast (%d)", this->my_ext, drop_this_amount_of_ack_to_ast );
                         return -71;    
                     }
                                         
@@ -1142,41 +901,45 @@
                     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("[%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 );
                         if( call == NULL ) memory_is_over = true;
                     }
                     char ans[ 1024 ];
                     build_ack_package( ans, ( unsigned char *)buffer );
                     
+                    int send = 0;
                     
                     if ( drop_ack_pkg )
                     {
-                        debug_msg("Droped ack pkt to ast");
-                        //return -73;
-                        // nao faz sentido ter esse return por que na logica o resto eh processado
+                        debug_msg("[%d] Droped ack pkt to ast", this->my_ext );
                     }
                         else
                     {
-                        ok_sent = sock.sendTo( sip_server, ans, strlen( ans ) );
+                        send = sock.sendTo( sip_server, ans, strlen( ans ) );
+                    
+                        if( send != strlen( ans ) )
+                        {
+                            if( debug_reconnect ) debug_msg("[%d] Reconnect SIP -- Ok -- Call mode on", this->my_ext );
+                            __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( debug_invite ) debug_msg("[%d] Call alocada -- sizeof( ack ) :: %d - strlen( ans ):: %d", this->my_ext, send, strlen( ans ) );
+                    sip_set_status ( sip_on_call );
                 }
             }
         }
         if( ( status == sip_trying ) || ( status == sip_ringing ) ){                
-            //if( !( strncmp( buffer + 7, " 4", 2 ) ) ){
             if( sip_response >= 400 && sip_response < 700 ){
                 if( debug_invite ) { 
                     buffer[ 11 ] = 0; 
-                    if( debug_invite ) debug_msg("Busy Here :: %s", buffer + 8 );
+                    if( debug_invite ) debug_msg("[%d] Busy Here :: %s", this->my_ext, buffer + 8 );
                 }
                 send_bye();
-                //if( call != NULL ) delete( call );
-                set_status( status, sip_busy );
+                sip_set_status ( sip_busy );
                 return( NULL );  
-            }else //if( !( strncasecmp( buffer + 7, " 183 ", 5 ) ) ){ // Session in Progress
+            }else // Session in Progress
                 if( sip_response == 183 ){
                 char *ref = strstr( buffer, "audio" );
                 if( ref != NULL ){
@@ -1184,6 +947,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("[%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 );
                         if( call == NULL ) memory_is_over = true;
                     }
                     waiting = true;
@@ -1191,17 +955,15 @@
             }
         }
         if( status == sip_trying ){
-            //if( !( strncasecmp( buffer + 7, " 180 ", 5 ) ) ){
             if( sip_response == 180 ){
-                if( debug_invite ) debug_msg("ringing");
-                set_status( status, sip_ringing );
+                if( debug_invite ) debug_msg("[%d] ringing", this->my_ext );
+                sip_set_status ( sip_ringing );
             }
         }
         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 );
+                if( debug_invite ) debug_msg("[%d] trying", this->my_ext )
+                sip_set_status ( sip_trying );
             }
         }
         if( status == sip_on_call ){
@@ -1209,10 +971,18 @@
                 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 ) debug_msg("[%d] Reconnect SIP -- RCV BYE from * -- ( expected, realized ) ( %d, %d )", this->my_ext, strlen( ans ), send );
+                    __reconnect__();
+                    miss_sip_rcv_bye_send_pkg++;
+                }
+                
+                if( debug_invite ) debug_msg("[%d] Bye request received from * - sizeof( bye reply pkg ) :: %d", this->my_ext, strlen( ans ) );
+                
+                
+                set_sip_rtp_port( 0 );
                 
                 listen_SIP_server_return = my_ext;
             }
@@ -1235,14 +1005,13 @@
 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( "[%d] this->my_rtp_port ( %d ) = new_my_rtp_port( %d )", this->my_ext, this->my_rtp_port, new_my_rtp_port );
 }
 
 int Sip::print_yourself ( void )
 {
     send_msg ("");
     send_msg ("Values ::\r\n");
-    send_msg("id :: %d", id );
     send_msg("server_ip :: %s", server_ip );
     send_msg("server_port :: %d", server_port );
     send_msg("my_ip :: %s", my_ip );
@@ -1255,7 +1024,6 @@
     send_msg("last_invite_tag :: %s", last_invite_tag );
     send_msg("last_invite_callid :: %s", last_invite_callid );
     send_msg("SVNREV :: %s", SVNREV );
-    // o.O
     send_msg("char buffer[ 1024 ] :: %p", ( void * ) buffer );
     
     //FIXME esse tipo de tag existe como metalinguagem ???
@@ -1263,15 +1031,49 @@
     send_msg("UDPSocket sock :: %p", ( void * ) &sock );
     send_msg("Endpoint sip_server :: %p", ( void * ) &sip_server );
     
-    send_msg("muted :: %s", ( muted ) ? "true" : "false" );
-    send_msg("invite_pkg_sent :: %d", invite_pkg_sent );
     send_msg("call :: %p", ( void * ) call );
     send_msg("invite_timer :: %d", ( int ) invite_timer.read () );
-    send_msg("ok_sent :: %d", ok_sent );
     send_msg("waiting ::  %s", ( waiting ) ? "true" : "false" );
-    send_msg("length_muted :: %d",length_muted );
     send_msg("listen_SIP_server_return :: %d", listen_SIP_server_return );
     send_msg ("");
 
     return( sizeof( Sip ) );    
+}
+
+int Sip::retry_send_last_invite_pkg_to_ast ( void )
+{
+    char callbox_string[ 32 ];
+    snprintf ( callbox_string, sizeof ( callbox_string ) -1, "%i", get_id () );
+    
+    int cseq = 0;
+
+    call = NULL;
+
+    build_invite_package( buffer, callbox_string, &cseq, true );
+    
+    debug_msg ("cseq::%d", cseq );
+    
+    int send = 0;
+    
+    if ( drop_invite_to_ast_pkg )
+    {
+            debug_msg("[%d] Droped invite pkg to ast", this->my_ext );
+    }
+        else
+    {
+        do{
+            send = sock.sendTo( sip_server, buffer, strlen( buffer ) );
+        }while( send == 0 );
+        
+        if( send != strlen( buffer ) )
+        {
+            if( debug_reconnect ) debug_msg("[%d] Reconnect SIP -- Invite", this->my_ext );
+            __reconnect__();
+            miss_sip_invite_send_pkg++;
+        }
+     
+        if( debug_invite || debug_reconnect ) debug_msg("[%d] Return value for invite pkg %d", this->my_ext, send );
+    }
+    
+    return ( send );
 }
\ No newline at end of file