|
2 | 2 | // SPDX-License-Identifier: BSD-3-Clause
|
3 | 3 |
|
4 | 4 | #include "tailscale.h"
|
5 |
| -#include <sys/socket.h> |
| 5 | +#ifdef __APPLE__ || __linux__ |
| 6 | + #include <sys/socket.h> |
| 7 | +#elif _WIN32 |
| 8 | + #include <winsock2.h> |
| 9 | + #include <windows.h> |
| 10 | + #include <ws2tcpip.h> |
| 11 | +#else |
| 12 | +#endif |
| 13 | + |
6 | 14 | #include <stdio.h>
|
7 | 15 | #include <unistd.h>
|
8 | 16 |
|
@@ -47,27 +55,46 @@ int tailscale_listen(tailscale sd, const char* network, const char* addr, tailsc
|
47 | 55 | }
|
48 | 56 |
|
49 | 57 | int tailscale_accept(tailscale_listener ld, tailscale_conn* conn_out) {
|
50 |
| - struct msghdr msg = {0}; |
51 |
| - |
52 |
| - char mbuf[256]; |
53 |
| - struct iovec io = { .iov_base = mbuf, .iov_len = sizeof(mbuf) }; |
54 |
| - msg.msg_iov = &io; |
55 |
| - msg.msg_iovlen = 1; |
56 |
| - |
57 |
| - char cbuf[256]; |
58 |
| - msg.msg_control = cbuf; |
59 |
| - msg.msg_controllen = sizeof(cbuf); |
60 |
| - |
61 |
| - if (recvmsg(ld, &msg, 0) == -1) { |
62 |
| - return -1; |
63 |
| - } |
64 |
| - |
65 |
| - struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
66 |
| - unsigned char* data = CMSG_DATA(cmsg); |
67 | 58 |
|
68 |
| - int fd = *(int*)data; |
69 |
| - *conn_out = fd; |
70 |
| - return 0; |
| 59 | + #ifdef __APPLE__ || __linux__ |
| 60 | + struct msghdr msg = {0}; |
| 61 | + |
| 62 | + char mbuf[256]; |
| 63 | + struct iovec io = { .iov_base = mbuf, .iov_len = sizeof(mbuf) }; |
| 64 | + msg.msg_iov = &io; |
| 65 | + msg.msg_iovlen = 1; |
| 66 | + |
| 67 | + char cbuf[256]; |
| 68 | + msg.msg_control = cbuf; |
| 69 | + msg.msg_controllen = sizeof(cbuf); |
| 70 | + |
| 71 | + if (recvmsg(ld, &msg, 0) == -1) { |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 76 | + unsigned char* data = CMSG_DATA(cmsg); |
| 77 | + |
| 78 | + int fd = *(int*)data; |
| 79 | + *conn_out = fd; |
| 80 | + return 0; |
| 81 | + #elif _WIN32 |
| 82 | + SOCKET ConnectSocket = INVALID_SOCKET; |
| 83 | + int mbuflen=256; |
| 84 | + char mbuf[mbuflen]; |
| 85 | + int iResult; |
| 86 | + do { |
| 87 | + iResult = recv(ConnectSocket, mbuf, mbuflen, 0); |
| 88 | + if ( iResult > 0 ) |
| 89 | + printf("Bytes received: %d\n", iResult); |
| 90 | + else if ( iResult == 0 ) |
| 91 | + printf("Connection closed\n"); |
| 92 | + else |
| 93 | + printf("recv failed with error: %d\n", WSAGetLastError()); |
| 94 | + |
| 95 | + } while( iResult > 0 ); |
| 96 | + |
| 97 | + #endif |
71 | 98 | }
|
72 | 99 |
|
73 | 100 | int tailscale_set_dir(tailscale sd, const char* dir) {
|
|
0 commit comments