return false;
}
-#if 0
-
- // TODO: check this?
if(mesh->name && strcmp(mesh->name, name)) {
logger(NULL, MESHLINK_ERROR, "Configuration is for a different name (%s)!", name);
meshlink_errno = MESHLINK_ESTORAGE;
return false;
}
-#endif
-
free(mesh->name);
mesh->name = name;
xasprintf(&mesh->myport, "%u", myport);
return NULL;
}
- if(!name || !*name) {
- logger(NULL, MESHLINK_ERROR, "No name given!\n");
- meshlink_errno = MESHLINK_EINVAL;
- return NULL;
- };
-
- if(!check_id(name)) {
+ if(name && !check_id(name)) {
logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
meshlink_errno = MESHLINK_EINVAL;
return NULL;
meshlink_open_params_t *params = xzalloc(sizeof * params);
params->confbase = xstrdup(confbase);
- params->name = xstrdup(name);
+ params->name = name ? xstrdup(name) : NULL;
params->appname = xstrdup(appname);
params->devclass = devclass;
params->netns = -1;
}
meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) {
+ if(!name) {
+ logger(NULL, MESHLINK_ERROR, "No name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(!check_id(name)) {
+ logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(!appname || !*appname) {
+ logger(NULL, MESHLINK_ERROR, "No appname given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(strchr(appname, ' ')) {
+ logger(NULL, MESHLINK_ERROR, "Invalid appname given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
+ logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
/* Create a temporary struct on the stack, to avoid allocating and freeing one. */
meshlink_open_params_t params;
memset(¶ms, 0, sizeof(params));
}
meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) {
- // Validate arguments provided by the application
- bool usingname = false;
-
logger(NULL, MESHLINK_DEBUG, "meshlink_open called\n");
+ // Validate arguments provided by the application
if(!params->appname || !*params->appname) {
logger(NULL, MESHLINK_ERROR, "No appname given!\n");
meshlink_errno = MESHLINK_EINVAL;
return NULL;
}
- if(!params->name || !*params->name) {
- logger(NULL, MESHLINK_ERROR, "No name given!\n");
- //return NULL;
- } else { //check name only if there is a name != NULL
-
- if(!check_id(params->name)) {
- logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
- meshlink_errno = MESHLINK_EINVAL;
- return NULL;
- } else {
- usingname = true;
- }
+ if(params->name && !check_id(params->name)) {
+ logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
}
if(params->devclass < 0 || params->devclass >= DEV_CLASS_COUNT) {
memcpy(mesh->dev_class_traits, default_class_traits, sizeof(default_class_traits));
- if(usingname) {
- mesh->name = xstrdup(params->name);
- }
+ mesh->name = params->name ? xstrdup(params->name) : NULL;
// Hash the key
if(params->key) {
// If no configuration exists yet, create it.
if(!meshlink_confbase_exists(mesh)) {
+ if(!mesh->name) {
+ logger(NULL, MESHLINK_ERROR, "No configuration files found!\n");
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return NULL;
+ }
+
if(!meshlink_setup(mesh)) {
logger(NULL, MESHLINK_ERROR, "Cannot create initial configuration\n");
meshlink_close(mesh);
* After the function returns, the application is free to overwrite or free @a confbase.
* @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.
+ * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
* @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.
* @param devclass The device class which will be used in the mesh.
* After the function returns, the application is free to overwrite or free @a confbase.
* @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.
+ * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
* @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.
* @param devclass The device class which will be used in the mesh.
* After the function returns, the application is free to overwrite or free @a confbase.
* @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.
+ * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
* @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.
* @param devclass The device class which will be used in the mesh.
int main() {
meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
- // Open a new meshlink instance.
+ // Check that the first time we need to supply a name
assert(meshlink_destroy("basic_conf"));
- meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
+
+ meshlink_handle_t *mesh = meshlink_open("basic_conf", NULL, "basic", DEV_CLASS_BACKBONE);
+ assert(!mesh);
+
+ // Open a new meshlink instance.
+
+ mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
assert(mesh);
// Check that we can't open a second instance of the same node.
meshlink_close(mesh);
mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
+ assert(!mesh);
+
+ // Open it without providing a name
+
+ mesh = meshlink_open("basic_conf", NULL, "basic", DEV_CLASS_BACKBONE);
assert(mesh);
self = meshlink_get_self(mesh);
assert(self);
+ assert(!strcmp(mesh->name, "foo"));
+ assert(!strcmp(self->name, "foo"));
// Check that we remembered we were reachable