+ if(!DeleteService(service)) {
+ fprintf(stderr, "Could not remove %s service: %s\n", identname, winerror(GetLastError()));
+ return false;
+ }
+
+ fprintf(stderr, "%s service removed\n", identname);
+
+ return true;
+}
+#endif
+
+static bool connect_tincd() {
+ if(fd >= 0)
+ return true;
+
+ FILE *f = fopen(pidfilename, "r");
+ if(!f) {
+ fprintf(stderr, "Could not open pid file %s: %s\n", pidfilename, strerror(errno));
+ return false;
+ }
+
+ char host[128];
+ char port[128];
+
+ if(fscanf(f, "%20d %1024s %128s port %128s", &pid, controlcookie, host, port) != 4) {
+ fprintf(stderr, "Could not parse pid file %s\n", pidfilename);
+ return false;
+ }
+
+#ifdef HAVE_MINGW
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return false;
+ }
+#endif
+
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ .ai_protocol = IPPROTO_TCP,
+ .ai_flags = 0,
+ };
+
+ struct addrinfo *res = NULL;
+
+ if(getaddrinfo(host, port, &hints, &res) || !res) {
+ fprintf(stderr, "Cannot resolve %s port %s: %s", host, port, strerror(errno));
+ return false;
+ }
+
+ fd = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ if(fd < 0) {
+ fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno));
+ return false;
+ }
+
+#ifdef HAVE_MINGW
+ unsigned long arg = 0;
+
+ if(ioctlsocket(fd, FIONBIO, &arg) != 0) {
+ fprintf(stderr, "ioctlsocket failed: %s", sockstrerror(sockerrno));
+ }
+#endif
+
+ if(connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
+ fprintf(stderr, "Cannot connect to %s port %s: %s\n", host, port, sockstrerror(sockerrno));
+ return false;
+ }
+
+ freeaddrinfo(res);
+
+ char data[4096];
+ int version;
+
+ if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %s %d", &code, data, &version) != 3 || code != 0) {
+ fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno));
+ return false;
+ }
+
+ sendline(fd, "%d ^%s %d", ID, controlcookie, TINC_CTL_VERSION_CURRENT);
+
+ if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &version, &pid) != 3 || code != 4 || version != TINC_CTL_VERSION_CURRENT) {
+ fprintf(stderr, "Could not fully establish control socket connection\n");
+ return false;
+ }
+
+ return true;
+}
+
+
+static int cmd_start(int argc, char *argv[]) {
+ int i, j;
+ char *c;
+ char *slash = strrchr(argv[0], '/');
+
+#ifdef HAVE_MINGW
+ if ((c = strrchr(argv[0], '\\')) > slash)
+ slash = c;
+#endif
+
+ if (slash++) {
+ c = xmalloc((slash - argv[0]) + sizeof("tincd"));
+ sprintf(c, "%.*stincd", (int)(slash - argv[0]), argv[0]);
+ }
+ else
+ c = "tincd";
+
+ argv[0] = c;
+
+ for(i = j = 1; argv[i]; ++i)
+ if (i != optind && strcmp(argv[i], "--") != 0)
+ argv[j++] = argv[i];
+
+ argv[j] = NULL;
+ execvp(c, argv);
+ fprintf(stderr, "Could not start %s: %s\n", c, strerror(errno));
+ return 1;
+}
+
+static int cmd_stop(int argc, char *argv[]) {
+#ifndef HAVE_MINGW
+ if(!connect_tincd())
+ return 1;
+
+ sendline(fd, "%d %d", CONTROL, REQ_STOP);
+ if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_STOP || result) {
+ fprintf(stderr, "Could not stop tinc daemon.\n");
+ return 1;
+ }
+#else
+ if(!remove_service())
+ return 1;
+#endif
+ return 0;
+}
+
+static int cmd_restart(int argc, char *argv[]) {
+ return cmd_stop(argc, argv) ?: cmd_start(argc, argv);
+}
+
+static int cmd_reload(int argc, char *argv[]) {
+ if(!connect_tincd())
+ return 1;
+
+ sendline(fd, "%d %d", CONTROL, REQ_RELOAD);
+ if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RELOAD || result) {
+ fprintf(stderr, "Could not reload configuration.\n");
+ return 1;
+ }
+
+ return 0;
+
+}
+
+static int cmd_dump(int argc, char *argv[]) {
+ if(argc != 2) {
+ fprintf(stderr, "Invalid number of arguments.\n");
+ usage(true);
+ return 1;
+ }
+
+ bool do_graph = false;
+
+ if(!strcasecmp(argv[1], "nodes"))
+ sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
+ else if(!strcasecmp(argv[1], "edges"))
+ sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
+ else if(!strcasecmp(argv[1], "subnets"))
+ sendline(fd, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
+ else if(!strcasecmp(argv[1], "connections"))
+ sendline(fd, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
+ else if(!strcasecmp(argv[1], "graph")) {
+ sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
+ sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
+ do_graph = true;
+ } else {
+ fprintf(stderr, "Unknown dump type '%s'.\n", argv[1]);
+ usage(true);
+ return 1;
+ }
+
+ if(!connect_tincd())
+ return 1;
+
+ if(do_graph)
+ printf("digraph {\n");
+
+ while(recvline(fd, line, sizeof line)) {
+ char node1[4096], node2[4096];
+ int n = sscanf(line, "%d %d %s to %s", &code, &req, node1, node2);
+ if(n == 2) {
+ if(do_graph && req == REQ_DUMP_NODES)
+ continue;
+ else {
+ if(do_graph)
+ printf("}\n");
+ return 0;
+ }