]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - be_misc.c
first pass
[PuTTY.git] / be_misc.c
1 /*
2  * be_misc.c: helper functions shared between main network backends.
3  */
4
5 #include <assert.h>
6 #include <string.h>
7
8 #define DEFINE_PLUG_METHOD_MACROS
9 #include "putty.h"
10 #include "network.h"
11
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)
15 {
16     char addrbuf[256], *msg;
17
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);
25         }
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. */
34         {
35             int len, log_to_term;
36
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);
41
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);
47
48             msg[len-2] = '\0';         /* remove the \r\n again */
49         }
50         break;
51       default:
52         msg = NULL;  /* shouldn't happen, but placate optimiser */
53         break;
54     }
55
56     if (msg) {
57         logevent(frontend, msg);
58         sfree(msg);
59     }
60 }
61
62 void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len)
63 {
64     const char *data = (const char *)vdata;
65     int pos = 0;
66     int msglen;
67     char *nlpos, *msg, *fullmsg;
68
69     /*
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.
74      *
75      * Prerequisites: a plug to log to, and a bufchain stored
76      * somewhere to collect the data in.
77      */
78
79     while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
80         /*
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).
84          */
85         bufchain_add(buf, data + pos, nlpos - (data + pos));
86
87         /*
88          * Collect the resulting line of data and pass it to plug_log.
89          */
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);
99
100         /*
101          * Advance past the newline.
102          */
103         pos += nlpos+1 - (data + pos);
104     }
105
106     /*
107      * Now any remaining data is a partial line, which we save for
108      * next time.
109      */
110     bufchain_add(buf, data + pos, len - pos);
111 }