CPatch: New memory management
[TortoiseGit.git] / src / TortoisePlink / be_misc.c
blob3d89e48080a0a34408719eed3915693860e1a356
1 /*
2 * be_misc.c: helper functions shared between main network backends.
3 */
5 #include <assert.h>
6 #include <string.h>
8 #define DEFINE_PLUG_METHOD_MACROS
9 #include "putty.h"
10 #include "network.h"
12 void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
13 const char *error_msg, int error_code, Conf *conf,
14 int session_started)
16 char addrbuf[256], *msg;
18 switch (type) {
19 case 0:
20 sk_getaddr(addr, addrbuf, lenof(addrbuf));
21 if (sk_addr_needs_port(addr)) {
22 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
23 } else {
24 msg = dupprintf("Connecting to %s", addrbuf);
26 break;
27 case 1:
28 sk_getaddr(addr, addrbuf, lenof(addrbuf));
29 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
30 break;
31 case 2:
32 /* Proxy-related log messages have their own identifying
33 * prefix already, put on by our caller. */
35 int len, log_to_term;
37 /* Suffix \r\n temporarily, so we can log to the terminal. */
38 msg = dupprintf("%s\r\n", error_msg);
39 len = strlen(msg);
40 assert(len >= 2);
42 log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
43 if (log_to_term == AUTO)
44 log_to_term = session_started ? FORCE_OFF : FORCE_ON;
45 if (log_to_term == FORCE_ON)
46 from_backend(frontend, TRUE, msg, len);
48 msg[len-2] = '\0'; /* remove the \r\n again */
50 break;
51 default:
52 msg = NULL; /* shouldn't happen, but placate optimiser */
53 break;
56 if (msg) {
57 logevent(frontend, msg);
58 sfree(msg);
62 void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len)
64 const char *data = (const char *)vdata;
65 int pos = 0;
66 int msglen;
67 char *nlpos, *msg, *fullmsg;
70 * This helper function allows us to collect the data written to a
71 * local proxy command's standard error in whatever size chunks we
72 * happen to get from its pipe, and whenever we have a complete
73 * line, we pass it to plug_log.
75 * Prerequisites: a plug to log to, and a bufchain stored
76 * somewhere to collect the data in.
79 while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
81 * Found a newline in the current input buffer. Append it to
82 * the bufchain (which may contain a partial line from last
83 * time).
85 bufchain_add(buf, data + pos, nlpos - (data + pos));
88 * Collect the resulting line of data and pass it to plug_log.
90 msglen = bufchain_size(buf);
91 msg = snewn(msglen+1, char);
92 bufchain_fetch(buf, msg, msglen);
93 bufchain_consume(buf, msglen);
94 msg[msglen] = '\0';
95 fullmsg = dupprintf("proxy: %s", msg);
96 plug_log(plug, 2, NULL, 0, fullmsg, 0);
97 sfree(fullmsg);
98 sfree(msg);
101 * Advance past the newline.
103 pos += nlpos+1 - (data + pos);
107 * Now any remaining data is a partial line, which we save for
108 * next time.
110 bufchain_add(buf, data + pos, len - pos);