-static void usage() {
- fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name);
- fprintf(stderr, "Valid options are:\n"
- " -d, --datagram Enable datagram mode.\n"
- " -l, --packet-loss RATE Fake packet loss of RATE percent.\n"
- "\n");
- fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
+static void usage(void) {
+ static const char *message =
+ "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n"
+ "\n"
+ "Valid options are:\n"
+ " -d, --datagram Enable datagram mode.\n"
+ " -q, --quit Quit when EOF occurs on stdin.\n"
+ " -r, --readonly Only send data from the socket to stdout.\n"
+#ifdef HAVE_LINUX
+ " -t, --tun Use a tun device instead of stdio.\n"
+#endif
+ " -w, --writeonly Only send data from stdin to the socket.\n"
+ " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
+ " -R, --replay-window N Set replay window to N bytes.\n"
+ " -s, --special Enable special handling of lines starting with #, ^ and $.\n"
+ " -v, --verbose Display debug messages.\n"
+ " -4 Use IPv4.\n"
+ " -6 Use IPv6.\n"
+ "\n"
+ "Report bugs to tinc@tinc-vpn.org.\n";
+
+ fprintf(stderr, message, program_name);
+}
+
+#ifdef HAVE_MINGW
+
+int stdin_sock_fd = -1;
+
+// Windows does not allow calling select() on anything but sockets. Therefore,
+// to keep the same code as on other operating systems, we have to put a
+// separate thread between the stdin and the sptps loop way below. This thread
+// reads stdin and sends its content to the main thread through a TCP socket,
+// which can be properly select()'ed.
+static void *stdin_reader_thread(void *arg) {
+ struct sockaddr_in sa;
+ socklen_t sa_size = sizeof(sa);
+
+ while(true) {
+ int peer_fd = accept(stdin_sock_fd, (struct sockaddr *) &sa, &sa_size);
+
+ if(peer_fd < 0) {
+ fprintf(stderr, "accept() failed: %s\n", strerror(errno));
+ continue;
+ }
+
+ if(verbose) {
+ fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port));
+ }
+
+ char buf[1024];
+ ssize_t nread;
+
+ while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
+ if(verbose) {
+ fprintf(stderr, "Read %lld bytes from input\n", nread);
+ }
+
+ char *start = buf;
+ ssize_t nleft = nread;
+
+ while(nleft) {
+ ssize_t nsend = send(peer_fd, start, nleft, 0);
+
+ if(nsend < 0) {
+ if(sockwouldblock(sockerrno)) {
+ continue;
+ }
+
+ break;
+ }
+
+ start += nsend;
+ nleft -= nsend;
+ }
+
+ if(nleft) {
+ fprintf(stderr, "Could not send data: %s\n", strerror(errno));
+ break;
+ }
+
+ if(verbose) {
+ fprintf(stderr, "Sent %lld bytes to peer\n", nread);
+ }
+ }
+
+ closesocket(peer_fd);
+ }
+
+ closesocket(stdin_sock_fd);
+ stdin_sock_fd = -1;
+ return NULL;