From RDB: telnet can now start up in passive mode, in which it
[u/mdw/putty] / rlogin.c
CommitLineData
c91409da 1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
b6c680d4 4#include <ctype.h>
c91409da 5
6#include "putty.h"
7
8#ifndef FALSE
9#define FALSE 0
10#endif
11#ifndef TRUE
12#define TRUE 1
13#endif
14
15static Socket s = NULL;
16
17static void rlogin_size(void);
18
19static int sb_opt, sb_len;
20static char *sb_buf = NULL;
21static int sb_size = 0;
22#define SB_DELTA 1024
23
32874aea 24static void c_write(char *buf, int len)
25{
c91409da 26 from_backend(0, buf, len);
27}
28
32874aea 29static int rlogin_closing(Plug plug, char *error_msg, int error_code,
30 int calling_back)
31{
7e78000d 32 sk_close(s);
33 s = NULL;
34 if (error_msg) {
32874aea 35 /* A socket error has occurred. */
36 connection_fatal(error_msg);
37 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 38 return 0;
39}
40
32874aea 41static int rlogin_receive(Plug plug, int urgent, char *data, int len)
42{
c91409da 43 if (urgent == 2) {
32874aea 44 char c;
45
46 c = *data++;
47 len--;
48 if (c == '\x80')
49 rlogin_size();
50 /*
51 * We should flush everything (aka Telnet SYNCH) if we see
52 * 0x02, and we should turn off and on _local_ flow control
53 * on 0x10 and 0x20 respectively. I'm not convinced it's
54 * worth it...
55 */
8a5fb9e3 56 } else {
32874aea 57 /*
58 * Main rlogin protocol. This is really simple: the first
59 * byte is expected to be NULL and is ignored, and the rest
60 * is printed.
61 */
62 static int firstbyte = 1;
63 if (firstbyte) {
64 if (data[0] == '\0') {
65 data++;
66 len--;
67 }
68 firstbyte = 0;
69 }
70 c_write(data, len);
c91409da 71 }
c91409da 72 return 1;
73}
74
75/*
76 * Called to set up the rlogin connection.
77 *
78 * Returns an error message, or NULL on success.
79 *
80 * Also places the canonical host name into `realhost'.
81 */
32874aea 82static char *rlogin_init(char *host, int port, char **realhost)
83{
7e78000d 84 static struct plug_function_table fn_table = {
85 rlogin_closing,
86 rlogin_receive
87 }, *fn_table_ptr = &fn_table;
88
c91409da 89 SockAddr addr;
90 char *err;
91
92 /*
93 * Try to find host.
94 */
95 addr = sk_namelookup(host, realhost);
32874aea 96 if ((err = sk_addr_error(addr)))
c91409da 97 return err;
98
99 if (port < 0)
100 port = 513; /* default rlogin port */
101
102 /*
103 * Open socket.
104 */
7e78000d 105 s = sk_new(addr, port, 1, 0, &fn_table_ptr);
32874aea 106 if ((err = sk_socket_error(s)))
c91409da 107 return err;
108
109 sk_addr_free(addr);
110
111 /*
112 * Send local username, remote username, terminal/speed
113 */
114
115 {
32874aea 116 char z = 0;
117 char *p;
118 sk_write(s, &z, 1);
119 sk_write(s, cfg.localusername, strlen(cfg.localusername));
120 sk_write(s, &z, 1);
121 sk_write(s, cfg.username, strlen(cfg.username));
122 sk_write(s, &z, 1);
123 sk_write(s, cfg.termtype, strlen(cfg.termtype));
124 sk_write(s, "/", 1);
125 for (p = cfg.termspeed; isdigit(*p); p++);
126 sk_write(s, cfg.termspeed, p - cfg.termspeed);
127 sk_write(s, &z, 1);
c91409da 128 }
129
c91409da 130 return NULL;
131}
132
133/*
134 * Called to send data down the rlogin connection.
135 */
32874aea 136static void rlogin_send(char *buf, int len)
137{
c91409da 138
139 if (s == NULL)
140 return;
141
142 sk_write(s, buf, len);
143}
144
145/*
146 * Called to set the size of the window
147 */
32874aea 148static void rlogin_size(void)
149{
e418595a 150 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 151
32874aea 152 b[6] = cols >> 8;
153 b[7] = cols & 0xFF;
154 b[4] = rows >> 8;
155 b[5] = rows & 0xFF;
c91409da 156 sk_write(s, b, 12);
157 return;
158}
159
160/*
161 * Send rlogin special codes.
162 */
32874aea 163static void rlogin_special(Telnet_Special code)
164{
c91409da 165 /* Do nothing! */
166 return;
167}
168
32874aea 169static Socket rlogin_socket(void)
170{
171 return s;
172}
c91409da 173
32874aea 174static int rlogin_sendok(void)
175{
176 return 1;
177}
c91409da 178
32874aea 179static int rlogin_ldisc(int option)
180{
0965bee0 181 return 0;
182}
183
c91409da 184Backend rlogin_backend = {
185 rlogin_init,
186 rlogin_send,
187 rlogin_size,
188 rlogin_special,
189 rlogin_socket,
190 rlogin_sendok,
0965bee0 191 rlogin_ldisc,
c91409da 192 1
193};