MDNSService.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <arpa/inet.h>
  2. #include <ifaddrs.h>
  3. #include <net/if.h>
  4. #include <netdb.h>
  5. #include <unistd.h>
  6. #include <cstring>
  7. #include <vector>
  8. #if __has_include("avahi-client/client.h")
  9. #include <avahi-client/client.h>
  10. #include <avahi-client/publish.h>
  11. #include <avahi-common/alternative.h>
  12. #include <avahi-common/simple-watch.h>
  13. #elif !defined(BELL_DISABLE_AVAHI)
  14. #define BELL_DISABLE_AVAHI
  15. #endif
  16. #include "BellLogger.h"
  17. #include "MDNSService.h"
  18. #include "mdnssvc.h"
  19. using namespace bell;
  20. #ifndef BELL_DISABLE_AVAHI
  21. static void groupHandler(AvahiEntryGroup* g, AvahiEntryGroupState state,
  22. AVAHI_GCC_UNUSED void* userdata) {}
  23. #endif
  24. class implMDNSService : public MDNSService {
  25. private:
  26. #ifndef BELL_DISABLE_AVAHI
  27. AvahiEntryGroup* avahiGroup;
  28. #endif
  29. struct mdns_service* service;
  30. public:
  31. #ifndef BELL_DISABLE_AVAHI
  32. static AvahiClient* avahiClient;
  33. static AvahiSimplePoll* avahiPoll;
  34. #endif
  35. static struct mdnsd* mdnsServer;
  36. static in_addr_t host;
  37. implMDNSService(struct mdns_service* service) : service(service){};
  38. #ifndef BELL_DISABLE_AVAHI
  39. implMDNSService(AvahiEntryGroup* avahiGroup) : avahiGroup(avahiGroup){};
  40. #endif
  41. void unregisterService();
  42. };
  43. struct mdnsd* implMDNSService::mdnsServer = NULL;
  44. in_addr_t implMDNSService::host = INADDR_ANY;
  45. static std::mutex registerMutex;
  46. #ifndef BELL_DISABLE_AVAHI
  47. AvahiClient* implMDNSService::avahiClient = NULL;
  48. AvahiSimplePoll* implMDNSService::avahiPoll = NULL;
  49. #endif
  50. /**
  51. * Linux implementation of MDNSService using avahi.
  52. * @see https://www.avahi.org/doxygen/html/
  53. **/
  54. void implMDNSService::unregisterService() {
  55. #ifndef BELL_DISABLE_AVAHI
  56. if (avahiGroup) {
  57. avahi_entry_group_free(avahiGroup);
  58. } else
  59. #endif
  60. {
  61. mdns_service_remove(implMDNSService::mdnsServer, service);
  62. }
  63. }
  64. std::unique_ptr<MDNSService> MDNSService::registerService(
  65. const std::string& serviceName, const std::string& serviceType,
  66. const std::string& serviceProto, const std::string& serviceHost,
  67. int servicePort, const std::map<std::string, std::string> txtData) {
  68. std::lock_guard lock(registerMutex);
  69. #ifndef BELL_DISABLE_AVAHI
  70. // try avahi first if available
  71. if (!implMDNSService::avahiPoll) {
  72. implMDNSService::avahiPoll = avahi_simple_poll_new();
  73. }
  74. if (implMDNSService::avahiPoll && !implMDNSService::avahiClient) {
  75. implMDNSService::avahiClient =
  76. avahi_client_new(avahi_simple_poll_get(implMDNSService::avahiPoll),
  77. AvahiClientFlags(0), NULL, NULL, NULL);
  78. }
  79. AvahiEntryGroup* avahiGroup = NULL;
  80. if (implMDNSService::avahiClient &&
  81. (avahiGroup = avahi_entry_group_new(implMDNSService::avahiClient,
  82. groupHandler, NULL)) == NULL) {
  83. BELL_LOG(error, "MDNS", "cannot create service %s", serviceName.c_str());
  84. }
  85. if (avahiGroup != NULL) {
  86. AvahiStringList* avahiTxt = NULL;
  87. for (auto& [key, value] : txtData) {
  88. avahiTxt =
  89. avahi_string_list_add_pair(avahiTxt, key.c_str(), value.c_str());
  90. }
  91. std::string type(serviceType + "." + serviceProto);
  92. int ret = avahi_entry_group_add_service_strlst(
  93. avahiGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0,
  94. serviceName.c_str(), type.c_str(), NULL, NULL, servicePort, avahiTxt);
  95. avahi_string_list_free(avahiTxt);
  96. if (ret >= 0) {
  97. ret = avahi_entry_group_commit(avahiGroup);
  98. }
  99. if (ret < 0) {
  100. BELL_LOG(error, "MDNS", "cannot run service %s", serviceName.c_str());
  101. avahi_entry_group_free(avahiGroup);
  102. } else {
  103. BELL_LOG(info, "MDNS", "using avahi for %s", serviceName.c_str());
  104. return std::make_unique<implMDNSService>(avahiGroup);
  105. }
  106. }
  107. #endif
  108. // avahi failed, use build-in server
  109. struct ifaddrs* ifaddr;
  110. // get the host address first
  111. if (serviceHost.size()) {
  112. struct hostent* h = gethostbyname(serviceHost.c_str());
  113. if (h) {
  114. memcpy(&implMDNSService::host, h->h_addr_list[0], 4);
  115. }
  116. }
  117. // try go guess ifaddr if we have nothing as listening to INADDR_ANY usually does not work
  118. if (implMDNSService::host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
  119. for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  120. if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET ||
  121. !(ifa->ifa_flags & IFF_UP) || !(ifa->ifa_flags & IFF_MULTICAST) ||
  122. (ifa->ifa_flags & IFF_LOOPBACK))
  123. continue;
  124. implMDNSService::host =
  125. ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
  126. break;
  127. }
  128. freeifaddrs(ifaddr);
  129. }
  130. if (!implMDNSService::mdnsServer) {
  131. char hostname[256];
  132. struct in_addr addr;
  133. // it's the same, but who knows..
  134. addr.s_addr = implMDNSService::host;
  135. gethostname(hostname, sizeof(hostname));
  136. implMDNSService::mdnsServer = mdnsd_start(addr, false);
  137. if (implMDNSService::mdnsServer) {
  138. mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, addr);
  139. }
  140. }
  141. if (implMDNSService::mdnsServer) {
  142. std::vector<const char*> txt;
  143. std::vector<std::unique_ptr<std::string>> txtStr;
  144. for (auto& [key, value] : txtData) {
  145. auto str = make_unique<std::string>(key + "=" + value);
  146. txtStr.push_back(std::move(str));
  147. txt.push_back(txtStr.back()->c_str());
  148. }
  149. txt.push_back(NULL);
  150. std::string type(serviceType + "." + serviceProto + ".local");
  151. BELL_LOG(info, "MDNS", "using built-in mDNS for %s", serviceName.c_str());
  152. struct mdns_service* mdnsService =
  153. mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
  154. type.c_str(), servicePort, NULL, txt.data());
  155. if (mdnsService) {
  156. auto service =
  157. mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
  158. type.c_str(), servicePort, NULL, txt.data());
  159. return std::make_unique<implMDNSService>(service);
  160. }
  161. }
  162. BELL_LOG(error, "MDNS", "cannot start any mDNS listener for %s",
  163. serviceName.c_str());
  164. return NULL;
  165. }