Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[50ef25]: server / zeroconf.cpp Maximize Restore History

Download this file

zeroconf.cpp    266 lines (222 with data), 7.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (C) 2007-2010 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sipwitch-config.h>
#include <sipwitch/sipwitch.h>
NAMESPACE_SIPWITCH
using namespace UCOMMON_NAMESPACE;
#ifdef ZEROCONF_AVAHI
extern "C" {
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/thread-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <avahi-common/timeval.h>
}
class __LOCAL zeroconf : public modules::generic
{
public:
zeroconf();
void setClient(AvahiClientState state);
void setGroup(AvahiEntryGroupState state);
inline void setClient(AvahiClient *c)
{client = c;};
static zeroconf plugin;
private:
void start(service *cfg);
void stop(service *cfg);
void reload(service *cfg);
void publish(service *cfg);
AvahiThreadedPoll *poller;
AvahiClient *client;
AvahiEntryGroup *group;
char *name;
const char *protocol;
int error;
};
extern "C" {
static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata)
{
if(!c)
return;
zeroconf::plugin.setClient(c);
zeroconf::plugin.setClient(state);
}
static void group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata)
{
zeroconf::plugin.setGroup(state);
}
}
zeroconf::zeroconf() :
modules::generic()
{
protocol = "_sip._udp";
poller = NULL;
client = NULL;
group = NULL;
name = avahi_strdup("sipwitch");
shell::log(shell::ERR, "zeroconf plugin using avahi");
}
void zeroconf::setGroup(AvahiEntryGroupState state)
{
char *newname;
switch(state)
{
case AVAHI_ENTRY_GROUP_ESTABLISHED:
shell::log(shell::INFO, "zeroconf %s service(s) established", name);
break;
case AVAHI_ENTRY_GROUP_COLLISION:
newname = avahi_alternative_service_name(name);
shell::log(shell::NOTIFY, "zeroconf service %s renamed %s", name, newname);
avahi_free(name);
name = newname;
setClient(AVAHI_CLIENT_S_RUNNING);
break;
case AVAHI_ENTRY_GROUP_FAILURE:
shell::log(shell::ERR, "zeroconf service failure; error=%s",
avahi_strerror(avahi_client_errno(client)));
// avahi_thread_poll_quit(poller);
default:
break;
}
}
void zeroconf::setClient(AvahiClientState state)
{
int ret;
AvahiProtocol avifamily = AVAHI_PROTO_UNSPEC;
switch(state) {
case AVAHI_CLIENT_S_RUNNING:
goto add;
case AVAHI_CLIENT_FAILURE:
failed:
shell::log(shell::ERR, "zeroconf failure; error=%s",
avahi_strerror(avahi_client_errno(client)));
break;
case AVAHI_CLIENT_S_COLLISION:
case AVAHI_CLIENT_S_REGISTERING:
if(group)
avahi_entry_group_reset(group);
default:
break;
}
return;
add:
if(!group)
group = avahi_entry_group_new(client, group_callback, NULL);
if(!group)
goto failed;
shell::log(shell::INFO, "zeroconf adding sip on port %d", sip_port);
if(sip_domain) {
char domain[256];
char prefix[32];
char range[32];
char uuid[64];
snprintf(domain, sizeof(domain), "domain=%s", sip_domain);
snprintf(prefix, sizeof(prefix), "prefix=%u", sip_prefix);
snprintf(range, sizeof(range), "range=%u", sip_range);
snprintf(uuid, sizeof(uuid), "uuid=%s", session_uuid);
ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, avifamily,
(AvahiPublishFlags)0, name, protocol, NULL, NULL, sip_port,
"type=sipwitch", domain, prefix, range, uuid, NULL);
}
else
ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, avifamily,
(AvahiPublishFlags)0, name, protocol, NULL, NULL, sip_port,
"type=sipwitch", NULL);
if(ret < 0)
shell::log(shell::ERR, "zeroconf %s failed; error=%s",
protocol, avahi_strerror(ret));
ret = avahi_entry_group_commit(group);
if(ret >= 0)
return;
shell::log(shell::ERR, "zeroconf service commit failure; error=%s",
avahi_strerror(ret));
}
void zeroconf::stop(service *cfg)
{
if(poller)
avahi_threaded_poll_stop(poller);
if(client)
avahi_client_free(client);
if(name)
avahi_free(name);
// if(poller)
// avahi_threaded_poll_free(poller);
client = NULL;
poller = NULL;
name = NULL;
}
void zeroconf::start(service *cfg)
{
poller = avahi_threaded_poll_new();
if(!poller) {
shell::log(shell::ERR, "zeroconf service failed to start");
return;
}
client = avahi_client_new(avahi_threaded_poll_get(poller),
(AvahiClientFlags)0, client_callback, NULL, &error);
shell::log(shell::INFO, "zeroconf service started");
avahi_threaded_poll_start(poller);
}
void zeroconf::reload(service *cfg)
{
if(sip_protocol == IPPROTO_TCP)
protocol = "_sip._tcp";
}
void zeroconf::publish(service *cfg)
{
assert(cfg != NULL);
char domain[256];
char prefix[32];
char range[32];
char uuid[64];
static bool started = false;
AvahiProtocol avifamily = AVAHI_PROTO_UNSPEC;
int ret = 0;
if(started && group && sip_domain) {
snprintf(domain, sizeof(domain), "domain=%s", sip_domain);
snprintf(prefix, sizeof(prefix), "prefix=%u", sip_prefix);
snprintf(range, sizeof(range), "range=%u", sip_range);
snprintf(uuid, sizeof(uuid), "uuid=%s", session_uuid);
ret = avahi_entry_group_update_service_txt(group, AVAHI_IF_UNSPEC, avifamily,
(AvahiPublishFlags)0, name, protocol, NULL,
"type=sipwitch", domain, prefix, range, uuid, NULL);
}
else if(started && group) {
ret = avahi_entry_group_update_service_txt(group, AVAHI_IF_UNSPEC, avifamily,
(AvahiPublishFlags)0, name, protocol, NULL,
"type=sipwitch", NULL);
}
if(ret < 0)
shell::log(shell::ERR, "zeroconf %s failed; error=%s",
protocol, avahi_strerror(ret));
started = true;
}
#else
class __LOCAL zeroconf : modules::generic
{
public:
static zeroconf plugin;
zeroconf();
};
zeroconf::zeroconf() :
modules::generic()
{
shell::log(shell::ERR, "zeroconf plugin could not be built");
}
#endif
zeroconf zeroconf::plugin;
END_NAMESPACE