voltando a versao de n aberturas e fechamentos de sockets

Dependencies:   EthernetInterface NTPClient mbed-rtos_old mbed

Fork of header_main_public by VZTECH

Committer:
klauss
Date:
Wed Sep 10 18:21:48 2014 +0000
Revision:
3:cd9148672e25
Parent:
1:a1758104fa1d
Child:
4:de46f0d9b14d
versao estavel; - Realiza ligacao de 1 cb, master/slave, como musica de conforto e tratamento sip/rtp, desalocacao de recursos.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
klauss 0:4d17cd9c8f9d 1 #include "sip.h"
klauss 0:4d17cd9c8f9d 2 #include "utils.h"
klauss 0:4d17cd9c8f9d 3
klauss 0:4d17cd9c8f9d 4 void Sip::__init_sock__( void ){
klauss 0:4d17cd9c8f9d 5 sip_server.set_address( this->server_ip , this->server_port );
klauss 0:4d17cd9c8f9d 6 sock.set_blocking( false, 1 ); // verificar isso com mais cuidado depois
klauss 0:4d17cd9c8f9d 7 sock.bind( this->my_port );
klauss 0:4d17cd9c8f9d 8 sock.init();
klauss 0:4d17cd9c8f9d 9 }
klauss 0:4d17cd9c8f9d 10
klauss 0:4d17cd9c8f9d 11 void Sip::__end_sock__(){
klauss 0:4d17cd9c8f9d 12 sock.close();
klauss 0:4d17cd9c8f9d 13 }
klauss 0:4d17cd9c8f9d 14
klauss 0:4d17cd9c8f9d 15 Sip::Sip( char * server_ip, int server_port, char * my_ip, int my_port, int my_ext, int peer_ext, int id ){
klauss 0:4d17cd9c8f9d 16 this->id = id;
klauss 0:4d17cd9c8f9d 17 strncpy( this->server_ip, server_ip, 20 );
klauss 0:4d17cd9c8f9d 18 this->server_ip[19] = 0;
klauss 0:4d17cd9c8f9d 19 this->server_port = server_port;
klauss 0:4d17cd9c8f9d 20 strncpy( this->my_ip, my_ip, 20 );
klauss 0:4d17cd9c8f9d 21 this->my_ip[19] = 0;
klauss 0:4d17cd9c8f9d 22 this->my_port = my_port;
klauss 0:4d17cd9c8f9d 23 this->my_rtp_port = fill_random_rtp_port();
klauss 0:4d17cd9c8f9d 24 this->my_ext = my_ext;
klauss 0:4d17cd9c8f9d 25 itoa( this->my_ext, this->my_display, 10 );
klauss 0:4d17cd9c8f9d 26 this->peer_ext = peer_ext;
klauss 0:4d17cd9c8f9d 27
klauss 0:4d17cd9c8f9d 28 strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
klauss 0:4d17cd9c8f9d 29 strcpy( this->last_invite_tag, "" );
klauss 0:4d17cd9c8f9d 30 strcpy( this->last_invite_callid, "");
klauss 0:4d17cd9c8f9d 31 strcpy( this->SVNREV, "COLA" );
klauss 0:4d17cd9c8f9d 32 status = sip_idle;
klauss 0:4d17cd9c8f9d 33
klauss 0:4d17cd9c8f9d 34 __init_sock__();
klauss 0:4d17cd9c8f9d 35 }
klauss 0:4d17cd9c8f9d 36
klauss 0:4d17cd9c8f9d 37 Sip::Sip(){
klauss 0:4d17cd9c8f9d 38 this->id = __MY_EXT__;
klauss 0:4d17cd9c8f9d 39 strncpy( this->server_ip, __SEVER_IP__, 20 );
klauss 0:4d17cd9c8f9d 40 this->server_ip[19] = 0;
klauss 0:4d17cd9c8f9d 41 this->server_port = __SERVER_PORT__;
klauss 0:4d17cd9c8f9d 42 strncpy( this->my_ip, __MY_IP__, 20 );
klauss 0:4d17cd9c8f9d 43 this->my_ip[19] = 0;
klauss 0:4d17cd9c8f9d 44 this->my_port = __MY_PORT__;
klauss 0:4d17cd9c8f9d 45 this->my_rtp_port = fill_random_rtp_port();
klauss 0:4d17cd9c8f9d 46 this->my_ext = __MY_EXT__;
klauss 0:4d17cd9c8f9d 47 itoa( this->my_ext, this->my_display, 10 );
klauss 0:4d17cd9c8f9d 48 this->peer_ext = __PEER_EXT__;
klauss 0:4d17cd9c8f9d 49
klauss 0:4d17cd9c8f9d 50 strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
klauss 0:4d17cd9c8f9d 51 strcpy( this->last_invite_tag, "" );
klauss 0:4d17cd9c8f9d 52 strcpy( this->last_invite_callid, "");
klauss 0:4d17cd9c8f9d 53 strcpy( this->SVNREV, "COLA" );
klauss 0:4d17cd9c8f9d 54 status = sip_idle;
klauss 0:4d17cd9c8f9d 55
klauss 0:4d17cd9c8f9d 56 __init_sock__();
klauss 0:4d17cd9c8f9d 57 }
klauss 0:4d17cd9c8f9d 58
klauss 0:4d17cd9c8f9d 59 Sip::Sip( int id, uint16_t my_port ){
klauss 0:4d17cd9c8f9d 60 this->id = id;
klauss 0:4d17cd9c8f9d 61 strncpy( this->server_ip, __SEVER_IP__, 20 );
klauss 0:4d17cd9c8f9d 62 this->server_ip[19] = 0;
klauss 0:4d17cd9c8f9d 63 this->server_port = __SERVER_PORT__;
klauss 0:4d17cd9c8f9d 64 strncpy( this->my_ip, __MY_IP__, 20 );
klauss 0:4d17cd9c8f9d 65 this->my_ip[19] = 0;
klauss 0:4d17cd9c8f9d 66 this->my_port = my_port;
klauss 0:4d17cd9c8f9d 67 this->my_rtp_port = fill_random_rtp_port();
klauss 0:4d17cd9c8f9d 68 this->my_ext = id;
klauss 0:4d17cd9c8f9d 69 itoa( this->my_ext, this->my_display, 10 );
klauss 0:4d17cd9c8f9d 70 this->peer_ext = __PEER_EXT__;
klauss 0:4d17cd9c8f9d 71
klauss 0:4d17cd9c8f9d 72 strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
klauss 0:4d17cd9c8f9d 73 strcpy( this->last_invite_tag, "" );
klauss 0:4d17cd9c8f9d 74 strcpy( this->last_invite_callid, "");
klauss 0:4d17cd9c8f9d 75 strcpy( this->SVNREV, "COLA" );
klauss 0:4d17cd9c8f9d 76 status = sip_idle;
klauss 0:4d17cd9c8f9d 77
klauss 0:4d17cd9c8f9d 78 __init_sock__();
klauss 0:4d17cd9c8f9d 79 }
klauss 0:4d17cd9c8f9d 80
klauss 0:4d17cd9c8f9d 81 Sip::Sip( int id, uint16_t my_port, int dest_ext ){
klauss 0:4d17cd9c8f9d 82 this->id = id;
klauss 0:4d17cd9c8f9d 83 strncpy( this->server_ip, __SEVER_IP__, 20 );
klauss 0:4d17cd9c8f9d 84 this->server_ip[19] = 0;
klauss 0:4d17cd9c8f9d 85 this->server_port = __SERVER_PORT__;
klauss 0:4d17cd9c8f9d 86 strncpy( this->my_ip, __MY_IP__, 20 );
klauss 0:4d17cd9c8f9d 87 this->my_ip[19] = 0;
klauss 0:4d17cd9c8f9d 88 this->my_port = my_port;
klauss 0:4d17cd9c8f9d 89 this->my_rtp_port = fill_random_rtp_port();
klauss 0:4d17cd9c8f9d 90 this->my_ext = id;
klauss 0:4d17cd9c8f9d 91 itoa( this->my_ext, this->my_display, 10 );
klauss 0:4d17cd9c8f9d 92 this->peer_ext = dest_ext;
klauss 0:4d17cd9c8f9d 93
klauss 0:4d17cd9c8f9d 94 strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
klauss 0:4d17cd9c8f9d 95 strcpy( this->last_invite_tag, "" );
klauss 0:4d17cd9c8f9d 96 strcpy( this->last_invite_callid, "");
klauss 0:4d17cd9c8f9d 97 strcpy( this->SVNREV, "COLA" );
klauss 0:4d17cd9c8f9d 98 status = sip_idle;
klauss 0:4d17cd9c8f9d 99
klauss 0:4d17cd9c8f9d 100 __init_sock__();
klauss 0:4d17cd9c8f9d 101 }
klauss 0:4d17cd9c8f9d 102
klauss 0:4d17cd9c8f9d 103 Sip::~Sip(){
klauss 0:4d17cd9c8f9d 104 __end_sock__();
klauss 0:4d17cd9c8f9d 105 }
klauss 0:4d17cd9c8f9d 106
klauss 0:4d17cd9c8f9d 107 void Sip::registry(){
klauss 0:4d17cd9c8f9d 108 char out[ 1000 ];
klauss 0:4d17cd9c8f9d 109 build_registry_package( buffer );
klauss 0:4d17cd9c8f9d 110 sock.sendTo( sip_server, buffer, sizeof( buffer ) );
klauss 0:4d17cd9c8f9d 111 sock.receiveFrom( sip_server, buffer, sizeof( buffer ) );
klauss 0:4d17cd9c8f9d 112 if( decode_gettag( (unsigned char *)buffer, "cseq: ", out ) ){
klauss 0:4d17cd9c8f9d 113 if(strlen( out ) > 7){
klauss 0:4d17cd9c8f9d 114 if(strcasecmp("options",out+strlen(out)-7) == 0){
klauss 0:4d17cd9c8f9d 115 char ans[ 2000 ];
klauss 0:4d17cd9c8f9d 116 build_reply_package( ans, (unsigned char*)buffer );
klauss 0:4d17cd9c8f9d 117 sock.sendTo( sip_server, ans, sizeof( ans ) );
klauss 0:4d17cd9c8f9d 118 }
klauss 0:4d17cd9c8f9d 119 }
klauss 0:4d17cd9c8f9d 120 }
klauss 0:4d17cd9c8f9d 121 }
klauss 0:4d17cd9c8f9d 122
klauss 0:4d17cd9c8f9d 123 VZ_call * Sip::old_invite(){
klauss 0:4d17cd9c8f9d 124 Timer t;
klauss 0:4d17cd9c8f9d 125 char buffer[ 1024 ];
klauss 0:4d17cd9c8f9d 126 char callbox_string[ 32 ];
klauss 0:4d17cd9c8f9d 127 itoa( get_id(), callbox_string, 10 );
klauss 0:4d17cd9c8f9d 128 int cseq;
klauss 0:4d17cd9c8f9d 129
klauss 0:4d17cd9c8f9d 130 build_invite_package( buffer, callbox_string, &cseq );
klauss 0:4d17cd9c8f9d 131 sock.sendTo( sip_server, buffer, sizeof( buffer ) );
klauss 0:4d17cd9c8f9d 132
klauss 0:4d17cd9c8f9d 133 t.start();
klauss 0:4d17cd9c8f9d 134 while( sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ) < 1 ) {
klauss 0:4d17cd9c8f9d 135 if( t.read() > __INVITE_MAX_WAITING_TIME__ ) { /* Trying package */
klauss 0:4d17cd9c8f9d 136 change_sip_server();
klauss 0:4d17cd9c8f9d 137 t.reset();
klauss 0:4d17cd9c8f9d 138 break;
klauss 0:4d17cd9c8f9d 139 }
klauss 0:4d17cd9c8f9d 140 }
klauss 0:4d17cd9c8f9d 141 while( sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ) < 1 ) {
klauss 0:4d17cd9c8f9d 142 if( t.read() > __INVITE_MAX_WAITING_TIME__ ) { /* Ringing package */
klauss 0:4d17cd9c8f9d 143 change_sip_server();
klauss 0:4d17cd9c8f9d 144 t.reset();
klauss 0:4d17cd9c8f9d 145 break;
klauss 0:4d17cd9c8f9d 146 }
klauss 0:4d17cd9c8f9d 147 }
klauss 0:4d17cd9c8f9d 148 int call_trying = 1;
klauss 0:4d17cd9c8f9d 149 while( call_trying-- ){
klauss 0:4d17cd9c8f9d 150 while( sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ) < 1 ) {
klauss 0:4d17cd9c8f9d 151 if( t.read() > __INVITE_MAX_WAITING_TIME__ ) { /* ok package */
klauss 0:4d17cd9c8f9d 152 change_sip_server();
klauss 0:4d17cd9c8f9d 153 t.reset();
klauss 0:4d17cd9c8f9d 154 break;
klauss 0:4d17cd9c8f9d 155 }
klauss 0:4d17cd9c8f9d 156 }
klauss 0:4d17cd9c8f9d 157 wait( 3 );
klauss 0:4d17cd9c8f9d 158 char * ref = strstr( buffer, "audio" );
klauss 0:4d17cd9c8f9d 159 if( ref != NULL ){
klauss 0:4d17cd9c8f9d 160 ref += 6; /*audio */
klauss 0:4d17cd9c8f9d 161 ref = strtok( ref, " ");
klauss 0:4d17cd9c8f9d 162 VZ_call * call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
klauss 0:4d17cd9c8f9d 163 char ans[ 1024 ];
klauss 0:4d17cd9c8f9d 164 build_ack_package( ans, ( unsigned char *)buffer );
klauss 0:4d17cd9c8f9d 165 sock.sendTo( sip_server, ans, sizeof( ans ) );
klauss 0:4d17cd9c8f9d 166
klauss 0:4d17cd9c8f9d 167 return( call );
klauss 0:4d17cd9c8f9d 168 }
klauss 0:4d17cd9c8f9d 169 }
klauss 0:4d17cd9c8f9d 170 return( NULL );
klauss 0:4d17cd9c8f9d 171 }
klauss 0:4d17cd9c8f9d 172
klauss 0:4d17cd9c8f9d 173 VZ_call * Sip::invite(){
klauss 0:4d17cd9c8f9d 174 Timer t;
klauss 0:4d17cd9c8f9d 175 char callbox_string[ 32 ];
klauss 0:4d17cd9c8f9d 176 itoa( get_id(), callbox_string, 10 );
klauss 0:4d17cd9c8f9d 177
klauss 0:4d17cd9c8f9d 178 if( status != sip_on_call ) set_status(status, sip_waiting_trying );
klauss 0:4d17cd9c8f9d 179
klauss 0:4d17cd9c8f9d 180 else return( NULL );
klauss 0:4d17cd9c8f9d 181
klauss 0:4d17cd9c8f9d 182 int cseq = 0;
klauss 0:4d17cd9c8f9d 183
klauss 0:4d17cd9c8f9d 184 build_invite_package( buffer, callbox_string, &cseq );
klauss 0:4d17cd9c8f9d 185 sock.sendTo( sip_server, buffer, sizeof( buffer ) );
klauss 0:4d17cd9c8f9d 186
klauss 0:4d17cd9c8f9d 187 debug_msg( "Inicio do invite" );
klauss 0:4d17cd9c8f9d 188 debug_msg( "Sip::status::%d", status );
klauss 0:4d17cd9c8f9d 189 t.start();
klauss 0:4d17cd9c8f9d 190
klauss 0:4d17cd9c8f9d 191 sock.set_blocking( false, 5 );
klauss 0:4d17cd9c8f9d 192
klauss 0:4d17cd9c8f9d 193 char buffer_eth_invite[ 1024 ];
klauss 0:4d17cd9c8f9d 194 int loop_times = 0;
klauss 0:4d17cd9c8f9d 195 int received_loop_times = 0;
klauss 0:4d17cd9c8f9d 196 Timer reconnect;
klauss 0:4d17cd9c8f9d 197 reconnect.start();
klauss 0:4d17cd9c8f9d 198 static int id_ = 0;
klauss 0:4d17cd9c8f9d 199 int length = 0;
klauss 3:cd9148672e25 200 bool waiting = false;
klauss 3:cd9148672e25 201
klauss 3:cd9148672e25 202 VZ_call * call = NULL;
klauss 0:4d17cd9c8f9d 203 while( t.read() < __INVITE_MAX_WAITING_TIME__ ){
klauss 0:4d17cd9c8f9d 204 loop_times++;
klauss 0:4d17cd9c8f9d 205 length = sock.receiveFrom( sip_server, buffer_eth_invite, sizeof( buffer_eth_invite ) );
klauss 0:4d17cd9c8f9d 206 if( length > 0 ){
klauss 0:4d17cd9c8f9d 207 reconnect.reset();
klauss 0:4d17cd9c8f9d 208 debug_msg("Pacotes recebidos :: %d", id_ );
klauss 0:4d17cd9c8f9d 209 buffer_eth_invite[ length ] = 0;
klauss 0:4d17cd9c8f9d 210 debug_msg("-- %d :: %s --", id_++, buffer_eth_invite );
klauss 0:4d17cd9c8f9d 211 received_loop_times++;
klauss 0:4d17cd9c8f9d 212 if( ( status == sip_trying ) || ( status == sip_ringing ) ){
klauss 0:4d17cd9c8f9d 213 if( !( strncasecmp( buffer_eth_invite + 12, "ok", strlen("ok") ) ) ){
klauss 0:4d17cd9c8f9d 214 // tratar depois o esquema do Cseq ::
klauss 0:4d17cd9c8f9d 215 debug_msg( "Cseq == %d", cseq );
klauss 0:4d17cd9c8f9d 216 set_status(status, sip_on_call);
klauss 0:4d17cd9c8f9d 217 char *ref = strstr( buffer_eth_invite, "audio" );
klauss 0:4d17cd9c8f9d 218 debug_msg("ok");
klauss 0:4d17cd9c8f9d 219 if( ref != NULL ){
klauss 0:4d17cd9c8f9d 220 ref += 6; // audio
klauss 0:4d17cd9c8f9d 221 ref = strtok( ref, " ");
klauss 3:cd9148672e25 222 if( call == NULL ) call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
klauss 0:4d17cd9c8f9d 223 char ans[ 1024 ];
klauss 0:4d17cd9c8f9d 224 build_ack_package( ans, ( unsigned char *)buffer_eth_invite );
klauss 0:4d17cd9c8f9d 225 sock.sendTo( sip_server, ans, sizeof( ans ) );
klauss 1:a1758104fa1d 226 debug_msg("Call alocada -- Sip::status::%d -- loop_times :: %d :: received_loop_times :: %d", status, loop_times, received_loop_times );
klauss 0:4d17cd9c8f9d 227 set_status( status, sip_on_call );
klauss 0:4d17cd9c8f9d 228 return( call );
klauss 0:4d17cd9c8f9d 229 }
klauss 0:4d17cd9c8f9d 230 }
klauss 0:4d17cd9c8f9d 231 }
klauss 3:cd9148672e25 232 if( ( status == sip_trying ) || ( status == sip_ringing ) ){
klauss 3:cd9148672e25 233 if( !( strncasecmp( buffer_eth_invite + 12, "Session Progress", strlen("Session Progress") ) ) ){
klauss 3:cd9148672e25 234 char *ref = strstr( buffer_eth_invite, "audio" );
klauss 3:cd9148672e25 235 debug_msg( "Trocando dados de audio -- musica de espera com o CBx" );
klauss 3:cd9148672e25 236 if( ref != NULL ){
klauss 3:cd9148672e25 237 ref += 6; // audio
klauss 3:cd9148672e25 238 ref = strtok( ref, " ");
klauss 3:cd9148672e25 239 if( call == NULL ) call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
klauss 3:cd9148672e25 240 waiting = true;
klauss 3:cd9148672e25 241 }
klauss 3:cd9148672e25 242 }
klauss 3:cd9148672e25 243 }
klauss 0:4d17cd9c8f9d 244 if( status == sip_trying ){
klauss 0:4d17cd9c8f9d 245 if( !( strncasecmp( buffer_eth_invite + 12, "ringing", strlen("ringing") ) ) ){
klauss 0:4d17cd9c8f9d 246 debug_msg("ringing");
klauss 0:4d17cd9c8f9d 247 t.reset();
klauss 0:4d17cd9c8f9d 248 set_status( status, sip_ringing );
klauss 0:4d17cd9c8f9d 249 }
klauss 0:4d17cd9c8f9d 250 }
klauss 0:4d17cd9c8f9d 251 if( status == sip_waiting_trying ){
klauss 0:4d17cd9c8f9d 252 if( !( strncasecmp( buffer_eth_invite + 12, "trying", strlen("trying") ) ) ){
klauss 0:4d17cd9c8f9d 253 debug_msg("trying")
klauss 0:4d17cd9c8f9d 254 t.reset();
klauss 0:4d17cd9c8f9d 255 set_status( status, sip_trying );
klauss 0:4d17cd9c8f9d 256 }
klauss 0:4d17cd9c8f9d 257 }
klauss 0:4d17cd9c8f9d 258 }else if( reconnect.read() > 4 ){
klauss 0:4d17cd9c8f9d 259 sock.close();
klauss 0:4d17cd9c8f9d 260 sock.bind( this->my_port );
klauss 0:4d17cd9c8f9d 261 sock.init();
klauss 0:4d17cd9c8f9d 262 sock.set_blocking( false, 1 );
klauss 0:4d17cd9c8f9d 263 debug_msg("Reconnect");
klauss 0:4d17cd9c8f9d 264 reconnect.reset();
klauss 0:4d17cd9c8f9d 265 //sock.sendTo( sip_server, buffer, sizeof( buffer ) );
klauss 0:4d17cd9c8f9d 266 led4 = !led4;
klauss 0:4d17cd9c8f9d 267 }
klauss 3:cd9148672e25 268 if( waiting == true ){
klauss 3:cd9148672e25 269 char * tmp = call->get_eth_message( &length );
klauss 3:cd9148672e25 270 if( tmp != NULL ){
klauss 3:cd9148672e25 271 uint8_t * pkg2cb = __build_cb_package__( this->my_ext, this->my_port, __AUDIO__,
klauss 3:cd9148672e25 272 tmp, __AUDIO__, length, (uint8_t *)buffer );
klauss 3:cd9148672e25 273 __send_to_cb__( pkg2cb );
klauss 3:cd9148672e25 274 }
klauss 3:cd9148672e25 275 }
klauss 0:4d17cd9c8f9d 276 }
klauss 0:4d17cd9c8f9d 277
klauss 0:4d17cd9c8f9d 278 debug_msg("Call nao alocada -- Sip::status::%d -- loop_times :: %d :: received_loop_times :: %d", status, loop_times, received_loop_times );
klauss 0:4d17cd9c8f9d 279 if( t.read() > __INVITE_MAX_WAITING_TIME__ ){
klauss 0:4d17cd9c8f9d 280 set_status(status, sip_denied);
klauss 0:4d17cd9c8f9d 281 }
klauss 3:cd9148672e25 282 delete( call );
klauss 0:4d17cd9c8f9d 283 return( NULL );
klauss 0:4d17cd9c8f9d 284 }
klauss 0:4d17cd9c8f9d 285
klauss 0:4d17cd9c8f9d 286 char * Sip::build_registry_package( char * buffer ){
klauss 0:4d17cd9c8f9d 287 char branch[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 288 char tag[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 289 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 290 fill_random16h( branch );
klauss 0:4d17cd9c8f9d 291 fill_random( tag, 18 );
klauss 0:4d17cd9c8f9d 292 fill_random16h( callid );
klauss 0:4d17cd9c8f9d 293 char itoa_buffer[ 65 ];
klauss 0:4d17cd9c8f9d 294 strcpy( buffer, "REGISTER sip:" );
klauss 0:4d17cd9c8f9d 295 strcat( buffer, server_ip );
klauss 0:4d17cd9c8f9d 296 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 297 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 298 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 299 strcat( buffer, " SIP/2.0\r\nVia: SIP/2.0/UDP " );
klauss 0:4d17cd9c8f9d 300 strcat( buffer, my_ip );
klauss 0:4d17cd9c8f9d 301 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 302 itoa( my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 303 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 304 strcat( buffer, ";branch=" );
klauss 0:4d17cd9c8f9d 305 strcat( buffer, branch );
klauss 0:4d17cd9c8f9d 306 strcat( buffer, "\r\nFrom: " );
klauss 0:4d17cd9c8f9d 307 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 308 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 309 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 310 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 311 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 312 strcat( buffer, server_ip );
klauss 0:4d17cd9c8f9d 313 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 314 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 315 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 316 strcat( buffer, ">;tag=" );
klauss 0:4d17cd9c8f9d 317 strcat( buffer, tag );
klauss 0:4d17cd9c8f9d 318 strcat( buffer, "\r\nTo: " );
klauss 0:4d17cd9c8f9d 319 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 320 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 321 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 322 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 323 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 324 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 325 strcat( buffer, server_ip );
klauss 0:4d17cd9c8f9d 326 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 327 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 328 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 329 strcat( buffer, ">\r\nCall-ID: " );
klauss 0:4d17cd9c8f9d 330 strcat( buffer, callid );
klauss 0:4d17cd9c8f9d 331 strcat( buffer, "\r\nCSeq: " );
klauss 0:4d17cd9c8f9d 332 itoa( get_cseq(), itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 333 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 334 strcat( buffer, " REGISTER\r\n" );
klauss 0:4d17cd9c8f9d 335 strcat( buffer, "Max-Forwards: " );
klauss 0:4d17cd9c8f9d 336 itoa( 70, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 337 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 338 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 339
klauss 0:4d17cd9c8f9d 340 strcat( buffer, "Contact: " );
klauss 0:4d17cd9c8f9d 341 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 342 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 343 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 344 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 345 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 346 strcat( buffer, my_ip );
klauss 0:4d17cd9c8f9d 347 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 348 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 349 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 350 strcat( buffer, ">;expires=120" );
klauss 0:4d17cd9c8f9d 351 //itoa( 1200, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 352 //strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 353 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 354
klauss 0:4d17cd9c8f9d 355 strcat( buffer, "User-Agent: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 356 strcat( buffer, SVNREV );
klauss 0:4d17cd9c8f9d 357 strcat( buffer, "\r\nContent-Length: 0\r\n" );
klauss 0:4d17cd9c8f9d 358 strcat( buffer, SIP_ALLOW );
klauss 0:4d17cd9c8f9d 359 strcat( buffer, "\r\n\r\n" );
klauss 0:4d17cd9c8f9d 360 buffer[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 361 return buffer;
klauss 0:4d17cd9c8f9d 362 }
klauss 0:4d17cd9c8f9d 363
klauss 0:4d17cd9c8f9d 364 char * Sip::build_invite_package( char * pkg, char * callbox_string, int * cseq ){
klauss 0:4d17cd9c8f9d 365 char header[ SIP_MAXMSGSIZE ], body[ SIP_MAXMSGSIZE ];
klauss 0:4d17cd9c8f9d 366 char branch[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 367 char tag[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 368 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 369 char itoa_buffer[ 65 ];
klauss 0:4d17cd9c8f9d 370 *cseq = get_cseq();
klauss 0:4d17cd9c8f9d 371 fill_random16h( branch );
klauss 0:4d17cd9c8f9d 372 fill_random( tag,18 );
klauss 0:4d17cd9c8f9d 373 fill_random16h( callid );
klauss 0:4d17cd9c8f9d 374
klauss 0:4d17cd9c8f9d 375 strcpy( last_invite_tag,tag );
klauss 0:4d17cd9c8f9d 376 strcpy( last_invite_callid, callid );
klauss 0:4d17cd9c8f9d 377
klauss 0:4d17cd9c8f9d 378 strcpy( header, "INVITE sip:" );
klauss 0:4d17cd9c8f9d 379 itoa( this->peer_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 380 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 381 strcat( header, "@" );
klauss 0:4d17cd9c8f9d 382 strcat( header, this->server_ip );
klauss 0:4d17cd9c8f9d 383 strcat( header, ":" );
klauss 0:4d17cd9c8f9d 384 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 385 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 386 strcat( header, " SIP/" );
klauss 0:4d17cd9c8f9d 387 itoa( 2, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 388 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 389 strcat( header, "." );
klauss 0:4d17cd9c8f9d 390 itoa( 0, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 391 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 392 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 393 strcat( header, "Via: SIP/" );
klauss 0:4d17cd9c8f9d 394 itoa( 2, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 395 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 396 strcat( header, "." );
klauss 0:4d17cd9c8f9d 397 itoa( 0, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 398 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 399 strcat( header, "/UDP" );
klauss 0:4d17cd9c8f9d 400 strcat( header, this->my_ip );
klauss 0:4d17cd9c8f9d 401 strcat( header, ":" );
klauss 0:4d17cd9c8f9d 402 itoa( this->my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 403 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 404 strcat( header, ";branch=" );
klauss 0:4d17cd9c8f9d 405 strcat( header, branch );
klauss 0:4d17cd9c8f9d 406 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 407 strcat( header, "From: " );
klauss 0:4d17cd9c8f9d 408 strcat( header, callbox_string );
klauss 0:4d17cd9c8f9d 409 strcat( header, " <sip:" );
klauss 0:4d17cd9c8f9d 410 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 411 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 412 strcat( header, "@" );
klauss 0:4d17cd9c8f9d 413 strcat( header, this->server_ip );
klauss 0:4d17cd9c8f9d 414 strcat( header, ":" );
klauss 0:4d17cd9c8f9d 415 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 416 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 417 strcat( header, ">;tag=" );
klauss 0:4d17cd9c8f9d 418 strcat( header, tag );
klauss 0:4d17cd9c8f9d 419 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 420 strcat( header, "To: <sip:" );
klauss 0:4d17cd9c8f9d 421 itoa( this->peer_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 422 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 423 strcat( header, "@" );
klauss 0:4d17cd9c8f9d 424 strcat( header, this->server_ip );
klauss 0:4d17cd9c8f9d 425 strcat( header, ":" );
klauss 0:4d17cd9c8f9d 426 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 427 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 428 strcat( header, ">\r\n" );
klauss 0:4d17cd9c8f9d 429 strcat( header, "Call-ID: " );
klauss 0:4d17cd9c8f9d 430 strcat( header, callid );
klauss 0:4d17cd9c8f9d 431 strcat( header, "@" );
klauss 0:4d17cd9c8f9d 432 strcat( header, this->my_ip );
klauss 0:4d17cd9c8f9d 433 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 434 strcat( header, "CSeq: " );
klauss 0:4d17cd9c8f9d 435 itoa( *cseq, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 436 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 437 strcat( header, " INVITE\r\n" );
klauss 0:4d17cd9c8f9d 438 strcat( header, "Contact: " );
klauss 0:4d17cd9c8f9d 439 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 440 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 441 strcat( header, " <sip:" );
klauss 0:4d17cd9c8f9d 442 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 443 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 444 strcat( header, "@" );
klauss 0:4d17cd9c8f9d 445 strcat( header, this->my_ip );
klauss 0:4d17cd9c8f9d 446 strcat( header, ":" );
klauss 0:4d17cd9c8f9d 447 itoa( this->my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 448 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 449 strcat( header, ">\r\n" );
klauss 0:4d17cd9c8f9d 450 strcat( header, "Max-Forwards: " );
klauss 0:4d17cd9c8f9d 451 itoa( 20, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 452 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 453 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 454 strcat( header, "User-Agent: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 455 strcat( header, SVNREV );
klauss 0:4d17cd9c8f9d 456 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 457 strcat( header, "Expires: " );
klauss 0:4d17cd9c8f9d 458 itoa( 70, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 459 strcat( header, itoa_buffer );
klauss 0:4d17cd9c8f9d 460 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 461 strcat( header, SIP_ALLOW );
klauss 0:4d17cd9c8f9d 462 strcat( header, "\r\n" );
klauss 0:4d17cd9c8f9d 463 strcat( header, "Content-Type: application/sdp\r\n" );
klauss 0:4d17cd9c8f9d 464 header[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 465
klauss 0:4d17cd9c8f9d 466 strcpy( body, "v=0\r\n" );
klauss 0:4d17cd9c8f9d 467 strcat( body, "o=- " );
klauss 0:4d17cd9c8f9d 468 itoa( 7377, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 469 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 470 strcat( body, " " );
klauss 0:4d17cd9c8f9d 471 itoa( 18176, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 472 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 473 strcat( body, " IN IP" );
klauss 0:4d17cd9c8f9d 474 itoa( 4, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 475 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 476 strcat( body, " " );
klauss 0:4d17cd9c8f9d 477 strcat( body, this->my_ip );
klauss 0:4d17cd9c8f9d 478 strcat( body, "\r\n" );
klauss 0:4d17cd9c8f9d 479 strcat( body, "s=-\r\n" );
klauss 0:4d17cd9c8f9d 480 strcat( body, "c=IN IP" );
klauss 0:4d17cd9c8f9d 481 itoa( 4, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 482 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 483 strcat( body, " " );
klauss 0:4d17cd9c8f9d 484 strcat( body, this->my_ip );
klauss 0:4d17cd9c8f9d 485 strcat( body, "\r\n" );
klauss 0:4d17cd9c8f9d 486 strcat( body, "t=" );
klauss 0:4d17cd9c8f9d 487 itoa( 0, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 488 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 489 strcat( body, " " );
klauss 0:4d17cd9c8f9d 490 itoa( 0, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 491 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 492 strcat( body, "\r\n" );
klauss 0:4d17cd9c8f9d 493 strcat( body, "m=audio " );
klauss 0:4d17cd9c8f9d 494 itoa( this->my_rtp_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 495 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 496 strcat( body, " RTP/AVP " );
klauss 0:4d17cd9c8f9d 497 itoa( 8, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 498 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 499 strcat( body, " " );
klauss 0:4d17cd9c8f9d 500 itoa( 101, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 501 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 502 strcat( body, "\r\n" );
klauss 0:4d17cd9c8f9d 503 strcat( body, "a=rtpmap:" );
klauss 0:4d17cd9c8f9d 504 itoa( 8, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 505 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 506 strcat( body, " PCMA/" );
klauss 0:4d17cd9c8f9d 507 itoa( 8000, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 508 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 509 strcat( body, "/" );
klauss 0:4d17cd9c8f9d 510 itoa( 1, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 511 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 512 strcat( body, "\r\n" );
klauss 0:4d17cd9c8f9d 513 strcat( body, "a=rtpmap:" );
klauss 0:4d17cd9c8f9d 514 itoa( 101, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 515 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 516 strcat( body, " telephone-event/" );
klauss 0:4d17cd9c8f9d 517 itoa( 8000, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 518 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 519 strcat( body, "\r\n" );
klauss 0:4d17cd9c8f9d 520 strcat( body, "a=fmtp: " );
klauss 0:4d17cd9c8f9d 521 itoa( 101, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 522 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 523 strcat( body, " " );
klauss 0:4d17cd9c8f9d 524 itoa( 0, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 525 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 526 strcat( body, "-" );
klauss 0:4d17cd9c8f9d 527 itoa( 11, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 528 strcat( body, itoa_buffer );
klauss 0:4d17cd9c8f9d 529 body[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 530
klauss 0:4d17cd9c8f9d 531 make_content_length( header, body, pkg );
klauss 0:4d17cd9c8f9d 532 return( pkg );
klauss 0:4d17cd9c8f9d 533 }
klauss 0:4d17cd9c8f9d 534
klauss 0:4d17cd9c8f9d 535 char * Sip::make_content_length( char * header, char * body, char * pkg ){
klauss 0:4d17cd9c8f9d 536 int size;
klauss 0:4d17cd9c8f9d 537 char itoa_buffer[ 65 ];
klauss 0:4d17cd9c8f9d 538 size = strlen( body );
klauss 0:4d17cd9c8f9d 539 strcpy( pkg, header );
klauss 0:4d17cd9c8f9d 540 strcat( pkg, "Content-Length: " );
klauss 0:4d17cd9c8f9d 541 itoa( size, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 542 strcat( pkg, itoa_buffer );
klauss 0:4d17cd9c8f9d 543 strcat( pkg, "\r\n\r\n" );
klauss 0:4d17cd9c8f9d 544 strcat( pkg, body );
klauss 0:4d17cd9c8f9d 545 pkg[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 546 return pkg;
klauss 0:4d17cd9c8f9d 547 }
klauss 0:4d17cd9c8f9d 548
klauss 0:4d17cd9c8f9d 549 char * Sip::build_bye_package( char * buffer ){
klauss 0:4d17cd9c8f9d 550 char branch[SIP_MAXFIELDSIZE];
klauss 0:4d17cd9c8f9d 551 char tag[SIP_MAXFIELDSIZE];
klauss 0:4d17cd9c8f9d 552 char itoa_buffer[ 65 ];
klauss 0:4d17cd9c8f9d 553 fill_random16h(branch);
klauss 0:4d17cd9c8f9d 554 fill_random(tag,18);
klauss 0:4d17cd9c8f9d 555
klauss 0:4d17cd9c8f9d 556 strcpy( buffer, "BYE sip:" );
klauss 0:4d17cd9c8f9d 557 itoa( this->peer_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 558 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 559 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 560 strcat( buffer, this->server_ip );
klauss 0:4d17cd9c8f9d 561 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 562 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 563 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 564 strcat( buffer, " SIP/2.0\r\n" );
klauss 0:4d17cd9c8f9d 565 strcat( buffer, "Via: SIP/2.0/UDP " );
klauss 0:4d17cd9c8f9d 566 strcat( buffer, this->my_ip );
klauss 0:4d17cd9c8f9d 567 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 568 itoa( this->my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 569 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 570 strcat( buffer, ";branch=" );
klauss 0:4d17cd9c8f9d 571 strcat( buffer, branch );
klauss 0:4d17cd9c8f9d 572 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 573 strcat( buffer, "From: " );
klauss 0:4d17cd9c8f9d 574 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 575 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 576 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 577 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 578 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 579 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 580 strcat( buffer, this->server_ip );
klauss 0:4d17cd9c8f9d 581 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 582 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 583 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 584 strcat( buffer, ">;tag=" );
klauss 0:4d17cd9c8f9d 585 strcat( buffer, last_invite_tag );
klauss 0:4d17cd9c8f9d 586 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 587 strcat( buffer, "To: <sip:" );
klauss 0:4d17cd9c8f9d 588 itoa( this->peer_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 589 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 590 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 591 strcat( buffer, this->server_ip );
klauss 0:4d17cd9c8f9d 592 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 593 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 594 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 595 strcat( buffer, ">;tag=" );
klauss 0:4d17cd9c8f9d 596 strcat( buffer, tag );
klauss 0:4d17cd9c8f9d 597 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 598 strcat( buffer, "Call-ID: " );
klauss 0:4d17cd9c8f9d 599 strcat( buffer, last_invite_callid );
klauss 0:4d17cd9c8f9d 600 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 601 strcat( buffer, this->my_ip );
klauss 0:4d17cd9c8f9d 602 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 603 strcat( buffer, "CSeq: " );
klauss 0:4d17cd9c8f9d 604 itoa( get_cseq(), itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 605 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 606 strcat( buffer, " BYE\r\n" );
klauss 0:4d17cd9c8f9d 607 strcat( buffer, "Max-Forwards: 70\r\n" );
klauss 0:4d17cd9c8f9d 608 strcat( buffer, "User-Agent: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 609 strcat( buffer, SVNREV );
klauss 0:4d17cd9c8f9d 610 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 611 strcat( buffer, "Content-Length: 0\r\n\r\n" );
klauss 0:4d17cd9c8f9d 612 return( buffer );
klauss 0:4d17cd9c8f9d 613 }
klauss 0:4d17cd9c8f9d 614 char * Sip::build_ack_package( char * buffer, unsigned char * orig ){
klauss 0:4d17cd9c8f9d 615 char to[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 616 char from[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 617 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 618 char via[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 619 char branch[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 620 char cseq[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 621 char itoa_buffer[ 65 ];
klauss 0:4d17cd9c8f9d 622 // FIXME confirmar se nao preciso converter todos os ints com itoa
klauss 0:4d17cd9c8f9d 623 if( decode_gettag( orig, "to: ", to ) == 0) {
klauss 0:4d17cd9c8f9d 624 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 625 return NULL;
klauss 0:4d17cd9c8f9d 626 }
klauss 0:4d17cd9c8f9d 627 if( decode_gettag( orig, "from: ", from ) == 0) {
klauss 0:4d17cd9c8f9d 628 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 629 return NULL;
klauss 0:4d17cd9c8f9d 630 }
klauss 0:4d17cd9c8f9d 631 if( decode_gettag( orig, "call-id: ", callid ) == 0) {
klauss 0:4d17cd9c8f9d 632 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 633 return NULL;
klauss 0:4d17cd9c8f9d 634 }
klauss 0:4d17cd9c8f9d 635 decode_branch( orig, branch );
klauss 0:4d17cd9c8f9d 636 decode_cseq( orig, cseq );
klauss 0:4d17cd9c8f9d 637
klauss 0:4d17cd9c8f9d 638 if( strlen( via ) > 6 ) {
klauss 0:4d17cd9c8f9d 639 if( strcasecmp( ";rport", via+strlen( via )-6) == 0) {
klauss 0:4d17cd9c8f9d 640 via[strlen(via)-6] = 0;
klauss 0:4d17cd9c8f9d 641 }
klauss 0:4d17cd9c8f9d 642 }
klauss 0:4d17cd9c8f9d 643 strcpy( buffer, "ACK sip:" );
klauss 0:4d17cd9c8f9d 644 itoa( this->peer_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 645 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 646 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 647 strcat( buffer, this->server_ip );
klauss 0:4d17cd9c8f9d 648 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 649 itoa( this->server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 650 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 651 strcat( buffer, " SIP/2.0\r\n" );
klauss 0:4d17cd9c8f9d 652 strcat( buffer, "Via: SIP/2.0/UDP " );
klauss 0:4d17cd9c8f9d 653 strcat( buffer, this->my_ip );
klauss 0:4d17cd9c8f9d 654 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 655 itoa( this->my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 656 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 657 strcat( buffer, ";branch=" );
klauss 0:4d17cd9c8f9d 658 strcat( buffer, branch );
klauss 0:4d17cd9c8f9d 659 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 660 strcat( buffer, "From: " );
klauss 0:4d17cd9c8f9d 661 strcat( buffer, from );
klauss 0:4d17cd9c8f9d 662 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 663 strcat( buffer, "To: " );
klauss 0:4d17cd9c8f9d 664 strcat( buffer, to );
klauss 0:4d17cd9c8f9d 665 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 666 strcat( buffer, "Call-ID: " );
klauss 0:4d17cd9c8f9d 667 strcat( buffer, callid );
klauss 0:4d17cd9c8f9d 668 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 669 strcat( buffer, "CSeq: " );
klauss 0:4d17cd9c8f9d 670 strcat( buffer, cseq );
klauss 0:4d17cd9c8f9d 671 strcat( buffer, " ACK\r\n" );
klauss 0:4d17cd9c8f9d 672 strcat( buffer, "Max-Forwards: 70\r\n" );
klauss 0:4d17cd9c8f9d 673 strcat( buffer, "Contact: " );
klauss 0:4d17cd9c8f9d 674 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 675 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 676 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 677 itoa( this->my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 678 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 679 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 680 strcat( buffer, this->my_ip );
klauss 0:4d17cd9c8f9d 681 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 682 itoa( this->my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 683 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 684 strcat( buffer, ">\r\n" );
klauss 0:4d17cd9c8f9d 685 strcat( buffer, "User-Agent: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 686 strcat( buffer, SVNREV );
klauss 0:4d17cd9c8f9d 687 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 688 strcat( buffer, "Content-Length: 0\r\n\r\n" );
klauss 0:4d17cd9c8f9d 689 buffer[SIP_MAXMSGSIZE-1] = 0;
klauss 0:4d17cd9c8f9d 690 return( buffer );
klauss 0:4d17cd9c8f9d 691 }
klauss 0:4d17cd9c8f9d 692
klauss 0:4d17cd9c8f9d 693 int Sip::get_return_code( char * buffer ){
klauss 0:4d17cd9c8f9d 694 char a[32];
klauss 0:4d17cd9c8f9d 695 int i = 0, j;
klauss 0:4d17cd9c8f9d 696 if( strncmp( "SIP/", buffer, 4 ) != 0 ) return 0;
klauss 0:4d17cd9c8f9d 697 if( strlen( buffer ) < 32) return 0;
klauss 0:4d17cd9c8f9d 698 strncpy( a, buffer, 31 );
klauss 0:4d17cd9c8f9d 699 a[ 31 ] = 0;
klauss 0:4d17cd9c8f9d 700 while ( a[ i ] > ' ') i++;
klauss 0:4d17cd9c8f9d 701 j = i+i;
klauss 0:4d17cd9c8f9d 702 while( (a[ j ] >= '0') && ( a[ j ] <= '9') ) j++;
klauss 0:4d17cd9c8f9d 703 a[ j ] = 0;
klauss 0:4d17cd9c8f9d 704 return atoi( a + i + 1 );
klauss 0:4d17cd9c8f9d 705 }
klauss 0:4d17cd9c8f9d 706
klauss 0:4d17cd9c8f9d 707 char * Sip::build_generic_reply_package(char * buffer, unsigned char * orig, char * tag){
klauss 0:4d17cd9c8f9d 708 char to[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 709 char from[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 710 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 711 char cseq[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 712 char via[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 713
klauss 0:4d17cd9c8f9d 714 if( decode_gettag( orig, "to: ", to ) == 0) {
klauss 0:4d17cd9c8f9d 715 buffer[0] = 0;
klauss 0:4d17cd9c8f9d 716 return NULL;
klauss 0:4d17cd9c8f9d 717 }
klauss 0:4d17cd9c8f9d 718 if( decode_gettag( orig, "from: ", from ) == 0) {
klauss 0:4d17cd9c8f9d 719 buffer[0] = 0;
klauss 0:4d17cd9c8f9d 720 return NULL;
klauss 0:4d17cd9c8f9d 721 }
klauss 0:4d17cd9c8f9d 722 if( decode_gettag( orig, "call-id: ", callid ) == 0) {
klauss 0:4d17cd9c8f9d 723 buffer[0] = 0;
klauss 0:4d17cd9c8f9d 724 return NULL;
klauss 0:4d17cd9c8f9d 725 }
klauss 0:4d17cd9c8f9d 726 if( decode_gettag( orig, "cseq: ", cseq ) == 0) {
klauss 0:4d17cd9c8f9d 727 buffer[0] = 0;
klauss 0:4d17cd9c8f9d 728 return NULL;
klauss 0:4d17cd9c8f9d 729 }
klauss 0:4d17cd9c8f9d 730 if( decode_gettag( orig, "via: ", via ) == 0) {
klauss 0:4d17cd9c8f9d 731 buffer[0] = 0;
klauss 0:4d17cd9c8f9d 732 return NULL;
klauss 0:4d17cd9c8f9d 733 }
klauss 0:4d17cd9c8f9d 734
klauss 0:4d17cd9c8f9d 735 if (strlen(via) > 6) {
klauss 0:4d17cd9c8f9d 736 if (strcasecmp(";rport",via+strlen(via)-6)==0) {
klauss 0:4d17cd9c8f9d 737 via[strlen(via)-6] = 0;
klauss 0:4d17cd9c8f9d 738 }
klauss 0:4d17cd9c8f9d 739 }
klauss 0:4d17cd9c8f9d 740
klauss 0:4d17cd9c8f9d 741 strcpy( buffer, "SIP/2.0 " );
klauss 0:4d17cd9c8f9d 742 strcat( buffer, tag );
klauss 0:4d17cd9c8f9d 743 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 744 strcat( buffer, "To: " );
klauss 0:4d17cd9c8f9d 745 strcat( buffer, to );
klauss 0:4d17cd9c8f9d 746 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 747 strcat( buffer, "From: " );
klauss 0:4d17cd9c8f9d 748 strcat( buffer, from );
klauss 0:4d17cd9c8f9d 749 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 750 strcat( buffer, "Call-ID: " );
klauss 0:4d17cd9c8f9d 751 strcat( buffer, callid );
klauss 0:4d17cd9c8f9d 752 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 753 strcat( buffer, "CSeq: " );
klauss 0:4d17cd9c8f9d 754 strcat( buffer, cseq );
klauss 0:4d17cd9c8f9d 755 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 756 strcat( buffer, "Via: " );
klauss 0:4d17cd9c8f9d 757 strcat( buffer, via );
klauss 0:4d17cd9c8f9d 758 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 759 strcat( buffer, "Server: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 760 strcat( buffer, SVNREV );
klauss 0:4d17cd9c8f9d 761 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 762 strcat( buffer, "Content-Length: 0\r\n\r\n" );
klauss 0:4d17cd9c8f9d 763 buffer[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 764 return( buffer );
klauss 0:4d17cd9c8f9d 765 }
klauss 0:4d17cd9c8f9d 766 char * Sip::build_trying_package( char * buffer, unsigned char * orig ){
klauss 0:4d17cd9c8f9d 767 // FIXME trocar 100 para itoa( 100 ... )
klauss 0:4d17cd9c8f9d 768 build_generic_reply_package( buffer, orig, "100 Trying" );
klauss 0:4d17cd9c8f9d 769 return( buffer );
klauss 0:4d17cd9c8f9d 770 }
klauss 0:4d17cd9c8f9d 771
klauss 0:4d17cd9c8f9d 772 char * Sip::build_busy_package( char * buffer, unsigned char * orig ){
klauss 0:4d17cd9c8f9d 773 // FIXME trocar 100 para itoa( 100 ... )
klauss 0:4d17cd9c8f9d 774 build_generic_reply_package( buffer, orig, "486 Busy Here");
klauss 0:4d17cd9c8f9d 775 return( buffer );
klauss 0:4d17cd9c8f9d 776 }
klauss 0:4d17cd9c8f9d 777 char * Sip::build_reply_package( char * buffer, unsigned char * orig ){
klauss 0:4d17cd9c8f9d 778 char to[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 779 char from[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 780 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 781 char cseq[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 782 char via[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 783 char tag[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 784
klauss 0:4d17cd9c8f9d 785 if( decode_gettag( orig, "to: ", to ) == 0) {
klauss 0:4d17cd9c8f9d 786 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 787 return NULL;
klauss 0:4d17cd9c8f9d 788 }
klauss 0:4d17cd9c8f9d 789 if( decode_gettag( orig, "from: ", from ) == 0) {
klauss 0:4d17cd9c8f9d 790 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 791 return NULL;
klauss 0:4d17cd9c8f9d 792 }
klauss 0:4d17cd9c8f9d 793 if( decode_gettag( orig, "call-id: ", callid ) == 0 ) {
klauss 0:4d17cd9c8f9d 794 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 795 return NULL;
klauss 0:4d17cd9c8f9d 796 }
klauss 0:4d17cd9c8f9d 797 if( decode_gettag( orig, "cseq: ", cseq) == 0 ) {
klauss 0:4d17cd9c8f9d 798 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 799 return NULL;
klauss 0:4d17cd9c8f9d 800 }
klauss 0:4d17cd9c8f9d 801 if( decode_gettag( orig, "via: ", via) == 0 ) {
klauss 0:4d17cd9c8f9d 802 buffer[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 803 return NULL;
klauss 0:4d17cd9c8f9d 804 }
klauss 0:4d17cd9c8f9d 805
klauss 0:4d17cd9c8f9d 806 if (strlen(via) > 6) {
klauss 0:4d17cd9c8f9d 807 if (strcasecmp(";rport",via+strlen(via)-6)==0) {
klauss 0:4d17cd9c8f9d 808 via[strlen(via)-6] = 0;
klauss 0:4d17cd9c8f9d 809 }
klauss 0:4d17cd9c8f9d 810 }
klauss 0:4d17cd9c8f9d 811 fill_random(tag,18);
klauss 0:4d17cd9c8f9d 812
klauss 0:4d17cd9c8f9d 813 strcpy( buffer, "SIP/2.0 200 OK\r\n" );
klauss 0:4d17cd9c8f9d 814 strcat( buffer, "To: " );
klauss 0:4d17cd9c8f9d 815 strcat( buffer, to );
klauss 0:4d17cd9c8f9d 816 strcat( buffer, ";tag=" );
klauss 0:4d17cd9c8f9d 817 strcat( buffer, tag );
klauss 0:4d17cd9c8f9d 818 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 819
klauss 0:4d17cd9c8f9d 820 strcat( buffer, "From: " );
klauss 0:4d17cd9c8f9d 821 strcat( buffer, from );
klauss 0:4d17cd9c8f9d 822 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 823
klauss 0:4d17cd9c8f9d 824 strcat( buffer, "Call-ID: " );
klauss 0:4d17cd9c8f9d 825 strcat( buffer, callid );
klauss 0:4d17cd9c8f9d 826 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 827
klauss 0:4d17cd9c8f9d 828 strcat( buffer, "CSeq: " );
klauss 0:4d17cd9c8f9d 829 strcat( buffer, cseq );
klauss 0:4d17cd9c8f9d 830 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 831
klauss 0:4d17cd9c8f9d 832 strcat( buffer, "Via: " );
klauss 0:4d17cd9c8f9d 833 strcat( buffer, via );
klauss 0:4d17cd9c8f9d 834 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 835
klauss 0:4d17cd9c8f9d 836 strcat( buffer, "Server: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 837 strcat( buffer, SVNREV );
klauss 0:4d17cd9c8f9d 838 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 839 strcat( buffer, "Content-Length: 0\r\n" );
klauss 0:4d17cd9c8f9d 840 strcat( buffer, SIP_ALLOW );
klauss 0:4d17cd9c8f9d 841 strcat( buffer, "\r\n\r\n" );
klauss 0:4d17cd9c8f9d 842
klauss 0:4d17cd9c8f9d 843 buffer[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 844 return( buffer );
klauss 0:4d17cd9c8f9d 845 }
klauss 0:4d17cd9c8f9d 846 char * Sip::fill_random16h(char * buffer ){
klauss 0:4d17cd9c8f9d 847 fill_random( buffer, 16 );
klauss 0:4d17cd9c8f9d 848 buffer[ 7 ] = '-';
klauss 0:4d17cd9c8f9d 849 return( buffer );
klauss 0:4d17cd9c8f9d 850 }
klauss 0:4d17cd9c8f9d 851
klauss 0:4d17cd9c8f9d 852 char * Sip::fill_random( char * buffer, int size ){
klauss 0:4d17cd9c8f9d 853 static uint16_t seed = time( NULL );
klauss 0:4d17cd9c8f9d 854 seed += 1;
klauss 0:4d17cd9c8f9d 855 srand( seed );
klauss 0:4d17cd9c8f9d 856
klauss 0:4d17cd9c8f9d 857 int i;
klauss 0:4d17cd9c8f9d 858 for (i=0; i<size; i++) {
klauss 0:4d17cd9c8f9d 859 buffer[ i ] = fill_random_aux[ rand() & 0x3f ];
klauss 0:4d17cd9c8f9d 860 }
klauss 0:4d17cd9c8f9d 861 buffer[ size ] = 0;
klauss 0:4d17cd9c8f9d 862 return( buffer );
klauss 0:4d17cd9c8f9d 863 }
klauss 0:4d17cd9c8f9d 864
klauss 0:4d17cd9c8f9d 865 int Sip::get_cseq( void ){
klauss 0:4d17cd9c8f9d 866 static unsigned int cseq = 0;
klauss 0:4d17cd9c8f9d 867 if( cseq == 0 ) {
klauss 0:4d17cd9c8f9d 868 /* initial value, random number */
klauss 0:4d17cd9c8f9d 869 cseq = rand();
klauss 0:4d17cd9c8f9d 870 cseq &= 0x7fff;
klauss 0:4d17cd9c8f9d 871 }
klauss 0:4d17cd9c8f9d 872 cseq++;
klauss 0:4d17cd9c8f9d 873 cseq &= 0x7fff;
klauss 0:4d17cd9c8f9d 874 if( cseq == 0 ) cseq++;
klauss 0:4d17cd9c8f9d 875 return cseq;
klauss 0:4d17cd9c8f9d 876 }
klauss 0:4d17cd9c8f9d 877
klauss 0:4d17cd9c8f9d 878 char * Sip::decode_cseq( unsigned char * package, char * cseq ){
klauss 0:4d17cd9c8f9d 879 char pkg[ 2048 ], cs[ 2048 ];
klauss 0:4d17cd9c8f9d 880 if( decode_gettag( package, "cseq: ", pkg ) != 0) {
klauss 0:4d17cd9c8f9d 881 int i = 0;
klauss 0:4d17cd9c8f9d 882 strcpy( cs, pkg );
klauss 0:4d17cd9c8f9d 883 while( ( cs[ i ] != 0 ) && ( cs[ i ] != ' ' ) ) i++;
klauss 0:4d17cd9c8f9d 884 cs[ i ] = 0;
klauss 0:4d17cd9c8f9d 885 strcpy( cseq, cs );
klauss 0:4d17cd9c8f9d 886 } else {
klauss 0:4d17cd9c8f9d 887 cseq[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 888 }
klauss 0:4d17cd9c8f9d 889 return( cseq );
klauss 0:4d17cd9c8f9d 890 }
klauss 0:4d17cd9c8f9d 891
klauss 0:4d17cd9c8f9d 892 char * Sip::decode_branch( unsigned char * package, char * branch ){
klauss 0:4d17cd9c8f9d 893 char pkg[ 2048 ];
klauss 0:4d17cd9c8f9d 894 if( decode_gettag( package, "branch=", pkg ) != 0 ) {
klauss 0:4d17cd9c8f9d 895 int i = 0;
klauss 0:4d17cd9c8f9d 896 strcpy( branch, pkg );
klauss 0:4d17cd9c8f9d 897 while( ( branch[ i ] != 0 ) && ( branch[ i ] != ';' ) ) i++;
klauss 0:4d17cd9c8f9d 898 branch[ i ] = 0;
klauss 0:4d17cd9c8f9d 899 } else {
klauss 0:4d17cd9c8f9d 900 branch[ 0 ] = 0;
klauss 0:4d17cd9c8f9d 901 }
klauss 0:4d17cd9c8f9d 902 return( branch );
klauss 0:4d17cd9c8f9d 903 }
klauss 0:4d17cd9c8f9d 904 int Sip::decode_gettag( unsigned char * package, char * tag, char * out ){
klauss 0:4d17cd9c8f9d 905 int size, i, cmpsize;
klauss 0:4d17cd9c8f9d 906 cmpsize = strlen( tag );
klauss 0:4d17cd9c8f9d 907 size = strlen( ( char * )package )-cmpsize;
klauss 0:4d17cd9c8f9d 908 for ( i=0; i<size; i++) {
klauss 0:4d17cd9c8f9d 909 if( strncasecmp( tag, (char*)package+i, cmpsize ) == 0) {
klauss 0:4d17cd9c8f9d 910 char * s;
klauss 0:4d17cd9c8f9d 911 s = (char*)package+i+cmpsize;
klauss 0:4d17cd9c8f9d 912 register int j = 0;
klauss 0:4d17cd9c8f9d 913 while ((s[ j ] != '\r') && (s[ j ] != '\n')) j++;
klauss 0:4d17cd9c8f9d 914 strncpy( out, s, j ); // copy string to output
klauss 0:4d17cd9c8f9d 915 out[ j ] = 0; // terminate string
klauss 0:4d17cd9c8f9d 916 return 1; // found
klauss 0:4d17cd9c8f9d 917 }
klauss 0:4d17cd9c8f9d 918 }
klauss 0:4d17cd9c8f9d 919 return 0; // not found
klauss 0:4d17cd9c8f9d 920 }
klauss 0:4d17cd9c8f9d 921
klauss 0:4d17cd9c8f9d 922 int Sip::get_id( void ){
klauss 0:4d17cd9c8f9d 923 return this->id;
klauss 0:4d17cd9c8f9d 924 }
klauss 0:4d17cd9c8f9d 925 int Sip::get_my_rtp_port( void ){
klauss 0:4d17cd9c8f9d 926 return this->my_rtp_port;
klauss 0:4d17cd9c8f9d 927 }
klauss 0:4d17cd9c8f9d 928 inline int Sip::fill_random_rtp_port(){
klauss 0:4d17cd9c8f9d 929 // o range [ 16384 - 32767 ] eh configuravel no server
klauss 0:4d17cd9c8f9d 930 return ( ( this->my_ext + 16384 ) > 32767 ) ? ( ( this->my_ext + 16384 ) % ( 32767 - 16384 ) + 16384 ) : this->my_ext + 16384;
klauss 0:4d17cd9c8f9d 931 }
klauss 0:4d17cd9c8f9d 932
klauss 0:4d17cd9c8f9d 933 void Sip::change_sip_server(){
klauss 0:4d17cd9c8f9d 934 // just in case sip server don't answer, invite another server
klauss 0:4d17cd9c8f9d 935 // mudar para os sets
klauss 0:4d17cd9c8f9d 936 set_server_ip( get_next_server_ip( this->buffer) );
klauss 0:4d17cd9c8f9d 937 set_server_ext( get_next_server_ext() );
klauss 0:4d17cd9c8f9d 938 set_server_port( get_next_server_port() );
klauss 0:4d17cd9c8f9d 939 registry();
klauss 0:4d17cd9c8f9d 940 }
klauss 0:4d17cd9c8f9d 941
klauss 0:4d17cd9c8f9d 942 char * Sip::get_next_server_ip( char * server_ip ){
klauss 0:4d17cd9c8f9d 943 strcpy( server_ip, this->server_ip );
klauss 0:4d17cd9c8f9d 944 return server_ip;
klauss 0:4d17cd9c8f9d 945 }
klauss 0:4d17cd9c8f9d 946 int Sip::get_next_server_port(){
klauss 0:4d17cd9c8f9d 947 return this->server_port;
klauss 0:4d17cd9c8f9d 948 }
klauss 0:4d17cd9c8f9d 949 int Sip::get_next_server_ext(){
klauss 0:4d17cd9c8f9d 950 return this->peer_ext;
klauss 0:4d17cd9c8f9d 951 }
klauss 0:4d17cd9c8f9d 952 void Sip::set_server_port( int new_server_port ){
klauss 0:4d17cd9c8f9d 953 this->server_port = new_server_port;
klauss 0:4d17cd9c8f9d 954 }
klauss 0:4d17cd9c8f9d 955 void Sip::set_server_ext( int new_server_ext ){
klauss 0:4d17cd9c8f9d 956 this->peer_ext = new_server_ext;
klauss 0:4d17cd9c8f9d 957 }
klauss 0:4d17cd9c8f9d 958 void Sip::set_server_ip( char * new_server_ip ){
klauss 0:4d17cd9c8f9d 959 strcpy( this->server_ip, new_server_ip );
klauss 0:4d17cd9c8f9d 960 this->server_ip[19] = 0;
klauss 0:4d17cd9c8f9d 961 }
klauss 0:4d17cd9c8f9d 962
klauss 0:4d17cd9c8f9d 963 /* Retorna
klauss 0:4d17cd9c8f9d 964 = 0 :: ok
klauss 0:4d17cd9c8f9d 965 < 0 :: tive problemas
klauss 0:4d17cd9c8f9d 966 > 0 :: devo remover essa call do vetor de calls
klauss 0:4d17cd9c8f9d 967 */
klauss 0:4d17cd9c8f9d 968 int Sip::listen_SIP_server( void ){
klauss 0:4d17cd9c8f9d 969 sock.set_blocking( false, 3 ); // verificar isso com mais cuidado depois
klauss 0:4d17cd9c8f9d 970 uint8_t read = sock.receiveFrom( sip_server, this->buffer, sizeof( this->buffer ) );
klauss 0:4d17cd9c8f9d 971 if( read > 0 ){
klauss 0:4d17cd9c8f9d 972 debug_msg( "-- SIP recebeu algo --" );
klauss 0:4d17cd9c8f9d 973 if( !( strncasecmp( buffer, "bye", strlen("bye") ) ) ){
klauss 0:4d17cd9c8f9d 974 char ans[ 2000 ];
klauss 0:4d17cd9c8f9d 975 build_reply_package( ans, (unsigned char*)buffer );
klauss 0:4d17cd9c8f9d 976 sock.sendTo( sip_server, ans, sizeof( ans ) );
klauss 0:4d17cd9c8f9d 977
klauss 0:4d17cd9c8f9d 978 debug_msg(" Server request BYE to %d ext", my_ext );
klauss 0:4d17cd9c8f9d 979 return( this->my_ext );
klauss 0:4d17cd9c8f9d 980 }else if( !( strncasecmp( buffer, "options", strlen("options") ) ) ){
klauss 0:4d17cd9c8f9d 981 char ans[ 2000 ];
klauss 0:4d17cd9c8f9d 982 debug_msg(" Options received ");
klauss 0:4d17cd9c8f9d 983 build_reply_package( ans, (unsigned char*)buffer );
klauss 0:4d17cd9c8f9d 984 sock.sendTo( sip_server, ans, sizeof( ans ) );
klauss 0:4d17cd9c8f9d 985 }
klauss 0:4d17cd9c8f9d 986 }
klauss 0:4d17cd9c8f9d 987 return( 0 );
klauss 0:4d17cd9c8f9d 988 }
klauss 0:4d17cd9c8f9d 989
klauss 0:4d17cd9c8f9d 990 void Sip::set_ext( int ext ){
klauss 0:4d17cd9c8f9d 991 my_ext = ext;
klauss 0:4d17cd9c8f9d 992 }
klauss 0:4d17cd9c8f9d 993
klauss 0:4d17cd9c8f9d 994 void Sip::set_port( int port ){
klauss 0:4d17cd9c8f9d 995 my_port = port;
klauss 0:4d17cd9c8f9d 996 }
klauss 0:4d17cd9c8f9d 997
klauss 0:4d17cd9c8f9d 998 void Sip::send_bye( void ){
klauss 0:4d17cd9c8f9d 999 build_bye_package( this->buffer );
klauss 0:4d17cd9c8f9d 1000 sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) );
klauss 0:4d17cd9c8f9d 1001 }
klauss 0:4d17cd9c8f9d 1002
klauss 0:4d17cd9c8f9d 1003 void Sip::send_unregistry_pkg(){
klauss 0:4d17cd9c8f9d 1004 build_unregistry_package( this->buffer );
klauss 0:4d17cd9c8f9d 1005 for( register int i = 0; i < 7; i++ )
klauss 0:4d17cd9c8f9d 1006 sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) );
klauss 0:4d17cd9c8f9d 1007 }
klauss 0:4d17cd9c8f9d 1008
klauss 0:4d17cd9c8f9d 1009 char * Sip::build_unregistry_package( char * buffer ){
klauss 0:4d17cd9c8f9d 1010 char branch[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 1011 char tag[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 1012 char callid[ SIP_MAXFIELDSIZE ];
klauss 0:4d17cd9c8f9d 1013 fill_random16h( branch );
klauss 0:4d17cd9c8f9d 1014 fill_random( tag, 18 );
klauss 0:4d17cd9c8f9d 1015 fill_random16h( callid );
klauss 0:4d17cd9c8f9d 1016 char itoa_buffer[ 65 ];
klauss 0:4d17cd9c8f9d 1017 strcpy( buffer, "REGISTER sip:" );
klauss 0:4d17cd9c8f9d 1018 strcat( buffer, server_ip );
klauss 0:4d17cd9c8f9d 1019 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 1020 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1021 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1022 strcat( buffer, " SIP/2.0\r\nVia: SIP/2.0/UDP " );
klauss 0:4d17cd9c8f9d 1023 strcat( buffer, my_ip );
klauss 0:4d17cd9c8f9d 1024 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 1025 itoa( my_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1026 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1027 strcat( buffer, ";branch=" );
klauss 0:4d17cd9c8f9d 1028 strcat( buffer, branch );
klauss 0:4d17cd9c8f9d 1029 strcat( buffer, "\r\nFrom: " );
klauss 0:4d17cd9c8f9d 1030 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1031 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1032 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 1033 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1034 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 1035 strcat( buffer, server_ip );
klauss 0:4d17cd9c8f9d 1036 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 1037 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1038 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1039 strcat( buffer, ">;tag=" );
klauss 0:4d17cd9c8f9d 1040 strcat( buffer, tag );
klauss 0:4d17cd9c8f9d 1041 strcat( buffer, "\r\nTo: " );
klauss 0:4d17cd9c8f9d 1042 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1043 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1044 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 1045 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1046 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1047 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 1048 strcat( buffer, server_ip );
klauss 0:4d17cd9c8f9d 1049 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 1050 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1051 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1052 strcat( buffer, ">\r\nCall-ID: " );
klauss 0:4d17cd9c8f9d 1053 strcat( buffer, callid );
klauss 0:4d17cd9c8f9d 1054 strcat( buffer, "\r\nCSeq: " );
klauss 0:4d17cd9c8f9d 1055 itoa( get_cseq(), itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1056 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1057 strcat( buffer, " REGISTER\r\n" );
klauss 0:4d17cd9c8f9d 1058 strcat( buffer, "Max-Forwards: " );
klauss 0:4d17cd9c8f9d 1059 itoa( 70, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1060 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1061 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 1062
klauss 0:4d17cd9c8f9d 1063 strcat( buffer, "Contact: " );
klauss 0:4d17cd9c8f9d 1064 itoa( my_ext, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1065 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1066 strcat( buffer, " <sip:" );
klauss 0:4d17cd9c8f9d 1067 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1068 strcat( buffer, "@" );
klauss 0:4d17cd9c8f9d 1069 strcat( buffer, my_ip );
klauss 0:4d17cd9c8f9d 1070 strcat( buffer, ":" );
klauss 0:4d17cd9c8f9d 1071 itoa( server_port, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1072 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1073 strcat( buffer, ">;expires=0\r\n" );
klauss 0:4d17cd9c8f9d 1074 /*
klauss 0:4d17cd9c8f9d 1075 strcat( buffer, ">;expires=" );
klauss 0:4d17cd9c8f9d 1076 itoa( 0, itoa_buffer, 10 );
klauss 0:4d17cd9c8f9d 1077 strcat( buffer, itoa_buffer );
klauss 0:4d17cd9c8f9d 1078 strcat( buffer, "\r\n" );
klauss 0:4d17cd9c8f9d 1079 */
klauss 0:4d17cd9c8f9d 1080
klauss 0:4d17cd9c8f9d 1081 strcat( buffer, "User-Agent: VZtech/pabxdriver-" );
klauss 0:4d17cd9c8f9d 1082 strcat( buffer, SVNREV );
klauss 0:4d17cd9c8f9d 1083 strcat( buffer, "\r\nContent-Length: 0\r\n" );
klauss 0:4d17cd9c8f9d 1084 strcat( buffer, SIP_ALLOW );
klauss 0:4d17cd9c8f9d 1085 strcat( buffer, "\r\n\r\n" );
klauss 0:4d17cd9c8f9d 1086 buffer[ SIP_MAXMSGSIZE - 1 ] = 0;
klauss 0:4d17cd9c8f9d 1087 return buffer;
klauss 0:4d17cd9c8f9d 1088 }