Skip to content

Commit 5d227b4

Browse files
committed
netdb:Add macro to control DNS using TCP transmission
Signed-off-by: yangjianqing <[email protected]>
1 parent 1e49cb4 commit 5d227b4

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

libs/libc/netdb/Kconfig

+7
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ config NETDB_DNSSERVER_IPv4ADDR
237237
Default DNS server IPv4 address in host byte order. Default value
238238
10.0.0.1. This may be changed via dns_add_nameserver().
239239

240+
config NETDB_DNS_STREAM
241+
bool "DNS supports TCP transmission"
242+
depends on LIBC_NETDB
243+
default n
244+
---help---
245+
Enable support for DNS Name resolution over TCP.
246+
240247
if NETDB_DNSSERVER_IPv6
241248

242249
config NETDB_DNSSERVER_IPv6ADDR_1

libs/libc/netdb/lib_dns.h

+4
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ void dns_restorelock(unsigned int count);
170170
*
171171
****************************************************************************/
172172

173+
#ifndef CONFIG_NETDB_DNS_STREAM
174+
int dns_bind(sa_family_t family);
175+
#else
173176
int dns_bind(sa_family_t family, bool stream);
177+
#endif
174178

175179
/****************************************************************************
176180
* Name: dns_query

libs/libc/netdb/lib_dnsbind.c

+12
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,28 @@
6363
*
6464
****************************************************************************/
6565

66+
#ifndef CONFIG_NETDB_DNS_STREAM
67+
int dns_bind(sa_family_t family)
68+
#else
6669
int dns_bind(sa_family_t family, bool stream)
70+
#endif
6771
{
72+
#ifdef CONFIG_NETDB_DNS_STREAM
6873
int stype = stream ? SOCK_STREAM : SOCK_DGRAM;
74+
#endif
75+
6976
struct timeval tv;
7077
int sd;
7178
int ret;
7279

7380
/* Create a new socket */
7481

82+
#ifndef CONFIG_NETDB_DNS_STREAM
83+
sd = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
84+
#else
7585
sd = socket(family, stype | SOCK_CLOEXEC, 0);
86+
#endif
87+
7688
if (sd < 0)
7789
{
7890
ret = -get_errno();

libs/libc/netdb/lib_dnsquery.c

+65-2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct dns_query_data_s
124124
*
125125
****************************************************************************/
126126

127+
#ifdef CONFIG_NETDB_DNS_STREAM
127128
static ssize_t stream_send(int fd, FAR const void *buf, size_t len)
128129
{
129130
ssize_t total = 0;
@@ -292,6 +293,7 @@ static ssize_t stream_recv_record(int fd, FAR void *buf, size_t len)
292293

293294
return ret;
294295
}
296+
#endif
295297

296298
/****************************************************************************
297299
* Name: dns_parse_name
@@ -380,11 +382,18 @@ static inline uint16_t dns_alloc_id(void)
380382
*
381383
****************************************************************************/
382384

385+
#ifndef CONFIG_NETDB_DNS_STREAM
386+
static int dns_send_query(int sd, FAR const char *name,
387+
FAR union dns_addr_u *uaddr, uint16_t rectype,
388+
FAR struct dns_query_info_s *qinfo,
389+
FAR uint8_t *buffer)
390+
#else
383391
static int dns_send_query(int sd, FAR const char *name,
384392
FAR union dns_addr_u *uaddr, uint16_t rectype,
385393
FAR struct dns_query_info_s *qinfo,
386394
FAR uint8_t *buffer,
387395
bool stream)
396+
#endif
388397
{
389398
FAR struct dns_header_s *hdr;
390399
FAR uint8_t *dest;
@@ -488,11 +497,13 @@ static int dns_send_query(int sd, FAR const char *name,
488497
return ret;
489498
}
490499

500+
#ifdef CONFIG_NETDB_DNS_STREAM
491501
if (stream)
492502
{
493503
ret = stream_send_record(sd, buffer, dest - buffer);
494504
}
495505
else
506+
#endif
496507
{
497508
ret = send(sd, buffer, dest - buffer, 0);
498509
}
@@ -519,10 +530,16 @@ static int dns_send_query(int sd, FAR const char *name,
519530
*
520531
****************************************************************************/
521532

533+
#ifndef CONFIG_NETDB_DNS_STREAM
534+
static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr,
535+
FAR struct dns_query_info_s *qinfo,
536+
FAR uint32_t *ttl, FAR uint8_t *buffer)
537+
#else
522538
static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr,
523539
FAR struct dns_query_info_s *qinfo,
524540
FAR uint32_t *ttl, FAR uint8_t *buffer,
525541
bool stream, bool *should_try_stream)
542+
#endif
526543
{
527544
FAR uint8_t *nameptr;
528545
FAR uint8_t *namestart;
@@ -543,11 +560,13 @@ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr,
543560

544561
/* Receive the response */
545562

563+
#ifdef CONFIG_NETDB_DNS_STREAM
546564
if (stream)
547565
{
548566
ret = stream_recv_record(sd, buffer, RECV_BUFFER_SIZE);
549567
}
550568
else
569+
#endif
551570
{
552571
ret = recv(sd, buffer, RECV_BUFFER_SIZE, 0);
553572
}
@@ -579,6 +598,7 @@ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr,
579598

580599
/* Check for error */
581600

601+
#ifdef CONFIG_NETDB_DNS_STREAM
582602
if ((hdr->flags1 & DNS_FLAG1_TRUNC) != 0)
583603
{
584604
/* RFC 2181
@@ -601,6 +621,7 @@ static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr,
601621
*should_try_stream = true;
602622
return -EAGAIN;
603623
}
624+
#endif
604625

605626
if ((hdr->flags2 & DNS_FLAG2_ERR_MASK) != 0)
606627
{
@@ -851,33 +872,47 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
851872
int retries;
852873
int ret;
853874
int sd;
875+
#ifdef CONFIG_NETDB_DNS_STREAM
854876
bool stream = false;
877+
#endif
855878

856879
/* Loop while receive timeout errors occur and there are remaining
857880
* retries.
858881
*/
859882

860883
for (retries = 0; retries < CONFIG_NETDB_DNSCLIENT_RETRIES; retries++)
861884
{
885+
#ifdef CONFIG_NETDB_DNS_STREAM
862886
bool should_try_stream;
863887

864888
try_stream:
889+
#endif
865890
#ifdef CONFIG_NET_IPv6
866891
if (dns_is_queryfamily(AF_INET6))
867892
{
868893
/* Send the IPv6 query */
869-
894+
#ifndef CONFIG_NETDB_DNS_STREAM
895+
sd = dns_bind(addr->sa_family);
896+
#else
870897
sd = dns_bind(addr->sa_family, stream);
898+
#endif
871899
if (sd < 0)
872900
{
873901
query->result = sd;
874902
return 0;
875903
}
876904

905+
#ifndef CONFIG_NETDB_DNS_STREAM
906+
ret = dns_send_query(sd, query->hostname,
907+
(FAR union dns_addr_u *)addr,
908+
DNS_RECTYPE_AAAA, &qdata->qinfo,
909+
qdata->buffer);
910+
#else
877911
ret = dns_send_query(sd, query->hostname,
878912
(FAR union dns_addr_u *)addr,
879913
DNS_RECTYPE_AAAA, &qdata->qinfo,
880914
qdata->buffer, stream);
915+
#endif
881916
if (ret < 0)
882917
{
883918
dns_query_error("ERROR: IPv6 dns_send_query failed",
@@ -887,25 +922,33 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
887922
else
888923
{
889924
/* Obtain the IPv6 response */
890-
925+
#ifndef CONFIG_NETDB_DNS_STREAM
926+
ret = dns_recv_response(sd, &query->addr[next],
927+
CONFIG_NETDB_MAX_IPv6ADDR,
928+
&qdata->qinfo,
929+
&query->ttl, qdata->buffer);
930+
#else
891931
should_try_stream = false;
892932
ret = dns_recv_response(sd, &query->addr[next],
893933
CONFIG_NETDB_MAX_IPv6ADDR,
894934
&qdata->qinfo,
895935
&query->ttl, qdata->buffer,
896936
stream, &should_try_stream);
937+
#endif
897938
if (ret >= 0)
898939
{
899940
next += ret;
900941
}
901942
else
902943
{
944+
#ifdef CONFIG_NETDB_DNS_STREAM
903945
if (!stream && should_try_stream)
904946
{
905947
stream = true;
906948
goto try_stream; /* Don't consume retry count */
907949
}
908950

951+
#endif
909952
dns_query_error("ERROR: IPv6 dns_recv_response failed",
910953
ret, (FAR union dns_addr_u *)addr);
911954
query->result = ret;
@@ -914,24 +957,35 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
914957

915958
close(sd);
916959
}
960+
917961
#endif
918962

919963
#ifdef CONFIG_NET_IPv4
920964
if (dns_is_queryfamily(AF_INET))
921965
{
922966
/* Send the IPv4 query */
923967

968+
#ifndef CONFIG_NETDB_DNS_STREAM
969+
sd = dns_bind(addr->sa_family);
970+
#else
924971
sd = dns_bind(addr->sa_family, stream);
972+
#endif
925973
if (sd < 0)
926974
{
927975
query->result = sd;
928976
return 0;
929977
}
930978

979+
#ifndef CONFIG_NETDB_DNS_STREAM
980+
ret = dns_send_query(sd, query->hostname,
981+
(FAR union dns_addr_u *)addr,
982+
DNS_RECTYPE_A, &qdata->qinfo, qdata->buffer);
983+
#else
931984
ret = dns_send_query(sd, query->hostname,
932985
(FAR union dns_addr_u *)addr,
933986
DNS_RECTYPE_A, &qdata->qinfo, qdata->buffer,
934987
stream);
988+
#endif
935989
if (ret < 0)
936990
{
937991
dns_query_error("ERROR: IPv4 dns_send_query failed",
@@ -947,24 +1001,33 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
9471001
next = *query->naddr / 2;
9481002
}
9491003

1004+
#ifndef CONFIG_NETDB_DNS_STREAM
1005+
ret = dns_recv_response(sd, &query->addr[next],
1006+
CONFIG_NETDB_MAX_IPv4ADDR,
1007+
&qdata->qinfo,
1008+
&query->ttl, qdata->buffer);
1009+
#else
9501010
should_try_stream = false;
9511011
ret = dns_recv_response(sd, &query->addr[next],
9521012
CONFIG_NETDB_MAX_IPv4ADDR,
9531013
&qdata->qinfo,
9541014
&query->ttl, qdata->buffer,
9551015
stream, &should_try_stream);
1016+
#endif
9561017
if (ret >= 0)
9571018
{
9581019
next += ret;
9591020
}
9601021
else
9611022
{
1023+
#ifdef CONFIG_NETDB_DNS_STREAM
9621024
if (!stream && should_try_stream)
9631025
{
9641026
stream = true;
9651027
goto try_stream; /* Don't consume retry count */
9661028
}
9671029

1030+
#endif
9681031
dns_query_error("ERROR: IPv4 dns_recv_response failed",
9691032
ret, (FAR union dns_addr_u *)addr);
9701033
query->result = ret;

0 commit comments

Comments
 (0)