1
0

log_proxy_stderr.c 3.2 KB

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