dhcpclient_test.c 3.7 KB

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