be_misc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * be_misc.c: helper functions shared between main network backends.
  3. */
  4. #include <assert.h>
  5. #include <string.h>
  6. #define DEFINE_PLUG_METHOD_MACROS
  7. #include "putty.h"
  8. #include "network.h"
  9. void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
  10. const char *error_msg, int error_code, Conf *conf,
  11. int 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. from_backend(frontend, TRUE, 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(frontend, msg);
  50. sfree(msg);
  51. }
  52. }
  53. void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len)
  54. {
  55. const char *data = (const char *)vdata;
  56. int pos = 0;
  57. int msglen;
  58. char *nlpos, *msg, *fullmsg;
  59. /*
  60. * This helper function allows us to collect the data written to a
  61. * local proxy command's standard error in whatever size chunks we
  62. * happen to get from its pipe, and whenever we have a complete
  63. * line, we pass it to plug_log.
  64. *
  65. * Prerequisites: a plug to log to, and a bufchain stored
  66. * somewhere to collect the data in.
  67. */
  68. while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
  69. /*
  70. * Found a newline in the current input buffer. Append it to
  71. * the bufchain (which may contain a partial line from last
  72. * time).
  73. */
  74. bufchain_add(buf, data + pos, nlpos - (data + pos));
  75. /*
  76. * Collect the resulting line of data and pass it to plug_log.
  77. */
  78. msglen = bufchain_size(buf);
  79. msg = snewn(msglen+1, char);
  80. bufchain_fetch(buf, msg, msglen);
  81. bufchain_consume(buf, msglen);
  82. msg[msglen] = '\0';
  83. fullmsg = dupprintf("proxy: %s", msg);
  84. plug_log(plug, 2, NULL, 0, fullmsg, 0);
  85. sfree(fullmsg);
  86. sfree(msg);
  87. /*
  88. * Advance past the newline.
  89. */
  90. pos += nlpos+1 - (data + pos);
  91. }
  92. /*
  93. * Now any remaining data is a partial line, which we save for
  94. * next time.
  95. */
  96. bufchain_add(buf, data + pos, len - pos);
  97. }