Changeset 4397
- Timestamp:
- Feb 27, 2013 1:44:48 PM (12 years ago)
- Location:
- pjproject/branches/1.x
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/1.x
- Property svn:mergeinfo changed
/pjproject/trunk merged: 4317,4333
- Property svn:mergeinfo changed
-
pjproject/branches/1.x/pjlib-util/src/pjlib-util/resolver.c
r4390 r4397 181 181 unsigned char udp_tx_pkt[UDPSZ];/**< UDP receive buffer. */ 182 182 pj_ssize_t udp_len; /**< Length of received packet. */ 183 pj_ioqueue_op_key_t udp_op_key; /**< UDP read operation key. */ 183 pj_ioqueue_op_key_t udp_op_rx_key; /**< UDP read operation key. */ 184 pj_ioqueue_op_key_t udp_op_tx_key; /**< UDP write operation key. */ 184 185 pj_sockaddr_in udp_src_addr; /**< Source address of packet */ 185 186 int udp_addr_len; /**< Source address length. */ … … 222 223 unsigned *count, 223 224 unsigned servers[]); 225 226 227 /* Close UDP socket */ 228 static void close_sock(pj_dns_resolver *resv) 229 { 230 /* Close existing socket */ 231 if (resv->udp_key != NULL) { 232 pj_ioqueue_unregister(resv->udp_key); 233 resv->udp_key = NULL; 234 resv->udp_sock = PJ_INVALID_SOCKET; 235 } else if (resv->udp_sock != PJ_INVALID_SOCKET) { 236 pj_sock_close(resv->udp_sock); 237 resv->udp_sock = PJ_INVALID_SOCKET; 238 } 239 } 240 241 242 /* Initialize UDP socket */ 243 static pj_status_t init_sock(pj_dns_resolver *resv) 244 { 245 pj_ioqueue_callback socket_cb; 246 pj_status_t status; 247 248 /* Create the UDP socket */ 249 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &resv->udp_sock); 250 if (status != PJ_SUCCESS) 251 return status; 252 253 /* Bind to any address/port */ 254 status = pj_sock_bind_in(resv->udp_sock, 0, 0); 255 if (status != PJ_SUCCESS) 256 return status; 257 258 /* Register to ioqueue */ 259 pj_bzero(&socket_cb, sizeof(socket_cb)); 260 socket_cb.on_read_complete = &on_read_complete; 261 status = pj_ioqueue_register_sock(resv->pool, resv->ioqueue, 262 resv->udp_sock, resv, &socket_cb, 263 &resv->udp_key); 264 if (status != PJ_SUCCESS) 265 return status; 266 267 pj_ioqueue_op_key_init(&resv->udp_op_rx_key, sizeof(resv->udp_op_rx_key)); 268 pj_ioqueue_op_key_init(&resv->udp_op_tx_key, sizeof(resv->udp_op_tx_key)); 269 270 /* Start asynchronous read to the UDP socket */ 271 resv->udp_len = sizeof(resv->udp_rx_pkt); 272 resv->udp_addr_len = sizeof(resv->udp_src_addr); 273 status = pj_ioqueue_recvfrom(resv->udp_key, &resv->udp_op_rx_key, 274 resv->udp_rx_pkt, &resv->udp_len, 275 PJ_IOQUEUE_ALWAYS_ASYNC, 276 &resv->udp_src_addr, &resv->udp_addr_len); 277 if (status != PJ_EPENDING) 278 return status; 279 280 return PJ_SUCCESS; 281 } 224 282 225 283 … … 248 306 pj_pool_t *pool; 249 307 pj_dns_resolver *resv; 250 pj_ioqueue_callback socket_cb;251 308 pj_status_t status; 252 309 … … 303 360 pj_list_init(&resv->query_free_nodes); 304 361 305 /* Create the UDP socket */306 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &resv->udp_sock);362 /* Initialize the UDP socket */ 363 status = init_sock(resv); 307 364 if (status != PJ_SUCCESS) 308 365 goto on_error; 309 310 /* Bind to any address/port */311 status = pj_sock_bind_in(resv->udp_sock, 0, 0);312 if (status != PJ_SUCCESS)313 goto on_error;314 315 /* Register to ioqueue */316 pj_bzero(&socket_cb, sizeof(socket_cb));317 socket_cb.on_read_complete = &on_read_complete;318 status = pj_ioqueue_register_sock(pool, resv->ioqueue, resv->udp_sock,319 resv, &socket_cb, &resv->udp_key);320 if (status != PJ_SUCCESS)321 goto on_error;322 323 pj_ioqueue_op_key_init(&resv->udp_op_key, sizeof(resv->udp_op_key));324 325 /* Start asynchronous read to the UDP socket */326 resv->udp_len = sizeof(resv->udp_rx_pkt);327 resv->udp_addr_len = sizeof(resv->udp_src_addr);328 status = pj_ioqueue_recvfrom(resv->udp_key, &resv->udp_op_key,329 resv->udp_rx_pkt, &resv->udp_len,330 PJ_IOQUEUE_ALWAYS_ASYNC,331 &resv->udp_src_addr, &resv->udp_addr_len);332 if (status != PJ_EPENDING)333 goto on_error;334 335 366 336 367 /* Looks like everything is okay */ … … 393 424 } 394 425 395 if (resolver->udp_key != NULL) { 396 pj_ioqueue_unregister(resolver->udp_key); 397 resolver->udp_key = NULL; 398 resolver->udp_sock = PJ_INVALID_SOCKET; 399 } else if (resolver->udp_sock != PJ_INVALID_SOCKET) { 400 pj_sock_close(resolver->udp_sock); 401 resolver->udp_sock = PJ_INVALID_SOCKET; 402 } 426 close_sock(resolver); 403 427 404 428 if (resolver->own_ioqueue && resolver->ioqueue) { … … 562 586 pj_status_t status; 563 587 564 /* Create DNS query packet */565 pkt_size = sizeof(resolver->udp_tx_pkt);566 name = pj_str(q->key.name);567 status = pj_dns_make_query(resolver->udp_tx_pkt, &pkt_size,568 q->id, q->key.qtype, &name);569 if (status != PJ_SUCCESS) {570 return status;571 }572 573 588 /* Select which nameserver(s) to send requests to. */ 574 589 server_cnt = PJ_ARRAY_SIZE(servers); … … 596 611 } 597 612 613 /* Check if the socket is available for sending */ 614 if (pj_ioqueue_is_pending(resolver->udp_key, &resolver->udp_op_tx_key)) { 615 ++q->transmit_cnt; 616 PJ_LOG(4,(resolver->name.ptr, 617 "Socket busy in transmitting DNS %s query for %s%s", 618 pj_dns_get_type_name(q->key.qtype), 619 q->key.name, 620 (q->transmit_cnt < resolver->settings.qretr_count? 621 ", will try again later":""))); 622 return PJ_SUCCESS; 623 } 624 625 /* Create DNS query packet */ 626 pkt_size = sizeof(resolver->udp_tx_pkt); 627 name = pj_str(q->key.name); 628 status = pj_dns_make_query(resolver->udp_tx_pkt, &pkt_size, 629 q->id, q->key.qtype, &name); 630 if (status != PJ_SUCCESS) { 631 pj_timer_heap_cancel(resolver->timer, &q->timer_entry); 632 return status; 633 } 634 598 635 /* Get current time. */ 599 636 pj_gettimeofday(&now); … … 604 641 struct nameserver *ns = &resolver->ns[servers[i]]; 605 642 606 pj_sock_sendto(resolver->udp_sock, resolver->udp_tx_pkt, &sent, 0, 607 &resolver->ns[servers[i]].addr, sizeof(pj_sockaddr_in)); 608 609 PJ_LOG(4,(resolver->name.ptr, 643 status = pj_ioqueue_sendto(resolver->udp_key, 644 &resolver->udp_op_tx_key, 645 resolver->udp_tx_pkt, &sent, 0, 646 &resolver->ns[servers[i]].addr, 647 sizeof(pj_sockaddr_in)); 648 649 PJ_PERROR(4,(resolver->name.ptr, status, 610 650 "%s %d bytes to NS %d (%s:%d): DNS %s query for %s", 611 651 (q->transmit_cnt==0? "Transmitting":"Re-transmitting"), 612 (int) sent, servers[i],652 (int)pkt_size, servers[i], 613 653 pj_inet_ntoa(ns->addr.sin_addr), 614 654 (int)pj_ntohs(ns->addr.sin_port),
Note: See TracChangeset
for help on using the changeset viewer.