1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 11:02:48 -05:00

Replace the hacky 'OSSocket' type with a closure.

The mechanism for constructing a new connection-type Socket when a
listening one receives an incoming connection previously worked by
passing a platform-specific 'OSSocket' type to the plug_accepting
function, which would then call sk_register to wrap it with a proper
Socket instance. This is less flexible than ideal, because it presumes
that only one kind of OS object might ever need to be turned into a
Socket. So I've replaced OSSocket throughout the code base with a pair
of parameters consisting of a function pointer and a context such that
passing the latter to the former returns the appropriate Socket; this
will permit different classes of listening Socket to pass different
function pointers.

In deference to the reality that OSSockets tend to be small integers
or pointer-sized OS handles, I've made the context parameter an
int/pointer union that can hold either of those directly, rather than
the usual approach of making it a plain 'void *' and requiring a
context structure to be dynamically allocated every time.

[originally from svn r10068]
This commit is contained in:
Simon Tatham
2013-11-17 14:03:55 +00:00
parent 9093071faa
commit 19fba3fe55
8 changed files with 39 additions and 28 deletions

View File

@ -24,9 +24,6 @@ struct FontSpec *fontspec_new(const char *name);
typedef void *Context; /* FIXME: probably needs changing */
typedef int OSSocket;
#define OSSOCKET_DEFINED /* stop network.h using its default */
extern Backend pty_backend;
typedef uint32_t uint32; /* C99: uint32_t defined in stdint.h */

View File

@ -490,8 +490,9 @@ static struct socket_function_table tcp_fn_table = {
sk_tcp_socket_error
};
Socket sk_register(OSSocket sockfd, Plug plug)
static Socket sk_tcp_accept(accept_ctx_t ctx, Plug plug)
{
int sockfd = ctx.i;
Actual_Socket ret;
/*
@ -1268,6 +1269,7 @@ static int net_select_result(int fd, int event)
*/
union sockaddr_union su;
socklen_t addrlen = sizeof(su);
accept_ctx_t actx;
int t; /* socket of connection */
memset(&su, 0, addrlen);
@ -1277,11 +1279,12 @@ static int net_select_result(int fd, int event)
}
nonblock(t);
actx.i = t;
if (s->localhost_only &&
!sockaddr_is_loopback(&su.sa)) {
close(t); /* someone let nonlocal through?! */
} else if (plug_accepting(s->plug, t)) {
} else if (plug_accepting(s->plug, sk_tcp_accept, actx)) {
close(t); /* denied or error */
}
break;