UpperDistributor.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "interface/Iface.h"
  16. #include "memory/Allocator.h"
  17. #include "util/Identity.h"
  18. #include "net/UpperDistributor.h"
  19. #include "net/SessionManager.h"
  20. #include "net/EventEmitter.h"
  21. #include "wire/DataHeader.h"
  22. #include "wire/RouteHeader.h"
  23. struct UpperDistributor_pvt
  24. {
  25. struct UpperDistributor pub;
  26. struct Iface eventIf;
  27. struct Log* log;
  28. Identity
  29. };
  30. static Iface_DEFUN incomingFromEventIf(struct Message* msg, struct Iface* eventIf)
  31. {
  32. struct UpperDistributor_pvt* ud =
  33. Identity_containerOf(eventIf, struct UpperDistributor_pvt, eventIf);
  34. Assert_true(Message_pop32(msg, NULL) == PFChan_Pathfinder_SENDMSG);
  35. Message_pop32(msg, NULL);
  36. return Iface_next(&ud->pub.sessionManagerIf, msg);
  37. }
  38. static Iface_DEFUN incomingFromTunAdapterIf(struct Message* msg, struct Iface* tunAdapterIf)
  39. {
  40. struct UpperDistributor_pvt* ud =
  41. Identity_containerOf(tunAdapterIf, struct UpperDistributor_pvt, pub.tunAdapterIf);
  42. return Iface_next(&ud->pub.sessionManagerIf, msg);
  43. }
  44. static Iface_DEFUN incomingFromIpTunnelIf(struct Message* msg, struct Iface* ipTunnelIf)
  45. {
  46. struct UpperDistributor_pvt* ud =
  47. Identity_containerOf(ipTunnelIf, struct UpperDistributor_pvt, pub.ipTunnelIf);
  48. return Iface_next(&ud->pub.sessionManagerIf, msg);
  49. }
  50. static Iface_DEFUN incomingFromSessionManagerIf(struct Message* msg, struct Iface* sessionManagerIf)
  51. {
  52. struct UpperDistributor_pvt* ud =
  53. Identity_containerOf(sessionManagerIf, struct UpperDistributor_pvt, pub.sessionManagerIf);
  54. Assert_true(msg->length >= RouteHeader_SIZE + DataHeader_SIZE);
  55. struct RouteHeader* hdr = (struct RouteHeader*) msg->bytes;
  56. struct DataHeader* dh = (struct DataHeader*) &hdr[1];
  57. enum ContentType type = DataHeader_getContentType(dh);
  58. if (type <= ContentType_IP6_RAW) {
  59. return Iface_next(&ud->pub.tunAdapterIf, msg);
  60. }
  61. if (type == ContentType_CJDHT) {
  62. Message_push32(msg, 0xffffffff, NULL);
  63. Message_push32(msg, PFChan_Core_MSG, NULL);
  64. return Iface_next(&ud->eventIf, msg);
  65. }
  66. if (type == ContentType_IPTUN) {
  67. return Iface_next(&ud->pub.ipTunnelIf, msg);
  68. }
  69. Log_debug(ud->log, "DROP message with unknown type [%d]", type);
  70. return NULL;
  71. }
  72. struct UpperDistributor* UpperDistributor_new(struct Allocator* allocator,
  73. struct Log* log,
  74. struct EventEmitter* ee)
  75. {
  76. struct Allocator* alloc = Allocator_child(allocator);
  77. struct UpperDistributor_pvt* out =
  78. Allocator_calloc(alloc, sizeof(struct UpperDistributor_pvt), 1);
  79. out->eventIf.send = incomingFromEventIf;
  80. out->pub.tunAdapterIf.send = incomingFromTunAdapterIf;
  81. out->pub.ipTunnelIf.send = incomingFromIpTunnelIf;
  82. out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
  83. out->log = log;
  84. Identity_set(out);
  85. EventEmitter_regCore(ee, &out->eventIf, PFChan_Pathfinder_SENDMSG);
  86. return &out->pub;
  87. }