1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci#include "private-lib-core.h"
26d4afb5ceSopenharmony_ci
27d4afb5ceSopenharmony_cilws_async_dns_server_check_t
28d4afb5ceSopenharmony_cilws_plat_asyncdns_init(struct lws_context *context, lws_sockaddr46 *sa46)
29d4afb5ceSopenharmony_ci{
30d4afb5ceSopenharmony_ci	lws_async_dns_server_check_t s = LADNS_CONF_SERVER_CHANGED;
31d4afb5ceSopenharmony_ci	lws_sockaddr46 sa46t;
32d4afb5ceSopenharmony_ci	lws_tokenize_t ts;
33d4afb5ceSopenharmony_ci	char ads[48], *r;
34d4afb5ceSopenharmony_ci	int fd, ns = 0;
35d4afb5ceSopenharmony_ci	ssize_t n;
36d4afb5ceSopenharmony_ci
37d4afb5ceSopenharmony_ci	r = (char *)context->pt[0].serv_buf;
38d4afb5ceSopenharmony_ci
39d4afb5ceSopenharmony_ci	/* grab the first chunk of /etc/resolv.conf */
40d4afb5ceSopenharmony_ci
41d4afb5ceSopenharmony_ci	fd = open("/etc/resolv.conf", LWS_O_RDONLY);
42d4afb5ceSopenharmony_ci	if (fd < 0)
43d4afb5ceSopenharmony_ci		return LADNS_CONF_SERVER_UNKNOWN;
44d4afb5ceSopenharmony_ci
45d4afb5ceSopenharmony_ci	n = read(fd, r, context->pt_serv_buf_size - 1);
46d4afb5ceSopenharmony_ci	close(fd);
47d4afb5ceSopenharmony_ci	if (n < 0)
48d4afb5ceSopenharmony_ci		return LADNS_CONF_SERVER_UNKNOWN;
49d4afb5ceSopenharmony_ci
50d4afb5ceSopenharmony_ci	r[n] = '\0';
51d4afb5ceSopenharmony_ci	lws_tokenize_init(&ts, r, LWS_TOKENIZE_F_DOT_NONTERM |
52d4afb5ceSopenharmony_ci				  LWS_TOKENIZE_F_NO_FLOATS |
53d4afb5ceSopenharmony_ci				  LWS_TOKENIZE_F_NO_INTEGERS |
54d4afb5ceSopenharmony_ci				  LWS_TOKENIZE_F_MINUS_NONTERM |
55d4afb5ceSopenharmony_ci				  LWS_TOKENIZE_F_HASH_COMMENT);
56d4afb5ceSopenharmony_ci	do {
57d4afb5ceSopenharmony_ci		ts.e = (int8_t)lws_tokenize(&ts);
58d4afb5ceSopenharmony_ci		if (ts.e != LWS_TOKZE_TOKEN) {
59d4afb5ceSopenharmony_ci			ns = 0;
60d4afb5ceSopenharmony_ci			continue;
61d4afb5ceSopenharmony_ci		}
62d4afb5ceSopenharmony_ci
63d4afb5ceSopenharmony_ci		if (!ns && !strncmp("nameserver", ts.token, ts.token_len)) {
64d4afb5ceSopenharmony_ci			ns = 1;
65d4afb5ceSopenharmony_ci			continue;
66d4afb5ceSopenharmony_ci		}
67d4afb5ceSopenharmony_ci		if (!ns)
68d4afb5ceSopenharmony_ci			continue;
69d4afb5ceSopenharmony_ci
70d4afb5ceSopenharmony_ci		/* we are a token just after the "nameserver" token */
71d4afb5ceSopenharmony_ci
72d4afb5ceSopenharmony_ci		ns = 0;
73d4afb5ceSopenharmony_ci		if (ts.token_len > (int)sizeof(ads) - 1)
74d4afb5ceSopenharmony_ci			continue;
75d4afb5ceSopenharmony_ci
76d4afb5ceSopenharmony_ci		memcpy(ads, ts.token, ts.token_len);
77d4afb5ceSopenharmony_ci		ads[ts.token_len] = '\0';
78d4afb5ceSopenharmony_ci		if (lws_sa46_parse_numeric_address(ads, &sa46t) < 0)
79d4afb5ceSopenharmony_ci			continue;
80d4afb5ceSopenharmony_ci
81d4afb5ceSopenharmony_ci		if (!lws_sa46_compare_ads(sa46, &sa46t))
82d4afb5ceSopenharmony_ci			s = LADNS_CONF_SERVER_SAME;
83d4afb5ceSopenharmony_ci
84d4afb5ceSopenharmony_ci		*sa46 = sa46t;
85d4afb5ceSopenharmony_ci
86d4afb5ceSopenharmony_ci		return s;
87d4afb5ceSopenharmony_ci
88d4afb5ceSopenharmony_ci	} while (ts.e > 0);
89d4afb5ceSopenharmony_ci
90d4afb5ceSopenharmony_ci	return LADNS_CONF_SERVER_UNKNOWN;
91d4afb5ceSopenharmony_ci}
92