dhcpclient_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @file dhcpclient_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 <misc/debug.h>
  31. #include <base/DebugObject.h>
  32. #include <base/BLog.h>
  33. #include <system/BReactor.h>
  34. #include <system/BSignal.h>
  35. #include <system/BTime.h>
  36. #include <system/BNetwork.h>
  37. #include <dhcpclient/BDHCPClient.h>
  38. BReactor reactor;
  39. BRandom2 random2;
  40. BDHCPClient dhcp;
  41. static void signal_handler (void *user);
  42. static void dhcp_handler (void *unused, int event);
  43. int main (int argc, char **argv)
  44. {
  45. if (argc <= 0) {
  46. return 1;
  47. }
  48. if (argc != 2) {
  49. printf("Usage: %s <interface>\n", argv[0]);
  50. goto fail0;
  51. }
  52. char *ifname = argv[1];
  53. BTime_Init();
  54. BLog_InitStdout();
  55. if (!BNetwork_GlobalInit()) {
  56. DEBUG("BNetwork_GlobalInit failed");
  57. goto fail1;
  58. }
  59. if (!BReactor_Init(&reactor)) {
  60. DEBUG("BReactor_Init failed");
  61. goto fail1;
  62. }
  63. if (!BRandom2_Init(&random2, 0)) {
  64. DEBUG("BRandom2_Init failed");
  65. goto fail1a;
  66. }
  67. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  68. DEBUG("BSignal_Init failed");
  69. goto fail2;
  70. }
  71. struct BDHCPClient_opts opts = {};
  72. if (!BDHCPClient_Init(&dhcp, ifname, opts, &reactor, &random2, dhcp_handler, NULL)) {
  73. DEBUG("BDHCPClient_Init failed");
  74. goto fail3;
  75. }
  76. BReactor_Exec(&reactor);
  77. BDHCPClient_Free(&dhcp);
  78. fail3:
  79. BSignal_Finish();
  80. fail2:
  81. BRandom2_Free(&random2);
  82. fail1a:
  83. BReactor_Free(&reactor);
  84. fail1:
  85. BLog_Free();
  86. fail0:
  87. DebugObjectGlobal_Finish();
  88. return 1;
  89. }
  90. void signal_handler (void *user)
  91. {
  92. DEBUG("termination requested");
  93. BReactor_Quit(&reactor, 0);
  94. }
  95. void dhcp_handler (void *unused, int event)
  96. {
  97. switch (event) {
  98. case BDHCPCLIENT_EVENT_UP: {
  99. printf("DHCP: up");
  100. uint32_t ip;
  101. uint8_t *ipb = (void *)&ip;
  102. BDHCPClient_GetClientIP(&dhcp, &ip);
  103. printf(" IP=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
  104. BDHCPClient_GetClientMask(&dhcp, &ip);
  105. printf(" Mask=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
  106. if (BDHCPClient_GetRouter(&dhcp, &ip)) {
  107. printf(" Router=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
  108. }
  109. uint32_t dns[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  110. int num = BDHCPClient_GetDNS(&dhcp, dns, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  111. for (int i = 0; i < num; i++) {
  112. ip=dns[i];
  113. printf(" DNS=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
  114. }
  115. printf("\n");
  116. } break;
  117. case BDHCPCLIENT_EVENT_DOWN: {
  118. printf("DHCP: down\n");
  119. } break;
  120. case BDHCPCLIENT_EVENT_ERROR: {
  121. printf("DHCP: error\n");
  122. // exit reactor
  123. BReactor_Quit(&reactor, 0);
  124. } break;
  125. default:
  126. ASSERT(0);
  127. }
  128. }