Skip to content

Commit

Permalink
sockets(windows): correctly obtain socket family for accept
Browse files Browse the repository at this point in the history
  • Loading branch information
alaviss committed Feb 14, 2024
1 parent 9bba643 commit 4664a74
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/sys/private/sockets_windows.nim
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ template acceptCommon(listener: SocketFD, conn: var Handle[SocketFD],
# Common parts for dealing with AcceptEx, `overlapped` dictates whether the
# operation should be done in an overlapped manner and yields an overlapped
# socket.
const addressFamily =
when endpoint is IP4Endpoint:
AF_INET
elif endpoint is IP6Endpoint:
AF_INET6

# TODO : cache this data
let lendpoint = getLocalEndpoint(listener)
let addressFamily =
case lendpoint.kind
of V4: AF_INET
of V6: AF_INET6

conn = initHandle:
SocketFD:
Expand Down Expand Up @@ -518,12 +520,15 @@ template tcpAsyncAccept() {.dirty.} =
result.conn = AsyncConn[TCP] newAsyncSocket(move conn)

template tcpLocalEndpoint() {.dirty.} =
getLocalEndpoint(l.fd)

proc getLocalEndpoint(fd: SocketFD): IPEndpoint =
var
saddr: SockaddrStorage
endpointLen = cint sizeof(saddr)

if getsockname(
wincore.Socket(l.fd),
wincore.Socket(fd),
cast[ptr sockaddr](addr saddr),
addr endpointLen
) == SocketError:
Expand Down

0 comments on commit 4664a74

Please sign in to comment.