Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 574880
Collapse All | Expand All

(-)a/resolv/nss_dns/dns-host.c (-2 / +109 lines)
Lines 1031-1037 gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname, Link Here
1031
  int h_namelen = 0;
1031
  int h_namelen = 0;
1032
1032
1033
  if (ancount == 0)
1033
  if (ancount == 0)
1034
    return NSS_STATUS_NOTFOUND;
1034
    {
1035
      *h_errnop = HOST_NOT_FOUND;
1036
      return NSS_STATUS_NOTFOUND;
1037
    }
1035
1038
1036
  while (ancount-- > 0 && cp < end_of_message && had_error == 0)
1039
  while (ancount-- > 0 && cp < end_of_message && had_error == 0)
1037
    {
1040
    {
Lines 1208-1214 gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname, Link Here
1208
  /* Special case here: if the resolver sent a result but it only
1211
  /* Special case here: if the resolver sent a result but it only
1209
     contains a CNAME while we are looking for a T_A or T_AAAA record,
1212
     contains a CNAME while we are looking for a T_A or T_AAAA record,
1210
     we fail with NOTFOUND instead of TRYAGAIN.  */
1213
     we fail with NOTFOUND instead of TRYAGAIN.  */
1211
  return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
1214
  if (canon != NULL)
1215
    {
1216
      *h_errnop = HOST_NOT_FOUND;
1217
      return NSS_STATUS_NOTFOUND;
1218
    }
1219
1220
  *h_errnop = NETDB_INTERNAL;
1221
  return NSS_STATUS_TRYAGAIN;
1212
}
1222
}
1213
1223
1214
1224
Lines 1222-1232 gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2, Link Here
1222
1232
1223
  enum nss_status status = NSS_STATUS_NOTFOUND;
1233
  enum nss_status status = NSS_STATUS_NOTFOUND;
1224
1234
1235
  /* Combining the NSS status of two distinct queries requires some
1236
     compromise and attention to symmetry (A or AAAA queries can be
1237
     returned in any order).  What follows is a breakdown of how this
1238
     code is expected to work and why. We discuss only SUCCESS,
1239
     TRYAGAIN, NOTFOUND and UNAVAIL, since they are the only returns
1240
     that apply (though RETURN and MERGE exist).  We make a distinction
1241
     between TRYAGAIN (recoverable) and TRYAGAIN' (not-recoverable).
1242
     A recoverable TRYAGAIN is almost always due to buffer size issues
1243
     and returns ERANGE in errno and the caller is expected to retry
1244
     with a larger buffer.
1245
1246
     Lastly, you may be tempted to make significant changes to the
1247
     conditions in this code to bring about symmetry between responses.
1248
     Please don't change anything without due consideration for
1249
     expected application behaviour.  Some of the synthesized responses
1250
     aren't very well thought out and sometimes appear to imply that
1251
     IPv4 responses are always answer 1, and IPv6 responses are always
1252
     answer 2, but that's not true (see the implemetnation of send_dg
1253
     and send_vc to see response can arrive in any order, particlarly
1254
     for UDP). However, we expect it holds roughly enough of the time
1255
     that this code works, but certainly needs to be fixed to make this
1256
     a more robust implementation.
1257
1258
     ----------------------------------------------
1259
     | Answer 1 Status /   | Synthesized | Reason |
1260
     | Answer 2 Status     | Status      |        |
1261
     |--------------------------------------------|
1262
     | SUCCESS/SUCCESS     | SUCCESS     | [1]    |
1263
     | SUCCESS/TRYAGAIN    | TRYAGAIN    | [5]    |
1264
     | SUCCESS/TRYAGAIN'   | SUCCESS     | [1]    |
1265
     | SUCCESS/NOTFOUND    | SUCCESS     | [1]    |
1266
     | SUCCESS/UNAVAIL     | SUCCESS     | [1]    |
1267
     | TRYAGAIN/SUCCESS    | TRYAGAIN    | [2]    |
1268
     | TRYAGAIN/TRYAGAIN   | TRYAGAIN    | [2]    |
1269
     | TRYAGAIN/TRYAGAIN'  | TRYAGAIN    | [2]    |
1270
     | TRYAGAIN/NOTFOUND   | TRYAGAIN    | [2]    |
1271
     | TRYAGAIN/UNAVAIL    | TRYAGAIN    | [2]    |
1272
     | TRYAGAIN'/SUCCESS   | SUCCESS     | [3]    |
1273
     | TRYAGAIN'/TRYAGAIN  | TRYAGAIN    | [3]    |
1274
     | TRYAGAIN'/TRYAGAIN' | TRYAGAIN'   | [3]    |
1275
     | TRYAGAIN'/NOTFOUND  | TRYAGAIN'   | [3]    |
1276
     | TRYAGAIN'/UNAVAIL   | UNAVAIL     | [3]    |
1277
     | NOTFOUND/SUCCESS    | SUCCESS     | [3]    |
1278
     | NOTFOUND/TRYAGAIN   | TRYAGAIN    | [3]    |
1279
     | NOTFOUND/TRYAGAIN'  | TRYAGAIN'   | [3]    |
1280
     | NOTFOUND/NOTFOUND   | NOTFOUND    | [3]    |
1281
     | NOTFOUND/UNAVAIL    | UNAVAIL     | [3]    |
1282
     | UNAVAIL/SUCCESS     | UNAVAIL     | [4]    |
1283
     | UNAVAIL/TRYAGAIN    | UNAVAIL     | [4]    |
1284
     | UNAVAIL/TRYAGAIN'   | UNAVAIL     | [4]    |
1285
     | UNAVAIL/NOTFOUND    | UNAVAIL     | [4]    |
1286
     | UNAVAIL/UNAVAIL     | UNAVAIL     | [4]    |
1287
     ----------------------------------------------
1288
1289
     [1] If the first response is a success we return success.
1290
         This ignores the state of the second answer and in fact
1291
         incorrectly sets errno and h_errno to that of the second
1292
	 answer.  However because the response is a success we ignore
1293
	 *errnop and *h_errnop (though that means you touched errno on
1294
         success).  We are being conservative here and returning the
1295
         likely IPv4 response in the first answer as a success.
1296
1297
     [2] If the first response is a recoverable TRYAGAIN we return
1298
	 that instead of looking at the second response.  The
1299
	 expectation here is that we have failed to get an IPv4 response
1300
	 and should retry both queries.
1301
1302
     [3] If the first response was not a SUCCESS and the second
1303
	 response is not NOTFOUND (had a SUCCESS, need to TRYAGAIN,
1304
	 or failed entirely e.g. TRYAGAIN' and UNAVAIL) then use the
1305
	 result from the second response, otherwise the first responses
1306
	 status is used.  Again we have some odd side-effects when the
1307
	 second response is NOTFOUND because we overwrite *errnop and
1308
	 *h_errnop that means that a first answer of NOTFOUND might see
1309
	 its *errnop and *h_errnop values altered.  Whether it matters
1310
	 in practice that a first response NOTFOUND has the wrong
1311
	 *errnop and *h_errnop is undecided.
1312
1313
     [4] If the first response is UNAVAIL we return that instead of
1314
	 looking at the second response.  The expectation here is that
1315
	 it will have failed similarly e.g. configuration failure.
1316
1317
     [5] Testing this code is complicated by the fact that truncated
1318
	 second response buffers might be returned as SUCCESS if the
1319
	 first answer is a SUCCESS.  To fix this we add symmetry to
1320
	 TRYAGAIN with the second response.  If the second response
1321
	 is a recoverable error we now return TRYAGIN even if the first
1322
	 response was SUCCESS.  */
1323
1225
  if (anslen1 > 0)
1324
  if (anslen1 > 0)
1226
    status = gaih_getanswer_slice(answer1, anslen1, qname,
1325
    status = gaih_getanswer_slice(answer1, anslen1, qname,
1227
				  &pat, &buffer, &buflen,
1326
				  &pat, &buffer, &buflen,
1228
				  errnop, h_errnop, ttlp,
1327
				  errnop, h_errnop, ttlp,
1229
				  &first);
1328
				  &first);
1329
1230
  if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND
1330
  if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND
1231
       || (status == NSS_STATUS_TRYAGAIN
1331
       || (status == NSS_STATUS_TRYAGAIN
1232
	   /* We want to look at the second answer in case of an
1332
	   /* We want to look at the second answer in case of an
Lines 1242-1249 gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2, Link Here
1242
						     &pat, &buffer, &buflen,
1342
						     &pat, &buffer, &buflen,
1243
						     errnop, h_errnop, ttlp,
1343
						     errnop, h_errnop, ttlp,
1244
						     &first);
1344
						     &first);
1345
      /* Use the second response status in some cases.  */
1245
      if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND)
1346
      if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND)
1246
	status = status2;
1347
	status = status2;
1348
      /* Do not return a truncated second response (unless it was
1349
         unavoidable e.g. unrecoverable TRYAGAIN).  */
1350
      if (status == NSS_STATUS_SUCCESS
1351
	  && (status2 == NSS_STATUS_TRYAGAIN
1352
	      && *errnop == ERANGE && *h_errnop != NO_RECOVERY))
1353
	status = NSS_STATUS_TRYAGAIN;
1247
    }
1354
    }
1248
1355
1249
  return status;
1356
  return status;
(-)a/resolv/res_query.c (+3 lines)
Lines 396-401 __libc_res_nsearch(res_state statp, Link Here
396
		  {
396
		  {
397
		    free (*answerp2);
397
		    free (*answerp2);
398
		    *answerp2 = NULL;
398
		    *answerp2 = NULL;
399
		    *nanswerp2 = 0;
399
		    *answerp2_malloced = 0;
400
		    *answerp2_malloced = 0;
400
		  }
401
		  }
401
	}
402
	}
Lines 447-452 __libc_res_nsearch(res_state statp, Link Here
447
			  {
448
			  {
448
			    free (*answerp2);
449
			    free (*answerp2);
449
			    *answerp2 = NULL;
450
			    *answerp2 = NULL;
451
			    *nanswerp2 = 0;
450
			    *answerp2_malloced = 0;
452
			    *answerp2_malloced = 0;
451
			  }
453
			  }
452
454
Lines 521-526 __libc_res_nsearch(res_state statp, Link Here
521
	  {
523
	  {
522
	    free (*answerp2);
524
	    free (*answerp2);
523
	    *answerp2 = NULL;
525
	    *answerp2 = NULL;
526
	    *nanswerp2 = 0;
524
	    *answerp2_malloced = 0;
527
	    *answerp2_malloced = 0;
525
	  }
528
	  }
526
	if (saved_herrno != -1)
529
	if (saved_herrno != -1)
(-)a/resolv/res_send.c (-67 / +197 lines)
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);

Return to bug 574880