From 428daba5cefd8e0e66cac3b0bfaaa5ca5ffaeadd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= Date: Mon, 29 Oct 2018 09:15:42 +0000 Subject: [PATCH] Do not use vector socket functions in diagnostic tools Refactor diagnostic tools code to no longer use: - isc_socket_recvv() - isc_socket_sendtov2() - isc_socket_sendv() as these functions will be removed shortly. --- bin/dig/dighost.c | 156 +++++++++++++++----------------------- bin/dig/include/dig/dig.h | 12 +-- bin/dig/nslookup.c | 6 -- 3 files changed, 64 insertions(+), 110 deletions(-) diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c index 522c5761f4..f79c92a98d 100644 --- a/bin/dig/dighost.c +++ b/bin/dig/dighost.c @@ -1534,12 +1534,6 @@ clear_query(dig_query_t *query) { ISC_LIST_UNLINK(lookup->q, query, link); if (ISC_LINK_LINKED(query, clink)) ISC_LIST_UNLINK(lookup->connecting, query, clink); - if (ISC_LINK_LINKED(&query->recvbuf, link)) - ISC_LIST_DEQUEUE(query->recvlist, &query->recvbuf, - link); - if (ISC_LINK_LINKED(&query->lengthbuf, link)) - ISC_LIST_DEQUEUE(query->lengthlist, &query->lengthbuf, - link); INSIST(query->recvspace != NULL); if (query->sock != NULL) { @@ -1548,6 +1542,7 @@ clear_query(dig_query_t *query) { debug("sockcount=%d", sockcount); } isc_mempool_put(commctx, query->recvspace); + isc_mempool_put(commctx, query->tmpsendspace); isc_buffer_invalidate(&query->recvbuf); isc_buffer_invalidate(&query->lengthbuf); if (query->waiting_senddone) @@ -2488,16 +2483,15 @@ setup_lookup(dig_lookup_t *lookup) { query->msg_count = 0; query->byte_count = 0; query->ixfr_axfr = false; - ISC_LIST_INIT(query->recvlist); - ISC_LIST_INIT(query->lengthlist); query->sock = NULL; query->recvspace = isc_mempool_get(commctx); + query->tmpsendspace = isc_mempool_get(commctx); if (query->recvspace == NULL) fatal("memory allocation failure"); isc_buffer_init(&query->recvbuf, query->recvspace, COMMSIZE); isc_buffer_init(&query->lengthbuf, query->lengthspace, 2); - isc_buffer_init(&query->slbuf, query->slspace, 2); + isc_buffer_init(&query->tmpsendbuf, query->tmpsendspace, COMMSIZE); query->sendbuf = lookup->renderbuf; ISC_LINK_INIT(query, clink); @@ -2523,8 +2517,6 @@ setup_lookup(dig_lookup_t *lookup) { */ static void send_done(isc_task_t *_task, isc_event_t *event) { - isc_socketevent_t *sevent = (isc_socketevent_t *)event; - isc_buffer_t *b = NULL; dig_query_t *query, *next; dig_lookup_t *l; @@ -2539,13 +2531,6 @@ send_done(isc_task_t *_task, isc_event_t *event) { debug("sendcount=%d", sendcount); INSIST(sendcount >= 0); - for (b = ISC_LIST_HEAD(sevent->bufferlist); - b != NULL; - b = ISC_LIST_HEAD(sevent->bufferlist)) { - ISC_LIST_DEQUEUE(sevent->bufferlist, b, link); - isc_mem_free(mctx, b); - } - query = event->ev_arg; query->waiting_senddone = false; l = query->lookup; @@ -2777,17 +2762,6 @@ send_tcp_connect(dig_query_t *query) { } } -static isc_buffer_t * -clone_buffer(isc_buffer_t *source) { - isc_buffer_t *buffer; - buffer = isc_mem_allocate(mctx, sizeof(*buffer)); - if (buffer == NULL) - fatal("memory allocation failure in %s:%d", - __FILE__, __LINE__); - *buffer = *source; - return (buffer); -} - /*% * Send a UDP packet to the remote nameserver, possible starting the * recv action as well. Also make sure that the timer is running and @@ -2797,8 +2771,9 @@ static void send_udp(dig_query_t *query) { dig_lookup_t *l = NULL; isc_result_t result; - isc_buffer_t *sendbuf; dig_query_t *next; + isc_region_t r; + isc_socketevent_t *sevent; debug("send_udp(%p)", query); @@ -2859,29 +2834,27 @@ send_udp(dig_query_t *query) { check_result(result, "isc_socket_bind"); query->recv_made = true; - ISC_LINK_INIT(&query->recvbuf, link); - ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, - link); + isc_buffer_availableregion(&query->recvbuf, &r); debug("recving with lookup=%p, query=%p, sock=%p", query->lookup, query, query->sock); - result = isc_socket_recvv(query->sock, &query->recvlist, 1, - global_task, recv_done, query); - check_result(result, "isc_socket_recvv"); + result = isc_socket_recv(query->sock, &r, 1, + global_task, recv_done, query); + check_result(result, "isc_socket_recv"); recvcount++; debug("recvcount=%d", recvcount); } - ISC_LIST_INIT(query->sendlist); - sendbuf = clone_buffer(&query->sendbuf); - ISC_LIST_ENQUEUE(query->sendlist, sendbuf, link); + isc_buffer_usedregion(&query->sendbuf, &r); debug("sending a request"); TIME_NOW(&query->time_sent); INSIST(query->sock != NULL); query->waiting_senddone = true; - result = isc_socket_sendtov2(query->sock, &query->sendlist, - global_task, send_done, query, - &query->sockaddr, NULL, - ISC_SOCKFLAG_NORETRY); - check_result(result, "isc_socket_sendtov"); + sevent = isc_socket_socketevent(mctx, query->sock, + ISC_SOCKEVENT_SENDDONE, + send_done, query); + result = isc_socket_sendto2(query->sock, &r, + global_task, &query->sockaddr, NULL, + sevent, ISC_SOCKFLAG_NORETRY); + check_result(result, "isc_socket_sendto2"); sendcount++; } @@ -2971,7 +2944,8 @@ connect_timeout(isc_task_t *task, isc_event_t *event) { static void tcp_length_done(isc_task_t *task, isc_event_t *event) { isc_socketevent_t *sevent; - isc_buffer_t *b = NULL; + isc_buffer_t b; + isc_region_t r; isc_result_t result; dig_query_t *query = NULL; dig_lookup_t *l, *n; @@ -2991,10 +2965,6 @@ tcp_length_done(isc_task_t *task, isc_event_t *event) { recvcount--; INSIST(recvcount >= 0); - b = ISC_LIST_HEAD(sevent->bufferlist); - INSIST(b == &query->lengthbuf); - ISC_LIST_DEQUEUE(sevent->bufferlist, b, link); - if (sevent->result == ISC_R_CANCELED) { isc_event_free(&event); l = query->lookup; @@ -3027,7 +2997,10 @@ tcp_length_done(isc_task_t *task, isc_event_t *event) { UNLOCK_LOOKUP; return; } - length = isc_buffer_getuint16(b); + isc_buffer_init(&b, sevent->region.base, sevent->n); + isc_buffer_add(&b, sevent->n); + length = isc_buffer_getuint16(&b); + if (length == 0) { isc_event_free(&event); launch_next_query(query, false); @@ -3041,13 +3014,11 @@ tcp_length_done(isc_task_t *task, isc_event_t *event) { */ isc_buffer_invalidate(&query->recvbuf); isc_buffer_init(&query->recvbuf, query->recvspace, length); - ENSURE(ISC_LIST_EMPTY(query->recvlist)); - ISC_LINK_INIT(&query->recvbuf, link); - ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link); + isc_buffer_availableregion(&query->recvbuf, &r); debug("recving with lookup=%p, query=%p", query->lookup, query); - result = isc_socket_recvv(query->sock, &query->recvlist, length, task, - recv_done, query); - check_result(result, "isc_socket_recvv"); + result = isc_socket_recv(query->sock, &r, length, task, + recv_done, query); + check_result(result, "isc_socket_recv"); recvcount++; debug("resubmitted recv request with length %d, recvcount=%d", length, recvcount); @@ -3063,7 +3034,7 @@ static void launch_next_query(dig_query_t *query, bool include_question) { isc_result_t result; dig_lookup_t *l; - isc_buffer_t *buffer; + isc_region_t r; INSIST(!free_now); @@ -3082,35 +3053,28 @@ launch_next_query(dig_query_t *query, bool include_question) { return; } - isc_buffer_clear(&query->slbuf); isc_buffer_clear(&query->lengthbuf); - isc_buffer_putuint16(&query->slbuf, (uint16_t) query->sendbuf.used); - ISC_LIST_INIT(query->sendlist); - ISC_LINK_INIT(&query->slbuf, link); - if (!query->first_soa_rcvd) { - buffer = clone_buffer(&query->slbuf); - ISC_LIST_ENQUEUE(query->sendlist, buffer, link); - if (include_question) { - buffer = clone_buffer(&query->sendbuf); - ISC_LIST_ENQUEUE(query->sendlist, buffer, link); - } - } - - ISC_LINK_INIT(&query->lengthbuf, link); - ISC_LIST_ENQUEUE(query->lengthlist, &query->lengthbuf, link); - - result = isc_socket_recvv(query->sock, &query->lengthlist, 0, - global_task, tcp_length_done, query); - check_result(result, "isc_socket_recvv"); + isc_buffer_availableregion(&query->lengthbuf, &r); + result = isc_socket_recv(query->sock, &r, 0, + global_task, tcp_length_done, query); + check_result(result, "isc_socket_recv"); recvcount++; debug("recvcount=%d", recvcount); if (!query->first_soa_rcvd) { debug("sending a request in launch_next_query"); TIME_NOW(&query->time_sent); query->waiting_senddone = true; - result = isc_socket_sendv(query->sock, &query->sendlist, - global_task, send_done, query); - check_result(result, "isc_socket_sendv"); + isc_buffer_clear(&query->tmpsendbuf); + isc_buffer_putuint16(&query->tmpsendbuf, + isc_buffer_usedlength(&query->sendbuf)); + if (include_question) { + isc_buffer_usedregion(&query->sendbuf, &r); + isc_buffer_copyregion(&query->tmpsendbuf, &r); + } + isc_buffer_usedregion(&query->tmpsendbuf, &r); + result = isc_socket_send(query->sock, &r, + global_task, send_done, query); + check_result(result, "isc_socket_send"); sendcount++; debug("sendcount=%d", sendcount); } @@ -3460,8 +3424,9 @@ ednsvers(dns_rdataset_t *opt) { static void recv_done(isc_task_t *task, isc_event_t *event) { isc_socketevent_t *sevent = NULL; + isc_region_t r; dig_query_t *query = NULL; - isc_buffer_t *b = NULL; + isc_buffer_t b; dns_message_t *msg = NULL; isc_result_t result; dig_lookup_t *n, *l; @@ -3491,9 +3456,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE); sevent = (isc_socketevent_t *)event; - b = ISC_LIST_HEAD(sevent->bufferlist); - INSIST(b == &query->recvbuf); - ISC_LIST_DEQUEUE(sevent->bufferlist, &query->recvbuf, link); + isc_buffer_init(&b, sevent->region.base, sevent->n); + isc_buffer_add(&b, sevent->n); if ((l->tcp_mode) && (query->timer != NULL)) isc_timer_touch(query->timer); @@ -3569,7 +3533,7 @@ recv_done(isc_task_t *task, isc_event_t *event) { } } - result = dns_message_peekheader(b, &id, &msgflags); + result = dns_message_peekheader(&b, &id, &msgflags); if (result != ISC_R_SUCCESS || l->sendmsg->id != id) { match = false; if (l->tcp_mode) { @@ -3638,7 +3602,7 @@ recv_done(isc_task_t *task, isc_event_t *event) { parseflags |= DNS_MESSAGEPARSE_BESTEFFORT; parseflags |= DNS_MESSAGEPARSE_IGNORETRUNCATION; } - result = dns_message_parse(msg, b, parseflags); + result = dns_message_parse(msg, &b, parseflags); if (result == DNS_R_RECOVERABLE) { printf(";; Warning: Message parser reports malformed " "message packet.\n"); @@ -3646,7 +3610,7 @@ recv_done(isc_task_t *task, isc_event_t *event) { } if (result != ISC_R_SUCCESS) { printf(";; Got bad packet: %s\n", isc_result_totext(result)); - hex_dump(b); + hex_dump(&b); query->waiting_connect = false; dns_message_destroy(&msg); isc_event_free(&event); @@ -3800,7 +3764,7 @@ recv_done(isc_task_t *task, isc_event_t *event) { } if (tsigkey != NULL) { - result = dns_tsig_verify(&query->recvbuf, msg, NULL, NULL); + result = dns_tsig_verify(&b, msg, NULL, NULL); if (result != ISC_R_SUCCESS) { printf(";; Couldn't verify signature: %s\n", isc_result_totext(result)); @@ -3816,7 +3780,7 @@ recv_done(isc_task_t *task, isc_event_t *event) { check_result(result,"dns_message_getquerytsig"); } - extrabytes = isc_buffer_remaininglength(b); + extrabytes = isc_buffer_remaininglength(&b); debug("after parse"); if (l->doing_xfr && l->xfr_q == NULL) { @@ -3863,8 +3827,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { (l->origin != NULL || l->need_search)) { if (!next_origin(query->lookup) || showsearch) { dighost_printmessage(query, msg, true); - dighost_received(b->used, &sevent->address, - query); + dighost_received(isc_buffer_usedlength(&b), + &sevent->address, query); } } else if (!l->trace && !l->ns_search_only) { dighost_printmessage(query, msg, true); @@ -3931,7 +3895,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { } else { if (msg->rcode == dns_rcode_noerror || l->origin == NULL) { - dighost_received(b->used, &sevent->address, query); + dighost_received(isc_buffer_usedlength(&b), + &sevent->address, query); } if (!query->lookup->ns_search_only) @@ -3954,11 +3919,10 @@ recv_done(isc_task_t *task, isc_event_t *event) { udp_mismatch: isc_buffer_invalidate(&query->recvbuf); - isc_buffer_init(&query->recvbuf, query->recvspace, COMMSIZE); - ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link); - result = isc_socket_recvv(query->sock, &query->recvlist, 1, - global_task, recv_done, query); - check_result(result, "isc_socket_recvv"); + isc_buffer_availableregion(&query->recvbuf, &r); + result = isc_socket_recv(query->sock, &r, 1, + global_task, recv_done, query); + check_result(result, "isc_socket_recv"); recvcount++; isc_event_free(&event); UNLOCK_LOOKUP; diff --git a/bin/dig/include/dig/dig.h b/bin/dig/include/dig/dig.h index 2982f5e186..e7c14a4b85 100644 --- a/bin/dig/include/dig/dig.h +++ b/bin/dig/include/dig/dig.h @@ -203,15 +203,12 @@ struct dig_query { bool ixfr_axfr; char *servname; char *userarg; - isc_bufferlist_t sendlist, - recvlist, - lengthlist; isc_buffer_t recvbuf, lengthbuf, - slbuf; - char *recvspace, - lengthspace[4], - slspace[4]; + tmpsendbuf, + sendbuf; + char *recvspace, *tmpsendspace, + lengthspace[4]; isc_socket_t *sock; ISC_LINK(dig_query_t) link; ISC_LINK(dig_query_t) clink; @@ -219,7 +216,6 @@ struct dig_query { isc_time_t time_sent; isc_time_t time_recv; uint64_t byte_count; - isc_buffer_t sendbuf; isc_timer_t *timer; }; diff --git a/bin/dig/nslookup.c b/bin/dig/nslookup.c index aa4000fdbb..77b940d2e6 100644 --- a/bin/dig/nslookup.c +++ b/bin/dig/nslookup.c @@ -923,12 +923,6 @@ flush_lookup_list(void) { ISC_SOCKCANCEL_ALL); isc_socket_detach(&q->sock); } - if (ISC_LINK_LINKED(&q->recvbuf, link)) - ISC_LIST_DEQUEUE(q->recvlist, &q->recvbuf, - link); - if (ISC_LINK_LINKED(&q->lengthbuf, link)) - ISC_LIST_DEQUEUE(q->lengthlist, &q->lengthbuf, - link); isc_buffer_invalidate(&q->recvbuf); isc_buffer_invalidate(&q->lengthbuf); qp = q;