]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - be_misc.c
In GUI PuTTY, log standard error from local proxy commands.
[PuTTY.git] / be_misc.c
1 /*
2  * be_misc.c: helper functions shared between main network backends.
3  */
4
5 #define DEFINE_PLUG_METHOD_MACROS
6 #include "putty.h"
7 #include "network.h"
8
9 void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
10                         const char *error_msg, int error_code)
11 {
12     char addrbuf[256], *msg;
13
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         msg = dupstr(error_msg);
31         break;
32       default:
33         msg = NULL;  /* shouldn't happen, but placate optimiser */
34         break;
35     }
36
37     if (msg) {
38         logevent(frontend, msg);
39         sfree(msg);
40     }
41 }
42
43 void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len)
44 {
45     const char *data = (const char *)vdata;
46     int pos = 0;
47     int msglen;
48     char *nlpos, *msg, *fullmsg;
49
50     /*
51      * This helper function allows us to collect the data written to a
52      * local proxy command's standard error in whatever size chunks we
53      * happen to get from its pipe, and whenever we have a complete
54      * line, we pass it to plug_log.
55      *
56      * Prerequisites: a plug to log to, and a bufchain stored
57      * somewhere to collect the data in.
58      */
59
60     while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
61         /*
62          * Found a newline in the current input buffer. Append it to
63          * the bufchain (which may contain a partial line from last
64          * time).
65          */
66         bufchain_add(buf, data + pos, nlpos - (data + pos));
67
68         /*
69          * Collect the resulting line of data and pass it to plug_log.
70          */
71         msglen = bufchain_size(buf);
72         msg = snewn(msglen+1, char);
73         bufchain_fetch(buf, msg, msglen);
74         bufchain_consume(buf, msglen);
75         msg[msglen] = '\0';
76         fullmsg = dupprintf("proxy: %s", msg);
77         plug_log(plug, 2, NULL, 0, fullmsg, 0);
78         sfree(fullmsg);
79         sfree(msg);
80
81         /*
82          * Advance past the newline.
83          */
84         pos += nlpos+1 - (data + pos);
85     }
86
87     /*
88      * Now any remaining data is a partial line, which we save for
89      * next time.
90      */
91     bufchain_add(buf, data + pos, len - pos);
92 }