Net.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Net.h"
  5. #include "PuttyIntf.h"
  6. #include "Interface.h"
  7. #include "SecureShell.h"
  8. #include "TextsCore.h"
  9. #include "Common.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. int SessionsCount = 0;
  14. //---------------------------------------------------------------------------
  15. void __fastcall InitWinsock();
  16. static int get_line(void * frontend, const char * prompt, char * str,
  17. int maxlen, int is_pw);
  18. //---------------------------------------------------------------------------
  19. void __fastcall NetInitialize()
  20. {
  21. ssh_get_line = get_line;
  22. ssh_getline_pw_only = TRUE;
  23. InitWinsock();
  24. sk_init();
  25. AnsiString VersionString = SshVersionString();
  26. assert(!VersionString.IsEmpty() && VersionString.Length() < 50);
  27. strcpy(sshver, VersionString.c_str());
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall NetFinalize()
  31. {
  32. WSACleanup();
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall InitWinsock(void)
  36. {
  37. // see scp.c init_winsock()
  38. WORD winsock_ver;
  39. WSADATA wsadata;
  40. #pragma option push -w-prc
  41. winsock_ver = MAKEWORD(1, 1);
  42. #pragma option pop
  43. if (WSAStartup(winsock_ver, &wsadata))
  44. {
  45. SSH_FATAL_ERROR("Unable to initialise WinSock");
  46. }
  47. if (LOBYTE(wsadata.wVersion) != 1 ||
  48. HIBYTE(wsadata.wVersion) != 1)
  49. {
  50. SSH_FATAL_ERROR("WinSock version is incompatible with 1.1");
  51. }
  52. }
  53. //---------------------------------------------------------------------------
  54. extern "C" char * do_select(Plug plug, SOCKET skt, int startup)
  55. {
  56. assert(plug != NULL);
  57. void * frontend;
  58. if (!is_ssh(plug))
  59. {
  60. // If it is not SSH plug, them it must be Proxy plug.
  61. // Get SSH plug which it wraps.
  62. Proxy_Socket ProxySocket = ((Proxy_Plug)plug)->proxy_socket;
  63. plug = ProxySocket->plug;
  64. }
  65. frontend = get_ssh_frontend(plug);
  66. assert(frontend);
  67. if (startup == 0)
  68. {
  69. skt = INVALID_SOCKET;
  70. }
  71. reinterpret_cast<TSecureShell*>(frontend)->SetSocket(&skt);
  72. return NULL;
  73. }
  74. //---------------------------------------------------------------------------
  75. int from_backend(void * frontend, int is_stderr, char * data, int datalen)
  76. {
  77. assert(frontend);
  78. ((TSecureShell *)frontend)->FromBackend((is_stderr == 1), data, datalen);
  79. return 0;
  80. }
  81. //---------------------------------------------------------------------------
  82. static int get_line(void * frontend, const char * prompt, char * str,
  83. int maxlen, int is_pw)
  84. {
  85. assert(frontend != NULL);
  86. TSecureShell * SecureShell = reinterpret_cast<TSecureShell*>(frontend);
  87. AnsiString Response;
  88. bool Result = SecureShell->PromptUser(prompt, Response, is_pw);
  89. if (Result)
  90. {
  91. strcpy(str, Response.SubString(1, maxlen - 1).c_str());
  92. }
  93. return Result ? 1 : 0;
  94. }
  95. //---------------------------------------------------------------------------
  96. void SSHLogEvent(void * frontend, const char * string)
  97. {
  98. // Frontend maybe NULL here
  99. if (frontend != NULL)
  100. {
  101. ((TSecureShell *)frontend)->PuttyLogEvent(string);
  102. }
  103. }
  104. //---------------------------------------------------------------------------
  105. void SSHFatalError(char * string)
  106. {
  107. // Only few calls from putty\winnet.c might be connected with specific
  108. // TSecureShell. Otherwise called only for really fatal errors
  109. // like 'out of memory' from putty\ssh.c.
  110. SSH_FATAL_ERROR_EXT(NULL, string);
  111. }
  112. //---------------------------------------------------------------------------
  113. void SSHConnectionFatal(void * frontend, char * string)
  114. {
  115. assert(frontend);
  116. ((TSecureShell *)frontend)->FatalError(string);
  117. }
  118. //---------------------------------------------------------------------------
  119. void SSHVerifyHostKey(void * frontend, char * Host, int Port, char * KeyType,
  120. char * KeyStr, char * Fingerprint)
  121. {
  122. assert(frontend);
  123. ((TSecureShell *)frontend)->VerifyHostKey(Host, Port, KeyType, KeyStr, Fingerprint);
  124. }
  125. //---------------------------------------------------------------------------
  126. void SSHAskAlg(void * frontend, const char * AlgType, const char * AlgName)
  127. {
  128. assert(frontend);
  129. ((TSecureShell *)frontend)->AskAlg(AlgType, AlgName);
  130. }
  131. //---------------------------------------------------------------------------
  132. void SSHOldKeyfileWarning(void)
  133. {
  134. // no reference to TSecureShell instace available
  135. }
  136. //---------------------------------------------------------------------------
  137. void SSHDisplayBanner(void * frontend, const char * banner, int size)
  138. {
  139. assert(frontend);
  140. AnsiString Banner(banner, size);
  141. ((TSecureShell *)frontend)->DisplayBanner(Banner);
  142. }