MDNSService.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "MDNSService.h"
  2. #include "dns_sd.h"
  3. #include <arpa/inet.h>
  4. using namespace bell;
  5. /**
  6. * MacOS implementation of MDNSService.
  7. * @see https://developer.apple.com/documentation/dnssd/1804733-dnsserviceregister
  8. **/
  9. void* MDNSService::registerService(
  10. const std::string& serviceName,
  11. const std::string& serviceType,
  12. const std::string& serviceProto,
  13. const std::string& serviceHost,
  14. int servicePort,
  15. const std::map<std::string, std::string> txtData
  16. ) {
  17. DNSServiceRef* ref = new DNSServiceRef();
  18. TXTRecordRef txtRecord;
  19. TXTRecordCreate(&txtRecord, 0, NULL);
  20. for (auto& data : txtData) {
  21. TXTRecordSetValue(&txtRecord, data.first.c_str(), data.second.size(), data.second.c_str());
  22. }
  23. DNSServiceRegister(
  24. ref, /* sdRef */
  25. 0, /* flags */
  26. 0, /* interfaceIndex */
  27. serviceName.c_str(), /* name */
  28. (serviceType + "." + serviceProto).c_str(), /* regType (_spotify-connect._tcp) */
  29. NULL, /* domain */
  30. NULL, /* host */
  31. htons(servicePort), /* port */
  32. TXTRecordGetLength(&txtRecord), /* txtLen */
  33. TXTRecordGetBytesPtr(&txtRecord), /* txtRecord */
  34. NULL, /* callBack */
  35. NULL /* context */
  36. );
  37. TXTRecordDeallocate(&txtRecord);
  38. return ref;
  39. }
  40. void MDNSService::unregisterService(void* ref) {
  41. DNSServiceRefDeallocate((DNSServiceRef)ref);
  42. }