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. #include "putty.h"
  7. #include "network.h"
  8. void backend_socket_log(Frontend *frontend, int type, SockAddr *addr, int port,
  9. const char *error_msg, int error_code, Conf *conf,
  10. int session_started)
  11. {
  12. char addrbuf[256], *msg;
  13. switch (type) {
  14. case 0:
  15. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  16. if (sk_addr_needs_port(addr)) {
  17. msg = dupprintf("Connecting to %s port %d", addrbuf, port);
  18. } else {
  19. msg = dupprintf("Connecting to %s", addrbuf);
  20. }
  21. break;
  22. case 1:
  23. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  24. msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
  25. break;
  26. case 2:
  27. /* Proxy-related log messages have their own identifying
  28. * prefix already, put on by our caller. */
  29. {
  30. int len, log_to_term;
  31. /* Suffix \r\n temporarily, so we can log to the terminal. */
  32. msg = dupprintf("%s\r\n", error_msg);
  33. len = strlen(msg);
  34. assert(len >= 2);
  35. log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
  36. if (log_to_term == AUTO)
  37. log_to_term = session_started ? FORCE_OFF : FORCE_ON;
  38. if (log_to_term == FORCE_ON)
  39. from_backend(frontend, TRUE, msg, len);
  40. msg[len-2] = '\0'; /* remove the \r\n again */
  41. }
  42. break;
  43. default:
  44. msg = NULL; /* shouldn't happen, but placate optimiser */
  45. break;
  46. }
  47. if (msg) {
  48. logevent(frontend, msg);
  49. sfree(msg);
  50. }
  51. }
  52. void log_proxy_stderr(Plug *plug, bufchain *buf, const void *vdata, int len)
  53. {
  54. const char *data = (const char *)vdata;
  55. int pos = 0;
  56. int msglen;
  57. const char *nlpos;
  58. char *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. }