From cf5a9d0ff6ef59cd130a865ce1be44b4e8b8471a Mon Sep 17 00:00:00 2001 From: Niklas Hofmann Date: Fri, 8 Aug 2014 12:35:05 +0200 Subject: [PATCH] appname added, which is used by mdns as part of the service type --- examples/chat.c | 2 +- examples/chatpp.cc | 2 +- examples/manynodes.c | 2 +- examples/meshlinkapp.c | 2 +- src/discovery.c | 38 +++++++++++++++++++++++++++++++------- src/meshlink++.h | 5 +++-- src/meshlink.c | 9 ++++++++- src/meshlink.h | 4 +++- src/meshlink_internal.h | 3 +++ 9 files changed, 52 insertions(+), 15 deletions(-) diff --git a/examples/chat.c b/examples/chat.c index ace1e783..070600f4 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -182,7 +182,7 @@ int main(int argc, char *argv[]) { 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; diff --git a/examples/chatpp.cc b/examples/chatpp.cc index f21e96de..608367a9 100644 --- a/examples/chatpp.cc +++ b/examples/chatpp.cc @@ -182,7 +182,7 @@ int main(int argc, char *argv[]) { 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; diff --git a/examples/manynodes.c b/examples/manynodes.c index 86883ea1..e2b1630c 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -222,7 +222,7 @@ int main(int argc, char *argv[]) { 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]) { diff --git a/examples/meshlinkapp.c b/examples/meshlinkapp.c index f506c964..85cdf6fe 100644 --- a/examples/meshlinkapp.c +++ b/examples/meshlinkapp.c @@ -14,7 +14,7 @@ int main(int argc , char **argv){ 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); diff --git a/src/discovery.c b/src/discovery.c index ea1f6b73..7a7f1483 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -17,7 +17,7 @@ #include -#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" @@ -66,6 +66,7 @@ static void discovery_create_services(meshlink_handle_t *mesh) assert(mesh->myport != NULL); assert(mesh->avahi_server != NULL); assert(mesh->avahi_poll != NULL); + assert(mesh->avahi_servicetype != NULL); fprintf(stderr, "Adding service\n"); @@ -96,7 +97,7 @@ static void discovery_create_services(meshlink_handle_t *mesh) /* 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; @@ -332,8 +333,8 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde { 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: { @@ -395,6 +396,19 @@ bool discovery_start(meshlink_handle_t *mesh) 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())) @@ -435,7 +449,7 @@ bool discovery_start(meshlink_handle_t *mesh) } // 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; @@ -454,24 +468,30 @@ bool discovery_start(meshlink_handle_t *mesh) 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; } @@ -483,6 +503,7 @@ void discovery_stop(meshlink_handle_t *mesh) 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); @@ -499,4 +520,7 @@ void discovery_stop(meshlink_handle_t *mesh) avahi_simple_poll_free(mesh->avahi_poll); mesh->avahi_poll = NULL; + + free(mesh->avahi_servicetype); + mesh->avahi_servicetype = NULL; } diff --git a/src/meshlink++.h b/src/meshlink++.h index 1d6c9861..2bc1f8b0 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -373,11 +373,12 @@ namespace meshlink { * * @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. diff --git a/src/meshlink.c b/src/meshlink.c index 1e87c3ab..33af6c36 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -743,7 +743,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) { 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; @@ -753,6 +753,12 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) { 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; @@ -768,6 +774,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) { 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); diff --git a/src/meshlink.h b/src/meshlink.h index 169052e8..37fe5540 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -114,11 +114,13 @@ extern const char *meshlink_strerror(meshlink_errno_t err); * 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 diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 52137efb..8ac270be 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -67,6 +67,8 @@ struct meshlink_handle { char *confbase; + char *appname; + meshlink_receive_cb_t receive_cb; meshlink_node_status_cb_t node_status_cb; meshlink_log_cb_t log_cb; @@ -138,6 +140,7 @@ struct meshlink_handle { struct AvahiSServiceBrowser *avahi_browser; struct AvahiSimplePoll *avahi_poll; struct AvahiSEntryGroup *avahi_group; + char* avahi_servicetype; }; /// A handle for a MeshLink node. -- 2.39.2