Skip to content

Commit 9683add

Browse files
Mistukekazu-yamamoto
authored andcommitted
Fix Win32 linking issues. now need to fixtest failures
1 parent 834e6ff commit 9683add

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

Network/Socket/Buffer.hsc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ foreign import CALLCONV SAFE_ON_WIN "ioctlsocket"
302302
c_ioctlsocket :: CInt -> CLong -> Ptr CULong -> IO CInt
303303
foreign import CALLCONV SAFE_ON_WIN "WSAGetLastError"
304304
c_WSAGetLastError :: IO CInt
305-
foreign import CALLCONV SAFE_ON_WIN "sendmsg"
305+
foreign import CALLCONV SAFE_ON_WIN "WSASendMsg"
306306
-- fixme Handle for SOCKET, see #426
307307
c_sendmsg :: CInt -> Ptr (MsgHdr sa) -> DWORD -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
308-
foreign import CALLCONV SAFE_ON_WIN "recvmsg"
308+
foreign import CALLCONV SAFE_ON_WIN "WSARecvMsg"
309309
c_recvmsg :: CInt -> Ptr (MsgHdr sa) -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
310310
#endif
311311

Network/Socket/ByteString/Internal.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ foreign import ccall unsafe "recvmsg"
6262
c_recvmsg :: CInt -> Ptr (MsgHdr SockAddr) -> CInt -> IO CSsize
6363
#else
6464
-- fixme Handle for SOCKET, see #426
65-
foreign import CALLCONV SAFE_ON_WIN "wsasend"
65+
foreign import CALLCONV SAFE_ON_WIN "WSASend"
6666
c_wsasend :: CInt -> Ptr WSABuf -> DWORD -> LPDWORD -> DWORD -> Ptr () -> Ptr () -> IO CInt
67-
foreign import CALLCONV SAFE_ON_WIN "sendmsg"
67+
foreign import CALLCONV SAFE_ON_WIN "WSASendMsg"
6868
c_sendmsg :: CInt -> Ptr (MsgHdr SockAddr) -> DWORD -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
69-
foreign import CALLCONV SAFE_ON_WIN "recvmsg"
69+
foreign import CALLCONV SAFE_ON_WIN "WSARecvMsg"
7070
c_recvmsg :: CInt -> Ptr (MsgHdr SockAddr) -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
7171
#endif

cbits/cmsg.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
#ifdef _WIN32
55

6-
struct LPCMSGHDR cmsg_firsthdr(LPWSAMSG mhdr) {
6+
LPWSACMSGHDR cmsg_firsthdr(LPWSAMSG mhdr) {
77
return (WSA_CMSG_FIRSTHDR(mhdr));
88
}
99

10-
struct LPCMSGHDR cmsg_nxthdr(LPWSAMSG mhdr, LPWSACMSGHDR cmsg) {
10+
LPWSACMSGHDR cmsg_nxthdr(LPWSAMSG mhdr, LPWSACMSGHDR cmsg) {
1111
return (WSA_CMSG_NXTHDR(mhdr, cmsg));
1212
}
1313

@@ -22,6 +22,49 @@ unsigned int cmsg_space(unsigned int l) {
2222
unsigned int cmsg_len(unsigned int l) {
2323
return (WSA_CMSG_LEN(l));
2424
}
25+
26+
static LPFN_WSASENDMSG ptr_SendMsg;
27+
static LPFN_WSARECVMSG ptr_RecvMsg;
28+
/* GUIDS to lookup WSASend/RecvMsg */
29+
static GUID WSARecvMsgGUID = WSAID_WSARECVMSG;
30+
static GUID WSASendMsgGUID = WSAID_WSASENDMSG;
31+
32+
int WINAPI
33+
WSASendMsg (SOCKET s, LPWSAMSG lpMsg, DWORD flags,
34+
LPDWORD lpdwNumberOfBytesRecvd, LPWSAOVERLAPPED lpOverlapped,
35+
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) {
36+
37+
if (!ptr_SendMsg) {
38+
DWORD len;
39+
if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
40+
&WSASendMsgGUID, sizeof(WSASendMsgGUID), &ptr_SendMsg,
41+
sizeof(ptr_SendMsg), &len, NULL, NULL) != 0)
42+
return -1;
43+
}
44+
45+
return ptr_SendMsg (s, lpMsg, flags, lpdwNumberOfBytesRecvd, lpOverlapped,
46+
lpCompletionRoutine);
47+
}
48+
49+
/**
50+
* WSARecvMsg function
51+
*/
52+
int WINAPI
53+
WSARecvMsg (SOCKET s, LPWSAMSG lpMsg, LPDWORD lpdwNumberOfBytesRecvd,
54+
LPWSAOVERLAPPED lpOverlapped,
55+
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) {
56+
57+
if (!ptr_RecvMsg) {
58+
DWORD len;
59+
if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
60+
&WSARecvMsgGUID, sizeof(WSARecvMsgGUID), &ptr_RecvMsg,
61+
sizeof(ptr_RecvMsg), &len, NULL, NULL) != 0)
62+
return -1;
63+
}
64+
65+
return ptr_RecvMsg (s, lpMsg, lpdwNumberOfBytesRecvd, lpOverlapped,
66+
lpCompletionRoutine);
67+
}
2568
#else
2669
struct cmsghdr *cmsg_firsthdr(struct msghdr *mhdr) {
2770
return (CMSG_FIRSTHDR(mhdr));

include/HsNet.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extern void* newAcceptParams(int sock,
8282
extern int acceptNewSock(void* d);
8383
extern int acceptDoProc(void* param);
8484

85-
extern struct LPWSACMSGHDR
85+
extern LPWSACMSGHDR
8686
cmsg_firsthdr(LPWSAMSG mhdr);
8787

8888
extern LPWSACMSGHDR
@@ -96,6 +96,20 @@ cmsg_space(unsigned int l);
9696

9797
extern unsigned int
9898
cmsg_len(unsigned int l);
99+
100+
/**
101+
* WSASendMsg function
102+
*/
103+
extern WINAPI int
104+
WSASendMsg (SOCKET, LPWSAMSG, DWORD, LPDWORD,
105+
LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE);
106+
107+
/**
108+
* WSARecvMsg function
109+
*/
110+
extern WINAPI int
111+
WSARecvMsg (SOCKET, LPWSAMSG, LPDWORD,
112+
LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE);
99113
#else /* _WIN32 */
100114
extern int
101115
sendFd(int sock, int outfd);

network.cabal

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ library
8888
include-dirs: include
8989
includes: HsNet.h HsNetDef.h alignment.h win32defs.h
9090
install-includes: HsNet.h HsNetDef.h win32defs.h
91-
c-sources: cbits/HsNet.c
91+
c-sources: cbits/HsNet.c cbits/cmsg.c
9292
ghc-options: -Wall -fwarn-tabs
9393
build-tools: hsc2hs
9494

@@ -102,8 +102,6 @@ library
102102
Network.Socket.Posix.IOVec
103103
Network.Socket.Posix.MsgHdr
104104
Network.Socket.Unix
105-
c-sources: cbits/cmsg.c
106-
107105

108106
if os(solaris)
109107
extra-libraries: nsl, socket
@@ -116,10 +114,11 @@ library
116114
Network.Socket.Win32.WSABuf
117115
Network.Socket.Win32.MsgHdr
118116
c-sources: cbits/initWinSock.c, cbits/winSockErr.c, cbits/asyncAccept.c
119-
extra-libraries: ws2_32, iphlpapi
117+
extra-libraries: ws2_32, iphlpapi, mswsock
120118
-- See https://github.com/haskell/network/pull/362
121119
if impl(ghc >= 7.10)
122120
cpp-options: -D_WIN32_WINNT=0x0600
121+
cc-options: -D_WIN32_WINNT=0x0600
123122

124123
test-suite spec
125124
default-language: Haskell2010

tests/Network/SocketSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ spec = do
126126
-- check if an exception is not thrown.
127127
isSupportedSockAddr addr `shouldBe` True
128128

129+
#if !defined(mingw32_HOST_OS)
129130
when isUnixDomainSocketAvailable $ do
130131
context "unix sockets" $ do
131132
it "basic unix sockets end-to-end" $ do
@@ -134,6 +135,7 @@ spec = do
134135
recv sock 1024 `shouldReturn` testMsg
135136
addr `shouldBe` (SockAddrUnix "")
136137
test . setClientAction client $ unixWithUnlink unixAddr server
138+
#endif
137139

138140
#ifdef linux_HOST_OS
139141
it "can end-to-end with an abstract socket" $ do
@@ -152,6 +154,7 @@ spec = do
152154
bind sock (SockAddrUnix abstractAddress) `shouldThrow` anyErrorCall
153155
#endif
154156

157+
#if !defined(mingw32_HOST_OS)
155158
describe "socketPair" $ do
156159
it "can send and recieve bi-directionally" $ do
157160
(s1, s2) <- socketPair AF_UNIX Stream defaultProtocol
@@ -206,6 +209,7 @@ spec = do
206209
cred1 <- getPeerCredential s
207210
cred1 `shouldBe` (Nothing,Nothing,Nothing)
208211
-}
212+
#endif
209213

210214
describe "gracefulClose" $ do
211215
it "does not send TCP RST back" $ do

0 commit comments

Comments
 (0)