Placate gcc's `-Wall' warnings.
[u/mdw/putty] / rlogin.c
... / ...
CommitLineData
1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <ctype.h>
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 void c_write(char *buf, int len)
20{
21 from_backend(0, buf, len);
22}
23
24static int rlogin_closing(Plug plug, char *error_msg, int error_code,
25 int calling_back)
26{
27 sk_close(s);
28 s = NULL;
29 if (error_msg) {
30 /* A socket error has occurred. */
31 connection_fatal(error_msg);
32 } /* Otherwise, the remote side closed the connection normally. */
33 return 0;
34}
35
36static int rlogin_receive(Plug plug, int urgent, char *data, int len)
37{
38 if (urgent == 2) {
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 */
51 } else {
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);
66 }
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 *
75 * Also places the canonical host name into `realhost'. It must be
76 * freed by the caller.
77 */
78static char *rlogin_init(char *host, int port, char **realhost)
79{
80 static struct plug_function_table fn_table = {
81 rlogin_closing,
82 rlogin_receive
83 }, *fn_table_ptr = &fn_table;
84
85 SockAddr addr;
86 char *err;
87
88 /*
89 * Try to find host.
90 */
91 addr = sk_namelookup(host, realhost);
92 if ((err = sk_addr_error(addr)))
93 return err;
94
95 if (port < 0)
96 port = 513; /* default rlogin port */
97
98 /*
99 * Open socket.
100 */
101 s = sk_new(addr, port, 1, 0, &fn_table_ptr);
102 if ((err = sk_socket_error(s)))
103 return err;
104
105 sk_addr_free(addr);
106
107 /*
108 * Send local username, remote username, terminal/speed
109 */
110
111 {
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);
124 }
125
126 return NULL;
127}
128
129/*
130 * Called to send data down the rlogin connection.
131 */
132static void rlogin_send(char *buf, int len)
133{
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 */
144static void rlogin_size(void)
145{
146 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
147
148 b[6] = cols >> 8;
149 b[7] = cols & 0xFF;
150 b[4] = rows >> 8;
151 b[5] = rows & 0xFF;
152 sk_write(s, b, 12);
153 return;
154}
155
156/*
157 * Send rlogin special codes.
158 */
159static void rlogin_special(Telnet_Special code)
160{
161 /* Do nothing! */
162 return;
163}
164
165static Socket rlogin_socket(void)
166{
167 return s;
168}
169
170static int rlogin_sendok(void)
171{
172 return 1;
173}
174
175static int rlogin_ldisc(int option)
176{
177 return 0;
178}
179
180Backend rlogin_backend = {
181 rlogin_init,
182 rlogin_send,
183 rlogin_size,
184 rlogin_special,
185 rlogin_socket,
186 rlogin_sendok,
187 rlogin_ldisc,
188 1
189};