]> git.meshlink.io Git - meshlink/commitdiff
Ensure Catta gets a valid service name.
authorGuus Sliepen <guus@meshlink.io>
Sun, 22 Sep 2019 14:08:48 +0000 (16:08 +0200)
committerGuus Sliepen <guus@meshlink.io>
Sun, 22 Sep 2019 14:08:48 +0000 (16:08 +0200)
Normally the appname is used as the service name as well. However,
Catta's rules are that the service name must be at least two characters,
and only contain alphanumeric characters, dashses and underscores.

src/discovery.c

index 481b2f2872f22675d909924fea9b61d89f342189..72ff8bc5b5c6b57adae057e4c6db0fdb8d12d7f2 100644 (file)
@@ -349,7 +349,21 @@ static void *discovery_loop(void *userdata) {
        catta_set_log_function(discovery_log_cb);
 
        // create service type string
        catta_set_log_function(discovery_log_cb);
 
        // create service type string
-       size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(mesh->appname) + 1;
+       char appname[strlen(mesh->appname) + 2];
+       strcpy(appname, mesh->appname);
+
+       for(char *p = appname; *p; p++) {
+               if(!isalnum(*p) && *p != '_' && *p != '-') {
+                       *p = '_';
+               }
+       }
+
+       if(!appname[1]) {
+               appname[1] = '_';
+               appname[2] = '\0';
+       }
+
+       size_t servicetype_strlen = sizeof(MESHLINK_MDNS_SERVICE_TYPE) + strlen(appname) + 1;
        mesh->catta_servicetype = malloc(servicetype_strlen);
 
        if(mesh->catta_servicetype == NULL) {
        mesh->catta_servicetype = malloc(servicetype_strlen);
 
        if(mesh->catta_servicetype == NULL) {
@@ -357,7 +371,7 @@ static void *discovery_loop(void *userdata) {
                goto fail;
        }
 
                goto fail;
        }
 
-       snprintf(mesh->catta_servicetype, servicetype_strlen, MESHLINK_MDNS_SERVICE_TYPE, mesh->appname);
+       snprintf(mesh->catta_servicetype, servicetype_strlen, MESHLINK_MDNS_SERVICE_TYPE, appname);
 
        // Allocate discovery loop object
        if(!(mesh->catta_poll = catta_simple_poll_new())) {
 
        // Allocate discovery loop object
        if(!(mesh->catta_poll = catta_simple_poll_new())) {