Changeset 5359
- Timestamp:
- Jun 28, 2016 6:33:20 AM (8 years ago)
- Location:
- pjproject/trunk/pjnath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/include/pjnath/nat_detect.h
r3553 r5359 175 175 * asynchronously. Application can register a callback to be notified 176 176 * when such detection has completed. 177 * 178 * See also #pj_stun_detect_nat_type2() which supports IPv6. 177 179 * 178 180 * @param server STUN server address. … … 198 200 199 201 /** 202 * Variant of #pj_stun_detect_nat_type() that supports IPv6. 203 * 204 * @param server STUN server address. 205 * @param stun_cfg A structure containing various STUN configurations, 206 * such as the ioqueue and timer heap instance used 207 * to receive network I/O and timer events. 208 * @param user_data Application data, which will be returned back 209 * in the callback. 210 * @param cb Callback to be registered to receive notification 211 * about detection result. 212 * 213 * @return If this function returns PJ_SUCCESS, the procedure 214 * will complete asynchronously and callback will be 215 * called when it completes. For other return 216 * values, it means that an error has occured and 217 * the procedure did not start. 218 */ 219 PJ_DECL(pj_status_t) pj_stun_detect_nat_type2(const pj_sockaddr *server, 220 pj_stun_config *stun_cfg, 221 void *user_data, 222 pj_stun_nat_detect_cb *cb); 223 224 225 /** 200 226 * @} 201 227 */ -
pjproject/trunk/pjnath/src/pjnath/nat_detect.c
r5311 r5359 85 85 pj_stun_nat_detect_cb *cb; 86 86 pj_sock_t sock; 87 pj_sockaddr _inlocal_addr;87 pj_sockaddr local_addr; 88 88 pj_ioqueue_key_t *key; 89 pj_sockaddr _inserver;90 pj_sockaddr _in*cur_server;89 pj_sockaddr server; 90 pj_sockaddr *cur_server; 91 91 pj_stun_session *stun_sess; 92 92 … … 94 94 pj_uint8_t rx_pkt[PJ_STUN_MAX_PKT_LEN]; 95 95 pj_ssize_t rx_pkt_len; 96 pj_sockaddr _insrc_addr;96 pj_sockaddr src_addr; 97 97 int src_addr_len; 98 98 … … 102 102 pj_bool_t complete; 103 103 pj_status_t status; 104 pj_sockaddr _inma;105 pj_sockaddr _inca;104 pj_sockaddr ma; 105 pj_sockaddr ca; 106 106 pj_stun_tx_data *tdata; 107 107 } result[ST_MAX]; … … 129 129 static pj_status_t send_test(nat_detect_session *sess, 130 130 enum test_type test_id, 131 const pj_sockaddr _in*alt_addr,131 const pj_sockaddr *alt_addr, 132 132 pj_uint32_t change_flag); 133 133 static void on_sess_timer(pj_timer_heap_t *th, … … 167 167 } 168 168 169 static pj_status_t get_local_interface(const pj_sockaddr _in*server,170 pj_ in_addr *local_addr)169 static pj_status_t get_local_interface(const pj_sockaddr *server, 170 pj_sockaddr *local_addr) 171 171 { 172 172 pj_sock_t sock; 173 pj_sockaddr _in tmp;173 pj_sockaddr tmp, local; 174 174 int addr_len; 175 175 pj_status_t status; 176 176 177 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock); 177 status = pj_sock_socket(server->addr.sa_family, pj_SOCK_DGRAM(), 178 0, &sock); 178 179 if (status != PJ_SUCCESS) 179 180 return status; 180 181 181 status = pj_sock_bind_in(sock, 0, 0); 182 addr_len = pj_sockaddr_get_len(server); 183 pj_bzero(&local, sizeof(pj_sockaddr)); 184 status = pj_sock_bind(sock, &local, addr_len); 182 185 if (status != PJ_SUCCESS) { 183 186 pj_sock_close(sock); … … 185 188 } 186 189 187 status = pj_sock_connect(sock, server, sizeof(pj_sockaddr_in));190 status = pj_sock_connect(sock, server, addr_len); 188 191 if (status != PJ_SUCCESS) { 189 192 pj_sock_close(sock); … … 191 194 } 192 195 193 addr_len = sizeof(pj_sockaddr_in);194 196 status = pj_sock_getsockname(sock, &tmp, &addr_len); 195 197 if (status != PJ_SUCCESS) { … … 198 200 } 199 201 200 local_addr->s_addr = tmp.sin_addr.s_addr;202 pj_sockaddr_cp(local_addr, &tmp); 201 203 202 204 pj_sock_close(sock); … … 210 212 pj_stun_nat_detect_cb *cb) 211 213 { 214 pj_sockaddr srv; 215 216 if (server) 217 pj_sockaddr_cp(&srv, server); 218 219 return pj_stun_detect_nat_type2(&srv, stun_cfg, user_data, cb); 220 } 221 222 PJ_DEF(pj_status_t) pj_stun_detect_nat_type2(const pj_sockaddr *server, 223 pj_stun_config *stun_cfg, 224 void *user_data, 225 pj_stun_nat_detect_cb *cb) 226 { 212 227 pj_pool_t *pool; 213 228 nat_detect_session *sess; 214 229 pj_stun_session_cb sess_cb; 215 230 pj_ioqueue_callback ioqueue_cb; 216 int a ddr_len;217 char addr[PJ_INET _ADDRSTRLEN];231 int af, addr_len; 232 char addr[PJ_INET6_ADDRSTRLEN]; 218 233 pj_status_t status; 219 234 … … 245 260 pj_grp_lock_add_handler(sess->grp_lock, pool, sess, &sess_on_destroy); 246 261 247 pj_ memcpy(&sess->server, server, sizeof(pj_sockaddr_in));262 pj_sockaddr_cp(&sess->server, server); 248 263 249 264 /* … … 258 273 * Initialize socket. 259 274 */ 260 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sess->sock); 275 af = server->addr.sa_family; 276 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sess->sock); 261 277 if (status != PJ_SUCCESS) 262 278 goto on_error; … … 265 281 * Bind to any. 266 282 */ 267 pj_bzero(&sess->local_addr, sizeof(pj_sockaddr_in)); 268 sess->local_addr.sin_family = pj_AF_INET(); 269 status = pj_sock_bind(sess->sock, &sess->local_addr, 270 sizeof(pj_sockaddr_in)); 283 pj_bzero(&sess->local_addr, sizeof(pj_sockaddr)); 284 addr_len = pj_sockaddr_get_len(server); 285 status = pj_sock_bind(sess->sock, &sess->local_addr, addr_len); 271 286 if (status != PJ_SUCCESS) 272 287 goto on_error; … … 275 290 * Get local/bound address. 276 291 */ 277 addr_len = sizeof(sess->local_addr);278 292 status = pj_sock_getsockname(sess->sock, &sess->local_addr, &addr_len); 279 293 if (status != PJ_SUCCESS) … … 283 297 * Find out which interface is used to send to the server. 284 298 */ 285 status = get_local_interface(server, &sess->local_addr .sin_addr);299 status = get_local_interface(server, &sess->local_addr); 286 300 if (status != PJ_SUCCESS) 287 301 goto on_error; 288 302 289 303 PJ_LOG(5,(sess->pool->obj_name, "Local address is %s:%d", 290 pj_inet_ntop2(pj_AF_INET(), &sess->local_addr.sin_addr, 291 addr, sizeof(addr)), 292 pj_ntohs(sess->local_addr.sin_port))); 304 pj_sockaddr_print(&sess->local_addr, addr, sizeof(addr), 0), 305 pj_sockaddr_get_port(&sess->local_addr))); 293 306 294 307 PJ_LOG(5,(sess->pool->obj_name, "Server set to %s:%d", 295 pj_inet_ntop2(pj_AF_INET(), &server->sin_addr, addr, 296 sizeof(addr)), 297 pj_ntohs(server->sin_port))); 308 pj_sockaddr_print(server, addr, sizeof(addr), 0), 309 pj_sockaddr_get_port(server))); 298 310 299 311 /* … … 575 587 sess->result[test_id].status = status; 576 588 if (status == PJ_SUCCESS) { 577 pj_memcpy(&sess->result[test_id].ma, &mattr->sockaddr.ipv4, 578 sizeof(pj_sockaddr_in)); 579 pj_memcpy(&sess->result[test_id].ca, &ca->sockaddr.ipv4, 580 sizeof(pj_sockaddr_in)); 589 pj_sockaddr_cp(&sess->result[test_id].ma, &mattr->sockaddr); 590 pj_sockaddr_cp(&sess->result[test_id].ca, &ca->sockaddr); 581 591 } 582 592 … … 590 600 sess->result[ST_TEST_1].status == PJ_SUCCESS) 591 601 { 592 cmp = pj_memcmp(&sess->local_addr, &sess->result[ST_TEST_1].ma, 593 sizeof(pj_sockaddr_in)); 602 cmp = pj_sockaddr_cmp(&sess->local_addr, &sess->result[ST_TEST_1].ma); 594 603 if (cmp != 0) 595 604 send_test(sess, ST_TEST_1B, &sess->result[ST_TEST_1].ca, 0); … … 660 669 * NAT type. Compare the MAPPED-ADDRESS with the local address. 661 670 */ 662 cmp = pj_memcmp(&sess->local_addr, &sess->result[ST_TEST_1].ma, 663 sizeof(pj_sockaddr_in)); 671 cmp = pj_sockaddr_cmp(&sess->local_addr, &sess->result[ST_TEST_1].ma); 664 672 if (cmp==0) { 665 673 /* … … 711 719 * MAPPED-ADDRESS returned in test 1.. 712 720 */ 713 cmp = pj_memcmp(&sess->result[ST_TEST_1].ma, 714 &sess->result[ST_TEST_1B].ma, 715 sizeof(pj_sockaddr_in)); 721 cmp = pj_sockaddr_cmp(&sess->result[ST_TEST_1].ma, 722 &sess->result[ST_TEST_1B].ma); 716 723 if (cmp != 0) { 717 724 /* … … 814 821 static pj_status_t send_test(nat_detect_session *sess, 815 822 enum test_type test_id, 816 const pj_sockaddr _in*alt_addr,823 const pj_sockaddr *alt_addr, 817 824 pj_uint32_t change_flag) 818 825 { 819 826 pj_uint32_t magic, tsx_id[3]; 820 char addr[PJ_INET _ADDRSTRLEN];827 char addr[PJ_INET6_ADDRSTRLEN]; 821 828 pj_status_t status; 822 829 … … 850 857 /* Configure alternate address */ 851 858 if (alt_addr) 852 sess->cur_server = (pj_sockaddr _in*) alt_addr;859 sess->cur_server = (pj_sockaddr*) alt_addr; 853 860 else 854 861 sess->cur_server = &sess->server; … … 857 864 "Performing %s to %s:%d", 858 865 test_names[test_id], 859 pj_inet_ntop2(pj_AF_INET(), &sess->cur_server->sin_addr, 860 addr, sizeof(addr)), 861 pj_ntohs(sess->cur_server->sin_port))); 866 pj_sockaddr_print(sess->cur_server, addr, sizeof(addr), 0), 867 pj_sockaddr_get_port(sess->cur_server))); 862 868 863 869 /* Send the request */ 864 870 status = pj_stun_session_send_msg(sess->stun_sess, NULL, PJ_TRUE, 865 871 PJ_TRUE, sess->cur_server, 866 sizeof(pj_sockaddr_in),872 pj_sockaddr_get_len(sess->cur_server), 867 873 sess->result[test_id].tdata); 868 874 if (status != PJ_SUCCESS)
Note: See TracChangeset
for help on using the changeset viewer.