2013-11-17 14:03:44 +00:00
|
|
|
/*
|
|
|
|
* General mechanism for wrapping up reading/writing of Windows
|
|
|
|
* HANDLEs into a PuTTY Socket abstraction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2013-11-17 14:03:48 +00:00
|
|
|
#include <limits.h>
|
2013-11-17 14:03:44 +00:00
|
|
|
|
|
|
|
#include "tree234.h"
|
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
typedef struct HandleSocket {
|
2015-11-22 11:50:37 +00:00
|
|
|
HANDLE send_H, recv_H, stderr_H;
|
|
|
|
struct handle *send_h, *recv_h, *stderr_h;
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2013-11-17 14:03:48 +00:00
|
|
|
/*
|
|
|
|
* Freezing one of these sockets is a slightly fiddly business,
|
|
|
|
* because the reads from the handle are happening in a separate
|
|
|
|
* thread as blocking system calls and so once one is in progress
|
|
|
|
* it can't sensibly be interrupted. Hence, after the user tries
|
|
|
|
* to freeze one of these sockets, it's unavoidable that we may
|
|
|
|
* receive one more load of data before we manage to get
|
|
|
|
* winhandl.c to stop reading.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
UNFROZEN, /* reading as normal */
|
|
|
|
FREEZING, /* have been set to frozen but winhandl is still reading */
|
|
|
|
FROZEN, /* really frozen - winhandl has been throttled */
|
|
|
|
THAWING /* we're gradually releasing our remaining data */
|
|
|
|
} frozen;
|
|
|
|
/* We buffer data here if we receive it from winhandl while frozen. */
|
|
|
|
bufchain inputdata;
|
|
|
|
|
2015-11-22 11:50:37 +00:00
|
|
|
/* Data received from stderr_H, if we have one. */
|
|
|
|
bufchain stderrdata;
|
|
|
|
|
2017-02-16 20:26:58 +00:00
|
|
|
int defer_close, deferred_close; /* in case of re-entrance */
|
|
|
|
|
2013-11-17 14:03:44 +00:00
|
|
|
char *error;
|
|
|
|
|
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
|
|
|
Plug *plug;
|
2018-05-27 08:29:33 +00:00
|
|
|
|
2018-10-05 06:24:16 +00:00
|
|
|
Socket sock;
|
2018-05-27 08:29:33 +00:00
|
|
|
} HandleSocket;
|
2013-11-17 14:03:44 +00:00
|
|
|
|
|
|
|
static int handle_gotdata(struct handle *h, void *data, int len)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
|
|
|
if (len < 0) {
|
2018-05-27 08:29:33 +00:00
|
|
|
plug_closing(hs->plug, "Read error from handle", 0, 0);
|
2016-06-04 14:42:06 +00:00
|
|
|
return 0;
|
2013-11-17 14:03:44 +00:00
|
|
|
} else if (len == 0) {
|
2018-05-27 08:29:33 +00:00
|
|
|
plug_closing(hs->plug, NULL, 0, 0);
|
2016-06-04 14:42:06 +00:00
|
|
|
return 0;
|
2013-11-17 14:03:44 +00:00
|
|
|
} else {
|
2018-05-27 08:29:33 +00:00
|
|
|
assert(hs->frozen != FROZEN && hs->frozen != THAWING);
|
|
|
|
if (hs->frozen == FREEZING) {
|
2013-11-17 14:03:48 +00:00
|
|
|
/*
|
|
|
|
* If we've received data while this socket is supposed to
|
|
|
|
* be frozen (because the read winhandl.c started before
|
|
|
|
* sk_set_frozen was called has now returned) then buffer
|
|
|
|
* the data for when we unfreeze.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
bufchain_add(&hs->inputdata, data, len);
|
|
|
|
hs->frozen = FROZEN;
|
2013-11-17 14:03:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And return a very large backlog, to prevent further
|
|
|
|
* data arriving from winhandl until we unfreeze.
|
|
|
|
*/
|
|
|
|
return INT_MAX;
|
|
|
|
} else {
|
2018-05-27 08:29:33 +00:00
|
|
|
plug_receive(hs->plug, 0, data, len);
|
2016-06-04 14:42:06 +00:00
|
|
|
return 0;
|
2013-11-17 14:03:48 +00:00
|
|
|
}
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 11:50:37 +00:00
|
|
|
static int handle_stderr(struct handle *h, void *data, int len)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
|
2015-11-22 11:50:37 +00:00
|
|
|
|
|
|
|
if (len > 0)
|
2018-05-27 08:29:33 +00:00
|
|
|
log_proxy_stderr(hs->plug, &hs->stderrdata, data, len);
|
2015-11-22 11:50:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-17 14:03:44 +00:00
|
|
|
static void handle_sentdata(struct handle *h, int new_backlog)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
|
2017-02-22 21:57:04 +00:00
|
|
|
|
|
|
|
if (new_backlog < 0) {
|
|
|
|
/* Special case: this is actually reporting an error writing
|
|
|
|
* to the underlying handle, and our input value is the error
|
|
|
|
* code itself, negated. */
|
2018-05-27 08:29:33 +00:00
|
|
|
plug_closing(hs->plug, win_strerror(-new_backlog), -new_backlog, 0);
|
2017-02-22 21:57:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
plug_sent(hs->plug, new_backlog);
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static Plug *sk_handle_plug(Socket *s, Plug *p)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
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
|
|
|
Plug *ret = hs->plug;
|
2013-11-17 14:03:44 +00:00
|
|
|
if (p)
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->plug = p;
|
2013-11-17 14:03:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_handle_close(Socket *s)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
if (hs->defer_close) {
|
|
|
|
hs->deferred_close = TRUE;
|
2017-02-16 20:26:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
handle_free(hs->send_h);
|
|
|
|
handle_free(hs->recv_h);
|
|
|
|
CloseHandle(hs->send_H);
|
|
|
|
if (hs->recv_H != hs->send_H)
|
|
|
|
CloseHandle(hs->recv_H);
|
|
|
|
bufchain_clear(&hs->inputdata);
|
|
|
|
bufchain_clear(&hs->stderrdata);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
sfree(hs);
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static int sk_handle_write(Socket *s, const void *data, int len)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
return handle_write(hs->send_h, data, len);
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static int sk_handle_write_oob(Socket *s, const void *data, int len)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* oob data is treated as inband; nasty, but nothing really
|
|
|
|
* better we can do
|
|
|
|
*/
|
|
|
|
return sk_handle_write(s, data, len);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_handle_write_eof(Socket *s)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
handle_write_eof(hs->send_h);
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_handle_flush(Socket *s)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
/* HandleSocket *hs = container_of(s, HandleSocket, sock); */
|
2013-11-17 14:03:44 +00:00
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
static void handle_socket_unfreeze(void *hsv)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)hsv;
|
2013-11-17 14:03:48 +00:00
|
|
|
void *data;
|
2016-06-04 14:42:06 +00:00
|
|
|
int len;
|
2013-11-17 14:03:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we've been put into a state other than THAWING since the
|
|
|
|
* last callback, then we're done.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
if (hs->frozen != THAWING)
|
2013-11-17 14:03:48 +00:00
|
|
|
return;
|
2013-11-17 14:03:44 +00:00
|
|
|
|
|
|
|
/*
|
2013-11-17 14:03:48 +00:00
|
|
|
* Get some of the data we've buffered.
|
2013-11-17 14:03:44 +00:00
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
bufchain_prefix(&hs->inputdata, &data, &len);
|
2013-11-17 14:03:48 +00:00
|
|
|
assert(len > 0);
|
|
|
|
|
|
|
|
/*
|
2017-02-16 20:26:58 +00:00
|
|
|
* Hand it off to the plug. Be careful of re-entrance - that might
|
|
|
|
* have the effect of trying to close this socket.
|
2013-11-17 14:03:48 +00:00
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->defer_close = TRUE;
|
|
|
|
plug_receive(hs->plug, 0, data, len);
|
|
|
|
bufchain_consume(&hs->inputdata, len);
|
|
|
|
hs->defer_close = FALSE;
|
|
|
|
if (hs->deferred_close) {
|
2018-10-05 06:24:16 +00:00
|
|
|
sk_handle_close(&hs->sock);
|
2017-02-16 20:26:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-17 14:03:48 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
if (bufchain_size(&hs->inputdata) > 0) {
|
2013-11-17 14:03:48 +00:00
|
|
|
/*
|
|
|
|
* If there's still data in our buffer, stay in THAWING state,
|
|
|
|
* and reschedule ourself.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
queue_toplevel_callback(handle_socket_unfreeze, hs);
|
2013-11-17 14:03:48 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Otherwise, we've successfully thawed!
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->frozen = UNFROZEN;
|
|
|
|
handle_unthrottle(hs->recv_h, 0);
|
2013-11-17 14:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_handle_set_frozen(Socket *s, int is_frozen)
|
2013-11-17 14:03:48 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
2013-11-17 14:03:48 +00:00
|
|
|
|
|
|
|
if (is_frozen) {
|
2018-05-27 08:29:33 +00:00
|
|
|
switch (hs->frozen) {
|
2013-11-17 14:03:48 +00:00
|
|
|
case FREEZING:
|
|
|
|
case FROZEN:
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
case THAWING:
|
|
|
|
/*
|
|
|
|
* We were in the middle of emptying our bufchain, and got
|
|
|
|
* frozen again. In that case, winhandl.c is already
|
|
|
|
* throttled, so just return to FROZEN state. The toplevel
|
|
|
|
* callback will notice and disable itself.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->frozen = FROZEN;
|
2013-11-17 14:03:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNFROZEN:
|
|
|
|
/*
|
|
|
|
* The normal case. Go to FREEZING, and expect one more
|
|
|
|
* load of data from winhandl if we're unlucky.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->frozen = FREEZING;
|
2013-11-17 14:03:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-27 08:29:33 +00:00
|
|
|
switch (hs->frozen) {
|
2013-11-17 14:03:48 +00:00
|
|
|
case UNFROZEN:
|
|
|
|
case THAWING:
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
case FREEZING:
|
|
|
|
/*
|
|
|
|
* If winhandl didn't send us any data throughout the time
|
|
|
|
* we were frozen, then we'll still be in this state and
|
|
|
|
* can just unfreeze in the trivial way.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
assert(bufchain_size(&hs->inputdata) == 0);
|
|
|
|
hs->frozen = UNFROZEN;
|
2013-11-17 14:03:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FROZEN:
|
|
|
|
/*
|
|
|
|
* If we have buffered data, go to THAWING and start
|
|
|
|
* releasing it in top-level callbacks.
|
|
|
|
*/
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->frozen = THAWING;
|
|
|
|
queue_toplevel_callback(handle_socket_unfreeze, hs);
|
2013-11-17 14:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static const char *sk_handle_socket_error(Socket *s)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
2018-05-27 08:29:33 +00:00
|
|
|
return hs->error;
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static char *sk_handle_peer_info(Socket *s)
|
2015-05-18 12:57:45 +00:00
|
|
|
{
|
2018-10-05 22:49:08 +00:00
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
2015-05-18 15:00:13 +00:00
|
|
|
ULONG pid;
|
|
|
|
static HMODULE kernel32_module;
|
|
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
|
|
|
|
(HANDLE, PULONG));
|
|
|
|
|
|
|
|
if (!kernel32_module) {
|
|
|
|
kernel32_module = load_system32_dll("kernel32.dll");
|
2017-06-20 18:02:48 +00:00
|
|
|
#if (defined _MSC_VER && _MSC_VER < 1900) || defined __MINGW32__ || defined COVERITY
|
2017-04-15 17:13:47 +00:00
|
|
|
/* For older Visual Studio, and MinGW too (at least as of
|
|
|
|
* Ubuntu 16.04), this function isn't available in the header
|
2017-06-20 18:02:48 +00:00
|
|
|
* files to type-check. Ditto the toolchain I use for
|
|
|
|
* Coveritying the Windows code. */
|
Add automatic type-checking to GET_WINDOWS_FUNCTION.
This gives me an extra safety-check against having mistyped one of the
function prototypes that we load at run time from DLLs: we verify that
the typedef we defined based on the prototype in our source code
matches the type of the real function as declared in the Windows
headers.
This was an idea I had while adding a pile of further functions using
this mechanism. It didn't catch any errors (either in the new
functions or in the existing collection), but that's no reason not to
keep it anyway now that I've thought of it!
In VS2015, this automated type-check works for most functions, but a
couple manage to break it. SetCurrentProcessExplicitAppUserModelID in
winjump.c can't be type-checked, because including <shobjidl.h> where
that function is declared would also bring in a load of other stuff
that conflicts with the painful manual COM declarations in winjump.c.
(That stuff could probably be removed now we're on an up-to-date
Visual Studio, on the other hand, but that's a separate chore.) And
gai_strerror, used in winnet.c, does _have_ an implementation in a
DLL, but the header files like to provide an inline version with a
different calling convention, which defeats this error-checking trick.
And in the older VS2003 that we still precautionarily build with,
several more type-checks have to be #ifdeffed out because the
functions they check against just aren't there at all.
2017-04-11 17:56:55 +00:00
|
|
|
GET_WINDOWS_FUNCTION_NO_TYPECHECK(
|
|
|
|
kernel32_module, GetNamedPipeClientProcessId);
|
|
|
|
#else
|
|
|
|
GET_WINDOWS_FUNCTION(
|
|
|
|
kernel32_module, GetNamedPipeClientProcessId);
|
|
|
|
#endif
|
2015-05-18 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Of course, not all handles managed by this module will be
|
|
|
|
* server ends of named pipes, but if they are, then it's useful
|
|
|
|
* to log what we can find out about the client end.
|
|
|
|
*/
|
|
|
|
if (p_GetNamedPipeClientProcessId &&
|
2018-05-27 08:29:33 +00:00
|
|
|
p_GetNamedPipeClientProcessId(hs->send_H, &pid))
|
2015-05-18 15:00:13 +00:00
|
|
|
return dupprintf("process id %lu", (unsigned long)pid);
|
|
|
|
|
2015-05-18 12:57:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-05 06:03:46 +00:00
|
|
|
static const SocketVtable HandleSocket_sockvt = {
|
2018-05-27 08:29:33 +00:00
|
|
|
sk_handle_plug,
|
|
|
|
sk_handle_close,
|
|
|
|
sk_handle_write,
|
|
|
|
sk_handle_write_oob,
|
|
|
|
sk_handle_write_eof,
|
|
|
|
sk_handle_flush,
|
|
|
|
sk_handle_set_frozen,
|
|
|
|
sk_handle_socket_error,
|
|
|
|
sk_handle_peer_info,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
|
|
|
|
Plug *plug, int overlapped)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs;
|
2013-11-17 14:03:44 +00:00
|
|
|
int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
hs = snew(HandleSocket);
|
2018-10-05 06:24:16 +00:00
|
|
|
hs->sock.vt = &HandleSocket_sockvt;
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->plug = plug;
|
|
|
|
hs->error = NULL;
|
|
|
|
hs->frozen = UNFROZEN;
|
|
|
|
bufchain_init(&hs->inputdata);
|
|
|
|
bufchain_init(&hs->stderrdata);
|
|
|
|
|
|
|
|
hs->recv_H = recv_H;
|
|
|
|
hs->recv_h = handle_input_new(hs->recv_H, handle_gotdata, hs, flags);
|
|
|
|
hs->send_H = send_H;
|
|
|
|
hs->send_h = handle_output_new(hs->send_H, handle_sentdata, hs, flags);
|
|
|
|
hs->stderr_H = stderr_H;
|
|
|
|
if (hs->stderr_H)
|
|
|
|
hs->stderr_h = handle_input_new(hs->stderr_H, handle_stderr,
|
|
|
|
hs, flags);
|
|
|
|
|
|
|
|
hs->defer_close = hs->deferred_close = FALSE;
|
|
|
|
|
2018-10-05 06:24:16 +00:00
|
|
|
return &hs->sock;
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|