22// SPDX-License-Identifier: BSD-3-Clause
33
44#include "tailscale.h"
5- #ifdef __APPLE__ || __linux__
5+ #if defined( __APPLE__ ) || defined( __linux__ )
66#include <sys/socket.h>
77#elif _WIN32
88#include <winsock2.h>
99#include <windows.h>
1010#include <ws2tcpip.h>
1111#else
12+ #include <unistd.h>
1213#endif
1314
1415#include <stdio.h>
15- #include <unistd.h>
16+
1617
1718// Functions exported by Go.
1819extern int TsnetNewServer ();
@@ -63,7 +64,7 @@ int tailscale_listen(tailscale sd, const char *network, const char *addr, tailsc
6364int tailscale_accept (tailscale_listener ld , tailscale_conn * conn_out )
6465{
6566
66- #ifdef __APPLE__ || __linux__
67+ #if defined( __APPLE__ ) || defined( __linux__ )
6768 struct msghdr msg = {0 };
6869
6970 char mbuf [256 ];
@@ -87,30 +88,101 @@ int tailscale_accept(tailscale_listener ld, tailscale_conn *conn_out)
8788 * conn_out = fd ;
8889 return 0 ;
8990#elif _WIN32
90- char mbuf [256 ];
91- WSABUF wsaBuf ;
92- DWORD bytesReceived ;
93- DWORD flags = 0 ;
94- SOCKET fd ;
95-
96- wsaBuf .buf = mbuf ;
97- wsaBuf .len = sizeof (mbuf );
98-
99- if (WSARecv (ld , & wsaBuf , 1 , & bytesReceived , & flags , NULL , NULL ) == SOCKET_ERROR )
100- {
101- // Handle error, e.g., print error message or return appropriate error code.
102- return -1 ;
103- }
10491
105- // Extract the socket descriptor from the received control information
106- if (WSAGetOverlappedResult (ld , NULL , & bytesReceived , FALSE, & flags ) == SOCKET_ERROR )
107- {
108- // Handle error, e.g., print error message or return appropriate error code.
109- return -1 ;
110- }
111-
112- fd = * (SOCKET * )mbuf ;
113- * conn_out = fd ;
92+ // SOCKET ListenSocket = ld;
93+ // fd_set readfds;
94+ // struct timeval tv;
95+ // int result;
96+
97+ // // Initialize the set
98+ // FD_ZERO(&readfds);
99+ // FD_SET(ListenSocket, &readfds);
100+
101+ // // Set timeout to zero, for non-blocking operation
102+ // tv.tv_sec = 1;
103+ // tv.tv_usec = 0;
104+
105+ // result = select(ListenSocket + 1, &readfds, NULL, NULL, &tv);
106+
107+ // if (result == -1) {
108+ // printf("select failed with error: %u\n", WSAGetLastError());
109+ // } else if (result == 0) {
110+ // printf("No incoming connections\n");
111+ // } else {
112+ // printf("Socket is ready to accept a connection\n");
113+ // }
114+ // char mbuf[256];
115+ // WSABUF wsaBuf;
116+ // DWORD bytesReceived;
117+ // DWORD flags = 0;
118+ // SOCKET fd;
119+
120+ // wsaBuf.buf = mbuf;
121+ // wsaBuf.len = sizeof(mbuf);
122+
123+ // if (WSARecv(ld, &wsaBuf, 1, &bytesReceived, &flags, NULL, NULL) == SOCKET_ERROR)
124+ // {
125+ // // Print the error code
126+ // int error = WSAGetLastError();
127+ // fprintf(stderr, "WSARecv failed with error: %d\n", error);
128+ // return -1;
129+ // }
130+
131+ // // Extract the socket descriptor from the received control information
132+ // if (WSAGetOverlappedResult(ld, NULL, &bytesReceived, FALSE, &flags) == SOCKET_ERROR)
133+ // {
134+ // int error = WSAGetLastError();
135+ // fprintf(stderr, "WSAGetOverlappedResult failed with error: %d\n", error);
136+ // return -1;
137+ // }
138+ // second attemp
139+ // WSADATA wsaData;
140+ // int error = WSAStartup(MAKEWORD(2,2), &wsaData);
141+ // if (error) {
142+ // printf("WSAStartup() failed with error: %d\n", error);
143+ // return 1;
144+ // }
145+ // fd = WSAAccept(ListenSocket + 1, NULL, NULL, NULL, 0);
146+ // if (fd == INVALID_SOCKET)
147+ // {
148+ // int error = WSAGetLastError();
149+ // fprintf(stderr, "WSAAccept failed with error: %d\n", error);
150+ // //return -1;
151+ // }
152+
153+ // *conn_out = fd;
154+ // return 0;
155+ // third attempt
156+ // char mbuf[256];
157+ // WSABUF wsaBuf;
158+ // DWORD bytesReceived;
159+ // DWORD flags = 0;
160+
161+ // wsaBuf.buf = mbuf;
162+ // wsaBuf.len = sizeof(mbuf);
163+
164+ // if (WSARecv(ld, &wsaBuf, 1, &bytesReceived, &flags, NULL, NULL) == SOCKET_ERROR)
165+ // {
166+ // // Print the error code
167+ // int error = WSAGetLastError();
168+ // fprintf(stderr, "WSARecv failed with error: %d\n", error);
169+ // return -1;
170+ // }
171+
172+ // // If WSARecv succeeded, return the socket
173+ // *conn_out = ld;
174+ // return 0;
175+ struct sockaddr clientAddr ;
176+ int clientAddrSize = sizeof (clientAddr );
177+
178+ // Accept incoming connection
179+ * conn_out = accept (ld , & clientAddr , & clientAddrSize );
180+ if (* conn_out == INVALID_SOCKET ) {
181+ printf ("Accept failed with error code: %d\n" , WSAGetLastError ());
182+ return -1 ;
183+ }
184+
185+ return 0 ;
114186
115187#endif
116188}
0 commit comments