Oops - fix that fix :-/
[u/mdw/putty] / raw.c
CommitLineData
5e1a8e27 1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
5e1a8e27 4
5#include "putty.h"
6
7#ifndef FALSE
8#define FALSE 0
9#endif
10#ifndef TRUE
11#define TRUE 1
12#endif
13
8df7a775 14static Socket s = NULL;
5e1a8e27 15
5e1a8e27 16static void raw_size(void);
17
32874aea 18static void c_write(char *buf, int len)
19{
8df7a775 20 from_backend(0, buf, len);
5e1a8e27 21}
22
32874aea 23static int raw_closing(Plug plug, char *error_msg, int error_code,
24 int calling_back)
25{
f3ab576e 26 if (s) {
27 sk_close(s);
28 s = NULL;
29 }
7e78000d 30 if (error_msg) {
32874aea 31 /* A socket error has occurred. */
32 connection_fatal(error_msg);
33 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 34 return 0;
35}
36
32874aea 37static int raw_receive(Plug plug, int urgent, char *data, int len)
38{
8df7a775 39 c_write(data, len);
40 return 1;
5e1a8e27 41}
42
43/*
8df7a775 44 * Called to set up the raw connection.
45 *
5e1a8e27 46 * Returns an error message, or NULL on success.
47 *
6e1ebb76 48 * Also places the canonical host name into `realhost'. It must be
49 * freed by the caller.
5e1a8e27 50 */
32874aea 51static char *raw_init(char *host, int port, char **realhost)
52{
7e78000d 53 static struct plug_function_table fn_table = {
54 raw_closing,
55 raw_receive
56 }, *fn_table_ptr = &fn_table;
57
8df7a775 58 SockAddr addr;
59 char *err;
5e1a8e27 60
61 /*
62 * Try to find host.
63 */
8df7a775 64 addr = sk_namelookup(host, realhost);
32874aea 65 if ((err = sk_addr_error(addr)))
8df7a775 66 return err;
5e1a8e27 67
68 if (port < 0)
69 port = 23; /* default telnet port */
70
71 /*
72 * Open socket.
73 */
7e78000d 74 s = sk_new(addr, port, 0, 1, &fn_table_ptr);
32874aea 75 if ((err = sk_socket_error(s)))
8df7a775 76 return err;
5e1a8e27 77
8df7a775 78 sk_addr_free(addr);
5e1a8e27 79
80 return NULL;
81}
82
83/*
5e1a8e27 84 * Called to send data down the raw connection.
85 */
32874aea 86static void raw_send(char *buf, int len)
87{
5e1a8e27 88
8df7a775 89 if (s == NULL)
5e1a8e27 90 return;
91
8df7a775 92 sk_write(s, buf, len);
5e1a8e27 93}
94
95/*
96 * Called to set the size of the window
97 */
32874aea 98static void raw_size(void)
99{
5e1a8e27 100 /* Do nothing! */
101 return;
102}
103
104/*
105 * Send raw special codes.
106 */
32874aea 107static void raw_special(Telnet_Special code)
108{
5e1a8e27 109 /* Do nothing! */
110 return;
111}
112
32874aea 113static Socket raw_socket(void)
114{
115 return s;
116}
8ccc75b0 117
32874aea 118static int raw_sendok(void)
119{
120 return 1;
121}
4017be6d 122
32874aea 123static int raw_ldisc(int option)
124{
0965bee0 125 if (option == LD_EDIT || option == LD_ECHO)
32874aea 126 return 1;
0965bee0 127 return 0;
128}
129
5e1a8e27 130Backend raw_backend = {
131 raw_init,
5e1a8e27 132 raw_send,
133 raw_size,
4017be6d 134 raw_special,
8ccc75b0 135 raw_socket,
97db3be4 136 raw_sendok,
0965bee0 137 raw_ldisc,
97db3be4 138 1
5e1a8e27 139};