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