TUNTools.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/tuntap/test/TUNTools.h"
  16. #include "interface/addressable/UDPAddrInterface.h"
  17. #include "exception/Jmp.h"
  18. #include "util/events/Timeout.h"
  19. #ifdef win32
  20. #include <windows.h>
  21. #define sleep(x) Sleep(1000*x)
  22. #else
  23. #include <unistd.h>
  24. #endif
  25. static struct AddrInterface* setupUDP2(struct EventBase* base,
  26. struct Sockaddr* bindAddr,
  27. struct Allocator* allocator,
  28. struct Log* logger)
  29. {
  30. struct Jmp jmp;
  31. Jmp_try(jmp) {
  32. return UDPAddrInterface_new(base, bindAddr, allocator, &jmp.handler, logger);
  33. } Jmp_catch {
  34. sleep(1);
  35. return NULL;
  36. }
  37. }
  38. struct AddrInterface* TUNTools_setupUDP(struct EventBase* base,
  39. struct Sockaddr* bindAddr,
  40. struct Allocator* allocator,
  41. struct Log* logger)
  42. {
  43. // Mac OSX and BSD do not set up their TUN devices synchronously.
  44. // We'll just keep on trying until this works.
  45. struct AddrInterface* udp = NULL;
  46. for (int i = 0; i < 20; i++) {
  47. if ((udp = setupUDP2(base, bindAddr, allocator, logger))) {
  48. break;
  49. }
  50. }
  51. Assert_true(udp);
  52. return udp;
  53. }
  54. struct TUNTools_pvt
  55. {
  56. struct AddrInterface* iface;
  57. struct Sockaddr* dest;
  58. Identity
  59. };
  60. static void sendHello(void* vctx)
  61. {
  62. struct TUNTools_pvt* ctx = Identity_check((struct TUNTools_pvt*) vctx);
  63. struct Message* msg;
  64. Message_STACK(msg, 0, 64);
  65. Message_push(msg, "Hello World", 12, NULL);
  66. Message_push(msg, ctx->dest, ctx->dest->addrLen, NULL);
  67. Interface_sendMessage(&ctx->iface->generic, msg);
  68. }
  69. struct Timeout* TUNTools_sendHelloWorld(struct AddrInterface* iface,
  70. struct Sockaddr* dest,
  71. struct EventBase* base,
  72. struct Allocator* alloc)
  73. {
  74. struct TUNTools_pvt* ctx = Allocator_clone(alloc, (&(struct TUNTools_pvt) {
  75. .dest = dest,
  76. .iface = iface
  77. }));
  78. Identity_set(ctx);
  79. return Timeout_setInterval(sendHello, ctx, 1000, base, alloc);
  80. }