arpprobe_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file arpprobe_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <misc/debug.h>
  27. #include <base/DebugObject.h>
  28. #include <base/BLog.h>
  29. #include <system/BReactor.h>
  30. #include <system/BSignal.h>
  31. #include <system/BTime.h>
  32. #include <system/BNetwork.h>
  33. #include <arpprobe/BArpProbe.h>
  34. BReactor reactor;
  35. BArpProbe arpprobe;
  36. static void signal_handler (void *user);
  37. static void arpprobe_handler (void *unused, int event);
  38. int main (int argc, char **argv)
  39. {
  40. if (argc <= 0) {
  41. return 1;
  42. }
  43. if (argc != 3) {
  44. printf("Usage: %s <interface> <addr>\n", argv[0]);
  45. goto fail0;
  46. }
  47. char *ifname = argv[1];
  48. uint32_t addr = inet_addr(argv[2]);
  49. BTime_Init();
  50. BLog_InitStdout();
  51. if (!BNetwork_GlobalInit()) {
  52. DEBUG("BNetwork_GlobalInit failed");
  53. goto fail1;
  54. }
  55. if (!BReactor_Init(&reactor)) {
  56. DEBUG("BReactor_Init failed");
  57. goto fail1;
  58. }
  59. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  60. DEBUG("BSignal_Init failed");
  61. goto fail2;
  62. }
  63. if (!BArpProbe_Init(&arpprobe, ifname, addr, &reactor, NULL, arpprobe_handler)) {
  64. DEBUG("BArpProbe_Init failed");
  65. goto fail3;
  66. }
  67. BReactor_Exec(&reactor);
  68. BArpProbe_Free(&arpprobe);
  69. fail3:
  70. BSignal_Finish();
  71. fail2:
  72. BReactor_Free(&reactor);
  73. fail1:
  74. BLog_Free();
  75. fail0:
  76. DebugObjectGlobal_Finish();
  77. return 1;
  78. }
  79. void signal_handler (void *user)
  80. {
  81. DEBUG("termination requested");
  82. BReactor_Quit(&reactor, 0);
  83. }
  84. void arpprobe_handler (void *unused, int event)
  85. {
  86. switch (event) {
  87. case BARPPROBE_EVENT_EXIST: {
  88. printf("ARPPROBE: exist\n");
  89. } break;
  90. case BARPPROBE_EVENT_NOEXIST: {
  91. printf("ARPPROBE: noexist\n");
  92. } break;
  93. case BARPPROBE_EVENT_ERROR: {
  94. printf("ARPPROBE: error\n");
  95. // exit reactor
  96. BReactor_Quit(&reactor, 0);
  97. } break;
  98. default:
  99. ASSERT(0);
  100. }
  101. }