1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

Allow creating FdSocket/HandleSocket before the fds/handles.

Previously, a setup function returning one of these socket types (such
as platform_new_connection) had to do all its setup synchronously,
because if it was going to call make_fd_socket or make_handle_socket,
it had to have the actual fds or HANDLEs ready-made. If some kind of
asynchronous operation were needed before those fds become available,
there would be no way the function could achieve it, except by
becoming a whole extra permanent Socket wrapper layer.

Now there is, because you can make an FdSocket when you don't yet have
the fds, or a HandleSocket without the HANDLEs. Instead, you provide
an instance of the new trait 'DeferredSocketOpener', which is
responsible for setting in motion whatever asynchronous setup
procedure it needs, and when that finishes, calling back to
setup_fd_socket / setup_handle_socket to provide the missing pieces.

In the meantime, the FdSocket or HandleSocket will sit there inertly,
buffering any data the client might eagerly hand it via sk_write(),
and waiting for its setup to finish. When it does finish, buffered
data will be released.

In FdSocket, this is easy enough, because we were doing our own
buffering anyway - we called the uxsel system to find out when the fds
were readable/writable, and then wrote to them from our own bufchain.
So more or less all I had to do was make the try_send function do
nothing if the setup phase wasn't finished yet.

In HandleSocket, on the other hand, we're passing all our data to the
underlying handle-io.c system, and making _that_ deferrable in the
same way would be much more painful, because that's the place where
the scary threads live. So instead I've arranged it by replacing the
whole vtable, so that a deferred HandleSocket and a normal
HandleSocket are effectively separate trait implementations that can
share their state structure. And in fact that state struct itself now
contains a big anonymous union, containing one branch to go with each
vtable.

Nothing yet uses this system, but the next commit will do so.
This commit is contained in:
Simon Tatham
2021-12-22 09:31:06 +00:00
parent 48b7ef21a1
commit ca70b1285d
6 changed files with 245 additions and 37 deletions

View File

@ -11,32 +11,51 @@
#include "putty.h"
#include "network.h"
/*
* 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.
*/
typedef enum HandleSocketFreezeState {
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 */
} HandleSocketFreezeState;
typedef struct HandleSocket {
HANDLE send_H, recv_H, stderr_H;
struct handle *send_h, *recv_h, *stderr_h;
union {
struct {
HANDLE send_H, recv_H, stderr_H;
struct handle *send_h, *recv_h, *stderr_h;
/*
* 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;
HandleSocketFreezeState frozen;
/* We buffer data here if we receive it from winhandl
* while frozen. */
bufchain inputdata;
/* Handle logging proxy error messages from stderr_H, if we have one. */
ProxyStderrBuf psb;
/* Handle logging proxy error messages from stderr_H, if
* we have one */
ProxyStderrBuf psb;
bool defer_close, deferred_close; /* in case of re-entrance */
bool defer_close, deferred_close; /* in case of re-entrance */
};
struct {
DeferredSocketOpener *opener;
/* We buffer data here if we receive it via sk_write
* before the socket is opened. */
bufchain outputdata;
bool output_eof_pending;
bool start_frozen;
};
};
char *error;
@ -348,6 +367,7 @@ Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
hs->port = port;
hs->plug = plug;
hs->error = NULL;
hs->frozen = UNFROZEN;
bufchain_init(&hs->inputdata);
psb_init(&hs->psb);
@ -367,3 +387,119 @@ Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
return &hs->sock;
}
static void sk_handle_deferred_close(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
deferred_socket_opener_free(hs->opener);
bufchain_clear(&hs->outputdata);
if (hs->addr)
sk_addr_free(hs->addr);
delete_callbacks_for_context(hs);
sfree(hs);
}
static size_t sk_handle_deferred_write(Socket *s, const void *data, size_t len)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
assert(!hs->output_eof_pending);
bufchain_add(&hs->outputdata, data, len);
return bufchain_size(&hs->outputdata);
}
static void sk_handle_deferred_write_eof(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
assert(!hs->output_eof_pending);
hs->output_eof_pending = true;
}
static void sk_handle_deferred_set_frozen(Socket *s, bool is_frozen)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
hs->frozen = is_frozen;
}
static SocketPeerInfo *sk_handle_deferred_peer_info(Socket *s)
{
return NULL;
}
static const SocketVtable HandleSocket_deferred_sockvt = {
.plug = sk_handle_plug,
.close = sk_handle_deferred_close,
.write = sk_handle_deferred_write,
.write_oob = sk_handle_deferred_write,
.write_eof = sk_handle_deferred_write_eof,
.set_frozen = sk_handle_deferred_set_frozen,
.socket_error = sk_handle_socket_error,
.peer_info = sk_handle_deferred_peer_info,
};
Socket *make_deferred_handle_socket(DeferredSocketOpener *opener,
SockAddr *addr, int port, Plug *plug)
{
HandleSocket *hs = snew(HandleSocket);
hs->sock.vt = &HandleSocket_deferred_sockvt;
hs->addr = addr;
hs->port = port;
hs->plug = plug;
hs->error = NULL;
hs->opener = opener;
bufchain_init(&hs->outputdata);
hs->output_eof_pending = false;
hs->start_frozen = false;
return &hs->sock;
}
void setup_handle_socket(Socket *s, HANDLE send_H, HANDLE recv_H,
HANDLE stderr_H, bool overlapped)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
assert(hs->sock.vt == &HandleSocket_deferred_sockvt);
int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
struct handle *recv_h = handle_input_new(
recv_H, handle_gotdata, hs, flags);
struct handle *send_h = handle_output_new(
send_H, handle_sentdata, hs, flags);
struct handle *stderr_h = !stderr_H ? NULL : handle_input_new(
stderr_H, handle_stderr, hs, flags);
while (bufchain_size(&hs->outputdata)) {
ptrlen data = bufchain_prefix(&hs->outputdata);
handle_write(send_h, data.ptr, data.len);
bufchain_consume(&hs->outputdata, data.len);
}
if (hs->output_eof_pending)
handle_write_eof(send_h);
bool start_frozen = hs->start_frozen;
deferred_socket_opener_free(hs->opener);
bufchain_clear(&hs->outputdata);
hs->sock.vt = &HandleSocket_sockvt;
hs->frozen = start_frozen ? FREEZING : UNFROZEN;
bufchain_init(&hs->inputdata);
psb_init(&hs->psb);
hs->recv_H = recv_H;
hs->recv_h = recv_h;
hs->send_H = send_H;
hs->send_h = send_h;
hs->stderr_H = stderr_H;
hs->stderr_h = stderr_h;
hs->defer_close = hs->deferred_close = false;
queue_toplevel_callback(sk_handle_connect_success_callback, hs);
}

View File

@ -339,6 +339,10 @@ extern HANDLE winselcli_event;
Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
SockAddr *addr, int port, Plug *plug,
bool overlapped); /* winhsock */
Socket *make_deferred_handle_socket(DeferredSocketOpener *opener,
SockAddr *addr, int port, Plug *plug);
void setup_handle_socket(Socket *s, HANDLE send_H, HANDLE recv_H,
HANDLE stderr_H, bool overlapped);
Socket *new_named_pipe_client(const char *pipename, Plug *plug); /* winnpc */
Socket *new_named_pipe_listener(const char *pipename, Plug *plug); /* winnps */