-
Notifications
You must be signed in to change notification settings - Fork 0
/
NETWORK.CPP
278 lines (204 loc) · 6.8 KB
/
NETWORK.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "stdafx.h"
// WinSock is loaded on the fly these are pointers to functions used
LPWSAASYNCSELECT lpWSAAsyncSelect;
LPCONNECT lpconnect;
LPRECV lprecv;
LPWSAGETLASTERROR lpWSAGetLastError;
LPACCEPT lpaccept;
LPHTONS lphtons;
LPHTONL lphtonl;
LPINET_ADDR lpinet_addr;
LPSETSOCKOPT lpsetsockopt;
LPBIND lpbind;
LPLISTEN lplisten;
LPCLOSESOCKET lpclosesocket;
LPSEND lpsend;
LPSHUTDOWN lpshutdown;
LPSOCKET lpsocket;
LPGETHOSTNAME lpgethostname;
LPGETHOSTBYNAME lpgethostbyname;
LPWSASTARTUP lpWSAStartup;
LPWSACLEANUP lpWSACleanup;
LPWSASETLASTERROR lpWSASetLastError;
LPWSAASYNCGETHOSTBYNAME lpWSAAsyncGetHostByName;
LPWSAASYNCGETHOSTBYADDR lpWSAAsyncGetHostByAddr;
HMODULE hWinSockDLL = NULL;
// converts winsock errorcode to string
LPSTR WSAGetLastErrorString( int error_arg )
{
int error;
if (error_arg != 0)
{
error = error_arg;
}
else
{
error = lpWSAGetLastError();
}
switch( error )
{
case WSAENAMETOOLONG :
return "Name too long";
case WSANOTINITIALISED :
return "Not initialized";
case WSASYSNOTREADY :
return "System not ready";
case WSAVERNOTSUPPORTED :
return "Version is not supported";
case WSAESHUTDOWN :
return "Can't send after socket shutdown";
case WSAEINTR :
return "Interrupted system call";
case WSAHOST_NOT_FOUND :
return "Authoritative Answer: Host not found";
case WSATRY_AGAIN :
return "Non-Authoritative: Host not found, See NetworkStartup";
case WSANO_RECOVERY :
return "Non-recoverable error";
case WSANO_DATA :
return "No data record available";
case WSAEBADF :
return "Bad file number";
case WSAEWOULDBLOCK :
return "Operation would block";
case WSAEINPROGRESS :
return "Operation now in progress";
case WSAEALREADY :
return "Operation already in progress";
case WSAEFAULT :
return "Bad address";
case WSAEDESTADDRREQ :
return "Destination address required";
case WSAEMSGSIZE :
return "Message too long";
case WSAEPFNOSUPPORT :
return "Protocol family not supported";
case WSAENOTEMPTY :
return "Directory not empty";
case WSAEPROCLIM :
return "EPROCLIM returned";
case WSAEUSERS :
return "EUSERS returned";
case WSAEDQUOT :
return "Disk quota exceeded";
case WSAESTALE :
return "ESTALE returned";
case WSAEINVAL :
return "Invalid argument";
case WSAEMFILE :
return "Too many open files";
case WSAEACCES :
return "Access denied";
case WSAELOOP :
return "Too many levels of symbolic links";
case WSAEREMOTE :
return "The object is remote";
case WSAENOTSOCK :
return "Socket operation on non-socket";
case WSAEADDRNOTAVAIL :
return "Can't assign requested address";
case WSAEADDRINUSE :
return "Address already in use";
case WSAEAFNOSUPPORT :
return "Address family not supported by protocol family";
case WSAESOCKTNOSUPPORT :
return "Socket type not supported";
case WSAEPROTONOSUPPORT :
return "Protocol not supported";
case WSAENOBUFS :
return "No buffer space is supported";
case WSAETIMEDOUT :
return "Connection timed out";
case WSAEISCONN :
return "Socket is already connected";
case WSAENOTCONN :
return "Socket is not connected";
case WSAENOPROTOOPT :
return "Bad protocol option";
case WSAECONNRESET :
return "Connection reset by peer";
case WSAECONNABORTED :
return "Software caused connection abort";
case WSAENETDOWN :
return "Network is down";
case WSAENETRESET :
return "Network was reset";
case WSAECONNREFUSED :
return "Connection refused";
case WSAEHOSTDOWN :
return "Host is down";
case WSAEHOSTUNREACH :
return "Host is unreachable";
case WSAEPROTOTYPE :
return "Protocol is wrong type for socket";
case WSAEOPNOTSUPP :
return "Operation not supported on socket";
case WSAENETUNREACH :
return "ICMP network unreachable";
case WSAETOOMANYREFS :
return "Too many references";
default :
{
static char Buffer[32];
sprintf(Buffer,"Unknown %d",error);
return Buffer;
}
}
}
// startup network
BOOL NetworkStartup()
{
WSADATA WSAData;
// check if already started
if (hWinSockDLL != NULL)
{
return FALSE;
}
// load appropriate library
hWinSockDLL = LoadLibrary((LPSTR) "WSOCK32.DLL");
if (hWinSockDLL == NULL)
{
::MessageBox(::GetFocus(), "Cannot load WSOCK32.DLL", "Network Error", MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
return FALSE;
}
// fetch the routines
lpaccept = (LPACCEPT) GetProcAddress(hWinSockDLL, (LPSTR) "accept");
lpbind = (LPBIND) GetProcAddress(hWinSockDLL, (LPSTR) "bind");
lpclosesocket = (LPCLOSESOCKET) GetProcAddress(hWinSockDLL, (LPSTR) "closesocket");
lpconnect = (LPCONNECT) GetProcAddress(hWinSockDLL, (LPSTR) "connect");
lphtons = (LPHTONS) GetProcAddress(hWinSockDLL, (LPSTR) "htons");
lphtonl = (LPHTONL) GetProcAddress(hWinSockDLL, (LPSTR) "htonl");
lpinet_addr = (LPINET_ADDR) GetProcAddress(hWinSockDLL, (LPSTR) "inet_addr");
lpsetsockopt = (LPSETSOCKOPT) GetProcAddress(hWinSockDLL, (LPSTR) "setsockopt");
lplisten = (LPLISTEN) GetProcAddress(hWinSockDLL, (LPSTR) "listen");
lprecv = (LPRECV) GetProcAddress(hWinSockDLL, (LPSTR) "recv");
lpsend = (LPSEND) GetProcAddress(hWinSockDLL, (LPSTR) "send");
lpshutdown = (LPSHUTDOWN) GetProcAddress(hWinSockDLL, (LPSTR) "shutdown");
lpsocket = (LPSOCKET) GetProcAddress(hWinSockDLL, (LPSTR) "socket");
lpgethostname = (LPGETHOSTNAME) GetProcAddress(hWinSockDLL, (LPSTR) "gethostname");
lpWSAStartup = (LPWSASTARTUP) GetProcAddress(hWinSockDLL, (LPSTR) "WSAStartup");
lpWSACleanup = (LPWSACLEANUP) GetProcAddress(hWinSockDLL, (LPSTR) "WSACleanup");
lpWSAGetLastError = (LPWSAGETLASTERROR) GetProcAddress(hWinSockDLL, (LPSTR) "WSAGetLastError");
lpWSAAsyncGetHostByName = (LPWSAASYNCGETHOSTBYNAME) GetProcAddress(hWinSockDLL, (LPSTR) "WSAAsyncGetHostByName");
lpWSAAsyncGetHostByAddr = (LPWSAASYNCGETHOSTBYADDR) GetProcAddress(hWinSockDLL, (LPSTR) "WSAAsyncGetHostByAddr");
lpgethostbyname = (LPGETHOSTBYNAME) GetProcAddress(hWinSockDLL, (LPSTR) "gethostbyname");
lpWSAAsyncSelect = (LPWSAASYNCSELECT) GetProcAddress(hWinSockDLL, (LPSTR) "WSAAsyncSelect");
// tell winsock to wakeup
if (lpWSAStartup(MAKEWORD(1,1), &WSAData) != 0)
{
::MessageBox(::GetFocus(), WSAGetLastErrorString(0), "WSAStartup()", MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
return FALSE;
}
return TRUE;
}
// put everything back to original state
void NetworkShutdown()
{
// cleanup library
if (hWinSockDLL != NULL)
{
lpWSACleanup();
FreeLibrary(hWinSockDLL);
hWinSockDLL = NULL;
}
}