Skip to content

Commit

Permalink
native: Implement dup for Windows sockets
Browse files Browse the repository at this point in the history
Best effort though (works for AF_UNIX).
  • Loading branch information
kohlschuetter committed Feb 14, 2024
1 parent fa55ed4 commit c2602d3
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions junixsocket-native/src/main/c/filedescriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,26 @@ JNIEXPORT jobject JNICALL Java_org_newsclub_net_unix_NativeUnixSocket_duplicate
} else {
targetFD = dup2(sourceFD, targetFD);
}

#if defined(_WIN32)
if(targetFD == -1 && errno == EBADF) {
WSAPROTOCOL_INFO protocolInfo;
if(WSADuplicateSocket(sourceFD, GetCurrentProcessId(), &protocolInfo) != 0) {
_throwErrnumException(env, socket_errno, NULL);
return NULL;
}
SOCKET targetSocket = WSASocket(protocolInfo.iAddressFamily,
protocolInfo.iSocketType,
protocolInfo.iProtocol,&protocolInfo, 0, 0);
if(targetSocket == INVALID_SOCKET) {
_throwErrnumException(env, socket_errno, NULL);
return NULL;
}

targetFD = targetSocket;
}
#endif

if(targetFD == -1) {
_throwErrnumException(env, errno, NULL);
return NULL;
Expand Down

0 comments on commit c2602d3

Please sign in to comment.