#include <avahi-core/core.h>
#include <avahi-core/lookup.h>
#include <avahi-core/publish.h>
+#include <avahi-core/log.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/alternative.h>
assert(mesh->avahi_server != NULL);
assert(mesh->avahi_poll != NULL);
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
/* Called whenever the entry group state changes */
switch(state)
{
case AVAHI_ENTRY_GROUP_REGISTERING:
;
}
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
assert(mesh->avahi_servicetype != NULL);
assert(mesh->self != NULL);
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
logger(mesh, MESHLINK_DEBUG, "Adding service\n");
/* Ifthis is the first time we're called, let's create a new entry group */
done:
if(txt_name)
{ free(txt_name); }
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
static void discovery_server_callback(AvahiServer *server, AvahiServerState state, void * userdata)
// asserts
assert(mesh != NULL);
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
switch(state)
{
{
logger(mesh, MESHLINK_ERROR, "Avahi failed to set new host name: %s\n", avahi_strerror(result));
avahi_simple_poll_quit(mesh->avahi_poll);
- return;
}
}
break;
case AVAHI_SERVER_INVALID:
break;
}
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfIndex interface, AvahiProtocol protocol, 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, void* userdata)
assert(mesh != NULL);
assert(mesh->avahi_server != NULL);
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
/* Called whenever a service has been resolved successfully or timed out */
switch(event)
{
}
avahi_s_service_resolver_free(resolver);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata)
assert(mesh->avahi_server != NULL);
assert(mesh->avahi_poll != NULL);
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
/* Called whenever a new services becomes available on the LAN or is removed from the LAN */
switch (event)
{
logger(mesh, MESHLINK_ERROR, "(Browser) %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server)));
avahi_simple_poll_quit(mesh->avahi_poll);
}
- return;
+ break;
case AVAHI_BROWSER_NEW:
{
}
break;
}
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
}
static void *discovery_loop(void *userdata)
return NULL;
}
+static void discovery_log_cb(AvahiLogLevel level, const char *txt)
+{
+ meshlink_log_level_t mlevel = MESHLINK_CRITICAL;
+
+ switch(level)
+ {
+ case AVAHI_LOG_ERROR:
+ mlevel = MESHLINK_ERROR;
+ break;
+
+ case AVAHI_LOG_WARN:
+ mlevel = MESHLINK_WARNING;
+ break;
+
+ case AVAHI_LOG_NOTICE:
+ case AVAHI_LOG_INFO:
+ mlevel = MESHLINK_INFO;
+ break;
+
+ case AVAHI_LOG_DEBUG:
+ mlevel = MESHLINK_DEBUG;
+ break;
+ }
+
+ logger(NULL, mlevel, "%s\n", txt);
+}
+
bool discovery_start(meshlink_handle_t *mesh)
{
logger(mesh, MESHLINK_DEBUG, "discovery_start called\n");
assert(mesh->discovery_threadstarted == false);
assert(mesh->avahi_servicetype == NULL);
+ // handle avahi logs
+ avahi_set_log_function(discovery_log_cb);
+
// create service type string
size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(mesh->appname) + 1;
mesh->avahi_servicetype = malloc(servicetype_strlen);