be_misc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. assert(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. size_t pos = 0;
  93. while (pos < psb->size) {
  94. char *nlpos = memchr(psb->buf + pos, '\n', psb->size - pos);
  95. if (!nlpos)
  96. break;
  97. /*
  98. * Found a newline in the buffer, so we can output a line.
  99. */
  100. size_t endpos = nlpos - psb->buf;
  101. while (endpos > pos && (psb->buf[endpos-1] == '\n' ||
  102. psb->buf[endpos-1] == '\r'))
  103. endpos--;
  104. char *msg = dupprintf(
  105. "proxy: %.*s", (int)(endpos - pos), psb->buf + pos);
  106. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  107. sfree(msg);
  108. pos = nlpos - psb->buf + 1;
  109. assert(pos <= psb->size);
  110. }
  111. /*
  112. * If the buffer is completely full and we didn't output
  113. * anything, then output the whole thing, flagging it as a
  114. * truncated line.
  115. */
  116. if (pos == 0 && psb->size == lenof(psb->buf)) {
  117. char *msg = dupprintf(
  118. "proxy (partial line): %.*s", (int)psb->size, psb->buf);
  119. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  120. sfree(msg);
  121. pos = psb->size = 0;
  122. }
  123. /*
  124. * Now move any remaining data up to the front of the buffer.
  125. */
  126. size_t newsize = psb->size - pos;
  127. if (newsize)
  128. memmove(psb->buf, psb->buf + pos, newsize);
  129. psb->size = newsize;
  130. /*
  131. * And loop round again if there's more data to be read from
  132. * our input.
  133. */
  134. }
  135. }