#include <avahi-common/error.h>
#include <avahi-common/domain.h>
#include <avahi-common/alternative.h>
+
#include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+#include <avahi-client/lookup.h>
#include "warn.h"
#include "dns_sd.h"
enum {
COMMAND_POLL = 'p',
COMMAND_QUIT = 'q',
- COMMAND_POLL_DONE = 'P'
+ COMMAND_POLL_DONE = 'P',
+ COMMAND_POLL_FAILED = 'F'
};
struct _DNSServiceRef_t {
switch (command) {
- case COMMAND_POLL:
+ case COMMAND_POLL: {
+ int ret;
ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
-
- if (avahi_simple_poll_run(sdref->simple_poll) < 0) {
- fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n");
- ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
+
+ for (;;) {
+ errno = 0;
+
+ if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) {
+
+ if (errno == EINTR)
+ continue;
+
+ fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno));
+ }
+
break;
}
ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
- if (write_command(sdref->thread_fd, COMMAND_POLL_DONE) < 0)
+ if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0)
break;
break;
+ }
case COMMAND_QUIT:
return NULL;
ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
- ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, NULL));
+ ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
sdref->thread_running = 0;
DNSServiceRefDeallocate(sdref);
return ret;
-xb
- }
+}
static void service_resolver_callback(
AvahiServiceResolver *r,
void *userdata) {
DNSServiceRef sdref = userdata;
- char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
assert(r);
assert(sdref);
assert(sdref->n_ref >= 1);
- host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
-
switch (event) {
case AVAHI_RESOLVER_FOUND: {
+ char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
char full_name[AVAHI_DOMAIN_NAME_MAX];
int ret;
char *p = NULL;
size_t l = 0;
+ host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
+
if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
avahi_string_list_serialize(txt, p, l);
case AVAHI_RESOLVER_FAILURE:
sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
-
+ break;
}
}
return;
switch (state) {
- case AVAHI_CLIENT_DISCONNECTED: {
+ case AVAHI_CLIENT_DISCONNECTED:
reg_report_error(sdref, kDNSServiceErr_NoError);
break;
- }
case AVAHI_CLIENT_S_RUNNING: {
int ret;
/* If the service name is taken from the host name, copy that */
avahi_free(sdref->service_name_chosen);
+ sdref->service_name_chosen = NULL;
if (!(n = avahi_client_get_host_name(sdref->client))) {
reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
sdref->service_port = ntohs(port);
- sdref->service_txt = txtRecord ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
+ sdref->service_txt = txtRecord && txtLen > 0 ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
+
+ /* Some OOM checking would be cool here */
ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));