1
0

AddrIfaceAdapter.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/addressable/AddrIfaceAdapter.h"
  16. #include "memory/Allocator.h"
  17. #include "util/platform/Sockaddr.h"
  18. #include "util/Assert.h"
  19. #include "util/Identity.h"
  20. #include "wire/Message.h"
  21. /**
  22. * Convert a normal Interface to an AddrInterface, all incoming messages
  23. * will have the same address (Sockaddr_LOOPBACK).
  24. */
  25. struct AddrIfaceAdapter_pvt
  26. {
  27. struct AddrIfaceAdapter pub;
  28. Identity
  29. };
  30. static Iface_DEFUN incomingFromAddrIf(struct Message* msg, struct Iface* addrIf)
  31. {
  32. struct AddrIfaceAdapter_pvt* ctx =
  33. Identity_containerOf(addrIf, struct AddrIfaceAdapter_pvt, pub.generic.iface);
  34. Message_shift(msg, -(ctx->pub.generic.addr->addrLen), NULL);
  35. return Iface_next(&ctx->pub.inputIf, msg);
  36. }
  37. static Iface_DEFUN incomingFromInputIf(struct Message* msg, struct Iface* inputIf)
  38. {
  39. struct AddrIfaceAdapter_pvt* ctx =
  40. Identity_containerOf(inputIf, struct AddrIfaceAdapter_pvt, pub.inputIf);
  41. Message_push(msg, ctx->pub.generic.addr, ctx->pub.generic.addr->addrLen, NULL);
  42. return Iface_next(&ctx->pub.generic.iface, msg);
  43. }
  44. struct AddrIfaceAdapter* AddrIfaceAdapter_new(struct Allocator* alloc)
  45. {
  46. struct AddrIfaceAdapter_pvt* context =
  47. Allocator_calloc(alloc, sizeof(struct AddrIfaceAdapter_pvt), 1);
  48. context->pub.generic.addr = Sockaddr_clone(Sockaddr_LOOPBACK, alloc);
  49. context->pub.generic.alloc = alloc;
  50. context->pub.generic.iface.send = incomingFromAddrIf;
  51. context->pub.inputIf.send = incomingFromInputIf;
  52. Identity_set(context);
  53. return &context->pub;
  54. }