Iface.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Iface_H
  16. #define Iface_H
  17. #include <stdint.h>
  18. #include "rust/cjdns_sys/RTypes.h"
  19. #include "wire/Message.h"
  20. #include "util/Defined.h"
  21. #include "util/Identity.h"
  22. #include "util/Linker.h"
  23. Linker_require("interface/Iface.c")
  24. struct Iface;
  25. /**
  26. * @param thisInterface the interface which contains the sendMessage function pointer.
  27. * @param message the message
  28. */
  29. typedef struct RTypes_Error_t* (* Iface_Callback)(struct Message* message, struct Iface* thisInterface);
  30. #define Iface_DEFUN __attribute__ ((warn_unused_result)) struct RTypes_Error_t*
  31. struct Iface
  32. {
  33. /** Send a message through this interface. */
  34. Iface_Callback send;
  35. #ifdef PARANOIA
  36. /** This is for checking currentMsg Iface_next() has not been called incorrectly. */
  37. struct Message* currentMsg;
  38. #endif
  39. /** Interface to which this one is connected (if connected) */
  40. struct Iface* connectedIf;
  41. Identity
  42. } /* Iface_t defined in RffiPrefix.h */;
  43. // This needs to be in a C file in order to be accessible from Rust
  44. Iface_DEFUN Iface_incomingFromRust(struct Message* message, struct Iface* thisInterface);
  45. void Iface_setIdentity(struct Iface* iface);
  46. void Iface_checkIdentity(struct Iface* iface);
  47. /**
  48. * Send a message to an Iface.
  49. * Whatever interface has been *plumbed* to this interface using Iface_plumb() will receive
  50. * the message. If you are in an Iface_Callback function, you must not use this function to
  51. * forward the message which you have received as your input, to do so will cause a runtime
  52. * assertion error, in order to forward the message which you received, you must use Iface_next()
  53. * and it must be a tail-call.
  54. */
  55. static inline struct RTypes_Error_t* Iface_send(struct Iface* iface, struct Message* msg)
  56. {
  57. Iface_checkIdentity(iface);
  58. struct Iface* conn = iface->connectedIf;
  59. Iface_checkIdentity(conn);
  60. #ifdef PARANOIA
  61. Assert_true(conn);
  62. Assert_true(conn->send);
  63. Assert_true(msg);
  64. struct Message* currentMsg = conn->currentMsg;
  65. msg->currentIface = conn;
  66. conn->currentMsg = msg;
  67. #endif
  68. struct RTypes_Error_t* ret = conn->send(msg, conn);
  69. #ifdef PARANOIA
  70. msg->currentIface = NULL;
  71. conn->currentMsg = currentMsg;
  72. #endif
  73. return ret;
  74. }
  75. /**
  76. * Forward a message from inside of an Iface_Callback function.
  77. */
  78. static inline Iface_DEFUN Iface_next(struct Iface* iface, struct Message* msg)
  79. {
  80. #ifdef PARANOIA
  81. struct Iface* conn = iface->connectedIf;
  82. struct Message* currentMsg = conn->currentMsg;
  83. Assert_true(msg->currentIface);
  84. Assert_true(msg->currentIface->currentMsg == msg);
  85. msg->currentIface->currentMsg = NULL;
  86. #endif
  87. #ifdef PARANOIA
  88. // done inside of Iface_send()
  89. msg->currentIface = NULL;
  90. conn->currentMsg = NULL;
  91. #endif
  92. struct RTypes_Error_t* ret = Iface_send(iface, msg);
  93. #ifdef PARANOIA
  94. conn->currentMsg = currentMsg;
  95. #endif
  96. return ret;
  97. }
  98. /**
  99. * Call a function which might use Iface_next()
  100. * This macro will call a function, passing it a message and other arguments, if the function
  101. * you are calling might use Iface_next(), you must call it with this macro instead of calling
  102. * it directly.
  103. * If you are calling from a Iface_Callback function, you must not use this function to call
  104. * the next function with the message passed to you. In that case just call the function directly.
  105. *
  106. * Why is there code using this weird function rather than simply calling Iface_send() ?
  107. * Iface_send() only works when you send to *your* iface which is plumbed to the iface that you want
  108. * the data to go to, this macro will be used when you want to bypass the need to create a local
  109. * iface and plumb it to the remote one just in order to send one message.
  110. */
  111. #ifdef PARANOIA
  112. #define Iface_CALL(func, msg, ...) \
  113. (__extension__ ({ \
  114. Assert_true(!msg->currentIface); \
  115. struct Iface Iface_y = { .currentMsg = msg }; \
  116. msg->currentIface = &Iface_y; \
  117. struct RTypes_Error_t* ret = func(msg, __VA_ARGS__); \
  118. msg->currentIface = NULL; \
  119. ret; \
  120. }))
  121. // CHECKFILES_IGNORE missing ;
  122. #else
  123. #define Iface_CALL(func, msg, ...) func(msg, __VA_ARGS__)
  124. #endif
  125. static inline void Iface_plumb(struct Iface* a, struct Iface* b)
  126. {
  127. Assert_true(!a->connectedIf);
  128. Assert_true(!b->connectedIf);
  129. Iface_setIdentity(a);
  130. Iface_setIdentity(b);
  131. a->connectedIf = b;
  132. b->connectedIf = a;
  133. }
  134. static inline void Iface_unplumb(struct Iface* a, struct Iface* b)
  135. {
  136. Assert_true(a->connectedIf == b);
  137. Assert_true(b->connectedIf == a);
  138. a->connectedIf = NULL;
  139. b->connectedIf = NULL;
  140. }
  141. #endif