2 * be_misc.c: helper functions shared between main network backends.
5 #define DEFINE_PLUG_METHOD_MACROS
9 void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
10 const char *error_msg, int error_code)
12 char addrbuf[256], *msg;
16 sk_getaddr(addr, addrbuf, lenof(addrbuf));
17 if (sk_addr_needs_port(addr)) {
18 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
20 msg = dupprintf("Connecting to %s", addrbuf);
24 sk_getaddr(addr, addrbuf, lenof(addrbuf));
25 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
28 /* Proxy-related log messages have their own identifying
29 * prefix already, put on by our caller. */
30 msg = dupstr(error_msg);
33 msg = NULL; /* shouldn't happen, but placate optimiser */
38 logevent(frontend, msg);
43 void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len)
45 const char *data = (const char *)vdata;
48 char *nlpos, *msg, *fullmsg;
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.
56 * Prerequisites: a plug to log to, and a bufchain stored
57 * somewhere to collect the data in.
60 while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
62 * Found a newline in the current input buffer. Append it to
63 * the bufchain (which may contain a partial line from last
66 bufchain_add(buf, data + pos, nlpos - (data + pos));
69 * Collect the resulting line of data and pass it to plug_log.
71 msglen = bufchain_size(buf);
72 msg = snewn(msglen+1, char);
73 bufchain_fetch(buf, msg, msglen);
74 bufchain_consume(buf, msglen);
76 fullmsg = dupprintf("proxy: %s", msg);
77 plug_log(plug, 2, NULL, 0, fullmsg, 0);
82 * Advance past the newline.
84 pos += nlpos+1 - (data + pos);
88 * Now any remaining data is a partial line, which we save for
91 bufchain_add(buf, data + pos, len - pos);