arpprobe_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file arpprobe_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <misc/debug.h>
  34. #include <base/DebugObject.h>
  35. #include <base/BLog.h>
  36. #include <system/BReactor.h>
  37. #include <system/BSignal.h>
  38. #include <system/BTime.h>
  39. #include <system/BNetwork.h>
  40. #include <arpprobe/BArpProbe.h>
  41. BReactor reactor;
  42. BArpProbe arpprobe;
  43. static void signal_handler (void *user);
  44. static void arpprobe_handler (void *unused, int event);
  45. int main (int argc, char **argv)
  46. {
  47. if (argc <= 0) {
  48. return 1;
  49. }
  50. if (argc != 3) {
  51. printf("Usage: %s <interface> <addr>\n", argv[0]);
  52. goto fail0;
  53. }
  54. char *ifname = argv[1];
  55. uint32_t addr = inet_addr(argv[2]);
  56. BTime_Init();
  57. BLog_InitStdout();
  58. if (!BNetwork_GlobalInit()) {
  59. DEBUG("BNetwork_GlobalInit failed");
  60. goto fail1;
  61. }
  62. if (!BReactor_Init(&reactor)) {
  63. DEBUG("BReactor_Init failed");
  64. goto fail1;
  65. }
  66. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  67. DEBUG("BSignal_Init failed");
  68. goto fail2;
  69. }
  70. if (!BArpProbe_Init(&arpprobe, ifname, addr, &reactor, NULL, arpprobe_handler)) {
  71. DEBUG("BArpProbe_Init failed");
  72. goto fail3;
  73. }
  74. BReactor_Exec(&reactor);
  75. BArpProbe_Free(&arpprobe);
  76. fail3:
  77. BSignal_Finish();
  78. fail2:
  79. BReactor_Free(&reactor);
  80. fail1:
  81. BLog_Free();
  82. fail0:
  83. DebugObjectGlobal_Finish();
  84. return 1;
  85. }
  86. void signal_handler (void *user)
  87. {
  88. DEBUG("termination requested");
  89. BReactor_Quit(&reactor, 0);
  90. }
  91. void arpprobe_handler (void *unused, int event)
  92. {
  93. switch (event) {
  94. case BARPPROBE_EVENT_EXIST: {
  95. printf("ARPPROBE: exist\n");
  96. } break;
  97. case BARPPROBE_EVENT_NOEXIST: {
  98. printf("ARPPROBE: noexist\n");
  99. } break;
  100. case BARPPROBE_EVENT_ERROR: {
  101. printf("ARPPROBE: error\n");
  102. // exit reactor
  103. BReactor_Quit(&reactor, 0);
  104. } break;
  105. default:
  106. ASSERT(0);
  107. }
  108. }