Lines 1-3
Link Here
|
|
|
1 |
/* Copyright (C) 2016 Free Software Foundation, Inc. |
2 |
This file is part of the GNU C Library. |
3 |
|
4 |
The GNU C Library is free software; you can redistribute it and/or |
5 |
modify it under the terms of the GNU Lesser General Public |
6 |
License as published by the Free Software Foundation; either |
7 |
version 2.1 of the License, or (at your option) any later version. |
8 |
|
9 |
The GNU C Library is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
Lesser General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU Lesser General Public |
15 |
License along with the GNU C Library; if not, see |
16 |
<http://www.gnu.org/licenses/>. */ |
17 |
|
1 |
/* |
18 |
/* |
2 |
* Copyright (c) 1985, 1989, 1993 |
19 |
* Copyright (c) 1985, 1989, 1993 |
3 |
* The Regents of the University of California. All rights reserved. |
20 |
* The Regents of the University of California. All rights reserved. |
Lines 355-360
__libc_res_nsend(res_state statp, const u_char *buf, int buflen,
Link Here
|
355 |
#ifdef USE_HOOKS |
372 |
#ifdef USE_HOOKS |
356 |
if (__glibc_unlikely (statp->qhook || statp->rhook)) { |
373 |
if (__glibc_unlikely (statp->qhook || statp->rhook)) { |
357 |
if (anssiz < MAXPACKET && ansp) { |
374 |
if (anssiz < MAXPACKET && ansp) { |
|
|
375 |
/* Always allocate MAXPACKET, callers expect |
376 |
this specific size. */ |
358 |
u_char *buf = malloc (MAXPACKET); |
377 |
u_char *buf = malloc (MAXPACKET); |
359 |
if (buf == NULL) |
378 |
if (buf == NULL) |
360 |
return (-1); |
379 |
return (-1); |
Lines 630-635
get_nsaddr (res_state statp, int n)
Link Here
|
630 |
return (struct sockaddr *) (void *) &statp->nsaddr_list[n]; |
649 |
return (struct sockaddr *) (void *) &statp->nsaddr_list[n]; |
631 |
} |
650 |
} |
632 |
|
651 |
|
|
|
652 |
/* The send_vc function is responsible for sending a DNS query over TCP |
653 |
to the nameserver numbered NS from the res_state STATP i.e. |
654 |
EXT(statp).nssocks[ns]. The function supports sending both IPv4 and |
655 |
IPv6 queries at the same serially on the same socket. |
656 |
|
657 |
Please note that for TCP there is no way to disable sending both |
658 |
queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP |
659 |
and sends the queries serially and waits for the result after each |
660 |
sent query. This implemetnation should be corrected to honour these |
661 |
options. |
662 |
|
663 |
Please also note that for TCP we send both queries over the same |
664 |
socket one after another. This technically violates best practice |
665 |
since the server is allowed to read the first query, respond, and |
666 |
then close the socket (to service another client). If the server |
667 |
does this, then the remaining second query in the socket data buffer |
668 |
will cause the server to send the client an RST which will arrive |
669 |
asynchronously and the client's OS will likely tear down the socket |
670 |
receive buffer resulting in a potentially short read and lost |
671 |
response data. This will force the client to retry the query again, |
672 |
and this process may repeat until all servers and connection resets |
673 |
are exhausted and then the query will fail. It's not known if this |
674 |
happens with any frequency in real DNS server implementations. This |
675 |
implementation should be corrected to use two sockets by default for |
676 |
parallel queries. |
677 |
|
678 |
The query stored in BUF of BUFLEN length is sent first followed by |
679 |
the query stored in BUF2 of BUFLEN2 length. Queries are sent |
680 |
serially on the same socket. |
681 |
|
682 |
Answers to the query are stored firstly in *ANSP up to a max of |
683 |
*ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP |
684 |
is non-NULL (to indicate that modifying the answer buffer is allowed) |
685 |
then malloc is used to allocate a new response buffer and ANSCP and |
686 |
ANSP will both point to the new buffer. If more than *ANSSIZP bytes |
687 |
are needed but ANSCP is NULL, then as much of the response as |
688 |
possible is read into the buffer, but the results will be truncated. |
689 |
When truncation happens because of a small answer buffer the DNS |
690 |
packets header feild TC will bet set to 1, indicating a truncated |
691 |
message and the rest of the socket data will be read and discarded. |
692 |
|
693 |
Answers to the query are stored secondly in *ANSP2 up to a max of |
694 |
*ANSSIZP2 bytes, with the actual response length stored in |
695 |
*RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2 |
696 |
is non-NULL (required for a second query) then malloc is used to |
697 |
allocate a new response buffer, *ANSSIZP2 is set to the new buffer |
698 |
size and *ANSP2_MALLOCED is set to 1. |
699 |
|
700 |
The ANSP2_MALLOCED argument will eventually be removed as the |
701 |
change in buffer pointer can be used to detect the buffer has |
702 |
changed and that the caller should use free on the new buffer. |
703 |
|
704 |
Note that the answers may arrive in any order from the server and |
705 |
therefore the first and second answer buffers may not correspond to |
706 |
the first and second queries. |
707 |
|
708 |
It is not supported to call this function with a non-NULL ANSP2 |
709 |
but a NULL ANSCP. Put another way, you can call send_vc with a |
710 |
single unmodifiable buffer or two modifiable buffers, but no other |
711 |
combination is supported. |
712 |
|
713 |
It is the caller's responsibility to free the malloc allocated |
714 |
buffers by detecting that the pointers have changed from their |
715 |
original values i.e. *ANSCP or *ANSP2 has changed. |
716 |
|
717 |
If errors are encountered then *TERRNO is set to an appropriate |
718 |
errno value and a zero result is returned for a recoverable error, |
719 |
and a less-than zero result is returned for a non-recoverable error. |
720 |
|
721 |
If no errors are encountered then *TERRNO is left unmodified and |
722 |
a the length of the first response in bytes is returned. */ |
633 |
static int |
723 |
static int |
634 |
send_vc(res_state statp, |
724 |
send_vc(res_state statp, |
635 |
const u_char *buf, int buflen, const u_char *buf2, int buflen2, |
725 |
const u_char *buf, int buflen, const u_char *buf2, int buflen2, |
Lines 639-649
send_vc(res_state statp,
Link Here
|
639 |
{ |
729 |
{ |
640 |
const HEADER *hp = (HEADER *) buf; |
730 |
const HEADER *hp = (HEADER *) buf; |
641 |
const HEADER *hp2 = (HEADER *) buf2; |
731 |
const HEADER *hp2 = (HEADER *) buf2; |
642 |
u_char *ans = *ansp; |
732 |
HEADER *anhp = (HEADER *) *ansp; |
643 |
int orig_anssizp = *anssizp; |
|
|
644 |
// XXX REMOVE |
645 |
// int anssiz = *anssizp; |
646 |
HEADER *anhp = (HEADER *) ans; |
647 |
struct sockaddr *nsap = get_nsaddr (statp, ns); |
733 |
struct sockaddr *nsap = get_nsaddr (statp, ns); |
648 |
int truncating, connreset, n; |
734 |
int truncating, connreset, n; |
649 |
/* On some architectures compiler might emit a warning indicating |
735 |
/* On some architectures compiler might emit a warning indicating |
Lines 731-736
send_vc(res_state statp,
Link Here
|
731 |
* Receive length & response |
817 |
* Receive length & response |
732 |
*/ |
818 |
*/ |
733 |
int recvresp1 = 0; |
819 |
int recvresp1 = 0; |
|
|
820 |
/* Skip the second response if there is no second query. |
821 |
To do that we mark the second response as received. */ |
734 |
int recvresp2 = buf2 == NULL; |
822 |
int recvresp2 = buf2 == NULL; |
735 |
uint16_t rlen16; |
823 |
uint16_t rlen16; |
736 |
read_len: |
824 |
read_len: |
Lines 767-806
send_vc(res_state statp,
Link Here
|
767 |
u_char **thisansp; |
855 |
u_char **thisansp; |
768 |
int *thisresplenp; |
856 |
int *thisresplenp; |
769 |
if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) { |
857 |
if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) { |
|
|
858 |
/* We have not received any responses |
859 |
yet or we only have one response to |
860 |
receive. */ |
770 |
thisanssizp = anssizp; |
861 |
thisanssizp = anssizp; |
771 |
thisansp = anscp ?: ansp; |
862 |
thisansp = anscp ?: ansp; |
772 |
assert (anscp != NULL || ansp2 == NULL); |
863 |
assert (anscp != NULL || ansp2 == NULL); |
773 |
thisresplenp = &resplen; |
864 |
thisresplenp = &resplen; |
774 |
} else { |
865 |
} else { |
775 |
if (*anssizp != MAXPACKET) { |
|
|
776 |
/* No buffer allocated for the first |
777 |
reply. We can try to use the rest |
778 |
of the user-provided buffer. */ |
779 |
#if __GNUC_PREREQ (4, 7) |
780 |
DIAG_PUSH_NEEDS_COMMENT; |
781 |
DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); |
782 |
#endif |
783 |
#if _STRING_ARCH_unaligned |
784 |
*anssizp2 = orig_anssizp - resplen; |
785 |
*ansp2 = *ansp + resplen; |
786 |
#else |
787 |
int aligned_resplen |
788 |
= ((resplen + __alignof__ (HEADER) - 1) |
789 |
& ~(__alignof__ (HEADER) - 1)); |
790 |
*anssizp2 = orig_anssizp - aligned_resplen; |
791 |
*ansp2 = *ansp + aligned_resplen; |
792 |
#endif |
793 |
#if __GNUC_PREREQ (4, 7) |
794 |
DIAG_POP_NEEDS_COMMENT; |
795 |
#endif |
796 |
} else { |
797 |
/* The first reply did not fit into the |
798 |
user-provided buffer. Maybe the second |
799 |
answer will. */ |
800 |
*anssizp2 = orig_anssizp; |
801 |
*ansp2 = *ansp; |
802 |
} |
803 |
|
804 |
thisanssizp = anssizp2; |
866 |
thisanssizp = anssizp2; |
805 |
thisansp = ansp2; |
867 |
thisansp = ansp2; |
806 |
thisresplenp = resplen2; |
868 |
thisresplenp = resplen2; |
Lines 804-813
send_vc(res_state statp,
Link Here
|
804 |
anhp = (HEADER *) *thisansp; |
870 |
anhp = (HEADER *) *thisansp; |
805 |
|
871 |
|
806 |
*thisresplenp = rlen; |
872 |
*thisresplenp = rlen; |
807 |
if (rlen > *thisanssizp) { |
873 |
/* Is the answer buffer too small? */ |
808 |
/* Yes, we test ANSCP here. If we have two buffers |
874 |
if (*thisanssizp < rlen) { |
809 |
both will be allocatable. */ |
875 |
/* If the current buffer is not the the static |
810 |
if (__glibc_likely (anscp != NULL)) { |
876 |
user-supplied buffer then we can reallocate |
|
|
877 |
it. */ |
878 |
if (thisansp != NULL && thisansp != ansp) { |
879 |
/* Always allocate MAXPACKET, callers expect |
880 |
this specific size. */ |
811 |
u_char *newp = malloc (MAXPACKET); |
881 |
u_char *newp = malloc (MAXPACKET); |
812 |
if (newp == NULL) { |
882 |
if (newp == NULL) { |
813 |
*terrno = ENOMEM; |
883 |
*terrno = ENOMEM; |
Lines 819-824
send_vc(res_state statp,
Link Here
|
819 |
if (thisansp == ansp2) |
889 |
if (thisansp == ansp2) |
820 |
*ansp2_malloced = 1; |
890 |
*ansp2_malloced = 1; |
821 |
anhp = (HEADER *) newp; |
891 |
anhp = (HEADER *) newp; |
|
|
892 |
/* A uint16_t can't be larger than MAXPACKET |
893 |
thus it's safe to allocate MAXPACKET but |
894 |
read RLEN bytes instead. */ |
822 |
len = rlen; |
895 |
len = rlen; |
823 |
} else { |
896 |
} else { |
824 |
Dprint(statp->options & RES_DEBUG, |
897 |
Dprint(statp->options & RES_DEBUG, |
Lines 948-953
reopen (res_state statp, int *terrno, int ns)
Link Here
|
948 |
return 1; |
1021 |
return 1; |
949 |
} |
1022 |
} |
950 |
|
1023 |
|
|
|
1024 |
/* The send_dg function is responsible for sending a DNS query over UDP |
1025 |
to the nameserver numbered NS from the res_state STATP i.e. |
1026 |
EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries |
1027 |
along with the ability to send the query in parallel for both stacks |
1028 |
(default) or serially (RES_SINGLKUP). It also supports serial lookup |
1029 |
with a close and reopen of the socket used to talk to the server |
1030 |
(RES_SNGLKUPREOP) to work around broken name servers. |
1031 |
|
1032 |
The query stored in BUF of BUFLEN length is sent first followed by |
1033 |
the query stored in BUF2 of BUFLEN2 length. Queries are sent |
1034 |
in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP). |
1035 |
|
1036 |
Answers to the query are stored firstly in *ANSP up to a max of |
1037 |
*ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP |
1038 |
is non-NULL (to indicate that modifying the answer buffer is allowed) |
1039 |
then malloc is used to allocate a new response buffer and ANSCP and |
1040 |
ANSP will both point to the new buffer. If more than *ANSSIZP bytes |
1041 |
are needed but ANSCP is NULL, then as much of the response as |
1042 |
possible is read into the buffer, but the results will be truncated. |
1043 |
When truncation happens because of a small answer buffer the DNS |
1044 |
packets header feild TC will bet set to 1, indicating a truncated |
1045 |
message, while the rest of the UDP packet is discarded. |
1046 |
|
1047 |
Answers to the query are stored secondly in *ANSP2 up to a max of |
1048 |
*ANSSIZP2 bytes, with the actual response length stored in |
1049 |
*RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2 |
1050 |
is non-NULL (required for a second query) then malloc is used to |
1051 |
allocate a new response buffer, *ANSSIZP2 is set to the new buffer |
1052 |
size and *ANSP2_MALLOCED is set to 1. |
1053 |
|
1054 |
The ANSP2_MALLOCED argument will eventually be removed as the |
1055 |
change in buffer pointer can be used to detect the buffer has |
1056 |
changed and that the caller should use free on the new buffer. |
1057 |
|
1058 |
Note that the answers may arrive in any order from the server and |
1059 |
therefore the first and second answer buffers may not correspond to |
1060 |
the first and second queries. |
1061 |
|
1062 |
It is not supported to call this function with a non-NULL ANSP2 |
1063 |
but a NULL ANSCP. Put another way, you can call send_vc with a |
1064 |
single unmodifiable buffer or two modifiable buffers, but no other |
1065 |
combination is supported. |
1066 |
|
1067 |
It is the caller's responsibility to free the malloc allocated |
1068 |
buffers by detecting that the pointers have changed from their |
1069 |
original values i.e. *ANSCP or *ANSP2 has changed. |
1070 |
|
1071 |
If an answer is truncated because of UDP datagram DNS limits then |
1072 |
*V_CIRCUIT is set to 1 and the return value non-zero to indicate to |
1073 |
the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1 |
1074 |
if any progress was made reading a response from the nameserver and |
1075 |
is used by the caller to distinguish between ECONNREFUSED and |
1076 |
ETIMEDOUT (the latter if *GOTSOMEWHERE is 1). |
1077 |
|
1078 |
If errors are encountered then *TERRNO is set to an appropriate |
1079 |
errno value and a zero result is returned for a recoverable error, |
1080 |
and a less-than zero result is returned for a non-recoverable error. |
1081 |
|
1082 |
If no errors are encountered then *TERRNO is left unmodified and |
1083 |
a the length of the first response in bytes is returned. */ |
951 |
static int |
1084 |
static int |
952 |
send_dg(res_state statp, |
1085 |
send_dg(res_state statp, |
953 |
const u_char *buf, int buflen, const u_char *buf2, int buflen2, |
1086 |
const u_char *buf, int buflen, const u_char *buf2, int buflen2, |
Lines 957-964
send_dg(res_state statp,
Link Here
|
957 |
{ |
1090 |
{ |
958 |
const HEADER *hp = (HEADER *) buf; |
1091 |
const HEADER *hp = (HEADER *) buf; |
959 |
const HEADER *hp2 = (HEADER *) buf2; |
1092 |
const HEADER *hp2 = (HEADER *) buf2; |
960 |
u_char *ans = *ansp; |
|
|
961 |
int orig_anssizp = *anssizp; |
962 |
struct timespec now, timeout, finish; |
1093 |
struct timespec now, timeout, finish; |
963 |
struct pollfd pfd[1]; |
1094 |
struct pollfd pfd[1]; |
964 |
int ptimeout; |
1095 |
int ptimeout; |
Lines 991-996
send_dg(res_state statp,
Link Here
|
991 |
int need_recompute = 0; |
1122 |
int need_recompute = 0; |
992 |
int nwritten = 0; |
1123 |
int nwritten = 0; |
993 |
int recvresp1 = 0; |
1124 |
int recvresp1 = 0; |
|
|
1125 |
/* Skip the second response if there is no second query. |
1126 |
To do that we mark the second response as received. */ |
994 |
int recvresp2 = buf2 == NULL; |
1127 |
int recvresp2 = buf2 == NULL; |
995 |
pfd[0].fd = EXT(statp).nssocks[ns]; |
1128 |
pfd[0].fd = EXT(statp).nssocks[ns]; |
996 |
pfd[0].events = POLLOUT; |
1129 |
pfd[0].events = POLLOUT; |
Lines 1154-1208
send_dg(res_state statp,
Link Here
|
1154 |
int *thisresplenp; |
1287 |
int *thisresplenp; |
1155 |
|
1288 |
|
1156 |
if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) { |
1289 |
if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) { |
|
|
1290 |
/* We have not received any responses |
1291 |
yet or we only have one response to |
1292 |
receive. */ |
1157 |
thisanssizp = anssizp; |
1293 |
thisanssizp = anssizp; |
1158 |
thisansp = anscp ?: ansp; |
1294 |
thisansp = anscp ?: ansp; |
1159 |
assert (anscp != NULL || ansp2 == NULL); |
1295 |
assert (anscp != NULL || ansp2 == NULL); |
1160 |
thisresplenp = &resplen; |
1296 |
thisresplenp = &resplen; |
1161 |
} else { |
1297 |
} else { |
1162 |
if (*anssizp != MAXPACKET) { |
|
|
1163 |
/* No buffer allocated for the first |
1164 |
reply. We can try to use the rest |
1165 |
of the user-provided buffer. */ |
1166 |
#if _STRING_ARCH_unaligned |
1167 |
*anssizp2 = orig_anssizp - resplen; |
1168 |
*ansp2 = *ansp + resplen; |
1169 |
#else |
1170 |
int aligned_resplen |
1171 |
= ((resplen + __alignof__ (HEADER) - 1) |
1172 |
& ~(__alignof__ (HEADER) - 1)); |
1173 |
*anssizp2 = orig_anssizp - aligned_resplen; |
1174 |
*ansp2 = *ansp + aligned_resplen; |
1175 |
#endif |
1176 |
} else { |
1177 |
/* The first reply did not fit into the |
1178 |
user-provided buffer. Maybe the second |
1179 |
answer will. */ |
1180 |
*anssizp2 = orig_anssizp; |
1181 |
*ansp2 = *ansp; |
1182 |
} |
1183 |
|
1184 |
thisanssizp = anssizp2; |
1298 |
thisanssizp = anssizp2; |
1185 |
thisansp = ansp2; |
1299 |
thisansp = ansp2; |
1186 |
thisresplenp = resplen2; |
1300 |
thisresplenp = resplen2; |
1187 |
} |
1301 |
} |
1188 |
|
1302 |
|
1189 |
if (*thisanssizp < MAXPACKET |
1303 |
if (*thisanssizp < MAXPACKET |
1190 |
/* Yes, we test ANSCP here. If we have two buffers |
1304 |
/* If the current buffer is not the the static |
1191 |
both will be allocatable. */ |
1305 |
user-supplied buffer then we can reallocate |
1192 |
&& anscp |
1306 |
it. */ |
|
|
1307 |
&& (thisansp != NULL && thisansp != ansp) |
1193 |
#ifdef FIONREAD |
1308 |
#ifdef FIONREAD |
|
|
1309 |
/* Is the size too small? */ |
1194 |
&& (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0 |
1310 |
&& (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0 |
1195 |
|| *thisanssizp < *thisresplenp) |
1311 |
|| *thisanssizp < *thisresplenp) |
1196 |
#endif |
1312 |
#endif |
1197 |
) { |
1313 |
) { |
|
|
1314 |
/* Always allocate MAXPACKET, callers expect |
1315 |
this specific size. */ |
1198 |
u_char *newp = malloc (MAXPACKET); |
1316 |
u_char *newp = malloc (MAXPACKET); |
1199 |
if (newp != NULL) { |
1317 |
if (newp != NULL) { |
1200 |
*anssizp = MAXPACKET; |
1318 |
*thisanssizp = MAXPACKET; |
1201 |
*thisansp = ans = newp; |
1319 |
*thisansp = newp; |
1202 |
if (thisansp == ansp2) |
1320 |
if (thisansp == ansp2) |
1203 |
*ansp2_malloced = 1; |
1321 |
*ansp2_malloced = 1; |
1204 |
} |
1322 |
} |
1205 |
} |
1323 |
} |
|
|
1324 |
/* We could end up with truncation if anscp was NULL |
1325 |
(not allowed to change caller's buffer) and the |
1326 |
response buffer size is too small. This isn't a |
1327 |
reliable way to detect truncation because the ioctl |
1328 |
may be an inaccurate report of the UDP message size. |
1329 |
Therefore we use this only to issue debug output. |
1330 |
To do truncation accurately with UDP we need |
1331 |
MSG_TRUNC which is only available on Linux. We |
1332 |
can abstract out the Linux-specific feature in the |
1333 |
future to detect truncation. */ |
1334 |
if (__glibc_unlikely (*thisanssizp < *thisresplenp)) { |
1335 |
Dprint(statp->options & RES_DEBUG, |
1336 |
(stdout, ";; response may be truncated (UDP)\n") |
1337 |
); |
1338 |
} |
1339 |
|
1206 |
HEADER *anhp = (HEADER *) *thisansp; |
1340 |
HEADER *anhp = (HEADER *) *thisansp; |
1207 |
socklen_t fromlen = sizeof(struct sockaddr_in6); |
1341 |
socklen_t fromlen = sizeof(struct sockaddr_in6); |
1208 |
assert (sizeof(from) <= fromlen); |
1342 |
assert (sizeof(from) <= fromlen); |