server.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2025 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 "smartdns/dns.h"
  21. #include "smartdns/fast_ping.h"
  22. #include "include/utils.h"
  23. #include <functional>
  24. #include <string>
  25. #include <sys/socket.h>
  26. #include <thread>
  27. #include <unistd.h>
  28. #include <vector>
  29. namespace smartdns
  30. {
  31. class Server
  32. {
  33. public:
  34. struct MockPingIP {
  35. PING_TYPE type;
  36. std::string host;
  37. int ttl;
  38. float time;
  39. };
  40. enum CONF_TYPE {
  41. CONF_TYPE_STRING,
  42. CONF_TYPE_FILE,
  43. };
  44. enum CREATE_MODE {
  45. CREATE_MODE_FORK,
  46. CREATE_MODE_THREAD,
  47. };
  48. Server();
  49. Server(enum CREATE_MODE mode);
  50. virtual ~Server();
  51. void MockPing(PING_TYPE type, const std::string &host, int ttl, float time);
  52. bool Start(const std::string &conf, enum CONF_TYPE type = CONF_TYPE_STRING);
  53. void Stop(bool graceful = true);
  54. bool IsRunning();
  55. private:
  56. static void StartPost(void *arg);
  57. pid_t pid_;
  58. std::thread thread_;
  59. int fd_;
  60. std::string conf_file_;
  61. TempFile conf_temp_file_;
  62. std::vector<MockPingIP> mock_ping_ips_;
  63. enum CREATE_MODE mode_;
  64. };
  65. struct ServerRequestContext {
  66. std::string domain;
  67. dns_type qtype;
  68. int qclass;
  69. struct sockaddr_storage *from;
  70. socklen_t fromlen;
  71. struct dns_packet *packet;
  72. uint8_t *request_data;
  73. int request_data_len;
  74. uint8_t *response_data;
  75. struct dns_packet *response_packet;
  76. int response_data_max_len;
  77. int response_data_len;
  78. };
  79. typedef enum {
  80. SERVER_REQUEST_OK = 0,
  81. SERVER_REQUEST_ERROR,
  82. SERVER_REQUEST_NO_RESPONSE,
  83. SERVER_REQUEST_SOA,
  84. } ServerRequestResult;
  85. using ServerRequest = std::function<ServerRequestResult(struct ServerRequestContext *request)>;
  86. class MockServer
  87. {
  88. public:
  89. MockServer();
  90. virtual ~MockServer();
  91. bool Start(const std::string &url, ServerRequest callback);
  92. void Stop();
  93. bool IsRunning();
  94. static bool AddIP(struct ServerRequestContext *request, const std::string &domain, const std::string &ip,
  95. int ttl = 60);
  96. private:
  97. void Run();
  98. static bool GetAddr(const std::string &host, const std::string port, int type, int protocol,
  99. struct sockaddr_storage *addr, socklen_t *addrlen);
  100. int fd_{0};
  101. std::thread thread_;
  102. bool run_{false};
  103. ServerRequest callback_{nullptr};
  104. };
  105. } // namespace smartdns
  106. #endif // _SMARTDNS_SERVER_