-#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
-#include <winsock.h>
#include "putty.h"
#define TRUE 1
#endif
-static SOCKET s = INVALID_SOCKET;
+#define RAW_MAX_BACKLOG 4096
-static void raw_size(void);
+typedef struct raw_backend_data {
+ const struct plug_function_table *fn;
+ /* the above field _must_ be first in the structure */
-static int sb_opt, sb_len;
-static char *sb_buf = NULL;
-static int sb_size = 0;
-#define SB_DELTA 1024
+ Socket s;
+ int bufsize;
+ void *frontend;
+} *Raw;
-static void try_write (void) {
- while (outbuf_head != outbuf_reap) {
- int end = (outbuf_reap < outbuf_head ? outbuf_head : OUTBUF_SIZE);
- int len = end - outbuf_reap;
- int ret;
+static void raw_size(void *handle, int width, int height);
- ret = send (s, outbuf+outbuf_reap, len, 0);
- if (ret > 0)
- outbuf_reap = (outbuf_reap + ret) & OUTBUF_MASK;
- if (ret < len)
- return;
- }
+static void c_write(Raw raw, char *buf, int len)
+{
+ int backlog = from_backend(raw->frontend, 0, buf, len);
+ sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
}
-static void s_write (void *buf, int len) {
- unsigned char *p = buf;
- while (len--) {
- int new_head = (outbuf_head + 1) & OUTBUF_MASK;
- if (new_head != outbuf_reap) {
- outbuf[outbuf_head] = *p++;
- outbuf_head = new_head;
- }
+static int raw_closing(Plug plug, char *error_msg, int error_code,
+ int calling_back)
+{
+ Raw raw = (Raw) plug;
+
+ if (raw->s) {
+ sk_close(raw->s);
+ raw->s = NULL;
}
- try_write();
+ if (error_msg) {
+ /* A socket error has occurred. */
+ logevent(raw->frontend, error_msg);
+ connection_fatal("%s", error_msg);
+ } /* Otherwise, the remote side closed the connection normally. */
+ return 0;
}
-static void c_write (char *buf, int len) {
- while (len--)
- c_write1(*buf++);
+static int raw_receive(Plug plug, int urgent, char *data, int len)
+{
+ Raw raw = (Raw) plug;
+ c_write(raw, data, len);
+ return 1;
+}
+
+static void raw_sent(Plug plug, int bufsize)
+{
+ Raw raw = (Raw) plug;
+ raw->bufsize = bufsize;
}
/*
- * Called to set up the raw connection. Will arrange for
- * WM_NETEVENT messages to be passed to the specified window, whose
- * window procedure should then call raw_msg().
- *
+ * Called to set up the raw connection.
+ *
* Returns an error message, or NULL on success.
*
- * Also places the canonical host name into `realhost'.
+ * Also places the canonical host name into `realhost'. It must be
+ * freed by the caller.
*/
-static char *raw_init (HWND hwnd, char *host, int port, char **realhost) {
- SOCKADDR_IN addr;
- struct hostent *h;
- unsigned long a;
+static char *raw_init(void *frontend_handle, void **backend_handle,
+ char *host, int port, char **realhost, int nodelay)
+{
+ static const struct plug_function_table fn_table = {
+ raw_closing,
+ raw_receive,
+ raw_sent
+ };
+ SockAddr addr;
+ char *err;
+ Raw raw;
+
+ raw = smalloc(sizeof(*raw));
+ raw->fn = &fn_table;
+ raw->s = NULL;
+ *backend_handle = raw;
+
+ raw->frontend = frontend_handle;
/*
* Try to find host.
*/
- if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
- if ( (h = gethostbyname(host)) == NULL)
- switch (WSAGetLastError()) {
- case WSAENETDOWN: return "Network is down";
- case WSAHOST_NOT_FOUND: case WSANO_DATA:
- return "Host does not exist";
- case WSATRY_AGAIN: return "Host not found";
- default: return "gethostbyname: unknown error";
- }
- memcpy (&a, h->h_addr, sizeof(a));
- *realhost = h->h_name;
- } else
- *realhost = host;
- a = ntohl(a);
+ {
+ char *buf;
+ buf = dupprintf("Looking up host \"%s\"", host);
+ logevent(raw->frontend, buf);
+ sfree(buf);
+ }
+ addr = sk_namelookup(host, realhost);
+ if ((err = sk_addr_error(addr)))
+ return err;
if (port < 0)
port = 23; /* default telnet port */
/*
* Open socket.
*/
- s = socket(AF_INET, SOCK_STREAM, 0);
- if (s == INVALID_SOCKET)
- switch (WSAGetLastError()) {
- case WSAENETDOWN: return "Network is down";
- case WSAEAFNOSUPPORT: return "TCP/IP support not present";
- default: return "socket(): unknown error";
- }
+ {
+ char *buf, addrbuf[100];
+ sk_getaddr(addr, addrbuf, 100);
+ buf = dupprintf("Connecting to %s port %d", addrbuf, port);
+ logevent(raw->frontend, buf);
+ sfree(buf);
+ }
+ raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) raw);
+ if ((err = sk_socket_error(raw->s)))
+ return err;
- /*
- * Bind to local address.
- */
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_port = htons(0);
- if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
- switch (WSAGetLastError()) {
- case WSAENETDOWN: return "Network is down";
- default: return "bind(): unknown error";
- }
-
- /*
- * Connect to remote address.
- */
- addr.sin_addr.s_addr = htonl(a);
- addr.sin_port = htons((short)port);
- if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
- switch (WSAGetLastError()) {
- case WSAENETDOWN: return "Network is down";
- case WSAECONNREFUSED: return "Connection refused";
- case WSAENETUNREACH: return "Network is unreachable";
- case WSAEHOSTUNREACH: return "No route to host";
- default: return "connect(): unknown error";
- }
-
- if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ |
- FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR)
- switch (WSAGetLastError()) {
- case WSAENETDOWN: return "Network is down";
- default: return "WSAAsyncSelect(): unknown error";
- }
+ sk_addr_free(addr);
return NULL;
}
/*
- * Process a WM_NETEVENT message. Will return 0 if the connection
- * has closed, or <0 for a socket error.
+ * Called to send data down the raw connection.
*/
-static int raw_msg (WPARAM wParam, LPARAM lParam) {
- int ret;
- char buf[256];
+static int raw_send(void *handle, char *buf, int len)
+{
+ Raw raw = (Raw) handle;
- /*
- * Because reading less than the whole of the available pending
- * data can generate an FD_READ event, we need to allow for the
- * possibility that FD_READ may arrive with FD_CLOSE already in
- * the queue; so it's possible that we can get here even with s
- * invalid. If so, we return 1 and don't worry about it.
- */
- if (s == INVALID_SOCKET)
- return 1;
+ if (raw->s == NULL)
+ return 0;
- if (WSAGETSELECTERROR(lParam) != 0)
- return -WSAGETSELECTERROR(lParam);
-
- switch (WSAGETSELECTEVENT(lParam)) {
- case FD_READ:
- case FD_CLOSE:
- ret = recv(s, buf, sizeof(buf), 0);
- if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
- return 1;
- if (ret < 0) /* any _other_ error */
- return -10000-WSAGetLastError();
- if (ret == 0) {
- s = INVALID_SOCKET;
- return 0;
- }
- c_write( buf, ret );
- return 1;
- case FD_OOB:
- do {
- ret = recv(s, buf, sizeof(buf), 0);
- c_write( buf, ret );
- } while (ret > 0);
- do {
- ret = recv(s, buf, 1, MSG_OOB);
- } while (ret > 0);
- if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
- return -30000-WSAGetLastError();
- return 1;
- case FD_WRITE:
- if (outbuf_head != outbuf_reap)
- try_write();
- return 1;
- }
- return 1; /* shouldn't happen, but WTF */
+ raw->bufsize = sk_write(raw->s, buf, len);
+
+ return raw->bufsize;
}
/*
- * Called to send data down the raw connection.
+ * Called to query the current socket sendability status.
*/
-static void raw_send (char *buf, int len) {
-
- if (s == INVALID_SOCKET)
- return;
-
- s_write( buf, len );
+static int raw_sendbuffer(void *handle)
+{
+ Raw raw = (Raw) handle;
+ return raw->bufsize;
}
/*
* Called to set the size of the window
*/
-static void raw_size(void) {
+static void raw_size(void *handle, int width, int height)
+{
/* Do nothing! */
return;
}
/*
* Send raw special codes.
*/
-static void raw_special (Telnet_Special code) {
+static void raw_special(void *handle, Telnet_Special code)
+{
/* Do nothing! */
return;
}
-static SOCKET raw_socket(void) { return s; }
+static Socket raw_socket(void *handle)
+{
+ Raw raw = (Raw) handle;
+ return raw->s;
+}
+
+static int raw_sendok(void *handle)
+{
+ return 1;
+}
+
+static void raw_unthrottle(void *handle, int backlog)
+{
+ Raw raw = (Raw) handle;
+ sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
+}
-static int raw_sendok(void) { return 1; }
+static int raw_ldisc(void *handle, int option)
+{
+ if (option == LD_EDIT || option == LD_ECHO)
+ return 1;
+ return 0;
+}
+
+static void raw_provide_ldisc(void *handle, void *ldisc)
+{
+ /* This is a stub. */
+}
+
+static void raw_provide_logctx(void *handle, void *logctx)
+{
+ /* This is a stub. */
+}
+
+static int raw_exitcode(void *handle)
+{
+ /* Exit codes are a meaningless concept in the Raw protocol */
+ return 0;
+}
Backend raw_backend = {
raw_init,
- raw_msg,
raw_send,
+ raw_sendbuffer,
raw_size,
raw_special,
raw_socket,
- raw_sendok
+ raw_exitcode,
+ raw_sendok,
+ raw_ldisc,
+ raw_provide_ldisc,
+ raw_provide_logctx,
+ raw_unthrottle,
+ 1
};