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