dhcpclient_test.c 3.5 KB

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