Skip to content

Commit cf30891

Browse files
committed
feat: windows socket support
1 parent 9095651 commit cf30891

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

tailscale.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
// SPDX-License-Identifier: BSD-3-Clause
33

44
#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+
614
#include <stdio.h>
715
#include <unistd.h>
816

@@ -47,27 +55,46 @@ int tailscale_listen(tailscale sd, const char* network, const char* addr, tailsc
4755
}
4856

4957
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);
6758

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
7198
}
7299

73100
int tailscale_set_dir(tailscale sd, const char* dir) {

tailscale.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ func TsnetListen(sd C.int, network, addr *C.char, listenerOut *C.int) C.int {
255255
}
256256

257257
func newConn(s *server, netConn net.Conn, connOut *C.int) error {
258+
259+
// TODO https://github.com/ncm/selectable-socketpair/blob/master/socketpair.c
258260
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
259261
if err != nil {
260262
return err

tailscale_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package main
22

33
import (
4+
"github.com/tailscale/libtailscale/tsnetctest"
45
"testing"
56
"time"
6-
7-
"github.com/tailscale/libtailscale/tsnetctest"
87
)
98

109
func TestConn(t *testing.T) {

0 commit comments

Comments
 (0)