1999-11-01 16:40:40 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
2001-08-25 17:09:23 +00:00
|
|
|
#define RAW_MAX_BACKLOG 4096
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
typedef struct raw_backend_data {
|
|
|
|
const struct plug_function_table *fn;
|
|
|
|
/* the above field _must_ be first in the structure */
|
1999-11-01 16:40:40 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
Socket s;
|
|
|
|
int bufsize;
|
|
|
|
void *frontend;
|
|
|
|
} *Raw;
|
1999-11-01 16:40:40 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static void raw_size(void *handle, int width, int height);
|
|
|
|
|
|
|
|
static void c_write(Raw raw, char *buf, int len)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
int backlog = from_backend(raw->frontend, 0, buf, len);
|
|
|
|
sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
|
1999-11-01 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
static int raw_closing(Plug plug, char *error_msg, int error_code,
|
|
|
|
int calling_back)
|
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) plug;
|
|
|
|
|
|
|
|
if (raw->s) {
|
|
|
|
sk_close(raw->s);
|
|
|
|
raw->s = NULL;
|
2001-07-31 14:23:21 +00:00
|
|
|
}
|
2001-03-13 10:22:45 +00:00
|
|
|
if (error_msg) {
|
2001-05-06 14:35:20 +00:00
|
|
|
/* A socket error has occurred. */
|
2002-03-23 18:04:27 +00:00
|
|
|
logevent(error_msg);
|
|
|
|
connection_fatal("%s", error_msg);
|
2001-05-06 14:35:20 +00:00
|
|
|
} /* Otherwise, the remote side closed the connection normally. */
|
2001-03-13 10:22:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
static int raw_receive(Plug plug, int urgent, char *data, int len)
|
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) plug;
|
|
|
|
c_write(raw, data, len);
|
2000-10-23 10:32:37 +00:00
|
|
|
return 1;
|
1999-11-01 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
2001-09-07 22:39:01 +00:00
|
|
|
static void raw_sent(Plug plug, int bufsize)
|
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) plug;
|
|
|
|
raw->bufsize = bufsize;
|
2001-09-07 22:39:01 +00:00
|
|
|
}
|
|
|
|
|
1999-11-01 16:40:40 +00:00
|
|
|
/*
|
2000-10-23 10:32:37 +00:00
|
|
|
* Called to set up the raw connection.
|
|
|
|
*
|
1999-11-01 16:40:40 +00:00
|
|
|
* Returns an error message, or NULL on success.
|
|
|
|
*
|
2001-05-09 14:01:15 +00:00
|
|
|
* Also places the canonical host name into `realhost'. It must be
|
|
|
|
* freed by the caller.
|
1999-11-01 16:40:40 +00:00
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static char *raw_init(void *frontend_handle, void **backend_handle,
|
|
|
|
char *host, int port, char **realhost, int nodelay)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
static const struct plug_function_table fn_table = {
|
2001-03-13 10:22:45 +00:00
|
|
|
raw_closing,
|
2001-09-07 22:39:01 +00:00
|
|
|
raw_receive,
|
|
|
|
raw_sent
|
2002-10-25 11:30:33 +00:00
|
|
|
};
|
2000-10-23 10:32:37 +00:00
|
|
|
SockAddr addr;
|
|
|
|
char *err;
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw;
|
1999-11-01 16:40:40 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
raw = smalloc(sizeof(*raw));
|
|
|
|
raw->fn = &fn_table;
|
|
|
|
raw->s = NULL;
|
|
|
|
*backend_handle = raw;
|
|
|
|
|
|
|
|
raw->frontend = frontend_handle;
|
2002-10-22 16:11:33 +00:00
|
|
|
|
1999-11-01 16:40:40 +00:00
|
|
|
/*
|
|
|
|
* Try to find host.
|
|
|
|
*/
|
2001-09-07 22:39:01 +00:00
|
|
|
{
|
|
|
|
char buf[200];
|
|
|
|
sprintf(buf, "Looking up host \"%.170s\"", host);
|
|
|
|
logevent(buf);
|
|
|
|
}
|
2000-10-23 10:32:37 +00:00
|
|
|
addr = sk_namelookup(host, realhost);
|
2001-05-06 14:35:20 +00:00
|
|
|
if ((err = sk_addr_error(addr)))
|
2000-10-23 10:32:37 +00:00
|
|
|
return err;
|
1999-11-01 16:40:40 +00:00
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
port = 23; /* default telnet port */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
2001-09-07 22:39:01 +00:00
|
|
|
{
|
|
|
|
char buf[200], addrbuf[100];
|
|
|
|
sk_getaddr(addr, addrbuf, 100);
|
|
|
|
sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
|
|
|
|
logevent(buf);
|
|
|
|
}
|
2002-10-25 11:30:33 +00:00
|
|
|
raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) raw);
|
|
|
|
if ((err = sk_socket_error(raw->s)))
|
2000-10-23 10:32:37 +00:00
|
|
|
return err;
|
1999-11-01 16:40:40 +00:00
|
|
|
|
2000-10-23 10:32:37 +00:00
|
|
|
sk_addr_free(addr);
|
1999-11-01 16:40:40 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to send data down the raw connection.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static int raw_send(void *handle, char *buf, int len)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) handle;
|
|
|
|
|
|
|
|
if (raw->s == NULL)
|
2001-08-27 15:55:44 +00:00
|
|
|
return 0;
|
1999-11-01 16:40:40 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
raw->bufsize = sk_write(raw->s, buf, len);
|
2001-08-25 17:09:23 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
return raw->bufsize;
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to query the current socket sendability status.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static int raw_sendbuffer(void *handle)
|
2001-08-25 17:09:23 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) handle;
|
|
|
|
return raw->bufsize;
|
1999-11-01 16:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to set the size of the window
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static void raw_size(void *handle, int width, int height)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
1999-11-01 16:40:40 +00:00
|
|
|
/* Do nothing! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send raw special codes.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static void raw_special(void *handle, Telnet_Special code)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
1999-11-01 16:40:40 +00:00
|
|
|
/* Do nothing! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static Socket raw_socket(void *handle)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) handle;
|
|
|
|
return raw->s;
|
2001-05-06 14:35:20 +00:00
|
|
|
}
|
2000-09-08 16:42:11 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int raw_sendok(void *handle)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2000-09-08 14:45:20 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static void raw_unthrottle(void *handle, int backlog)
|
2001-08-25 17:09:23 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Raw raw = (Raw) handle;
|
|
|
|
sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int raw_ldisc(void *handle, int option)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2001-01-24 14:08:20 +00:00
|
|
|
if (option == LD_EDIT || option == LD_ECHO)
|
2001-05-06 14:35:20 +00:00
|
|
|
return 1;
|
2001-01-24 14:08:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int raw_exitcode(void *handle)
|
2001-12-29 15:31:42 +00:00
|
|
|
{
|
|
|
|
/* Exit codes are a meaningless concept in the Raw protocol */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-11-01 16:40:40 +00:00
|
|
|
Backend raw_backend = {
|
|
|
|
raw_init,
|
|
|
|
raw_send,
|
2001-08-25 17:09:23 +00:00
|
|
|
raw_sendbuffer,
|
1999-11-01 16:40:40 +00:00
|
|
|
raw_size,
|
2000-09-08 14:45:20 +00:00
|
|
|
raw_special,
|
2000-09-08 16:42:11 +00:00
|
|
|
raw_socket,
|
2001-12-29 15:31:42 +00:00
|
|
|
raw_exitcode,
|
2000-10-04 14:35:15 +00:00
|
|
|
raw_sendok,
|
2001-01-24 14:08:20 +00:00
|
|
|
raw_ldisc,
|
2001-08-25 17:09:23 +00:00
|
|
|
raw_unthrottle,
|
2000-10-04 14:35:15 +00:00
|
|
|
1
|
1999-11-01 16:40:40 +00:00
|
|
|
};
|