vici_dispatcher.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2013 Martin Willi
  3. * Copyright (C) 2013 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. /*
  16. * Copyright (C) 2014 Timo Teräs <timo.teras@iki.fi>
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy
  19. * of this software and associated documentation files (the "Software"), to deal
  20. * in the Software without restriction, including without limitation the rights
  21. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. * copies of the Software, and to permit persons to whom the Software is
  23. * furnished to do so, subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in
  26. * all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  34. * THE SOFTWARE.
  35. */
  36. /**
  37. * @defgroup vici_dispatcher vici_dispatcher
  38. * @{ @ingroup vici
  39. */
  40. #ifndef VICI_DISPATCHER_H_
  41. #define VICI_DISPATCHER_H_
  42. #include "vici_message.h"
  43. typedef struct vici_dispatcher_t vici_dispatcher_t;
  44. typedef enum vici_operation_t vici_operation_t;
  45. /**
  46. * Default socket URI of vici service
  47. */
  48. #ifdef WIN32
  49. # define VICI_DEFAULT_URI "tcp://127.0.0.1:4502"
  50. #else
  51. # define VICI_DEFAULT_URI "unix://" IPSEC_PIDDIR "/charon.vici"
  52. #endif
  53. /**
  54. * Kind of vici operation
  55. */
  56. enum vici_operation_t {
  57. /** a named request message */
  58. VICI_CMD_REQUEST,
  59. /** an unnamed response message to a request */
  60. VICI_CMD_RESPONSE,
  61. /** unnamed response if requested command is unknown */
  62. VICI_CMD_UNKNOWN,
  63. /** a named event registration request */
  64. VICI_EVENT_REGISTER,
  65. /** a named event unregistration request */
  66. VICI_EVENT_UNREGISTER,
  67. /** unnamed response for successful event (un-)registration */
  68. VICI_EVENT_CONFIRM,
  69. /** unnamed response if event (un-)registration failed */
  70. VICI_EVENT_UNKNOWN,
  71. /** a named event message */
  72. VICI_EVENT,
  73. };
  74. /**
  75. * Vici command callback function
  76. *
  77. * @param user user data, as supplied during registration
  78. * @param name name of the command it has been registered under
  79. * @param id client connection identifier
  80. * @param request request message data
  81. * @return response message
  82. */
  83. typedef vici_message_t* (*vici_command_cb_t)(void *user, char *name, u_int id,
  84. vici_message_t *request);
  85. /**
  86. * Vici command dispatcher.
  87. */
  88. struct vici_dispatcher_t {
  89. /**
  90. * Register/Unregister a callback invoked for a specific command request.
  91. *
  92. * @param name name of the command
  93. * @param cb callback function to register, NULL to unregister
  94. * @param user user data to pass to callback
  95. */
  96. void (*manage_command)(vici_dispatcher_t *this, char *name,
  97. vici_command_cb_t cb, void *user);
  98. /**
  99. * Register/Unregister an event type to send.
  100. *
  101. * The dispatcher internally manages event subscriptions. Clients registered
  102. * for an event will receive such messages when the event is raised.
  103. *
  104. * @param name event name to manager
  105. * @param reg TRUE to register, FALSE to unregister
  106. */
  107. void (*manage_event)(vici_dispatcher_t *this, char *name, bool reg);
  108. /**
  109. * Check if an event has listeners.
  110. *
  111. * This can be used to check if a vici message needs to be generated or not,
  112. * as in some cases the generation can be a heavy operation.
  113. *
  114. * @param name event name to check
  115. * @return TRUE if event has listeners
  116. */
  117. bool (*has_event_listeners)(vici_dispatcher_t *this, char *name);
  118. /**
  119. * Raise an event to a specific or all clients registered to that event.
  120. *
  121. * @param name event name to raise
  122. * @param id client connection ID, 0 for all
  123. * @param message event message to send, gets destroyed
  124. */
  125. void (*raise_event)(vici_dispatcher_t *this, char *name, u_int id,
  126. vici_message_t *message);
  127. /**
  128. * Destroy a vici_dispatcher_t.
  129. */
  130. void (*destroy)(vici_dispatcher_t *this);
  131. };
  132. /**
  133. * Create a vici_dispatcher instance.
  134. *
  135. * @param uri uri for listening stream service
  136. * @return dispatcher instance
  137. */
  138. vici_dispatcher_t *vici_dispatcher_create(char *uri);
  139. #endif /** VICI_DISPATCHER_H_ @}*/