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

Pageant: new asynchronous internal APIs.

This is a pure refactoring: no functional change expected.

This commit introduces two new small vtable-style APIs. One is
PageantClient, which identifies a particular client of the Pageant
'core' (meaning the code that handles each individual request). This
changes pageant_handle_msg into an asynchronous operation: you pass in
an agent request message and an identifier, and at some later point,
the got_response method in your PageantClient will be called with the
answer (and the same identifier, to allow you to match requests to
responses). The trait vtable also contains a logging system.

The main importance of PageantClient, and the reason why it has to
exist instead of just passing pageant_handle_msg a bare callback
function pointer and context parameter, is that it provides robustness
if a client stops existing while a request is still pending. You call
pageant_unregister_client, and any unfinished requests associated with
that client in the Pageant core will be cleaned up, so that you're
guaranteed that after the unregister operation, no stray callbacks
will happen with a stale pointer to that client.

The WM_COPYDATA interface of Windows Pageant is a direct client of
this API. The other client is PageantListener, the system that lives
in pageant.c and handles stream-based agent connections for both Unix
Pageant and the new Windows named-pipe IPC. More specifically, each
individual connection to the listening socket is a separate
PageantClient, which means that if a socket is closed abruptly or
suffers an OS error, that client can be unregistered and any pending
requests cancelled without disrupting other connections.

Users of PageantListener have a second client vtable they can use,
called PageantListenerClient. That contains _only_ logging facilities,
and at the moment, only Unix Pageant bothers to use it (and even that
only in debugging mode).

Finally, internally to the Pageant core, there's a new trait called
PageantAsyncOp which describes an agent request in the process of
being handled. But at the moment, it has only one trivial
implementation, which is handed the full response message already
constructed, and on the next toplevel callback, passes it back to the
PageantClient.
This commit is contained in:
Simon Tatham
2020-01-25 17:24:28 +00:00
parent 49cd1f7116
commit de38a4d826
5 changed files with 487 additions and 214 deletions

View File

@ -769,26 +769,35 @@ struct WmCopydataTransaction {
HANDLE ev_msg_ready, ev_reply_ready;
} wmct;
static struct PageantClient wmcpc;
static void wm_copydata_got_msg(void *vctx)
{
strbuf *sb = strbuf_new();
pageant_handle_msg(BinarySink_UPCAST(sb), wmct.body, wmct.bodylen,
NULL, NULL);
pageant_handle_msg(&wmcpc, NULL, make_ptrlen(wmct.body, wmct.bodylen));
}
if (sb->len > wmct.bodysize) {
static void wm_copydata_got_response(PageantClient *pc, void *reqid,
ptrlen response)
{
if (response.len > wmct.bodysize) {
/* Output would overflow message buffer. Replace with a
* failure message. */
sb->len = 0;
put_byte(sb, SSH_AGENT_FAILURE);
assert(sb->len <= wmct.bodysize);
static const unsigned char failure[] = { SSH_AGENT_FAILURE };
response = make_ptrlen(failure, lenof(failure));
assert(response.len <= wmct.bodysize);
}
PUT_32BIT_MSB_FIRST(wmct.length, sb->len);
memcpy(wmct.body, sb->u, sb->len);
PUT_32BIT_MSB_FIRST(wmct.length, response.len);
memcpy(wmct.body, response.ptr, response.len);
SetEvent(wmct.ev_reply_ready);
}
static const PageantClientVtable wmcpc_vtable = {
NULL, /* no logging in this client */
wm_copydata_got_response,
};
static char *answer_filemapping_message(const char *mapname)
{
HANDLE maphandle = INVALID_HANDLE_VALUE;
@ -1176,6 +1185,15 @@ void cleanup_exit(int code)
int flags = FLAG_SYNCAGENT;
struct winpgnt_client {
PageantListenerClient plc;
};
const PageantListenerClientVtable winpgnt_vtable = {
NULL, /* no logging */
};
static struct winpgnt_client wpc[1];
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
{
MSG msg;
@ -1328,7 +1346,10 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
*/
{
Plug *pl_plug;
struct pageant_listen_state *pl = pageant_listener_new(&pl_plug);
wpc->plc.vt = &winpgnt_vtable;
wpc->plc.suppress_logging = true;
struct pageant_listen_state *pl =
pageant_listener_new(&pl_plug, &wpc->plc);
char *pipename = agent_named_pipe_name();
Socket *sock = new_named_pipe_listener(pipename, pl_plug);
if (sk_socket_error(sock)) {
@ -1406,6 +1427,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
ShowWindow(hwnd, SW_HIDE);
wmcpc.vt = &wmcpc_vtable;
wmcpc.suppress_logging = true;
pageant_register_client(&wmcpc);
DWORD wm_copydata_threadid;
wmct.ev_msg_ready = CreateEvent(NULL, false, false, NULL);
wmct.ev_reply_ready = CreateEvent(NULL, false, false, NULL);