dns_plugin.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include "dns_plugin.h"
  19. #include "include/conf.h"
  20. #include "include/hashtable.h"
  21. #include "include/list.h"
  22. #include "util.h"
  23. #include <dlfcn.h>
  24. #include <limits.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "tlog.h"
  29. struct dns_plugin_ops {
  30. struct list_head list;
  31. struct smartdns_operations ops;
  32. };
  33. #define DNS_PLUGIN_MAX_ARGS 32
  34. struct dns_plugin {
  35. struct hlist_node node;
  36. char file[PATH_MAX];
  37. char args[PATH_MAX];
  38. int argc;
  39. char *argv[DNS_PLUGIN_MAX_ARGS];
  40. void *handle;
  41. dns_plugin_init_func init_func;
  42. dns_plugin_exit_func exit_func;
  43. };
  44. struct dns_plugins {
  45. struct list_head list;
  46. DECLARE_HASHTABLE(plugin, 4);
  47. };
  48. static struct dns_plugins plugins;
  49. static int is_plugin_init;
  50. int smartdns_plugin_func_server_recv(struct dns_packet *packet, unsigned char *inpacket, int inpacket_len,
  51. struct sockaddr_storage *local, socklen_t local_len, struct sockaddr_storage *from,
  52. socklen_t from_len)
  53. {
  54. struct dns_plugin_ops *chain = NULL;
  55. int ret = 0;
  56. list_for_each_entry(chain, &plugins.list, list)
  57. {
  58. if (!chain->ops.server_recv) {
  59. continue;
  60. }
  61. ret = chain->ops.server_recv(packet, inpacket, inpacket_len, local, local_len, from, from_len);
  62. if (ret != 0) {
  63. return ret;
  64. }
  65. }
  66. return 0;
  67. }
  68. void smartdns_plugin_func_server_complete_request(struct dns_request *request)
  69. {
  70. struct dns_plugin_ops *chain = NULL;
  71. list_for_each_entry(chain, &plugins.list, list)
  72. {
  73. if (!chain->ops.server_query_complete) {
  74. continue;
  75. }
  76. chain->ops.server_query_complete(request);
  77. }
  78. return;
  79. }
  80. int smartdns_operations_register(struct smartdns_operations *operations)
  81. {
  82. struct dns_plugin_ops *chain = NULL;
  83. chain = (struct dns_plugin_ops *)malloc(sizeof(struct dns_plugin_ops));
  84. if (!chain) {
  85. return -1;
  86. }
  87. memcpy(&chain->ops, operations, sizeof(struct smartdns_operations));
  88. list_add_tail(&chain->list, &plugins.list);
  89. return 0;
  90. }
  91. int smartdns_operations_unregister(struct smartdns_operations *operations)
  92. {
  93. struct dns_plugin_ops *chain = NULL;
  94. struct dns_plugin_ops *tmp = NULL;
  95. list_for_each_entry_safe(chain, tmp, &plugins.list, list)
  96. {
  97. if (memcmp(&chain->ops, operations, sizeof(struct smartdns_operations)) == 0) {
  98. list_del(&chain->list);
  99. free(chain);
  100. return 0;
  101. }
  102. }
  103. return -1;
  104. }
  105. static struct dns_plugin *_dns_plugin_get(const char *plugin_file)
  106. {
  107. struct dns_plugin *plugin = NULL;
  108. unsigned int key = 0;
  109. key = hash_string(plugin_file);
  110. hash_for_each_possible(plugins.plugin, plugin, node, key)
  111. {
  112. if (strncmp(plugin->file, plugin_file, PATH_MAX - 1) == 0) {
  113. return plugin;
  114. }
  115. }
  116. return NULL;
  117. }
  118. static int _dns_plugin_load_library(struct dns_plugin *plugin)
  119. {
  120. void *handle = NULL;
  121. dns_plugin_init_func init_func = NULL;
  122. dns_plugin_exit_func exit_func = NULL;
  123. handle = dlopen(plugin->file, RTLD_LAZY | RTLD_LOCAL);
  124. if (!handle) {
  125. tlog(TLOG_ERROR, "load plugin %s failed: %s", plugin->file, dlerror());
  126. return -1;
  127. }
  128. init_func = (dns_plugin_init_func)dlsym(handle, DNS_PLUGIN_INIT_FUNC);
  129. if (!init_func) {
  130. tlog(TLOG_ERROR, "load plugin %s failed: %s", plugin->file, dlerror());
  131. goto errout;
  132. }
  133. exit_func = (dns_plugin_exit_func)dlsym(handle, DNS_PLUGIN_EXIT_FUNC);
  134. if (!exit_func) {
  135. tlog(TLOG_ERROR, "load plugin %s failed: %s", plugin->file, dlerror());
  136. goto errout;
  137. }
  138. conf_getopt_reset();
  139. int ret = init_func(plugin);
  140. conf_getopt_reset();
  141. if (ret != 0) {
  142. tlog(TLOG_ERROR, "init plugin %s failed", plugin->file);
  143. goto errout;
  144. }
  145. plugin->handle = handle;
  146. plugin->init_func = init_func;
  147. plugin->exit_func = exit_func;
  148. return 0;
  149. errout:
  150. if (handle) {
  151. dlclose(handle);
  152. }
  153. return -1;
  154. }
  155. static int _dns_plugin_unload_library(struct dns_plugin *plugin)
  156. {
  157. int ret = 0;
  158. if (plugin->exit_func) {
  159. ret = plugin->exit_func(plugin);
  160. if (ret != 0) {
  161. tlog(TLOG_ERROR, "exit plugin %s failed", plugin->file);
  162. }
  163. }
  164. if (plugin->handle) {
  165. dlclose(plugin->handle);
  166. plugin->handle = NULL;
  167. }
  168. return 0;
  169. }
  170. static struct dns_plugin *_dns_plugin_new(const char *plugin_file)
  171. {
  172. struct dns_plugin *plugin = NULL;
  173. plugin = _dns_plugin_get(plugin_file);
  174. if (plugin) {
  175. return NULL;
  176. }
  177. plugin = (struct dns_plugin *)malloc(sizeof(struct dns_plugin));
  178. if (!plugin) {
  179. return NULL;
  180. }
  181. memset(plugin, 0, sizeof(struct dns_plugin));
  182. strncpy(plugin->file, plugin_file, PATH_MAX - 1);
  183. return plugin;
  184. }
  185. static int _dns_plugin_remove(struct dns_plugin *plugin)
  186. {
  187. _dns_plugin_unload_library(plugin);
  188. hash_del(&plugin->node);
  189. free(plugin);
  190. return 0;
  191. }
  192. int dns_plugin_get_argc(struct dns_plugin *plugin)
  193. {
  194. return plugin->argc;
  195. }
  196. const char **dns_plugin_get_argv(struct dns_plugin *plugin)
  197. {
  198. return (const char **)plugin->argv;
  199. }
  200. int dns_plugin_add(const char *plugin_file, int argc, const char *args, int args_len)
  201. {
  202. struct dns_plugin *plugin = NULL;
  203. const char *plugin_args = NULL;
  204. plugin = _dns_plugin_new(plugin_file);
  205. if (!plugin) {
  206. tlog(TLOG_ERROR, "add plugin %s failed", plugin_file);
  207. return -1;
  208. }
  209. memcpy(plugin->args, args, PATH_MAX - 1);
  210. plugin->argc = argc;
  211. plugin_args = plugin->args;
  212. for (int i = 0; i < argc && i < DNS_PLUGIN_MAX_ARGS; i++) {
  213. plugin->argv[i] = (char *)plugin_args;
  214. plugin_args += strlen(plugin_args) + 1;
  215. }
  216. if (_dns_plugin_load_library(plugin) != 0) {
  217. goto errout;
  218. }
  219. hash_add(plugins.plugin, &plugin->node, hash_string(plugin_file));
  220. return 0;
  221. errout:
  222. if (plugin) {
  223. _dns_plugin_remove(plugin);
  224. }
  225. return -1;
  226. }
  227. int dns_plugin_remove(const char *plugin_file)
  228. {
  229. struct dns_plugin *plugin = NULL;
  230. plugin = _dns_plugin_get(plugin_file);
  231. if (plugin == NULL) {
  232. return 0;
  233. }
  234. return _dns_plugin_remove(plugin);
  235. }
  236. static int _dns_plugin_remove_all_ops(void)
  237. {
  238. struct dns_plugin_ops *chain = NULL;
  239. struct dns_plugin_ops *tmp = NULL;
  240. list_for_each_entry_safe(chain, tmp, &plugins.list, list)
  241. {
  242. list_del(&chain->list);
  243. free(chain);
  244. }
  245. return 0;
  246. }
  247. static int _dns_plugin_remove_all(void)
  248. {
  249. struct dns_plugin *plugin = NULL;
  250. struct hlist_node *tmp = NULL;
  251. unsigned int key = 0;
  252. hash_for_each_safe(plugins.plugin, key, tmp, plugin, node)
  253. {
  254. _dns_plugin_remove(plugin);
  255. }
  256. return -1;
  257. }
  258. int dns_server_plugin_init(void)
  259. {
  260. if (is_plugin_init == 1) {
  261. return 0;
  262. }
  263. hash_init(plugins.plugin);
  264. INIT_LIST_HEAD(&plugins.list);
  265. is_plugin_init = 1;
  266. return 0;
  267. }
  268. void dns_server_plugin_exit(void)
  269. {
  270. if (is_plugin_init == 0) {
  271. return;
  272. }
  273. _dns_plugin_remove_all_ops();
  274. _dns_plugin_remove_all();
  275. return;
  276. }
  277. void smartdns_plugin_log(smartdns_log_level level, const char *file, int line, const char *func, const char *msg)
  278. {
  279. tlog_ext((tlog_level)level, file, line, func, NULL, "%s", msg);
  280. }