2
0

announce.c 2.1 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. switch (iface->announce_state) {
  37. case STATE_PROBE1:
  38. case STATE_PROBE2:
  39. case STATE_PROBE3:
  40. dns_send_question(iface, NULL, mdns_hostname_local, TYPE_ANY, 1);
  41. uloop_timeout_set(timeout, 250);
  42. iface->announce_state++;
  43. break;
  44. case STATE_PROBE_WAIT:
  45. uloop_timeout_set(timeout, 500);
  46. iface->announce_state++;
  47. break;
  48. case STATE_PROBE_END:
  49. if (cache_host_is_known(mdns_hostname_local)) {
  50. fprintf(stderr, "the host %s already exists. stopping announce service\n", mdns_hostname_local);
  51. return;
  52. }
  53. iface->announce_state++;
  54. /* Fall through */
  55. case STATE_ANNOUNCE:
  56. dns_reply_a(iface, NULL, announce_ttl, NULL);
  57. dns_reply_a_additional(iface, NULL, announce_ttl);
  58. service_announce_services(iface, NULL, announce_ttl);
  59. uloop_timeout_set(timeout, announce_ttl * 800);
  60. break;
  61. }
  62. }
  63. void
  64. announce_init(struct interface *iface)
  65. {
  66. iface->announce_state = STATE_PROBE1;
  67. iface->announce_timer.cb = announce_timer;
  68. uloop_timeout_set(&iface->announce_timer, 100);
  69. }
  70. void
  71. announce_free(struct interface *iface)
  72. {
  73. uloop_timeout_cancel(&iface->announce_timer);
  74. }