Choose a different Port when 655 isn't available when doing "tinc join".
This commit is contained in:
		
							parent
							
								
									8f2db4afdd
								
							
						
					
					
						commit
						37cca72e6c
					
				
					 3 changed files with 35 additions and 28 deletions
				
			
		|  | @ -695,6 +695,8 @@ ask_netname: | ||||||
| 	ecdsa_free(key); | 	ecdsa_free(key); | ||||||
| 	rsa_free(rsa); | 	rsa_free(rsa); | ||||||
| 
 | 
 | ||||||
|  | 	check_port(name); | ||||||
|  | 
 | ||||||
| 	fprintf(stderr, "Invitation succesfully accepted.\n"); | 	fprintf(stderr, "Invitation succesfully accepted.\n"); | ||||||
| 	shutdown(sock, SHUT_RDWR); | 	shutdown(sock, SHUT_RDWR); | ||||||
| 	success = true; | 	success = true; | ||||||
|  |  | ||||||
|  | @ -1686,6 +1686,37 @@ static bool try_bind(int port) { | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int check_port(char *name) { | ||||||
|  | 	if(try_bind(655)) | ||||||
|  | 		return 655; | ||||||
|  | 
 | ||||||
|  | 	fprintf(stderr, "Warning: could not bind to port 655. "); | ||||||
|  | 
 | ||||||
|  | 	srand(time(NULL)); | ||||||
|  | 
 | ||||||
|  | 	for(int i = 0; i < 100; i++) { | ||||||
|  | 		int port = 0x1000 + (rand() & 0x7fff); | ||||||
|  | 		if(try_bind(port)) { | ||||||
|  | 			char *filename; | ||||||
|  | 			xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name); | ||||||
|  | 			FILE *f = fopen(filename, "a"); | ||||||
|  | 			free(filename); | ||||||
|  | 			if(!f) { | ||||||
|  | 				fprintf(stderr, "Please change tinc's Port manually.\n"); | ||||||
|  | 				return 0; | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			fprintf(f, "Port = %d\n", port); | ||||||
|  | 			fclose(f); | ||||||
|  | 			fprintf(stderr, "Tinc will instead listen on port %d.\n", port); | ||||||
|  | 			return port; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	fprintf(stderr, "Please change tinc's Port manually.\n"); | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| static int cmd_init(int argc, char *argv[]) { | static int cmd_init(int argc, char *argv[]) { | ||||||
| 	if(!access(tinc_conf, F_OK)) { | 	if(!access(tinc_conf, F_OK)) { | ||||||
| 		fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf); | 		fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf); | ||||||
|  | @ -1754,34 +1785,7 @@ static int cmd_init(int argc, char *argv[]) { | ||||||
| 	if(!rsa_keygen(2048, false) || !ecdsa_keygen(false)) | 	if(!rsa_keygen(2048, false) || !ecdsa_keygen(false)) | ||||||
| 		return 1; | 		return 1; | ||||||
| 
 | 
 | ||||||
| 
 | 	check_port(name); | ||||||
| 	if(!try_bind(655)) { |  | ||||||
| 		srand(time(NULL)); |  | ||||||
| 		int port = 0; |  | ||||||
| 		for(int i = 0; i < 100; i++) { |  | ||||||
| 			port = 0x1000 + (rand() & 0x7fff); |  | ||||||
| 			if(try_bind(port)) |  | ||||||
| 				break; |  | ||||||
| 			port = 0; |  | ||||||
| 		} |  | ||||||
| 		if(port) { |  | ||||||
| 			char *filename; |  | ||||||
| 			xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name); |  | ||||||
| 			FILE *f = fopen(filename, "a"); |  | ||||||
| 			free(filename); |  | ||||||
| 			if(!f) { |  | ||||||
| 				port = 0; |  | ||||||
| 			} else { |  | ||||||
| 				fprintf(f, "Port = %d\n", port); |  | ||||||
| 				fclose(f); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		if(!port) |  | ||||||
| 			fprintf(stderr, "Warning: could not bind to port 655. Please change tinc's Port manually.\n"); |  | ||||||
| 		else |  | ||||||
| 			fprintf(stderr, "Warning: could not bind to port 655. Tinc will instead listen on port %d.\n", port); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| #ifndef HAVE_MINGW | #ifndef HAVE_MINGW | ||||||
| 	char *filename; | 	char *filename; | ||||||
|  |  | ||||||
|  | @ -47,6 +47,7 @@ extern char *get_my_name(bool verbose); | ||||||
| extern bool connect_tincd(bool verbose); | extern bool connect_tincd(bool verbose); | ||||||
| extern bool sendline(int fd, char *format, ...); | extern bool sendline(int fd, char *format, ...); | ||||||
| extern bool recvline(int fd, char *line, size_t len); | extern bool recvline(int fd, char *line, size_t len); | ||||||
|  | extern int check_port(char *name); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue