1 /* DNS test framework and libresolv redirection.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <support/resolv_test.h>
21 #include <arpa/inet.h>
29 #include <support/check.h>
30 #include <support/namespace.h>
31 #include <support/support.h>
32 #include <support/test-driver.h>
33 #include <support/xsocket.h>
34 #include <support/xthread.h>
35 #include <support/xunistd.h>
39 /* Response builder. */
43 max_response_length
= 65536
46 /* List of pointers to be freed. The hash table implementation
47 (struct hsearch_data) does not provide a way to deallocate all
48 objects, so this approach is used to avoid memory leaks. */
51 struct to_be_freed
*next
;
55 struct resolv_response_builder
57 const unsigned char *query_buffer
;
60 size_t offset
; /* Bytes written so far in buffer. */
61 ns_sect section
; /* Current section in the DNS packet. */
62 unsigned int truncate_bytes
; /* Bytes to remove at end of response. */
63 bool drop
; /* Discard generated response. */
64 bool close
; /* Close TCP client connection. */
66 /* Offset of the two-byte RDATA length field in the currently
67 written RDATA sub-structure. 0 if no RDATA is being written. */
68 size_t current_rdata_offset
;
70 /* Hash table for locating targets for label compression. */
71 struct hsearch_data compression_offsets
;
72 /* List of pointers which need to be freed. Used for domain names
73 involved in label compression. */
74 struct to_be_freed
*to_be_freed
;
76 /* Must be last. Not zeroed for performance reasons. */
77 unsigned char buffer
[max_response_length
];
80 /* Response builder. */
82 /* Add a pointer to the list of pointers to be freed when B is
85 response_push_pointer_to_free (struct resolv_response_builder
*b
, void *ptr
)
89 struct to_be_freed
*e
= xmalloc (sizeof (*e
));
90 *e
= (struct to_be_freed
) {b
->to_be_freed
, ptr
};
95 resolv_response_init (struct resolv_response_builder
*b
,
96 struct resolv_response_flags flags
)
99 FAIL_EXIT1 ("response_init: called at offset %zu", b
->offset
);
100 if (b
->query_length
< 12)
101 FAIL_EXIT1 ("response_init called for a query of size %zu",
103 if (flags
.rcode
> 15)
104 FAIL_EXIT1 ("response_init: invalid RCODE %u", flags
.rcode
);
106 /* Copy the transaction ID. */
107 b
->buffer
[0] = b
->query_buffer
[0];
108 b
->buffer
[1] = b
->query_buffer
[1];
110 /* Initialize the flags. */
111 b
->buffer
[2] = 0x80; /* Mark as response. */
112 b
->buffer
[2] |= b
->query_buffer
[2] & 0x01; /* Copy the RD bit. */
114 b
->buffer
[2] |= 0x02;
115 b
->buffer
[3] = 0x80 | flags
.rcode
; /* Always set RA. */
117 /* Fill in the initial section count values. */
118 b
->buffer
[4] = flags
.qdcount
>> 8;
119 b
->buffer
[5] = flags
.qdcount
;
120 b
->buffer
[6] = flags
.ancount
>> 8;
121 b
->buffer
[7] = flags
.ancount
;
122 b
->buffer
[8] = flags
.nscount
>> 8;
123 b
->buffer
[9] = flags
.nscount
;
124 b
->buffer
[10] = flags
.adcount
>> 8;
125 b
->buffer
[11] = flags
.adcount
;
131 resolv_response_section (struct resolv_response_builder
*b
, ns_sect section
)
134 FAIL_EXIT1 ("resolv_response_section: response_init not called before");
135 if (section
< b
->section
)
136 FAIL_EXIT1 ("resolv_response_section: cannot go back to previous section");
137 b
->section
= section
;
140 /* Add a single byte to B. */
142 response_add_byte (struct resolv_response_builder
*b
, unsigned char ch
)
144 if (b
->offset
== max_response_length
)
145 FAIL_EXIT1 ("DNS response exceeds 64 KiB limit");
146 b
->buffer
[b
->offset
] = ch
;
150 /* Add a 16-bit word VAL to B, in big-endian format. */
152 response_add_16 (struct resolv_response_builder
*b
, uint16_t val
)
154 response_add_byte (b
, val
>> 8);
155 response_add_byte (b
, val
);
158 /* Increment the pers-section record counter in the packet header. */
160 response_count_increment (struct resolv_response_builder
*b
)
162 unsigned int offset
= b
->section
;
163 offset
= 4 + 2 * offset
;
164 ++b
->buffer
[offset
+ 1];
165 if (b
->buffer
[offset
+ 1] == 0)
169 if (b
->buffer
[offset
] == 0)
171 FAIL_EXIT1 ("too many records in section");
176 resolv_response_add_question (struct resolv_response_builder
*b
,
177 const char *name
, uint16_t class, uint16_t type
)
180 FAIL_EXIT1 ("resolv_response_add_question: "
181 "resolv_response_init not called");
182 if (b
->section
!= ns_s_qd
)
183 FAIL_EXIT1 ("resolv_response_add_question: "
184 "must be called in the question section");
186 resolv_response_add_name (b
, name
);
187 response_add_16 (b
, type
);
188 response_add_16 (b
, class);
190 response_count_increment (b
);
194 resolv_response_add_name (struct resolv_response_builder
*b
,
195 const char *const origname
)
197 /* Normalized name. */
199 /* Normalized name with case preserved. */
202 size_t namelen
= strlen (origname
);
203 /* Remove trailing dots. FIXME: Handle trailing quoted dots. */
204 while (namelen
> 0 && origname
[namelen
- 1] == '.')
206 name
= xmalloc (namelen
+ 1);
207 name_case
= xmalloc (namelen
+ 1);
208 /* Copy and convert to lowercase. FIXME: This needs to normalize
210 for (size_t i
= 0; i
< namelen
; ++i
)
212 char ch
= origname
[i
];
214 if ('A' <= ch
&& ch
<= 'Z')
219 name_case
[namelen
] = 0;
221 char *name_start
= name
;
222 char *name_case_start
= name_case
;
224 bool compression
= false;
227 /* Search for a previous name we can reference. */
231 .data
= (void *) (uintptr_t) b
->offset
,
234 /* If the label can be a compression target because it is at a
235 reachable offset, add it to the hash table. */
237 if (b
->offset
< (1 << 12))
242 /* Search for known compression offsets in the hash table. */
244 if (hsearch_r (new_entry
, action
, &e
, &b
->compression_offsets
) == 0)
246 if (action
== FIND
&& errno
== ESRCH
)
250 FAIL_EXIT1 ("hsearch_r failure in name compression: %m");
253 /* The name is known. Reference the previous location. */
254 if (e
!= NULL
&& e
->data
!= new_entry
.data
)
256 size_t old_offset
= (uintptr_t) e
->data
;
257 response_add_byte (b
, 0xC0 | (old_offset
>> 8));
258 response_add_byte (b
, old_offset
);
263 /* The name does not exist yet. Write one label. First, add
264 room for the label length. */
265 size_t buffer_label_offset
= b
->offset
;
266 response_add_byte (b
, 0);
268 /* Copy the label. */
271 char ch
= *name_case
;
278 /* FIXME: Handle escaping. */
279 response_add_byte (b
, ch
);
282 /* Patch in the label length. */
283 size_t label_length
= b
->offset
- buffer_label_offset
- 1;
284 if (label_length
== 0)
285 FAIL_EXIT1 ("empty label in name compression: %s", origname
);
286 if (label_length
> 63)
287 FAIL_EXIT1 ("label too long in name compression: %s", origname
);
288 b
->buffer
[buffer_label_offset
] = label_length
;
290 /* Continue with the tail of the name and the next label. */
295 /* If we found an immediate match for the name, we have not put
296 it into the hash table, and can free it immediately. */
297 if (name
== name_start
)
300 response_push_pointer_to_free (b
, name_start
);
304 /* Terminate the sequence of labels. With compression, this is
305 implicit in the compression reference. */
306 response_add_byte (b
, 0);
307 response_push_pointer_to_free (b
, name_start
);
310 free (name_case_start
);
314 resolv_response_open_record (struct resolv_response_builder
*b
,
316 uint16_t class, uint16_t type
, uint32_t ttl
)
318 if (b
->section
== ns_s_qd
)
319 FAIL_EXIT1 ("resolv_response_open_record called in question section");
320 if (b
->current_rdata_offset
!= 0)
321 FAIL_EXIT1 ("resolv_response_open_record called with open record");
323 resolv_response_add_name (b
, name
);
324 response_add_16 (b
, type
);
325 response_add_16 (b
, class);
326 response_add_16 (b
, ttl
>> 16);
327 response_add_16 (b
, ttl
);
329 b
->current_rdata_offset
= b
->offset
;
330 /* Add room for the RDATA length. */
331 response_add_16 (b
, 0);
336 resolv_response_close_record (struct resolv_response_builder
*b
)
338 size_t rdata_offset
= b
->current_rdata_offset
;
339 if (rdata_offset
== 0)
340 FAIL_EXIT1 ("response_close_record called without open record");
341 size_t rdata_length
= b
->offset
- rdata_offset
- 2;
342 if (rdata_length
> 65535)
343 FAIL_EXIT1 ("RDATA length %zu exceeds limit", rdata_length
);
344 b
->buffer
[rdata_offset
] = rdata_length
>> 8;
345 b
->buffer
[rdata_offset
+ 1] = rdata_length
;
346 response_count_increment (b
);
347 b
->current_rdata_offset
= 0;
351 resolv_response_add_data (struct resolv_response_builder
*b
,
352 const void *data
, size_t length
)
354 size_t remaining
= max_response_length
- b
->offset
;
355 if (remaining
< length
)
356 FAIL_EXIT1 ("resolv_response_add_data: not enough room for %zu bytes",
358 memcpy (b
->buffer
+ b
->offset
, data
, length
);
363 resolv_response_drop (struct resolv_response_builder
*b
)
369 resolv_response_close (struct resolv_response_builder
*b
)
375 resolv_response_truncate_data (struct resolv_response_builder
*b
, size_t count
)
378 FAIL_EXIT1 ("resolv_response_truncate_data: argument too large: %zu",
380 b
->truncate_bytes
= count
;
385 resolv_response_length (const struct resolv_response_builder
*b
)
391 resolv_response_buffer (const struct resolv_response_builder
*b
)
393 unsigned char *result
= xmalloc (b
->offset
);
394 memcpy (result
, b
->buffer
, b
->offset
);
398 static struct resolv_response_builder
*
399 response_builder_allocate
400 (const unsigned char *query_buffer
, size_t query_length
)
402 struct resolv_response_builder
*b
= xmalloc (sizeof (*b
));
403 memset (b
, 0, offsetof (struct resolv_response_builder
, buffer
));
404 b
->query_buffer
= query_buffer
;
405 b
->query_length
= query_length
;
406 TEST_VERIFY_EXIT (hcreate_r (10000, &b
->compression_offsets
) != 0);
411 response_builder_free (struct resolv_response_builder
*b
)
413 struct to_be_freed
*current
= b
->to_be_freed
;
414 while (current
!= NULL
)
416 struct to_be_freed
*next
= current
->next
;
421 hdestroy_r (&b
->compression_offsets
);
425 /* DNS query processing. */
427 /* Data extracted from the question section of a DNS packet. */
430 char qname
[MAXDNAME
];
433 struct resolv_edns_info edns
;
436 /* Update *INFO from the specified DNS packet. */
438 parse_query (struct query_info
*info
,
439 const unsigned char *buffer
, size_t length
)
442 _Static_assert (sizeof (hd
) == 12, "DNS header size");
443 if (length
< sizeof (hd
))
444 FAIL_EXIT1 ("malformed DNS query: too short: %zu bytes", length
);
445 memcpy (&hd
, buffer
, sizeof (hd
));
447 if (ntohs (hd
.qdcount
) != 1)
448 FAIL_EXIT1 ("malformed DNS query: wrong question count: %d",
449 (int) ntohs (hd
.qdcount
));
450 if (ntohs (hd
.ancount
) != 0)
451 FAIL_EXIT1 ("malformed DNS query: wrong answer count: %d",
452 (int) ntohs (hd
.ancount
));
453 if (ntohs (hd
.nscount
) != 0)
454 FAIL_EXIT1 ("malformed DNS query: wrong authority count: %d",
455 (int) ntohs (hd
.nscount
));
456 if (ntohs (hd
.arcount
) > 1)
457 FAIL_EXIT1 ("malformed DNS query: wrong additional count: %d",
458 (int) ntohs (hd
.arcount
));
460 int ret
= dn_expand (buffer
, buffer
+ length
, buffer
+ sizeof (hd
),
461 info
->qname
, sizeof (info
->qname
));
463 FAIL_EXIT1 ("malformed DNS query: cannot uncompress QNAME");
465 /* Obtain QTYPE and QCLASS. */
466 size_t remaining
= length
- (12 + ret
);
472 if (remaining
< sizeof (qtype_qclass
))
473 FAIL_EXIT1 ("malformed DNS query: "
474 "query lacks QCLASS/QTYPE, QNAME: %s", info
->qname
);
475 memcpy (&qtype_qclass
, buffer
+ 12 + ret
, sizeof (qtype_qclass
));
476 info
->qclass
= ntohs (qtype_qclass
.qclass
);
477 info
->qtype
= ntohs (qtype_qclass
.qtype
);
479 memset (&info
->edns
, 0, sizeof (info
->edns
));
480 if (ntohs (hd
.arcount
) > 0)
482 /* Parse EDNS record. */
483 struct __attribute__ ((packed
, aligned (1)))
488 uint8_t edns_extended_rcode
;
489 uint8_t edns_version
;
493 _Static_assert (sizeof (rr
) == 11, "EDNS record size");
495 if (remaining
< 4 + sizeof (rr
))
496 FAIL_EXIT1 ("mailformed DNS query: no room for EDNS record");
497 memcpy (&rr
, buffer
+ 12 + ret
+ 4, sizeof (rr
));
499 FAIL_EXIT1 ("malformed DNS query: invalid OPT RNAME: %d\n", rr
.root
);
500 if (rr
.rtype
!= htons (41))
501 FAIL_EXIT1 ("malformed DNS query: invalid OPT type: %d\n",
503 info
->edns
.active
= true;
504 info
->edns
.extended_rcode
= rr
.edns_extended_rcode
;
505 info
->edns
.version
= rr
.edns_version
;
506 info
->edns
.flags
= ntohs (rr
.flags
);
507 info
->edns
.payload_size
= ntohs (rr
.payload
);
512 /* Main testing framework. */
514 /* Per-server information. One struct is allocated for each test
516 struct resolv_test_server
518 /* Local address of the server. UDP and TCP use the same port. */
519 struct sockaddr_in address
;
521 /* File descriptor of the UDP server, or -1 if this server is
525 /* File descriptor of the TCP server, or -1 if this server is
529 /* Counter of the number of responses processed so far. */
530 size_t response_number
;
532 /* Thread handles for the server threads (if not disabled in the
534 pthread_t thread_udp
;
535 pthread_t thread_tcp
;
538 /* Main struct for keeping track of libresolv redirection and
542 /* After initialization, any access to the struct must be performed
543 while this lock is acquired. */
544 pthread_mutex_t lock
;
546 /* Data for each test server. */
547 struct resolv_test_server servers
[resolv_max_test_servers
];
549 /* Used if config.single_thread_udp is true. */
550 pthread_t thread_udp_single
;
552 struct resolv_redirect_config config
;
553 bool termination_requested
;
556 /* Function implementing a server thread. */
557 typedef void (*thread_callback
) (struct resolv_test
*, int server_index
);
559 /* Storage for thread-specific data, for passing to the
560 thread_callback function. */
561 struct thread_closure
563 struct resolv_test
*obj
; /* Current test object. */
564 thread_callback callback
; /* Function to call. */
565 int server_index
; /* Index of the implemented server. */
568 /* Wrap response_callback as a function which can be passed to
571 thread_callback_wrapper (void *arg
)
573 struct thread_closure
*closure
= arg
;
574 closure
->callback (closure
->obj
, closure
->server_index
);
579 /* Start a server thread for the specified SERVER_INDEX, implemented
582 start_server_thread (struct resolv_test
*obj
, int server_index
,
583 thread_callback callback
)
585 struct thread_closure
*closure
= xmalloc (sizeof (*closure
));
586 *closure
= (struct thread_closure
)
589 .callback
= callback
,
590 .server_index
= server_index
,
592 return xpthread_create (NULL
, thread_callback_wrapper
, closure
);
595 /* Process one UDP query. Return false if a termination requested has
598 server_thread_udp_process_one (struct resolv_test
*obj
, int server_index
)
600 unsigned char query
[512];
601 struct sockaddr_storage peer
;
602 socklen_t peerlen
= sizeof (peer
);
603 size_t length
= xrecvfrom (obj
->servers
[server_index
].socket_udp
,
604 query
, sizeof (query
), 0,
605 (struct sockaddr
*) &peer
, &peerlen
);
606 /* Check for termination. */
608 bool termination_requested
;
609 xpthread_mutex_lock (&obj
->lock
);
610 termination_requested
= obj
->termination_requested
;
611 xpthread_mutex_unlock (&obj
->lock
);
612 if (termination_requested
)
617 struct query_info qinfo
;
618 parse_query (&qinfo
, query
, length
);
619 if (test_verbose
> 0)
621 if (test_verbose
> 1)
622 printf ("info: UDP server %d: incoming query:"
623 " %zd bytes, %s/%u/%u, tnxid=0x%02x%02x\n",
624 server_index
, length
, qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
,
627 printf ("info: UDP server %d: incoming query:"
628 " %zd bytes, %s/%u/%u\n",
629 server_index
, length
, qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
632 struct resolv_response_context ctx
=
634 .query_buffer
= query
,
635 .query_length
= length
,
636 .server_index
= server_index
,
640 struct resolv_response_builder
*b
= response_builder_allocate (query
, length
);
641 obj
->config
.response_callback
642 (&ctx
, b
, qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
647 printf ("info: UDP server %d: dropping response to %s/%u/%u\n",
648 server_index
, qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
655 printf ("info: UDP server %d: sending response:"
656 " %zu bytes, RCODE %d (for %s/%u/%u)\n",
657 server_index
, b
->offset
, b
->buffer
[3] & 0x0f,
658 qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
660 printf ("info: UDP server %d: sending response: %zu bytes"
662 server_index
, b
->offset
,
663 qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
664 if (b
->truncate_bytes
> 0)
665 printf ("info: truncated by %u bytes\n", b
->truncate_bytes
);
667 size_t to_send
= b
->offset
;
668 if (to_send
< b
->truncate_bytes
)
671 to_send
-= b
->truncate_bytes
;
673 /* Ignore most errors here because the other end may have closed
675 if (sendto (obj
->servers
[server_index
].socket_udp
,
676 b
->buffer
, to_send
, 0,
677 (struct sockaddr
*) &peer
, peerlen
) < 0)
678 TEST_VERIFY_EXIT (errno
!= EBADF
);
680 response_builder_free (b
);
684 /* UDP thread_callback function. Variant for one thread per
687 server_thread_udp (struct resolv_test
*obj
, int server_index
)
689 while (server_thread_udp_process_one (obj
, server_index
))
693 /* Single-threaded UDP processing function, for the single_thread_udp
696 server_thread_udp_single (void *closure
)
698 struct resolv_test
*obj
= closure
;
700 struct pollfd fds
[resolv_max_test_servers
];
701 for (int server_index
= 0; server_index
< resolv_max_test_servers
;
703 if (obj
->config
.servers
[server_index
].disable_udp
)
704 fds
[server_index
] = (struct pollfd
) {.fd
= -1};
707 fds
[server_index
] = (struct pollfd
)
709 .fd
= obj
->servers
[server_index
].socket_udp
,
713 /* Make the socket non-blocking. */
714 int flags
= fcntl (obj
->servers
[server_index
].socket_udp
, F_GETFL
, 0);
716 FAIL_EXIT1 ("fcntl (F_GETFL): %m");
718 if (fcntl (obj
->servers
[server_index
].socket_udp
, F_SETFL
, flags
) < 0)
719 FAIL_EXIT1 ("fcntl (F_SETFL): %m");
724 xpoll (fds
, resolv_max_test_servers
, -1);
725 for (int server_index
= 0; server_index
< resolv_max_test_servers
;
727 if (fds
[server_index
].revents
!= 0)
729 if (!server_thread_udp_process_one (obj
, server_index
))
731 fds
[server_index
].revents
= 0;
739 /* Start the single UDP handler thread (for the single_thread_udp
742 start_server_thread_udp_single (struct resolv_test
*obj
)
744 obj
->thread_udp_single
745 = xpthread_create (NULL
, server_thread_udp_single
, obj
);
748 /* Data describing a TCP client connect. */
749 struct tcp_thread_closure
751 struct resolv_test
*obj
;
756 /* Read a complete DNS query packet. If EOF_OK, an immediate
757 end-of-file condition is acceptable. */
759 read_fully (int fd
, void *buf
, size_t len
, bool eof_ok
)
761 const void *const end
= buf
+ len
;
764 ssize_t ret
= read (fd
, buf
, end
- buf
);
769 support_record_failure ();
770 printf ("error: unexpected EOF on TCP connection\n");
776 if (!eof_ok
|| errno
!= ECONNRESET
)
778 support_record_failure ();
779 printf ("error: TCP read: %m\n");
789 /* Write an array of iovecs. Terminate the process on failure. */
791 writev_fully (int fd
, struct iovec
*buffers
, size_t count
)
795 /* Skip zero-length write requests. */
796 if (buffers
->iov_len
== 0)
802 /* Try to rewrite the remaing buffers. */
803 ssize_t ret
= writev (fd
, buffers
, count
);
805 FAIL_EXIT1 ("writev: %m");
807 FAIL_EXIT1 ("writev: invalid return value zero");
808 /* Find the buffers that were successfully written. */
812 FAIL_EXIT1 ("internal writev consistency failure");
813 /* Current buffer was partially written. */
814 if (buffers
->iov_len
> (size_t) ret
)
816 buffers
->iov_base
+= ret
;
817 buffers
->iov_len
-= ret
;
822 ret
-= buffers
->iov_len
;
823 buffers
->iov_len
= 0;
831 /* Thread callback for handling a single established TCP connection to
834 server_thread_tcp_client (void *arg
)
836 struct tcp_thread_closure
*closure
= arg
;
840 /* Read packet length. */
841 uint16_t query_length
;
842 if (!read_fully (closure
->client_socket
,
843 &query_length
, sizeof (query_length
), true))
845 query_length
= ntohs (query_length
);
847 /* Read the packet. */
848 unsigned char *query_buffer
= xmalloc (query_length
);
849 read_fully (closure
->client_socket
, query_buffer
, query_length
, false);
851 struct query_info qinfo
;
852 parse_query (&qinfo
, query_buffer
, query_length
);
853 if (test_verbose
> 0)
855 if (test_verbose
> 1)
856 printf ("info: UDP server %d: incoming query:"
857 " %d bytes, %s/%u/%u, tnxid=0x%02x%02x\n",
858 closure
->server_index
, query_length
,
859 qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
,
860 query_buffer
[0], query_buffer
[1]);
862 printf ("info: TCP server %d: incoming query:"
863 " %u bytes, %s/%u/%u\n",
864 closure
->server_index
, query_length
,
865 qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
868 struct resolv_response_context ctx
=
870 .query_buffer
= query_buffer
,
871 .query_length
= query_length
,
872 .server_index
= closure
->server_index
,
876 struct resolv_response_builder
*b
= response_builder_allocate
877 (query_buffer
, query_length
);
878 closure
->obj
->config
.response_callback
879 (&ctx
, b
, qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
884 printf ("info: TCP server %d: dropping response to %s/%u/%u\n",
885 closure
->server_index
,
886 qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
891 printf ("info: TCP server %d: sending response: %zu bytes"
893 closure
->server_index
, b
->offset
,
894 qinfo
.qname
, qinfo
.qclass
, qinfo
.qtype
);
895 uint16_t length
= htons (b
->offset
);
896 size_t to_send
= b
->offset
;
897 if (to_send
< b
->truncate_bytes
)
900 to_send
-= b
->truncate_bytes
;
901 struct iovec buffers
[2] =
903 {&length
, sizeof (length
)},
906 writev_fully (closure
->client_socket
, buffers
, 2);
908 bool close_flag
= b
->close
;
909 response_builder_free (b
);
915 xclose (closure
->client_socket
);
920 /* thread_callback for the TCP case. Accept connections and create a
921 new thread for each client. */
923 server_thread_tcp (struct resolv_test
*obj
, int server_index
)
927 /* Get the client conenction. */
928 int client_socket
= xaccept
929 (obj
->servers
[server_index
].socket_tcp
, NULL
, NULL
);
931 /* Check for termination. */
932 xpthread_mutex_lock (&obj
->lock
);
933 if (obj
->termination_requested
)
935 xpthread_mutex_unlock (&obj
->lock
);
936 xclose (client_socket
);
939 xpthread_mutex_unlock (&obj
->lock
);
941 /* Spawn a new thread for handling this connection. */
942 struct tcp_thread_closure
*closure
= xmalloc (sizeof (*closure
));
943 *closure
= (struct tcp_thread_closure
)
946 .server_index
= server_index
,
947 .client_socket
= client_socket
,
951 = xpthread_create (NULL
, server_thread_tcp_client
, closure
);
952 /* TODO: We should keep track of this thread so that we can
953 block in resolv_test_end until it has exited. */
954 xpthread_detach (thr
);
958 /* Create UDP and TCP server sockets. */
960 make_server_sockets (struct resolv_test_server
*server
)
964 server
->socket_udp
= xsocket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
965 server
->socket_tcp
= xsocket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
967 /* Pick the address for the UDP socket. */
968 server
->address
= (struct sockaddr_in
)
970 .sin_family
= AF_INET
,
971 .sin_addr
= {.s_addr
= htonl (INADDR_LOOPBACK
)}
973 xbind (server
->socket_udp
,
974 (struct sockaddr
*)&server
->address
, sizeof (server
->address
));
976 /* Retrieve the address. */
977 socklen_t addrlen
= sizeof (server
->address
);
978 xgetsockname (server
->socket_udp
,
979 (struct sockaddr
*)&server
->address
, &addrlen
);
981 /* Bind the TCP socket to the same address. */
984 xsetsockopt (server
->socket_tcp
, SOL_SOCKET
, SO_REUSEADDR
,
987 if (bind (server
->socket_tcp
,
988 (struct sockaddr
*)&server
->address
,
989 sizeof (server
->address
)) != 0)
991 /* Port collision. The UDP bind succeeded, but the TCP BIND
992 failed. We assume here that the kernel will pick the
993 next local UDP address randomly. */
994 if (errno
== EADDRINUSE
)
996 xclose (server
->socket_udp
);
997 xclose (server
->socket_tcp
);
1000 FAIL_EXIT1 ("TCP bind: %m");
1002 xlisten (server
->socket_tcp
, 5);
1007 /* One-time initialization of NSS. */
1009 resolv_redirect_once (void)
1011 /* Only use nss_dns. */
1012 __nss_configure_lookup ("hosts", "dns");
1013 __nss_configure_lookup ("networks", "dns");
1014 /* Enter a network namespace for isolation and firewall state
1015 cleanup. The tests will still work if these steps fail, but they
1016 may be less reliable. */
1017 support_become_root ();
1018 support_enter_network_namespace ();
1020 pthread_once_t resolv_redirect_once_var
= PTHREAD_ONCE_INIT
;
1023 resolv_test_init (void)
1025 /* Perform one-time initialization of NSS. */
1026 xpthread_once (&resolv_redirect_once_var
, resolv_redirect_once
);
1029 /* Copy the search path from CONFIG.search to the _res object. */
1031 set_search_path (struct resolv_redirect_config config
)
1033 memset (_res
.defdname
, 0, sizeof (_res
.defdname
));
1034 memset (_res
.dnsrch
, 0, sizeof (_res
.dnsrch
));
1036 char *current
= _res
.defdname
;
1037 char *end
= current
+ sizeof (_res
.defdname
);
1039 for (unsigned int i
= 0;
1040 i
< sizeof (config
.search
) / sizeof (config
.search
[0]); ++i
)
1042 if (config
.search
[i
] == NULL
)
1045 size_t length
= strlen (config
.search
[i
]) + 1;
1046 size_t remaining
= end
- current
;
1047 TEST_VERIFY_EXIT (length
<= remaining
);
1048 memcpy (current
, config
.search
[i
], length
);
1049 _res
.dnsrch
[i
] = current
;
1054 struct resolv_test
*
1055 resolv_test_start (struct resolv_redirect_config config
)
1057 /* Apply configuration defaults. */
1058 if (config
.nscount
== 0)
1059 config
.nscount
= resolv_max_test_servers
;
1061 struct resolv_test
*obj
= xmalloc (sizeof (*obj
));
1062 *obj
= (struct resolv_test
) {
1064 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1067 resolv_test_init ();
1069 /* Create all the servers, to reserve the necessary ports. */
1070 for (int server_index
= 0; server_index
< config
.nscount
; ++server_index
)
1071 make_server_sockets (obj
->servers
+ server_index
);
1073 /* Start server threads. Disable the server ports, as
1075 for (int server_index
= 0; server_index
< config
.nscount
; ++server_index
)
1077 struct resolv_test_server
*server
= obj
->servers
+ server_index
;
1078 if (config
.servers
[server_index
].disable_udp
)
1080 xclose (server
->socket_udp
);
1081 server
->socket_udp
= -1;
1083 else if (!config
.single_thread_udp
)
1084 server
->thread_udp
= start_server_thread (obj
, server_index
,
1086 if (config
.servers
[server_index
].disable_tcp
)
1088 xclose (server
->socket_tcp
);
1089 server
->socket_tcp
= -1;
1092 server
->thread_tcp
= start_server_thread (obj
, server_index
,
1095 if (config
.single_thread_udp
)
1096 start_server_thread_udp_single (obj
);
1100 /* Initialize libresolv. */
1101 TEST_VERIFY_EXIT (res_init () == 0);
1103 /* Disable IPv6 name server addresses. The code below only
1104 overrides the IPv4 addresses. */
1105 __res_iclose (&_res
, true);
1106 _res
._u
._ext
.nscount
= 0;
1108 /* Redirect queries to the server socket. */
1111 printf ("info: old timeout value: %d\n", _res
.retrans
);
1112 printf ("info: old retry attempt value: %d\n", _res
.retry
);
1113 printf ("info: old _res.options: 0x%lx\n", _res
.options
);
1114 printf ("info: old _res.nscount value: %d\n", _res
.nscount
);
1115 printf ("info: old _res.ndots value: %d\n", _res
.ndots
);
1117 _res
.retrans
= timeout
;
1119 _res
.nscount
= config
.nscount
;
1120 _res
.options
= RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
;
1124 printf ("info: new timeout value: %d\n", _res
.retrans
);
1125 printf ("info: new retry attempt value: %d\n", _res
.retry
);
1126 printf ("info: new _res.options: 0x%lx\n", _res
.options
);
1127 printf ("info: new _res.nscount value: %d\n", _res
.nscount
);
1128 printf ("info: new _res.ndots value: %d\n", _res
.ndots
);
1130 for (int server_index
= 0; server_index
< config
.nscount
; ++server_index
)
1132 _res
.nsaddr_list
[server_index
] = obj
->servers
[server_index
].address
;
1137 (inet_ntop (AF_INET
, &obj
->servers
[server_index
].address
.sin_addr
,
1138 buf
, sizeof (buf
)) != NULL
);
1139 printf ("info: server %d: %s/%u\n",
1141 htons (obj
->servers
[server_index
].address
.sin_port
));
1145 set_search_path (config
);
1151 resolv_test_end (struct resolv_test
*obj
)
1155 xpthread_mutex_lock (&obj
->lock
);
1156 obj
->termination_requested
= true;
1157 xpthread_mutex_unlock (&obj
->lock
);
1159 /* Send trigger packets to unblock the server threads. */
1160 for (int server_index
= 0; server_index
< obj
->config
.nscount
;
1163 if (!obj
->config
.servers
[server_index
].disable_udp
)
1165 int sock
= xsocket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
1166 xsendto (sock
, "", 1, 0,
1167 (struct sockaddr
*) &obj
->servers
[server_index
].address
,
1168 sizeof (obj
->servers
[server_index
].address
));
1171 if (!obj
->config
.servers
[server_index
].disable_tcp
)
1173 int sock
= xsocket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
1175 (struct sockaddr
*) &obj
->servers
[server_index
].address
,
1176 sizeof (obj
->servers
[server_index
].address
));
1181 if (obj
->config
.single_thread_udp
)
1182 xpthread_join (obj
->thread_udp_single
);
1184 /* Wait for the server threads to terminate. */
1185 for (int server_index
= 0; server_index
< obj
->config
.nscount
;
1188 if (!obj
->config
.servers
[server_index
].disable_udp
)
1190 if (!obj
->config
.single_thread_udp
)
1191 xpthread_join (obj
->servers
[server_index
].thread_udp
);
1192 xclose (obj
->servers
[server_index
].socket_udp
);
1194 if (!obj
->config
.servers
[server_index
].disable_tcp
)
1196 xpthread_join (obj
->servers
[server_index
].thread_tcp
);
1197 xclose (obj
->servers
[server_index
].socket_tcp
);