Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
8.2 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. wswrap.hxx
  5. Abstract:
  6. Wrapper to avoid loading winsock until really needed.
  7. This allows RPC applications to listen on winsock
  8. without pulling in all the winsock DLLs.
  9. Author:
  10. Mario Goertzel [MarioGo]
  11. Revision History:
  12. MarioGo 3/21/1996 Bits 'n pieces
  13. --*/
  14. #ifndef _WSWRAP_HXX_
  15. #define _WSWRAP_HXX_
  16. // For debugging only
  17. // #define NOWRAP
  18. extern BOOL fWinsockLoaded;
  19. extern BOOL RPC_WSAStartup(void);
  20. typedef INT (APIENTRY *LPFN_SETSERVICEW)(IN DWORD dwNameSpace,
  21. IN DWORD dwOperation,
  22. IN DWORD dwFlags,
  23. IN LPSERVICE_INFO lpServiceInfo,
  24. IN LPSERVICE_ASYNC_INFO lpServiceAsyncInfo,
  25. IN OUT LPDWORD lpdwStatusFlags
  26. );
  27. typedef INT (APIENTRY * LPFN_GETADDRESSBYNAMEA)(IN DWORD dwNameSpace,
  28. IN LPGUID lpServiceType,
  29. IN LPSTR lpServiceName OPTIONAL,
  30. IN LPINT lpiProtocols OPTIONAL,
  31. IN DWORD dwResolution,
  32. IN LPSERVICE_ASYNC_INFO lpServiceAsyncInfo OPTIONAL,
  33. IN OUT LPVOID lpCsaddrBuffer,
  34. IN OUT LPDWORD lpdwBufferLength,
  35. IN OUT LPSTR lpAliasBuffer OPTIONAL,
  36. IN OUT LPDWORD lpdwAliasBufferLength OPTIONAL
  37. );
  38. typedef INT (APIENTRY * LPFN_GETADDRESSBYNAMEW)(IN DWORD dwNameSpace,
  39. IN LPGUID lpServiceType,
  40. IN LPWSTR lpServiceName OPTIONAL,
  41. IN LPINT lpiProtocols OPTIONAL,
  42. IN DWORD dwResolution,
  43. IN LPSERVICE_ASYNC_INFO lpServiceAsyncInfo OPTIONAL,
  44. IN OUT LPVOID lpCsaddrBuffer,
  45. IN OUT LPDWORD lpdwBufferLength,
  46. IN OUT LPWSTR lpAliasBuffer OPTIONAL,
  47. IN OUT LPDWORD lpdwAliasBufferLength OPTIONAL
  48. );
  49. // N.B. The typedef for this should ultimately be in the Winsock headers
  50. typedef
  51. int
  52. (WSAAPI * LPFN_GETNAMEINFO)(
  53. IN const struct sockaddr FAR * sa,
  54. IN socklen_t salen,
  55. OUT char FAR * host,
  56. IN DWORD hostlen,
  57. OUT char FAR * serv,
  58. IN DWORD servlen,
  59. IN int flags
  60. );
  61. struct WINSOCK_FUNCTION_TABLE {
  62. LPFN_SOCKET psocket;
  63. LPFN_BIND pbind;
  64. LPFN_CLOSESOCKET pclosesocket;
  65. LPFN_GETSOCKNAME pgetsockname;
  66. LPFN_CONNECT pconnect;
  67. LPFN_LISTEN plisten;
  68. LPFN_SEND psend;
  69. LPFN_RECV precv;
  70. LPFN_SENDTO psendto;
  71. LPFN_RECVFROM precvfrom;
  72. LPFN_SETSOCKOPT psetsockopt;
  73. LPFN_GETSOCKOPT pgetsockopt;
  74. LPFN_INET_NTOA pinet_ntoa;
  75. LPFN_GETHOSTBYNAME pgethostbyname;
  76. LPFN_GETADDRESSBYNAMEA pGetAddressByNameA;
  77. LPFN_WSASOCKETW pWSASocket;
  78. LPFN_WSARECV pWSARecv;
  79. LPFN_WSARECVFROM pWSARecvFrom;
  80. LPFN_WSASEND pWSASend;
  81. LPFN_WSASENDTO pWSASendTo;
  82. LPFN_WSAPROVIDERCONFIGCHANGE pWSAProviderConfigChange;
  83. LPFN_WSAENUMPROTOCOLSW pWSAEnumProtocols;
  84. LPFN_WSAIOCTL pWSAIoctl;
  85. LPFN_GETADDRINFO pgetaddrinfo;
  86. LPFN_FREEADDRINFO pfreeaddrinfo;
  87. LPFN_GETNAMEINFO pgetnameinfo;
  88. LPFN_WSAGETOVERLAPPEDRESULT pWSAGetOverlappedResult;
  89. // these pointers must be at the end!
  90. // they will be filled in dynamically after the rest
  91. LPFN_WSARECVMSG pWSARecvMsg;
  92. };
  93. extern struct WINSOCK_FUNCTION_TABLE WFT;
  94. #ifndef NOWRAP
  95. #define socket(a,t,p) (WFT.psocket)((a),(t),(p))
  96. #define bind(s,sa,l) (WFT.pbind)((s),(sa),(l))
  97. #define getsockname(s,sa,l) (WFT.pgetsockname)((s),(sa),(l))
  98. #define connect(s,sa,l) (WFT.pconnect)((s),(sa),(l))
  99. #define listen(s,b) (WFT.plisten)((s),(b))
  100. #define send(s,b,l,f) (WFT.psend)((s),(b),(l),(f))
  101. #define recv(s,b,l,f) (WFT.precv)((s),(b),(l),(f))
  102. #define sendto(s,b,l,f,t,tl) (WFT.psendto)((s),(b),(l),(f),(t),(tl))
  103. #define recvfrom(s,b,l,fg,sa,fl) (WFT.precvfrom)((s),(b),(l),(fg),(sa),(fl))
  104. #define setsockopt(s,l,on,ov,ol) (WFT.psetsockopt)((s),(l),(on),(ov),(ol))
  105. #define getsockopt(s,l,on,ov,ol) (WFT.pgetsockopt)((s),(l),(on),(ov),(ol))
  106. #define inet_ntoa(i) (WFT.pinet_ntoa)((i))
  107. #define gethostbyname(x) (WFT.pgethostbyname)(x)
  108. #define GetAddressByNameA(ns,st,sn,p,r,si,b,bl,ab,abl) (WFT.pGetAddressByNameA)((ns),(st),(sn),(p),(r),(si),(b),(bl),(ab),(abl))
  109. #define AcceptEx(sl,sa,b,l1,l2,l3,l4,o) (WFT.pAcceptEx)((sl),(sa),(b),(l1),(l2),(l3),(l4),(o))
  110. #define GetAcceptExSockaddrs(b,dl,lal,ral,lsa,ll,rsa,rl) (WFT.pGetAcceptExSockaddrs)((b),(dl),(lal),(ral),(lsa),(ll),(rsa),(rl))
  111. #define WSASocketT(a,t,p,pi,g,f) (WFT.pWSASocket)((a),(t),(p),(pi),(g),(f))
  112. #define WSARecv(s,bf,c,pb,pf,ol,cr) (WFT.pWSARecv)((s),(bf),(c),(pb),(pf),(ol),(cr))
  113. #define WSARecvFrom(s,bf,c,pb,pf,sa,fl,ol,cr) (WFT.pWSARecvFrom((s),(bf),(c),(pb),(pf),(sa),(fl),(ol),(cr)))
  114. #define WSARecvMsg(s,m,pb,ol,cr) (WFT.pWSARecvMsg((s),(m),(pb),(ol),(cr)))
  115. #define WSASend(s,bf,c,b,f,ol,cr) (WFT.pWSASend((s),(bf),(c),(b),(f),(ol),(cr)))
  116. #define WSASendTo(s,bf,c,pb,pf,sa,fl,ol,cr) (WFT.pWSASendTo((s),(bf),(c),(pb),(pf),(sa),(fl),(ol),(cr)))
  117. #define WSAEnumProtocolsT(p,i,s) (WFT.pWSAEnumProtocols((p),(i),(s)))
  118. #define WSAProviderConfigChange(n,o,c) (WFT.pWSAProviderConfigChange((n),(o),(c)))
  119. #define WSAIoctl(s,i,ib,is,ob,os,oas,o,c) (WFT.pWSAIoctl((s),(i),(ib),(is),(ob),(os),(oas),(o),(c)))
  120. #define getaddrinfo(n, s, h, r) (WFT.pgetaddrinfo((n), (s), (h), (r)))
  121. #define freeaddrinfo(a) (WFT.pfreeaddrinfo((a)))
  122. #define getnameinfo(so, l, h, hl, sv, sl, f) (WFT.pgetnameinfo((so), (l), (h), (hl), (sv), (sl), (f)))
  123. #define WSAGetOverlappedResult(s, o, t, w, f) (WFT.pWSAGetOverlappedResult((s), (o), (t), (w), (f)))
  124. extern LONG TriedUsingAfd;
  125. extern void TryUsingAfdProc(void);
  126. #define TryUsingAfd() if (InterlockedCompareExchange(&TriedUsingAfd, 1, 0) == 0) TryUsingAfdProc()
  127. #if DBG
  128. #define closesocket(s) { \
  129. int retval = (WFT.pclosesocket)((s)); \
  130. if (retval == SOCKET_ERROR) { \
  131. TransDbgPrint((DPFLTR_RPCPROXY_ID, \
  132. DPFLTR_WARNING_LEVEL, \
  133. RPCTRANS "Close socket %d failed %p\n", \
  134. (s), \
  135. GetLastError())); \
  136. } \
  137. }
  138. #else
  139. #define closesocket(s) (WFT.pclosesocket)((s))
  140. #endif
  141. #define WSAGetLastError() GetLastError()
  142. //
  143. // Little endian dependency.
  144. //
  145. #define htons(s) RpcpByteSwapShort(s)
  146. #define ntohs(s) RpcpByteSwapShort(s)
  147. #define htonl(l) RpcpByteSwapLong(l)
  148. #define ntohl(l) RpcpByteSwapLong(l)
  149. //
  150. // Direct to AFD wrappers
  151. //
  152. int
  153. WSAAPI
  154. AFD_SendTo(
  155. SOCKET s,
  156. LPWSABUF lpBuffers,
  157. DWORD dwBufferCount,
  158. LPDWORD lpNumberOfBytesSent,
  159. DWORD dwFlags,
  160. const struct sockaddr FAR * lpTo,
  161. int iTolen,
  162. LPWSAOVERLAPPED lpOverlapped,
  163. LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
  164. );
  165. int
  166. WSAAPI
  167. AFD_RecvFrom(
  168. SOCKET s,
  169. LPWSABUF lpBuffers,
  170. DWORD dwBufferCount,
  171. LPDWORD lpNumberOfBytesRecvd,
  172. LPDWORD lpFlags,
  173. struct sockaddr FAR * lpFrom,
  174. LPINT lpFromlen,
  175. LPWSAOVERLAPPED lpOverlapped,
  176. LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
  177. );
  178. #endif // NOWRAP
  179. #endif // _WSWRAP_HXX_