From 08b43c0ccaa7a80515a0829a09a247aa5b3c0634 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 13 Sep 2018 09:09:10 +0100 Subject: [PATCH] Expose structure tags for the connection-sharing data types. This was a particularly confusing piece of type-danger, because three different types were passed outside sshshare.c as 'void *' and only human vigilance prevented one coming back as the wrong one. Now they all keep their opaque structure tags when they move through other parts of the code. --- defs.h | 4 ++++ ssh.c | 27 +++++++++++++++------------ ssh.h | 34 +++++++++++++++++++--------------- sshshare.c | 32 +++++++++++++------------------- unix/uxpgnt.c | 3 ++- x11fwd.c | 3 ++- 6 files changed, 55 insertions(+), 48 deletions(-) diff --git a/defs.h b/defs.h index 2c597953..329883d9 100644 --- a/defs.h +++ b/defs.h @@ -55,6 +55,10 @@ typedef struct ssh_tag *Ssh; typedef struct Channel Channel; +typedef struct ssh_sharing_state ssh_sharing_state; +typedef struct ssh_sharing_connstate ssh_sharing_connstate; +typedef struct share_channel share_channel; + /* Note indirection: for historical reasons (it used to be closer to * the OS socket type), the type that most code uses for a socket is * 'Socket', not 'Socket *'. So an implementation of Socket or Plug diff --git a/ssh.c b/ssh.c index bc2f7bf8..dd1d1c1f 100644 --- a/ssh.c +++ b/ssh.c @@ -460,7 +460,8 @@ struct ssh_channel { } v2; } v; - void *sharectx; /* sharing context, if this is a downstream channel */ + ssh_sharing_connstate *sharectx; /* sharing context, if this is a + * downstream channel */ Channel *chan; /* handle the client side of this channel, if not */ }; @@ -497,7 +498,7 @@ struct ssh_rportfwd { unsigned sport, dport; char *shost, *dhost; char *sportdesc; - void *share_ctx; + ssh_sharing_connstate *share_ctx; struct ssh_portfwd *pfrec; }; @@ -695,7 +696,7 @@ struct ssh_tag { int bare_connection; int attempting_connshare; - void *connshare; + ssh_sharing_state *connshare; char *savedhost; int savedport; @@ -3800,7 +3801,7 @@ static void ssh_rportfwd_succfail(Ssh ssh, PktIn *pktin, void *ctx) } int ssh_alloc_sharing_rportfwd(Ssh ssh, const char *shost, int sport, - void *share_ctx) + ssh_sharing_connstate *share_ctx) { struct ssh_rportfwd *pf = snew(struct ssh_rportfwd); pf->dhost = NULL; @@ -3822,7 +3823,7 @@ int ssh_alloc_sharing_rportfwd(Ssh ssh, const char *shost, int sport, } void ssh_remove_sharing_rportfwd(Ssh ssh, const char *shost, int sport, - void *share_ctx) + ssh_sharing_connstate *share_ctx) { struct ssh_rportfwd pf, *realpf; @@ -3844,7 +3845,8 @@ static void ssh_sharing_global_request_response(Ssh ssh, PktIn *pktin, BinarySource_UPCAST(pktin)->len); } -void ssh_sharing_queue_global_request(Ssh ssh, void *share_ctx) +void ssh_sharing_queue_global_request(Ssh ssh, + ssh_sharing_connstate *share_ctx) { ssh_queue_handler(ssh, SSH2_MSG_REQUEST_SUCCESS, SSH2_MSG_REQUEST_FAILURE, ssh_sharing_global_request_response, share_ctx); @@ -7735,9 +7737,9 @@ static void ssh2_msg_global_request(Ssh ssh, PktIn *pktin) } } -struct X11FakeAuth *ssh_sharing_add_x11_display(Ssh ssh, int authtype, - void *share_cs, - void *share_chan) +struct X11FakeAuth *ssh_sharing_add_x11_display( + Ssh ssh, int authtype, ssh_sharing_connstate *share_cs, + share_channel *share_chan) { struct X11FakeAuth *auth; @@ -7876,7 +7878,8 @@ static void ssh2_msg_channel_open(Ssh ssh, PktIn *pktin) } void sshfwd_x11_sharing_handover(struct ssh_channel *c, - void *share_cs, void *share_chan, + ssh_sharing_connstate *share_cs, + share_channel *share_chan, const char *peer_addr, int peer_port, int endian, int protomajor, int protominor, const void *initial_data, int initial_len) @@ -11173,7 +11176,7 @@ static void ssh_special(Backend *be, Telnet_Special code) } } -unsigned ssh_alloc_sharing_channel(Ssh ssh, void *sharing_ctx) +unsigned ssh_alloc_sharing_channel(Ssh ssh, ssh_sharing_connstate *connstate) { struct ssh_channel *c; c = snew(struct ssh_channel); @@ -11181,7 +11184,7 @@ unsigned ssh_alloc_sharing_channel(Ssh ssh, void *sharing_ctx) c->ssh = ssh; ssh_channel_init(c); c->chan = NULL; - c->sharectx = sharing_ctx; + c->sharectx = connstate; return c->localid; } diff --git a/ssh.h b/ssh.h index 82810d78..4fee36bf 100644 --- a/ssh.h +++ b/ssh.h @@ -16,7 +16,8 @@ extern void sshfwd_unthrottle(struct ssh_channel *c, int bufsize); Conf *sshfwd_get_conf(struct ssh_channel *c); void sshfwd_window_override_removed(struct ssh_channel *c); void sshfwd_x11_sharing_handover(struct ssh_channel *c, - void *share_cs, void *share_chan, + ssh_sharing_connstate *share_cs, + share_channel *share_chan, const char *peer_addr, int peer_port, int endian, int protomajor, int protominor, const void *initial_data, int initial_len); @@ -148,26 +149,28 @@ void ssh_free_pktout(PktOut *pkt); extern Socket ssh_connection_sharing_init( const char *host, int port, Conf *conf, Ssh ssh, Plug sshplug, - void **state); + ssh_sharing_state **state); int ssh_share_test_for_upstream(const char *host, int port, Conf *conf); -void share_got_pkt_from_server(void *ctx, int type, +void share_got_pkt_from_server(ssh_sharing_connstate *ctx, int type, const void *pkt, int pktlen); -void share_activate(void *state, const char *server_verstring); -void sharestate_free(void *state); -int share_ndownstreams(void *state); +void share_activate(ssh_sharing_state *sharestate, + const char *server_verstring); +void sharestate_free(ssh_sharing_state *state); +int share_ndownstreams(ssh_sharing_state *state); void ssh_connshare_log(Ssh ssh, int event, const char *logtext, const char *ds_err, const char *us_err); -unsigned ssh_alloc_sharing_channel(Ssh ssh, void *sharing_ctx); +unsigned ssh_alloc_sharing_channel(Ssh ssh, ssh_sharing_connstate *connstate); void ssh_delete_sharing_channel(Ssh ssh, unsigned localid); int ssh_alloc_sharing_rportfwd(Ssh ssh, const char *shost, int sport, - void *share_ctx); + ssh_sharing_connstate *connstate); void ssh_remove_sharing_rportfwd(Ssh ssh, const char *shost, int sport, - void *share_ctx); -void ssh_sharing_queue_global_request(Ssh ssh, void *share_ctx); -struct X11FakeAuth *ssh_sharing_add_x11_display(Ssh ssh, int authtype, - void *share_cs, - void *share_chan); + ssh_sharing_connstate *connstate); +void ssh_sharing_queue_global_request( + Ssh ssh, ssh_sharing_connstate *connstate); +struct X11FakeAuth *ssh_sharing_add_x11_display( + Ssh ssh, int authtype, ssh_sharing_connstate *share_cs, + share_channel *share_chan); void ssh_sharing_remove_x11_display(Ssh ssh, struct X11FakeAuth *auth); void ssh_send_packet_from_downstream(Ssh ssh, unsigned id, int type, const void *pkt, int pktlen, @@ -177,7 +180,7 @@ void ssh_sharing_downstream_connected(Ssh ssh, unsigned id, void ssh_sharing_downstream_disconnected(Ssh ssh, unsigned id); void ssh_sharing_logf(Ssh ssh, unsigned id, const char *logfmt, ...); int ssh_agent_forwarding_permitted(Ssh ssh); -void share_setup_x11_channel(void *csv, void *chanv, +void share_setup_x11_channel(ssh_sharing_connstate *cs, share_channel *chan, unsigned upstream_id, unsigned server_id, unsigned server_currwin, unsigned server_maxpkt, unsigned client_adjusted_window, @@ -727,7 +730,8 @@ struct X11FakeAuth { * What to do with an X connection matching this auth data. */ struct X11Display *disp; - void *share_cs, *share_chan; + ssh_sharing_connstate *share_cs; + share_channel *share_chan; }; void *x11_make_greeting(int endian, int protomajor, int protominor, int auth_proto, const void *auth_data, int auth_len, diff --git a/sshshare.c b/sshshare.c index 30fe7360..22611e7d 100644 --- a/sshshare.c +++ b/sshshare.c @@ -507,9 +507,8 @@ static void share_connstate_free(struct ssh_sharing_connstate *cs) sfree(cs); } -void sharestate_free(void *v) +void sharestate_free(ssh_sharing_state *sharestate) { - struct ssh_sharing_state *sharestate = (struct ssh_sharing_state *)v; struct ssh_sharing_connstate *cs; platform_ssh_share_cleanup(sharestate->sockname); @@ -1061,7 +1060,7 @@ void share_xchannel_failure(struct ssh_sharing_connstate *cs, share_dead_xchannel_respond(cs, xc); } -void share_setup_x11_channel(void *csv, void *chanv, +void share_setup_x11_channel(ssh_sharing_connstate *cs, share_channel *chan, unsigned upstream_id, unsigned server_id, unsigned server_currwin, unsigned server_maxpkt, unsigned client_adjusted_window, @@ -1069,8 +1068,6 @@ void share_setup_x11_channel(void *csv, void *chanv, int protomajor, int protominor, const void *initial_data, int initial_len) { - struct ssh_sharing_connstate *cs = (struct ssh_sharing_connstate *)csv; - struct share_channel *chan = (struct share_channel *)chanv; struct share_xchannel *xc; void *greeting; int greeting_len; @@ -1128,11 +1125,10 @@ void share_setup_x11_channel(void *csv, void *chanv, } } -void share_got_pkt_from_server(void *csv, int type, +void share_got_pkt_from_server(ssh_sharing_connstate *cs, int type, const void *vpkt, int pktlen) { const unsigned char *pkt = (const unsigned char *)vpkt; - struct ssh_sharing_connstate *cs = (struct ssh_sharing_connstate *)csv; struct share_globreq *globreq; size_t id_pos; unsigned upstream_id, server_id; @@ -1718,8 +1714,8 @@ static void share_got_pkt_from_downstream(struct ssh_sharing_connstate *cs, static void share_receive(Plug plug, int urgent, char *data, int len) { - struct ssh_sharing_connstate *cs = FROMFIELD( - plug, struct ssh_sharing_connstate, plugvt); + ssh_sharing_connstate *cs = FROMFIELD( + plug, ssh_sharing_connstate, plugvt); static const char expected_verstring_prefix[] = "SSHCONNECTION@putty.projects.tartarus.org-2.0-"; unsigned char c; @@ -1795,8 +1791,8 @@ static void share_receive(Plug plug, int urgent, char *data, int len) static void share_sent(Plug plug, int bufsize) { - /* struct ssh_sharing_connstate *cs = FROMFIELD( - plug, struct ssh_sharing_connstate, plugvt); */ + /* ssh_sharing_connstate *cs = FROMFIELD( + plug, ssh_sharing_connstate, plugvt); */ /* * We do nothing here, because we expect that there won't be a @@ -1811,8 +1807,7 @@ static void share_sent(Plug plug, int bufsize) static void share_listen_closing(Plug plug, const char *error_msg, int error_code, int calling_back) { - struct ssh_sharing_state *sharestate = FROMFIELD( - plug, struct ssh_sharing_state, plugvt); + ssh_sharing_state *sharestate = FROMFIELD(plug, ssh_sharing_state, plugvt); if (error_msg) ssh_sharing_logf(sharestate->ssh, 0, "listening socket: %s", error_msg); @@ -1820,7 +1815,7 @@ static void share_listen_closing(Plug plug, const char *error_msg, sharestate->listensock = NULL; } -static void share_send_verstring(struct ssh_sharing_connstate *cs) +static void share_send_verstring(ssh_sharing_connstate *cs) { char *fullstring = dupcat("SSHCONNECTION@putty.projects.tartarus.org-2.0-", cs->parent->server_verstring, "\015\012", NULL); @@ -1830,19 +1825,18 @@ static void share_send_verstring(struct ssh_sharing_connstate *cs) cs->sent_verstring = TRUE; } -int share_ndownstreams(void *state) +int share_ndownstreams(ssh_sharing_state *sharestate) { - struct ssh_sharing_state *sharestate = (struct ssh_sharing_state *)state; return count234(sharestate->connections); } -void share_activate(void *state, const char *server_verstring) +void share_activate(ssh_sharing_state *sharestate, + const char *server_verstring) { /* * Indication from ssh.c that we are now ready to begin serving * any downstreams that have already connected to us. */ - struct ssh_sharing_state *sharestate = (struct ssh_sharing_state *)state; struct ssh_sharing_connstate *cs; int i; @@ -2028,7 +2022,7 @@ static const Plug_vtable ssh_sharing_listen_plugvt = { */ Socket ssh_connection_sharing_init(const char *host, int port, Conf *conf, Ssh ssh, Plug sshplug, - void **state) + ssh_sharing_state **state) { int result, can_upstream, can_downstream; char *logtext, *ds_err, *us_err; diff --git a/unix/uxpgnt.c b/unix/uxpgnt.c index a694bc44..efd93726 100644 --- a/unix/uxpgnt.c +++ b/unix/uxpgnt.c @@ -168,7 +168,8 @@ int chan_no_eager_close(Channel *chan, int s, int r) { return FALSE; } Conf *sshfwd_get_conf(struct ssh_channel *c) { return NULL; } void sshfwd_x11_sharing_handover(struct ssh_channel *c, - void *share_cs, void *share_chan, + ssh_sharing_connstate *share_cs, + share_channel *share_chan, const char *peer_addr, int peer_port, int endian, int protomajor, int protominor, const void *initial_data, int initial_len) {} diff --git a/x11fwd.c b/x11fwd.c index 419d4968..f3ea5397 100644 --- a/x11fwd.c +++ b/x11fwd.c @@ -128,7 +128,8 @@ struct X11FakeAuth *x11_invent_fake_auth(tree234 *authtree, int authtype) auth->data[i]); auth->disp = NULL; - auth->share_cs = auth->share_chan = NULL; + auth->share_cs = NULL; + auth->share_chan = NULL; return auth; }