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