#include "xalloc.h"
#include "random.h"
#include "pidfile.h"
+#include "fs.h"
#include "ed25519/sha512.h"
return 1;
}
+ if(argc > 2) {
+ fprintf(stderr, "Too many arguments!\n");
+ return 1;
+ }
+
// Check validity of the new node's name
if(!check_id(argv[1])) {
fprintf(stderr, "Invalid name for node.\n");
}
}
- snprintf(filename, sizeof(filename), "%s" SLASH "invitations", confbase);
-
- if(mkdir(filename, 0700) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
- return 1;
+ if(!makedirs(DIR_INVITATIONS)) {
+ return false;
}
+ snprintf(filename, sizeof(filename), "%s" SLASH "invitations", confbase);
+
// Count the number of valid invitations, clean up old ones
DIR *dir = opendir(filename);
goto make_names;
}
- if(mkdir(confbase, 0777) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
- return false;
- }
-
- if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
+ if(!makedirs(DIR_HOSTS | DIR_CONFBASE | DIR_CACHE)) {
return false;
}
return true;
}
+bool wait_socket_recv(int fd) {
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+
+ struct timeval tv = {.tv_sec = 5};
+
+ if(select(fd + 1, &fds, NULL, NULL, &tv) != 1) {
+ fprintf(stderr, "Timed out waiting for the server to reply.\n");
+ return false;
+ }
+
+ return true;
+}
+
int cmd_join(int argc, char *argv[]) {
free(data);
data = NULL;
}
// Make sure confbase exists and is accessible.
- if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
- return 1;
- }
-
- if(mkdir(confbase, 0777) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
- return 1;
+ if(!makedirs(DIR_CONFDIR | DIR_CONFBASE)) {
+ return false;
}
if(access(confbase, R_OK | W_OK | X_OK)) {
if(!fgets(line, sizeof(line), stdin)) {
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
- return false;
+ return 1;
}
invitation = line;
freeaddrinfo(ai);
free(b64_pubkey);
ecdsa_free(key);
+ fprintf(stderr, "Could not connect to inviter. Please make sure the URL you entered is valid.\n");
return 1;
}
}
}
if(memcmp(hishash, hash, 18)) {
- fprintf(stderr, "Peer has an invalid key!\n%s\n", line + 2);
+ fprintf(stderr, "Peer has an invalid key. Please make sure you're using the correct URL.\n%s\n", line + 2);
ecdsa_free(key);
return 1;
goto exit;
}
- while((len = recv(sock, line, sizeof(line), 0))) {
+ while(wait_socket_recv(sock) && (len = recv(sock, line, sizeof(line), 0))) {
if(len < 0) {
if(sockwouldblock(sockerrno)) {
continue;
closesocket(sock);
if(!success) {
- fprintf(stderr, "Invitation cancelled.\n");
+ fprintf(stderr, "Invitation cancelled. Please try again and contact the inviter for assistance if this error persists.\n");
return 1;
}