log_proxy_stderr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <assert.h>
  2. #include <string.h>
  3. #include "putty.h"
  4. #include "network.h"
  5. void psb_init(ProxyStderrBuf *psb)
  6. {
  7. psb->size = 0;
  8. psb->prefix = "proxy";
  9. }
  10. void psb_set_prefix(ProxyStderrBuf *psb, const char *prefix)
  11. {
  12. psb->prefix = prefix;
  13. }
  14. void log_proxy_stderr(Plug *plug, Socket *sock, ProxyStderrBuf *psb,
  15. const void *vdata, size_t len)
  16. {
  17. const char *data = (const char *)vdata;
  18. /*
  19. * This helper function allows us to collect the data written to a
  20. * local proxy command's standard error in whatever size chunks we
  21. * happen to get from its pipe, and whenever we have a complete
  22. * line, we pass it to plug_log.
  23. *
  24. * (We also do this when the buffer in psb fills up, to avoid just
  25. * allocating more and more memory forever, and also to keep Event
  26. * Log lines reasonably bounded in size.)
  27. *
  28. * Prerequisites: a plug to log to, and a ProxyStderrBuf stored
  29. * somewhere to collect any not-yet-output partial line.
  30. */
  31. while (len > 0) {
  32. /*
  33. * Copy as much data into psb->buf as will fit.
  34. */
  35. pinitassert(psb->size < lenof(psb->buf));
  36. size_t to_consume = lenof(psb->buf) - psb->size;
  37. if (to_consume > len)
  38. to_consume = len;
  39. memcpy(psb->buf + psb->size, data, to_consume);
  40. data += to_consume;
  41. len -= to_consume;
  42. psb->size += to_consume;
  43. /*
  44. * Output any full lines in psb->buf.
  45. */
  46. { // WINSCP
  47. size_t pos = 0;
  48. while (pos < psb->size) {
  49. char *nlpos = memchr(psb->buf + pos, '\n', psb->size - pos);
  50. if (!nlpos)
  51. break;
  52. /*
  53. * Found a newline in the buffer, so we can output a line.
  54. */
  55. { // WINSCP
  56. size_t endpos = nlpos - psb->buf;
  57. while (endpos > pos && (psb->buf[endpos-1] == '\n' ||
  58. psb->buf[endpos-1] == '\r'))
  59. endpos--;
  60. { // WINSCP
  61. char *msg = dupprintf(
  62. "%s: %.*s", psb->prefix, (int)(endpos - pos), psb->buf + pos);
  63. plug_log(plug, sock, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  64. sfree(msg);
  65. pos = nlpos - psb->buf + 1;
  66. assert(pos <= psb->size);
  67. } // WINSCP
  68. } // WINSCP
  69. }
  70. /*
  71. * If the buffer is completely full and we didn't output
  72. * anything, then output the whole thing, flagging it as a
  73. * truncated line.
  74. */
  75. if (pos == 0 && psb->size == lenof(psb->buf)) {
  76. char *msg = dupprintf(
  77. "%s (partial line): %.*s", psb->prefix, (int)psb->size,
  78. psb->buf);
  79. plug_log(plug, sock, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  80. sfree(msg);
  81. pos = psb->size = 0;
  82. }
  83. /*
  84. * Now move any remaining data up to the front of the buffer.
  85. */
  86. { // WINSCP
  87. size_t newsize = psb->size - pos;
  88. if (newsize)
  89. memmove(psb->buf, psb->buf + pos, newsize);
  90. psb->size = newsize;
  91. /*
  92. * And loop round again if there's more data to be read from
  93. * our input.
  94. */
  95. } // WINSCP
  96. } // WINSCP
  97. }
  98. }