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