if(argc > 2)
nick = argv[2];
- meshlink_handle_t *mesh = meshlink_open(confbase, nick);
+ meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat");
if(!mesh) {
fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
return 1;
if(argc > 2)
nick = argv[2];
- meshlink::mesh *mesh = meshlink::open(confbase, nick);
+ meshlink::mesh *mesh = meshlink::open(confbase, nick, "chatpp");
if(!mesh) {
fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
return 1;
snprintf(nodename, sizeof nodename, "node%d", i);
snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
bool itsnew = access(filename, R_OK);
- mesh[i] = meshlink_open(filename, nodename);
+ mesh[i] = meshlink_open(filename, nodename, "manynodes");
if(itsnew)
meshlink_add_address(mesh[i], "localhost");
if(!mesh[i]) {
meshlink_handle_t* myhandle;
- myhandle = meshlink_open(confbase, name);
+ myhandle = meshlink_open(confbase, name, "meshlinkapp");
//Register callback function for incoming data
meshlink_set_receive_cb(myhandle, (meshlink_receive_cb_t)handle_recv_data);
#include <uuid/uuid.h>
-#define MESHLINK_MDNS_SERVICE_TYPE "_meshlink._tcp"
+#define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp"
#define MESHLINK_MDNS_NAME_KEY "name"
#define MESHLINK_MDNS_FINGERPRINT_KEY "fingerprint"
assert(mesh->myport != NULL);
assert(mesh->avahi_server != NULL);
assert(mesh->avahi_poll != NULL);
+ assert(mesh->avahi_servicetype != NULL);
fprintf(stderr, "Adding service\n");
/* Add the service */
int ret = 0;
- if((ret = avahi_server_add_service(mesh->avahi_server, mesh->avahi_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, srvnamestr, MESHLINK_MDNS_SERVICE_TYPE, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0)
+ if((ret = avahi_server_add_service(mesh->avahi_server, mesh->avahi_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, srvnamestr, mesh->avahi_servicetype, NULL, NULL, atoi(mesh->myport), txt_name, txt_fingerprint, NULL)) < 0)
{
fprintf(stderr, "Failed to add service: %s\n", avahi_strerror(ret));
goto fail;
{
fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server)));
avahi_simple_poll_quit(mesh->avahi_poll);
- return;
}
+ return;
case AVAHI_BROWSER_NEW:
{
assert(mesh->avahi_server == NULL);
assert(mesh->avahi_browser == NULL);
assert(mesh->discovery_threadstarted == false);
+ assert(mesh->avahi_servicetype == NULL);
+
+ // create service type string
+ size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(mesh->appname) + 1;
+ mesh->avahi_servicetype = malloc(servicetype_strlen);
+
+ if(mesh->avahi_servicetype == NULL)
+ {
+ fprintf(stderr, "Failed to allocate memory for service type string.\n");
+ goto fail;
+ }
+
+ snprintf(mesh->avahi_servicetype, servicetype_strlen, MESHLINK_MDNS_SERVICE_TYPE, mesh->appname);
// Allocate discovery loop object
if(!(mesh->avahi_poll = avahi_simple_poll_new()))
}
// Create the service browser
- if(!(mesh->avahi_browser = avahi_s_service_browser_new(mesh->avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, MESHLINK_MDNS_SERVICE_TYPE, NULL, 0, discovery_browse_callback, mesh)))
+ if(!(mesh->avahi_browser = avahi_s_service_browser_new(mesh->avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, mesh->avahi_servicetype, NULL, 0, discovery_browse_callback, mesh)))
{
fprintf(stderr, "Failed to create discovery service browser: %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server)));
goto fail;
return true;
fail:
- if(mesh->avahi_browser)
+ if(mesh->avahi_browser != NULL)
{
avahi_s_service_browser_free(mesh->avahi_browser);
mesh->avahi_browser = NULL;
}
- if(mesh->avahi_server)
+ if(mesh->avahi_server != NULL)
{
avahi_server_free(mesh->avahi_server);
mesh->avahi_server = NULL;
}
- if(mesh->avahi_poll)
+ if(mesh->avahi_poll != NULL)
{
avahi_simple_poll_free(mesh->avahi_poll);
mesh->avahi_poll = NULL;
}
+ if(mesh->avahi_servicetype != NULL)
+ {
+ free(mesh->avahi_servicetype);
+ mesh->avahi_servicetype = NULL;
+ }
+
return false;
}
assert(mesh->avahi_server != NULL);
assert(mesh->avahi_browser != NULL);
assert(mesh->discovery_threadstarted == true);
+ assert(mesh->avahi_servicetype != NULL);
// Shut down
avahi_simple_poll_quit(mesh->avahi_poll);
avahi_simple_poll_free(mesh->avahi_poll);
mesh->avahi_poll = NULL;
+
+ free(mesh->avahi_servicetype);
+ mesh->avahi_servicetype = NULL;
}
*
* @param confbase The directory in which MeshLink will store its configuration files.
* @param name The name which this instance of the application will use in the mesh.
+ * @param appname The application name which will be used in the mesh.
*
* @return This function will return a pointer to a meshlink::mesh if MeshLink has succesfully set up its configuration files, NULL otherwise.
*/
- static mesh *open(const char *confbase, const char *name) {
- return (mesh *)meshlink_open(confbase, name);
+ static mesh *open(const char *confbase, const char *name, const char* appname) {
+ return (mesh *)meshlink_open(confbase, name, appname);
}
/// Close the MeshLink handle.
return true;
}
-meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname) {
// Validate arguments provided by the application
bool usingname = false;
return NULL;
}
+ if(!appname || !*appname) {
+ fprintf(stderr, "No appname given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
if(!name || !*name) {
fprintf(stderr, "No name given!\n");
//return NULL;
meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
mesh->confbase = xstrdup(confbase);
+ mesh->appname = xstrdup(appname);
if (usingname) mesh->name = xstrdup(name);
pthread_mutex_init ( &(mesh->outpacketqueue_mutex), NULL);
pthread_mutex_init ( &(mesh->nodes_mutex), NULL);
* After the function returns, the application is free to overwrite or free @a confbase @a.
* @param name The name which this instance of the application will use in the mesh.
* After the function returns, the application is free to overwrite or free @a name @a.
+ * @param appname The application name which will be used in the mesh.
+ * After the function returns, the application is free to overwrite or free @a name @a.
*
* @return A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
* The pointer is valid until meshlink_close() is called.
*/
-extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name);
+extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname);
/// Start MeshLink.
/** This function causes MeshLink to open network sockets, make outgoing connections, and
char *confbase;
+ char *appname;
+
meshlink_receive_cb_t receive_cb;
meshlink_node_status_cb_t node_status_cb;
meshlink_log_cb_t log_cb;
struct AvahiSServiceBrowser *avahi_browser;
struct AvahiSimplePoll *avahi_poll;
struct AvahiSEntryGroup *avahi_group;
+ char* avahi_servicetype;
};
/// A handle for a MeshLink node.