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.

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