dhcpclient_test.c 4.4 KB

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