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