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.

210 lines
6.3 KiB

  1. /****************************************************************************
  2. Microsoft RPC Version 1.0
  3. Copyright Microsoft Corp. 1992
  4. Hello Example
  5. FILE: hellos.c
  6. USAGE: hellos
  7. PURPOSE: Server side of RPC distributed application hello
  8. FUNCTIONS: main() - registers server as RPC server
  9. COMMENTS:
  10. This distributed application prints "hello, world" on the server.
  11. This version features a client that manages its connection to
  12. the server. It uses the binding handle hello_IfHandle that is defined
  13. in the generated header file hello.h.
  14. ****************************************************************************/
  15. #include <stdlib.h>
  16. #include <windows.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <rpc.h> // RPC data structures and APIs
  21. #include "hello2.h" // header file generated by MIDL compiler
  22. void Usage(char * pszProgramName)
  23. {
  24. fprintf(stderr, "Usage: %s\n", pszProgramName);
  25. fprintf(stderr, " -p protocol_sequence\n");
  26. fprintf(stderr, " -n network_address\n");
  27. fprintf(stderr, " -e endpoint\n");
  28. fprintf(stderr, " -o options\n");
  29. fprintf(stderr, " -u uuid\n");
  30. exit(1);
  31. }
  32. HANDLE TerminateEvent;
  33. int __cdecl
  34. main (argc, argv)
  35. int argc;
  36. char *argv[];
  37. {
  38. RPC_STATUS status;
  39. unsigned char * pszUuid = "12345678-1234-1234-1234-123456789AB3";
  40. unsigned char * pszProtocolSequence = "ncacn_ip_tcp";
  41. unsigned char * pszNetworkAddress = NULL;
  42. unsigned char * pszEndpoint = "760";
  43. unsigned char * pszOptions = NULL;
  44. unsigned char * pszStringBinding = NULL;
  45. int i;
  46. DWORD WaitStatus;
  47. // allow the user to override settings with command line switches
  48. for (i = 1; i < argc; i++) {
  49. if ((*argv[i] == '-') || (*argv[i] == '/')) {
  50. switch (tolower(*(argv[i]+1))) {
  51. case 'p': // protocol sequence
  52. pszProtocolSequence = argv[++i];
  53. break;
  54. case 'n': // network address
  55. pszNetworkAddress = argv[++i];
  56. break;
  57. case 'e':
  58. pszEndpoint = argv[++i];
  59. break;
  60. case 'o':
  61. pszOptions = argv[++i];
  62. break;
  63. case 'u':
  64. pszUuid = argv[++i];
  65. break;
  66. case 'h':
  67. case '?':
  68. default:
  69. Usage(argv[0]);
  70. }
  71. }
  72. else
  73. Usage(argv[0]);
  74. }
  75. //
  76. // Create an event to wait on
  77. //
  78. TerminateEvent = CreateEvent( NULL, // No security attributes
  79. TRUE, // Must be manually reset
  80. FALSE, // Initially not signaled
  81. NULL ); // No name
  82. if ( TerminateEvent == NULL ) {
  83. printf( "Couldn't CreateEvent %ld\n", GetLastError() );
  84. return 2;
  85. }
  86. status = RpcServerUseProtseqEp(pszProtocolSequence,
  87. 1, // maximum concurrent calls
  88. pszEndpoint,
  89. 0);
  90. if (status) {
  91. printf("RpcServerUseProtseqEp returned 0x%x\n", status);
  92. exit(2);
  93. }
  94. status = RpcServerRegisterIf(hello2_v1_0_s_ifspec, 0, 0);
  95. if (status) {
  96. printf("RpcServerRegisterIf returned 0x%x\n", status);
  97. exit(2);
  98. }
  99. status = RpcServerRegisterAuthInfo( "HelloS", RPC_C_AUTHN_DCE_PRIVATE, NULL, NULL );
  100. if (status) {
  101. printf("RpcServerRegisterAuthInfo returned 0x%x\n", status);
  102. exit(2);
  103. }
  104. printf("Calling RpcServerListen\n");
  105. status = RpcServerListen(1,12345,1);
  106. if (status) {
  107. printf("RpcServerListen returned: 0x%x\n", status);
  108. exit(2);
  109. }
  110. WaitStatus = WaitForSingleObject( TerminateEvent, INFINITE );
  111. if ( WaitStatus != WAIT_OBJECT_0 ) {
  112. printf( "Couldn't WaitForSingleObject %ld %ld\n", WaitStatus, GetLastError() );
  113. return 2;
  114. }
  115. return 0;
  116. } /* end main() */
  117. // ====================================================================
  118. // MIDL allocate and free
  119. // ====================================================================
  120. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  121. {
  122. return(malloc(len));
  123. }
  124. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  125. {
  126. free(ptr);
  127. }
  128. /*
  129. PURPOSE: Remote procedures that are linked with the server
  130. side of RPC distributed application
  131. FUNCTIONS: HelloProc() - prints "hello, world" or other string
  132. sent by client to server
  133. COMMENTS:
  134. This version of the distributed application that prints
  135. "hello, world" (or other string) on the server features a client
  136. that manages its connection to the server. It uses the binding
  137. handle hello_IfHandle, defined in the file hello.h.
  138. ****************************************************************************/
  139. void HelloProc2(unsigned char * pszString)
  140. {
  141. RPC_STATUS RpcStatus;
  142. CHAR UserName[100];
  143. ULONG NameLen = 100;
  144. RpcStatus = RpcImpersonateClient( NULL );
  145. if ( RpcStatus != RPC_S_OK ) {
  146. printf( "RpcImpersonateClient Failed %ld\n", RpcStatus );
  147. }
  148. GetUserName(UserName,&NameLen);
  149. printf("%s: %s\n",UserName, pszString);
  150. RpcStatus = RpcRevertToSelf();
  151. if ( RpcStatus != RPC_S_OK ) {
  152. printf( "RpcRevertToSelf Failed %ld\n", RpcStatus );
  153. }
  154. }
  155. void Shutdown2(void)
  156. {
  157. RPC_STATUS status;
  158. printf("Calling RpcMgmtStopServerListening\n");
  159. status = RpcMgmtStopServerListening(NULL);
  160. if (status) {
  161. printf("RpcMgmtStopServerListening returned: 0x%x\n", status);
  162. exit(2);
  163. }
  164. printf("Calling RpcServerUnregisterIf\n");
  165. status = RpcServerUnregisterIf(NULL, NULL, FALSE);
  166. if (status) {
  167. printf("RpcServerUnregisterIf returned 0x%x\n", status);
  168. exit(2);
  169. }
  170. if ( !SetEvent( TerminateEvent) ) {
  171. printf( "Couldn't SetEvent %ld\n", GetLastError() );
  172. }
  173. }
  174. /* end hellos.c */