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