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.

211 lines
3.6 KiB

  1. /*++
  2. Copyright 1996-1997 Microsoft Corporation
  3. Module Name:
  4. sockcomm.c
  5. Abstract:
  6. Implements a set of common operations for socket communication
  7. Revision History:
  8. --*/
  9. #include "sockcomm.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. BOOL
  13. InitWinsock(
  14. VOID
  15. )
  16. {
  17. ULONG nRes;
  18. WSADATA wsaData;
  19. WORD wVerRequested = 0x0101; // ver 1.1
  20. //
  21. // Init the sockets interface
  22. //
  23. nRes = WSAStartup(wVerRequested, &wsaData);
  24. if (nRes)
  25. {
  26. SetLastError(nRes);
  27. fprintf (stderr, "InitWinsock couldn't init winsock: %d\n", nRes);
  28. return (FALSE);
  29. }
  30. return (TRUE);
  31. }
  32. BOOL
  33. TermWinsock(
  34. VOID
  35. )
  36. {
  37. if (SOCKET_ERROR == WSACleanup())
  38. return (FALSE);
  39. else
  40. return (TRUE);
  41. }
  42. BOOL
  43. SendMsg(
  44. IN SOCKET s,
  45. IN ULONG cbBuf,
  46. IN VOID* pBuf
  47. )
  48. /*++
  49. Routine Description:
  50. Sends a message over the socket by first sending a ULONG that
  51. represents the size of the message followed by the message itself.
  52. Return Value:
  53. Returns TRUE is successful; otherwise FALSE is returned.
  54. --*/
  55. {
  56. //
  57. // send the size of the message
  58. //
  59. if (!SendBytes(s, sizeof(cbBuf), &cbBuf))
  60. return (FALSE);
  61. //
  62. // send the body of the message
  63. //
  64. if (cbBuf)
  65. {
  66. if (!SendBytes(s, cbBuf, pBuf))
  67. return (FALSE);
  68. }
  69. return (TRUE);
  70. }
  71. BOOL
  72. ReceiveMsg(
  73. IN SOCKET s,
  74. IN ULONG cbBuf,
  75. IN OUT VOID* pBuf,
  76. OUT ULONG *pcbRead
  77. )
  78. /*++
  79. Routine Description:
  80. Receives a message over the socket. The first ULONG in the message
  81. will be the message size. The remainder of the bytes will be the
  82. actual message.
  83. Return Value:
  84. Returns TRUE is successful; otherwise FALSE is returned.
  85. --*/
  86. {
  87. ULONG cbRead = 0;
  88. ULONG cbData = 0;
  89. *pcbRead = 0;
  90. //
  91. // find out how much data is in the message
  92. //
  93. if (!ReceiveBytes(s, sizeof(cbData), &cbData, &cbRead))
  94. return (FALSE);
  95. if (sizeof(cbData) != cbRead)
  96. return (FALSE);
  97. //
  98. // Read the full message
  99. //
  100. if (cbData)
  101. {
  102. if (!ReceiveBytes(s, cbData, pBuf, &cbRead))
  103. return (FALSE);
  104. if (cbRead != cbData)
  105. return (FALSE);
  106. *pcbRead = cbRead;
  107. }
  108. return (TRUE);
  109. }
  110. BOOL
  111. SendBytes(
  112. IN SOCKET s,
  113. IN ULONG cbBuf,
  114. IN VOID* pBuf
  115. )
  116. {
  117. PBYTE pTemp = (BYTE*) pBuf;
  118. ULONG cbSent = 0;
  119. ULONG cbRemaining = cbBuf;
  120. if (0 == cbBuf)
  121. return (TRUE);
  122. while (cbRemaining)
  123. {
  124. cbSent = send(s, pTemp, cbRemaining, 0);
  125. if (SOCKET_ERROR == cbSent)
  126. {
  127. fprintf (stderr, "SendBytes send failed: %u\n", GetLastError());
  128. return FALSE;
  129. }
  130. pTemp += cbSent;
  131. cbRemaining -= cbSent;
  132. }
  133. return TRUE;
  134. }
  135. BOOL
  136. ReceiveBytes(
  137. IN SOCKET s,
  138. IN ULONG cbBuf,
  139. IN OUT VOID* pBuf,
  140. OUT ULONG* pcbRead
  141. )
  142. {
  143. PBYTE pTemp = (BYTE*) pBuf;
  144. ULONG cbRead = 0;
  145. ULONG cbRemaining = cbBuf;
  146. while (cbRemaining)
  147. {
  148. cbRead = recv(s, pTemp, cbRemaining, 0);
  149. if (0 == cbRead)
  150. break;
  151. if (SOCKET_ERROR == cbRead)
  152. {
  153. fprintf (stderr, "ReceiveBytes recv failed: %u\n", GetLastError());
  154. return FALSE;
  155. }
  156. cbRemaining -= cbRead;
  157. pTemp += cbRead;
  158. }
  159. *pcbRead = cbBuf - cbRemaining;
  160. return TRUE;
  161. }