announce.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/types.h>
  14. #include <stdio.h>
  15. #include <libubox/uloop.h>
  16. #include "cache.h"
  17. #include "dns.h"
  18. #include "util.h"
  19. #include "service.h"
  20. #include "announce.h"
  21. #include "interface.h"
  22. #define TTL_TIMEOUT 75
  23. enum {
  24. STATE_PROBE1 = 0,
  25. STATE_PROBE2,
  26. STATE_PROBE3,
  27. STATE_PROBE_WAIT,
  28. STATE_PROBE_END,
  29. STATE_ANNOUNCE,
  30. };
  31. int announce_ttl = 75 * 60;
  32. static void
  33. announce_timer(struct uloop_timeout *timeout)
  34. {
  35. struct interface *iface = container_of(timeout, struct interface, announce_timer);
  36. char host[256];
  37. snprintf(host, sizeof(host), "%s.local", hostname);
  38. switch (iface->announce_state) {
  39. case STATE_PROBE1:
  40. case STATE_PROBE2:
  41. case STATE_PROBE3:
  42. dns_send_question(iface, host, TYPE_ANY);
  43. uloop_timeout_set(timeout, 250);
  44. iface->announce_state++;
  45. break;
  46. case STATE_PROBE_WAIT:
  47. uloop_timeout_set(timeout, 500);
  48. iface->announce_state++;
  49. break;
  50. case STATE_PROBE_END:
  51. if (cache_host_is_known(host)) {
  52. fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
  53. return;
  54. }
  55. iface->announce_state++;
  56. case STATE_ANNOUNCE:
  57. service_announce(iface);
  58. uloop_timeout_set(timeout, announce_ttl * 800);
  59. break;
  60. }
  61. }
  62. void
  63. announce_init(struct interface *iface)
  64. {
  65. iface->announce_state = STATE_PROBE1;
  66. iface->announce_timer.cb = announce_timer;
  67. uloop_timeout_set(&iface->announce_timer, 100);
  68. }
  69. void
  70. announce_free(struct interface *iface)
  71. {
  72. uloop_timeout_cancel(&iface->announce_timer);
  73. }