unity_plugin.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2012 Martin Willi
  3. * Copyright (C) 2012 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "unity_plugin.h"
  16. #include "unity_handler.h"
  17. #include "unity_narrow.h"
  18. #include "unity_provider.h"
  19. #include <daemon.h>
  20. typedef struct private_unity_plugin_t private_unity_plugin_t;
  21. /**
  22. * private data of unity_plugin
  23. */
  24. struct private_unity_plugin_t {
  25. /**
  26. * public functions
  27. */
  28. unity_plugin_t public;
  29. /**
  30. * Handler for UNITY configuration attributes
  31. */
  32. unity_handler_t *handler;
  33. /**
  34. * Responder Unity configuration attribute provider
  35. */
  36. unity_provider_t *provider;
  37. /**
  38. * Traffic selector narrower, for Unity Split-Includes
  39. */
  40. unity_narrow_t *narrower;
  41. };
  42. METHOD(plugin_t, get_name, char*,
  43. private_unity_plugin_t *this)
  44. {
  45. return "unity";
  46. }
  47. /**
  48. * Register listener
  49. */
  50. static bool plugin_cb(private_unity_plugin_t *this,
  51. plugin_feature_t *feature, bool reg, void *cb_data)
  52. {
  53. if (reg)
  54. {
  55. charon->attributes->add_handler(charon->attributes,
  56. &this->handler->handler);
  57. charon->attributes->add_provider(charon->attributes,
  58. &this->provider->provider);
  59. charon->bus->add_listener(charon->bus, &this->narrower->listener);
  60. }
  61. else
  62. {
  63. charon->bus->remove_listener(charon->bus, &this->narrower->listener);
  64. charon->attributes->remove_handler(charon->attributes,
  65. &this->handler->handler);
  66. charon->attributes->remove_provider(charon->attributes,
  67. &this->provider->provider);
  68. }
  69. return TRUE;
  70. }
  71. METHOD(plugin_t, get_features, int,
  72. private_unity_plugin_t *this, plugin_feature_t *features[])
  73. {
  74. static plugin_feature_t f[] = {
  75. PLUGIN_CALLBACK((plugin_feature_callback_t)plugin_cb, NULL),
  76. PLUGIN_PROVIDE(CUSTOM, "unity"),
  77. };
  78. *features = f;
  79. return countof(f);
  80. }
  81. METHOD(plugin_t, destroy, void,
  82. private_unity_plugin_t *this)
  83. {
  84. this->narrower->destroy(this->narrower);
  85. this->handler->destroy(this->handler);
  86. this->provider->destroy(this->provider);
  87. free(this);
  88. }
  89. /*
  90. * see header file
  91. */
  92. plugin_t *unity_plugin_create()
  93. {
  94. private_unity_plugin_t *this;
  95. INIT(this,
  96. .public = {
  97. .plugin = {
  98. .get_name = _get_name,
  99. .get_features = _get_features,
  100. .destroy = _destroy,
  101. },
  102. },
  103. .handler = unity_handler_create(),
  104. .provider = unity_provider_create(),
  105. );
  106. this->narrower = unity_narrow_create(this->handler);
  107. return &this->public.plugin;
  108. }