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.

263 lines
6.2 KiB

  1. /*************************************************************************************************\
  2. *
  3. * SOCKETS.C
  4. *
  5. * This file contains routines used for establishing Sockets connections.
  6. *
  7. \*************************************************************************************************/
  8. #include <precomp.h>
  9. #include <winsock.h>
  10. /* ---- Local variables and #defines ----
  11. */
  12. WSADATA WSAData;
  13. #define MAX_PENDING_CONNECTS 4 /* The backlog allowed for listen() */
  14. static PCHAR DBG_WSAERRORTEXT = "%s failed at line %d in %s: Error %d\n";
  15. #define WSAERROR(func) \
  16. // ERROR(( DBG_WSAERRORTEXT, func, __LINE__, __FILE__, WSAGetLastError() ))
  17. /* Error message macro:
  18. */
  19. #ifdef SOCKETS
  20. #undef SOCKETS
  21. #endif
  22. #define SOCKETS( args ) DBGMSG( DBG_SOCKETS, args )
  23. /* ---- Local function prototypes ----
  24. */
  25. /* ---- Function definitions ----
  26. */
  27. /****************************************************************************\
  28. *
  29. * FUNCTION: FillAddr(HWND, PSOCKADDR_IN, LPSTR)
  30. *
  31. * PURPOSE: Retrieves the IP address and port number.
  32. *
  33. * COMMENTS:
  34. * This function is called in two conditions.
  35. * 1.) When a client is preparing to call connect(), or
  36. * 2.) When a server host is going to call bind(), listen() and
  37. * accept().
  38. * In both situations, a SOCKADDR_IN structure is filled.
  39. * However, different fields are filled depending on the condition.
  40. *
  41. * ASSUMPTION:
  42. * bConnect determines if the socket address is being set up for a listen()
  43. * (bConnect == TRUE) or a connect() (bConnect == FALSE)
  44. *
  45. *
  46. *\***************************************************************************/
  47. BOOL
  48. FillAddr(
  49. HWND hWnd,
  50. PSOCKADDR_IN psin,
  51. LPSTR pServerName
  52. )
  53. {
  54. DWORD dwSize;
  55. PHOSTENT phe;
  56. char szTemp[200];
  57. CHAR szBuff[80];
  58. psin->sin_family = AF_INET;
  59. /*
  60. ** If we are setting up for a listen() call (pServerName == NULL),
  61. ** fill servent with our address.
  62. */
  63. if (!pServerName)
  64. {
  65. /*
  66. ** Retrieve my ip address. Assuming the hosts file in
  67. ** in %systemroot%/system/drivers/etc/hosts contains my computer name.
  68. */
  69. dwSize = sizeof(szBuff);
  70. GetComputerName(szBuff, &dwSize);
  71. CharLowerBuff( szBuff, dwSize );
  72. }
  73. /* gethostbyname() fails if the remote name is in upper-case characters!
  74. */
  75. else
  76. {
  77. strcpy( szBuff, pServerName );
  78. CharLowerBuff( szBuff, strlen( szBuff ) );
  79. }
  80. phe = gethostbyname(szBuff);
  81. if (phe == NULL) {
  82. wsprintf( szTemp, "%d is the error. Make sure '%s' is"
  83. " listed in the hosts file.", WSAGetLastError(), szBuff );
  84. MessageBox(hWnd, szTemp, "gethostbyname() failed.", MB_OK);
  85. return FALSE;
  86. }
  87. memcpy((char FAR *)&(psin->sin_addr), phe->h_addr, phe->h_length);
  88. return TRUE;
  89. }
  90. /* SocketConnect
  91. *
  92. * The counterpart to SocketListen.
  93. * Creates a socket and initializes it with the supplied TCP/IP
  94. * port address, then connects to a listening server.
  95. * The returned socket can be used to send() and recv() data.
  96. *
  97. * Parameters: TCPPort - The port to use.
  98. * pSocket - A pointer to a SOCKET, which will be filled in
  99. * if the call succeeds.
  100. *
  101. * Returns: TRUE if successful.
  102. *
  103. *
  104. * Created 16 November 1993 (andrewbe)
  105. *
  106. */
  107. BOOL SocketConnect( LPSTR pstrServerName, u_short TCPPort, SOCKET *pSocket )
  108. {
  109. SOCKET Socket;
  110. SOCKADDR_IN dest_sin; /* DESTination Socket INternet */
  111. /* Create a socket:
  112. */
  113. Socket = socket( AF_INET, SOCK_STREAM, 0);
  114. if (Socket == INVALID_SOCKET)
  115. {
  116. WSAERROR( "socket()");
  117. return FALSE;
  118. }
  119. if (!FillAddr( NULL, &dest_sin, pstrServerName ) )
  120. {
  121. return FALSE;
  122. }
  123. dest_sin.sin_port = htons( TCPPort );
  124. /* Someone must be listen()ing for this to succeed:
  125. */
  126. if (connect( Socket, (PSOCKADDR)&dest_sin, sizeof( dest_sin)) == SOCKET_ERROR)
  127. {
  128. closesocket( Socket );
  129. WSAERROR("connect()");
  130. MessageBox(NULL,
  131. "ERROR: Could not connect the socket. "
  132. "It may be that the hardcoded Sleep() value "
  133. "on the caller's side is not long enough.",
  134. "Video Conferencing Prototype", MB_OK);
  135. return FALSE;
  136. }
  137. *pSocket = Socket;
  138. return TRUE;
  139. }
  140. /* SocketListen
  141. *
  142. * The counterpart to SocketConnect.
  143. * Creates a socket and initializes it with the supplied TCP/IP
  144. * port address, then listens for a connecting client.
  145. * The returned socket can be used to send() and recv() data.
  146. *
  147. * Parameters: TCPPort - The port to use.
  148. * pSocket - A pointer to a SOCKET, which will be filled in
  149. * if the call succeeds.
  150. *
  151. * Returns: TRUE if successful.
  152. *
  153. *
  154. * Created 16 November 1993 (andrewbe)
  155. *
  156. */
  157. BOOL SocketListen( u_short TCPPort, SOCKET *pSocket )
  158. {
  159. SOCKET Socket;
  160. SOCKADDR_IN local_sin; /* Local socket - internet style */
  161. SOCKADDR_IN acc_sin; /* Accept socket address - internet style */
  162. int acc_sin_len; /* Accept socket address length */
  163. /* Create a socket:
  164. */
  165. Socket = socket( AF_INET, SOCK_STREAM, 0);
  166. if (Socket == INVALID_SOCKET)
  167. {
  168. WSAERROR( "socket()");
  169. return FALSE;
  170. }
  171. /*
  172. ** Retrieve the IP address and TCP Port number
  173. */
  174. if (!FillAddr(NULL, &local_sin, NULL ))
  175. {
  176. return FALSE;
  177. }
  178. /*
  179. ** Associate an address with a socket. (bind)
  180. */
  181. local_sin.sin_port = htons( TCPPort );
  182. if (bind( Socket, (struct sockaddr FAR *)&local_sin, sizeof(local_sin)) == SOCKET_ERROR)
  183. {
  184. WSAERROR( "bind()" );
  185. return FALSE;
  186. }
  187. if (listen( Socket, MAX_PENDING_CONNECTS ) == SOCKET_ERROR)
  188. {
  189. WSAERROR( "listen()" );
  190. return FALSE;
  191. }
  192. acc_sin_len = sizeof(acc_sin);
  193. Socket = accept( Socket, (struct sockaddr *)&acc_sin, (int *)&acc_sin_len );
  194. if (Socket == INVALID_SOCKET)
  195. {
  196. WSAERROR( "accept()" );
  197. return FALSE;
  198. }
  199. *pSocket = Socket;
  200. return TRUE;
  201. }