backend_socket_log.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <assert.h>
  2. #include <string.h>
  3. #include "putty.h"
  4. #include "network.h"
  5. void backend_socket_log(Seat *seat, LogContext *logctx, Socket *sock,
  6. PlugLogType type, SockAddr *addr, int port,
  7. const char *error_msg, int error_code, Conf *conf,
  8. bool session_started)
  9. {
  10. char addrbuf[256], *msg;
  11. switch (type) {
  12. case PLUGLOG_CONNECT_TRYING:
  13. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  14. if (sk_addr_needs_port(addr)) {
  15. msg = dupprintf("Connecting to %s port %d", addrbuf, port);
  16. } else {
  17. msg = dupprintf("Connecting to %s", addrbuf);
  18. }
  19. break;
  20. case PLUGLOG_CONNECT_FAILED:
  21. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  22. msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
  23. break;
  24. case PLUGLOG_CONNECT_SUCCESS:
  25. if (addr)
  26. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  27. else /* fallback if address unavailable */
  28. sprintf(addrbuf, "remote host");
  29. msg = dupprintf("Connected to %s", addrbuf);
  30. if (sock) {
  31. SocketEndpointInfo *local_end = sk_endpoint_info(sock, false);
  32. if (local_end) {
  33. char *newmsg = dupprintf("%s (from %s)", msg,
  34. local_end->log_text);
  35. sfree(msg);
  36. msg = newmsg;
  37. sk_free_endpoint_info(local_end);
  38. }
  39. }
  40. break;
  41. case PLUGLOG_PROXY_MSG: {
  42. /* Proxy-related log messages have their own identifying
  43. * prefix already, put on by our caller. */
  44. int len, log_to_term;
  45. /* Suffix \r\n temporarily, so we can log to the terminal. */
  46. msg = dupprintf("%s\r\n", error_msg);
  47. len = strlen(msg);
  48. assert(len >= 2);
  49. log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
  50. if (log_to_term == AUTO)
  51. log_to_term = session_started ? FORCE_OFF : FORCE_ON;
  52. if (log_to_term == FORCE_ON)
  53. seat_stderr(seat, msg, len);
  54. msg[len-2] = '\0'; /* remove the \r\n again */
  55. break;
  56. }
  57. default:
  58. msg = NULL; /* shouldn't happen, but placate optimiser */
  59. break;
  60. }
  61. if (msg) {
  62. logevent(logctx, msg);
  63. sfree(msg);
  64. }
  65. }