ReactOS 0.4.16-dev-290-g89a3b8f
moztest.c File Reference
#include <winsock.h>
#include <stdio.h>
Include dependency graph for moztest.c:

Go to the source code of this file.

Macros

#define DBG(x)
 

Functions

ULONG DbgPrint (PCH Format,...)
 
int SocketTest ()
 
int VirtualTest ()
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ DBG

#define DBG (   x)
Value:
printf("%s:%i - %s", __FILE__, __LINE__, x); \
DbgPrint("%s:%i - %s", __FILE__, __LINE__, x);
#define printf
Definition: freeldr.h:97
GLint GLint GLint GLint GLint x
Definition: gl.h:1548

Definition at line 12 of file moztest.c.

Function Documentation

◆ DbgPrint()

ULONG DbgPrint ( PCH  Format,
  ... 
)

◆ main()

int main ( int argc  ,
char **  argv 
)

Definition at line 232 of file moztest.c.

233{
234 if (argc != 2)
235 {
236 printf("Usage: %s test_name\n\n", argv[0]);
237 printf("Valid test names:\n");
238 printf("\tsocket\n");
239 printf("\tvirtual\n");
240 return 0;
241 }
242
243 if (!_stricmp(argv[1], "socket"))
244 return SocketTest();
245 if (!_stricmp(argv[1], "virtual"))
246 return VirtualTest();
247
248 printf("Test '%s' doesn't exist\n", argv[1]);
249
250 return 0;
251}
static int argc
Definition: ServiceArgs.c:12
#define _stricmp
Definition: cat.c:22
int VirtualTest()
Definition: moztest.c:175
int SocketTest()
Definition: moztest.c:16
#define argv
Definition: mplay32.c:18

◆ SocketTest()

int SocketTest ( )

Definition at line 16 of file moztest.c.

17{
18 /*
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
22 * child process.
23 *
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.
27 */
28 SOCKET listenSock;
29 SOCKET osfd[2];
30 struct sockaddr_in selfAddr, peerAddr;
31 int addrLen;
32 WORD wVersionRequested;
33 WSADATA wsaData;
34 int err;
35
36 /*
37 * Initialization.
38 */
39
40 wVersionRequested = MAKEWORD( 2, 2 );
41
42 DBG("Calling WSAStartup\n");
43 err = WSAStartup( wVersionRequested, &wsaData );
44 if ( err != 0 ) {
45 /* Tell the user that we could not find a usable */
46 /* WinSock DLL. */
47 DBG("WSAStartup failed\n");
48 return 1;
49 }
50
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 */
55 /* requested. */
56
57 if ( LOBYTE( wsaData.wVersion ) != 2 ||
58 HIBYTE( wsaData.wVersion ) != 2 ) {
59 /* Tell the user that we could not find a usable */
60 /* WinSock DLL. */
61 DBG("WSAStartup version unacceptable\n");
62 WSACleanup( );
63 return 1;
64 }
65
66 /* The WinSock DLL is acceptable. Proceed. */
67
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");
73 goto failed;
74 }
75
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) {
83 DBG("bind failed\n");
84 goto failed;
85 }
86
87 DBG("Calling getsockname\n");
88 if (getsockname(listenSock, (struct sockaddr *) &selfAddr,
89 &addrLen) == SOCKET_ERROR) {
90 DBG("getsockname failed\n");
91 goto failed;
92 }
93
94 DBG("Calling listen\n");
95 if (listen(listenSock, 5) == SOCKET_ERROR) {
96 DBG("listen failed\n");
97 goto failed;
98 }
99
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");
104 goto failed;
105 }
106 selfAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
107
108 /*
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.
116 */
117 DBG("Calling connect\n");
118 if (connect(osfd[0], (struct sockaddr *) &selfAddr,
119 addrLen) == SOCKET_ERROR) {
120 DBG("connect failed\n");
121 goto failed;
122 }
123
124 /*
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].
128 */
129 DBG("Calling getsockname\n");
130 if (getsockname(osfd[0], (struct sockaddr *) &selfAddr,
131 &addrLen) == SOCKET_ERROR) {
132 DBG("getsockname failed\n");
133 goto failed;
134 }
135
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");
140 goto failed;
141 }
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");
145 goto failed;
146 }
147
148 DBG("Hurray!\n");
149
150 closesocket(listenSock);
151
152 closesocket(osfd[0]);
153 closesocket(osfd[1]);
154
155 WSACleanup();
156
157 return 0;
158
159failed:
160 if (listenSock != INVALID_SOCKET) {
161 closesocket(listenSock);
162 }
163 if (osfd[0] != INVALID_SOCKET) {
164 closesocket(osfd[0]);
165 }
166 if (osfd[1] != INVALID_SOCKET) {
167 closesocket(osfd[1]);
168 }
169
170 WSACleanup();
171
172 return 1;
173}
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
#define SOCK_STREAM
Definition: tcpip.h:118
#define AF_INET
Definition: tcpip.h:117
unsigned short WORD
Definition: ntddk_ex.h:93
#define LOBYTE(W)
Definition: jmemdos.c:487
#define HIBYTE(W)
Definition: jmemdos.c:486
#define INADDR_LOOPBACK
Definition: inet.h:78
#define htonl(x)
Definition: module.h:214
#define DBG(x)
Definition: moztest.c:12
#define closesocket
Definition: ncftp.h:477
#define err(...)
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:213
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
WORD wVersion
Definition: winsock.h:517
#define MAKEWORD(a, b)
Definition: typedefs.h:248
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60
#define INVALID_SOCKET
Definition: winsock.h:332
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCKET_ERROR
Definition: winsock.h:333

Referenced by main().

◆ VirtualTest()

int VirtualTest ( )

Definition at line 175 of file moztest.c.

176{
177 DWORD dwErr;
178 SYSTEM_INFO si;
179 HANDLE hMap;
180 PBYTE pBufferStart;
181 PCHAR pszFileName = "test.txt";
182 ULONG dwMaxSize = strlen(pszFileName);
183
184 DBG("Calling CreateFileMappingA\n");
186 PAGE_READWRITE | SEC_RESERVE, 0, dwMaxSize, pszFileName);
187 if (!hMap)
188 {
189 DBG("CreateFileMappingA failed\n");
190 return 1;
191 }
192
194 DBG("Calling MapViewOfFile\n");
195 pBufferStart = (BYTE *)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
196 if (!pBufferStart)
197 {
198 DBG("MapViewOfFile failed\n");
199 return 1;
200 }
201
202 GetSystemInfo(&si);
203
205 {
206 DBG("MapViewOfFile returned ERROR_ALREADY_EXISTS\n");
207 DBG("This really shouldn't happen, but it's not fatal.\n");
208 UnmapViewOfFile(pBufferStart);
209 CloseHandle(hMap);
210 return 1;
211 }
212 else
213 {
214 DBG("Calling VirtualAlloc\n");
215 if (!VirtualAlloc(pBufferStart, si.dwPageSize, MEM_COMMIT, PAGE_READWRITE))
216 {
217 DBG("VirtualAlloc failed\n");
218 UnmapViewOfFile(pBufferStart);
219 CloseHandle(hMap);
220 return 1;
221 }
222 }
223
224 DBG("Hurray!\n");
225
226 UnmapViewOfFile(pBufferStart);
227 CloseHandle(hMap);
228
229 return 0;
230}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
DWORD dwErr
Definition: service.c:36
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
#define UnmapViewOfFile
Definition: compat.h:746
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MapViewOfFile
Definition: compat.h:745
VOID WINAPI GetSystemInfo(IN LPSYSTEM_INFO lpSystemInfo)
Definition: sysinfo.c:143
HANDLE NTAPI CreateFileMappingA(IN HANDLE hFile, IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, IN DWORD flProtect, IN DWORD dwMaximumSizeHigh, IN DWORD dwMaximumSizeLow, IN LPCSTR lpName)
Definition: filemap.c:23
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SEC_RESERVE
Definition: nt_native.h:1323
#define MEM_COMMIT
Definition: nt_native.h:1313
BYTE * PBYTE
Definition: pedump.c:66
DWORD dwPageSize
Definition: winbase.h:1198
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
WORD WORD PSZ PSZ pszFileName
Definition: vdmdbg.h:44
LPVOID NTAPI VirtualAlloc(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
Definition: virtmem.c:65
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FILE_MAP_ALL_ACCESS
Definition: winbase.h:157
unsigned char BYTE
Definition: xxhash.c:193

Referenced by main().