return meshlink_set_port(handle, port);
}
+ /// Set the timeout for invitations.
+ /** This function sets the timeout for invitations.
+ * The timeout is retroactively applied to all outstanding invitations.
+ *
+ * @param timeout The timeout for invitations in seconds.
+ */
+ void set_invitation_timeout(int timeout) {
+ meshlink_set_invitation_timeout(handle, timeout);
+ }
+
/// Invite another node into the mesh.
/** This function generates an invitation that can be used by another node to join the same mesh as the local node.
* The generated invitation is a string containing a URL.
mesh->appname = xstrdup(appname);
mesh->devclass = devclass;
mesh->discovery = true;
+ mesh->invitation_timeout = 604800; // 1 week
if(usingname) {
mesh->name = xstrdup(name);
return rval;
}
+void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout) {
+ mesh->invitation_timeout = timeout;
+}
+
char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
extern bool meshlink_set_port(meshlink_handle_t *mesh, int port);
+/// Set the timeout for invitations.
+/** This function sets the timeout for invitations.
+ * Note that timeouts are only checked at the time a node tries to join using an invitation.
+ * The default timeout for invitations is 1 week.
+ *
+ * @param mesh A handle which represents an instance of MeshLink.
+ * @param timeout The timeout for invitations in seconds.
+ */
+extern void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout);
+
/// Invite another node into the mesh.
/** This function generates an invitation that can be used by another node to join the same mesh as the local node.
* The generated invitation is a string containing a URL.
hash_t *node_udp_cache;
struct connection_t *everyone;
struct ecdsa *invitation_key;
+ int invitation_timeout;
int pinginterval; /* seconds between pings */
int pingtimeout; /* seconds to wait for response */
if(!f) {
logger(mesh, MESHLINK_ERROR, "Error trying to open invitation %s\n", cookie);
+ unlink(usedname);
+ return false;
+ }
+
+ // Check the timestamp
+ struct stat st;
+
+ if(fstat(fileno(f), &st)) {
+ logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", usedname);
+ fclose(f);
+ unlink(usedname);
+ return false;
+ }
+
+ if(time(NULL) > st.st_mtime + mesh->invitation_timeout) {
+ logger(mesh, MESHLINK_ERROR, "Peer %s tried to use an outdated invitation file %s\n", c->name, usedname);
+ fclose(f);
+ unlink(usedname);
return false;
}
return 1;
}
+ meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
+
+ if(!mesh3) {
+ fprintf(stderr, "Could not initialize configuration for quux\n");
+ return 1;
+ }
+
// Disable local discovery.
meshlink_enable_discovery(mesh1, false);
meshlink_enable_discovery(mesh2, false);
+ meshlink_enable_discovery(mesh3, false);
- // Start the first instance and have it generate an invitation.
+ // Start the first instance and have it generate invitations.
meshlink_set_node_status_cb(mesh1, status_cb);
}
meshlink_add_address(mesh1, "localhost");
- char *url = meshlink_invite(mesh1, "baz");
+ char *baz_url = meshlink_invite(mesh1, "baz");
- if(!url) {
+ if(!baz_url) {
fprintf(stderr, "Foo could not generate an invitation for baz\n");
return 1;
}
+ char *quux_url = meshlink_invite(mesh1, "quux");
+
+ if(!quux_url) {
+ fprintf(stderr, "Foo could not generate an invitation for quux\n");
+ return 1;
+ }
+
// Have the second instance join the first.
- if(!meshlink_join(mesh2, url)) {
+ if(!meshlink_join(mesh2, baz_url)) {
fprintf(stderr, "Baz could not join foo's mesh\n");
return 1;
}
- free(url);
+ free(baz_url);
if(!meshlink_start(mesh2)) {
fprintf(stderr, "Baz could not start\n");
return 1;
}
+ // Check that nodes cannot join with expired invitations
+
+ meshlink_set_invitation_timeout(mesh1, 0);
+
+ if(meshlink_join(mesh3, quux_url)) {
+ fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n");
+ return 1;
+ }
+
+ free(quux_url);
+
// Clean up.
meshlink_stop(mesh2);