From: Guus Sliepen Date: Sun, 22 Sep 2019 14:08:48 +0000 (+0200) Subject: Ensure Catta gets a valid service name. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=7b8fe1b56e0fa4258931b3cdba66b6edf0dc6b21;p=meshlink Ensure Catta gets a valid service name. 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. --- diff --git a/src/discovery.c b/src/discovery.c index 481b2f28..72ff8bc5 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -349,7 +349,21 @@ static void *discovery_loop(void *userdata) { 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) { @@ -357,7 +371,7 @@ static void *discovery_loop(void *userdata) { 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())) {