+ if(!mesh || !data) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ if(strncmp(data, "Name = ", 7)) {
+ fprintf(stderr, "Invalid data\n");
+ meshlink_errno = MESHLINK_EPEER;
+ return false;
+ }
+
+ char *end = strchr(data + 7, '\n');
+ if(!end) {
+ fprintf(stderr, "Invalid data\n");
+ meshlink_errno = MESHLINK_EPEER;
+ return false;
+ }
+
+ int len = end - (data + 7);
+ char name[len + 1];
+ memcpy(name, data + 7, len);
+ name[len] = 0;
+ if(!check_id(name)) {
+ fprintf(stderr, "Invalid Name\n");
+ meshlink_errno = MESHLINK_EPEER;
+ return false;
+ }
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+ if(!access(filename, F_OK)) {
+ fprintf(stderr, "File %s already exists, not importing\n", filename);
+ meshlink_errno = MESHLINK_EEXIST;
+ return false;
+ }
+
+ if(errno != ENOENT) {
+ fprintf(stderr, "Error accessing %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ FILE *f = fopen(filename, "w");
+ if(!f) {
+ fprintf(stderr, "Could not create %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ fwrite(end + 1, strlen(end + 1), 1, f);
+ fclose(f);
+
+ load_all_nodes(mesh);
+
+ return true;