announce.c 1.8 KB

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