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