server.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <functional>
  22. #include <string>
  23. #include <sys/socket.h>
  24. #include <thread>
  25. #include <unistd.h>
  26. namespace smartdns
  27. {
  28. class Server
  29. {
  30. public:
  31. enum CONF_TYPE {
  32. CONF_TYPE_STRING,
  33. CONF_TYPE_FILE,
  34. };
  35. Server();
  36. virtual ~Server();
  37. bool Start(const std::string &conf, enum CONF_TYPE type = CONF_TYPE_STRING);
  38. void Stop(bool graceful = true);
  39. bool IsRunning();
  40. private:
  41. pid_t pid_;
  42. int fd_;
  43. std::string conf_file_;
  44. bool clean_conf_file_{false};
  45. };
  46. struct ServerRequestContext {
  47. std::string domain;
  48. dns_type qtype;
  49. int qclass;
  50. struct sockaddr_storage *from;
  51. socklen_t fromlen;
  52. struct dns_packet *packet;
  53. uint8_t *request_data;
  54. int request_data_len;
  55. uint8_t *response_data;
  56. struct dns_packet *response_packet;
  57. int response_data_max_len;
  58. int response_data_len;
  59. };
  60. typedef enum {
  61. SERVER_REQUEST_OK = 0,
  62. SERVER_REQUEST_ERROR,
  63. SERVER_REQUEST_SOA,
  64. } ServerRequestResult;
  65. using ServerRequest = std::function<ServerRequestResult(struct ServerRequestContext *request)>;
  66. class MockServer
  67. {
  68. public:
  69. MockServer();
  70. virtual ~MockServer();
  71. bool Start(const std::string &url, ServerRequest callback);
  72. void Stop();
  73. bool IsRunning();
  74. static bool AddIP(struct ServerRequestContext *request, const std::string &domain, const std::string &ip,
  75. int ttl = 60);
  76. private:
  77. void Run();
  78. static bool GetAddr(const std::string &host, const std::string port, int type, int protocol,
  79. struct sockaddr_storage *addr, socklen_t *addrlen);
  80. int fd_;
  81. std::thread thread_;
  82. bool run_;
  83. ServerRequest callback_;
  84. };
  85. } // namespace smartdns
  86. #endif // _SMARTDNS_SERVER_