2 Unix SMB/CIFS implementation.
4 Small async DNS library for Samba with socketwrapper support
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/network.h"
25 #include "lib/tsocket/tsocket.h"
26 #include "libcli/dns/libdns.h"
27 #include "lib/util/tevent_unix.h"
28 #include "lib/util/samba_util.h"
29 #include "lib/util/debug.h"
30 #include "libcli/util/error.h"
31 #include "librpc/ndr/libndr.h"
32 #include "librpc/gen_ndr/ndr_dns.h"
34 struct dns_udp_request_state
{
35 struct tevent_context
*ev
;
36 struct tdgram_context
*dgram
;
42 #define DNS_REQUEST_TIMEOUT 10
44 /* Declare callback functions used below. */
45 static void dns_udp_request_get_reply(struct tevent_req
*subreq
);
46 static void dns_udp_request_done(struct tevent_req
*subreq
);
48 static struct tevent_req
*dns_udp_request_send(TALLOC_CTX
*mem_ctx
,
49 struct tevent_context
*ev
,
50 const char *server_addr_string
,
54 struct tevent_req
*req
, *subreq
;
55 struct dns_udp_request_state
*state
;
56 struct tsocket_address
*local_addr
, *server_addr
;
57 struct tdgram_context
*dgram
;
60 req
= tevent_req_create(mem_ctx
, &state
, struct dns_udp_request_state
);
67 /* Use connected UDP sockets */
68 ret
= tsocket_address_inet_from_strings(state
, "ip", NULL
, 0,
71 tevent_req_error(req
, errno
);
72 return tevent_req_post(req
, ev
);
75 ret
= tsocket_address_inet_from_hostport_strings(
76 state
, "ip", server_addr_string
, DNS_SERVICE_PORT
, &server_addr
);
78 tevent_req_error(req
, errno
);
79 return tevent_req_post(req
, ev
);
82 ret
= tdgram_inet_udp_socket(local_addr
, server_addr
, state
, &dgram
);
84 tevent_req_error(req
, errno
);
85 return tevent_req_post(req
, ev
);
89 state
->query_len
= query_len
;
91 dump_data(10, query
, query_len
);
93 subreq
= tdgram_sendto_send(state
, ev
, dgram
, query
, query_len
, NULL
);
94 if (tevent_req_nomem(subreq
, req
)) {
95 return tevent_req_post(req
, ev
);
98 if (!tevent_req_set_endtime(req
, ev
,
99 timeval_current_ofs(DNS_REQUEST_TIMEOUT
, 0))) {
100 return tevent_req_post(req
, ev
);
103 tevent_req_set_callback(subreq
, dns_udp_request_get_reply
, req
);
107 static void dns_udp_request_get_reply(struct tevent_req
*subreq
)
109 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
111 struct dns_udp_request_state
*state
= tevent_req_data(req
,
112 struct dns_udp_request_state
);
116 len
= tdgram_sendto_recv(subreq
, &err
);
119 if (len
== -1 && err
!= 0) {
120 tevent_req_error(req
, err
);
124 if (len
!= state
->query_len
) {
125 tevent_req_error(req
, EIO
);
129 subreq
= tdgram_recvfrom_send(state
, state
->ev
, state
->dgram
);
130 if (tevent_req_nomem(subreq
, req
)) {
134 tevent_req_set_callback(subreq
, dns_udp_request_done
, req
);
137 static void dns_udp_request_done(struct tevent_req
*subreq
)
139 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
141 struct dns_udp_request_state
*state
= tevent_req_data(req
,
142 struct dns_udp_request_state
);
147 len
= tdgram_recvfrom_recv(subreq
, &err
, state
, &state
->reply
, NULL
);
150 if (len
== -1 && err
!= 0) {
151 tevent_req_error(req
, err
);
155 state
->reply_len
= len
;
156 dump_data(10, state
->reply
, state
->reply_len
);
157 tevent_req_done(req
);
160 static int dns_udp_request_recv(struct tevent_req
*req
,
165 struct dns_udp_request_state
*state
= tevent_req_data(req
,
166 struct dns_udp_request_state
);
169 if (tevent_req_is_unix_error(req
, &err
)) {
170 tevent_req_received(req
);
174 *reply
= talloc_move(mem_ctx
, &state
->reply
);
175 *reply_len
= state
->reply_len
;
176 tevent_req_received(req
);
181 struct dns_tcp_request_state
{
182 struct tevent_context
*ev
;
183 struct tstream_context
*stream
;
184 const uint8_t *query
;
187 uint8_t dns_msglen_hdr
[2];
194 static void dns_tcp_request_connected(struct tevent_req
*subreq
);
195 static void dns_tcp_request_sent(struct tevent_req
*subreq
);
196 static int dns_tcp_request_next_vector(struct tstream_context
*stream
,
199 struct iovec
**_vector
,
201 static void dns_tcp_request_received(struct tevent_req
*subreq
);
203 static struct tevent_req
*dns_tcp_request_send(TALLOC_CTX
*mem_ctx
,
204 struct tevent_context
*ev
,
205 const char *server_addr_string
,
206 const uint8_t *query
,
209 struct tevent_req
*req
, *subreq
;
210 struct dns_tcp_request_state
*state
;
211 struct tsocket_address
*local
, *remote
;
214 req
= tevent_req_create(mem_ctx
, &state
,
215 struct dns_tcp_request_state
);
220 state
->query
= query
;
221 state
->query_len
= query_len
;
223 if (query_len
> UINT16_MAX
) {
224 tevent_req_error(req
, EMSGSIZE
);
225 return tevent_req_post(req
, ev
);
228 ret
= tsocket_address_inet_from_strings(state
, "ip", NULL
, 0, &local
);
230 tevent_req_error(req
, errno
);
231 return tevent_req_post(req
, ev
);
234 ret
= tsocket_address_inet_from_hostport_strings(
235 state
, "ip", server_addr_string
, DNS_SERVICE_PORT
, &remote
);
237 tevent_req_error(req
, errno
);
238 return tevent_req_post(req
, ev
);
241 subreq
= tstream_inet_tcp_connect_send(state
, state
->ev
,
243 if (tevent_req_nomem(subreq
, req
)) {
244 return tevent_req_post(req
, ev
);
246 tevent_req_set_callback(subreq
, dns_tcp_request_connected
, req
);
251 static void dns_tcp_request_connected(struct tevent_req
*subreq
)
253 struct tevent_req
*req
= tevent_req_callback_data(
254 subreq
, struct tevent_req
);
255 struct dns_tcp_request_state
*state
= tevent_req_data(
256 req
, struct dns_tcp_request_state
);
259 ret
= tstream_inet_tcp_connect_recv(subreq
, &err
, state
,
260 &state
->stream
, NULL
);
263 tevent_req_error(req
, err
);
267 RSSVAL(state
->dns_msglen_hdr
, 0, state
->query_len
);
268 state
->iov
[0] = (struct iovec
) {
269 .iov_base
= state
->dns_msglen_hdr
,
270 .iov_len
= sizeof(state
->dns_msglen_hdr
)
272 state
->iov
[1] = (struct iovec
) {
273 .iov_base
= discard_const_p(void, state
->query
),
274 .iov_len
= state
->query_len
277 subreq
= tstream_writev_send(state
, state
->ev
, state
->stream
,
278 state
->iov
, ARRAY_SIZE(state
->iov
));
279 if (tevent_req_nomem(subreq
, req
)) {
282 tevent_req_set_callback(subreq
, dns_tcp_request_sent
, req
);
285 static void dns_tcp_request_sent(struct tevent_req
*subreq
)
287 struct tevent_req
*req
= tevent_req_callback_data(
288 subreq
, struct tevent_req
);
289 struct dns_tcp_request_state
*state
= tevent_req_data(
290 req
, struct dns_tcp_request_state
);
293 ret
= tstream_writev_recv(subreq
, &err
);
296 tevent_req_error(req
, err
);
300 subreq
= tstream_readv_pdu_send(state
, state
->ev
, state
->stream
,
301 dns_tcp_request_next_vector
, state
);
302 if (tevent_req_nomem(subreq
, req
)) {
305 tevent_req_set_callback(subreq
, dns_tcp_request_received
, req
);
308 static int dns_tcp_request_next_vector(struct tstream_context
*stream
,
311 struct iovec
**_vector
,
314 struct dns_tcp_request_state
*state
= talloc_get_type_abort(
315 private_data
, struct dns_tcp_request_state
);
316 struct iovec
*vector
;
319 if (state
->nread
== 0) {
320 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
321 if (vector
== NULL
) {
324 vector
[0] = (struct iovec
) {
325 .iov_base
= state
->dns_msglen_hdr
,
326 .iov_len
= sizeof(state
->dns_msglen_hdr
)
328 state
->nread
= sizeof(state
->dns_msglen_hdr
);
335 if (state
->nread
== sizeof(state
->dns_msglen_hdr
)) {
336 msglen
= RSVAL(state
->dns_msglen_hdr
, 0);
338 state
->reply
= talloc_array(state
, uint8_t, msglen
);
339 if (state
->reply
== NULL
) {
343 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
344 if (vector
== NULL
) {
347 vector
[0] = (struct iovec
) {
348 .iov_base
= state
->reply
,
351 state
->nread
+= msglen
;
363 static void dns_tcp_request_received(struct tevent_req
*subreq
)
365 struct tevent_req
*req
= tevent_req_callback_data(
366 subreq
, struct tevent_req
);
369 ret
= tstream_readv_pdu_recv(subreq
, &err
);
372 tevent_req_error(req
, err
);
376 tevent_req_done(req
);
379 static int dns_tcp_request_recv(struct tevent_req
*req
,
384 struct dns_tcp_request_state
*state
= tevent_req_data(
385 req
, struct dns_tcp_request_state
);
388 if (tevent_req_is_unix_error(req
, &err
)) {
389 tevent_req_received(req
);
393 *reply_len
= talloc_array_length(state
->reply
);
394 *reply
= talloc_move(mem_ctx
, &state
->reply
);
395 tevent_req_received(req
);
400 struct dns_cli_request_state
{
401 struct tevent_context
*ev
;
402 const char *nameserver
;
408 struct dns_name_packet
*reply
;
411 static void dns_cli_request_udp_done(struct tevent_req
*subreq
);
412 static void dns_cli_request_tcp_done(struct tevent_req
*subreq
);
414 struct tevent_req
*dns_cli_request_send(TALLOC_CTX
*mem_ctx
,
415 struct tevent_context
*ev
,
416 const char *nameserver
,
418 enum dns_qclass qclass
,
419 enum dns_qtype qtype
)
421 struct tevent_req
*req
, *subreq
;
422 struct dns_cli_request_state
*state
;
423 struct dns_name_question question
;
424 struct dns_name_packet out_packet
;
425 enum ndr_err_code ndr_err
;
427 req
= tevent_req_create(mem_ctx
, &state
,
428 struct dns_cli_request_state
);
433 state
->nameserver
= nameserver
;
435 DBG_DEBUG("Asking %s for %s/%d/%d via UDP\n", nameserver
,
436 name
, (int)qclass
, (int)qtype
);
438 generate_random_buffer((uint8_t *)&state
->req_id
,
439 sizeof(state
->req_id
));
441 question
= (struct dns_name_question
) {
442 .name
= discard_const_p(char, name
),
443 .question_type
= qtype
, .question_class
= qclass
446 out_packet
= (struct dns_name_packet
) {
448 .operation
= DNS_OPCODE_QUERY
| DNS_FLAG_RECURSION_DESIRED
,
450 .questions
= &question
453 ndr_err
= ndr_push_struct_blob(
454 &state
->query
, state
, &out_packet
,
455 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
456 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
457 tevent_req_error(req
, ndr_map_error2errno(ndr_err
));
458 return tevent_req_post(req
, ev
);
461 subreq
= dns_udp_request_send(state
, state
->ev
, state
->nameserver
,
462 state
->query
.data
, state
->query
.length
);
463 if (tevent_req_nomem(subreq
, req
)) {
464 return tevent_req_post(req
, ev
);
466 tevent_req_set_callback(subreq
, dns_cli_request_udp_done
, req
);
470 static void dns_cli_request_udp_done(struct tevent_req
*subreq
)
472 struct tevent_req
*req
= tevent_req_callback_data(
473 subreq
, struct tevent_req
);
474 struct dns_cli_request_state
*state
= tevent_req_data(
475 req
, struct dns_cli_request_state
);
477 enum ndr_err_code ndr_err
;
478 uint16_t reply_id
, operation
;
481 ret
= dns_udp_request_recv(subreq
, state
, &reply
.data
, &reply
.length
);
483 if (tevent_req_error(req
, ret
)) {
487 if (reply
.length
< 4) {
488 DBG_DEBUG("Short DNS packet: length=%zu\n", reply
.length
);
489 tevent_req_error(req
, EINVAL
);
493 reply_id
= PULL_BE_U16(reply
.data
, 0);
494 if (reply_id
!= state
->req_id
) {
495 DBG_DEBUG("Got id %"PRIu16
", expected %"PRIu16
"\n",
496 state
->reply
->id
, state
->req_id
);
497 tevent_req_error(req
, ENOMSG
);
501 operation
= PULL_BE_U16(reply
.data
, 2);
502 if ((operation
& DNS_FLAG_TRUNCATION
) != 0) {
503 DBG_DEBUG("Reply was truncated, retrying TCP\n");
504 subreq
= dns_tcp_request_send(
509 state
->query
.length
);
510 if (tevent_req_nomem(subreq
, req
)) {
513 tevent_req_set_callback(subreq
, dns_cli_request_tcp_done
, req
);
517 state
->reply
= talloc(state
, struct dns_name_packet
);
518 if (tevent_req_nomem(state
->reply
, req
)) {
522 ndr_err
= ndr_pull_struct_blob(
523 &reply
, state
->reply
, state
->reply
,
524 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
525 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
526 tevent_req_error(req
, ndr_map_error2errno(ndr_err
));
529 TALLOC_FREE(reply
.data
);
531 tevent_req_done(req
);
534 static void dns_cli_request_tcp_done(struct tevent_req
*subreq
)
536 struct tevent_req
*req
= tevent_req_callback_data(
537 subreq
, struct tevent_req
);
538 struct dns_cli_request_state
*state
= tevent_req_data(
539 req
, struct dns_cli_request_state
);
541 enum ndr_err_code ndr_err
;
544 ret
= dns_tcp_request_recv(subreq
, state
, &reply
.data
, &reply
.length
);
546 if (tevent_req_error(req
, ret
)) {
550 state
->reply
= talloc(state
, struct dns_name_packet
);
551 if (tevent_req_nomem(state
->reply
, req
)) {
555 ndr_err
= ndr_pull_struct_blob(
556 &reply
, state
->reply
, state
->reply
,
557 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
558 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
559 tevent_req_error(req
, ndr_map_error2errno(ndr_err
));
562 TALLOC_FREE(reply
.data
);
564 if (state
->reply
->id
!= state
->req_id
) {
565 DBG_DEBUG("Got id %"PRIu16
", expected %"PRIu16
"\n",
566 state
->reply
->id
, state
->req_id
);
567 tevent_req_error(req
, ENOMSG
);
571 DBG_DEBUG("Got op=%x %"PRIu16
"/%"PRIu16
"/%"PRIu16
"/%"PRIu16
572 " recs\n", (int)state
->reply
->operation
,
573 state
->reply
->qdcount
, state
->reply
->ancount
,
574 state
->reply
->nscount
, state
->reply
->nscount
);
576 tevent_req_done(req
);
579 int dns_cli_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
580 struct dns_name_packet
**reply
)
582 struct dns_cli_request_state
*state
= tevent_req_data(
583 req
, struct dns_cli_request_state
);
586 if (tevent_req_is_unix_error(req
, &err
)) {
589 *reply
= talloc_move(mem_ctx
, &state
->reply
);