Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2695757

Browse files
committedDec 28, 2021
Proper type for variables and function arguments
The actual optval argument type is DWORD (boolean) for the SOL_SOCKET socket option SO_REUSEADDR: https://docs.microsoft.com/windows/win32/winsock/sol-socket-socket-options The type of the optlen argument of setsockopt() is "int": https://docs.microsoft.com/windows/win32/api/winsock/nf-winsock-setsockopt#syntax The type of the namelen argument of getsockname() is "int *": https://docs.microsoft.com/windows/win32/api/winsock/nf-winsock-getsockname#syntax After these changes, socklen_t is not used anymore. Therefore we can revert from <w2stcpip.h> to <winsock2.h>.
1 parent 54c8776 commit 2695757

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed
 

‎dummy_headers/windows.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
typedef void* SOCKET;
22
typedef unsigned int DWORD;
3+
typedef unsigned int GROUP;
34
#define WSA_FLAG_OVERLAPPED 1
45
extern void WSASetLastError(int);
56
extern int WSAGetLastError();
@@ -8,24 +9,23 @@ extern int WSAGetLastError();
89
#define AF_INET 33
910
#define SOCK_STREAM 44
1011
#define IPPROTO_TCP 55
11-
#define INVALID_SOCKET 0
12+
#define INVALID_SOCKET (SOCKET)(~0)
1213
extern void* socket(int, int, int);
1314
struct sockaddr_in {
1415
int sin_family;
1516
struct { unsigned s_addr; } sin_addr;
1617
unsigned short sin_port;
1718
};
1819
struct sockaddr { int dummy; };
19-
typedef unsigned socklen_t;
2020
#define SOL_SOCKET 66
2121
#define SO_REUSEADDR 77
2222
#define INADDR_LOOPBACK 88
23-
extern int setsockopt(SOCKET, int, int, char*, unsigned);
24-
extern int bind(SOCKET, struct sockaddr*, unsigned);
25-
extern int getsockname(SOCKET, struct sockaddr*, socklen_t*);
23+
extern int setsockopt(SOCKET, int, int, const char*, int);
24+
extern int bind(SOCKET, const struct sockaddr*, int);
25+
extern int getsockname(SOCKET, struct sockaddr*, int*);
2626
extern int listen(SOCKET, int);
27-
extern SOCKET WSASocket(int, int, int, void*, int, int);
28-
extern int connect(SOCKET, struct sockaddr*, unsigned);
29-
extern SOCKET accept(SOCKET, void*, void*);
27+
extern SOCKET WSASocket(int, int, int, void*, GROUP, DWORD);
28+
extern int connect(SOCKET, const struct sockaddr*, int);
29+
extern SOCKET accept(SOCKET, struct sockaddr*, int*);
3030
extern int closesocket(SOCKET);
31-
extern unsigned htonl(unsigned);
31+
extern unsigned long htonl(unsigned long);
File renamed without changes.

‎socketpair.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5151
#include <string.h>
5252

5353
#ifdef WIN32
54-
# include <ws2tcpip.h> /* socklen_t, et al (MSVC20xx) */
54+
# include <winsock2.h>
5555
# include <windows.h>
5656
# include <io.h>
5757
#else
@@ -77,16 +77,16 @@ int dumb_socketpair(SOCKET socks[2], int make_overlapped)
7777
struct sockaddr addr;
7878
} a;
7979
SOCKET listener;
80-
int e;
81-
socklen_t addrlen = sizeof(a.inaddr);
80+
int addrlen = (int) sizeof(a.inaddr);
8281
DWORD flags = (make_overlapped ? WSA_FLAG_OVERLAPPED : 0);
83-
int reuse = 1;
82+
DWORD reuse = 1;
83+
int e;
8484

8585
if (socks == 0) {
8686
WSASetLastError(WSAEINVAL);
8787
return SOCKET_ERROR;
8888
}
89-
socks[0] = socks[1] = -1;
89+
socks[0] = socks[1] = INVALID_SOCKET;
9090

9191
listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
9292
if (listener == INVALID_SOCKET)
@@ -99,9 +99,9 @@ int dumb_socketpair(SOCKET socks[2], int make_overlapped)
9999

100100
for (;;) {
101101
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
102-
(char*) &reuse, (socklen_t) sizeof(reuse)) == SOCKET_ERROR)
102+
(const char*) &reuse, (int) sizeof(reuse)) == SOCKET_ERROR)
103103
break;
104-
if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
104+
if (bind(listener, &a.addr, (int) sizeof(a.inaddr)) == SOCKET_ERROR)
105105
break;
106106

107107
memset(&a, 0, sizeof(a));
@@ -118,7 +118,7 @@ int dumb_socketpair(SOCKET socks[2], int make_overlapped)
118118
socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
119119
if (socks[0] == INVALID_SOCKET)
120120
break;
121-
if (connect(socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
121+
if (connect(socks[0], &a.addr, (int) sizeof(a.inaddr)) == SOCKET_ERROR)
122122
break;
123123

124124
socks[1] = accept(listener, NULL, NULL);
@@ -134,7 +134,7 @@ int dumb_socketpair(SOCKET socks[2], int make_overlapped)
134134
closesocket(socks[0]);
135135
closesocket(socks[1]);
136136
WSASetLastError(e);
137-
socks[0] = socks[1] = -1;
137+
socks[0] = socks[1] = INVALID_SOCKET;
138138
return SOCKET_ERROR;
139139
}
140140
#else

0 commit comments

Comments
 (0)
Please sign in to comment.