Defer handling netname conflicts when accepting an invitation.
In case no explicit netname of configuration directory is specified when accepting an invitation, the netname specified in the invitation data is used. However, this new netname is only known after making the connection to the server. If the new netname conflicts with an existing one at the client, we ask the user for a netname that doesn't conflict. However, we should first finish accepting the invitation, so we don't run into the problem that the server times out and cancels the invitation. So, we create a random netname and store the files there, and only after we finish accepting the invitation we ask the user for a better netname, and then just rename the temporary directory to the final name.
This commit is contained in:
parent
d47c79533f
commit
a1f4f14c6c
2 changed files with 33 additions and 12 deletions
|
@ -490,6 +490,9 @@ static bool finalize_join(void) {
|
||||||
if(!netname)
|
if(!netname)
|
||||||
netname = grep(data, "NetName");
|
netname = grep(data, "NetName");
|
||||||
|
|
||||||
|
bool ask_netname = false;
|
||||||
|
char temp_netname[32];
|
||||||
|
|
||||||
make_names:
|
make_names:
|
||||||
if(!confbasegiven) {
|
if(!confbasegiven) {
|
||||||
free(confbase);
|
free(confbase);
|
||||||
|
@ -508,17 +511,11 @@ make_names:
|
||||||
fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
|
fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
|
||||||
if(!tty || confbasegiven)
|
if(!tty || confbasegiven)
|
||||||
return false;
|
return false;
|
||||||
ask_netname:
|
|
||||||
fprintf(stderr, "Enter a new netname: ");
|
|
||||||
if(!fgets(line, sizeof line, stdin)) {
|
|
||||||
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if(!*line || *line == '\n')
|
|
||||||
goto ask_netname;
|
|
||||||
|
|
||||||
line[strlen(line) - 1] = 0;
|
// Generate a random netname, ask for a better one later.
|
||||||
netname = line;
|
ask_netname = true;
|
||||||
|
snprintf(temp_netname, sizeof temp_netname, "join_%x", rand());
|
||||||
|
netname = temp_netname;
|
||||||
goto make_names;
|
goto make_names;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -701,6 +698,31 @@ ask_netname:
|
||||||
shutdown(sock, SHUT_RDWR);
|
shutdown(sock, SHUT_RDWR);
|
||||||
success = true;
|
success = true;
|
||||||
|
|
||||||
|
ask_netname:
|
||||||
|
if(ask_netname) {
|
||||||
|
fprintf(stderr, "Enter a new netname: ");
|
||||||
|
if(!fgets(line, sizeof line, stdin)) {
|
||||||
|
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!*line || *line == '\n')
|
||||||
|
goto ask_netname;
|
||||||
|
|
||||||
|
line[strlen(line) - 1] = 0;
|
||||||
|
|
||||||
|
char *newbase;
|
||||||
|
xasprintf(&newbase, CONFDIR SLASH "tinc" SLASH "%s", line);
|
||||||
|
if(rename(confbase, newbase)) {
|
||||||
|
fprintf(stderr, "Error trying to rename %s to %s: %s\n", confbase, newbase, strerror(errno));
|
||||||
|
free(newbase);
|
||||||
|
goto ask_netname;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(newbase);
|
||||||
|
netname = line;
|
||||||
|
make_names();
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1692,8 +1692,6 @@ int check_port(char *name) {
|
||||||
|
|
||||||
fprintf(stderr, "Warning: could not bind to port 655. ");
|
fprintf(stderr, "Warning: could not bind to port 655. ");
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
for(int i = 0; i < 100; i++) {
|
for(int i = 0; i < 100; i++) {
|
||||||
int port = 0x1000 + (rand() & 0x7fff);
|
int port = 0x1000 + (rand() & 0x7fff);
|
||||||
if(try_bind(port)) {
|
if(try_bind(port)) {
|
||||||
|
@ -2376,6 +2374,7 @@ int main(int argc, char *argv[]) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
crypto_init();
|
crypto_init();
|
||||||
|
|
||||||
tty = isatty(0) && isatty(1);
|
tty = isatty(0) && isatty(1);
|
||||||
|
|
Loading…
Reference in a new issue