Source code of Windows XP (NT5)
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.

326 lines
8.3 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1993 **/
  4. /**********************************************************************/
  5. /*
  6. sockutil.c
  7. This module contains utility routines for managing & manipulating
  8. sockets.
  9. FILE HISTORY:
  10. VladimV 30-May-1995 Created
  11. */
  12. #define INCL_INETSRV_INCS
  13. #include "smtpinc.h"
  14. extern HINSTANCE g_WSockMsgDll;
  15. //
  16. // Public functions.
  17. //
  18. /*******************************************************************
  19. NAME: InitializeSockets
  20. SYNOPSIS: Initializes socket access. Among other things, this
  21. routine is responsible for connecting to WinSock,
  22. and creating the connection thread.
  23. RETURNS: APIERR - NO_ERROR if successful, otherwise a Win32
  24. error code.
  25. NOTES: This routine may only be called by a single thread
  26. of execution; it is not necessarily multi-thread safe.
  27. HISTORY:
  28. ********************************************************************/
  29. APIERR InitializeSockets( VOID )
  30. {
  31. WSADATA wsadata;
  32. SOCKERR serr;
  33. TraceFunctEnter( "InitializeSockets" );
  34. //
  35. // Connect to WinSock.
  36. //
  37. serr = WSAStartup( MAKEWORD( 1, 1 ), &wsadata );
  38. if( serr != 0 ) {
  39. // g_pInetSvc->LogEvent( SMTP_EVENT_CANNOT_INITIALIZE_WINSOCK,
  40. // 0, (const CHAR **) NULL, serr);
  41. FatalTrace( 0, "cannot initialize WinSock, socket error %d", serr); _ASSERT( FALSE);
  42. } else {
  43. DebugTrace( 0, "Sockets initialized");
  44. }
  45. TraceFunctLeave();
  46. return serr;
  47. } // InitializeSockets
  48. /*******************************************************************
  49. NAME: TerminateSockets
  50. SYNOPSIS: Terminate socket access. This routine is responsible
  51. for closing the connection socket(s) and detaching
  52. from WinSock.
  53. NOTES: This routine may only be called by a single thread
  54. of execution; it is not necessarily multi-thread safe.
  55. HISTORY:
  56. ********************************************************************/
  57. VOID TerminateSockets( VOID )
  58. {
  59. SOCKERR serr;
  60. TraceFunctEnter( "TerminateSockets" );
  61. //
  62. // Disconnect from WinSock.
  63. //
  64. serr = WSACleanup();
  65. if( serr != 0 ) {
  66. FatalTrace( 0, "cannot terminate WinSock, error=%d", serr); _ASSERT( FALSE);
  67. } else {
  68. DebugTrace( 0, "Sockets terminated");
  69. }
  70. TraceFunctLeave();
  71. } // TerminateSockets
  72. /*******************************************************************
  73. NAME: GetSockErrorMessage
  74. SYNOPSIS: This routine is responsible for getting winsock
  75. error messages stored in smtpsvc.dll
  76. HISTORY:
  77. ********************************************************************/
  78. DWORD GetSockErrorMessage(DWORD dwErrno, char * ErrorBuf, DWORD ErrorBufSize)
  79. {
  80. DWORD msglen;
  81. DWORD usMsgNum;
  82. switch (dwErrno)
  83. {
  84. case WSAENAMETOOLONG:
  85. usMsgNum = SMTP_WSAENAMETOOLONG;
  86. break;
  87. case WSASYSNOTREADY:
  88. usMsgNum = SMTP_WSASYSNOTREADY;
  89. break;
  90. case WSAVERNOTSUPPORTED:
  91. usMsgNum = SMTP_WSAVERNOTSUPPORTED;
  92. break;
  93. case WSAESHUTDOWN:
  94. usMsgNum = SMTP_WSAESHUTDOWN;
  95. break;
  96. case WSAEINTR:
  97. usMsgNum = SMTP_WSAEINTR;
  98. break;
  99. case WSAHOST_NOT_FOUND:
  100. usMsgNum = SMTP_WSAHOST_NOT_FOUND;
  101. break;
  102. case WSATRY_AGAIN:
  103. usMsgNum = SMTP_WSATRY_AGAIN;
  104. break;
  105. case WSANO_RECOVERY:
  106. usMsgNum = SMTP_WSANO_RECOVERY;
  107. break;
  108. case WSANO_DATA:
  109. usMsgNum = SMTP_WSANO_DATA;
  110. break;
  111. case WSAEBADF:
  112. usMsgNum = SMTP_WSAEBADF;
  113. break;
  114. case WSAEWOULDBLOCK:
  115. usMsgNum = SMTP_WSAEWOULDBLOCK;
  116. break;
  117. case WSAEINPROGRESS:
  118. usMsgNum = SMTP_WSAEINPROGRESS;
  119. break;
  120. case WSAEALREADY:
  121. usMsgNum = SMTP_WSAEALREADY;
  122. break;
  123. case WSAEFAULT:
  124. usMsgNum = SMTP_WSAEFAULT;
  125. break;
  126. case WSAEDESTADDRREQ:
  127. usMsgNum = SMTP_WSAEDESTADDRREQ;
  128. break;
  129. case WSAEMSGSIZE:
  130. usMsgNum = SMTP_WSAEMSGSIZE;
  131. break;
  132. case WSAEPFNOSUPPORT:
  133. usMsgNum = SMTP_WSAEPFNOSUPPORT;
  134. break;
  135. case WSAENOTEMPTY:
  136. usMsgNum = SMTP_WSAENOTEMPTY;
  137. break;
  138. case WSAEPROCLIM:
  139. usMsgNum = SMTP_WSAEPROCLIM;
  140. break;
  141. case WSAEUSERS:
  142. usMsgNum = SMTP_WSAEUSERS;
  143. break;
  144. case WSAEDQUOT:
  145. usMsgNum = SMTP_WSAEDQUOT;
  146. break;
  147. case WSAESTALE:
  148. usMsgNum = SMTP_WSAESTALE;
  149. break;
  150. case WSAEINVAL:
  151. usMsgNum = SMTP_WSAEINVAL;
  152. break;
  153. case WSAEMFILE:
  154. usMsgNum = SMTP_WSAEMFILE;
  155. break;
  156. case WSAELOOP:
  157. usMsgNum = SMTP_WSAELOOP;
  158. break;
  159. case WSAEREMOTE:
  160. usMsgNum = SMTP_WSAEREMOTE;
  161. break;
  162. case WSAENOTSOCK:
  163. usMsgNum = SMTP_WSAENOTSOCK;
  164. break;
  165. case WSAEADDRNOTAVAIL:
  166. usMsgNum = SMTP_WSAEADDRNOTAVAIL;
  167. break;
  168. case WSAEADDRINUSE:
  169. usMsgNum = SMTP_WSAEADDRINUSE;
  170. break;
  171. case WSAEAFNOSUPPORT:
  172. usMsgNum = SMTP_WSAEAFNOSUPPORT;
  173. break;
  174. case WSAESOCKTNOSUPPORT:
  175. usMsgNum = SMTP_WSAESOCKTNOSUPPORT;
  176. break;
  177. case WSAEPROTONOSUPPORT:
  178. usMsgNum = SMTP_WSAEPROTONOSUPPORT;
  179. break;
  180. case WSAENOBUFS:
  181. usMsgNum = SMTP_WSAENOBUFS;
  182. break;
  183. case WSAETIMEDOUT:
  184. usMsgNum = SMTP_WSAETIMEDOUT;
  185. break;
  186. case WSAEISCONN:
  187. usMsgNum = SMTP_WSAEISCONN;
  188. break;
  189. case WSAENOTCONN:
  190. usMsgNum = SMTP_WSAENOTCONN;
  191. break;
  192. case WSAENOPROTOOPT:
  193. usMsgNum = SMTP_WSAENOPROTOOPT;
  194. break;
  195. case WSAECONNRESET:
  196. usMsgNum = SMTP_WSAECONNRESET;
  197. break;
  198. case WSAECONNABORTED:
  199. usMsgNum = SMTP_WSAECONNABORTED;
  200. break;
  201. case WSAENETDOWN:
  202. usMsgNum = SMTP_WSAENETDOWN;
  203. break;
  204. case WSAENETRESET:
  205. usMsgNum = SMTP_WSAENETRESET;
  206. break;
  207. case WSAECONNREFUSED:
  208. usMsgNum = SMTP_WSAECONNREFUSED;
  209. break;
  210. case WSAEHOSTDOWN:
  211. usMsgNum = SMTP_WSAEHOSTDOWN;
  212. break;
  213. case WSAEHOSTUNREACH:
  214. usMsgNum = SMTP_WSAEHOSTUNREACH;
  215. break;
  216. case WSAEPROTOTYPE:
  217. usMsgNum = SMTP_WSAEPROTOTYPE;
  218. break;
  219. case WSAEOPNOTSUPP:
  220. usMsgNum = SMTP_WSAEOPNOTSUPP;
  221. break;
  222. case WSAENETUNREACH:
  223. usMsgNum = SMTP_WSAENETUNREACH;
  224. break;
  225. case WSAETOOMANYREFS:
  226. usMsgNum = SMTP_WSAETOOMANYREFS;
  227. break;
  228. default:
  229. usMsgNum = dwErrno;
  230. break;
  231. }
  232. // call the OS using US/ASCII
  233. msglen = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, g_WSockMsgDll, usMsgNum,
  234. MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
  235. ErrorBuf, ErrorBufSize, NULL);
  236. return(msglen);
  237. }
  238. /*++
  239. Name :
  240. MyStrChr
  241. Description:
  242. Works just like strchr(), but takes the size
  243. of the buffer
  244. Arguments:
  245. Line - Buffer to search
  246. Val - Value to look for in the buffer
  247. LineSize - Size of Line
  248. Returns:
  249. If Val is found, a pointer to Val is
  250. returned, els NULL
  251. --*/
  252. char * MyStrChr(char *Line, unsigned char Val, DWORD LineSize)
  253. {
  254. register DWORD Loop = 0;
  255. unsigned char * Match = NULL;
  256. register unsigned char * SearchPtr;
  257. ASSERT( Line != NULL);
  258. if(LineSize == 0)
  259. return NULL;
  260. SearchPtr = (unsigned char *) Line;
  261. // Scan the entire buffer looking for Val
  262. for(Loop = 0; Loop < LineSize; Loop++)
  263. {
  264. if (SearchPtr[Loop] == Val)
  265. {
  266. Match = &SearchPtr[Loop];
  267. break;
  268. }
  269. } // for
  270. return (char *) Match;
  271. }