announce.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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);
  57. service_announce_services(iface, NULL, announce_ttl);
  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. }