Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. // File DoRPC_Client.cpp
  2. #include <iostream>
  3. #include <DoRPC.h>
  4. #include <stdexcept>
  5. #include <string_view>
  6. #include <fmt/format.h>
  7.  
  8. #pragma comment(lib, "Rpcrt4.lib")
  9.  
  10. constexpr unsigned char * ToRpcStr(char *str)
  11. {
  12. return reinterpret_cast<unsigned char*>(str);
  13. }
  14.  
  15. int main(int argc, char **argv)
  16. {
  17. using namespace std;
  18. using fmt::format;
  19.  
  20. if (argc != 4)
  21. {
  22. cout << format("usage: {} serv-ip username passord", argv[0]) << endl;
  23. return 1;
  24. }
  25.  
  26. try {
  27. auto login = argv[2];
  28. auto password = argv[3];
  29. auto ip_addr = argv[1];
  30.  
  31. // create string binding handle
  32. unsigned char* szStringBinding = nullptr;
  33. auto status = RpcStringBindingCompose(
  34. NULL,
  35. ToRpcStr("ncacn_ip_tcp"),
  36. ToRpcStr(ip_addr),
  37. ToRpcStr("9191"),
  38. NULL,
  39. &szStringBinding);
  40. if (status)
  41. {
  42. throw runtime_error(format("creating string binding handle {}", status));
  43. }
  44.  
  45. // create binding handle
  46. status = RpcBindingFromStringBinding(
  47. szStringBinding,
  48. &hDoRPCBinding);
  49. if (status)
  50. {
  51. throw runtime_error(format("creating binding handle {}", status));
  52. }
  53. RpcStringFree(&szStringBinding);
  54.  
  55. // Setting auth info
  56. SEC_WINNT_AUTH_IDENTITY auth_info{};
  57. auth_info.User = ToRpcStr(login);
  58. auth_info.UserLength = strlen(login);
  59. auth_info.Password = ToRpcStr(password);
  60. auth_info.PasswordLength = strlen(password);
  61. auth_info.Domain = nullptr;
  62. auth_info.DomainLength = 0;
  63. auth_info.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
  64.  
  65. status = RpcBindingSetAuthInfo(
  66. hDoRPCBinding,
  67. nullptr,
  68. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  69. RPC_C_AUTHN_GSS_KERBEROS,
  70. 0,
  71. 0);
  72. if (status)
  73. {
  74. throw runtime_error(format("Binding auth info {}", status));
  75. }
  76.  
  77. auto call_it = []() {
  78. RpcTryExcept
  79. {
  80. const unsigned char szMsg[] = "Client: I Can RPC Now!";
  81. Show(szMsg);
  82. }
  83. RpcExcept(1)
  84. {
  85. std::cout << "Runtime exception occurred: " << RpcExceptionCode() << std::endl;
  86. }
  87. RpcEndExcept
  88. };
  89. call_it();
  90.  
  91. status = RpcBindingFree(&hDoRPCBinding);
  92. if (status)
  93. {
  94. throw runtime_error(format("freeing binding handle {}", status));
  95. }
  96. }
  97. catch (exception &e)
  98. {
  99. cout << "Error: " << e.what() << endl;
  100. }
  101.  
  102. return 0;
  103. }
  104.  
  105. // Memory allocation function for RPC.
  106. // The runtime uses these two functions for allocating/deallocating
  107. // enough memory to pass the string to the server.
  108. void* __RPC_USER midl_user_allocate(size_t size)
  109. {
  110. return reinterpret_cast<void*>(new char[size]());
  111. }
  112.  
  113. // Memory deallocation function for RPC.
  114. void __RPC_USER midl_user_free(void* p)
  115. {
  116. delete[] p;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement