2
0

main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <time.h>
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <getopt.h>
  19. #include <resolv.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <arpa/inet.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/nameser.h>
  26. #include <udebug.h>
  27. #include <libubus.h>
  28. #include <libubox/uloop.h>
  29. #include "dns.h"
  30. #include "ubus.h"
  31. #include "util.h"
  32. #include "cache.h"
  33. #include "service.h"
  34. #include "announce.h"
  35. #include "interface.h"
  36. int cfg_proto = 0;
  37. int cfg_no_subnet = 0;
  38. static struct udebug ud;
  39. static struct udebug_buf udb;
  40. static bool udebug_enabled;
  41. static void
  42. umdns_udebug_vprintf(const char *format, va_list ap)
  43. {
  44. if (!udebug_enabled)
  45. return;
  46. udebug_entry_init(&udb);
  47. udebug_entry_vprintf(&udb, format, ap);
  48. udebug_entry_add(&udb);
  49. }
  50. void umdns_udebug_printf(const char *format, ...)
  51. {
  52. va_list ap;
  53. va_start(ap, format);
  54. umdns_udebug_vprintf(format, ap);
  55. va_end(ap);
  56. }
  57. void umdns_udebug_set_enabled(bool val)
  58. {
  59. static const struct udebug_buf_meta meta = {
  60. .name = "umdns_log",
  61. .format = UDEBUG_FORMAT_STRING,
  62. };
  63. if (udebug_enabled == val)
  64. return;
  65. udebug_enabled = val;
  66. if (!val) {
  67. udebug_buf_free(&udb);
  68. udebug_free(&ud);
  69. return;
  70. }
  71. udebug_init(&ud);
  72. udebug_auto_connect(&ud, NULL);
  73. udebug_buf_init(&udb, 1024, 64 * 1024);
  74. udebug_buf_add(&ud, &udb, &meta);
  75. }
  76. static void
  77. signal_shutdown(int signal)
  78. {
  79. uloop_end();
  80. }
  81. int
  82. main(int argc, char **argv)
  83. {
  84. int ch, ttl;
  85. uloop_init();
  86. while ((ch = getopt(argc, argv, "t:i:d46n")) != -1) {
  87. switch (ch) {
  88. case 't':
  89. ttl = atoi(optarg);
  90. if (ttl > 0)
  91. announce_ttl = ttl;
  92. else
  93. fprintf(stderr, "invalid ttl\n");
  94. break;
  95. case 'd':
  96. debug++;
  97. break;
  98. case 'i':
  99. interface_add(optarg);
  100. break;
  101. case '4':
  102. cfg_proto = 4;
  103. break;
  104. case '6':
  105. cfg_proto = 6;
  106. break;
  107. case 'n':
  108. cfg_no_subnet = 1;
  109. break;
  110. default:
  111. return -1;
  112. }
  113. }
  114. signal(SIGPIPE, SIG_IGN);
  115. signal(SIGTERM, signal_shutdown);
  116. signal(SIGKILL, signal_shutdown);
  117. if (cache_init())
  118. return -1;
  119. ubus_startup();
  120. service_init(0);
  121. uloop_run();
  122. uloop_done();
  123. interface_shutdown();
  124. cache_cleanup(NULL);
  125. service_cleanup();
  126. vlist_flush(&interfaces);
  127. return 0;
  128. }