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