+ // asserts
+ assert(mesh != NULL);
+ assert(mesh->catta_poll == NULL);
+ assert(mesh->catta_server == NULL);
+ assert(mesh->catta_browser == NULL);
+ assert(mesh->discovery_threadstarted == false);
+ assert(mesh->catta_servicetype == NULL);
+
+ // handle catta logs
+ catta_set_log_function(discovery_log_cb);
+
+ // create service type string
+ size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(mesh->appname) + 1;
+ mesh->catta_servicetype = malloc(servicetype_strlen);
+
+ if(mesh->catta_servicetype == NULL) {
+ logger(mesh, MESHLINK_ERROR, "Failed to allocate memory for service type string.\n");
+ goto fail;
+ }
+
+ snprintf(mesh->catta_servicetype, servicetype_strlen, MESHLINK_MDNS_SERVICE_TYPE, mesh->appname);
+
+ // Allocate discovery loop object
+ if(!(mesh->catta_poll = catta_simple_poll_new())) {
+ logger(mesh, MESHLINK_ERROR, "Failed to create discovery poll object.\n");
+ goto fail;
+ }
+
+ // generate some unique host name (we actually do not care about it)
+ char hostname[17];
+ generate_rand_string(hostname, sizeof(hostname));
+
+ // Let's set the host name for this server.
+ CattaServerConfig config;
+ catta_server_config_init(&config);
+ config.host_name = catta_strdup(hostname);
+ config.publish_workstation = 0;
+ config.disallow_other_stacks = 0;
+ config.publish_hinfo = 0;
+ config.publish_addresses = 1;
+ config.publish_no_reverse = 1;
+
+ /* Allocate a new server */
+ int error;
+ mesh->catta_server = catta_server_new(catta_simple_poll_get(mesh->catta_poll), &config, discovery_server_callback, mesh, &error);
+
+ /* Free the configuration data */
+ catta_server_config_free(&config);
+
+ /* Check wether creating the server object succeeded */
+ if(!mesh->catta_server) {
+ logger(mesh, MESHLINK_ERROR, "Failed to create discovery server: %s\n", catta_strerror(error));
+ goto fail;
+ }
+
+ // Create the service browser
+ if(!(mesh->catta_browser = catta_s_service_browser_new(mesh->catta_server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, mesh->catta_servicetype, NULL, 0, discovery_browse_callback, mesh))) {
+ logger(mesh, MESHLINK_ERROR, "Failed to create discovery service browser: %s\n", catta_strerror(catta_server_errno(mesh->catta_server)));
+ goto fail;
+ }