1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

Mark Baker's raw-TCP back end (untested and experimental as yet)

[originally from svn r266]
This commit is contained in:
Simon Tatham 1999-11-01 16:40:40 +00:00
parent 2ec48b843b
commit 6693daa636
7 changed files with 266 additions and 22 deletions

View File

@ -10,7 +10,7 @@ CFLAGS = /nologo /W3 /YX /O2 /Yd /D_WINDOWS /DDEBUG /ML /Fd
.c.obj:
cl $(FWHACK) $(CFLAGS) /c $*.c
PUTTYOBJS = window.obj windlg.obj terminal.obj telnet.obj
PUTTYOBJS = window.obj windlg.obj terminal.obj telnet.obj raw.obj
OBJS1 = misc.obj noise.obj
OBJS2 = ssh.obj sshcrc.obj sshdes.obj sshmd5.obj sshrsa.obj sshrand.obj
OBJS3 = sshsha.obj sshblowf.obj version.obj
@ -43,6 +43,7 @@ window.obj: window.c putty.h win_res.h
windlg.obj: windlg.c putty.h ssh.h win_res.h
terminal.obj: terminal.c putty.h
telnet.obj: telnet.c putty.h
raw.obj: raw.c putty.h
misc.obj: misc.c putty.h
noise.obj: noise.c putty.h ssh.h
ssh.obj: ssh.c ssh.h putty.h

View File

@ -93,7 +93,7 @@ typedef struct {
/* Basic options */
char host[512];
int port;
enum { PROT_TELNET, PROT_SSH } protocol;
enum { PROT_RAW, PROT_TELNET, PROT_SSH } protocol;
int close_on_exit;
/* SSH options */
int nopty;
@ -203,6 +203,12 @@ void term_deselect (void);
void term_update (void);
void term_invalidate(void);
/*
* Exports from raw.c.
*/
Backend raw_backend;
/*
* Exports from telnet.c.
*/

227
raw.c Normal file
View File

@ -0,0 +1,227 @@
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include "putty.h"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
static SOCKET s = INVALID_SOCKET;
#define iswritable(x) ( (x) != IAC && (x) != CR )
static void raw_size(void);
static int sb_opt, sb_len;
static char *sb_buf = NULL;
static int sb_size = 0;
#define SB_DELTA 1024
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;
ret = send (s, outbuf+outbuf_reap, len, 0);
if (ret > 0)
outbuf_reap = (outbuf_reap + ret) & OUTBUF_MASK;
if (ret < len)
return;
}
}
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;
}
}
try_write();
}
static void c_write (char *buf, int len) {
while (len--) {
int new_head = (inbuf_head + 1) & INBUF_MASK;
int c = (unsigned char) *buf;
if (new_head != inbuf_reap) {
inbuf[inbuf_head] = *buf++;
inbuf_head = new_head;
}
}
}
/*
* 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().
*
* Returns an error message, or NULL on success.
*
* Also places the canonical host name into `realhost'.
*/
static char *raw_init (HWND hwnd, char *host, int port, char **realhost) {
SOCKADDR_IN addr;
struct hostent *h;
unsigned long a;
/*
* 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);
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";
}
/*
* 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 (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";
}
return NULL;
}
/*
* Process a WM_NETEVENT message. Will return 0 if the connection
* has closed, or <0 for a socket error.
*/
static int raw_msg (WPARAM wParam, LPARAM lParam) {
int ret;
char buf[256];
if (s == INVALID_SOCKET) /* how the hell did we get here?! */
return -5000;
if (WSAGETSELECTERROR(lParam) != 0)
return -WSAGETSELECTERROR(lParam);
switch (WSAGETSELECTEVENT(lParam)) {
case FD_READ:
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; /* can't happen, in theory */
}
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;
case FD_CLOSE:
s = INVALID_SOCKET;
return 0;
}
return 1; /* shouldn't happen, but WTF */
}
/*
* Called to send data down the raw connection.
*/
static void raw_send (char *buf, int len) {
if (s == INVALID_SOCKET)
return;
s_write( buf, len );
}
/*
* Called to set the size of the window
*/
static void raw_size(void) {
/* Do nothing! */
return;
}
/*
* Send raw special codes.
*/
static void raw_special (Telnet_Special code) {
/* Do nothing! */
return;
}
Backend raw_backend = {
raw_init,
raw_msg,
raw_send,
raw_size,
raw_special
};

View File

@ -32,15 +32,16 @@
#define IDC0_PORTSTATIC 1003
#define IDC0_PORT 1004
#define IDC0_PROTSTATIC 1005
#define IDC0_PROTTELNET 1006
#define IDC0_PROTSSH 1007
#define IDC0_SESSSTATIC 1008
#define IDC0_SESSEDIT 1009
#define IDC0_SESSLIST 1010
#define IDC0_SESSLOAD 1011
#define IDC0_SESSSAVE 1012
#define IDC0_SESSDEL 1013
#define IDC0_CLOSEEXIT 1014
#define IDC0_PROTRAW 1006
#define IDC0_PROTTELNET 1007
#define IDC0_PROTSSH 1008
#define IDC0_SESSSTATIC 1009
#define IDC0_SESSEDIT 1010
#define IDC0_SESSLIST 1011
#define IDC0_SESSLOAD 1012
#define IDC0_SESSSAVE 1013
#define IDC0_SESSDEL 1014
#define IDC0_CLOSEEXIT 1015
#define IDC1_DELSTATIC 1001
#define IDC1_DEL008 1002

View File

@ -51,12 +51,14 @@ BEGIN
LTEXT "&Port", IDC0_PORTSTATIC, 131, 3, 34, 8
EDITTEXT IDC0_PORT, 131, 11, 34, 12
#ifdef FWHACK
RTEXT "Protocol:", IDC0_PROTSTATIC, 3, 29, 77, 8
AUTORADIOBUTTON "&Telnet", IDC0_PROTTELNET, 86, 29, 33, 10, WS_GROUP
RTEXT "Protocol:", IDC0_PROTSTATIC, 3, 29, 42, 8
AUTORADIOBUTTON "R&aw", IDC0_PROTRAW, 51, 29, 33, 10, WS_GROUP
AUTORADIOBUTTON "&Telnet", IDC0_PROTTELNET, 86, 29, 33, 10
AUTORADIOBUTTON "SS&H/hack", IDC0_PROTSSH, 122, 29, 43, 10
#else
RTEXT "Protocol:", IDC0_PROTSTATIC, 3, 29, 87, 8
AUTORADIOBUTTON "&Telnet", IDC0_PROTTELNET, 96, 29, 33, 10, WS_GROUP
RTEXT "Protocol:", IDC0_PROTSTATIC, 3, 29, 52, 8
AUTORADIOBUTTON "R&aw", IDC0_PROTRAW, 61, 29, 33, 10, WS_GROUP
AUTORADIOBUTTON "&Telnet", IDC0_PROTTELNET, 96, 29, 33, 10
AUTORADIOBUTTON "SS&H", IDC0_PROTSSH, 132, 29, 33, 10
#endif
LTEXT "Sto&red Sessions", IDC0_SESSSTATIC, 3, 40, 122, 8

View File

@ -116,7 +116,8 @@ static void save_settings (char *section, int do_host) {
wpps (sesskey, "HostName", cfg.host);
wppi (sesskey, "PortNumber", cfg.port);
wpps (sesskey, "Protocol",
cfg.protocol == PROT_SSH ? "ssh" : "telnet");
cfg.protocol == PROT_SSH ? "ssh" :
cfg.protocol == PROT_TELNET ? "telnet" : "raw" );
}
wppi (sesskey, "CloseOnExit", !!cfg.close_on_exit);
wpps (sesskey, "TerminalType", cfg.termtype);
@ -228,6 +229,8 @@ static void load_settings (char *section, int do_host) {
cfg.protocol = PROT_SSH;
else if (!strcmp(prot, "telnet"))
cfg.protocol = PROT_TELNET;
else if (!strcmp(prot, "raw"))
cfg.protocol = PROT_RAW;
else
cfg.protocol = default_protocol;
@ -447,8 +450,9 @@ static int CALLBACK ConnectionProc (HWND hwnd, UINT msg,
for (i = 0; i < nsessions; i++)
SendDlgItemMessage (hwnd, IDC0_SESSLIST, LB_ADDSTRING,
0, (LPARAM) (sessions[i]));
CheckRadioButton (hwnd, IDC0_PROTTELNET, IDC0_PROTSSH,
cfg.protocol==PROT_SSH ? IDC0_PROTSSH : IDC0_PROTTELNET);
CheckRadioButton (hwnd, IDC0_PROTRAW, IDC0_PROTSSH,
cfg.protocol==PROT_SSH ? IDC0_PROTSSH :
cfg.protocol==PROT_TELNET ? IDC0_PROTTELNET : IDC0_PROTRAW );
CheckDlgButton (hwnd, IDC0_CLOSEEXIT, cfg.close_on_exit);
break;
case WM_LBUTTONUP:
@ -464,10 +468,12 @@ static int CALLBACK ConnectionProc (HWND hwnd, UINT msg,
switch (LOWORD(wParam)) {
case IDC0_PROTTELNET:
case IDC0_PROTSSH:
case IDC0_PROTRAW:
if (HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED) {
int i = IsDlgButtonChecked (hwnd, IDC0_PROTSSH);
cfg.protocol = i ? PROT_SSH : PROT_TELNET;
int j = IsDlgButtonChecked (hwnd, IDC0_PROTTELNET);
cfg.protocol = i ? PROT_SSH : j ? PROT_TELNET : PROT_RAW ;
if ((cfg.protocol == PROT_SSH && cfg.port == 23) ||
(cfg.protocol == PROT_TELNET && cfg.port == 22)) {
cfg.port = i ? 22 : 23;
@ -543,9 +549,9 @@ static int CALLBACK ConnectionProc (HWND hwnd, UINT msg,
!!strcmp(sessions[n], "Default Settings"));
SetDlgItemText (hwnd, IDC0_HOST, cfg.host);
SetDlgItemInt (hwnd, IDC0_PORT, cfg.port, FALSE);
CheckRadioButton (hwnd, IDC0_PROTTELNET, IDC0_PROTSSH,
CheckRadioButton (hwnd, IDC0_PROTRAW, IDC0_PROTSSH,
(cfg.protocol==PROT_SSH ? IDC0_PROTSSH :
IDC0_PROTTELNET));
cfg.protocol==PROT_TELNET ? IDC0_PROTTELNET : IDC0_PROTRAW));
CheckDlgButton (hwnd, IDC0_CLOSEEXIT, cfg.close_on_exit);
SendDlgItemMessage (hwnd, IDC0_SESSLIST, LB_SETCURSEL,
(WPARAM) -1, 0);

View File

@ -178,7 +178,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
}
}
back = (cfg.protocol == PROT_SSH ? &ssh_backend : &telnet_backend);
back = (cfg.protocol == PROT_SSH ? &ssh_backend :
cfg.protocol == PROT_TELNET ? &telnet_backend : &raw_backend );
if (!prev) {
wndclass.style = 0;