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.

750 lines
21 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. RnrSrv.c
  5. Abstract:
  6. Test and demonstration service for the RNR (service Registration and
  7. Name Resolution) APIs. This is a simple service designed to show
  8. the basic principles involved in using the RNR APIs to _write a
  9. protocol-independent Windows Sockets service.
  10. This service opens a number of listening sockets, waits for an
  11. incoming connection from a client, accepts the connection, then
  12. echos data back to the client until the client terminates the
  13. virtual circuit. This service is single-threaded and can handle
  14. only a single client at a time.
  15. The OpenListeners() routine implemented herein is intended to be a
  16. demonstration of RNR functionality commonly used in
  17. protocol-independent services. Service writers are encouraged to
  18. leverage this code to assist them in writing protocol-independent
  19. services on top of the Windows Sockets API.
  20. --*/
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <winsock2.h>
  24. #include <nspapi.h>
  25. WSADATA WsaData;
  26. PSTR ServiceTypeName = "EchoExample";
  27. PSTR ServiceName = "EchoServer";
  28. #define MAX_SOCKETS 20
  29. INT
  30. OpenListeners (
  31. IN PTSTR ServiceName,
  32. IN LPGUID ServiceType,
  33. IN BOOL Reliable,
  34. IN BOOL MessageOriented,
  35. IN BOOL StreamOriented,
  36. IN BOOL Connectionless,
  37. OUT SOCKET SocketHandles[],
  38. OUT INT ProtocolsUsed[]
  39. );
  40. INT
  41. AdvertiseService(
  42. IN PTSTR ServiceName,
  43. IN LPGUID ServiceType,
  44. IN SOCKET SocketHandles[],
  45. IN INT SocketCount
  46. );
  47. void __cdecl
  48. main (
  49. int argc,
  50. char *argv[]
  51. )
  52. {
  53. INT count, err, i ;
  54. DWORD tmpProtocol[2];
  55. BYTE buffer[1024];
  56. DWORD bytesRequired;
  57. PPROTOCOL_INFO protocolInfo;
  58. GUID serviceType;
  59. FD_SET readfds;
  60. SOCKET listenSockets[MAX_SOCKETS+1];
  61. INT protocols[MAX_SOCKETS+1];
  62. SOCKET s;
  63. //
  64. // Initialize the Windows Sockets DLL.
  65. //
  66. err = WSAStartup( 0x0202, &WsaData );
  67. if ( err == SOCKET_ERROR )
  68. {
  69. printf( "WSAStartup() failed: %ld\n", GetLastError( ) );
  70. return;
  71. }
  72. //
  73. // Determine the value of our GUID. The GUID uniquely identifies
  74. // the type of service we provide.
  75. //
  76. err = GetTypeByName( ServiceTypeName, &serviceType );
  77. if ( err == SOCKET_ERROR )
  78. {
  79. printf( "GetTypeByName for \"%s\" failed: %ld\n",
  80. ServiceTypeName, GetLastError( ) );
  81. exit( 1 );
  82. }
  83. //
  84. // Open listening sockets for this service.
  85. //
  86. count = OpenListeners(
  87. ServiceName,
  88. &serviceType,
  89. TRUE,
  90. FALSE,
  91. FALSE,
  92. FALSE,
  93. listenSockets,
  94. protocols
  95. );
  96. if ( count <= 0 )
  97. {
  98. printf( "failed to open listenSockets for name \"%s\" type \"%s\"\n",
  99. ServiceName, ServiceTypeName );
  100. exit( 1 );
  101. }
  102. //
  103. // We successfully opened some listening sockets. Display some
  104. // information on each protocol in use.
  105. //
  106. tmpProtocol[1] = 0;
  107. for ( i = 0; i < count; i++ )
  108. {
  109. tmpProtocol[0] = protocols[i];
  110. bytesRequired = sizeof(buffer);
  111. err = EnumProtocols( tmpProtocol, buffer, &bytesRequired );
  112. if ( err < 1 )
  113. {
  114. printf( "EnumProtocols failed for protocol %ld: %ld\n",
  115. tmpProtocol[0], GetLastError( ) );
  116. exit( 1 );
  117. }
  118. protocolInfo = (PPROTOCOL_INFO)buffer;
  119. printf( "Socket %lx listening on protocol \"%s\" (%ld)\n",
  120. listenSockets[i],
  121. protocolInfo->lpProtocol,
  122. protocolInfo->iProtocol );
  123. }
  124. //
  125. // Advertise the service so thet it can be found.
  126. //
  127. printf( "Going to advertise the service.\n" ) ;
  128. err = AdvertiseService(
  129. ServiceName,
  130. &serviceType,
  131. listenSockets,
  132. count) ;
  133. if (err == SOCKET_ERROR)
  134. {
  135. printf( "Failed to advertise the service. Error %d\n", GetLastError()) ;
  136. exit( 1 ) ;
  137. }
  138. printf( "Successfully advertised the service.\n" ) ;
  139. //
  140. // Loop accepting connections and servicing them.
  141. //
  142. FD_ZERO( &readfds );
  143. while ( TRUE )
  144. {
  145. //
  146. // Add the listening sockets to the FD_SET we'll pass to select.
  147. //
  148. for ( i = 0; i < count; i++ )
  149. {
  150. FD_SET( listenSockets[i], &readfds );
  151. }
  152. //
  153. // Wait for one of the listenSockets to receive an incoming connection.
  154. //
  155. err = select( count, &readfds, NULL, NULL, NULL );
  156. if ( err < 1 )
  157. {
  158. printf( "select() returned %ld, error %ld\n", err, GetLastError( ) );
  159. exit( 1 );
  160. }
  161. //
  162. // Find the socket that received an incoming connection and accept
  163. // the connection.
  164. //
  165. for ( i = 0; i < count; i++ )
  166. {
  167. if ( FD_ISSET( listenSockets[i], &readfds ) )
  168. break;
  169. }
  170. //
  171. // Accept the connection from the client. We ignore the client's
  172. // address here.
  173. //
  174. s = accept( listenSockets[i], NULL, NULL );
  175. if ( s == INVALID_SOCKET )
  176. {
  177. printf( "accept() failed, error %ld\n", GetLastError( ) );
  178. exit( 1 );
  179. }
  180. printf( "Accepted incoming connection on socket %lx\n",
  181. listenSockets[i] );
  182. //
  183. // Loop echoing data back to the client. Note that this
  184. // single-threaded service can handle only a single client at a
  185. // time. A more sophisticated service would service multiple
  186. // clients simultaneously by using multiple threads or
  187. // asynchronous I/O.
  188. //
  189. while ( TRUE )
  190. {
  191. err = recv( s, buffer, sizeof(buffer), 0 );
  192. if ( err == 0 )
  193. {
  194. printf( "Connection terminated gracefully.\n" );
  195. break;
  196. }
  197. else if ( err < 0 )
  198. {
  199. err = GetLastError();
  200. if ( err == WSAEDISCON )
  201. {
  202. printf( "Connection disconnected.\n" );
  203. }
  204. else
  205. {
  206. printf( "recv() failed, error %ld.\n", err );
  207. }
  208. break;
  209. }
  210. err = send( s, buffer, err, 0 );
  211. if ( err < 0 )
  212. {
  213. printf( "send() failed, error %ld\n", GetLastError( ) );
  214. break;
  215. }
  216. }
  217. //
  218. // Close the connected socket and continue accepting connections.
  219. //
  220. closesocket( s );
  221. }
  222. } // main
  223. INT
  224. OpenListeners (
  225. IN PTSTR ServiceName,
  226. IN LPGUID ServiceType,
  227. IN BOOL Reliable,
  228. IN BOOL MessageOriented,
  229. IN BOOL StreamOriented,
  230. IN BOOL Connectionless,
  231. OUT SOCKET SocketHandles[],
  232. OUT INT ProtocolsUsed[]
  233. )
  234. /*++
  235. Routine Description:
  236. Examines the Windows Sockets transport protocols loaded on a machine
  237. and opens listening sockets on all the protocols which support the
  238. characteristics requested by the caller.
  239. Arguments:
  240. ServiceName - a friendly name which identifies this service. On
  241. name spaces which support name resolution at the service level
  242. (e.g. SAP) this is the name clients will use to connect to this
  243. service. On name spaces which support name resolution at the
  244. host level (e.g. DNS) this name is ignored and applications
  245. must use the host name to establish communication with this
  246. service.
  247. ServiceType - the GUID value which uniquely identifies the type of
  248. service we provide. A GUID is created with the UUIDGEN program.
  249. Reliable - if TRUE, the caller requests that only transport protocols
  250. which support reliable data delivery be used. If FALSE, both
  251. reliable and unreliable protocols may be used.
  252. MessageOriented - if TRUE, only message-oriented transport protocols
  253. should be used. If FALSE, the caller either does not care
  254. whether the protocols used are message oriented or desires only
  255. stream-oriented protocols.
  256. StreamOriented - if TRUE, only stream-oriented transport protocols
  257. should be used. If FALSE, the caller either does not care
  258. whether the protocols used are stream oriented or desires only
  259. message-oriented protocols.
  260. Connectionless - if TRUE, only connectionless protocols should be
  261. used. If FALSE, both connection-oriented and connectionless
  262. protocols may be used.
  263. SocketHandles - an array of size MAX_SOCKETS which receives listening
  264. socket handles.
  265. ProtocolsUsed - an array of size MAX_SOCKETS which receives the
  266. protocol values for each of the socket handles in the
  267. SocketHandles array.
  268. Return Value:
  269. The count of listening sockets successfully opened, or -1 if no
  270. sockets could be successfully opened that met the desired
  271. characteristics.
  272. --*/
  273. {
  274. INT protocols[MAX_SOCKETS+1];
  275. BYTE buffer[2048];
  276. DWORD bytesRequired;
  277. INT err;
  278. PPROTOCOL_INFO protocolInfo;
  279. PCSADDR_INFO csaddrInfo;
  280. INT protocolCount;
  281. INT addressCount;
  282. INT i;
  283. DWORD protocolIndex;
  284. SOCKET s;
  285. DWORD index = 0;
  286. //
  287. // First look up the protocols installed on this machine. The
  288. // EnumProtocols() API returns about all the Windows Sockets
  289. // protocols loaded on this machine, and we'll use this information
  290. // to identify the protocols which provide the necessary semantics.
  291. //
  292. bytesRequired = sizeof(buffer);
  293. err = EnumProtocols( NULL, buffer, &bytesRequired );
  294. if ( err <= 0 )
  295. {
  296. return 0;
  297. }
  298. //
  299. // Walk through the available protocols and pick out the ones which
  300. // support the desired characteristics.
  301. //
  302. protocolCount = err;
  303. protocolInfo = (PPROTOCOL_INFO)buffer;
  304. for ( i = 0, protocolIndex = 0;
  305. i < protocolCount && protocolIndex < MAX_SOCKETS;
  306. i++, protocolInfo++ )
  307. {
  308. //
  309. // If "reliable" support is requested, then check if supported
  310. // by this protocol. Reliable support means that the protocol
  311. // guarantees delivery of data in the order in which it is sent.
  312. // Note that we assume here that if the caller requested reliable
  313. // service then they do not want a connectionless protocol.
  314. //
  315. if ( Reliable )
  316. {
  317. //
  318. // Check to see if the protocol is reliable. It must
  319. // guarantee both delivery of all data and the order in
  320. // which the data arrives. Also, it must not be a
  321. // connectionless protocol.
  322. //
  323. if ( (protocolInfo->dwServiceFlags &
  324. XP_GUARANTEED_DELIVERY) == 0 ||
  325. (protocolInfo->dwServiceFlags &
  326. XP_GUARANTEED_ORDER) == 0 )
  327. {
  328. continue;
  329. }
  330. if ( (protocolInfo->dwServiceFlags & XP_CONNECTIONLESS) != 0 )
  331. {
  332. continue;
  333. }
  334. //
  335. // Check to see that the protocol matches the stream/message
  336. // characteristics requested. A stream oriented protocol
  337. // either has the XP_MESSAGE_ORIENTED bit turned off, or
  338. // else supports "pseudo stream" capability. Pseudo stream
  339. // means that although the underlying protocol is message
  340. // oriented, the application may open a socket of type
  341. // SOCK_STREAM and the protocol will hide message boundaries
  342. // from the application.
  343. //
  344. if ( StreamOriented &&
  345. (protocolInfo->dwServiceFlags & XP_MESSAGE_ORIENTED) != 0 &&
  346. (protocolInfo->dwServiceFlags & XP_PSEUDO_STREAM) == 0 )
  347. {
  348. continue;
  349. }
  350. if ( MessageOriented &&
  351. (protocolInfo->dwServiceFlags & XP_MESSAGE_ORIENTED) == 0 )
  352. {
  353. continue;
  354. }
  355. }
  356. else if ( Connectionless )
  357. {
  358. //
  359. // Make sure that this is a connectionless protocol. In a
  360. // connectionless protocol, data is sent as discrete
  361. // datagrams with no connection establishment required.
  362. // Connectionless protocols typically have no reliability
  363. // guarantees.
  364. //
  365. if ( (protocolInfo->dwServiceFlags & XP_CONNECTIONLESS) != 0 )
  366. {
  367. continue;
  368. }
  369. }
  370. //
  371. // This protocol fits all the criteria. Add it to the list of
  372. // protocols in which we're interested.
  373. //
  374. protocols[protocolIndex++] = protocolInfo->iProtocol;
  375. }
  376. //
  377. // Make sure that we found at least one acceptable protocol. If
  378. // there no protocols on this machine which meet the caller's
  379. // requirements then fail here.
  380. //
  381. if ( protocolIndex == 0 )
  382. {
  383. return 0;
  384. }
  385. protocols[protocolIndex] = 0;
  386. //
  387. // Now attempt to find the socket addresses to which we need to
  388. // bind. Note that we restrict the scope of the search to those
  389. // protocols of interest by passing the protocol array we generated
  390. // above to GetAddressByName(). This forces GetAddressByName() to
  391. // return socket addresses for only the protocols we specify,
  392. // ignoring possible addresses for protocols we cannot support
  393. // because of the caller's constraints.
  394. //
  395. bytesRequired = sizeof(buffer);
  396. err = GetAddressByName(
  397. NS_DEFAULT,
  398. ServiceType,
  399. ServiceName,
  400. protocols,
  401. RES_SERVICE | RES_FIND_MULTIPLE,
  402. NULL, // lpServiceAsyncInfo
  403. buffer,
  404. &bytesRequired,
  405. NULL, // lpAliasBuffer
  406. NULL // lpdwAliasBufferLength
  407. );
  408. if ( err <= 0 )
  409. {
  410. return 0;
  411. }
  412. //
  413. // For each address, open a socket and attempt to listen. Note
  414. // that if anything fails for a particular protocol we just skip on
  415. // to the next protocol. As long as we can successfully listen on
  416. // one protocol we are satisfied here.
  417. //
  418. addressCount = err;
  419. csaddrInfo = (PCSADDR_INFO)buffer;
  420. for ( i = 0; i < addressCount; i++, csaddrInfo++ )
  421. {
  422. //
  423. // Open the socket. Note that we manually specify stream type
  424. // if so requested in case the protocol is natively a message
  425. // protocol but supports stream semantics.
  426. //
  427. s = socket( csaddrInfo->LocalAddr.lpSockaddr->sa_family,
  428. StreamOriented ? SOCK_STREAM : csaddrInfo->iSocketType,
  429. csaddrInfo->iProtocol );
  430. if ( s == INVALID_SOCKET )
  431. {
  432. continue;
  433. }
  434. //
  435. // Bind the socket to the local address specified.
  436. //
  437. err = bind( s, csaddrInfo->LocalAddr.lpSockaddr,
  438. csaddrInfo->LocalAddr.iSockaddrLength );
  439. if ( err != NO_ERROR )
  440. {
  441. closesocket( s );
  442. continue;
  443. }
  444. //
  445. // Start listening for incoming sockets on the socket if this is
  446. // not a datagram socket. If this is a datagram socket, then
  447. // the listen() API doesn't make sense; doing a bind() is
  448. // sufficient to listen for incoming datagrams on a
  449. // connectionless protocol.
  450. //
  451. if ( csaddrInfo->iSocketType != SOCK_DGRAM )
  452. {
  453. err = listen( s, 5 );
  454. if ( err != NO_ERROR )
  455. {
  456. closesocket( s );
  457. continue;
  458. }
  459. }
  460. //
  461. // The socket was successfully opened and we're listening on it.
  462. // Remember the protocol used and the socket handle and continue
  463. // listening on other protocols.
  464. //
  465. ProtocolsUsed[index] = csaddrInfo->iProtocol;
  466. SocketHandles[index] = s;
  467. index++;
  468. if ( index == MAX_SOCKETS )
  469. {
  470. return index;
  471. }
  472. }
  473. (void) LocalFree( (HLOCAL) csaddrInfo );
  474. //
  475. // Return the count of sockets that we're sucecssfully listening on.
  476. //
  477. return index;
  478. } // OpenListeners
  479. INT
  480. AdvertiseService(
  481. IN PTSTR ServiceName,
  482. IN LPGUID ServiceType,
  483. IN SOCKET SocketHandles[],
  484. IN INT SocketCount
  485. )
  486. /*++
  487. Routine Description:
  488. Advertises this service on all the default name spaces.
  489. Arguments:
  490. ServiceName - the name of the service.
  491. ServiceType - the GUID value which uniquely the service.
  492. SocketHandles - array of sockets that we have opened. For each socket,
  493. we do a getsockname() to discover the actual local address.
  494. SocketCount - number of sockets in SockHandles[]
  495. Return Value:
  496. 0 if success. SOCK_ERROR otherwise.
  497. --*/
  498. {
  499. WSAVERSION Version;
  500. WSAQUERYSET QuerySet;
  501. LPCSADDR_INFO lpCSAddrInfo;
  502. PSOCKADDR sockAddr ;
  503. BYTE * addressBuffer;
  504. DWORD addressBufferSize ;
  505. DWORD successCount = 0 ;
  506. INT i, err ;
  507. //
  508. // Allocate some memory for the CSADDR_INFO structures.
  509. //
  510. lpCSAddrInfo = (LPCSADDR_INFO) malloc( sizeof(CSADDR_INFO) * SocketCount );
  511. if (!lpCSAddrInfo)
  512. {
  513. SetLastError(ERROR_NOT_ENOUGH_MEMORY) ;
  514. return SOCKET_ERROR ;
  515. }
  516. //
  517. // Allocate some memory for the SOCKADDR addresses returned
  518. // by getsockname().
  519. //
  520. addressBufferSize = SocketCount * sizeof(SOCKADDR);
  521. addressBuffer = malloc( addressBufferSize ) ;
  522. if (!addressBuffer)
  523. {
  524. free(lpCSAddrInfo) ;
  525. SetLastError(ERROR_NOT_ENOUGH_MEMORY) ;
  526. return SOCKET_ERROR ;
  527. }
  528. RtlZeroMemory( &QuerySet, sizeof( WSAQUERYSET ) );
  529. //
  530. // For each socket, get its local association.
  531. //
  532. sockAddr = (PSOCKADDR) addressBuffer ;
  533. for (i = 0; i < SocketCount; i++)
  534. {
  535. int size = (int) addressBufferSize ;
  536. //
  537. // Call getsockname() to get the local association for the socket.
  538. //
  539. err = getsockname(
  540. SocketHandles[i],
  541. sockAddr,
  542. &size) ;
  543. if (err == SOCKET_ERROR)
  544. {
  545. continue ;
  546. }
  547. //
  548. // Now setup the Addressing information for this socket.
  549. // Only the dwAddressType, dwAddressLength and lpAddress
  550. // is of any interest in this example.
  551. //
  552. lpCSAddrInfo[i].LocalAddr.iSockaddrLength = size;
  553. lpCSAddrInfo[i].LocalAddr.lpSockaddr = sockAddr;
  554. lpCSAddrInfo[i].RemoteAddr.iSockaddrLength = size;
  555. lpCSAddrInfo[i].RemoteAddr.lpSockaddr = sockAddr;
  556. lpCSAddrInfo[i].iSocketType = SOCK_RDM; // Reliable
  557. lpCSAddrInfo[i].iProtocol = sockAddr->sa_family;
  558. //
  559. // Advance pointer and adjust buffer size. Assumes that
  560. // the structures are aligned.
  561. //
  562. addressBufferSize -= size ;
  563. sockAddr = (PSOCKADDR) ((BYTE*)sockAddr + size) ;
  564. successCount++ ;
  565. }
  566. //
  567. // If we got at least one address, go ahead and advertise it.
  568. //
  569. if (successCount)
  570. {
  571. QuerySet.dwSize = sizeof( WSAQUERYSET );
  572. QuerySet.lpServiceClassId = ServiceType;
  573. QuerySet.lpszServiceInstanceName = ServiceName;
  574. QuerySet.lpszComment = "D/C/M's Example Echo Service";
  575. QuerySet.lpVersion = &Version;
  576. QuerySet.lpVersion->dwVersion = 1;
  577. QuerySet.lpVersion->ecHow = COMP_NOTLESS;
  578. QuerySet.dwNameSpace = NS_ALL;
  579. QuerySet.dwNumberOfCsAddrs = successCount;
  580. QuerySet.lpcsaBuffer = lpCSAddrInfo;
  581. err = WSASetService( &QuerySet,
  582. RNRSERVICE_REGISTER,
  583. SERVICE_MULTIPLE );
  584. if ( err )
  585. err = SOCKET_ERROR;
  586. }
  587. else
  588. err = SOCKET_ERROR ;
  589. free (addressBuffer) ;
  590. return (err) ;
  591. }