240-ubus.patch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --- a/src/dnsmasq.c
  2. +++ b/src/dnsmasq.c
  3. @@ -19,6 +19,8 @@
  4. #include "dnsmasq.h"
  5. +#include <libubus.h>
  6. +
  7. struct daemon *daemon;
  8. static volatile pid_t pid = 0;
  9. @@ -32,6 +34,62 @@ static void fatal_event(struct event_des
  10. static int read_event(int fd, struct event_desc *evp, char **msg);
  11. static void poll_resolv(int force, int do_reload, time_t now);
  12. +static struct ubus_context *ubus;
  13. +static struct blob_buf b;
  14. +
  15. +static struct ubus_object_type ubus_object_type = {
  16. + .name = "dnsmasq",
  17. +};
  18. +
  19. +static struct ubus_object ubus_object = {
  20. + .name = "dnsmasq",
  21. + .type = &ubus_object_type,
  22. +};
  23. +
  24. +void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name)
  25. +{
  26. + if (!ubus || !ubus_object.has_subscribers)
  27. + return;
  28. +
  29. + blob_buf_init(&b, 0);
  30. + if (mac)
  31. + blobmsg_add_string(&b, "mac", mac);
  32. + if (ip)
  33. + blobmsg_add_string(&b, "ip", ip);
  34. + if (name)
  35. + blobmsg_add_string(&b, "name", name);
  36. + ubus_notify(ubus, &ubus_object, type, b.head, -1);
  37. +}
  38. +
  39. +static void set_ubus_listeners(void)
  40. +{
  41. + if (!ubus)
  42. + return;
  43. +
  44. + poll_listen(ubus->sock.fd, POLLIN);
  45. + poll_listen(ubus->sock.fd, POLLERR);
  46. + poll_listen(ubus->sock.fd, POLLHUP);
  47. +}
  48. +
  49. +static void check_ubus_listeners()
  50. +{
  51. + if (!ubus) {
  52. + ubus = ubus_connect(NULL);
  53. + if (ubus)
  54. + ubus_add_object(ubus, &ubus_object);
  55. + else
  56. + return;
  57. + }
  58. +
  59. + if (poll_check(ubus->sock.fd, POLLIN))
  60. + ubus_handle_event(ubus);
  61. +
  62. + if (poll_check(ubus->sock.fd, POLLHUP)) {
  63. + ubus_free(ubus);
  64. + ubus = NULL;
  65. + }
  66. +}
  67. +
  68. int main (int argc, char **argv)
  69. {
  70. int bind_fallback = 0;
  71. @@ -911,6 +969,7 @@ int main (int argc, char **argv)
  72. set_dbus_listeners();
  73. #endif
  74. + set_ubus_listeners();
  75. #ifdef HAVE_DHCP
  76. if (daemon->dhcp || daemon->relay4)
  77. {
  78. @@ -1041,6 +1100,8 @@ int main (int argc, char **argv)
  79. check_dbus_listeners();
  80. #endif
  81. + check_ubus_listeners();
  82. +
  83. check_dns_listeners(now);
  84. #ifdef HAVE_TFTP
  85. --- a/Makefile
  86. +++ b/Makefile
  87. @@ -85,7 +85,7 @@ all : $(BUILDDIR)
  88. @cd $(BUILDDIR) && $(MAKE) \
  89. top="$(top)" \
  90. build_cflags="$(version) $(dbus_cflags) $(idn2_cflags) $(idn_cflags) $(ct_cflags) $(lua_cflags) $(nettle_cflags)" \
  91. - build_libs="$(dbus_libs) $(idn2_libs) $(idn_libs) $(ct_libs) $(lua_libs) $(sunos_libs) $(nettle_libs) $(gmp_libs)" \
  92. + build_libs="$(dbus_libs) $(idn2_libs) $(idn_libs) $(ct_libs) $(lua_libs) $(sunos_libs) $(nettle_libs) $(gmp_libs) -lubox -lubus" \
  93. -f $(top)/Makefile dnsmasq
  94. mostly_clean :
  95. --- a/src/dnsmasq.h
  96. +++ b/src/dnsmasq.h
  97. @@ -1397,6 +1397,8 @@ void emit_dbus_signal(int action, struct
  98. # endif
  99. #endif
  100. +void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name);
  101. +
  102. /* ipset.c */
  103. #ifdef HAVE_IPSET
  104. void ipset_init(void);
  105. --- a/src/rfc2131.c
  106. +++ b/src/rfc2131.c
  107. @@ -1621,6 +1621,10 @@ static void log_packet(char *type, void
  108. daemon->namebuff,
  109. string ? string : "",
  110. err ? err : "");
  111. + if (!strcmp(type, "DHCPACK"))
  112. + ubus_event_bcast("dhcp.ack", addr ? inet_ntoa(a) : NULL, daemon->namebuff, string ? string : NULL);
  113. + else if (!strcmp(type, "DHCPRELEASE"))
  114. + ubus_event_bcast("dhcp.release", addr ? inet_ntoa(a) : NULL, daemon->namebuff, string ? string : NULL);
  115. }
  116. static void log_options(unsigned char *start, u32 xid)