1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/lib/plat/unix/unix-resolv.c
Andy Green c591e1adfc asynchronous dns for ipv4 and ipv6
This adds the option to have lws do its own dns resolution on
the event loop, without blocking.  Existing implementations get
the name resolution done by the libc, which is blocking.  In
the case you are opening client connections but need to carefully
manage latency, another connection opening and doing the name
resolution becomes a big problem.

Currently it supports

 - ipv4 / A records
 - ipv6 / AAAA records
 - ipv4-over-ipv6 ::ffff:1.2.3.4 A record promotion for ipv6
 - only one server supported over UDP :53
 - nameserver discovery on linux, windows, freertos

It also has some nice advantages

 - lws-style paranoid response parsing
 - random unique tid generation to increase difficulty of poisoning
 - it's really integrated with the lws event loop, it does not spawn
   threads or use the libc resolver, and of course no blocking at all
 - platform-specific server address capturing (from /etc/resolv.conf
   on linux, windows apis on windows)
 - it has LRU caching
 - piggybacking (multiple requests before the first completes go on
   a list on the first request, not spawn multiple requests)
 - observes TTL in cache
 - TTL and timeout use lws_sul timers on the event loop
 - ipv6 pieces only built if cmake LWS_IPV6 enabled
2019-09-19 06:54:53 +01:00

88 lines
2.6 KiB
C

/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "private-lib-core.h"
lws_async_dns_server_check_t
lws_plat_asyncdns_init(struct lws_context *context, lws_sockaddr46 *sa46)
{
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_CHANGED;
char resolv[512], ads[48];
lws_sockaddr46 sa46t;
lws_tokenize_t ts;
int fd, n, ns = 0;
/* grab the first chunk of /etc/resolv.conf */
fd = open("/etc/resolv.conf", LWS_O_RDONLY);
if (fd < 0)
return LADNS_CONF_SERVER_UNKNOWN;
n = read(fd, resolv, sizeof(resolv) - 1);
close(fd);
if (n < 0)
return LADNS_CONF_SERVER_UNKNOWN;
resolv[n] = '\0';
lws_tokenize_init(&ts, resolv, LWS_TOKENIZE_F_DOT_NONTERM |
LWS_TOKENIZE_F_NO_FLOATS |
LWS_TOKENIZE_F_NO_INTEGERS |
LWS_TOKENIZE_F_MINUS_NONTERM |
LWS_TOKENIZE_F_HASH_COMMENT);
do {
ts.e = lws_tokenize(&ts);
if (ts.e != LWS_TOKZE_TOKEN) {
ns = 0;
continue;
}
if (!ns && !strncmp("nameserver", ts.token, ts.token_len)) {
ns = 1;
continue;
}
if (!ns)
continue;
/* we are a token just after the "nameserver" token */
ns = 0;
if (ts.token_len > (int)sizeof(ads) - 1)
continue;
memcpy(ads, ts.token, ts.token_len);
ads[ts.token_len] = '\0';
if (lws_sa46_parse_numeric_address(ads, &sa46t) < 0)
continue;
if (!lws_sa46_compare_ads(sa46, &sa46t))
s = LADNS_CONF_SERVER_SAME;
*sa46 = sa46t;
return s;
} while (ts.e > 0);
return LADNS_CONF_SERVER_UNKNOWN;
}