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