UDPAddrIface.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "exception/Er.h"
  16. #include "interface/Iface.h"
  17. #include "rust/cjdns_sys/Rffi.h"
  18. #include "util/events/UDPAddrIface.h"
  19. #include "memory/Allocator.h"
  20. #include "util/platform/Sockaddr.h"
  21. #include "util/Assert.h"
  22. #include "util/Identity.h"
  23. #include "wire/Message.h"
  24. #include "wire/Error.h"
  25. #include "util/Hex.h"
  26. struct UDPAddrIface_pvt {
  27. struct UDPAddrIface pub;
  28. Rffi_UDPIface* internal;
  29. Identity
  30. };
  31. int UDPAddrIface_setDSCP(struct UDPAddrIface* iface, uint8_t dscp)
  32. {
  33. struct UDPAddrIface_pvt* ifp = Identity_check((struct UDPAddrIface_pvt*)iface);
  34. return Rffi_udpIfaceSetDscp(ifp->internal->pvt, dscp);
  35. }
  36. int UDPAddrIface_getFd(struct UDPAddrIface* iface)
  37. {
  38. struct UDPAddrIface_pvt* ifp = Identity_check((struct UDPAddrIface_pvt*)iface);
  39. return Rffi_udpIfaceGetFd(ifp->internal->pvt);
  40. }
  41. int UDPAddrIface_setBroadcast(struct UDPAddrIface* iface, bool enable)
  42. {
  43. struct UDPAddrIface_pvt* ifp = Identity_check((struct UDPAddrIface_pvt*)iface);
  44. return (int) Rffi_udpIfaceSetBroadcast(ifp->internal->pvt, enable);
  45. }
  46. Er_DEFUN(struct UDPAddrIface* UDPAddrIface_new(EventBase_t* eventBase,
  47. struct Sockaddr* addr,
  48. struct Allocator* userAlloc,
  49. struct Log* logger))
  50. {
  51. Rffi_UDPIface* internal = NULL;
  52. const char* err = NULL;
  53. Rffi_udpIfaceNew(&internal, &err, addr, userAlloc);
  54. if (err != NULL) {
  55. Er_raise(userAlloc, "Failed to create UDP interface: %s", err);
  56. }
  57. struct UDPAddrIface_pvt* out =
  58. Allocator_calloc(userAlloc, sizeof(struct UDPAddrIface_pvt), 1);
  59. out->pub.generic.iface = internal->iface;
  60. out->pub.generic.addr = internal->local_addr;
  61. out->pub.generic.alloc = userAlloc;
  62. out->internal = internal;
  63. Identity_set(out);
  64. Er_ret(&out->pub);
  65. }