Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Committer:
klauss
Date:
Thu Sep 10 20:11:35 2015 +0000
Revision:
135:2f4290590e51
Parent:
132:05cd37f7e007
inclus?o das funcionalidades; - [get|set]_cbx_wake_mode; - [get|set]_cbx_detach_mode; - [get|set]_cbx_mode; - simula < ext > <msg_id > < type >; - refresh_status

Who changed what in which revision?

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