be_misc.c 4.9 KB

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