2015-11-22 11:49:14 +00:00
|
|
|
/*
|
|
|
|
* be_misc.c: helper functions shared between main network backends.
|
|
|
|
*/
|
|
|
|
|
2015-11-22 14:33:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-11-22 11:49:14 +00:00
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
void backend_socket_log(Frontend *frontend, int type, SockAddr *addr, int port,
|
2015-11-22 14:33:28 +00:00
|
|
|
const char *error_msg, int error_code, Conf *conf,
|
|
|
|
int session_started)
|
2015-11-22 11:49:14 +00:00
|
|
|
{
|
|
|
|
char addrbuf[256], *msg;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
sk_getaddr(addr, addrbuf, lenof(addrbuf));
|
|
|
|
if (sk_addr_needs_port(addr)) {
|
|
|
|
msg = dupprintf("Connecting to %s port %d", addrbuf, port);
|
|
|
|
} else {
|
|
|
|
msg = dupprintf("Connecting to %s", addrbuf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sk_getaddr(addr, addrbuf, lenof(addrbuf));
|
|
|
|
msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
|
|
|
|
break;
|
2015-11-22 12:15:52 +00:00
|
|
|
case 2:
|
|
|
|
/* Proxy-related log messages have their own identifying
|
|
|
|
* prefix already, put on by our caller. */
|
2015-11-22 14:33:28 +00:00
|
|
|
{
|
|
|
|
int len, log_to_term;
|
|
|
|
|
|
|
|
/* Suffix \r\n temporarily, so we can log to the terminal. */
|
|
|
|
msg = dupprintf("%s\r\n", error_msg);
|
|
|
|
len = strlen(msg);
|
|
|
|
assert(len >= 2);
|
|
|
|
|
|
|
|
log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
|
|
|
|
if (log_to_term == AUTO)
|
|
|
|
log_to_term = session_started ? FORCE_OFF : FORCE_ON;
|
|
|
|
if (log_to_term == FORCE_ON)
|
|
|
|
from_backend(frontend, TRUE, msg, len);
|
|
|
|
|
|
|
|
msg[len-2] = '\0'; /* remove the \r\n again */
|
|
|
|
}
|
2015-11-22 12:15:52 +00:00
|
|
|
break;
|
2015-11-22 11:49:14 +00:00
|
|
|
default:
|
|
|
|
msg = NULL; /* shouldn't happen, but placate optimiser */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
logevent(frontend, msg);
|
|
|
|
sfree(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
void log_proxy_stderr(Plug *plug, bufchain *buf, const void *vdata, int len)
|
2015-11-22 11:50:37 +00:00
|
|
|
{
|
|
|
|
const char *data = (const char *)vdata;
|
|
|
|
int pos = 0;
|
|
|
|
int msglen;
|
2018-05-26 07:00:20 +00:00
|
|
|
const char *nlpos;
|
|
|
|
char *msg, *fullmsg;
|
2015-11-22 11:50:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This helper function allows us to collect the data written to a
|
|
|
|
* local proxy command's standard error in whatever size chunks we
|
|
|
|
* happen to get from its pipe, and whenever we have a complete
|
|
|
|
* line, we pass it to plug_log.
|
|
|
|
*
|
|
|
|
* Prerequisites: a plug to log to, and a bufchain stored
|
|
|
|
* somewhere to collect the data in.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
|
|
|
|
/*
|
|
|
|
* Found a newline in the current input buffer. Append it to
|
|
|
|
* the bufchain (which may contain a partial line from last
|
|
|
|
* time).
|
|
|
|
*/
|
|
|
|
bufchain_add(buf, data + pos, nlpos - (data + pos));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect the resulting line of data and pass it to plug_log.
|
|
|
|
*/
|
|
|
|
msglen = bufchain_size(buf);
|
|
|
|
msg = snewn(msglen+1, char);
|
|
|
|
bufchain_fetch(buf, msg, msglen);
|
|
|
|
bufchain_consume(buf, msglen);
|
2018-10-07 13:46:21 +00:00
|
|
|
while (msglen > 0 && (msg[msglen-1] == '\n' || msg[msglen-1] == '\r'))
|
|
|
|
msglen--;
|
2015-11-22 11:50:37 +00:00
|
|
|
msg[msglen] = '\0';
|
|
|
|
fullmsg = dupprintf("proxy: %s", msg);
|
|
|
|
plug_log(plug, 2, NULL, 0, fullmsg, 0);
|
|
|
|
sfree(fullmsg);
|
|
|
|
sfree(msg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Advance past the newline.
|
|
|
|
*/
|
|
|
|
pos += nlpos+1 - (data + pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now any remaining data is a partial line, which we save for
|
|
|
|
* next time.
|
|
|
|
*/
|
|
|
|
bufchain_add(buf, data + pos, len - pos);
|
|
|
|
}
|