summaryrefslogtreecommitdiff
path: root/protocols/MRA/src/Sdk/SocketFunctions.h
blob: 0d609d80f927fd1db734f6a43a6c041dd3db4389 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright (c) 2003 Rozhuk Ivan <rozhuk.im@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */



#if !defined(AFX_SOCKET_FUNCTIONS__H__INCLUDED_)
#define AFX_SOCKET_FUNCTIONS__H__INCLUDED_

#if(_WIN32_WINNT >= 0x0501)
static LPFN_CONNECTEX ConnectEx=NULL;
static LPFN_DISCONNECTEX DisconnectEx=NULL;
static LPFN_TRANSMITPACKETS TransmitPackets=NULL;
#endif


#ifndef NTOHS

__inline u_short USHORT_FLIP(u_short usIn)
{	
return(((usIn<<8) | (usIn>>8)));
}
#define NTOHS(in)   USHORT_FLIP(in)
#define HTONS(in)   USHORT_FLIP(in)

#endif



#ifndef NTOHL

__inline u_long ULONG_FLIP(u_long ulIn)
{
#if defined (_M_IA64) || defined (_M_AMD64)
return((((ulIn<<8) & 0x00ff0000) | (ulIn<<24) | ((ulIn>>8) & 0x0000ff00) | (ulIn>>24)));
#else
	__asm
	{
		mov		eax,ulIn
		bswap	eax
		mov		ulIn,eax
	};
return(ulIn);
#endif
}

#define NTOHL(in) ULONG_FLIP(in)
#define HTONL(in) ULONG_FLIP(in)

#endif





#define CLOSE_SOCKET(skt) if (skt) {closesocket(skt);skt=INVALID_SOCKET;}



__inline BOOL SocketGetACCEPTCONN(SOCKET skt)
{
	BOOL bRet;
	int iSize=sizeof(BOOL);
	if (getsockopt(skt,SOL_SOCKET,SO_ACCEPTCONN,(char*)&bRet,(int*)&iSize)!=NO_ERROR) bRet=FALSE;
return(bRet);
}


__inline BOOL SocketGetBROADCAST(SOCKET skt)
{
	BOOL bRet;
	int iSize=sizeof(BOOL);
	if (getsockopt(skt,SOL_SOCKET,SO_BROADCAST,(char*)&bRet,(int*)&iSize)!=NO_ERROR) bRet=FALSE;
return(bRet);
}

__inline int SocketSetBROADCAST(SOCKET skt,BOOL bBroadcast)
{
return(setsockopt(skt,SOL_SOCKET,SO_BROADCAST,(const char*)&bBroadcast,sizeof(BOOL)));
}



__inline int SocketGetCONNECT_TIME(SOCKET skt)
{
	int iSeconds,iSize=sizeof(int);
	if (getsockopt(skt,SOL_SOCKET,SO_CONNECT_TIME,(char*)&iSeconds,(int*)&iSize)!=NO_ERROR) iSeconds=-1;
return(iSeconds);
}


__inline BOOL SocketGetKEEPALIVE(SOCKET skt)
{
	BOOL bRet;
	int iSize=sizeof(BOOL);
	if (getsockopt(skt,SOL_SOCKET,SO_KEEPALIVE,(char*)&bRet,(int*)&iSize)!=NO_ERROR) bRet=FALSE;
return(bRet);
}

__inline int SocketSetKEEPALIVE(SOCKET skt,BOOL bKeepAlive)
{
return(setsockopt(skt,SOL_SOCKET,SO_KEEPALIVE,(const char*)&bKeepAlive,sizeof(BOOL)));
}



__inline int SocketGetMAX_MSG_SIZE(SOCKET skt)
{
	int iMaxMsgSize,iSize=sizeof(int);
	if (getsockopt(skt,SOL_SOCKET,SO_MAX_MSG_SIZE,(char*)&iMaxMsgSize,(int*)&iSize)!=NO_ERROR) iMaxMsgSize=-1;
return(iMaxMsgSize);
}


__inline int SocketSetEXCLUSIVEADDRUSE(SOCKET skt,BOOL bExclusiveAddrUse)
{
return(setsockopt(skt,SOL_SOCKET,SO_EXCLUSIVEADDRUSE,(const char*)&bExclusiveAddrUse,sizeof(BOOL)));
}


__inline BOOL SocketGetREUSEADDR(SOCKET skt)
{
	BOOL bRet;
	int iSize=sizeof(BOOL);
	if (getsockopt(skt,SOL_SOCKET,SO_REUSEADDR,(char*)&bRet,(int*)&iSize)!=NO_ERROR) bRet=FALSE;
return(bRet);
}

__inline int SocketSetREUSEADDR(SOCKET skt,BOOL bReuseAddr)
{
return(setsockopt(skt,SOL_SOCKET,SO_REUSEADDR,(const char*)&bReuseAddr,sizeof(BOOL)));
}


__inline int SocketSetRCVBUF(SOCKET skt,unsigned int uiBuffSize)
{
return(setsockopt(skt,SOL_SOCKET,SO_RCVBUF,(const char*)&uiBuffSize,sizeof(int)));
}


__inline int SocketSetSNDBUF(SOCKET skt,unsigned int uiBuffSize)
{
return(setsockopt(skt,SOL_SOCKET,SO_SNDBUF,(const char*)&uiBuffSize,sizeof(int)));
}



__inline int SocketSetUPDATE_ACCEPT_CONTEXT(SOCKET skt,SOCKET sktAccept)
{
return(setsockopt(skt,SOL_SOCKET,SO_UPDATE_ACCEPT_CONTEXT,(char*)&sktAccept,sizeof(SOCKET)));
}


#if(_WIN32_WINNT >= 0x0501)
__inline int SocketSetUPDATE_CONNECT_CONTEXT(SOCKET skt)
{
return(setsockopt(skt,SOL_SOCKET,SO_UPDATE_CONNECT_CONTEXT,NULL,0));
}
#endif




__inline BOOL SocketGetHDRINCL(SOCKET skt)
{
	BOOL bRet;
	int iSize=sizeof(BOOL);
	if (getsockopt(skt,IPPROTO_IP,IP_HDRINCL,(char*)&bRet,(int*)&iSize)!=NO_ERROR) bRet=FALSE;
return(bRet);
}

__inline int SocketSetHDRINCL(SOCKET skt,BOOL bProvideIPHdr)
{
return(setsockopt(skt,IPPROTO_IP,IP_HDRINCL,(const char*)&bProvideIPHdr,sizeof(DWORD)));
}


#define TOS_DEFAULT				0
#define TOS_MIN_MONETARY_COST	2
#define TOS_MIN_RELIABILITY		4
#define TOS_MAX_THROUGHPUT		8
#define TOS_MIN_DELAY			16
#define TOS_MAX_SECURITY		30

__inline int SocketSetTOS(SOCKET skt,DWORD dwTOS)
{
return(setsockopt(skt,IPPROTO_IP,IP_TOS,(const char*)&dwTOS,sizeof(DWORD)));
}


__inline int SocketSetTTL(SOCKET skt,UINT iTTL)
{
return(setsockopt(skt,IPPROTO_IP,IP_TTL,(const char*)&iTTL,sizeof(DWORD)));
}


__inline int SocketSetTCP_NODELAY(SOCKET skt,BOOL bTCPNoDelay)
{
return(setsockopt(skt,IPPROTO_TCP,TCP_NODELAY,(const char*)&bTCPNoDelay,sizeof(DWORD)));
}

/*
int                  // OUT: whatever setsockopt() returns
join_source_group(int sd, u_int32 grpaddr, 
   u_int32 srcaddr, u_int32 iaddr)
{
   struct ip_mreq_source imr; 
   
   imr.imr_multiaddr.s_addr  = grpaddr;
   imr.imr_sourceaddr.s_addr = srcaddr;
   imr.imr_interface.s_addr  = iaddr;
   return setsockopt(sd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &imr, sizeof(imr));  
}

int
leave_source_group(int sd, u_int32 grpaddr, 
   u_int32 srcaddr, u_int32 iaddr)
{
   struct ip_mreq_source imr;

   imr.imr_multiaddr.s_addr  = grpaddr;
   imr.imr_sourceaddr.s_addr = srcaddr;
   imr.imr_interface.s_addr  = iaddr;
   return setsockopt(sd, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &imr, sizeof(imr));
}*/


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
__inline BOOL WINAPI DisconnectExTF(SOCKET hSocket,LPOVERLAPPED lpOverlapped,DWORD dwFlags,DWORD reserved)
{
	reserved=0;
	dwFlags&=TF_REUSE_SOCKET;
	dwFlags|=TF_DISCONNECT;
return(TransmitFile(hSocket,NULL,0,0,lpOverlapped,NULL,dwFlags));
}



__inline DWORD SocketsInitialize()
{
	DWORD dwRetErrorCode;
	WSADATA wsaData;

	if ((dwRetErrorCode=WSAStartup(MAKEWORD(1,1),&wsaData))==NO_ERROR)
	{// version 1.1 OK
		WSACleanup();
		
		dwRetErrorCode=WSAStartup(wsaData.wHighVersion,&wsaData);
	}
return(dwRetErrorCode);
}


#if(_WIN32_WINNT >= 0x0501)
__inline DWORD SocketsInitializeEx(DWORD dwFlags)
{
	DWORD dwRetErrorCode;
	WSADATA wsaData;

	dwFlags=0;
	if ((dwRetErrorCode=WSAStartup(MAKEWORD(1,1),&wsaData))==NO_ERROR)
	{// version 1.1 OK
		WSACleanup();
		if ((dwRetErrorCode=WSAStartup(wsaData.wHighVersion,&wsaData))==NO_ERROR)
		{// max version initialized
			SOCKET skt;

			if ((skt=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,0,0,WSA_FLAG_OVERLAPPED))!=INVALID_SOCKET)
			{
				DWORD dwSize;
				GUID ExtensionGuidCONNECTEX=WSAID_CONNECTEX;
				GUID ExtensionGuidDISCONNECTEX=WSAID_DISCONNECTEX;
				GUID ExtensionGuidTRANSMITPACKETS=WSAID_TRANSMITPACKETS;

				dwRetErrorCode=NO_ERROR;

				if (WSAIoctl(skt,SIO_GET_EXTENSION_FUNCTION_POINTER,&ExtensionGuidCONNECTEX,sizeof(GUID),&ConnectEx,sizeof(FARPROC),&dwSize,NULL,NULL)==0)
				{
					if (ConnectEx==NULL) dwRetErrorCode=WSAGetLastError();
				}else{
					dwRetErrorCode=WSAGetLastError();
				}


				if (WSAIoctl(skt,SIO_GET_EXTENSION_FUNCTION_POINTER,&ExtensionGuidDISCONNECTEX,sizeof(GUID),&DisconnectEx,sizeof(FARPROC),&dwSize,NULL,NULL)==0)
				{
					if (DisconnectEx==NULL)
					{
						DisconnectEx=DisconnectExTF;
						//dwRetErrorCode=WSAGetLastError();
					}
				}else{
					dwRetErrorCode=WSAGetLastError();
				}

				if (WSAIoctl(skt,SIO_GET_EXTENSION_FUNCTION_POINTER,&ExtensionGuidTRANSMITPACKETS,sizeof(GUID),&TransmitPackets,sizeof(FARPROC),&dwSize,NULL,NULL)==0)
				{
					if (TransmitPackets==NULL) dwRetErrorCode=WSAGetLastError();
				}else{
					dwRetErrorCode=WSAGetLastError();
				}

				closesocket(skt);
			}else{
				dwRetErrorCode=WSAGetLastError();
			}
		}
	}
return(dwRetErrorCode);
}
#endif



////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
///////////////////////////SOCKADDR_STORAGE/////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#if(_WIN32_WINNT >= 0x0501)

__inline DWORD SockAddrInDataSet(LPCVOID lpcsasSockAddrStorage,DWORD dwPort,LPCVOID lpcAddress,size_t dwAddressSize)
{
	DWORD dwRetErrorCode=NO_ERROR;

	if (lpcsasSockAddrStorage && lpcAddress)
	{
		switch(dwAddressSize){
		case sizeof(in_addr):
			((sockaddr_in*)lpcsasSockAddrStorage)->sin_family=AF_INET;
			((sockaddr_in*)lpcsasSockAddrStorage)->sin_port=HTONS((WORD)dwPort);
			(*((DWORD*)&(((sockaddr_in*)lpcsasSockAddrStorage)->sin_addr)))=(*((DWORD*)lpcAddress));
			break;
		case sizeof(in6_addr):
			((sockaddr_in6*)lpcsasSockAddrStorage)->sin6_family=AF_INET6;
			((sockaddr_in6*)lpcsasSockAddrStorage)->sin6_port=HTONS((WORD)dwPort);
			memmove(&(((sockaddr_in6*)lpcsasSockAddrStorage)->sin6_addr),lpcAddress,sizeof(in6_addr));
			break;
		default:
			dwRetErrorCode=ERROR_INVALID_PARAMETER;
			break;
		}
	}else{
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}


__inline DWORD SockAddrInPortSet(LPCVOID lpcsasSockAddrStorage,DWORD dwPort)
{
	DWORD dwRetErrorCode=NO_ERROR;

	if (lpcsasSockAddrStorage)
	{
		switch(((SOCKADDR_STORAGE*)lpcsasSockAddrStorage)->ss_family){
		case AF_INET:
			((sockaddr_in*)lpcsasSockAddrStorage)->sin_port=HTONS((WORD)dwPort);
			break;
		case AF_INET6:
			((sockaddr_in6*)lpcsasSockAddrStorage)->sin6_port=HTONS((WORD)dwPort);
			break;
		default:
			dwRetErrorCode=ERROR_INVALID_PARAMETER;
			break;
		}
	}else{
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}


__inline DWORD SockAddrInAddressSet(LPCVOID lpcsasSockAddrStorage,LPCVOID lpcAddress,size_t dwAddressSize)
{
	DWORD dwRetErrorCode=NO_ERROR;

	if (lpcsasSockAddrStorage && lpcAddress)
	{
		switch(dwAddressSize){
		case sizeof(in_addr):
			((sockaddr_in*)lpcsasSockAddrStorage)->sin_family=AF_INET;
			(*((DWORD*)&(((sockaddr_in*)lpcsasSockAddrStorage)->sin_addr)))=(*((DWORD*)lpcAddress));
			break;
		case sizeof(in6_addr):
			((sockaddr_in6*)lpcsasSockAddrStorage)->sin6_family=AF_INET6;
			memmove(&(((sockaddr_in6*)lpcsasSockAddrStorage)->sin6_addr),lpcAddress,sizeof(in6_addr));
			break;
		default:
			dwRetErrorCode=ERROR_INVALID_PARAMETER;
			break;
		}
	}else{
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}


__inline DWORD SockAddrAddressSet(LPCVOID lpcsasSockAddrStorage,LPCVOID lpcAddress,size_t dwAddressSize)
{
	DWORD dwRetErrorCode=NO_ERROR;

	if (lpcsasSockAddrStorage && lpcAddress)
	{
		switch(dwAddressSize){
		case sizeof(in_addr):
			((sockaddr*)lpcsasSockAddrStorage)->sa_family=AF_INET;
			(*((DWORD*)&(((sockaddr*)lpcsasSockAddrStorage)->sa_data)))=(*((DWORD*)lpcAddress));
			break;
		case sizeof(in6_addr):
			((sockaddr*)lpcsasSockAddrStorage)->sa_family=AF_INET6;
			memmove(&(((sockaddr*)lpcsasSockAddrStorage)->sa_data),lpcAddress,sizeof(in6_addr));
			break;
		default:
			dwRetErrorCode=ERROR_INVALID_PARAMETER;
			break;
		}
	}else{
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}


__inline size_t SockAddrGetSize(LPCVOID lpcsasSockAddrStorage)
{
	size_t dwRet;

	if (lpcsasSockAddrStorage)
	{
		switch(((SOCKADDR_STORAGE*)lpcsasSockAddrStorage)->ss_family){
		case AF_INET:
			dwRet=sizeof(sockaddr_in);
			break;
		case AF_INET6:
			dwRet=sizeof(sockaddr_in6);
			break;
		default:
			dwRet=sizeof(SOCKADDR_STORAGE);
			break;
		}
	}else{
		dwRet=0;
	}
return(dwRet);
}


#endif
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////


#endif //AFX_SOCKET_FUNCTIONS__H__INCLUDED_