2 * General mechanism for wrapping up reading/writing of Windows
3 * HANDLEs into a PuTTY Socket abstraction.
9 #define DEFINE_PLUG_METHOD_MACROS
14 typedef struct Socket_handle_tag *Handle_Socket;
16 struct Socket_handle_tag {
17 const struct socket_function_table *fn;
18 /* the above variable absolutely *must* be the first in this structure */
20 HANDLE send_H, recv_H;
21 struct handle *send_h, *recv_h;
30 static int handle_gotdata(struct handle *h, void *data, int len)
32 Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
35 return plug_closing(ps->plug, "Read error from handle",
37 } else if (len == 0) {
38 return plug_closing(ps->plug, NULL, 0, 0);
40 return plug_receive(ps->plug, 0, data, len);
44 static void handle_sentdata(struct handle *h, int new_backlog)
46 Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
48 plug_sent(ps->plug, new_backlog);
51 static Plug sk_handle_plug(Socket s, Plug p)
53 Handle_Socket ps = (Handle_Socket) s;
60 static void sk_handle_close(Socket s)
62 Handle_Socket ps = (Handle_Socket) s;
64 handle_free(ps->send_h);
65 handle_free(ps->recv_h);
66 CloseHandle(ps->send_H);
67 if (ps->recv_H != ps->send_H)
68 CloseHandle(ps->recv_H);
73 static int sk_handle_write(Socket s, const char *data, int len)
75 Handle_Socket ps = (Handle_Socket) s;
77 return handle_write(ps->send_h, data, len);
80 static int sk_handle_write_oob(Socket s, const char *data, int len)
83 * oob data is treated as inband; nasty, but nothing really
86 return sk_handle_write(s, data, len);
89 static void sk_handle_write_eof(Socket s)
91 Handle_Socket ps = (Handle_Socket) s;
93 handle_write_eof(ps->send_h);
96 static void sk_handle_flush(Socket s)
98 /* Handle_Socket ps = (Handle_Socket) s; */
102 static void sk_handle_set_private_ptr(Socket s, void *ptr)
104 Handle_Socket ps = (Handle_Socket) s;
108 static void *sk_handle_get_private_ptr(Socket s)
110 Handle_Socket ps = (Handle_Socket) s;
114 static void sk_handle_set_frozen(Socket s, int is_frozen)
116 Handle_Socket ps = (Handle_Socket) s;
123 static const char *sk_handle_socket_error(Socket s)
125 Handle_Socket ps = (Handle_Socket) s;
129 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
132 static const struct socket_function_table socket_fn_table = {
139 sk_handle_set_private_ptr,
140 sk_handle_get_private_ptr,
141 sk_handle_set_frozen,
142 sk_handle_socket_error
146 int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
148 ret = snew(struct Socket_handle_tag);
149 ret->fn = &socket_fn_table;
153 ret->recv_H = recv_H;
154 ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
155 ret->send_H = send_H;
156 ret->send_h = handle_output_new(ret->send_H, handle_sentdata, ret, flags);