X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftincctl.c;h=6d7f03b877e0be11a69e81093c46b4bfa060616a;hb=af2e0c9a32642065aedd2e67ca1f5791ca7a407d;hp=5477fd0e4b4cbb2139b93fcb1e2f39b07b9197be;hpb=d772289f6d6adfb8932658b533349d43f08ec326;p=tinc diff --git a/src/tincctl.c b/src/tincctl.c index 5477fd0e..6d7f03b8 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -38,18 +38,13 @@ static bool show_help = false; /* If nonzero, print the version on standard output and exit. */ static bool show_version = false; -/* If nonzero, it will attempt to kill a running tincd and exit. */ -static int kill_tincd = 0; - -/* If nonzero, generate public/private keypair for this host/net. */ -static int generate_keys = 0; - static char *name = NULL; static char *identname = NULL; /* program name for syslog */ static char *controlcookiename = NULL; /* cookie file location */ static char controlcookie[1024]; char *netname = NULL; char *confbase = NULL; +static char *host = NULL; #ifdef HAVE_MINGW static struct WSAData wsa_state; @@ -57,6 +52,7 @@ static struct WSAData wsa_state; static struct option const long_options[] = { {"config", required_argument, NULL, 'c'}, + {"host", required_argument, NULL, 'h'}, {"net", required_argument, NULL, 'n'}, {"help", no_argument, NULL, 1}, {"version", no_argument, NULL, 2}, @@ -108,7 +104,7 @@ static bool parse_options(int argc, char **argv) { int r; int option_index = 0; - while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) { + while((r = getopt_long(argc, argv, "c:n:h:", long_options, &option_index)) != EOF) { switch (r) { case 0: /* long option */ break; @@ -117,6 +113,10 @@ static bool parse_options(int argc, char **argv) { confbase = xstrdup(optarg); break; + case 'h': /* alternative host to connect to */ + host = xstrdup(optarg); + break; + case 'n': /* net name given */ netname = xstrdup(optarg); break; @@ -159,7 +159,7 @@ FILE *ask_and_open(const char *filename, const char *what, const char *mode) { what, filename); fflush(stdout); - if(fgets(buf, sizeof buf, stdin) < 0) { + if(fgets(buf, sizeof buf, stdin) == NULL) { fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno)); return NULL; @@ -430,7 +430,7 @@ void pcap(int fd, FILE *out) { int main(int argc, char *argv[], char *envp[]) { int fd; int result; - int port; + char port[128]; int pid; program_name = argv[0]; @@ -488,7 +488,7 @@ int main(int argc, char *argv[], char *envp[]) { fprintf(stderr, "Could not open control socket cookie file %s: %s\n", controlcookiename, strerror(errno)); return 1; } - if(fscanf(f, "%1024s %d %d", controlcookie, &port, &pid) != 3) { + if(fscanf(f, "%1024s %128s %d", controlcookie, port, &pid) != 3) { fprintf(stderr, "Could not parse control socket cookie file %s\n", controlcookiename); return 1; } @@ -500,13 +500,21 @@ int main(int argc, char *argv[], char *envp[]) { } #endif - struct sockaddr_in addr; - memset(&addr, 0, sizeof addr); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(0x7f000001); - addr.sin_port = htons(port); + struct addrinfo hints = { + .ai_family = AF_UNSPEC, + .ai_socktype = SOCK_STREAM, + .ai_protocol = IPPROTO_TCP, + .ai_flags = 0, + }; + + struct addrinfo *res = NULL; - fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if(getaddrinfo(host, port, &hints, &res) || !res) { + fprintf(stderr, "Cannot resolve %s port %s: %s", host ?: "localhost", port, strerror(errno)); + return 1; + } + + fd = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP); if(fd < 0) { fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno)); return 1; @@ -520,12 +528,13 @@ int main(int argc, char *argv[], char *envp[]) { } #endif - if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) { - - fprintf(stderr, "Cannot connect to %s: %s\n", controlcookiename, sockstrerror(sockerrno)); + if(connect(fd, res->ai_addr, res->ai_addrlen) < 0) { + fprintf(stderr, "Cannot connect to %s port %s: %s\n", host ?: "localhost", port, sockstrerror(sockerrno)); return 1; } + freeaddrinfo(res); + char line[4096]; char data[4096]; int code, version, req;