`realhost', passed back from all the backend init functions, was
[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 *
6e1ebb76 80 * Also places the canonical host name into `realhost'. It must be
81 * freed by the caller.
c91409da 82 */
32874aea 83static char *rlogin_init(char *host, int port, char **realhost)
84{
7e78000d 85 static struct plug_function_table fn_table = {
86 rlogin_closing,
87 rlogin_receive
88 }, *fn_table_ptr = &fn_table;
89
c91409da 90 SockAddr addr;
91 char *err;
92
93 /*
94 * Try to find host.
95 */
96 addr = sk_namelookup(host, realhost);
32874aea 97 if ((err = sk_addr_error(addr)))
c91409da 98 return err;
99
100 if (port < 0)
101 port = 513; /* default rlogin port */
102
103 /*
104 * Open socket.
105 */
7e78000d 106 s = sk_new(addr, port, 1, 0, &fn_table_ptr);
32874aea 107 if ((err = sk_socket_error(s)))
c91409da 108 return err;
109
110 sk_addr_free(addr);
111
112 /*
113 * Send local username, remote username, terminal/speed
114 */
115
116 {
32874aea 117 char z = 0;
118 char *p;
119 sk_write(s, &z, 1);
120 sk_write(s, cfg.localusername, strlen(cfg.localusername));
121 sk_write(s, &z, 1);
122 sk_write(s, cfg.username, strlen(cfg.username));
123 sk_write(s, &z, 1);
124 sk_write(s, cfg.termtype, strlen(cfg.termtype));
125 sk_write(s, "/", 1);
126 for (p = cfg.termspeed; isdigit(*p); p++);
127 sk_write(s, cfg.termspeed, p - cfg.termspeed);
128 sk_write(s, &z, 1);
c91409da 129 }
130
c91409da 131 return NULL;
132}
133
134/*
135 * Called to send data down the rlogin connection.
136 */
32874aea 137static void rlogin_send(char *buf, int len)
138{
c91409da 139
140 if (s == NULL)
141 return;
142
143 sk_write(s, buf, len);
144}
145
146/*
147 * Called to set the size of the window
148 */
32874aea 149static void rlogin_size(void)
150{
e418595a 151 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 152
32874aea 153 b[6] = cols >> 8;
154 b[7] = cols & 0xFF;
155 b[4] = rows >> 8;
156 b[5] = rows & 0xFF;
c91409da 157 sk_write(s, b, 12);
158 return;
159}
160
161/*
162 * Send rlogin special codes.
163 */
32874aea 164static void rlogin_special(Telnet_Special code)
165{
c91409da 166 /* Do nothing! */
167 return;
168}
169
32874aea 170static Socket rlogin_socket(void)
171{
172 return s;
173}
c91409da 174
32874aea 175static int rlogin_sendok(void)
176{
177 return 1;
178}
c91409da 179
32874aea 180static int rlogin_ldisc(int option)
181{
0965bee0 182 return 0;
183}
184
c91409da 185Backend rlogin_backend = {
186 rlogin_init,
187 rlogin_send,
188 rlogin_size,
189 rlogin_special,
190 rlogin_socket,
191 rlogin_sendok,
0965bee0 192 rlogin_ldisc,
c91409da 193 1
194};