/* * generic process function */ static void processfds(ares_channel channel, fd_set *read_fds, ares_socket_t read_fd, fd_set *write_fds, ares_socket_t write_fd) { struct timeval now = ares__tvnow(); write_tcp_data(channel, write_fds, write_fd, &now); read_tcp_data(channel, read_fds, read_fd, &now); read_udp_packets(channel, read_fds, read_fd, &now); process_timeouts(channel, &now); process_broken_connections(channel, &now); }
/* Something interesting happened on the wire, or there was a timeout. * See what's up and respond accordingly. */ void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds) { time_t now; time(&now); write_tcp_data(channel, write_fds, now); read_tcp_data(channel, -1, read_fds, now); read_udp_packets(channel, -1, read_fds, now); process_timeouts(channel, now); /* See if our local pseudo-db has any results. */ /* Querying this only on timeouts is OK (is not high-performance) */ ares_local_process_requests(); }
void ares__send_query(ares_channel channel, struct query *query, time_t now) { struct send_request *sendreq; struct server_state *server; server = &channel->servers[query->server]; if (query->using_tcp) { int tryWrite = 0; /* Make sure the TCP socket for this server is set up and queue * a send request. */ if (server->tcp_socket == -1) { if (open_tcp_socket(channel, server) == -1) { query->skip_server[query->server] = 1; next_server(channel, query, now); return; } if ( channel->poll_cb_func ) { // printf("ares_send_q: pollopen tcp fd=%d\n", server->tcp_socket); (*(channel->poll_cb_func))( channel->poll_cb_data, channel, query->server, server->tcp_socket, ARES_POLLACTION_OPEN); } } sendreq = malloc(sizeof(struct send_request)); if (!sendreq) end_query(channel, query, ARES_ENOMEM, NULL, 0); sendreq->data = query->tcpbuf; sendreq->len = query->tcplen; sendreq->next = NULL; if (server->qtail) { server->qtail->next = sendreq; } else { server->qhead = sendreq; tryWrite = 1; } server->qtail = sendreq; query->timeout = 0; if ( tryWrite ) { #if 0 time_t now; time(&now); write_tcp_data(channel, query->server, now); /* XXX: the write code doesn't seem to handle EAGAIN properly! */ #else if ( channel->poll_cb_func ) (*(channel->poll_cb_func))( channel->poll_cb_data, channel, query->server, server->tcp_socket, ARES_POLLACTION_WRITEON); #endif } } else { if (server->udp_socket == -1) { if (open_udp_socket(channel, server) == -1) { //fprintf(stderr,"kennard:ares:send_query:open_udp failed\n"); query->skip_server[query->server] = 1; next_server(channel, query, now); return; } if ( channel->poll_cb_func ) { // printf("ares_send_q: pollopen udp fd=%d\n", server->udp_socket); (*(channel->poll_cb_func))( channel->poll_cb_data, channel, query->server, server->udp_socket, ARES_POLLACTION_OPEN); } } if (send(server->udp_socket, query->qbuf, query->qlen, 0) == -1) { //fprintf(stderr,"kennard:ares:send_query:send_udp failed\n"); query->skip_server[query->server] = 1; next_server(channel, query, now); return; } query->timeout = now + ((query->itry == 0) ? channel->timeout : channel->timeout << query->itry / channel->nservers); } }