MDNSService.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <cassert>
  2. #include <vector>
  3. #include "BellLogger.h"
  4. #include "MDNSService.h"
  5. #ifdef _WIN32
  6. #include <WinSock2.h>
  7. #include <iphlpapi.h>
  8. #pragma comment(lib, "IPHLPAPI.lib")
  9. #include "mdnssvc.h"
  10. #else
  11. #include <arpa/inet.h>
  12. #include "mdns.h"
  13. #endif
  14. using namespace bell;
  15. class implMDNSService : public MDNSService {
  16. private:
  17. struct mdns_service* service;
  18. void unregisterService(void) {
  19. mdns_service_remove(implMDNSService::mdnsServer, service);
  20. };
  21. public:
  22. static struct mdnsd* mdnsServer;
  23. implMDNSService(struct mdns_service* service) : service(service){};
  24. };
  25. /**
  26. * Win32 implementation of MDNSService
  27. **/
  28. struct mdnsd* implMDNSService::mdnsServer = NULL;
  29. static std::mutex registerMutex;
  30. std::unique_ptr<MDNSService> MDNSService::registerService(
  31. const std::string& serviceName, const std::string& serviceType,
  32. const std::string& serviceProto, const std::string& serviceHost,
  33. int servicePort, const std::map<std::string, std::string> txtData) {
  34. std::lock_guard lock(registerMutex);
  35. if (!implMDNSService::mdnsServer) {
  36. char hostname[128];
  37. gethostname(hostname, sizeof(hostname));
  38. struct sockaddr_in* host = NULL;
  39. ULONG size = sizeof(IP_ADAPTER_ADDRESSES) * 64;
  40. IP_ADAPTER_ADDRESSES* adapters = (IP_ADAPTER_ADDRESSES*)malloc(size);
  41. int ret = GetAdaptersAddresses(AF_UNSPEC,
  42. GAA_FLAG_INCLUDE_GATEWAYS |
  43. GAA_FLAG_SKIP_MULTICAST |
  44. GAA_FLAG_SKIP_ANYCAST,
  45. 0, adapters, &size);
  46. for (PIP_ADAPTER_ADDRESSES adapter = adapters; adapter && !host;
  47. adapter = adapter->Next) {
  48. if (adapter->TunnelType == TUNNEL_TYPE_TEREDO)
  49. continue;
  50. if (adapter->OperStatus != IfOperStatusUp)
  51. continue;
  52. for (IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress;
  53. unicast; unicast = unicast->Next) {
  54. if (adapter->FirstGatewayAddress &&
  55. unicast->Address.lpSockaddr->sa_family == AF_INET) {
  56. host = (struct sockaddr_in*)unicast->Address.lpSockaddr;
  57. BELL_LOG(info, "mdns", "mDNS on interface %s",
  58. inet_ntoa(host->sin_addr));
  59. implMDNSService::mdnsServer = mdnsd_start(host->sin_addr, false);
  60. break;
  61. }
  62. }
  63. }
  64. assert(implMDNSService::mdnsServer);
  65. mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, host->sin_addr);
  66. free(adapters);
  67. }
  68. std::vector<const char*> txt;
  69. std::vector<std::unique_ptr<std::string>> txtStr;
  70. for (auto& [key, value] : txtData) {
  71. auto str = make_unique<std::string>(key + "=" + value);
  72. txtStr.push_back(std::move(str));
  73. txt.push_back(txtStr.back()->c_str());
  74. }
  75. txt.push_back(NULL);
  76. std::string type(serviceType + "." + serviceProto + ".local");
  77. auto service =
  78. mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
  79. type.c_str(), servicePort, NULL, txt.data());
  80. return std::make_unique<implMDNSService>(service);
  81. }