server.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _SMARTDNS_SERVER_
  19. #define _SMARTDNS_SERVER_
  20. #include "dns.h"
  21. #include "include/utils.h"
  22. #include <functional>
  23. #include <string>
  24. #include <sys/socket.h>
  25. #include <thread>
  26. #include <unistd.h>
  27. namespace smartdns
  28. {
  29. class Server
  30. {
  31. public:
  32. enum CONF_TYPE {
  33. CONF_TYPE_STRING,
  34. CONF_TYPE_FILE,
  35. };
  36. enum CREATE_MODE {
  37. CREATE_MODE_FORK,
  38. CREATE_MODE_THREAD,
  39. };
  40. Server();
  41. Server(enum CREATE_MODE mode);
  42. virtual ~Server();
  43. bool Start(const std::string &conf, enum CONF_TYPE type = CONF_TYPE_STRING);
  44. void Stop(bool graceful = true);
  45. bool IsRunning();
  46. private:
  47. pid_t pid_;
  48. std::thread thread_;
  49. int fd_;
  50. std::string conf_file_;
  51. TempFile conf_temp_file_;
  52. enum CREATE_MODE mode_;
  53. };
  54. struct ServerRequestContext {
  55. std::string domain;
  56. dns_type qtype;
  57. int qclass;
  58. struct sockaddr_storage *from;
  59. socklen_t fromlen;
  60. struct dns_packet *packet;
  61. uint8_t *request_data;
  62. int request_data_len;
  63. uint8_t *response_data;
  64. struct dns_packet *response_packet;
  65. int response_data_max_len;
  66. int response_data_len;
  67. };
  68. typedef enum {
  69. SERVER_REQUEST_OK = 0,
  70. SERVER_REQUEST_ERROR,
  71. SERVER_REQUEST_SOA,
  72. } ServerRequestResult;
  73. using ServerRequest = std::function<ServerRequestResult(struct ServerRequestContext *request)>;
  74. class MockServer
  75. {
  76. public:
  77. MockServer();
  78. virtual ~MockServer();
  79. bool Start(const std::string &url, ServerRequest callback);
  80. void Stop();
  81. bool IsRunning();
  82. static bool AddIP(struct ServerRequestContext *request, const std::string &domain, const std::string &ip,
  83. int ttl = 60);
  84. private:
  85. void Run();
  86. static bool GetAddr(const std::string &host, const std::string port, int type, int protocol,
  87. struct sockaddr_storage *addr, socklen_t *addrlen);
  88. int fd_;
  89. std::thread thread_;
  90. bool run_;
  91. ServerRequest callback_;
  92. };
  93. } // namespace smartdns
  94. #endif // _SMARTDNS_SERVER_