![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
sip.cpp@113:db67ae00550e, 2015-04-16 (annotated)
- Committer:
- klauss
- Date:
- Thu Apr 16 12:57:13 2015 +0000
- Revision:
- 113:db67ae00550e
- Parent:
- 112:6ae726539ab9
- Child:
- 114:472502b31a12
valide esse fw
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "sip.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 0:4d17cd9c8f9d | 3 | void Sip::__init_sock__( void ){ |
klauss | 0:4d17cd9c8f9d | 4 | sip_server.set_address( this->server_ip , this->server_port ); |
klauss | 89:0fe315117b00 | 5 | sock.set_blocking( false, 0 ); |
klauss | 0:4d17cd9c8f9d | 6 | sock.bind( this->my_port ); |
klauss | 0:4d17cd9c8f9d | 7 | } |
klauss | 0:4d17cd9c8f9d | 8 | |
klauss | 48:195c97f12e8e | 9 | void Sip::__end_sock__( void ){ |
klauss | 0:4d17cd9c8f9d | 10 | sock.close(); |
klauss | 0:4d17cd9c8f9d | 11 | } |
klauss | 0:4d17cd9c8f9d | 12 | |
klauss | 48:195c97f12e8e | 13 | void Sip::__reconnect__( void ){ |
klauss | 113:db67ae00550e | 14 | __end_sock__(); |
klauss | 113:db67ae00550e | 15 | __init_sock__(); |
klauss | 48:195c97f12e8e | 16 | } |
klauss | 29:7246460b73f8 | 17 | |
klauss | 29:7246460b73f8 | 18 | Sip::Sip( int id, uint16_t my_port ){ |
klauss | 29:7246460b73f8 | 19 | FILE * fp = NULL; |
klauss | 29:7246460b73f8 | 20 | char inner_tmp[ 20 ]; |
klauss | 28:94aec56c6329 | 21 | |
klauss | 29:7246460b73f8 | 22 | if( debug_sip ) debug_msg("Building SIP ext :: %d | Port :: %d", id, my_port ); |
klauss | 27:98f824719d1c | 23 | |
klauss | 29:7246460b73f8 | 24 | this->id = id; |
klauss | 112:6ae726539ab9 | 25 | this->my_ext = id; |
klauss | 112:6ae726539ab9 | 26 | this->my_port = my_port; |
klauss | 29:7246460b73f8 | 27 | |
klauss | 28:94aec56c6329 | 28 | fp = fopen( "/qspi/serverip.txt", "r"); |
klauss | 28:94aec56c6329 | 29 | if( fp == NULL){ |
klauss | 29:7246460b73f8 | 30 | if( debug_sip ) debug_msg("Failed to open /qspi/serverip.txt" ); |
klauss | 28:94aec56c6329 | 31 | strncpy( this->server_ip, __SERVER_IP__, 20 ); |
klauss | 28:94aec56c6329 | 32 | }else{ |
klauss | 50:d9b6577a70f5 | 33 | char tmp[ 16 ] = "\0"; |
klauss | 46:a670f187a704 | 34 | int read = fread( tmp, 1, 15, fp ); |
klauss | 46:a670f187a704 | 35 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 36 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 37 | if( tmp[ i ] == '\n' || tmp[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 38 | tmp[ i ] = '\0'; |
klauss | 46:a670f187a704 | 39 | break; |
klauss | 46:a670f187a704 | 40 | } |
klauss | 43:455522f98de5 | 41 | strcpy( this->server_ip, tmp ); |
klauss | 50:d9b6577a70f5 | 42 | if( debug_sip ) debug_msg("Objeto SIP fixado no server ip %s( read %d chars ), valid %d", this->server_ip, read, strlen( this->server_ip ) ); |
klauss | 28:94aec56c6329 | 43 | }else{ |
klauss | 29:7246460b73f8 | 44 | if( debug_sip ) debug_msg("Failed to read /qspi/serverip.txt" ); |
klauss | 28:94aec56c6329 | 45 | strncpy( this->server_ip, __SERVER_IP__, 20 ); |
klauss | 28:94aec56c6329 | 46 | } |
klauss | 28:94aec56c6329 | 47 | } |
klauss | 28:94aec56c6329 | 48 | fclose( fp ); |
klauss | 50:d9b6577a70f5 | 49 | this->server_ip[ 15 ] = '\0'; |
klauss | 28:94aec56c6329 | 50 | |
klauss | 28:94aec56c6329 | 51 | fp = fopen( "/qspi/serverport.txt", "r"); |
klauss | 28:94aec56c6329 | 52 | if( fp == NULL ){ |
klauss | 29:7246460b73f8 | 53 | if( debug_sip ) debug_msg("Failed to open /qspi/serverport.txt" ); |
klauss | 28:94aec56c6329 | 54 | this->server_port = __SERVER_PORT__; |
klauss | 28:94aec56c6329 | 55 | }else{ |
klauss | 29:7246460b73f8 | 56 | if( fread( (void *)inner_tmp, 1, 20, fp ) > 0 ){ |
klauss | 29:7246460b73f8 | 57 | server_port = atoi( inner_tmp ); |
klauss | 29:7246460b73f8 | 58 | if( debug_sip ) debug_msg("Objeto SIP fixado no server port %d", this->server_port ); |
klauss | 28:94aec56c6329 | 59 | }else{ |
klauss | 29:7246460b73f8 | 60 | if( debug_sip ) debug_msg("Failed to read /qspi/serverport.txt" ); |
klauss | 28:94aec56c6329 | 61 | this->server_port = __SERVER_PORT__; |
klauss | 28:94aec56c6329 | 62 | } |
klauss | 28:94aec56c6329 | 63 | } |
klauss | 28:94aec56c6329 | 64 | fclose( fp ); |
klauss | 28:94aec56c6329 | 65 | |
klauss | 28:94aec56c6329 | 66 | fp = fopen( "/qspi/myip.txt", "r"); |
klauss | 27:98f824719d1c | 67 | if (fp == NULL){ |
klauss | 29:7246460b73f8 | 68 | if( debug_sip ) debug_msg("Failed to open /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 69 | strncpy( this->my_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 70 | }else{ |
klauss | 50:d9b6577a70f5 | 71 | char tmp[ 16 ] = "\0"; |
klauss | 46:a670f187a704 | 72 | int read = fread( tmp, 1, 15, fp ); |
klauss | 46:a670f187a704 | 73 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 74 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 75 | if( tmp[ i ] == '\n' || tmp[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 76 | tmp[ i ] = '\0'; |
klauss | 46:a670f187a704 | 77 | break; |
klauss | 46:a670f187a704 | 78 | } |
klauss | 43:455522f98de5 | 79 | strcpy( this->my_ip, tmp ); |
klauss | 50:d9b6577a70f5 | 80 | if( debug_sip ) debug_msg("Objeto SIP fixado no ip %s( lidos %d chars ), valid %d", this->my_ip, read, strlen( this->my_ip ) ); |
klauss | 27:98f824719d1c | 81 | }else{ |
klauss | 29:7246460b73f8 | 82 | if( debug_sip ) debug_msg("Failed to read /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 83 | strncpy( this->my_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 84 | } |
klauss | 27:98f824719d1c | 85 | } |
klauss | 27:98f824719d1c | 86 | fclose( fp ); |
klauss | 46:a670f187a704 | 87 | |
klauss | 50:d9b6577a70f5 | 88 | this->my_ip[ 15 ] = '\0'; |
klauss | 28:94aec56c6329 | 89 | |
klauss | 97:8985817e8847 | 90 | if( debug_sip ) debug_msg("Fixado na porta %d", this->my_port ); |
klauss | 97:8985817e8847 | 91 | |
klauss | 28:94aec56c6329 | 92 | itoa( this->my_ext, this->my_display, 10 ); |
klauss | 112:6ae726539ab9 | 93 | |
klauss | 112:6ae726539ab9 | 94 | this->my_rtp_port = 0; |
klauss | 112:6ae726539ab9 | 95 | if( debug_rtp ) debug_msg("new with rtp port %d ", this->my_rtp_port ); |
klauss | 46:a670f187a704 | 96 | |
klauss | 28:94aec56c6329 | 97 | fp = fopen( "/qspi/peerext.txt", "r"); |
klauss | 29:7246460b73f8 | 98 | if( fp == NULL ) { |
klauss | 29:7246460b73f8 | 99 | if( debug_sip ) debug_msg("Failed to open /qspi/peerext.txt" ); |
klauss | 28:94aec56c6329 | 100 | this->peer_ext = __PEER_EXT__; |
klauss | 29:7246460b73f8 | 101 | } else { |
klauss | 29:7246460b73f8 | 102 | if( fread( (void *)inner_tmp, 1, 32, fp ) > 0 ) { |
klauss | 29:7246460b73f8 | 103 | this->peer_ext = atoi( inner_tmp ); |
klauss | 29:7246460b73f8 | 104 | if( debug_sip ) debug_msg("Objeto SIP fixado no server ext %d", this->peer_ext ); |
klauss | 29:7246460b73f8 | 105 | } else { |
klauss | 29:7246460b73f8 | 106 | if( debug_sip ) debug_msg("Failed to read /qspi/peerext.txt" ); |
klauss | 28:94aec56c6329 | 107 | this->peer_ext = __PEER_EXT__; |
klauss | 28:94aec56c6329 | 108 | } |
klauss | 28:94aec56c6329 | 109 | } |
klauss | 28:94aec56c6329 | 110 | fclose( fp ); |
klauss | 0:4d17cd9c8f9d | 111 | strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" ); |
klauss | 0:4d17cd9c8f9d | 112 | strcpy( this->last_invite_tag, "" ); |
klauss | 0:4d17cd9c8f9d | 113 | strcpy( this->last_invite_callid, ""); |
klauss | 0:4d17cd9c8f9d | 114 | strcpy( this->SVNREV, "COLA" ); |
klauss | 0:4d17cd9c8f9d | 115 | status = sip_idle; |
klauss | 0:4d17cd9c8f9d | 116 | |
klauss | 0:4d17cd9c8f9d | 117 | __init_sock__(); |
klauss | 81:3656f00ab3db | 118 | muted = false; |
klauss | 85:b6f2dc1d0f4f | 119 | |
klauss | 91:c2a86b1f8aaa | 120 | invite_pkg_sent = 0; |
klauss | 91:c2a86b1f8aaa | 121 | call = NULL; |
klauss | 91:c2a86b1f8aaa | 122 | |
klauss | 91:c2a86b1f8aaa | 123 | ok_sent = 0; |
klauss | 91:c2a86b1f8aaa | 124 | waiting = false; |
klauss | 91:c2a86b1f8aaa | 125 | length_muted = 0; |
klauss | 91:c2a86b1f8aaa | 126 | listen_SIP_server_return = 0; |
klauss | 91:c2a86b1f8aaa | 127 | |
klauss | 85:b6f2dc1d0f4f | 128 | sip_new_counter++; |
klauss | 0:4d17cd9c8f9d | 129 | } |
klauss | 29:7246460b73f8 | 130 | |
klauss | 0:4d17cd9c8f9d | 131 | Sip::~Sip(){ |
klauss | 0:4d17cd9c8f9d | 132 | __end_sock__(); |
klauss | 85:b6f2dc1d0f4f | 133 | sip_delete_counter++; |
klauss | 0:4d17cd9c8f9d | 134 | } |
klauss | 0:4d17cd9c8f9d | 135 | |
klauss | 78:1353744f01e1 | 136 | int Sip::registry(){ |
klauss | 0:4d17cd9c8f9d | 137 | build_registry_package( buffer ); |
klauss | 81:3656f00ab3db | 138 | |
klauss | 113:db67ae00550e | 139 | int send = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400 |
klauss | 113:db67ae00550e | 140 | if( send != strlen( buffer ) ) |
klauss | 113:db67ae00550e | 141 | { |
klauss | 113:db67ae00550e | 142 | if( debug_reconnect ) send_msg("Reconnect SIP -- Registry"); |
klauss | 113:db67ae00550e | 143 | if( debug_reconnect ) send_msg("Sent %i of %i bytes", send, strlen( buffer ) ); |
klauss | 113:db67ae00550e | 144 | __reconnect__(); |
klauss | 113:db67ae00550e | 145 | miss_sip_registry_send_pkg++; |
klauss | 113:db67ae00550e | 146 | } |
klauss | 109:a5b8264ffbbc | 147 | |
klauss | 113:db67ae00550e | 148 | if( debug_sip ) send_msg("Registry ( %d, %d ) -- Sent %i of %i bytes", this->my_ext, this->my_port, send, strlen( buffer ) ); |
klauss | 109:a5b8264ffbbc | 149 | |
klauss | 113:db67ae00550e | 150 | return( send ); |
klauss | 0:4d17cd9c8f9d | 151 | } |
klauss | 0:4d17cd9c8f9d | 152 | |
klauss | 0:4d17cd9c8f9d | 153 | VZ_call * Sip::invite(){ |
klauss | 0:4d17cd9c8f9d | 154 | char callbox_string[ 32 ]; |
klauss | 0:4d17cd9c8f9d | 155 | itoa( get_id(), callbox_string, 10 ); |
klauss | 91:c2a86b1f8aaa | 156 | |
klauss | 91:c2a86b1f8aaa | 157 | if( status == sip_on_call ) return call; |
klauss | 87:679ee0d594a9 | 158 | |
klauss | 91:c2a86b1f8aaa | 159 | else if( status == sip_idle ){ |
klauss | 91:c2a86b1f8aaa | 160 | int cseq = 0; |
klauss | 91:c2a86b1f8aaa | 161 | |
klauss | 91:c2a86b1f8aaa | 162 | call = NULL; |
klauss | 91:c2a86b1f8aaa | 163 | |
klauss | 91:c2a86b1f8aaa | 164 | build_invite_package( buffer, callbox_string, &cseq ); |
klauss | 91:c2a86b1f8aaa | 165 | |
klauss | 91:c2a86b1f8aaa | 166 | this->invite_pkg_sent = 0; |
klauss | 91:c2a86b1f8aaa | 167 | |
klauss | 113:db67ae00550e | 168 | do{ |
klauss | 113:db67ae00550e | 169 | this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); |
klauss | 113:db67ae00550e | 170 | }while( this->invite_pkg_sent == 0 ); |
klauss | 113:db67ae00550e | 171 | |
klauss | 113:db67ae00550e | 172 | //FIXME verificar se posso nao usar invite_pkg_sent |
klauss | 113:db67ae00550e | 173 | int send = this->invite_pkg_sent; |
klauss | 113:db67ae00550e | 174 | if( send != strlen( buffer ) ) |
klauss | 104:62646ef786a3 | 175 | { |
klauss | 113:db67ae00550e | 176 | if( debug_reconnect ) send_msg("Reconnect SIP -- Invite"); |
klauss | 113:db67ae00550e | 177 | __reconnect__(); |
klauss | 113:db67ae00550e | 178 | miss_sip_invite_send_pkg++; |
klauss | 104:62646ef786a3 | 179 | } |
klauss | 104:62646ef786a3 | 180 | |
klauss | 113:db67ae00550e | 181 | if( debug_invite || debug_reconnect ) send_msg("Return value for invite pkg %d", invite_pkg_sent ); |
klauss | 113:db67ae00550e | 182 | |
klauss | 99:e80850c51106 | 183 | invite_timer.stop(); |
klauss | 99:e80850c51106 | 184 | invite_timer.reset(); |
klauss | 91:c2a86b1f8aaa | 185 | invite_timer.start(); |
klauss | 91:c2a86b1f8aaa | 186 | |
klauss | 91:c2a86b1f8aaa | 187 | set_status( status, sip_waiting_trying ); |
klauss | 91:c2a86b1f8aaa | 188 | |
klauss | 91:c2a86b1f8aaa | 189 | waiting = false; |
klauss | 91:c2a86b1f8aaa | 190 | |
klauss | 91:c2a86b1f8aaa | 191 | ok_sent = 0; |
klauss | 91:c2a86b1f8aaa | 192 | |
klauss | 91:c2a86b1f8aaa | 193 | length_muted = 0; |
klauss | 78:1353744f01e1 | 194 | } |
klauss | 0:4d17cd9c8f9d | 195 | |
klauss | 78:1353744f01e1 | 196 | if( invite_timer.read() > __INVITE_MAX_WAITING_TIME__ ){ |
klauss | 92:92df17f538a8 | 197 | if( debug_invite == true ) debug_msg("Invite call timeout :("); |
klauss | 91:c2a86b1f8aaa | 198 | |
klauss | 91:c2a86b1f8aaa | 199 | if( ( this->invite_pkg_sent == -1 ) || length_muted || ok_sent == -1 ){ |
klauss | 81:3656f00ab3db | 200 | muted = true; |
klauss | 81:3656f00ab3db | 201 | if( debug_invite || debug_muted ) debug_msg("RX from eth having situation, length_muted( %d ) - ok_sent( %d )", length_muted, ok_sent ); |
klauss | 81:3656f00ab3db | 202 | ok_sent = 0; |
klauss | 81:3656f00ab3db | 203 | length_muted = 0; |
klauss | 81:3656f00ab3db | 204 | __reconnect__(); |
klauss | 81:3656f00ab3db | 205 | }else{ |
klauss | 81:3656f00ab3db | 206 | muted = false; |
klauss | 81:3656f00ab3db | 207 | } |
klauss | 81:3656f00ab3db | 208 | |
klauss | 78:1353744f01e1 | 209 | if( debug_invite ) debug_msg("Timeout do pedido"); |
klauss | 78:1353744f01e1 | 210 | invite_timer.stop(); |
klauss | 78:1353744f01e1 | 211 | invite_timer.reset(); |
klauss | 81:3656f00ab3db | 212 | set_status( status, sip_denied ); |
klauss | 78:1353744f01e1 | 213 | send_bye(); |
klauss | 91:c2a86b1f8aaa | 214 | |
klauss | 91:c2a86b1f8aaa | 215 | //if( call != NULL ) delete( call ); |
klauss | 91:c2a86b1f8aaa | 216 | call = NULL; |
klauss | 78:1353744f01e1 | 217 | return( NULL ); |
klauss | 78:1353744f01e1 | 218 | } |
klauss | 77:e8c0253b57bc | 219 | |
klauss | 91:c2a86b1f8aaa | 220 | if( waiting == true ){ return call; } |
klauss | 91:c2a86b1f8aaa | 221 | |
klauss | 91:c2a86b1f8aaa | 222 | else return( NULL ); |
klauss | 0:4d17cd9c8f9d | 223 | } |
klauss | 0:4d17cd9c8f9d | 224 | |
klauss | 0:4d17cd9c8f9d | 225 | char * Sip::build_registry_package( char * buffer ){ |
klauss | 0:4d17cd9c8f9d | 226 | char branch[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 227 | char tag[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 228 | char callid[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 229 | fill_random16h( branch ); |
klauss | 0:4d17cd9c8f9d | 230 | fill_random( tag, 18 ); |
klauss | 0:4d17cd9c8f9d | 231 | fill_random16h( callid ); |
klauss | 0:4d17cd9c8f9d | 232 | char itoa_buffer[ 65 ]; |
klauss | 0:4d17cd9c8f9d | 233 | strcpy( buffer, "REGISTER sip:" ); |
klauss | 0:4d17cd9c8f9d | 234 | strcat( buffer, server_ip ); |
klauss | 0:4d17cd9c8f9d | 235 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 236 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 237 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 238 | strcat( buffer, " SIP/2.0\r\nVia: SIP/2.0/UDP " ); |
klauss | 0:4d17cd9c8f9d | 239 | strcat( buffer, my_ip ); |
klauss | 0:4d17cd9c8f9d | 240 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 241 | itoa( my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 242 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 243 | strcat( buffer, ";branch=" ); |
klauss | 0:4d17cd9c8f9d | 244 | strcat( buffer, branch ); |
klauss | 0:4d17cd9c8f9d | 245 | strcat( buffer, "\r\nFrom: " ); |
klauss | 0:4d17cd9c8f9d | 246 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 247 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 248 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 249 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 250 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 251 | strcat( buffer, server_ip ); |
klauss | 0:4d17cd9c8f9d | 252 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 253 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 254 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 255 | strcat( buffer, ">;tag=" ); |
klauss | 0:4d17cd9c8f9d | 256 | strcat( buffer, tag ); |
klauss | 0:4d17cd9c8f9d | 257 | strcat( buffer, "\r\nTo: " ); |
klauss | 0:4d17cd9c8f9d | 258 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 259 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 260 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 261 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 262 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 263 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 264 | strcat( buffer, server_ip ); |
klauss | 0:4d17cd9c8f9d | 265 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 266 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 267 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 268 | strcat( buffer, ">\r\nCall-ID: " ); |
klauss | 0:4d17cd9c8f9d | 269 | strcat( buffer, callid ); |
klauss | 0:4d17cd9c8f9d | 270 | strcat( buffer, "\r\nCSeq: " ); |
klauss | 0:4d17cd9c8f9d | 271 | itoa( get_cseq(), itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 272 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 273 | strcat( buffer, " REGISTER\r\n" ); |
klauss | 0:4d17cd9c8f9d | 274 | strcat( buffer, "Max-Forwards: " ); |
klauss | 0:4d17cd9c8f9d | 275 | itoa( 70, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 276 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 277 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 278 | |
klauss | 0:4d17cd9c8f9d | 279 | strcat( buffer, "Contact: " ); |
klauss | 0:4d17cd9c8f9d | 280 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 281 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 282 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 283 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 284 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 285 | strcat( buffer, my_ip ); |
klauss | 0:4d17cd9c8f9d | 286 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 287 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 288 | strcat( buffer, itoa_buffer ); |
klauss | 92:92df17f538a8 | 289 | strcat( buffer, ">;expires=270" ); |
klauss | 0:4d17cd9c8f9d | 290 | //itoa( 1200, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 291 | //strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 292 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 293 | |
klauss | 0:4d17cd9c8f9d | 294 | strcat( buffer, "User-Agent: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 295 | strcat( buffer, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 296 | strcat( buffer, "\r\nContent-Length: 0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 297 | strcat( buffer, SIP_ALLOW ); |
klauss | 0:4d17cd9c8f9d | 298 | strcat( buffer, "\r\n\r\n" ); |
klauss | 89:0fe315117b00 | 299 | //buffer[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 300 | return buffer; |
klauss | 0:4d17cd9c8f9d | 301 | } |
klauss | 0:4d17cd9c8f9d | 302 | |
klauss | 0:4d17cd9c8f9d | 303 | char * Sip::build_invite_package( char * pkg, char * callbox_string, int * cseq ){ |
klauss | 0:4d17cd9c8f9d | 304 | char header[ SIP_MAXMSGSIZE ], body[ SIP_MAXMSGSIZE ]; |
klauss | 0:4d17cd9c8f9d | 305 | char branch[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 306 | char tag[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 307 | char callid[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 308 | char itoa_buffer[ 65 ]; |
klauss | 0:4d17cd9c8f9d | 309 | *cseq = get_cseq(); |
klauss | 0:4d17cd9c8f9d | 310 | fill_random16h( branch ); |
klauss | 0:4d17cd9c8f9d | 311 | fill_random( tag,18 ); |
klauss | 0:4d17cd9c8f9d | 312 | fill_random16h( callid ); |
klauss | 0:4d17cd9c8f9d | 313 | |
klauss | 0:4d17cd9c8f9d | 314 | strcpy( last_invite_tag,tag ); |
klauss | 0:4d17cd9c8f9d | 315 | strcpy( last_invite_callid, callid ); |
klauss | 0:4d17cd9c8f9d | 316 | |
klauss | 0:4d17cd9c8f9d | 317 | strcpy( header, "INVITE sip:" ); |
klauss | 0:4d17cd9c8f9d | 318 | itoa( this->peer_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 319 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 320 | strcat( header, "@" ); |
klauss | 0:4d17cd9c8f9d | 321 | strcat( header, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 322 | strcat( header, ":" ); |
klauss | 0:4d17cd9c8f9d | 323 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 324 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 325 | strcat( header, " SIP/" ); |
klauss | 0:4d17cd9c8f9d | 326 | itoa( 2, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 327 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 328 | strcat( header, "." ); |
klauss | 0:4d17cd9c8f9d | 329 | itoa( 0, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 330 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 331 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 332 | strcat( header, "Via: SIP/" ); |
klauss | 0:4d17cd9c8f9d | 333 | itoa( 2, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 334 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 335 | strcat( header, "." ); |
klauss | 0:4d17cd9c8f9d | 336 | itoa( 0, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 337 | strcat( header, itoa_buffer ); |
klauss | 113:db67ae00550e | 338 | strcat( header, "/UDP " ); |
klauss | 0:4d17cd9c8f9d | 339 | strcat( header, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 340 | strcat( header, ":" ); |
klauss | 0:4d17cd9c8f9d | 341 | itoa( this->my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 342 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 343 | strcat( header, ";branch=" ); |
klauss | 0:4d17cd9c8f9d | 344 | strcat( header, branch ); |
klauss | 0:4d17cd9c8f9d | 345 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 346 | strcat( header, "From: " ); |
klauss | 0:4d17cd9c8f9d | 347 | strcat( header, callbox_string ); |
klauss | 0:4d17cd9c8f9d | 348 | strcat( header, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 349 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 350 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 351 | strcat( header, "@" ); |
klauss | 0:4d17cd9c8f9d | 352 | strcat( header, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 353 | strcat( header, ":" ); |
klauss | 0:4d17cd9c8f9d | 354 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 355 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 356 | strcat( header, ">;tag=" ); |
klauss | 0:4d17cd9c8f9d | 357 | strcat( header, tag ); |
klauss | 0:4d17cd9c8f9d | 358 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 359 | strcat( header, "To: <sip:" ); |
klauss | 0:4d17cd9c8f9d | 360 | itoa( this->peer_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 361 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 362 | strcat( header, "@" ); |
klauss | 0:4d17cd9c8f9d | 363 | strcat( header, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 364 | strcat( header, ":" ); |
klauss | 0:4d17cd9c8f9d | 365 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 366 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 367 | strcat( header, ">\r\n" ); |
klauss | 0:4d17cd9c8f9d | 368 | strcat( header, "Call-ID: " ); |
klauss | 0:4d17cd9c8f9d | 369 | strcat( header, callid ); |
klauss | 0:4d17cd9c8f9d | 370 | strcat( header, "@" ); |
klauss | 0:4d17cd9c8f9d | 371 | strcat( header, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 372 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 373 | strcat( header, "CSeq: " ); |
klauss | 0:4d17cd9c8f9d | 374 | itoa( *cseq, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 375 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 376 | strcat( header, " INVITE\r\n" ); |
klauss | 0:4d17cd9c8f9d | 377 | strcat( header, "Contact: " ); |
klauss | 0:4d17cd9c8f9d | 378 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 379 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 380 | strcat( header, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 381 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 382 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 383 | strcat( header, "@" ); |
klauss | 0:4d17cd9c8f9d | 384 | strcat( header, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 385 | strcat( header, ":" ); |
klauss | 0:4d17cd9c8f9d | 386 | itoa( this->my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 387 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 388 | strcat( header, ">\r\n" ); |
klauss | 0:4d17cd9c8f9d | 389 | strcat( header, "Max-Forwards: " ); |
klauss | 0:4d17cd9c8f9d | 390 | itoa( 20, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 391 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 392 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 393 | strcat( header, "User-Agent: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 394 | strcat( header, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 395 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 396 | strcat( header, "Expires: " ); |
klauss | 112:6ae726539ab9 | 397 | itoa( 71, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 398 | strcat( header, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 399 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 400 | strcat( header, SIP_ALLOW ); |
klauss | 0:4d17cd9c8f9d | 401 | strcat( header, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 402 | strcat( header, "Content-Type: application/sdp\r\n" ); |
klauss | 100:09a23fcd3bdf | 403 | //header[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 404 | |
klauss | 0:4d17cd9c8f9d | 405 | strcpy( body, "v=0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 406 | strcat( body, "o=- " ); |
klauss | 0:4d17cd9c8f9d | 407 | itoa( 7377, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 408 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 409 | strcat( body, " " ); |
klauss | 0:4d17cd9c8f9d | 410 | itoa( 18176, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 411 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 412 | strcat( body, " IN IP" ); |
klauss | 0:4d17cd9c8f9d | 413 | itoa( 4, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 414 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 415 | strcat( body, " " ); |
klauss | 0:4d17cd9c8f9d | 416 | strcat( body, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 417 | strcat( body, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 418 | strcat( body, "s=-\r\n" ); |
klauss | 0:4d17cd9c8f9d | 419 | strcat( body, "c=IN IP" ); |
klauss | 0:4d17cd9c8f9d | 420 | itoa( 4, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 421 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 422 | strcat( body, " " ); |
klauss | 0:4d17cd9c8f9d | 423 | strcat( body, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 424 | strcat( body, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 425 | strcat( body, "t=" ); |
klauss | 0:4d17cd9c8f9d | 426 | itoa( 0, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 427 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 428 | strcat( body, " " ); |
klauss | 0:4d17cd9c8f9d | 429 | itoa( 0, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 430 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 431 | strcat( body, "\r\n" ); |
klauss | 112:6ae726539ab9 | 432 | |
klauss | 112:6ae726539ab9 | 433 | this->my_rtp_port = fill_random_rtp_port(); |
klauss | 112:6ae726539ab9 | 434 | sprintf( itoa_buffer,"i=UDP %d\r\n", this->my_rtp_port ); |
klauss | 112:6ae726539ab9 | 435 | strcat( body, itoa_buffer ); |
klauss | 113:db67ae00550e | 436 | |
klauss | 0:4d17cd9c8f9d | 437 | strcat( body, "m=audio " ); |
klauss | 0:4d17cd9c8f9d | 438 | itoa( this->my_rtp_port, itoa_buffer, 10 ); |
klauss | 112:6ae726539ab9 | 439 | strcat( body, itoa_buffer ); |
klauss | 113:db67ae00550e | 440 | if( debug_rtp ) debug_msg("Invite PKG with rtp port ( %d - %s )", this->my_rtp_port, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 441 | strcat( body, " RTP/AVP " ); |
klauss | 0:4d17cd9c8f9d | 442 | itoa( 8, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 443 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 444 | strcat( body, " " ); |
klauss | 0:4d17cd9c8f9d | 445 | itoa( 101, 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, "a=rtpmap:" ); |
klauss | 0:4d17cd9c8f9d | 449 | itoa( 8, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 450 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 451 | strcat( body, " PCMA/" ); |
klauss | 0:4d17cd9c8f9d | 452 | itoa( 8000, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 453 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 454 | strcat( body, "/" ); |
klauss | 0:4d17cd9c8f9d | 455 | itoa( 1, 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( 101, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 460 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 461 | strcat( body, " telephone-event/" ); |
klauss | 0:4d17cd9c8f9d | 462 | itoa( 8000, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 463 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 464 | strcat( body, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 465 | strcat( body, "a=fmtp: " ); |
klauss | 0:4d17cd9c8f9d | 466 | itoa( 101, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 467 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 468 | strcat( body, " " ); |
klauss | 0:4d17cd9c8f9d | 469 | itoa( 0, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 470 | strcat( body, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 471 | strcat( body, "-" ); |
klauss | 0:4d17cd9c8f9d | 472 | itoa( 11, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 473 | strcat( body, itoa_buffer ); |
klauss | 100:09a23fcd3bdf | 474 | //body[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 475 | |
klauss | 0:4d17cd9c8f9d | 476 | make_content_length( header, body, pkg ); |
klauss | 0:4d17cd9c8f9d | 477 | return( pkg ); |
klauss | 0:4d17cd9c8f9d | 478 | } |
klauss | 0:4d17cd9c8f9d | 479 | |
klauss | 0:4d17cd9c8f9d | 480 | char * Sip::make_content_length( char * header, char * body, char * pkg ){ |
klauss | 0:4d17cd9c8f9d | 481 | int size; |
klauss | 0:4d17cd9c8f9d | 482 | char itoa_buffer[ 65 ]; |
klauss | 0:4d17cd9c8f9d | 483 | size = strlen( body ); |
klauss | 0:4d17cd9c8f9d | 484 | strcpy( pkg, header ); |
klauss | 0:4d17cd9c8f9d | 485 | strcat( pkg, "Content-Length: " ); |
klauss | 0:4d17cd9c8f9d | 486 | itoa( size, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 487 | strcat( pkg, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 488 | strcat( pkg, "\r\n\r\n" ); |
klauss | 0:4d17cd9c8f9d | 489 | strcat( pkg, body ); |
klauss | 89:0fe315117b00 | 490 | //pkg[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 491 | return pkg; |
klauss | 0:4d17cd9c8f9d | 492 | } |
klauss | 0:4d17cd9c8f9d | 493 | |
klauss | 0:4d17cd9c8f9d | 494 | char * Sip::build_bye_package( char * buffer ){ |
klauss | 0:4d17cd9c8f9d | 495 | char branch[SIP_MAXFIELDSIZE]; |
klauss | 0:4d17cd9c8f9d | 496 | char tag[SIP_MAXFIELDSIZE]; |
klauss | 0:4d17cd9c8f9d | 497 | char itoa_buffer[ 65 ]; |
klauss | 0:4d17cd9c8f9d | 498 | fill_random16h(branch); |
klauss | 0:4d17cd9c8f9d | 499 | fill_random(tag,18); |
klauss | 0:4d17cd9c8f9d | 500 | |
klauss | 0:4d17cd9c8f9d | 501 | strcpy( buffer, "BYE sip:" ); |
klauss | 0:4d17cd9c8f9d | 502 | itoa( this->peer_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 503 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 504 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 505 | strcat( buffer, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 506 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 507 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 508 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 509 | strcat( buffer, " SIP/2.0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 510 | strcat( buffer, "Via: SIP/2.0/UDP " ); |
klauss | 0:4d17cd9c8f9d | 511 | strcat( buffer, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 512 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 513 | itoa( this->my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 514 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 515 | strcat( buffer, ";branch=" ); |
klauss | 0:4d17cd9c8f9d | 516 | strcat( buffer, branch ); |
klauss | 0:4d17cd9c8f9d | 517 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 518 | strcat( buffer, "From: " ); |
klauss | 0:4d17cd9c8f9d | 519 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 520 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 521 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 522 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 523 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 524 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 525 | strcat( buffer, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 526 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 527 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 528 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 529 | strcat( buffer, ">;tag=" ); |
klauss | 0:4d17cd9c8f9d | 530 | strcat( buffer, last_invite_tag ); |
klauss | 0:4d17cd9c8f9d | 531 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 532 | strcat( buffer, "To: <sip:" ); |
klauss | 0:4d17cd9c8f9d | 533 | itoa( this->peer_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 534 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 535 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 536 | strcat( buffer, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 537 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 538 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 539 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 540 | strcat( buffer, ">;tag=" ); |
klauss | 0:4d17cd9c8f9d | 541 | strcat( buffer, tag ); |
klauss | 0:4d17cd9c8f9d | 542 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 543 | strcat( buffer, "Call-ID: " ); |
klauss | 0:4d17cd9c8f9d | 544 | strcat( buffer, last_invite_callid ); |
klauss | 0:4d17cd9c8f9d | 545 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 546 | strcat( buffer, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 547 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 548 | strcat( buffer, "CSeq: " ); |
klauss | 0:4d17cd9c8f9d | 549 | itoa( get_cseq(), itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 550 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 551 | strcat( buffer, " BYE\r\n" ); |
klauss | 0:4d17cd9c8f9d | 552 | strcat( buffer, "Max-Forwards: 70\r\n" ); |
klauss | 0:4d17cd9c8f9d | 553 | strcat( buffer, "User-Agent: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 554 | strcat( buffer, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 555 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 556 | strcat( buffer, "Content-Length: 0\r\n\r\n" ); |
klauss | 0:4d17cd9c8f9d | 557 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 558 | } |
klauss | 0:4d17cd9c8f9d | 559 | char * Sip::build_ack_package( char * buffer, unsigned char * orig ){ |
klauss | 0:4d17cd9c8f9d | 560 | char to[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 561 | char from[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 562 | char callid[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 563 | char via[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 564 | char branch[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 565 | char cseq[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 566 | char itoa_buffer[ 65 ]; |
klauss | 0:4d17cd9c8f9d | 567 | // FIXME confirmar se nao preciso converter todos os ints com itoa |
klauss | 0:4d17cd9c8f9d | 568 | if( decode_gettag( orig, "to: ", to ) == 0) { |
klauss | 0:4d17cd9c8f9d | 569 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 570 | return NULL; |
klauss | 0:4d17cd9c8f9d | 571 | } |
klauss | 0:4d17cd9c8f9d | 572 | if( decode_gettag( orig, "from: ", from ) == 0) { |
klauss | 0:4d17cd9c8f9d | 573 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 574 | return NULL; |
klauss | 0:4d17cd9c8f9d | 575 | } |
klauss | 0:4d17cd9c8f9d | 576 | if( decode_gettag( orig, "call-id: ", callid ) == 0) { |
klauss | 0:4d17cd9c8f9d | 577 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 578 | return NULL; |
klauss | 0:4d17cd9c8f9d | 579 | } |
klauss | 0:4d17cd9c8f9d | 580 | decode_branch( orig, branch ); |
klauss | 0:4d17cd9c8f9d | 581 | decode_cseq( orig, cseq ); |
klauss | 0:4d17cd9c8f9d | 582 | |
klauss | 0:4d17cd9c8f9d | 583 | if( strlen( via ) > 6 ) { |
klauss | 0:4d17cd9c8f9d | 584 | if( strcasecmp( ";rport", via+strlen( via )-6) == 0) { |
klauss | 0:4d17cd9c8f9d | 585 | via[strlen(via)-6] = 0; |
klauss | 0:4d17cd9c8f9d | 586 | } |
klauss | 0:4d17cd9c8f9d | 587 | } |
klauss | 0:4d17cd9c8f9d | 588 | strcpy( buffer, "ACK sip:" ); |
klauss | 0:4d17cd9c8f9d | 589 | itoa( this->peer_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 590 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 591 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 592 | strcat( buffer, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 593 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 594 | itoa( this->server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 595 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 596 | strcat( buffer, " SIP/2.0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 597 | strcat( buffer, "Via: SIP/2.0/UDP " ); |
klauss | 0:4d17cd9c8f9d | 598 | strcat( buffer, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 599 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 600 | itoa( this->my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 601 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 602 | strcat( buffer, ";branch=" ); |
klauss | 0:4d17cd9c8f9d | 603 | strcat( buffer, branch ); |
klauss | 0:4d17cd9c8f9d | 604 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 605 | strcat( buffer, "From: " ); |
klauss | 0:4d17cd9c8f9d | 606 | strcat( buffer, from ); |
klauss | 0:4d17cd9c8f9d | 607 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 608 | strcat( buffer, "To: " ); |
klauss | 0:4d17cd9c8f9d | 609 | strcat( buffer, to ); |
klauss | 0:4d17cd9c8f9d | 610 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 611 | strcat( buffer, "Call-ID: " ); |
klauss | 0:4d17cd9c8f9d | 612 | strcat( buffer, callid ); |
klauss | 0:4d17cd9c8f9d | 613 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 614 | strcat( buffer, "CSeq: " ); |
klauss | 0:4d17cd9c8f9d | 615 | strcat( buffer, cseq ); |
klauss | 0:4d17cd9c8f9d | 616 | strcat( buffer, " ACK\r\n" ); |
klauss | 0:4d17cd9c8f9d | 617 | strcat( buffer, "Max-Forwards: 70\r\n" ); |
klauss | 0:4d17cd9c8f9d | 618 | strcat( buffer, "Contact: " ); |
klauss | 0:4d17cd9c8f9d | 619 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 620 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 621 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 622 | itoa( this->my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 623 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 624 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 625 | strcat( buffer, this->my_ip ); |
klauss | 0:4d17cd9c8f9d | 626 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 627 | itoa( this->my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 628 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 629 | strcat( buffer, ">\r\n" ); |
klauss | 0:4d17cd9c8f9d | 630 | strcat( buffer, "User-Agent: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 631 | strcat( buffer, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 632 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 633 | strcat( buffer, "Content-Length: 0\r\n\r\n" ); |
klauss | 89:0fe315117b00 | 634 | //buffer[SIP_MAXMSGSIZE-1] = 0; |
klauss | 0:4d17cd9c8f9d | 635 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 636 | } |
klauss | 0:4d17cd9c8f9d | 637 | |
klauss | 0:4d17cd9c8f9d | 638 | int Sip::get_return_code( char * buffer ){ |
klauss | 0:4d17cd9c8f9d | 639 | char a[32]; |
klauss | 0:4d17cd9c8f9d | 640 | int i = 0, j; |
klauss | 0:4d17cd9c8f9d | 641 | if( strncmp( "SIP/", buffer, 4 ) != 0 ) return 0; |
klauss | 0:4d17cd9c8f9d | 642 | if( strlen( buffer ) < 32) return 0; |
klauss | 0:4d17cd9c8f9d | 643 | strncpy( a, buffer, 31 ); |
klauss | 0:4d17cd9c8f9d | 644 | a[ 31 ] = 0; |
klauss | 0:4d17cd9c8f9d | 645 | while ( a[ i ] > ' ') i++; |
klauss | 0:4d17cd9c8f9d | 646 | j = i+i; |
klauss | 0:4d17cd9c8f9d | 647 | while( (a[ j ] >= '0') && ( a[ j ] <= '9') ) j++; |
klauss | 0:4d17cd9c8f9d | 648 | a[ j ] = 0; |
klauss | 0:4d17cd9c8f9d | 649 | return atoi( a + i + 1 ); |
klauss | 0:4d17cd9c8f9d | 650 | } |
klauss | 0:4d17cd9c8f9d | 651 | |
klauss | 0:4d17cd9c8f9d | 652 | char * Sip::build_generic_reply_package(char * buffer, unsigned char * orig, char * tag){ |
klauss | 0:4d17cd9c8f9d | 653 | char to[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 654 | char from[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 655 | char callid[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 656 | char cseq[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 657 | char via[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 658 | |
klauss | 0:4d17cd9c8f9d | 659 | if( decode_gettag( orig, "to: ", to ) == 0) { |
klauss | 0:4d17cd9c8f9d | 660 | buffer[0] = 0; |
klauss | 0:4d17cd9c8f9d | 661 | return NULL; |
klauss | 0:4d17cd9c8f9d | 662 | } |
klauss | 0:4d17cd9c8f9d | 663 | if( decode_gettag( orig, "from: ", from ) == 0) { |
klauss | 0:4d17cd9c8f9d | 664 | buffer[0] = 0; |
klauss | 0:4d17cd9c8f9d | 665 | return NULL; |
klauss | 0:4d17cd9c8f9d | 666 | } |
klauss | 0:4d17cd9c8f9d | 667 | if( decode_gettag( orig, "call-id: ", callid ) == 0) { |
klauss | 0:4d17cd9c8f9d | 668 | buffer[0] = 0; |
klauss | 0:4d17cd9c8f9d | 669 | return NULL; |
klauss | 0:4d17cd9c8f9d | 670 | } |
klauss | 0:4d17cd9c8f9d | 671 | if( decode_gettag( orig, "cseq: ", cseq ) == 0) { |
klauss | 0:4d17cd9c8f9d | 672 | buffer[0] = 0; |
klauss | 0:4d17cd9c8f9d | 673 | return NULL; |
klauss | 0:4d17cd9c8f9d | 674 | } |
klauss | 0:4d17cd9c8f9d | 675 | if( decode_gettag( orig, "via: ", via ) == 0) { |
klauss | 0:4d17cd9c8f9d | 676 | buffer[0] = 0; |
klauss | 0:4d17cd9c8f9d | 677 | return NULL; |
klauss | 0:4d17cd9c8f9d | 678 | } |
klauss | 0:4d17cd9c8f9d | 679 | |
klauss | 0:4d17cd9c8f9d | 680 | if (strlen(via) > 6) { |
klauss | 0:4d17cd9c8f9d | 681 | if (strcasecmp(";rport",via+strlen(via)-6)==0) { |
klauss | 0:4d17cd9c8f9d | 682 | via[strlen(via)-6] = 0; |
klauss | 0:4d17cd9c8f9d | 683 | } |
klauss | 0:4d17cd9c8f9d | 684 | } |
klauss | 0:4d17cd9c8f9d | 685 | |
klauss | 0:4d17cd9c8f9d | 686 | strcpy( buffer, "SIP/2.0 " ); |
klauss | 0:4d17cd9c8f9d | 687 | strcat( buffer, tag ); |
klauss | 0:4d17cd9c8f9d | 688 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 689 | strcat( buffer, "To: " ); |
klauss | 0:4d17cd9c8f9d | 690 | strcat( buffer, to ); |
klauss | 0:4d17cd9c8f9d | 691 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 692 | strcat( buffer, "From: " ); |
klauss | 0:4d17cd9c8f9d | 693 | strcat( buffer, from ); |
klauss | 0:4d17cd9c8f9d | 694 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 695 | strcat( buffer, "Call-ID: " ); |
klauss | 0:4d17cd9c8f9d | 696 | strcat( buffer, callid ); |
klauss | 0:4d17cd9c8f9d | 697 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 698 | strcat( buffer, "CSeq: " ); |
klauss | 0:4d17cd9c8f9d | 699 | strcat( buffer, cseq ); |
klauss | 0:4d17cd9c8f9d | 700 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 701 | strcat( buffer, "Via: " ); |
klauss | 0:4d17cd9c8f9d | 702 | strcat( buffer, via ); |
klauss | 0:4d17cd9c8f9d | 703 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 704 | strcat( buffer, "Server: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 705 | strcat( buffer, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 706 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 707 | strcat( buffer, "Content-Length: 0\r\n\r\n" ); |
klauss | 89:0fe315117b00 | 708 | //buffer[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 709 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 710 | } |
klauss | 0:4d17cd9c8f9d | 711 | char * Sip::build_trying_package( char * buffer, unsigned char * orig ){ |
klauss | 0:4d17cd9c8f9d | 712 | // FIXME trocar 100 para itoa( 100 ... ) |
klauss | 0:4d17cd9c8f9d | 713 | build_generic_reply_package( buffer, orig, "100 Trying" ); |
klauss | 0:4d17cd9c8f9d | 714 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 715 | } |
klauss | 0:4d17cd9c8f9d | 716 | |
klauss | 0:4d17cd9c8f9d | 717 | char * Sip::build_busy_package( char * buffer, unsigned char * orig ){ |
klauss | 0:4d17cd9c8f9d | 718 | // FIXME trocar 100 para itoa( 100 ... ) |
klauss | 0:4d17cd9c8f9d | 719 | build_generic_reply_package( buffer, orig, "486 Busy Here"); |
klauss | 0:4d17cd9c8f9d | 720 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 721 | } |
klauss | 0:4d17cd9c8f9d | 722 | char * Sip::build_reply_package( char * buffer, unsigned char * orig ){ |
klauss | 0:4d17cd9c8f9d | 723 | char to[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 724 | char from[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 725 | char callid[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 726 | char cseq[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 727 | char via[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 728 | char tag[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 729 | |
klauss | 0:4d17cd9c8f9d | 730 | if( decode_gettag( orig, "to: ", to ) == 0) { |
klauss | 0:4d17cd9c8f9d | 731 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 732 | return NULL; |
klauss | 0:4d17cd9c8f9d | 733 | } |
klauss | 0:4d17cd9c8f9d | 734 | if( decode_gettag( orig, "from: ", from ) == 0) { |
klauss | 0:4d17cd9c8f9d | 735 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 736 | return NULL; |
klauss | 0:4d17cd9c8f9d | 737 | } |
klauss | 0:4d17cd9c8f9d | 738 | if( decode_gettag( orig, "call-id: ", callid ) == 0 ) { |
klauss | 0:4d17cd9c8f9d | 739 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 740 | return NULL; |
klauss | 0:4d17cd9c8f9d | 741 | } |
klauss | 0:4d17cd9c8f9d | 742 | if( decode_gettag( orig, "cseq: ", cseq) == 0 ) { |
klauss | 0:4d17cd9c8f9d | 743 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 744 | return NULL; |
klauss | 0:4d17cd9c8f9d | 745 | } |
klauss | 0:4d17cd9c8f9d | 746 | if( decode_gettag( orig, "via: ", via) == 0 ) { |
klauss | 0:4d17cd9c8f9d | 747 | buffer[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 748 | return NULL; |
klauss | 0:4d17cd9c8f9d | 749 | } |
klauss | 0:4d17cd9c8f9d | 750 | |
klauss | 0:4d17cd9c8f9d | 751 | if (strlen(via) > 6) { |
klauss | 0:4d17cd9c8f9d | 752 | if (strcasecmp(";rport",via+strlen(via)-6)==0) { |
klauss | 0:4d17cd9c8f9d | 753 | via[strlen(via)-6] = 0; |
klauss | 0:4d17cd9c8f9d | 754 | } |
klauss | 0:4d17cd9c8f9d | 755 | } |
klauss | 0:4d17cd9c8f9d | 756 | fill_random(tag,18); |
klauss | 0:4d17cd9c8f9d | 757 | |
klauss | 0:4d17cd9c8f9d | 758 | strcpy( buffer, "SIP/2.0 200 OK\r\n" ); |
klauss | 0:4d17cd9c8f9d | 759 | strcat( buffer, "To: " ); |
klauss | 0:4d17cd9c8f9d | 760 | strcat( buffer, to ); |
klauss | 0:4d17cd9c8f9d | 761 | strcat( buffer, ";tag=" ); |
klauss | 0:4d17cd9c8f9d | 762 | strcat( buffer, tag ); |
klauss | 0:4d17cd9c8f9d | 763 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 764 | |
klauss | 0:4d17cd9c8f9d | 765 | strcat( buffer, "From: " ); |
klauss | 0:4d17cd9c8f9d | 766 | strcat( buffer, from ); |
klauss | 0:4d17cd9c8f9d | 767 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 768 | |
klauss | 0:4d17cd9c8f9d | 769 | strcat( buffer, "Call-ID: " ); |
klauss | 0:4d17cd9c8f9d | 770 | strcat( buffer, callid ); |
klauss | 0:4d17cd9c8f9d | 771 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 772 | |
klauss | 0:4d17cd9c8f9d | 773 | strcat( buffer, "CSeq: " ); |
klauss | 0:4d17cd9c8f9d | 774 | strcat( buffer, cseq ); |
klauss | 0:4d17cd9c8f9d | 775 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 776 | |
klauss | 0:4d17cd9c8f9d | 777 | strcat( buffer, "Via: " ); |
klauss | 0:4d17cd9c8f9d | 778 | strcat( buffer, via ); |
klauss | 0:4d17cd9c8f9d | 779 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 780 | |
klauss | 0:4d17cd9c8f9d | 781 | strcat( buffer, "Server: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 782 | strcat( buffer, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 783 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 784 | strcat( buffer, "Content-Length: 0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 785 | strcat( buffer, SIP_ALLOW ); |
klauss | 0:4d17cd9c8f9d | 786 | strcat( buffer, "\r\n\r\n" ); |
klauss | 0:4d17cd9c8f9d | 787 | |
klauss | 89:0fe315117b00 | 788 | //buffer[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 789 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 790 | } |
klauss | 0:4d17cd9c8f9d | 791 | char * Sip::fill_random16h(char * buffer ){ |
klauss | 0:4d17cd9c8f9d | 792 | fill_random( buffer, 16 ); |
klauss | 0:4d17cd9c8f9d | 793 | buffer[ 7 ] = '-'; |
klauss | 0:4d17cd9c8f9d | 794 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 795 | } |
klauss | 0:4d17cd9c8f9d | 796 | |
klauss | 0:4d17cd9c8f9d | 797 | char * Sip::fill_random( char * buffer, int size ){ |
klauss | 0:4d17cd9c8f9d | 798 | static uint16_t seed = time( NULL ); |
klauss | 0:4d17cd9c8f9d | 799 | seed += 1; |
klauss | 0:4d17cd9c8f9d | 800 | srand( seed ); |
klauss | 0:4d17cd9c8f9d | 801 | |
klauss | 0:4d17cd9c8f9d | 802 | int i; |
klauss | 100:09a23fcd3bdf | 803 | for( i = 0; i < size - 1; i++ ) { |
klauss | 0:4d17cd9c8f9d | 804 | buffer[ i ] = fill_random_aux[ rand() & 0x3f ]; |
klauss | 0:4d17cd9c8f9d | 805 | } |
klauss | 100:09a23fcd3bdf | 806 | |
klauss | 100:09a23fcd3bdf | 807 | buffer[ size - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 808 | return( buffer ); |
klauss | 0:4d17cd9c8f9d | 809 | } |
klauss | 0:4d17cd9c8f9d | 810 | |
klauss | 0:4d17cd9c8f9d | 811 | int Sip::get_cseq( void ){ |
klauss | 0:4d17cd9c8f9d | 812 | static unsigned int cseq = 0; |
klauss | 0:4d17cd9c8f9d | 813 | if( cseq == 0 ) { |
klauss | 0:4d17cd9c8f9d | 814 | /* initial value, random number */ |
klauss | 0:4d17cd9c8f9d | 815 | cseq = rand(); |
klauss | 0:4d17cd9c8f9d | 816 | cseq &= 0x7fff; |
klauss | 0:4d17cd9c8f9d | 817 | } |
klauss | 0:4d17cd9c8f9d | 818 | cseq++; |
klauss | 0:4d17cd9c8f9d | 819 | cseq &= 0x7fff; |
klauss | 0:4d17cd9c8f9d | 820 | if( cseq == 0 ) cseq++; |
klauss | 0:4d17cd9c8f9d | 821 | return cseq; |
klauss | 0:4d17cd9c8f9d | 822 | } |
klauss | 0:4d17cd9c8f9d | 823 | |
klauss | 0:4d17cd9c8f9d | 824 | char * Sip::decode_cseq( unsigned char * package, char * cseq ){ |
klauss | 0:4d17cd9c8f9d | 825 | char pkg[ 2048 ], cs[ 2048 ]; |
klauss | 0:4d17cd9c8f9d | 826 | if( decode_gettag( package, "cseq: ", pkg ) != 0) { |
klauss | 0:4d17cd9c8f9d | 827 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 828 | strcpy( cs, pkg ); |
klauss | 0:4d17cd9c8f9d | 829 | while( ( cs[ i ] != 0 ) && ( cs[ i ] != ' ' ) ) i++; |
klauss | 0:4d17cd9c8f9d | 830 | cs[ i ] = 0; |
klauss | 0:4d17cd9c8f9d | 831 | strcpy( cseq, cs ); |
klauss | 0:4d17cd9c8f9d | 832 | } else { |
klauss | 0:4d17cd9c8f9d | 833 | cseq[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 834 | } |
klauss | 0:4d17cd9c8f9d | 835 | return( cseq ); |
klauss | 0:4d17cd9c8f9d | 836 | } |
klauss | 0:4d17cd9c8f9d | 837 | |
klauss | 0:4d17cd9c8f9d | 838 | char * Sip::decode_branch( unsigned char * package, char * branch ){ |
klauss | 0:4d17cd9c8f9d | 839 | char pkg[ 2048 ]; |
klauss | 0:4d17cd9c8f9d | 840 | if( decode_gettag( package, "branch=", pkg ) != 0 ) { |
klauss | 0:4d17cd9c8f9d | 841 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 842 | strcpy( branch, pkg ); |
klauss | 0:4d17cd9c8f9d | 843 | while( ( branch[ i ] != 0 ) && ( branch[ i ] != ';' ) ) i++; |
klauss | 0:4d17cd9c8f9d | 844 | branch[ i ] = 0; |
klauss | 0:4d17cd9c8f9d | 845 | } else { |
klauss | 0:4d17cd9c8f9d | 846 | branch[ 0 ] = 0; |
klauss | 0:4d17cd9c8f9d | 847 | } |
klauss | 0:4d17cd9c8f9d | 848 | return( branch ); |
klauss | 0:4d17cd9c8f9d | 849 | } |
klauss | 0:4d17cd9c8f9d | 850 | int Sip::decode_gettag( unsigned char * package, char * tag, char * out ){ |
klauss | 0:4d17cd9c8f9d | 851 | int size, i, cmpsize; |
klauss | 0:4d17cd9c8f9d | 852 | cmpsize = strlen( tag ); |
klauss | 0:4d17cd9c8f9d | 853 | size = strlen( ( char * )package )-cmpsize; |
klauss | 0:4d17cd9c8f9d | 854 | for ( i=0; i<size; i++) { |
klauss | 0:4d17cd9c8f9d | 855 | if( strncasecmp( tag, (char*)package+i, cmpsize ) == 0) { |
klauss | 0:4d17cd9c8f9d | 856 | char * s; |
klauss | 0:4d17cd9c8f9d | 857 | s = (char*)package+i+cmpsize; |
klauss | 0:4d17cd9c8f9d | 858 | register int j = 0; |
klauss | 0:4d17cd9c8f9d | 859 | while ((s[ j ] != '\r') && (s[ j ] != '\n')) j++; |
klauss | 0:4d17cd9c8f9d | 860 | strncpy( out, s, j ); // copy string to output |
klauss | 0:4d17cd9c8f9d | 861 | out[ j ] = 0; // terminate string |
klauss | 0:4d17cd9c8f9d | 862 | return 1; // found |
klauss | 0:4d17cd9c8f9d | 863 | } |
klauss | 0:4d17cd9c8f9d | 864 | } |
klauss | 0:4d17cd9c8f9d | 865 | return 0; // not found |
klauss | 0:4d17cd9c8f9d | 866 | } |
klauss | 0:4d17cd9c8f9d | 867 | |
klauss | 0:4d17cd9c8f9d | 868 | int Sip::get_id( void ){ |
klauss | 0:4d17cd9c8f9d | 869 | return this->id; |
klauss | 0:4d17cd9c8f9d | 870 | } |
klauss | 0:4d17cd9c8f9d | 871 | int Sip::get_my_rtp_port( void ){ |
klauss | 0:4d17cd9c8f9d | 872 | return this->my_rtp_port; |
klauss | 0:4d17cd9c8f9d | 873 | } |
klauss | 112:6ae726539ab9 | 874 | int Sip::fill_random_rtp_port () |
klauss | 113:db67ae00550e | 875 | { |
klauss | 112:6ae726539ab9 | 876 | static uint16_t port = 0; |
klauss | 112:6ae726539ab9 | 877 | port += 2; |
klauss | 112:6ae726539ab9 | 878 | port &= 0x7fe; // 0 to 2046, always even |
klauss | 113:db67ae00550e | 879 | |
klauss | 112:6ae726539ab9 | 880 | return port + 16384; |
klauss | 0:4d17cd9c8f9d | 881 | } |
klauss | 0:4d17cd9c8f9d | 882 | |
klauss | 0:4d17cd9c8f9d | 883 | void Sip::change_sip_server(){ |
klauss | 0:4d17cd9c8f9d | 884 | // just in case sip server don't answer, invite another server |
klauss | 0:4d17cd9c8f9d | 885 | // mudar para os sets |
klauss | 0:4d17cd9c8f9d | 886 | set_server_ip( get_next_server_ip( this->buffer) ); |
klauss | 0:4d17cd9c8f9d | 887 | set_server_ext( get_next_server_ext() ); |
klauss | 0:4d17cd9c8f9d | 888 | set_server_port( get_next_server_port() ); |
klauss | 0:4d17cd9c8f9d | 889 | registry(); |
klauss | 0:4d17cd9c8f9d | 890 | } |
klauss | 0:4d17cd9c8f9d | 891 | |
klauss | 0:4d17cd9c8f9d | 892 | char * Sip::get_next_server_ip( char * server_ip ){ |
klauss | 0:4d17cd9c8f9d | 893 | strcpy( server_ip, this->server_ip ); |
klauss | 0:4d17cd9c8f9d | 894 | return server_ip; |
klauss | 0:4d17cd9c8f9d | 895 | } |
klauss | 0:4d17cd9c8f9d | 896 | int Sip::get_next_server_port(){ |
klauss | 0:4d17cd9c8f9d | 897 | return this->server_port; |
klauss | 0:4d17cd9c8f9d | 898 | } |
klauss | 0:4d17cd9c8f9d | 899 | int Sip::get_next_server_ext(){ |
klauss | 0:4d17cd9c8f9d | 900 | return this->peer_ext; |
klauss | 0:4d17cd9c8f9d | 901 | } |
klauss | 0:4d17cd9c8f9d | 902 | void Sip::set_server_port( int new_server_port ){ |
klauss | 0:4d17cd9c8f9d | 903 | this->server_port = new_server_port; |
klauss | 0:4d17cd9c8f9d | 904 | } |
klauss | 0:4d17cd9c8f9d | 905 | void Sip::set_server_ext( int new_server_ext ){ |
klauss | 0:4d17cd9c8f9d | 906 | this->peer_ext = new_server_ext; |
klauss | 0:4d17cd9c8f9d | 907 | } |
klauss | 0:4d17cd9c8f9d | 908 | void Sip::set_server_ip( char * new_server_ip ){ |
klauss | 0:4d17cd9c8f9d | 909 | strcpy( this->server_ip, new_server_ip ); |
klauss | 0:4d17cd9c8f9d | 910 | this->server_ip[19] = 0; |
klauss | 0:4d17cd9c8f9d | 911 | } |
klauss | 0:4d17cd9c8f9d | 912 | |
klauss | 0:4d17cd9c8f9d | 913 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 914 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 915 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 916 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 917 | */ |
klauss | 0:4d17cd9c8f9d | 918 | int Sip::listen_SIP_server( void ){ |
klauss | 91:c2a86b1f8aaa | 919 | int ret = listen_SIP_server_return; |
klauss | 91:c2a86b1f8aaa | 920 | listen_SIP_server_return = 0; |
klauss | 91:c2a86b1f8aaa | 921 | return ret; |
klauss | 0:4d17cd9c8f9d | 922 | } |
klauss | 0:4d17cd9c8f9d | 923 | |
klauss | 0:4d17cd9c8f9d | 924 | void Sip::set_ext( int ext ){ |
klauss | 0:4d17cd9c8f9d | 925 | my_ext = ext; |
klauss | 0:4d17cd9c8f9d | 926 | } |
klauss | 0:4d17cd9c8f9d | 927 | |
klauss | 0:4d17cd9c8f9d | 928 | void Sip::set_port( int port ){ |
klauss | 0:4d17cd9c8f9d | 929 | my_port = port; |
klauss | 0:4d17cd9c8f9d | 930 | } |
klauss | 0:4d17cd9c8f9d | 931 | |
klauss | 0:4d17cd9c8f9d | 932 | void Sip::send_bye( void ){ |
klauss | 113:db67ae00550e | 933 | int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); |
klauss | 91:c2a86b1f8aaa | 934 | |
klauss | 91:c2a86b1f8aaa | 935 | if( debug_sip ) send_msg( "sizeof( bye pkg ) :: %d", strlen( buffer ) ); |
klauss | 81:3656f00ab3db | 936 | |
klauss | 113:db67ae00550e | 937 | if( send != strlen( this->buffer ) ) |
klauss | 113:db67ae00550e | 938 | { |
klauss | 113:db67ae00550e | 939 | if( debug_reconnect ) send_msg("Reconnect SIP -- BYE, %d, %d", send, strlen( this->buffer ) ); |
klauss | 113:db67ae00550e | 940 | __reconnect__(); |
klauss | 113:db67ae00550e | 941 | miss_sip_bye_send_pkg++; |
klauss | 81:3656f00ab3db | 942 | } |
klauss | 0:4d17cd9c8f9d | 943 | } |
klauss | 0:4d17cd9c8f9d | 944 | |
klauss | 0:4d17cd9c8f9d | 945 | void Sip::send_unregistry_pkg(){ |
klauss | 0:4d17cd9c8f9d | 946 | build_unregistry_package( this->buffer ); |
klauss | 91:c2a86b1f8aaa | 947 | |
klauss | 113:db67ae00550e | 948 | int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); |
klauss | 113:db67ae00550e | 949 | if( send != strlen( this->buffer ) ) |
klauss | 113:db67ae00550e | 950 | { |
klauss | 113:db67ae00550e | 951 | if( debug_reconnect ) send_msg("Reconnect SIP -- Unregistry"); |
klauss | 113:db67ae00550e | 952 | __reconnect__(); |
klauss | 113:db67ae00550e | 953 | miss_sip_unregistry_send_pkg++; |
klauss | 113:db67ae00550e | 954 | } |
klauss | 113:db67ae00550e | 955 | |
klauss | 109:a5b8264ffbbc | 956 | if( debug_sip ) send_msg("sizeof( unregistry_pkg ) :: %d", strlen( buffer ) ); |
klauss | 0:4d17cd9c8f9d | 957 | } |
klauss | 0:4d17cd9c8f9d | 958 | |
klauss | 0:4d17cd9c8f9d | 959 | char * Sip::build_unregistry_package( char * buffer ){ |
klauss | 0:4d17cd9c8f9d | 960 | char branch[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 961 | char tag[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 962 | char callid[ SIP_MAXFIELDSIZE ]; |
klauss | 0:4d17cd9c8f9d | 963 | fill_random16h( branch ); |
klauss | 0:4d17cd9c8f9d | 964 | fill_random( tag, 18 ); |
klauss | 0:4d17cd9c8f9d | 965 | fill_random16h( callid ); |
klauss | 0:4d17cd9c8f9d | 966 | char itoa_buffer[ 65 ]; |
klauss | 0:4d17cd9c8f9d | 967 | strcpy( buffer, "REGISTER sip:" ); |
klauss | 0:4d17cd9c8f9d | 968 | strcat( buffer, server_ip ); |
klauss | 0:4d17cd9c8f9d | 969 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 970 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 971 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 972 | strcat( buffer, " SIP/2.0\r\nVia: SIP/2.0/UDP " ); |
klauss | 0:4d17cd9c8f9d | 973 | strcat( buffer, my_ip ); |
klauss | 0:4d17cd9c8f9d | 974 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 975 | itoa( my_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 976 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 977 | strcat( buffer, ";branch=" ); |
klauss | 0:4d17cd9c8f9d | 978 | strcat( buffer, branch ); |
klauss | 0:4d17cd9c8f9d | 979 | strcat( buffer, "\r\nFrom: " ); |
klauss | 0:4d17cd9c8f9d | 980 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 981 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 982 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 983 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 984 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 985 | strcat( buffer, server_ip ); |
klauss | 0:4d17cd9c8f9d | 986 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 987 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 988 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 989 | strcat( buffer, ">;tag=" ); |
klauss | 0:4d17cd9c8f9d | 990 | strcat( buffer, tag ); |
klauss | 0:4d17cd9c8f9d | 991 | strcat( buffer, "\r\nTo: " ); |
klauss | 0:4d17cd9c8f9d | 992 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 993 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 994 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 995 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 996 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 997 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 998 | strcat( buffer, server_ip ); |
klauss | 0:4d17cd9c8f9d | 999 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 1000 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 1001 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 1002 | strcat( buffer, ">\r\nCall-ID: " ); |
klauss | 0:4d17cd9c8f9d | 1003 | strcat( buffer, callid ); |
klauss | 0:4d17cd9c8f9d | 1004 | strcat( buffer, "\r\nCSeq: " ); |
klauss | 0:4d17cd9c8f9d | 1005 | itoa( get_cseq(), itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 1006 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 1007 | strcat( buffer, " REGISTER\r\n" ); |
klauss | 0:4d17cd9c8f9d | 1008 | strcat( buffer, "Max-Forwards: " ); |
klauss | 0:4d17cd9c8f9d | 1009 | itoa( 70, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 1010 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 1011 | strcat( buffer, "\r\n" ); |
klauss | 0:4d17cd9c8f9d | 1012 | |
klauss | 0:4d17cd9c8f9d | 1013 | strcat( buffer, "Contact: " ); |
klauss | 0:4d17cd9c8f9d | 1014 | itoa( my_ext, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 1015 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 1016 | strcat( buffer, " <sip:" ); |
klauss | 0:4d17cd9c8f9d | 1017 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 1018 | strcat( buffer, "@" ); |
klauss | 0:4d17cd9c8f9d | 1019 | strcat( buffer, my_ip ); |
klauss | 0:4d17cd9c8f9d | 1020 | strcat( buffer, ":" ); |
klauss | 0:4d17cd9c8f9d | 1021 | itoa( server_port, itoa_buffer, 10 ); |
klauss | 0:4d17cd9c8f9d | 1022 | strcat( buffer, itoa_buffer ); |
klauss | 0:4d17cd9c8f9d | 1023 | strcat( buffer, ">;expires=0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 1024 | strcat( buffer, "User-Agent: VZtech/pabxdriver-" ); |
klauss | 0:4d17cd9c8f9d | 1025 | strcat( buffer, SVNREV ); |
klauss | 0:4d17cd9c8f9d | 1026 | strcat( buffer, "\r\nContent-Length: 0\r\n" ); |
klauss | 0:4d17cd9c8f9d | 1027 | strcat( buffer, SIP_ALLOW ); |
klauss | 0:4d17cd9c8f9d | 1028 | strcat( buffer, "\r\n\r\n" ); |
klauss | 89:0fe315117b00 | 1029 | //buffer[ SIP_MAXMSGSIZE - 1 ] = 0; |
klauss | 0:4d17cd9c8f9d | 1030 | return buffer; |
klauss | 78:1353744f01e1 | 1031 | } |
klauss | 78:1353744f01e1 | 1032 | |
klauss | 78:1353744f01e1 | 1033 | int Sip::get_status( void ){ |
klauss | 78:1353744f01e1 | 1034 | return this->status; |
klauss | 78:1353744f01e1 | 1035 | } |
klauss | 78:1353744f01e1 | 1036 | |
klauss | 78:1353744f01e1 | 1037 | void Sip::sip_set_status( uint8_t status ){ |
klauss | 78:1353744f01e1 | 1038 | this->status = status; |
klauss | 81:3656f00ab3db | 1039 | } |
klauss | 81:3656f00ab3db | 1040 | |
klauss | 81:3656f00ab3db | 1041 | void Sip::sip_check_muted( void ){ |
klauss | 81:3656f00ab3db | 1042 | if( muted == true ) __reconnect__(); |
klauss | 81:3656f00ab3db | 1043 | if( debug_muted ) send_msg("For some reason sip muted, reconnected"); |
klauss | 89:0fe315117b00 | 1044 | } |
klauss | 89:0fe315117b00 | 1045 | |
klauss | 89:0fe315117b00 | 1046 | int Sip::get_socket_fd( void ){ |
klauss | 89:0fe315117b00 | 1047 | return sock.get_fd(); |
klauss | 89:0fe315117b00 | 1048 | } |
klauss | 89:0fe315117b00 | 1049 | |
klauss | 91:c2a86b1f8aaa | 1050 | int Sip::udp_incomming_pkg( void ){ |
klauss | 91:c2a86b1f8aaa | 1051 | Endpoint from; |
klauss | 91:c2a86b1f8aaa | 1052 | |
klauss | 91:c2a86b1f8aaa | 1053 | int length = sock.receiveFrom( from, buffer, sizeof( buffer ) ); |
klauss | 91:c2a86b1f8aaa | 1054 | |
klauss | 113:db67ae00550e | 1055 | //checa se o pacote foi recebido do antigo host conhecido. |
klauss | 91:c2a86b1f8aaa | 1056 | if( memcmp( ( ( u8_t * )( &(from._remoteHost) ) ) + 2, ( ( u8_t * )( &(sip_server._remoteHost) ) ) + 2, 6 ) != 0 ) return 0; |
klauss | 91:c2a86b1f8aaa | 1057 | |
klauss | 113:db67ae00550e | 1058 | if( length == -1 ) |
klauss | 113:db67ae00550e | 1059 | { |
klauss | 113:db67ae00550e | 1060 | if( debug_reconnect ) send_msg("Reconnect SIP -- UDP Incomming"); |
klauss | 113:db67ae00550e | 1061 | __reconnect__(); |
klauss | 113:db67ae00550e | 1062 | miss_sip_inc_pkg++; |
klauss | 113:db67ae00550e | 1063 | } |
klauss | 91:c2a86b1f8aaa | 1064 | |
klauss | 91:c2a86b1f8aaa | 1065 | if( length > 0 ){ |
klauss | 91:c2a86b1f8aaa | 1066 | if( sizeof( buffer ) > length ) buffer[ length ] = 0; |
klauss | 91:c2a86b1f8aaa | 1067 | |
klauss | 91:c2a86b1f8aaa | 1068 | int sip_response = -1; |
klauss | 91:c2a86b1f8aaa | 1069 | |
klauss | 91:c2a86b1f8aaa | 1070 | // faster than atoi() (: |
klauss | 91:c2a86b1f8aaa | 1071 | if( buffer[ 7 ] == ' ' || buffer[ 11 ] == ' ' ){ |
klauss | 91:c2a86b1f8aaa | 1072 | sip_response = ( buffer[ 8 ] - '0' ) * 100 + ( buffer[ 9 ] - '0' ) * 10 + ( buffer[ 10 ] - '0' ); |
klauss | 91:c2a86b1f8aaa | 1073 | } |
klauss | 91:c2a86b1f8aaa | 1074 | |
klauss | 91:c2a86b1f8aaa | 1075 | if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) ){ |
klauss | 113:db67ae00550e | 1076 | //if( !( strncasecmp( buffer + 7, " 200 ", 5 ) ) ){ |
klauss | 113:db67ae00550e | 1077 | if( sip_response == 200 ){ // Ok |
klauss | 91:c2a86b1f8aaa | 1078 | char *ref = strstr( buffer, "audio" ); |
klauss | 91:c2a86b1f8aaa | 1079 | if( debug_invite ) debug_msg("ok"); |
klauss | 91:c2a86b1f8aaa | 1080 | if( ref != NULL ){ |
klauss | 91:c2a86b1f8aaa | 1081 | ref += 6; // audio |
klauss | 91:c2a86b1f8aaa | 1082 | ref = strtok( ref, " "); |
klauss | 91:c2a86b1f8aaa | 1083 | if( call == NULL ){ |
klauss | 91:c2a86b1f8aaa | 1084 | call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); |
klauss | 113:db67ae00550e | 1085 | if( debug_rtp ) debug_msg("Ok invite ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref ); |
klauss | 91:c2a86b1f8aaa | 1086 | if( call == NULL ) memory_is_over = true; |
klauss | 91:c2a86b1f8aaa | 1087 | } |
klauss | 91:c2a86b1f8aaa | 1088 | char ans[ 1024 ]; |
klauss | 91:c2a86b1f8aaa | 1089 | build_ack_package( ans, ( unsigned char *)buffer ); |
klauss | 91:c2a86b1f8aaa | 1090 | |
klauss | 113:db67ae00550e | 1091 | int send = sock.sendTo( sip_server, ans, strlen( ans ) ); |
klauss | 104:62646ef786a3 | 1092 | |
klauss | 113:db67ae00550e | 1093 | if( send != strlen( ans ) ) |
klauss | 104:62646ef786a3 | 1094 | { |
klauss | 113:db67ae00550e | 1095 | if( debug_reconnect ) send_msg("Reconnect SIP -- Ok -- Call mode on"); |
klauss | 113:db67ae00550e | 1096 | __reconnect__(); |
klauss | 113:db67ae00550e | 1097 | miss_sip_ok_send_pkg++; |
klauss | 104:62646ef786a3 | 1098 | } |
klauss | 91:c2a86b1f8aaa | 1099 | |
klauss | 91:c2a86b1f8aaa | 1100 | if( debug_invite ) debug_msg("Call alocada -- Sip::status::%d :: ", status ); |
klauss | 91:c2a86b1f8aaa | 1101 | set_status( status, sip_on_call ); |
klauss | 91:c2a86b1f8aaa | 1102 | } |
klauss | 91:c2a86b1f8aaa | 1103 | } |
klauss | 91:c2a86b1f8aaa | 1104 | } |
klauss | 113:db67ae00550e | 1105 | if( ( status == sip_trying ) || ( status == sip_ringing ) ){ |
klauss | 91:c2a86b1f8aaa | 1106 | //if( !( strncmp( buffer + 7, " 4", 2 ) ) ){ |
klauss | 91:c2a86b1f8aaa | 1107 | if( sip_response >= 400 && sip_response < 700 ){ |
klauss | 91:c2a86b1f8aaa | 1108 | if( debug_invite ) { |
klauss | 91:c2a86b1f8aaa | 1109 | buffer[ 11 ] = 0; |
klauss | 97:8985817e8847 | 1110 | if( debug_invite ) debug_msg("Busy Here :: %s", buffer + 8 ); |
klauss | 91:c2a86b1f8aaa | 1111 | } |
klauss | 91:c2a86b1f8aaa | 1112 | send_bye(); |
klauss | 91:c2a86b1f8aaa | 1113 | //if( call != NULL ) delete( call ); |
klauss | 91:c2a86b1f8aaa | 1114 | set_status( status, sip_busy ); |
klauss | 91:c2a86b1f8aaa | 1115 | return( NULL ); |
klauss | 91:c2a86b1f8aaa | 1116 | }else //if( !( strncasecmp( buffer + 7, " 183 ", 5 ) ) ){ // Session in Progress |
klauss | 91:c2a86b1f8aaa | 1117 | if( sip_response == 183 ){ |
klauss | 91:c2a86b1f8aaa | 1118 | char *ref = strstr( buffer, "audio" ); |
klauss | 91:c2a86b1f8aaa | 1119 | if( ref != NULL ){ |
klauss | 91:c2a86b1f8aaa | 1120 | ref += 6; // strlen( "audio" ) == 6 |
klauss | 91:c2a86b1f8aaa | 1121 | ref = strtok( ref, " "); |
klauss | 91:c2a86b1f8aaa | 1122 | if( call == NULL ){ |
klauss | 91:c2a86b1f8aaa | 1123 | this->call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); |
klauss | 113:db67ae00550e | 1124 | if( debug_rtp ) debug_msg("Session in Progress ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref ); |
klauss | 91:c2a86b1f8aaa | 1125 | if( call == NULL ) memory_is_over = true; |
klauss | 91:c2a86b1f8aaa | 1126 | } |
klauss | 91:c2a86b1f8aaa | 1127 | waiting = true; |
klauss | 91:c2a86b1f8aaa | 1128 | } |
klauss | 91:c2a86b1f8aaa | 1129 | } |
klauss | 91:c2a86b1f8aaa | 1130 | } |
klauss | 91:c2a86b1f8aaa | 1131 | if( status == sip_trying ){ |
klauss | 91:c2a86b1f8aaa | 1132 | //if( !( strncasecmp( buffer + 7, " 180 ", 5 ) ) ){ |
klauss | 91:c2a86b1f8aaa | 1133 | if( sip_response == 180 ){ |
klauss | 91:c2a86b1f8aaa | 1134 | if( debug_invite ) debug_msg("ringing"); |
klauss | 91:c2a86b1f8aaa | 1135 | set_status( status, sip_ringing ); |
klauss | 91:c2a86b1f8aaa | 1136 | } |
klauss | 91:c2a86b1f8aaa | 1137 | } |
klauss | 91:c2a86b1f8aaa | 1138 | if( status == sip_waiting_trying ){ |
klauss | 91:c2a86b1f8aaa | 1139 | if( sip_response == 100 ){ |
klauss | 91:c2a86b1f8aaa | 1140 | if( debug_invite ) debug_msg("trying") |
klauss | 91:c2a86b1f8aaa | 1141 | set_status( status, sip_trying ); |
klauss | 91:c2a86b1f8aaa | 1142 | } |
klauss | 91:c2a86b1f8aaa | 1143 | } |
klauss | 91:c2a86b1f8aaa | 1144 | if( status == sip_on_call ){ |
klauss | 91:c2a86b1f8aaa | 1145 | if( !( strncasecmp( buffer, "bye ", 4 ) ) ){ |
klauss | 91:c2a86b1f8aaa | 1146 | char ans[ 2000 ]; |
klauss | 91:c2a86b1f8aaa | 1147 | build_reply_package( ans, (unsigned char*)buffer ); |
klauss | 91:c2a86b1f8aaa | 1148 | |
klauss | 113:db67ae00550e | 1149 | int send = sock.sendTo( sip_server, ans, strlen( ans ) ); |
klauss | 113:db67ae00550e | 1150 | if( send == strlen( ans ) ) |
klauss | 113:db67ae00550e | 1151 | { |
klauss | 113:db67ae00550e | 1152 | if( debug_reconnect ) send_msg("Reconnect SIP -- RCV BYE from *"); |
klauss | 113:db67ae00550e | 1153 | __reconnect__(); |
klauss | 113:db67ae00550e | 1154 | miss_sip_rcv_bye_send_pkg++; |
klauss | 113:db67ae00550e | 1155 | } |
klauss | 113:db67ae00550e | 1156 | |
klauss | 113:db67ae00550e | 1157 | if( debug_invite ) debug_msg("sizeof( bye reply pkg ) :: %d\nServer request BYE to %d ext\nBye request received from *", strlen( ans ), my_ext ); |
klauss | 113:db67ae00550e | 1158 | |
klauss | 113:db67ae00550e | 1159 | set_sip_rtp_port( 0 ); |
klauss | 108:18a3702650f3 | 1160 | |
klauss | 91:c2a86b1f8aaa | 1161 | listen_SIP_server_return = my_ext; |
klauss | 91:c2a86b1f8aaa | 1162 | } |
klauss | 91:c2a86b1f8aaa | 1163 | } |
klauss | 91:c2a86b1f8aaa | 1164 | } |
klauss | 91:c2a86b1f8aaa | 1165 | return length; |
klauss | 91:c2a86b1f8aaa | 1166 | } |
klauss | 91:c2a86b1f8aaa | 1167 | |
klauss | 91:c2a86b1f8aaa | 1168 | void Sip::reset_call( void ){ |
klauss | 91:c2a86b1f8aaa | 1169 | if( call != NULL ) call = NULL; |
klauss | 97:8985817e8847 | 1170 | } |
klauss | 97:8985817e8847 | 1171 | |
klauss | 97:8985817e8847 | 1172 | int Sip::get_ext( void ){ return my_ext; } |
klauss | 97:8985817e8847 | 1173 | |
klauss | 108:18a3702650f3 | 1174 | int Sip::get_port( void ){ return my_port; } |
klauss | 108:18a3702650f3 | 1175 | |
klauss | 108:18a3702650f3 | 1176 | int Sip::get_sip_rtp_port( void ){ return my_rtp_port; } |
klauss | 108:18a3702650f3 | 1177 | |
klauss | 109:a5b8264ffbbc | 1178 | void Sip::set_sip_rtp_port ( int new_my_rtp_port ) |
klauss | 109:a5b8264ffbbc | 1179 | { |
klauss | 109:a5b8264ffbbc | 1180 | this->my_rtp_port = new_my_rtp_port; |
klauss | 113:db67ae00550e | 1181 | if( debug_rtp ) debug_msg( "this->my_rtp_port ( %d ) = new_my_rtp_port( %d )", this->my_rtp_port, new_my_rtp_port ); |
klauss | 109:a5b8264ffbbc | 1182 | } |
klauss | 109:a5b8264ffbbc | 1183 | |
klauss | 109:a5b8264ffbbc | 1184 | int Sip::print_yourself ( void ) |
klauss | 108:18a3702650f3 | 1185 | { |
klauss | 109:a5b8264ffbbc | 1186 | send_msg (""); |
klauss | 109:a5b8264ffbbc | 1187 | send_msg ("Values ::\r\n"); |
klauss | 109:a5b8264ffbbc | 1188 | send_msg("id :: %d", id ); |
klauss | 109:a5b8264ffbbc | 1189 | send_msg("server_ip :: %s", server_ip ); |
klauss | 109:a5b8264ffbbc | 1190 | send_msg("server_port :: %d", server_port ); |
klauss | 109:a5b8264ffbbc | 1191 | send_msg("my_ip :: %s", my_ip ); |
klauss | 109:a5b8264ffbbc | 1192 | send_msg("my_port :: %d", my_port ); |
klauss | 109:a5b8264ffbbc | 1193 | send_msg("my_ext :: %d", my_ext ); |
klauss | 109:a5b8264ffbbc | 1194 | send_msg("my_rtp_port :: %d", my_rtp_port ); |
klauss | 109:a5b8264ffbbc | 1195 | send_msg("my_display :: %s", my_display ); |
klauss | 109:a5b8264ffbbc | 1196 | send_msg("peer_ext :: %d", peer_ext ); |
klauss | 109:a5b8264ffbbc | 1197 | send_msg("fill_random_aux :: %s", fill_random_aux ); |
klauss | 109:a5b8264ffbbc | 1198 | send_msg("last_invite_tag :: %s", last_invite_tag ); |
klauss | 109:a5b8264ffbbc | 1199 | send_msg("last_invite_callid :: %s", last_invite_callid ); |
klauss | 109:a5b8264ffbbc | 1200 | send_msg("SVNREV :: %s", SVNREV ); |
klauss | 109:a5b8264ffbbc | 1201 | // o.O |
klauss | 109:a5b8264ffbbc | 1202 | send_msg("char buffer[ 1024 ] :: %p", ( void * ) buffer ); |
klauss | 109:a5b8264ffbbc | 1203 | |
klauss | 109:a5b8264ffbbc | 1204 | //FIXME esse tipo de tag existe como metalinguagem ??? |
klauss | 109:a5b8264ffbbc | 1205 | //TOTHINK o que fazer pra entender melhor o estado dos sockets |
klauss | 109:a5b8264ffbbc | 1206 | send_msg("UDPSocket sock :: %p", ( void * ) &sock ); |
klauss | 109:a5b8264ffbbc | 1207 | send_msg("Endpoint sip_server :: %p", ( void * ) &sip_server ); |
klauss | 109:a5b8264ffbbc | 1208 | |
klauss | 109:a5b8264ffbbc | 1209 | send_msg("muted :: %s", ( muted ) ? "true" : "false" ); |
klauss | 109:a5b8264ffbbc | 1210 | send_msg("invite_pkg_sent :: %d", invite_pkg_sent ); |
klauss | 109:a5b8264ffbbc | 1211 | send_msg("call :: %p", ( void * ) call ); |
klauss | 109:a5b8264ffbbc | 1212 | send_msg("invite_timer :: %d", ( int ) invite_timer.read () ); |
klauss | 109:a5b8264ffbbc | 1213 | send_msg("ok_sent :: %d", ok_sent ); |
klauss | 109:a5b8264ffbbc | 1214 | send_msg("waiting :: %s", ( waiting ) ? "true" : "false" ); |
klauss | 109:a5b8264ffbbc | 1215 | send_msg("length_muted :: %d",length_muted ); |
klauss | 109:a5b8264ffbbc | 1216 | send_msg("listen_SIP_server_return :: %d", listen_SIP_server_return ); |
klauss | 109:a5b8264ffbbc | 1217 | send_msg (""); |
klauss | 109:a5b8264ffbbc | 1218 | |
klauss | 109:a5b8264ffbbc | 1219 | return( sizeof( Sip ) ); |
klauss | 108:18a3702650f3 | 1220 | } |