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"
|
|
|
|
|
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.
2021-12-22 09:31:06 +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
|
2022-01-22 15:38:53 +00:00
|
|
|
* one more load of data before we manage to get handle-io.c to stop
|
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.
2021-12-22 09:31:06 +00:00
|
|
|
* 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;
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
typedef struct HandleSocket {
|
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.
2021-12-22 09:31:06 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
HANDLE send_H, recv_H, stderr_H;
|
|
|
|
struct handle *send_h, *recv_h, *stderr_h;
|
2013-11-17 14:03:44 +00: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.
2021-12-22 09:31:06 +00:00
|
|
|
HandleSocketFreezeState frozen;
|
|
|
|
/* We buffer data here if we receive it from winhandl
|
|
|
|
* while frozen. */
|
|
|
|
bufchain inputdata;
|
2013-11-17 14:03:48 +00: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.
2021-12-22 09:31:06 +00:00
|
|
|
/* Handle logging proxy error messages from stderr_H, if
|
|
|
|
* we have one */
|
|
|
|
ProxyStderrBuf psb;
|
2015-11-22 11:50:37 +00: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.
2021-12-22 09:31:06 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
2017-02-16 20:26:58 +00:00
|
|
|
|
2013-11-17 14:03:44 +00:00
|
|
|
char *error;
|
|
|
|
|
2021-09-13 13:34:46 +00:00
|
|
|
SockAddr *addr;
|
|
|
|
int port;
|
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
|
|
|
|
2019-02-06 20:42:44 +00:00
|
|
|
static size_t handle_gotdata(
|
|
|
|
struct handle *h, const void *data, size_t len, int err)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2019-02-06 20:36:11 +00:00
|
|
|
if (err) {
|
Convenience wrappers on plug_closing().
Having a single plug_closing() function covering various kinds of
closure is reasonably convenient from the point of view of Plug
implementations, but it's annoying for callers, who all have to fill
in pointless NULL and 0 parameters in the cases where they're not
used.
Added some inline helper functions in network.h alongside the main
plug_closing() dispatch wrappers, so that each kind of connection
closure can present a separate API for the Socket side of the
interface, without complicating the vtable for the Plug side.
Also, added OS-specific extra helpers in the Unix and Windows
directories, which centralise the job of taking an OS error code (of
whatever kind) and translating it into its error message.
In passing, this removes the horrible ad-hoc made-up error codes in
proxy.h, which is OK, because nothing checked for them anyway, and
also I'm about to do an API change to plug_closing proper that removes
the need for them.
2021-11-06 13:25:42 +00:00
|
|
|
plug_closing_error(hs->plug, "Read error from handle");
|
2019-09-08 19:29:00 +00:00
|
|
|
return 0;
|
2013-11-17 14:03:44 +00:00
|
|
|
} else if (len == 0) {
|
Convenience wrappers on plug_closing().
Having a single plug_closing() function covering various kinds of
closure is reasonably convenient from the point of view of Plug
implementations, but it's annoying for callers, who all have to fill
in pointless NULL and 0 parameters in the cases where they're not
used.
Added some inline helper functions in network.h alongside the main
plug_closing() dispatch wrappers, so that each kind of connection
closure can present a separate API for the Socket side of the
interface, without complicating the vtable for the Plug side.
Also, added OS-specific extra helpers in the Unix and Windows
directories, which centralise the job of taking an OS error code (of
whatever kind) and translating it into its error message.
In passing, this removes the horrible ad-hoc made-up error codes in
proxy.h, which is OK, because nothing checked for them anyway, and
also I'm about to do an API change to plug_closing proper that removes
the need for them.
2021-11-06 13:25:42 +00:00
|
|
|
plug_closing_normal(hs->plug);
|
2019-09-08 19:29:00 +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
|
2022-01-22 15:38:53 +00:00
|
|
|
* be frozen (because the read handle-io.c started before
|
2013-11-17 14:03:48 +00:00
|
|
|
* 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);
|
2019-09-08 19:29:00 +00:00
|
|
|
return 0;
|
2013-11-17 14:03:48 +00:00
|
|
|
}
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 20:42:44 +00:00
|
|
|
static size_t handle_stderr(
|
|
|
|
struct handle *h, const void *data, size_t len, int err)
|
2015-11-22 11:50:37 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
|
2015-11-22 11:50:37 +00:00
|
|
|
|
2019-02-06 20:36:11 +00:00
|
|
|
if (!err && len > 0)
|
2024-06-26 07:29:39 +00:00
|
|
|
log_proxy_stderr(hs->plug, &hs->sock, &hs->psb, data, len);
|
2015-11-22 11:50:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
handle_write_eof: delegate CloseHandle back to the client.
When a writable HANDLE is managed by the handle-io.c system, you ask
to send EOF on the handle by calling handle_write_eof. That waits
until all buffered data has been written, and then sends an EOF event
by simply closing the handle.
That is, of course, the only way to send an EOF signal on a handle at
all. And yet, it's a bug, because the handle_output system does not
take ownership of the handle you give it: the client of handle_output
retains ownership, keeps its own copy of the handle, and will expect
to close it itself.
In most cases, the extra close will harmlessly fail, and return
ERROR_INVALID_HANDLE (which the caller didn't notice anyway). But if
you're unlucky, in conditions of frantic handle opening and closing
(e.g. with a lot of separate named-pipe-style agent forwarding
connections being constantly set up and torn down), the handle value
might have been reused between the two closes, so that the second
CloseHandle closes an unrelated handle belonging to some other part of
the program.
We can't fix this by giving handle_output permanent ownership of the
handle, because it really _is_ necessary for copies of it to survive
elsewhere: in particular, for a bidirectional file such as a serial
port or named pipe, the reading side also needs a copy of the same
handle! And yet, we can't replace the handle_write_eof call in the
client with a direct CloseHandle, because that won't wait until
buffered output has been drained.
The solution is that the client still calls handle_write_eof to
register that it _wants_ an EOF sent; the handle_output system will
wait until it's ready, but then, instead of calling CloseHandle, it
will ask its _client_ to close the handle, by calling the provided
'sentdata' callback with the new 'close' flag set to true. And then
the client can not only close the handle, but do whatever else it
needs to do to record that that has been done.
2021-09-30 18:16:20 +00:00
|
|
|
static void handle_sentdata(struct handle *h, size_t new_backlog, int err,
|
|
|
|
bool close)
|
2013-11-17 14:03:44 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
|
2017-02-22 21:57:04 +00:00
|
|
|
|
handle_write_eof: delegate CloseHandle back to the client.
When a writable HANDLE is managed by the handle-io.c system, you ask
to send EOF on the handle by calling handle_write_eof. That waits
until all buffered data has been written, and then sends an EOF event
by simply closing the handle.
That is, of course, the only way to send an EOF signal on a handle at
all. And yet, it's a bug, because the handle_output system does not
take ownership of the handle you give it: the client of handle_output
retains ownership, keeps its own copy of the handle, and will expect
to close it itself.
In most cases, the extra close will harmlessly fail, and return
ERROR_INVALID_HANDLE (which the caller didn't notice anyway). But if
you're unlucky, in conditions of frantic handle opening and closing
(e.g. with a lot of separate named-pipe-style agent forwarding
connections being constantly set up and torn down), the handle value
might have been reused between the two closes, so that the second
CloseHandle closes an unrelated handle belonging to some other part of
the program.
We can't fix this by giving handle_output permanent ownership of the
handle, because it really _is_ necessary for copies of it to survive
elsewhere: in particular, for a bidirectional file such as a serial
port or named pipe, the reading side also needs a copy of the same
handle! And yet, we can't replace the handle_write_eof call in the
client with a direct CloseHandle, because that won't wait until
buffered output has been drained.
The solution is that the client still calls handle_write_eof to
register that it _wants_ an EOF sent; the handle_output system will
wait until it's ready, but then, instead of calling CloseHandle, it
will ask its _client_ to close the handle, by calling the provided
'sentdata' callback with the new 'close' flag set to true. And then
the client can not only close the handle, but do whatever else it
needs to do to record that that has been done.
2021-09-30 18:16:20 +00:00
|
|
|
if (close) {
|
|
|
|
if (hs->send_H != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hs->send_H);
|
|
|
|
if (hs->recv_H != INVALID_HANDLE_VALUE && hs->recv_H != hs->send_H)
|
|
|
|
CloseHandle(hs->recv_H);
|
|
|
|
hs->send_H = hs->recv_H = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2019-02-06 20:36:11 +00:00
|
|
|
if (err) {
|
Convenience wrappers on plug_closing().
Having a single plug_closing() function covering various kinds of
closure is reasonably convenient from the point of view of Plug
implementations, but it's annoying for callers, who all have to fill
in pointless NULL and 0 parameters in the cases where they're not
used.
Added some inline helper functions in network.h alongside the main
plug_closing() dispatch wrappers, so that each kind of connection
closure can present a separate API for the Socket side of the
interface, without complicating the vtable for the Plug side.
Also, added OS-specific extra helpers in the Unix and Windows
directories, which centralise the job of taking an OS error code (of
whatever kind) and translating it into its error message.
In passing, this removes the horrible ad-hoc made-up error codes in
proxy.h, which is OK, because nothing checked for them anyway, and
also I'm about to do an API change to plug_closing proper that removes
the need for them.
2021-11-06 13:25:42 +00:00
|
|
|
plug_closing_system_error(hs->plug, err);
|
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)
|
2019-09-08 19:29:00 +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) {
|
2018-10-29 19:50:29 +00:00
|
|
|
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);
|
handle_write_eof: delegate CloseHandle back to the client.
When a writable HANDLE is managed by the handle-io.c system, you ask
to send EOF on the handle by calling handle_write_eof. That waits
until all buffered data has been written, and then sends an EOF event
by simply closing the handle.
That is, of course, the only way to send an EOF signal on a handle at
all. And yet, it's a bug, because the handle_output system does not
take ownership of the handle you give it: the client of handle_output
retains ownership, keeps its own copy of the handle, and will expect
to close it itself.
In most cases, the extra close will harmlessly fail, and return
ERROR_INVALID_HANDLE (which the caller didn't notice anyway). But if
you're unlucky, in conditions of frantic handle opening and closing
(e.g. with a lot of separate named-pipe-style agent forwarding
connections being constantly set up and torn down), the handle value
might have been reused between the two closes, so that the second
CloseHandle closes an unrelated handle belonging to some other part of
the program.
We can't fix this by giving handle_output permanent ownership of the
handle, because it really _is_ necessary for copies of it to survive
elsewhere: in particular, for a bidirectional file such as a serial
port or named pipe, the reading side also needs a copy of the same
handle! And yet, we can't replace the handle_write_eof call in the
client with a direct CloseHandle, because that won't wait until
buffered output has been drained.
The solution is that the client still calls handle_write_eof to
register that it _wants_ an EOF sent; the handle_output system will
wait until it's ready, but then, instead of calling CloseHandle, it
will ask its _client_ to close the handle, by calling the provided
'sentdata' callback with the new 'close' flag set to true. And then
the client can not only close the handle, but do whatever else it
needs to do to record that that has been done.
2021-09-30 18:16:20 +00:00
|
|
|
if (hs->send_H != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hs->send_H);
|
|
|
|
if (hs->recv_H != INVALID_HANDLE_VALUE && hs->recv_H != hs->send_H)
|
2018-05-27 08:29:33 +00:00
|
|
|
CloseHandle(hs->recv_H);
|
|
|
|
bufchain_clear(&hs->inputdata);
|
2013-11-17 14:03:44 +00:00
|
|
|
|
2021-09-13 13:34:46 +00:00
|
|
|
if (hs->addr)
|
|
|
|
sk_addr_free(hs->addr);
|
|
|
|
|
2019-04-20 07:20:34 +00:00
|
|
|
delete_callbacks_for_context(hs);
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
sfree(hs);
|
2013-11-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 20:42:44 +00:00
|
|
|
static size_t sk_handle_write(Socket *s, const void *data, size_t 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
|
|
|
}
|
|
|
|
|
2019-02-06 20:42:44 +00:00
|
|
|
static size_t sk_handle_write_oob(Socket *s, const void *data, size_t 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
*/
|
2019-02-06 20:46:45 +00:00
|
|
|
ptrlen data = bufchain_prefix(&hs->inputdata);
|
|
|
|
assert(data.len > 0);
|
2013-11-17 14:03:48 +00:00
|
|
|
|
|
|
|
/*
|
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-10-29 19:50:29 +00:00
|
|
|
hs->defer_close = true;
|
2019-02-06 20:46:45 +00:00
|
|
|
plug_receive(hs->plug, 0, data.ptr, data.len);
|
|
|
|
bufchain_consume(&hs->inputdata, data.len);
|
2018-10-29 19:50:29 +00:00
|
|
|
hs->defer_close = false;
|
2018-05-27 08:29:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
static void sk_handle_set_frozen(Socket *s, bool 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
|
2022-01-22 15:38:53 +00:00
|
|
|
* frozen again. In that case, handle-io.c is already
|
2013-11-17 14:03:48 +00:00
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2024-06-26 05:47:53 +00:00
|
|
|
static SocketEndpointInfo *sk_handle_endpoint_info(Socket *s, bool peer)
|
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));
|
|
|
|
|
2024-06-26 05:47:53 +00:00
|
|
|
if (!peer)
|
|
|
|
return NULL;
|
|
|
|
|
2015-05-18 15:00:13 +00:00
|
|
|
if (!kernel32_module) {
|
|
|
|
kernel32_module = load_system32_dll("kernel32.dll");
|
Replace mkfiles.pl with a CMake build system.
This brings various concrete advantages over the previous system:
- consistent support for out-of-tree builds on all platforms
- more thorough support for Visual Studio IDE project files
- support for Ninja-based builds, which is particularly useful on
Windows where the alternative nmake has no parallel option
- a really simple set of build instructions that work the same way on
all the major platforms (look how much shorter README is!)
- better decoupling of the project configuration from the toolchain
configuration, so that my Windows cross-building doesn't need
(much) special treatment in CMakeLists.txt
- configure-time tests on Windows as well as Linux, so that a lot of
ad-hoc #ifdefs second-guessing a particular feature's presence from
the compiler version can now be replaced by tests of the feature
itself
Also some longer-term software-engineering advantages:
- other people have actually heard of CMake, so they'll be able to
produce patches to the new build setup more easily
- unlike the old mkfiles.pl, CMake is not my personal problem to
maintain
- most importantly, mkfiles.pl was just a horrible pile of
unmaintainable cruft, which even I found it painful to make changes
to or to use, and desperately needed throwing in the bin. I've
already thrown away all the variants of it I had in other projects
of mine, and was only delaying this one so we could make the 0.75
release branch first.
This change comes with a noticeable build-level restructuring. The
previous Recipe worked by compiling every object file exactly once,
and then making each executable by linking a precisely specified
subset of the same object files. But in CMake, that's not the natural
way to work - if you write the obvious command that puts the same
source file into two executable targets, CMake generates a makefile
that compiles it once per target. That can be an advantage, because it
gives you the freedom to compile it differently in each case (e.g.
with a #define telling it which program it's part of). But in a
project that has many executable targets and had carefully contrived
to _never_ need to build any module more than once, all it does is
bloat the build time pointlessly!
To avoid slowing down the build by a large factor, I've put most of
the modules of the code base into a collection of static libraries
organised vaguely thematically (SSH, other backends, crypto, network,
...). That means all those modules can still be compiled just once
each, because once each library is built it's reused unchanged for all
the executable targets.
One upside of this library-based structure is that now I don't have to
manually specify exactly which objects go into which programs any more
- it's enough to specify which libraries are needed, and the linker
will figure out the fine detail automatically. So there's less
maintenance to do in CMakeLists.txt when the source code changes.
But that reorganisation also adds fragility, because of the trad Unix
linker semantics of walking along the library list once each, so that
cyclic references between your libraries will provoke link errors. The
current setup builds successfully, but I suspect it only just manages
it.
(In particular, I've found that MinGW is the most finicky on this
score of the Windows compilers I've tried building with. So I've
included a MinGW test build in the new-look Buildscr, because
otherwise I think there'd be a significant risk of introducing
MinGW-only build failures due to library search order, which wasn't a
risk in the previous library-free build organisation.)
In the longer term I hope to be able to reduce the risk of that, via
gradual reorganisation (in particular, breaking up too-monolithic
modules, to reduce the risk of knock-on references when you included a
module for function A and it also contains function B with an
unsatisfied dependency you didn't really need). Ideally I want to
reach a state in which the libraries all have sensibly described
purposes, a clearly documented (partial) order in which they're
permitted to depend on each other, and a specification of what stubs
you have to put where if you're leaving one of them out (e.g.
nocrypto) and what callbacks you have to define in your non-library
objects to satisfy dependencies from things low in the stack (e.g.
out_of_memory()).
One thing that's gone completely missing in this migration,
unfortunately, is the unfinished MacOS port linked against Quartz GTK.
That's because it turned out that I can't currently build it myself,
on my own Mac: my previous installation of GTK had bit-rotted as a
side effect of an Xcode upgrade, and I haven't yet been able to
persuade jhbuild to make me a new one. So I can't even build the MacOS
port with the _old_ makefiles, and hence, I have no way of checking
that the new ones also work. I hope to bring that port back to life at
some point, but I don't want it to block the rest of this change.
2021-04-10 14:21:11 +00:00
|
|
|
#if !HAVE_GETNAMEDPIPECLIENTPROCESSID
|
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-10-18 19:06:42 +00:00
|
|
|
p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
|
2024-06-26 05:35:40 +00:00
|
|
|
SocketEndpointInfo *pi = snew(SocketEndpointInfo);
|
2018-10-18 19:06:42 +00:00
|
|
|
pi->addressfamily = ADDRTYPE_LOCAL;
|
|
|
|
pi->addr_text = NULL;
|
|
|
|
pi->port = -1;
|
|
|
|
pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
|
|
|
|
return pi;
|
|
|
|
}
|
2015-05-18 15:00:13 +00:00
|
|
|
|
2015-05-18 12:57:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-05 06:03:46 +00:00
|
|
|
static const SocketVtable HandleSocket_sockvt = {
|
Change vtable defs to use C99 designated initialisers.
This is a sweeping change applied across the whole code base by a spot
of Emacs Lisp. Now, everywhere I declare a vtable filled with function
pointers (and the occasional const data member), all the members of
the vtable structure are initialised by name using the '.fieldname =
value' syntax introduced in C99.
We were already using this syntax for a handful of things in the new
key-generation progress report system, so it's not new to the code
base as a whole.
The advantage is that now, when a vtable only declares a subset of the
available fields, I can initialise the rest to NULL or zero just by
leaving them out. This is most dramatic in a couple of the outlying
vtables in things like psocks (which has a ConnectionLayerVtable
containing only one non-NULL method), but less dramatically, it means
that the new 'flags' field in BackendVtable can be completely left out
of every backend definition except for the SUPDUP one which defines it
to a nonzero value. Similarly, the test_for_upstream method only used
by SSH doesn't have to be mentioned in the rest of the backends;
network Plugs for listening sockets don't have to explicitly null out
'receive' and 'sent', and vice versa for 'accepting', and so on.
While I'm at it, I've normalised the declarations so they don't use
the unnecessarily verbose 'struct' keyword. Also a handful of them
weren't const; now they are.
2020-03-10 21:06:29 +00:00
|
|
|
.plug = sk_handle_plug,
|
|
|
|
.close = sk_handle_close,
|
|
|
|
.write = sk_handle_write,
|
|
|
|
.write_oob = sk_handle_write_oob,
|
|
|
|
.write_eof = sk_handle_write_eof,
|
|
|
|
.set_frozen = sk_handle_set_frozen,
|
|
|
|
.socket_error = sk_handle_socket_error,
|
2024-06-26 05:47:53 +00:00
|
|
|
.endpoint_info = sk_handle_endpoint_info,
|
2018-05-27 08:29:33 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 13:28:47 +00:00
|
|
|
static void sk_handle_connect_success_callback(void *ctx)
|
|
|
|
{
|
|
|
|
HandleSocket *hs = (HandleSocket *)ctx;
|
2024-06-26 07:29:39 +00:00
|
|
|
plug_log(hs->plug, &hs->sock, PLUGLOG_CONNECT_SUCCESS, hs->addr, hs->port,
|
|
|
|
NULL, 0);
|
2021-09-13 13:28:47 +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
|
|
|
Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
|
2021-09-13 13:34:46 +00:00
|
|
|
SockAddr *addr, int port, Plug *plug,
|
|
|
|
bool 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;
|
2021-09-13 13:34:46 +00:00
|
|
|
hs->addr = addr;
|
|
|
|
hs->port = port;
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->plug = plug;
|
|
|
|
hs->error = NULL;
|
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.
2021-12-22 09:31:06 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
hs->frozen = UNFROZEN;
|
|
|
|
bufchain_init(&hs->inputdata);
|
2019-03-01 19:18:31 +00:00
|
|
|
psb_init(&hs->psb);
|
2018-05-27 08:29:33 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
hs->defer_close = hs->deferred_close = false;
|
2018-05-27 08:29:33 +00:00
|
|
|
|
2021-09-13 13:28:47 +00:00
|
|
|
queue_toplevel_callback(sk_handle_connect_success_callback, hs);
|
|
|
|
|
2018-10-05 06:24:16 +00:00
|
|
|
return &hs->sock;
|
2013-11-17 14:03:44 +00: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.
2021-12-22 09:31:06 +00:00
|
|
|
|
2022-08-22 17:46:32 +00:00
|
|
|
void handle_socket_set_psb_prefix(Socket *s, const char *prefix)
|
|
|
|
{
|
|
|
|
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
|
|
|
assert(hs->sock.vt == &HandleSocket_sockvt);
|
|
|
|
psb_set_prefix(&hs->psb, prefix);
|
|
|
|
}
|
|
|
|
|
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.
2021-12-22 09:31:06 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-26 05:47:53 +00:00
|
|
|
static SocketEndpointInfo *sk_handle_deferred_endpoint_info(
|
|
|
|
Socket *s, bool peer)
|
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.
2021-12-22 09:31:06 +00:00
|
|
|
{
|
|
|
|
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,
|
2024-06-26 05:47:53 +00:00
|
|
|
.endpoint_info = sk_handle_deferred_endpoint_info,
|
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.
2021-12-22 09:31:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|