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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2004 Alistair Riddoch
//
// 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 2 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, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// $Id: CommMDNSPublisher.cpp,v 1.18 2007-12-05 01:01:51 alriddoch Exp $
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#undef PACKAGE
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef PACKAGE_BUGREPORT
#undef VERSION
#include "CommMDNSPublisher.h"
#include "CommServer.h"
#include "ServerRouting.h"
#include "common/log.h"
#include "common/const.h"
#include "common/debug.h"
#include "common/globals.h"
#include "common/BaseWorld.h"
#include "common/compose.hpp"
#include <iostream>
#if defined(HAVE_LIBHOWL)
#include <discovery/discovery.h>
#include <cassert>
static const bool debug_flag = false;
static sw_result reply_callback(sw_discovery,
sw_discovery_oid,
sw_discovery_publish_status status,
sw_opaque)
{
if (status == SW_DISCOVERY_PUBLISH_STARTED) {
// log(NOTICE, "Started publishing using MDNS");
} else if (status == SW_DISCOVERY_PUBLISH_STOPPED) {
// log(NOTICE, "Stopped publishing using MDNS");
} else if (status == SW_DISCOVERY_PUBLISH_NAME_COLLISION) {
log(WARNING, "Name collision publishing using howl MDNS");
} else if (status == SW_DISCOVERY_PUBLISH_INVALID) {
log(WARNING, "Invalid error publishing using howl MDNS");
} else {
log(ERROR, "Unknown error code using howl MDNS");
}
return SW_OKAY;
}
CommMDNSPublisher::CommMDNSPublisher(CommServer & svr) : Idle(svr),
CommSocket(svr),
m_session(0)
{
}
CommMDNSPublisher::~CommMDNSPublisher()
{
if (m_session != 0) {
sw_discovery_fina(m_session);
m_session = 0;
}
}
int CommMDNSPublisher::setup()
{
if (sw_discovery_init(&m_session) != SW_OKAY) {
log(WARNING, "Unable to create MDNS publisher session");
return -1;
}
if (sw_discovery_publish(m_session, 0,
m_commServer.m_server.getName().c_str(),
"_worldforge._tcp.", NULL, NULL,
client_port_num, NULL, 0,
reply_callback, this, &m_oid) != SW_OKAY) {
log(WARNING, "Unable to publish our presence using MDNS");
return -1;
}
return 0;
}
void CommMDNSPublisher::idle(time_t t)
{
}
int CommMDNSPublisher::getFd() const
{
assert(m_session != 0);
return sw_discovery_socket(m_session);
}
bool CommMDNSPublisher::isOpen() const
{
assert(m_session != 0);
return sw_discovery_socket(m_session) != -1;
}
bool CommMDNSPublisher::eof()
{
return false;
}
int CommMDNSPublisher::read()
{
assert(m_session != 0);
if (sw_discovery_read_socket(m_session) != SW_OKAY) {
log(WARNING, "Error publishing our presence using MDNS. Disabled.");
return -1;
}
return 0;
}
void CommMDNSPublisher::dispatch()
{
}
#elif defined(HAVE_AVAHI)
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/error.h>
static const bool debug_flag = false;
static void avahi_client_callback(AvahiClient * s,
AvahiClientState state,
void * userdata)
{
CommMDNSPublisher * cmp = (CommMDNSPublisher*)userdata;
switch (state) {
case AVAHI_CLIENT_S_RUNNING:
// Check we have not already started them
if (cmp->m_group == 0) {
cmp->setup_service(s);
}
break;
case AVAHI_CLIENT_S_COLLISION:
log(WARNING, "Name collision while publishing using howl MDNS");
break;
case AVAHI_CLIENT_FAILURE:
log(WARNING, "Failure while publishing using howl MDNS");
break;
case AVAHI_CLIENT_CONNECTING:
log(ERROR, "Avahi returned connecting, but we did not specify NO_FAIL");
break;
case AVAHI_CLIENT_S_REGISTERING:
break;
}
}
void group_callback(AvahiEntryGroup * g,
AvahiEntryGroupState state,
void * userdata)
{
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED :
/* The entry group has been established successfully */
break;
case AVAHI_ENTRY_GROUP_COLLISION : {
log(NOTICE, "Avahi callback reported group collision");
/* A service name collision happened. Let's pick a new name */
// char * n = avahi_alternative_service_name(name);
// avahi_free(name);
// name = n;
// fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
/* And recreate the services */
// create_services(avahi_entry_group_get_client(g));
break;
}
case AVAHI_ENTRY_GROUP_FAILURE :
log(NOTICE, "Avahi callback reported group failure");
/* Some kind of failure happened while we were registering our services */
// avahi_simple_poll_quit(simple_poll);
break;
case AVAHI_ENTRY_GROUP_UNCOMMITED:
log(NOTICE, "Avahi callback reported group uncommited");
break;
case AVAHI_ENTRY_GROUP_REGISTERING:
log(NOTICE, "Avahi callback reported group registering");
break;
}
}
struct AvahiWatch {
CommMDNSPublisher * m_publisher;
AvahiWatchEvent m_requiredEvent;
AvahiWatchCallback m_callback;
AvahiWatchEvent m_events;
void * m_userdata;
};
static AvahiWatch* watch_new(const AvahiPoll *api,
int fd,
AvahiWatchEvent event,
AvahiWatchCallback callback,
void *userdata)
{
debug(std::cout << "avahi_watch_new " << fd << std::endl << std::flush;);
CommMDNSPublisher * cmp = (CommMDNSPublisher*)api->userdata;
if (cmp->getFd() != -1) {
log(ERROR, "Avahi asked for multiple fds. Unable to comply.");
} else {
cmp->m_avahiFd = fd;
}
if (!event & AVAHI_WATCH_IN) {
log(ERROR, "Avahi watcher does not require read events.");
}
if (event & ~AVAHI_WATCH_IN) {
log(WARNING, "Avahi watcher requires unsupported events.");
}
AvahiWatch * aw = new AvahiWatch;
aw->m_publisher = cmp;
aw->m_requiredEvent = event;
aw->m_callback = callback;
aw->m_events = (AvahiWatchEvent)0;
aw->m_userdata = userdata;
cmp->m_avahiWatch = aw;
return aw;
}
static void watch_update(AvahiWatch *w, AvahiWatchEvent event)
{
debug(std::cout << "avahi_watch_update" << std::endl << std::flush;);
w->m_requiredEvent = event;
}
static AvahiWatchEvent watch_get_events(AvahiWatch *w)
{
debug(std::cout << "avahi_watch_get_events" << std::endl << std::flush;);
return w->m_events;
}
static void watch_free(AvahiWatch *w)
{
debug(std::cout << "avahi_watch_free" << std::endl << std::flush;);
log(WARNING, "avahi watch_free handler called");
}
struct AvahiTimeout {
CommMDNSPublisher * m_publisher;
struct timeval m_tv;
AvahiTimeoutCallback m_callback;
void * m_userdata;
};
static AvahiTimeout* timeout_new(const AvahiPoll * api,
const struct timeval * tv,
AvahiTimeoutCallback callback,
void *userdata)
{
debug(std::cout << "avahi_timeout_new " << tv << " " << callback << std::endl << std::flush;);
CommMDNSPublisher * cmp = (CommMDNSPublisher*)api->userdata;
AvahiTimeout * at = new AvahiTimeout;
at->m_publisher = cmp;
if (tv != 0) {
at->m_tv = *tv;
} else {
at->m_tv.tv_sec = 0;
}
at->m_callback = callback;
at->m_userdata = userdata;
cmp->m_avahiTimeouts.insert(at);
return at;
}
static void timeout_update(AvahiTimeout * at, const struct timeval *tv)
{
debug(std::cout << "avahi_timeout_update " << at << std::endl << std::flush;);
if (tv != 0) {
at->m_tv = *tv;
} else {
at->m_tv.tv_sec = 0;
}
}
static void timeout_free(AvahiTimeout * at)
{
debug(std::cout << "avahi_timeout_free " << at << std::endl << std::endl << std::flush;);
at->m_publisher->m_avahiTimeouts.erase(at);
delete at;
}
CommMDNSPublisher::CommMDNSPublisher(CommServer & svr) : Idle(svr),
CommSocket(svr),
m_avahiFd(-1),
m_group(0)
{
}
CommMDNSPublisher::~CommMDNSPublisher()
{
// Finalise and delete
}
int CommMDNSPublisher::setup()
{
AvahiPoll poll = { this,
watch_new,
watch_update,
watch_get_events,
watch_free,
timeout_new,
timeout_update,
timeout_free
};
m_avahiClient = avahi_client_new(&poll, (AvahiClientFlags)0, &avahi_client_callback, this, &m_avahiError);
if (m_avahiClient == 0) {
log(ERROR, "Avahi client creation failed");
return -1;
}
if (m_avahiFd == -1) {
log(ERROR, "Avahi client has not registed a file descriptor");
return -1;
}
return 0;
}
void CommMDNSPublisher::setup_service(AvahiClient * client)
{
if (m_group == 0) {
m_group = avahi_entry_group_new(client, &group_callback, this);
}
if (m_group == 0) {
log(ERROR, String::compose("Avahi group creation failure. %1",
avahi_strerror(avahi_client_errno(client))));
return;
}
AvahiStringList * txt;
txt = avahi_string_list_new(String::compose("builddate=%1", std::string(consts::buildTime)+", "+std::string(consts::buildDate)).c_str(),
String::compose("clients=%1", m_commServer.m_server.getClients()).c_str(),
String::compose("ruleset=%1", m_commServer.m_server.getRuleset()).c_str(),
String::compose("server=%1", "cyphesis").c_str(),
String::compose("uptime=%1", m_commServer.m_server.m_world.upTime()).c_str(),
String::compose("version=%1", std::string(consts::version)).c_str(),
NULL);
int ret;
ret = avahi_entry_group_add_service_strlst(m_group,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags)0,
m_commServer.m_server.getName().c_str(),
"_worldforge._tcp", NULL, NULL,
client_port_num, txt);
avahi_string_list_free(txt);
if (ret < 0) {
log(ERROR, "Avahi service publish failed");
return;
}
ret = avahi_entry_group_commit(m_group);
if (ret < 0) {
log(ERROR, "Avahi service commit failed");
return;
}
}
void CommMDNSPublisher::idle(time_t t)
{
std::set<AvahiTimeout *>::const_iterator I = m_avahiTimeouts.begin();
std::set<AvahiTimeout *>::const_iterator Iend = m_avahiTimeouts.end();
for (; I != Iend; ++I) {
if ((*I)->m_tv.tv_sec == 0) {
continue;
}
if ((*I)->m_tv.tv_sec <= t) {
debug(std::cout << "TImeout " << (*I) << " is now due" << std::endl << std::flush;);
(*I)->m_callback(*I, (*I)->m_userdata);
(*I)->m_tv.tv_sec = 0;
}
}
}
int CommMDNSPublisher::getFd() const
{
return m_avahiFd;
}
bool CommMDNSPublisher::isOpen() const
{
return m_avahiFd != -1;
}
bool CommMDNSPublisher::eof()
{
return false;
}
int CommMDNSPublisher::read()
{
assert(m_avahiWatch != 0);
m_avahiWatch->m_events = AVAHI_WATCH_IN;
m_avahiWatch->m_callback(m_avahiWatch, m_avahiFd, AVAHI_WATCH_IN, m_avahiWatch->m_userdata);
m_avahiWatch->m_events = (AvahiWatchEvent)0;
return 0;
}
void CommMDNSPublisher::dispatch()
{
}
#endif // defined(HAVE_LIBHOWL)
|