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:
Fri Sep 19 17:39:09 2014 +0000
Revision:
18:01a93677e40c
Parent:
17:67a6b557eda5
hills_01

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