3 * Copyright (C) 2004 Filip Navara
9 ULONG
DbgPrint(PCH Format
,...);
13 printf("%s:%i - %s", __FILE__, __LINE__, x); \
14 DbgPrint("%s:%i - %s", __FILE__, __LINE__, x);
19 * A socket pair is often used for interprocess communication,
20 * so we need to make sure neither socket is associated with
21 * the I/O completion port; otherwise it can't be used by a
24 * The default implementation below cannot be used for NT
25 * because PR_Accept would have associated the I/O completion
26 * port with the listening and accepted sockets.
30 struct sockaddr_in selfAddr
, peerAddr
;
32 WORD wVersionRequested
;
40 wVersionRequested
= MAKEWORD( 2, 2 );
42 DBG("Calling WSAStartup\n");
43 err
= WSAStartup( wVersionRequested
, &wsaData
);
45 /* Tell the user that we could not find a usable */
47 DBG("WSAStartup failed\n");
51 /* Confirm that the WinSock DLL supports 2.2.*/
52 /* Note that if the DLL supports versions greater */
53 /* than 2.2 in addition to 2.2, it will still return */
54 /* 2.2 in wVersion since that is the version we */
57 if ( LOBYTE( wsaData
.wVersion
) != 2 ||
58 HIBYTE( wsaData
.wVersion
) != 2 ) {
59 /* Tell the user that we could not find a usable */
61 DBG("WSAStartup version unacceptable\n");
66 /* The WinSock DLL is acceptable. Proceed. */
68 DBG("Calling socket\n");
69 osfd
[0] = osfd
[1] = INVALID_SOCKET
;
70 listenSock
= socket(AF_INET
, SOCK_STREAM
, 0);
71 if (listenSock
== INVALID_SOCKET
) {
72 DBG("socket failed\n");
76 selfAddr
.sin_family
= AF_INET
;
77 selfAddr
.sin_port
= 0;
78 selfAddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
); /* BugZilla: 35408 */
79 addrLen
= sizeof(selfAddr
);
80 DBG("Calling bind\n");
81 if (bind(listenSock
, (struct sockaddr
*) &selfAddr
,
82 addrLen
) == SOCKET_ERROR
) {
87 DBG("Calling getsockname\n");
88 if (getsockname(listenSock
, (struct sockaddr
*) &selfAddr
,
89 &addrLen
) == SOCKET_ERROR
) {
90 DBG("getsockname failed\n");
94 DBG("Calling listen\n");
95 if (listen(listenSock
, 5) == SOCKET_ERROR
) {
96 DBG("listen failed\n");
100 DBG("Calling socket\n");
101 osfd
[0] = socket(AF_INET
, SOCK_STREAM
, 0);
102 if (osfd
[0] == INVALID_SOCKET
) {
103 DBG("socket failed\n");
106 selfAddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
109 * Only a thread is used to do the connect and accept.
110 * I am relying on the fact that connect returns
111 * successfully as soon as the connect request is put
112 * into the listen queue (but before accept is called).
113 * This is the behavior of the BSD socket code. If
114 * connect does not return until accept is called, we
115 * will need to create another thread to call connect.
117 DBG("Calling connect\n");
118 if (connect(osfd
[0], (struct sockaddr
*) &selfAddr
,
119 addrLen
) == SOCKET_ERROR
) {
120 DBG("connect failed\n");
125 * A malicious local process may connect to the listening
126 * socket, so we need to verify that the accepted connection
127 * is made from our own socket osfd[0].
129 DBG("Calling getsockname\n");
130 if (getsockname(osfd
[0], (struct sockaddr
*) &selfAddr
,
131 &addrLen
) == SOCKET_ERROR
) {
132 DBG("getsockname failed\n");
136 DBG("Calling accept\n");
137 osfd
[1] = accept(listenSock
, (struct sockaddr
*) &peerAddr
, &addrLen
);
138 if (osfd
[1] == INVALID_SOCKET
) {
139 DBG("accept failed\n");
142 if (peerAddr
.sin_port
!= selfAddr
.sin_port
) {
143 /* the connection we accepted is not from osfd[0] */
144 DBG("peerAddr.sin_port != selfAddr.sin_port\n");
150 closesocket(listenSock
);
152 closesocket(osfd
[0]);
153 closesocket(osfd
[1]);
160 if (listenSock
!= INVALID_SOCKET
) {
161 closesocket(listenSock
);
163 if (osfd
[0] != INVALID_SOCKET
) {
164 closesocket(osfd
[0]);
166 if (osfd
[1] != INVALID_SOCKET
) {
167 closesocket(osfd
[1]);
181 PCHAR pszFileName
= "test.txt";
182 ULONG dwMaxSize
= strlen(pszFileName
);
184 DBG("Calling CreateFileMappingA\n");
185 hMap
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
,
186 PAGE_READWRITE
| SEC_RESERVE
, 0, dwMaxSize
, pszFileName
);
189 DBG("CreateFileMappingA failed\n");
193 dwErr
= GetLastError();
194 DBG("Calling MapViewOfFile\n");
195 pBufferStart
= (BYTE
*)MapViewOfFile(hMap
, FILE_MAP_ALL_ACCESS
, 0, 0, 0);
198 DBG("MapViewOfFile failed\n");
204 if (dwErr
== ERROR_ALREADY_EXISTS
)
206 DBG("MapViewOfFile returned ERROR_ALREADY_EXISTS\n");
207 DBG("This really shouldn't happen, but it's not fatal.\n");
208 UnmapViewOfFile(pBufferStart
);
214 DBG("Calling VirtualAlloc\n");
215 if (!VirtualAlloc(pBufferStart
, si
.dwPageSize
, MEM_COMMIT
, PAGE_READWRITE
))
217 DBG("VirtualAlloc failed\n");
218 UnmapViewOfFile(pBufferStart
);
226 UnmapViewOfFile(pBufferStart
);
232 int main(int argc
, char **argv
)
236 printf("Usage: %s test_name\n\n", argv
[0]);
237 printf("Valid test names:\n");
238 printf("\tsocket\n");
239 printf("\tvirtual\n");
243 if (!stricmp(argv
[1], "socket"))
245 if (!stricmp(argv
[1], "virtual"))
246 return VirtualTest();
248 printf("Test '%s' doesn't exist\n", argv
[1]);