be_misc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * be_misc.c: helper functions shared between main network backends.
  3. */
  4. #include <assert.h>
  5. #include <string.h>
  6. #include "putty.h"
  7. #include "network.h"
  8. void backend_socket_log(Seat *seat, LogContext *logctx,
  9. PlugLogType type, SockAddr *addr, int port,
  10. const char *error_msg, int error_code, Conf *conf,
  11. bool session_started)
  12. {
  13. char addrbuf[256], *msg;
  14. switch (type) {
  15. case PLUGLOG_CONNECT_TRYING:
  16. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  17. if (sk_addr_needs_port(addr)) {
  18. msg = dupprintf("Connecting to %s port %d", addrbuf, port);
  19. } else {
  20. msg = dupprintf("Connecting to %s", addrbuf);
  21. }
  22. break;
  23. case PLUGLOG_CONNECT_FAILED:
  24. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  25. msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
  26. break;
  27. case PLUGLOG_CONNECT_SUCCESS:
  28. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  29. msg = dupprintf("Connected to %s", addrbuf);
  30. break;
  31. case PLUGLOG_PROXY_MSG: {
  32. /* Proxy-related log messages have their own identifying
  33. * prefix already, put on by our caller. */
  34. int len, log_to_term;
  35. /* Suffix \r\n temporarily, so we can log to the terminal. */
  36. msg = dupprintf("%s\r\n", error_msg);
  37. len = strlen(msg);
  38. assert(len >= 2);
  39. log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
  40. if (log_to_term == AUTO)
  41. log_to_term = session_started ? FORCE_OFF : FORCE_ON;
  42. if (log_to_term == FORCE_ON)
  43. seat_stderr(seat, msg, len);
  44. msg[len-2] = '\0'; /* remove the \r\n again */
  45. break;
  46. }
  47. default:
  48. msg = NULL; /* shouldn't happen, but placate optimiser */
  49. break;
  50. }
  51. if (msg) {
  52. logevent(logctx, msg);
  53. sfree(msg);
  54. }
  55. }
  56. void psb_init(ProxyStderrBuf *psb)
  57. {
  58. psb->size = 0;
  59. }
  60. void log_proxy_stderr(Plug *plug, ProxyStderrBuf *psb,
  61. const void *vdata, size_t len)
  62. {
  63. const char *data = (const char *)vdata;
  64. /*
  65. * This helper function allows us to collect the data written to a
  66. * local proxy command's standard error in whatever size chunks we
  67. * happen to get from its pipe, and whenever we have a complete
  68. * line, we pass it to plug_log.
  69. *
  70. * (We also do this when the buffer in psb fills up, to avoid just
  71. * allocating more and more memory forever, and also to keep Event
  72. * Log lines reasonably bounded in size.)
  73. *
  74. * Prerequisites: a plug to log to, and a ProxyStderrBuf stored
  75. * somewhere to collect any not-yet-output partial line.
  76. */
  77. while (len > 0) {
  78. /*
  79. * Copy as much data into psb->buf as will fit.
  80. */
  81. pinitassert(psb->size < lenof(psb->buf));
  82. size_t to_consume = lenof(psb->buf) - psb->size;
  83. if (to_consume > len)
  84. to_consume = len;
  85. memcpy(psb->buf + psb->size, data, to_consume);
  86. data += to_consume;
  87. len -= to_consume;
  88. psb->size += to_consume;
  89. /*
  90. * Output any full lines in psb->buf.
  91. */
  92. { // WINSCP
  93. size_t pos = 0;
  94. while (pos < psb->size) {
  95. char *nlpos = memchr(psb->buf + pos, '\n', psb->size - pos);
  96. if (!nlpos)
  97. break;
  98. /*
  99. * Found a newline in the buffer, so we can output a line.
  100. */
  101. { // WINSCP
  102. size_t endpos = nlpos - psb->buf;
  103. while (endpos > pos && (psb->buf[endpos-1] == '\n' ||
  104. psb->buf[endpos-1] == '\r'))
  105. endpos--;
  106. { // WINSCP
  107. char *msg = dupprintf(
  108. "proxy: %.*s", (int)(endpos - pos), psb->buf + pos);
  109. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  110. sfree(msg);
  111. pos = nlpos - psb->buf + 1;
  112. assert(pos <= psb->size);
  113. } // WINSCP
  114. } // WINSCP
  115. }
  116. /*
  117. * If the buffer is completely full and we didn't output
  118. * anything, then output the whole thing, flagging it as a
  119. * truncated line.
  120. */
  121. if (pos == 0 && psb->size == lenof(psb->buf)) {
  122. char *msg = dupprintf(
  123. "proxy (partial line): %.*s", (int)psb->size, psb->buf);
  124. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  125. sfree(msg);
  126. pos = psb->size = 0;
  127. }
  128. /*
  129. * Now move any remaining data up to the front of the buffer.
  130. */
  131. { // WINSCP
  132. size_t newsize = psb->size - pos;
  133. if (newsize)
  134. memmove(psb->buf, psb->buf + pos, newsize);
  135. psb->size = newsize;
  136. /*
  137. * And loop round again if there's more data to be read from
  138. * our input.
  139. */
  140. } // WINSCP
  141. } // WINSCP
  142. }
  143. }