2 * be_misc.c: helper functions shared between main network backends.
8 #define DEFINE_PLUG_METHOD_MACROS
12 void backend_socket_log(void *frontend
, int type
, SockAddr addr
, int port
,
13 const char *error_msg
, int error_code
, Conf
*conf
,
16 char addrbuf
[256], *msg
;
20 sk_getaddr(addr
, addrbuf
, lenof(addrbuf
));
21 if (sk_addr_needs_port(addr
)) {
22 msg
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
24 msg
= dupprintf("Connecting to %s", addrbuf
);
28 sk_getaddr(addr
, addrbuf
, lenof(addrbuf
));
29 msg
= dupprintf("Failed to connect to %s: %s", addrbuf
, error_msg
);
32 /* Proxy-related log messages have their own identifying
33 * prefix already, put on by our caller. */
37 /* Suffix \r\n temporarily, so we can log to the terminal. */
38 msg
= dupprintf("%s\r\n", error_msg
);
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 */
52 msg
= NULL
; /* shouldn't happen, but placate optimiser */
57 logevent(frontend
, msg
);
62 void log_proxy_stderr(Plug plug
, bufchain
*buf
, const void *vdata
, int len
)
64 const char *data
= (const char *)vdata
;
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
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
);
95 fullmsg
= dupprintf("proxy: %s", msg
);
96 plug_log(plug
, 2, NULL
, 0, fullmsg
, 0);
101 * Advance past the newline.
103 pos
+= nlpos
+1 - (data
+ pos
);
107 * Now any remaining data is a partial line, which we save for
110 bufchain_add(buf
, data
+ pos
, len
- pos
);