dhcpclient_test.c 3.3 KB

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