1 /***
2     This file is part of snapcast
3     Copyright (C) 2014-2020  Johannes Pohl
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 ***/
18 
19 #include <cstdlib>
20 #include <thread>
21 
22 #include "common/aixlog.hpp"
23 #include "publish_bonjour.hpp"
24 
25 static constexpr auto LOG_TAG = "Bonjour";
26 
27 typedef union
28 {
29     unsigned char b[2];
30     unsigned short NotAnInteger;
31 } Opaque16;
32 
33 
PublishBonjour(const std::string & serviceName,boost::asio::io_context & ioc)34 PublishBonjour::PublishBonjour(const std::string& serviceName, boost::asio::io_context& ioc) : PublishmDNS(serviceName, ioc), active_(false)
35 {
36     ///	dns-sd -R Snapcast _snapcast._tcp local 1704
37     ///	dns-sd -R Snapcast _snapcast-jsonrpc._tcp local 1705
38 }
39 
40 
~PublishBonjour()41 PublishBonjour::~PublishBonjour()
42 {
43     active_ = false;
44     pollThread_.join();
45     for (auto client : clients)
46     {
47         if (client)
48             DNSServiceRefDeallocate(client);
49     }
50 }
51 
52 
53 
worker()54 void PublishBonjour::worker()
55 {
56     //    int dns_sd_fd  = client ? DNSServiceRefSockFD(client) : -1;
57     // 1. Set up the fd_set as usual here.
58     // This example client has no file descriptors of its own,
59     // but a real application would call FD_SET to add them to the set here
60     fd_set readfds;
61     FD_ZERO(&readfds);
62 
63     std::vector<int> dns_sd_fds;
64     int nfds = -1;
65     for (size_t n = 0; n < clients.size(); ++n)
66     {
67         int dns_sd_fd = DNSServiceRefSockFD(clients[n]);
68         dns_sd_fds.push_back(dns_sd_fd);
69         if (nfds < dns_sd_fd)
70             nfds = dns_sd_fd;
71         // 2. Add the fd for our client(s) to the fd_set
72         FD_SET(dns_sd_fd, &readfds);
73     }
74     ++nfds;
75 
76     // 3. Set up the timeout.
77     struct timeval tv;
78     tv.tv_sec = 0;
79     tv.tv_usec = 100 * 1000;
80 
81     active_ = true;
82     while (active_)
83     {
84         FD_ZERO(&readfds);
85         for (size_t n = 0; n < dns_sd_fds.size(); ++n)
86             FD_SET(dns_sd_fds[n], &readfds);
87 
88         int result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
89         if (result > 0)
90         {
91 
92             for (size_t n = 0; n < dns_sd_fds.size(); ++n)
93             {
94                 if (clients[n] && FD_ISSET(dns_sd_fds[n], &readfds))
95                 {
96                     DNSServiceErrorType err = DNSServiceProcessResult(clients[n]);
97                     if (err)
98                     {
99                         LOG(ERROR, LOG_TAG) << "DNSServiceProcessResult returned " << err << "\n";
100                         active_ = false;
101                     }
102                 }
103             }
104         }
105         //		else if (result == 0)
106         //			myTimerCallBack();
107         else if (result < 0)
108         {
109             LOG(ERROR, LOG_TAG) << "select() returned " << result << " errno " << errno << " " << strerror(errno) << "\n";
110             if (errno != EINTR)
111                 active_ = false;
112         }
113     }
114 }
115 
116 
reg_reply(DNSServiceRef sdref,const DNSServiceFlags flags,DNSServiceErrorType errorCode,const char * name,const char * regtype,const char * domain,void * context)117 static void DNSSD_API reg_reply(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode, const char* name, const char* regtype,
118                                 const char* domain, void* context)
119 {
120     (void)sdref; // Unused
121     (void)flags; // Unused
122 
123     PublishBonjour* publishBonjour = (PublishBonjour*)context;
124     (void)publishBonjour; // unused
125 
126     LOG(INFO, LOG_TAG) << "Got a reply for service " << name << "." << regtype << domain << "\n";
127 
128     if (errorCode == kDNSServiceErr_NoError)
129     {
130         if (flags & kDNSServiceFlagsAdd)
131             LOG(INFO, LOG_TAG) << "Name now registered and active\n";
132         else
133             LOG(INFO, LOG_TAG) << "Name registration removed\n";
134     }
135     else if (errorCode == kDNSServiceErr_NameConflict)
136     {
137         /// TODO: Error handling
138         LOG(INFO, LOG_TAG) << "Name in use, please choose another\n";
139         exit(-1);
140     }
141     else
142         LOG(INFO, LOG_TAG) << "Error " << errorCode << "\n";
143 
144     if (!(flags & kDNSServiceFlagsMoreComing))
145         fflush(stdout);
146 }
147 
148 
publish(const std::vector<mDNSService> & services)149 void PublishBonjour::publish(const std::vector<mDNSService>& services)
150 {
151     for (auto service : services)
152     {
153         DNSServiceFlags flags = 0;
154         Opaque16 registerPort = {{static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF)}};
155         DNSServiceRef client = NULL;
156         // DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger,
157         // service.txt_.size(), service.txt_.empty()?NULL:service.txt_.c_str(), reg_reply, this);
158         DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger, 0,
159                            NULL, reg_reply, this);
160         clients.push_back(client);
161     }
162 
163     pollThread_ = std::thread(&PublishBonjour::worker, this);
164 }
165