Linux HLSW LAN Master
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

  1. /***************************************************************************
  2. * Copyright (C) 03/2005 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program 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 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program 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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <dlfcn.h>
  24. #include "plugin.h"
  25. #include "configfile.h"
  26. #include "logging.h"
  27. #include "netpkt.h"
  28. #include "list.h"
  29. #include "event.h"
  30. #define BUFSIZE 512
  31. static LIST_HEAD(plugin_list);
  32. static int plugin_load(const char *filename, void *privdata)
  33. {
  34. char *plugin_dir = (char *)privdata;
  35. char *fullname = malloc(BUFSIZE);
  36. if (fullname == NULL) {
  37. log_print(LOG_ERROR, "plugin_load: out of memory");
  38. return -1;
  39. }
  40. int len = snprintf(fullname, BUFSIZE, "%s/%s", plugin_dir, filename);
  41. if (len < 0 || len >= BUFSIZE) {
  42. log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
  43. free(fullname);
  44. return -1;
  45. }
  46. dlerror();
  47. void *dlref = dlopen(fullname, RTLD_NOW);
  48. if (dlref == NULL) {
  49. log_print(LOG_WARN, "plugin_load: %s", dlerror());
  50. free(fullname);
  51. return -1;
  52. }
  53. struct hlswmaster_plugin *plugin = dlsym(dlref, "plugin");
  54. if (plugin == NULL) {
  55. log_print(LOG_WARN, "plugin_load: invalid plugin '%s'", fullname);
  56. dlclose(dlref);
  57. free(fullname);
  58. return -1;
  59. }
  60. log_print(LOG_INFO, "loading plugin '%s'", plugin->name);
  61. if (plugin->init == NULL || (plugin->init() == 0)) {
  62. list_add_tail(&plugin->list, &plugin_list);
  63. free(fullname);
  64. return 0;
  65. }
  66. log_print(LOG_WARN, "plugin_load: failed to load '%s'", plugin->name);
  67. dlclose(dlref);
  68. free(fullname);
  69. return -1;
  70. }
  71. int plugins_scan(void)
  72. {
  73. struct hlswmaster_plugin *plugin;
  74. list_for_each_entry(plugin, &plugin_list, list)
  75. if (plugin->scan && !plugin->scan())
  76. log_print(LOG_WARN, "plugin %s: scan error", plugin->name);
  77. return 0;
  78. }
  79. int plugins_parse(struct net_pkt *pkt)
  80. {
  81. int retval;
  82. struct hlswmaster_plugin *plugin;
  83. list_for_each_entry(plugin, &plugin_list, list)
  84. if (plugin->parse && (retval = plugin->parse(pkt)))
  85. return retval;
  86. return PARSE_REJECT;
  87. }
  88. int plugin_init(void)
  89. {
  90. char *dir = config_get_string("global", "plugin_dir", ".");
  91. int cnt = config_get_strings("global", "plugin", plugin_load, (void *)dir);
  92. log_print(LOG_INFO, "%d plugins loaded", cnt);
  93. return 0;
  94. }