1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
/*
* General mechanism for wrapping up reading/writing of Windows
* HANDLEs into a PuTTY Socket abstraction.
*/
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include "tree234.h"
#include "putty.h"
#include "network.h"
/*
* Freezing one of these sockets is a slightly fiddly business,
* because the reads from the handle are happening in a separate
* thread as blocking system calls and so once one is in progress it
* can't sensibly be interrupted. Hence, after the user tries to
* freeze one of these sockets, it's unavoidable that we may receive
* one more load of data before we manage to get handle-io.c to stop
* reading.
*/
typedef enum HandleSocketFreezeState {
UNFROZEN, /* reading as normal */
FREEZING, /* have been set to frozen but winhandl is still reading */
FROZEN, /* really frozen - winhandl has been throttled */
THAWING /* we're gradually releasing our remaining data */
} HandleSocketFreezeState;
typedef struct HandleSocket {
union {
struct {
HANDLE send_H, recv_H, stderr_H;
struct handle *send_h, *recv_h, *stderr_h;
HandleSocketFreezeState frozen;
/* We buffer data here if we receive it from winhandl
* while frozen. */
bufchain inputdata;
/* Handle logging proxy error messages from stderr_H, if
* we have one */
ProxyStderrBuf psb;
bool defer_close, deferred_close; /* in case of re-entrance */
};
struct {
DeferredSocketOpener *opener;
/* We buffer data here if we receive it via sk_write
* before the socket is opened. */
bufchain outputdata;
bool output_eof_pending;
bool start_frozen;
};
};
char *error;
SockAddr *addr;
int port;
Plug *plug;
Socket sock;
} HandleSocket;
static size_t handle_gotdata(
struct handle *h, const void *data, size_t len, int err)
{
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
if (err) {
plug_closing_error(hs->plug, "Read error from handle");
return 0;
} else if (len == 0) {
plug_closing_normal(hs->plug);
return 0;
} else {
assert(hs->frozen != FROZEN && hs->frozen != THAWING);
if (hs->frozen == FREEZING) {
/*
* If we've received data while this socket is supposed to
* be frozen (because the read handle-io.c started before
* sk_set_frozen was called has now returned) then buffer
* the data for when we unfreeze.
*/
bufchain_add(&hs->inputdata, data, len);
hs->frozen = FROZEN;
/*
* And return a very large backlog, to prevent further
* data arriving from winhandl until we unfreeze.
*/
return INT_MAX;
} else {
plug_receive(hs->plug, 0, data, len);
return 0;
}
}
}
static size_t handle_stderr(
struct handle *h, const void *data, size_t len, int err)
{
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
if (!err && len > 0)
log_proxy_stderr(hs->plug, &hs->sock, &hs->psb, data, len);
return 0;
}
static void handle_sentdata(struct handle *h, size_t new_backlog, int err,
bool close)
{
HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
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;
}
if (err) {
plug_closing_system_error(hs->plug, err);
return;
}
plug_sent(hs->plug, new_backlog);
}
static Plug *sk_handle_plug(Socket *s, Plug *p)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
Plug *ret = hs->plug;
if (p)
hs->plug = p;
return ret;
}
static void sk_handle_close(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
if (hs->defer_close) {
hs->deferred_close = true;
return;
}
handle_free(hs->send_h);
handle_free(hs->recv_h);
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);
bufchain_clear(&hs->inputdata);
if (hs->addr)
sk_addr_free(hs->addr);
delete_callbacks_for_context(hs);
sfree(hs);
}
static size_t sk_handle_write(Socket *s, const void *data, size_t len)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
return handle_write(hs->send_h, data, len);
}
static size_t sk_handle_write_oob(Socket *s, const void *data, size_t len)
{
/*
* oob data is treated as inband; nasty, but nothing really
* better we can do
*/
return sk_handle_write(s, data, len);
}
static void sk_handle_write_eof(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
handle_write_eof(hs->send_h);
}
static void handle_socket_unfreeze(void *hsv)
{
HandleSocket *hs = (HandleSocket *)hsv;
/*
* If we've been put into a state other than THAWING since the
* last callback, then we're done.
*/
if (hs->frozen != THAWING)
return;
/*
* Get some of the data we've buffered.
*/
ptrlen data = bufchain_prefix(&hs->inputdata);
assert(data.len > 0);
/*
* Hand it off to the plug. Be careful of re-entrance - that might
* have the effect of trying to close this socket.
*/
hs->defer_close = true;
plug_receive(hs->plug, 0, data.ptr, data.len);
bufchain_consume(&hs->inputdata, data.len);
hs->defer_close = false;
if (hs->deferred_close) {
sk_handle_close(&hs->sock);
return;
}
if (bufchain_size(&hs->inputdata) > 0) {
/*
* If there's still data in our buffer, stay in THAWING state,
* and reschedule ourself.
*/
queue_toplevel_callback(handle_socket_unfreeze, hs);
} else {
/*
* Otherwise, we've successfully thawed!
*/
hs->frozen = UNFROZEN;
handle_unthrottle(hs->recv_h, 0);
}
}
static void sk_handle_set_frozen(Socket *s, bool is_frozen)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
if (is_frozen) {
switch (hs->frozen) {
case FREEZING:
case FROZEN:
return; /* nothing to do */
case THAWING:
/*
* We were in the middle of emptying our bufchain, and got
* frozen again. In that case, handle-io.c is already
* throttled, so just return to FROZEN state. The toplevel
* callback will notice and disable itself.
*/
hs->frozen = FROZEN;
break;
case UNFROZEN:
/*
* The normal case. Go to FREEZING, and expect one more
* load of data from winhandl if we're unlucky.
*/
hs->frozen = FREEZING;
break;
}
} else {
switch (hs->frozen) {
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.
*/
assert(bufchain_size(&hs->inputdata) == 0);
hs->frozen = UNFROZEN;
break;
case FROZEN:
/*
* If we have buffered data, go to THAWING and start
* releasing it in top-level callbacks.
*/
hs->frozen = THAWING;
queue_toplevel_callback(handle_socket_unfreeze, hs);
}
}
}
static const char *sk_handle_socket_error(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
return hs->error;
}
static SocketEndpointInfo *sk_handle_endpoint_info(Socket *s, bool peer)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
ULONG pid;
static HMODULE kernel32_module;
DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
(HANDLE, PULONG));
if (!peer)
return NULL;
if (!kernel32_module) {
kernel32_module = load_system32_dll("kernel32.dll");
#if !HAVE_GETNAMEDPIPECLIENTPROCESSID
/* For older Visual Studio, and MinGW too (at least as of
* Ubuntu 16.04), this function isn't available in the header
* files to type-check. Ditto the toolchain I use for
* Coveritying the Windows code. */
GET_WINDOWS_FUNCTION_NO_TYPECHECK(
kernel32_module, GetNamedPipeClientProcessId);
#else
GET_WINDOWS_FUNCTION(
kernel32_module, GetNamedPipeClientProcessId);
#endif
}
/*
* 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 &&
p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
SocketEndpointInfo *pi = snew(SocketEndpointInfo);
pi->addressfamily = ADDRTYPE_LOCAL;
pi->addr_text = NULL;
pi->port = -1;
pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
return pi;
}
return NULL;
}
static const SocketVtable HandleSocket_sockvt = {
.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,
.endpoint_info = sk_handle_endpoint_info,
};
static void sk_handle_connect_success_callback(void *ctx)
{
HandleSocket *hs = (HandleSocket *)ctx;
plug_log(hs->plug, &hs->sock, PLUGLOG_CONNECT_SUCCESS, hs->addr, hs->port,
NULL, 0);
}
Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
SockAddr *addr, int port, Plug *plug,
bool overlapped)
{
HandleSocket *hs;
int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
hs = snew(HandleSocket);
hs->sock.vt = &HandleSocket_sockvt;
hs->addr = addr;
hs->port = port;
hs->plug = plug;
hs->error = NULL;
hs->frozen = UNFROZEN;
bufchain_init(&hs->inputdata);
psb_init(&hs->psb);
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);
hs->defer_close = hs->deferred_close = false;
queue_toplevel_callback(sk_handle_connect_success_callback, hs);
return &hs->sock;
}
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);
}
static void sk_handle_deferred_close(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
deferred_socket_opener_free(hs->opener);
bufchain_clear(&hs->outputdata);
if (hs->addr)
sk_addr_free(hs->addr);
delete_callbacks_for_context(hs);
sfree(hs);
}
static size_t sk_handle_deferred_write(Socket *s, const void *data, size_t len)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
assert(!hs->output_eof_pending);
bufchain_add(&hs->outputdata, data, len);
return bufchain_size(&hs->outputdata);
}
static void sk_handle_deferred_write_eof(Socket *s)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
assert(!hs->output_eof_pending);
hs->output_eof_pending = true;
}
static void sk_handle_deferred_set_frozen(Socket *s, bool is_frozen)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
hs->frozen = is_frozen;
}
static SocketEndpointInfo *sk_handle_deferred_endpoint_info(
Socket *s, bool peer)
{
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,
.endpoint_info = sk_handle_deferred_endpoint_info,
};
Socket *make_deferred_handle_socket(DeferredSocketOpener *opener,
SockAddr *addr, int port, Plug *plug)
{
HandleSocket *hs = snew(HandleSocket);
hs->sock.vt = &HandleSocket_deferred_sockvt;
hs->addr = addr;
hs->port = port;
hs->plug = plug;
hs->error = NULL;
hs->opener = opener;
bufchain_init(&hs->outputdata);
hs->output_eof_pending = false;
hs->start_frozen = false;
return &hs->sock;
}
void setup_handle_socket(Socket *s, HANDLE send_H, HANDLE recv_H,
HANDLE stderr_H, bool overlapped)
{
HandleSocket *hs = container_of(s, HandleSocket, sock);
assert(hs->sock.vt == &HandleSocket_deferred_sockvt);
int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
struct handle *recv_h = handle_input_new(
recv_H, handle_gotdata, hs, flags);
struct handle *send_h = handle_output_new(
send_H, handle_sentdata, hs, flags);
struct handle *stderr_h = !stderr_H ? NULL : handle_input_new(
stderr_H, handle_stderr, hs, flags);
while (bufchain_size(&hs->outputdata)) {
ptrlen data = bufchain_prefix(&hs->outputdata);
handle_write(send_h, data.ptr, data.len);
bufchain_consume(&hs->outputdata, data.len);
}
if (hs->output_eof_pending)
handle_write_eof(send_h);
bool start_frozen = hs->start_frozen;
deferred_socket_opener_free(hs->opener);
bufchain_clear(&hs->outputdata);
hs->sock.vt = &HandleSocket_sockvt;
hs->frozen = start_frozen ? FREEZING : UNFROZEN;
bufchain_init(&hs->inputdata);
psb_init(&hs->psb);
hs->recv_H = recv_H;
hs->recv_h = recv_h;
hs->send_H = send_H;
hs->send_h = send_h;
hs->stderr_H = stderr_H;
hs->stderr_h = stderr_h;
hs->defer_close = hs->deferred_close = false;
queue_toplevel_callback(sk_handle_connect_success_callback, hs);
}
|