1 /*
2  * Copyright 2007-2021 CM4all GmbH
3  * All rights reserved.
4  *
5  * author: Max Kellermann <mk@cm4all.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "Publisher.hxx"
34 #include "Service.hxx"
35 #include "Client.hxx"
36 #include "Error.hxx"
37 #include "ErrorHandler.hxx"
38 #include "net/SocketAddress.hxx"
39 
40 #include <avahi-common/error.h>
41 #include <avahi-common/malloc.h>
42 #include <avahi-common/alternative.h>
43 
44 #include <cassert>
45 
46 #include <stdio.h>
47 #include <unistd.h>
48 
49 namespace Avahi {
50 
51 /**
52  * Append the process id to the given prefix string.  This is used as
53  * a workaround for an avahi-daemon bug/problem: when a service gets
54  * restarted, and then binds to a new port number (e.g. beng-proxy
55  * with automatic port assignment), we don't get notified, and so we
56  * never query the new port.  By appending the process id to the
57  * client name, we ensure that the exiting old process broadcasts
58  * AVAHI_BROWSER_REMOVE, and hte new process broadcasts
59  * AVAHI_BROWSER_NEW.
60  */
61 static std::string
MakePidName(const char * prefix)62 MakePidName(const char *prefix)
63 {
64 	char buffer[256];
65 	snprintf(buffer, sizeof(buffer), "%s[%u]", prefix, (unsigned)getpid());
66 	return buffer;
67 }
68 
Publisher(Client & _client,const char * _name,std::forward_list<Service> _services,ErrorHandler & _error_handler)69 Publisher::Publisher(Client &_client, const char *_name,
70 		     std::forward_list<Service> _services,
71 		     ErrorHandler &_error_handler) noexcept
72 	:error_handler(_error_handler),
73 	 name(MakePidName(_name)),
74 	 client(_client), services(std::move(_services))
75 {
76 	assert(!services.empty());
77 
78 	client.AddListener(*this);
79 
80 	auto *c = client.GetClient();
81 	if (c != nullptr)
82 		RegisterServices(c);
83 }
84 
~Publisher()85 Publisher::~Publisher() noexcept
86 {
87 	client.RemoveListener(*this);
88 }
89 
90 inline void
GroupCallback(AvahiEntryGroup * g,AvahiEntryGroupState state)91 Publisher::GroupCallback(AvahiEntryGroup *g,
92 			 AvahiEntryGroupState state) noexcept
93 {
94 	switch (state) {
95 	case AVAHI_ENTRY_GROUP_ESTABLISHED:
96 		break;
97 
98 	case AVAHI_ENTRY_GROUP_COLLISION:
99 		if (!visible)
100 			/* meanwhile, HideServices() has been called */
101 			return;
102 
103 		/* pick a new name */
104 
105 		{
106 			char *new_name = avahi_alternative_service_name(name.c_str());
107 			name = new_name;
108 			avahi_free(new_name);
109 		}
110 
111 		/* And recreate the services */
112 		RegisterServices(avahi_entry_group_get_client(g));
113 		break;
114 
115 	case AVAHI_ENTRY_GROUP_FAILURE:
116 		error_handler.OnAvahiError(std::make_exception_ptr(MakeError(*avahi_entry_group_get_client(g),
117 									     "Avahi service group failure")));
118 		break;
119 
120 	case AVAHI_ENTRY_GROUP_UNCOMMITED:
121 	case AVAHI_ENTRY_GROUP_REGISTERING:
122 		break;
123 	}
124 }
125 
126 void
GroupCallback(AvahiEntryGroup * g,AvahiEntryGroupState state,void * userdata)127 Publisher::GroupCallback(AvahiEntryGroup *g,
128 			 AvahiEntryGroupState state,
129 			 void *userdata) noexcept
130 {
131 	auto &publisher = *(Publisher *)userdata;
132 	publisher.GroupCallback(g, state);
133 }
134 
135 static void
AddService(AvahiEntryGroup & group,const Service & service,const char * name)136 AddService(AvahiEntryGroup &group, const Service &service,
137 	   const char *name)
138 {
139 	int error = avahi_entry_group_add_service(&group,
140 						  service.interface,
141 						  service.protocol,
142 						  AvahiPublishFlags(0),
143 						  name, service.type.c_str(),
144 						  nullptr, nullptr,
145 						  service.port, nullptr);
146 	if (error != AVAHI_OK)
147 		throw MakeError(error, "Failed to add Avahi service");
148 }
149 
150 static void
AddServices(AvahiEntryGroup & group,const std::forward_list<Service> & services,const char * name)151 AddServices(AvahiEntryGroup &group,
152 	    const std::forward_list<Service> &services, const char *name)
153 {
154 	for (const auto &i : services)
155 		AddService(group, i, name);
156 }
157 
158 static EntryGroupPtr
MakeEntryGroup(AvahiClient & c,const std::forward_list<Service> & services,const char * name,AvahiEntryGroupCallback callback,void * userdata)159 MakeEntryGroup(AvahiClient &c,
160 	       const std::forward_list<Service> &services, const char *name,
161 	       AvahiEntryGroupCallback callback, void *userdata)
162 {
163 	EntryGroupPtr group(avahi_entry_group_new(&c, callback, userdata));
164 	if (!group)
165 		throw MakeError(c, "Failed to create Avahi service group");
166 
167 	AddServices(*group, services, name);
168 
169 	int error = avahi_entry_group_commit(group.get());
170 	if (error != AVAHI_OK)
171 		throw MakeError(error, "Failed to commit Avahi service group");
172 
173 	return group;
174 }
175 
176 void
RegisterServices(AvahiClient * c)177 Publisher::RegisterServices(AvahiClient *c) noexcept
178 {
179 	assert(visible);
180 
181 	try {
182 		group = MakeEntryGroup(*c, services, name.c_str(),
183 				       GroupCallback, this);
184 	} catch (...) {
185 		error_handler.OnAvahiError(std::current_exception());
186 	}
187 }
188 
189 void
HideServices()190 Publisher::HideServices() noexcept
191 {
192 	if (!visible)
193 		return;
194 
195 	visible = false;
196 	group.reset();
197 }
198 
199 void
ShowServices()200 Publisher::ShowServices() noexcept
201 {
202 	if (visible)
203 		return;
204 
205 	visible = true;
206 
207 	auto *c = client.GetClient();
208 	if (c != nullptr)
209 		RegisterServices(c);
210 }
211 
212 void
OnAvahiConnect(AvahiClient * c)213 Publisher::OnAvahiConnect(AvahiClient *c) noexcept
214 {
215 	if (group == nullptr && visible)
216 		RegisterServices(c);
217 }
218 
219 void
OnAvahiDisconnect()220 Publisher::OnAvahiDisconnect() noexcept
221 {
222 	group.reset();
223 }
224 
225 void
OnAvahiChanged()226 Publisher::OnAvahiChanged() noexcept
227 {
228 	group.reset();
229 }
230 
231 } // namespace Avahi
232