unix-resolv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  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. #include "private-lib-core.h"
  25. lws_async_dns_server_check_t
  26. lws_plat_asyncdns_init(struct lws_context *context, lws_sockaddr46 *sa46)
  27. {
  28. lws_async_dns_server_check_t s = LADNS_CONF_SERVER_CHANGED;
  29. lws_sockaddr46 sa46t;
  30. lws_tokenize_t ts;
  31. int fd, n, ns = 0;
  32. char ads[48], *r;
  33. r = (char *)context->pt[0].serv_buf;
  34. /* grab the first chunk of /etc/resolv.conf */
  35. fd = open("/etc/resolv.conf", LWS_O_RDONLY);
  36. if (fd < 0)
  37. return LADNS_CONF_SERVER_UNKNOWN;
  38. n = read(fd, r, context->pt_serv_buf_size - 1);
  39. close(fd);
  40. if (n < 0)
  41. return LADNS_CONF_SERVER_UNKNOWN;
  42. r[n] = '\0';
  43. lws_tokenize_init(&ts, r, LWS_TOKENIZE_F_DOT_NONTERM |
  44. LWS_TOKENIZE_F_NO_FLOATS |
  45. LWS_TOKENIZE_F_NO_INTEGERS |
  46. LWS_TOKENIZE_F_MINUS_NONTERM |
  47. LWS_TOKENIZE_F_HASH_COMMENT);
  48. do {
  49. ts.e = lws_tokenize(&ts);
  50. if (ts.e != LWS_TOKZE_TOKEN) {
  51. ns = 0;
  52. continue;
  53. }
  54. if (!ns && !strncmp("nameserver", ts.token, ts.token_len)) {
  55. ns = 1;
  56. continue;
  57. }
  58. if (!ns)
  59. continue;
  60. /* we are a token just after the "nameserver" token */
  61. ns = 0;
  62. if (ts.token_len > (int)sizeof(ads) - 1)
  63. continue;
  64. memcpy(ads, ts.token, ts.token_len);
  65. ads[ts.token_len] = '\0';
  66. if (lws_sa46_parse_numeric_address(ads, &sa46t) < 0)
  67. continue;
  68. if (!lws_sa46_compare_ads(sa46, &sa46t))
  69. s = LADNS_CONF_SERVER_SAME;
  70. *sa46 = sa46t;
  71. return s;
  72. } while (ts.e > 0);
  73. return LADNS_CONF_SERVER_UNKNOWN;
  74. }
  75. /*
  76. * Platform-specific ntpclient server configuration
  77. */
  78. int
  79. lws_plat_ntpclient_config(struct lws_context *context)
  80. {
  81. #if defined(LWS_HAVE_GETENV)
  82. char *ntpsrv = getenv("LWS_NTP_SERVER");
  83. if (ntpsrv && strlen(ntpsrv) < 64) {
  84. lws_system_blob_direct_set(lws_system_get_blob(context,
  85. LWS_SYSBLOB_TYPE_NTP_SERVER, 0),
  86. (const uint8_t *)ntpsrv,
  87. strlen(ntpsrv));
  88. return 1;
  89. }
  90. #endif
  91. return 0;
  92. }