log_proxy_stderr.c 3.1 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. 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. assert(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. size_t pos = 0;
  47. while (pos < psb->size) {
  48. char *nlpos = memchr(psb->buf + pos, '\n', psb->size - pos);
  49. if (!nlpos)
  50. break;
  51. /*
  52. * Found a newline in the buffer, so we can output a line.
  53. */
  54. size_t endpos = nlpos - psb->buf;
  55. while (endpos > pos && (psb->buf[endpos-1] == '\n' ||
  56. psb->buf[endpos-1] == '\r'))
  57. endpos--;
  58. char *msg = dupprintf(
  59. "%s: %.*s", psb->prefix, (int)(endpos - pos), psb->buf + pos);
  60. plug_log(plug, sock, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  61. sfree(msg);
  62. pos = nlpos - psb->buf + 1;
  63. assert(pos <= psb->size);
  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. "%s (partial line): %.*s", psb->prefix, (int)psb->size,
  73. psb->buf);
  74. plug_log(plug, sock, PLUGLOG_PROXY_MSG, NULL, 0, msg, 0);
  75. sfree(msg);
  76. pos = psb->size = 0;
  77. }
  78. /*
  79. * Now move any remaining data up to the front of the buffer.
  80. */
  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. }
  90. }