nut/tools/nut-scanner/scan_avahi.c

594 lines
17 KiB
C
Raw Normal View History

2016-07-18 00:11:41 +00:00
/*
* Copyright (C) 2011 - EATON
2011-09-29 18:14:46 +00:00
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2016-07-18 00:11:41 +00:00
/*! \file scan_avahi.c
\brief detect NUT through Avahi mDNS / DNS-SD services
\author Frederic Bohe <fredericbohe@eaton.com>
*/
2011-09-29 18:14:46 +00:00
#include "common.h"
#include "nut-scan.h"
2012-01-24 10:22:33 +00:00
#ifdef WITH_AVAHI
2011-09-29 18:14:46 +00:00
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
2012-01-24 10:22:33 +00:00
#include "timehead.h"
2011-09-29 18:14:46 +00:00
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
2012-01-24 10:22:33 +00:00
#include <ltdl.h>
/* dynamic link library stuff */
static lt_dlhandle dl_handle = NULL;
static const char *dl_error = NULL;
static AvahiClient* (*nut_avahi_service_browser_get_client)(AvahiServiceBrowser *);
static int (*nut_avahi_simple_poll_loop)(AvahiSimplePoll *s);
static void (*nut_avahi_client_free)(AvahiClient *client);
static int (*nut_avahi_client_errno)(AvahiClient*);
static void (*nut_avahi_free)(void *p);
static void (*nut_avahi_simple_poll_quit)(AvahiSimplePoll *s);
static AvahiClient* (*nut_avahi_client_new)(
const AvahiPoll *poll_api,
AvahiClientFlags flags,
AvahiClientCallback callback,
void *userdata,
int *error);
static void (*nut_avahi_simple_poll_free)(AvahiSimplePoll *s);
static AvahiServiceResolver * (*nut_avahi_service_resolver_new)(
AvahiClient *client,
AvahiIfIndex interface,
AvahiProtocol protocol,
const char *name,
const char *type,
const char *domain,
AvahiProtocol aprotocol,
AvahiLookupFlags flags,
AvahiServiceResolverCallback callback,
void *userdata);
static const char * (*nut_avahi_strerror)(int error);
static AvahiClient* (*nut_avahi_service_resolver_get_client)(AvahiServiceResolver *);
static AvahiServiceBrowser* (*nut_avahi_service_browser_new)(
AvahiClient *client,
AvahiIfIndex interface,
AvahiProtocol protocol,
const char *type,
const char *domain,
AvahiLookupFlags flags,
AvahiServiceBrowserCallback callback,
void *userdata);
static int (*nut_avahi_service_resolver_free)(AvahiServiceResolver *r);
static AvahiSimplePoll *(*nut_avahi_simple_poll_new)(void);
static char* (*nut_avahi_string_list_to_string)(AvahiStringList *l);
static int (*nut_avahi_service_browser_free)(AvahiServiceBrowser *);
static char * (*nut_avahi_address_snprint)(char *ret_s, size_t length, const AvahiAddress *a);
static const AvahiPoll* (*nut_avahi_simple_poll_get)(AvahiSimplePoll *s);
2022-06-29 10:37:36 +00:00
/* return 0 on error; visible externally */
int nutscan_load_avahi_library(const char *libname_path);
2016-07-18 00:11:41 +00:00
int nutscan_load_avahi_library(const char *libname_path)
2012-01-24 10:22:33 +00:00
{
2022-06-29 10:37:36 +00:00
if (dl_handle != NULL) {
2016-07-18 00:11:41 +00:00
/* if previous init failed */
2022-06-29 10:37:36 +00:00
if (dl_handle == (void *)1) {
return 0;
2016-07-18 00:11:41 +00:00
}
/* init has already been done */
return 1;
}
if (libname_path == NULL) {
fprintf(stderr, "AVAHI client library not found. AVAHI search disabled.\n");
return 0;
}
2022-06-29 10:37:36 +00:00
if (lt_dlinit() != 0) {
2016-07-18 00:11:41 +00:00
fprintf(stderr, "Error initializing lt_init\n");
return 0;
}
dl_handle = lt_dlopen(libname_path);
if (!dl_handle) {
dl_error = lt_dlerror();
goto err;
}
lt_dlerror(); /* Clear any existing error */
*(void **) (&nut_avahi_service_browser_get_client) = lt_dlsym(dl_handle, "avahi_service_browser_get_client");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_simple_poll_loop) = lt_dlsym(dl_handle, "avahi_simple_poll_loop");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_client_free) = lt_dlsym(dl_handle, "avahi_client_free");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_client_errno) = lt_dlsym(dl_handle, "avahi_client_errno");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_free) = lt_dlsym(dl_handle, "avahi_free");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_simple_poll_quit) = lt_dlsym(dl_handle, "avahi_simple_poll_quit");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_client_new) = lt_dlsym(dl_handle, "avahi_client_new");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_simple_poll_free) = lt_dlsym(dl_handle, "avahi_simple_poll_free");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_service_resolver_new) = lt_dlsym(dl_handle, "avahi_service_resolver_new");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_strerror) = lt_dlsym(dl_handle, "avahi_strerror");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_service_resolver_get_client) = lt_dlsym(dl_handle, "avahi_service_resolver_get_client");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_service_browser_new) = lt_dlsym(dl_handle, "avahi_service_browser_new");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_service_resolver_free) = lt_dlsym(dl_handle, "avahi_service_resolver_free");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_simple_poll_new) = lt_dlsym(dl_handle, "avahi_simple_poll_new");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_string_list_to_string) = lt_dlsym(dl_handle, "avahi_string_list_to_string");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_service_browser_free) = lt_dlsym(dl_handle, "avahi_service_browser_free");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_address_snprint) = lt_dlsym(dl_handle, "avahi_address_snprint");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
*(void **) (&nut_avahi_simple_poll_get) = lt_dlsym(dl_handle, "avahi_simple_poll_get");
2022-06-29 10:37:36 +00:00
if ((dl_error = lt_dlerror()) != NULL) {
2016-07-18 00:11:41 +00:00
goto err;
}
return 1;
2012-01-24 10:22:33 +00:00
err:
2022-06-29 10:37:36 +00:00
fprintf(stderr,
"Cannot load AVAHI library (%s) : %s. AVAHI search disabled.\n",
libname_path, dl_error);
2016-07-18 00:11:41 +00:00
dl_handle = (void *)1;
2012-06-01 13:55:19 +00:00
lt_dlexit();
2016-07-18 00:11:41 +00:00
return 0;
2012-01-24 10:22:33 +00:00
}
/* end of dynamic link library stuff */
2011-09-29 18:14:46 +00:00
static AvahiSimplePoll *simple_poll = NULL;
static nutscan_device_t * dev_ret = NULL;
2022-06-29 10:37:36 +00:00
static useconds_t avahi_usec_timeout = 0;
2011-09-29 18:14:46 +00:00
2022-06-29 10:37:36 +00:00
static void update_device(const char * host_name, const char *ip, uint16_t port, char * text, int proto)
2011-09-29 18:14:46 +00:00
{
nutscan_device_t * dev = NULL;
char * t = NULL;
char * t_saveptr = NULL;
char * phrase = NULL;
char * phrase_saveptr = NULL;
char * word = NULL;
char * value = NULL;
char * device = NULL;
char * device_saveptr = NULL;
int device_found = 0;
char buf[6];
2022-06-29 10:37:36 +00:00
size_t buf_size;
2011-09-29 18:14:46 +00:00
2022-06-29 10:37:36 +00:00
if (text == NULL) {
2011-09-29 18:14:46 +00:00
return;
}
t = strdup(text);
2022-06-29 10:37:36 +00:00
phrase = strtok_r(t, "\"", &t_saveptr);
while (phrase != NULL) {
word = strtok_r(phrase, "=", &phrase_saveptr);
if (word == NULL) {
phrase = strtok_r(NULL, "\"", &t_saveptr);
2011-09-29 18:14:46 +00:00
continue;
}
2022-06-29 10:37:36 +00:00
value = strtok_r(NULL, "=", &phrase_saveptr);
if (value == NULL) {
phrase = strtok_r(NULL, "\"", &t_saveptr);
2011-09-29 18:14:46 +00:00
continue;
}
2022-06-29 10:37:36 +00:00
if (strcmp(word, "device_list") != 0) {
phrase = strtok_r(NULL, "\"", &t_saveptr);
2011-09-29 18:14:46 +00:00
continue;
}
2022-06-29 10:37:36 +00:00
device = strtok_r(value, ";", &device_saveptr);
while (device != NULL) {
2011-09-29 18:14:46 +00:00
device_found = 1;
dev = nutscan_new_device();
dev->type = TYPE_NUT;
dev->driver = strdup("nutclient");
2022-06-29 10:37:36 +00:00
if (proto == AVAHI_PROTO_INET) {
nutscan_add_option_to_device(dev, "desc", "IPv4");
2011-09-29 18:14:46 +00:00
}
2022-06-29 10:37:36 +00:00
if (proto == AVAHI_PROTO_INET6) {
nutscan_add_option_to_device(dev, "desc", "IPv6");
2011-09-29 18:14:46 +00:00
}
2022-06-29 10:37:36 +00:00
if (port != PORT) {
/* +5+1+1+1 is for :
2011-09-29 18:14:46 +00:00
- port number (max 65535 so 5 characters),
- '@' and ':' characters
- terminating 0 */
2022-06-29 10:37:36 +00:00
buf_size = strlen(device) +
strlen(host_name) +
5 + 1 + 1 + 1;
dev->port = malloc(buf_size);
if (dev->port) {
snprintf(dev->port, buf_size, "%s@%s:%u",
device, host_name, port);
2011-09-29 18:14:46 +00:00
}
}
else {
/*+1+1 is for '@' character and terminating 0 */
2022-06-29 10:37:36 +00:00
buf_size = strlen(device) + strlen(host_name) + 1 + 1;
dev->port = malloc(buf_size);
if (dev->port) {
snprintf(dev->port, buf_size, "%s@%s",
device, host_name);
2011-09-29 18:14:46 +00:00
}
}
2022-06-29 10:37:36 +00:00
if (dev->port) {
dev_ret = nutscan_add_device_to_device(dev_ret, dev);
2011-09-29 18:14:46 +00:00
}
else {
nutscan_free_device(dev);
}
2022-06-29 10:37:36 +00:00
device = strtok_r(NULL, ";", &device_saveptr);
}
2011-09-29 18:14:46 +00:00
2022-06-29 10:37:36 +00:00
phrase = strtok_r(NULL, "\"", &t_saveptr);
}
2011-09-29 18:14:46 +00:00
free(t);
/* If no device published in avahi data, try to get the device by
connecting directly to upsd */
2022-06-29 10:37:36 +00:00
if (!device_found) {
snprintf(buf, sizeof(buf), "%u", port);
dev = nutscan_scan_nut(ip, ip, buf, avahi_usec_timeout);
if (dev) {
dev_ret = nutscan_add_device_to_device(dev_ret, dev);
2011-09-29 18:14:46 +00:00
}
/* add an upsd entry without associated device */
else {
dev = nutscan_new_device();
dev->type = TYPE_NUT;
dev->driver = strdup("nutclient");
2022-06-29 10:37:36 +00:00
if (proto == AVAHI_PROTO_INET) {
nutscan_add_option_to_device(dev, "desc", "IPv4");
2011-09-29 18:14:46 +00:00
}
2022-06-29 10:37:36 +00:00
if (proto == AVAHI_PROTO_INET6) {
nutscan_add_option_to_device(dev, "desc", "IPv6");
2011-09-29 18:14:46 +00:00
}
2022-06-29 10:37:36 +00:00
if (port != PORT) {
2011-09-29 18:14:46 +00:00
/*+1+1 is for ':' character and terminating 0 */
/*buf is the string containing the port number*/
2022-06-29 10:37:36 +00:00
buf_size = strlen(host_name) + strlen(buf) + 1 + 1;
dev->port = malloc(buf_size);
if (dev->port) {
snprintf(dev->port, buf_size, "%s:%s",
host_name, buf);
2011-09-29 18:14:46 +00:00
}
}
else {
2022-06-29 10:37:36 +00:00
dev->port = strdup(host_name);
2011-09-29 18:14:46 +00:00
}
2022-06-29 10:37:36 +00:00
if (dev->port) {
dev_ret = nutscan_add_device_to_device(dev_ret, dev);
2011-09-29 18:14:46 +00:00
}
else {
nutscan_free_device(dev);
}
}
}
}
static void resolve_callback(
AvahiServiceResolver *r,
2022-06-29 10:37:36 +00:00
AvahiIfIndex interface,
AvahiProtocol protocol,
2011-09-29 18:14:46 +00:00
AvahiResolverEvent event,
const char *name,
const char *type,
const char *domain,
const char *host_name,
const AvahiAddress *address,
uint16_t port,
AvahiStringList *txt,
AvahiLookupResultFlags flags,
2022-06-29 10:37:36 +00:00
void* userdata)
{
2011-09-29 18:14:46 +00:00
assert(r);
2022-06-29 10:37:36 +00:00
NUT_UNUSED_VARIABLE(interface);
NUT_UNUSED_VARIABLE(protocol);
NUT_UNUSED_VARIABLE(userdata);
2011-09-29 18:14:46 +00:00
/* Called whenever a service has been resolved successfully or timed out */
switch (event) {
case AVAHI_RESOLVER_FAILURE:
2022-06-29 10:37:36 +00:00
fprintf(stderr,
"(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n",
name, type, domain,
(*nut_avahi_strerror)((*nut_avahi_client_errno)((*nut_avahi_service_resolver_get_client)(r))));
2011-09-29 18:14:46 +00:00
break;
case AVAHI_RESOLVER_FOUND: {
char a[AVAHI_ADDRESS_STR_MAX], *t;
/* fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain); */
2012-01-24 10:22:33 +00:00
(*nut_avahi_address_snprint)(a, sizeof(a), address);
t = (*nut_avahi_string_list_to_string)(txt);
2022-06-29 10:37:36 +00:00
NUT_UNUSED_VARIABLE(flags);
2011-09-29 18:14:46 +00:00
/*
fprintf(stderr,
"\t%s:%u (%s)\n"
"\tTXT=%s\n"
"\tcookie is %u\n"
"\tis_local: %i\n"
"\tour_own: %i\n"
"\twide_area: %i\n"
"\tmulticast: %i\n"
"\tcached: %i\n",
host_name, port, a,
t,
avahi_string_list_get_service_cookie(txt),
!!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
!!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
!!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
!!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
!!(flags & AVAHI_LOOKUP_RESULT_CACHED));
*/
2022-06-29 10:37:36 +00:00
update_device(host_name, a, port, t, address->proto);
2012-01-24 10:22:33 +00:00
(*nut_avahi_free)(t);
2011-09-29 18:14:46 +00:00
}
}
2012-01-24 10:22:33 +00:00
(*nut_avahi_service_resolver_free)(r);
2011-09-29 18:14:46 +00:00
}
static void browse_callback(
AvahiServiceBrowser *b,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char *name,
const char *type,
const char *domain,
2022-06-29 10:37:36 +00:00
AvahiLookupResultFlags flags,
void* userdata)
{
2011-09-29 18:14:46 +00:00
AvahiClient *c = userdata;
assert(b);
2022-06-29 10:37:36 +00:00
NUT_UNUSED_VARIABLE(flags);
2011-09-29 18:14:46 +00:00
/* Called whenever a new services becomes available on the LAN or is removed from the LAN */
switch (event) {
case AVAHI_BROWSER_FAILURE:
2022-06-29 10:37:36 +00:00
fprintf(stderr,
"(Browser) %s\n",
(*nut_avahi_strerror)((*nut_avahi_client_errno)((*nut_avahi_service_browser_get_client)(b))));
2012-01-24 10:22:33 +00:00
(*nut_avahi_simple_poll_quit)(simple_poll);
2011-09-29 18:14:46 +00:00
return;
case AVAHI_BROWSER_NEW:
/* fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain); */
/* We ignore the returned resolver object. In the callback
function we free it. If the server is terminated before
the callback function is called the server will free
the resolver for us. */
2022-06-29 10:37:36 +00:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_ASSIGN_ENUM)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wassign-enum"
#endif
/* It seems that avahi-common/defs.h only defines the flags in a
* manner similar to bitmask flags to request certain features,
* but lacks a value in that enum for lack of flags (unconstrained
* lookup). So we have to silence a warning here...
*/
2012-01-24 10:22:33 +00:00
if (!((*nut_avahi_service_resolver_new)(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
2022-06-29 10:37:36 +00:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_ASSIGN_ENUM)
# pragma GCC diagnostic pop
#endif
fprintf(stderr,
"Failed to resolve service '%s': %s\n",
name, (*nut_avahi_strerror)((*nut_avahi_client_errno)(c)));
2011-09-29 18:14:46 +00:00
break;
case AVAHI_BROWSER_REMOVE:
fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
2012-01-24 10:22:33 +00:00
(*nut_avahi_simple_poll_quit)(simple_poll);
2022-06-29 10:37:36 +00:00
goto fallthrough_AVAHI_BROWSER_CACHE_EXHAUSTED; /* be explicit */
2011-09-29 18:14:46 +00:00
case AVAHI_BROWSER_CACHE_EXHAUSTED:
2022-06-29 10:37:36 +00:00
fallthrough_AVAHI_BROWSER_CACHE_EXHAUSTED:
2011-09-29 18:14:46 +00:00
/* fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW"); */
break;
}
}
2022-06-29 10:37:36 +00:00
static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
2011-09-29 18:14:46 +00:00
assert(c);
2022-06-29 10:37:36 +00:00
NUT_UNUSED_VARIABLE(userdata);
2011-09-29 18:14:46 +00:00
/* Called whenever the client or server state changes */
if (state == AVAHI_CLIENT_FAILURE) {
2022-06-29 10:37:36 +00:00
fprintf(stderr,
"Server connection failure: %s\n",
(*nut_avahi_strerror)((*nut_avahi_client_errno)(c)));
2012-01-24 10:22:33 +00:00
(*nut_avahi_simple_poll_quit)(simple_poll);
2011-09-29 18:14:46 +00:00
}
}
2022-06-29 10:37:36 +00:00
nutscan_device_t * nutscan_scan_avahi(useconds_t usec_timeout)
2011-09-29 18:14:46 +00:00
{
2022-06-29 10:37:36 +00:00
/* Example service publication
2011-09-29 18:14:46 +00:00
* $ avahi-publish -s nut _upsd._tcp 3493 txtvers=1 protovers=1.0.0 device_list="dev1;dev2"
*/
AvahiClient *client = NULL;
AvahiServiceBrowser *sb = NULL;
int error;
2012-01-24 10:22:33 +00:00
2022-06-29 10:37:36 +00:00
if (!nutscan_avail_avahi) {
2012-01-24 10:22:33 +00:00
return NULL;
}
2011-09-29 18:14:46 +00:00
avahi_usec_timeout = usec_timeout;
/* Allocate main loop object */
2012-01-24 10:22:33 +00:00
if (!(simple_poll = (*nut_avahi_simple_poll_new)())) {
2011-09-29 18:14:46 +00:00
fprintf(stderr, "Failed to create simple poll object.\n");
goto fail;
}
/* Allocate a new client */
2022-06-29 10:37:36 +00:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_ASSIGN_ENUM)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wassign-enum"
#endif
/* It seems that avahi-common/defs.h only defines the flags in a
* manner similar to bitmask flags to request certain features,
* but lacks a value in that enum for lack of flags (unconstrained
* lookup). So we have to silence a warning here...
*/
2012-01-24 10:22:33 +00:00
client = (*nut_avahi_client_new)((*nut_avahi_simple_poll_get)(simple_poll), 0, client_callback, NULL, &error);
2022-06-29 10:37:36 +00:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_ASSIGN_ENUM)
# pragma GCC diagnostic pop
#endif
2011-09-29 18:14:46 +00:00
/* Check wether creating the client object succeeded */
if (!client) {
2022-06-29 10:37:36 +00:00
fprintf(stderr,
"Failed to create client: %s\n",
(*nut_avahi_strerror)(error));
2011-09-29 18:14:46 +00:00
goto fail;
}
/* Create the service browser */
2022-06-29 10:37:36 +00:00
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_ASSIGN_ENUM)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wassign-enum"
#endif
/* See comments about flags just a bit above */
if (!(sb = (*nut_avahi_service_browser_new)(
client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
"_upsd._tcp", NULL, 0, browse_callback, client))
) {
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_ASSIGN_ENUM)
# pragma GCC diagnostic pop
#endif
fprintf(stderr,
"Failed to create service browser: %s\n",
(*nut_avahi_strerror)((*nut_avahi_client_errno)(client)));
2011-09-29 18:14:46 +00:00
goto fail;
}
/* Run the main loop */
2012-01-24 10:22:33 +00:00
(*nut_avahi_simple_poll_loop)(simple_poll);
2011-09-29 18:14:46 +00:00
fail:
/* Cleanup things */
if (sb)
2012-01-24 10:22:33 +00:00
(*nut_avahi_service_browser_free)(sb);
2011-09-29 18:14:46 +00:00
if (client)
2012-01-24 10:22:33 +00:00
(*nut_avahi_client_free)(client);
2011-09-29 18:14:46 +00:00
if (simple_poll)
2012-01-24 10:22:33 +00:00
(*nut_avahi_simple_poll_free)(simple_poll);
2011-09-29 18:14:46 +00:00
2013-11-24 15:00:12 +00:00
return nutscan_rewind_device(dev_ret);
2011-09-29 18:14:46 +00:00
}
2012-01-24 10:22:33 +00:00
#else /* WITH_AVAHI */
/* stub function */
2022-06-29 10:37:36 +00:00
nutscan_device_t * nutscan_scan_avahi(useconds_t usec_timeout)
2012-01-24 10:22:33 +00:00
{
2022-06-29 10:37:36 +00:00
NUT_UNUSED_VARIABLE(usec_timeout);
2012-01-24 10:22:33 +00:00
return NULL;
}
#endif /* WITH_AVAHI */