ReactOS  0.4.14-dev-293-g2b39b42
close.c File Reference
#include "ws2_32.h"
Include dependency graph for close.c:

Go to the source code of this file.

Macros

#define TIMEOUT_SEC   0
 
#define TIMEOUT_USEC   100000
 
#define TIME_SLEEP1   250
 
#define THREAD_PROC_LOOPS   5
 
#define LISTEN_PORT   22222
 
#define LISTEN_BACKLOG   5
 

Functions

void Test_CloseDuplicatedSocket ()
 
DWORD WINAPI thread_proc (void *param)
 
void Test_CloseWhileSelectSameSocket ()
 
void Test_CloseWhileSelectDuplicatedSocket ()
 
 START_TEST (close)
 

Macro Definition Documentation

◆ LISTEN_BACKLOG

#define LISTEN_BACKLOG   5

Definition at line 64 of file close.c.

◆ LISTEN_PORT

#define LISTEN_PORT   22222

Definition at line 63 of file close.c.

◆ THREAD_PROC_LOOPS

#define THREAD_PROC_LOOPS   5

Definition at line 61 of file close.c.

◆ TIME_SLEEP1

#define TIME_SLEEP1   250

Definition at line 59 of file close.c.

◆ TIMEOUT_SEC

#define TIMEOUT_SEC   0

Definition at line 55 of file close.c.

◆ TIMEOUT_USEC

#define TIMEOUT_USEC   100000

Definition at line 56 of file close.c.

Function Documentation

◆ START_TEST()

START_TEST ( close  )

Definition at line 177 of file close.c.

178 {
179  int err;
180  WSADATA wdata;
181 
182  /* Start up Winsock */
183  err = WSAStartup(MAKEWORD(2, 2), &wdata);
184  ok(err == 0, "WSAStartup failed, iResult == %d %d\n", err, WSAGetLastError());
185 
189 
190  WSACleanup();
191 }
#define MAKEWORD(a, b)
Definition: typedefs.h:247
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
void Test_CloseDuplicatedSocket()
Definition: close.c:10
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
void Test_CloseWhileSelectSameSocket()
Definition: close.c:88
#define err(...)
#define ok(value,...)
Definition: atltest.h:57
void Test_CloseWhileSelectDuplicatedSocket()
Definition: close.c:125

◆ Test_CloseDuplicatedSocket()

void Test_CloseDuplicatedSocket ( )

Definition at line 10 of file close.c.

11 {
12  char szBuf[10];
13  int err;
14  SOCKET sck, dup_sck;
15  WSAPROTOCOL_INFOW ProtocolInfo;
16  struct sockaddr_in to = { AF_INET, 2222, {{{ 0x7f, 0x00, 0x00, 0x01 }}} };
17 
18  /* Create the socket */
20  if(sck == INVALID_SOCKET)
21  {
22  skip("socket failed %d. Aborting test.\n", WSAGetLastError());
23  return;
24  }
25 
26  err = sendto(sck, szBuf, _countof(szBuf), 0, (struct sockaddr *)&to, sizeof(to));
27  ok(err == _countof(szBuf), "sendto err = %d %d\n", err, WSAGetLastError());
28 
29  err = WSADuplicateSocketW(sck, GetCurrentProcessId(), &ProtocolInfo);
30  ok(err == 0, "WSADuplicateSocketW err = %d %d\n", err, WSAGetLastError());
31 
32  dup_sck = WSASocketW(0, 0, 0, &ProtocolInfo, 0, 0);
33  if (dup_sck == INVALID_SOCKET)
34  {
35  skip("WSASocketW failed %d. Aborting test.\n", WSAGetLastError());
36  closesocket(sck);
37  return;
38  }
39 
40  err = sendto(dup_sck, szBuf, _countof(szBuf), 0, (struct sockaddr *)&to, sizeof(to));
41  ok(err == _countof(szBuf), "sendto err = %d %d\n", err, WSAGetLastError());
42 
43  err = closesocket(sck);
44  ok(err == 0, "closesocket sck err = %d %d\n", err, WSAGetLastError());
45 
46  err = sendto(dup_sck, szBuf, _countof(szBuf), 0, (struct sockaddr *)&to, sizeof(to));
47  ok(err == _countof(szBuf), "sendto err = %d %d\n", err, WSAGetLastError());
48 
49  err = closesocket(dup_sck);
50  ok(err == 0, "closesocket dup_sck err = %d %d\n", err, WSAGetLastError());
51  return;
52 }
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
#define closesocket
#define _countof(array)
Definition: fontsub.cpp:30
SOCKET WSAAPI WSASocketW(IN INT af, IN INT type, IN INT protocol, IN LPWSAPROTOCOL_INFOW lpProtocolInfo, IN GROUP g, IN DWORD dwFlags)
Definition: socklife.c:490
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
INT WSAAPI WSADuplicateSocketW(IN SOCKET s, IN DWORD dwProcessId, OUT LPWSAPROTOCOL_INFOW lpProtocolInfo)
Definition: dupsock.c:68
#define err(...)
#define ok(value,...)
Definition: atltest.h:57
INT WSAAPI sendto(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags, IN CONST struct sockaddr *to, IN INT tolen)
Definition: send.c:82
#define skip(...)
Definition: atltest.h:64
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_DGRAM
Definition: winsock.h:336
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158

Referenced by START_TEST().

◆ Test_CloseWhileSelectDuplicatedSocket()

void Test_CloseWhileSelectDuplicatedSocket ( )

Definition at line 125 of file close.c.

126 {
127  int err;
128  SOCKET sock, dup_sock;
129  WSAPROTOCOL_INFOW ProtocolInfo;
130  struct sockaddr_in addrin;
131  HANDLE hthread;
132 
133  /* Create the socket */
135  if (sock == INVALID_SOCKET)
136  {
137  skip("socket failed %d. Aborting test.\n", WSAGetLastError());
138  return;
139  }
140 
141  memset(&addrin, 0, sizeof(struct sockaddr_in));
142  addrin.sin_family = AF_INET;
143  addrin.sin_addr.s_addr = inet_addr("127.0.0.1");
144  addrin.sin_port = htons(LISTEN_PORT);
145 
146  err = bind(sock, (struct sockaddr*)(&addrin), sizeof(struct sockaddr_in));
147  ok(err == 0, "bind err = %d %d\n", err, WSAGetLastError());
149  ok(err == 0, "listen err = %d %d\n", err, WSAGetLastError());
150 
151  err = WSADuplicateSocketW(sock, GetCurrentProcessId(), &ProtocolInfo);
152  ok(err == 0, "WSADuplicateSocketW err = %d %d\n", err, WSAGetLastError());
153 
154  dup_sock = WSASocketW(0, 0, 0, &ProtocolInfo, 0, 0);
155  if (dup_sock == INVALID_SOCKET)
156  {
157  skip("WSASocketW failed %d. Aborting test.\n", WSAGetLastError());
158  closesocket(sock);
159  return;
160  }
161 
162  hthread = CreateThread(NULL, 0, thread_proc, (void*)dup_sock, 0, NULL);
163  ok(hthread != NULL, "CreateThread %ld\n", GetLastError());
164 
165  err = closesocket(sock);
166  ok(err == 0, "closesocket err = %d %d\n", err, WSAGetLastError());
167 
169  err = closesocket(dup_sock);
170  ok(err == 0, "closesocket err = %d %d\n", err, WSAGetLastError());
171 
172  WaitForSingleObject(hthread, INFINITE);
173  CloseHandle(hthread);
174  return;
175 }
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static SOCKET sock
Definition: syslog.c:45
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
#define inet_addr(cp)
Definition: inet.h:98
#define closesocket
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define TIME_SLEEP1
Definition: close.c:59
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
DWORD WINAPI thread_proc(void *param)
Definition: close.c:66
SOCKET WSAAPI WSASocketW(IN INT af, IN INT type, IN INT protocol, IN LPWSAPROTOCOL_INFOW lpProtocolInfo, IN GROUP g, IN DWORD dwFlags)
Definition: socklife.c:490
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:136
smooth NULL
Definition: ftsmooth.c:416
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
INT WSAAPI WSADuplicateSocketW(IN SOCKET s, IN DWORD dwProcessId, OUT LPWSAPROTOCOL_INFOW lpProtocolInfo)
Definition: dupsock.c:68
#define err(...)
#define LISTEN_PORT
Definition: close.c:63
#define ok(value,...)
Definition: atltest.h:57
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
Definition: tcpcore.h:1455
#define skip(...)
Definition: atltest.h:64
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define LISTEN_BACKLOG
Definition: close.c:64
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
#define INFINITE
Definition: serial.h:102
#define memset(x, y, z)
Definition: compat.h:39
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158

Referenced by START_TEST().

◆ Test_CloseWhileSelectSameSocket()

void Test_CloseWhileSelectSameSocket ( )

Definition at line 88 of file close.c.

89 {
90  int err;
91  SOCKET sock;
92  struct sockaddr_in addrin;
93  HANDLE hthread;
94 
95  /* Create the socket */
97  if (sock == INVALID_SOCKET)
98  {
99  skip("socket failed %d. Aborting test.\n", WSAGetLastError());
100  return;
101  }
102 
103  memset(&addrin, 0, sizeof(struct sockaddr_in));
104  addrin.sin_family = AF_INET;
105  addrin.sin_addr.s_addr = inet_addr("127.0.0.1");
106  addrin.sin_port = htons(LISTEN_PORT);
107 
108  err = bind(sock, (struct sockaddr*)(&addrin), sizeof(struct sockaddr_in));
109  ok(err == 0, "bind err = %d %d\n", err, WSAGetLastError());
111  ok(err == 0, "listen err = %d %d\n", err, WSAGetLastError());
112 
113  hthread = CreateThread(NULL, 0, thread_proc, (void*)sock, 0, NULL);
114  ok(hthread != NULL, "CreateThread %ld\n", GetLastError());
115 
117  err = closesocket(sock);
118  ok(err == 0, "closesocket err = %d %d\n", err, WSAGetLastError());
119 
120  WaitForSingleObject(hthread, INFINITE);
121  CloseHandle(hthread);
122  return;
123 }
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static SOCKET sock
Definition: syslog.c:45
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
#define inet_addr(cp)
Definition: inet.h:98
#define closesocket
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define TIME_SLEEP1
Definition: close.c:59
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
DWORD WINAPI thread_proc(void *param)
Definition: close.c:66
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:136
smooth NULL
Definition: ftsmooth.c:416
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
#define err(...)
#define LISTEN_PORT
Definition: close.c:63
#define ok(value,...)
Definition: atltest.h:57
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
Definition: tcpcore.h:1455
#define skip(...)
Definition: atltest.h:64
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define LISTEN_BACKLOG
Definition: close.c:64
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
#define INFINITE
Definition: serial.h:102
#define memset(x, y, z)
Definition: compat.h:39

Referenced by START_TEST().

◆ thread_proc()

DWORD WINAPI thread_proc ( void param)

Definition at line 66 of file close.c.

67 {
69  struct timeval tval;
71  int i;
72 
73  tval.tv_sec = TIMEOUT_SEC;
74  tval.tv_usec = TIMEOUT_USEC;
75 
76  for (i = 0; i < THREAD_PROC_LOOPS; ++i)
77  {
79  // write will be empty
81 
82  select(0, &read, &write, &except, &tval);
83  }
84 
85  return 0;
86 }
Definition: winsock.h:66
#define TIMEOUT_SEC
Definition: close.c:55
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
#define write
Definition: acwin.h:97
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
#define THREAD_PROC_LOOPS
Definition: close.c:61
GLfloat param
Definition: glext.h:5796
#define TIMEOUT_USEC
Definition: close.c:56
#define except(x)
Definition: btrfs_drv.h:135
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)

Referenced by Test_CloseWhileSelectDuplicatedSocket(), and Test_CloseWhileSelectSameSocket().