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 | |
14 | static Socket s = NULL; |
15 | |
16 | static void rlogin_size(void); |
17 | |
18 | static int sb_opt, sb_len; |
19 | static char *sb_buf = NULL; |
20 | static int sb_size = 0; |
21 | #define SB_DELTA 1024 |
22 | |
23 | static void c_write (char *buf, int len) { |
24 | from_backend(0, buf, len); |
25 | } |
26 | |
27 | static int rlogin_receive (Socket s, int urgent, char *data, int len) { |
28 | if (!len) { |
29 | /* Connection has closed. */ |
30 | sk_close(s); |
31 | s = NULL; |
32 | return 0; |
33 | } |
34 | if (urgent == 2) { |
35 | char c; |
c91409da |
36 | |
37 | c = *data++; len--; |
38 | if (c == 0x80) |
39 | rlogin_size(); |
40 | /* |
41 | * We should flush everything (aka Telnet SYNCH) if we see |
42 | * 0x02, and we should turn off and on _local_ flow control |
43 | * on 0x10 and 0x20 respectively. I'm not convinced it's |
44 | * worth it... |
45 | */ |
46 | } |
47 | c_write(data, len); |
48 | return 1; |
49 | } |
50 | |
51 | /* |
52 | * Called to set up the rlogin connection. |
53 | * |
54 | * Returns an error message, or NULL on success. |
55 | * |
56 | * Also places the canonical host name into `realhost'. |
57 | */ |
58 | static char *rlogin_init (char *host, int port, char **realhost) { |
59 | SockAddr addr; |
60 | char *err; |
61 | |
62 | /* |
63 | * Try to find host. |
64 | */ |
65 | addr = sk_namelookup(host, realhost); |
66 | if ( (err = sk_addr_error(addr)) ) |
67 | return err; |
68 | |
69 | if (port < 0) |
70 | port = 513; /* default rlogin port */ |
71 | |
72 | /* |
73 | * Open socket. |
74 | */ |
75 | s = sk_new(addr, port, 1, rlogin_receive); |
76 | if ( (err = sk_socket_error(s)) ) |
77 | return err; |
78 | |
79 | sk_addr_free(addr); |
80 | |
81 | /* |
82 | * Send local username, remote username, terminal/speed |
83 | */ |
84 | |
85 | { |
86 | char z = 0; |
87 | char *p; |
88 | sk_write(s, &z, 1); |
89 | sk_write(s, cfg.localusername, strlen(cfg.localusername)); |
90 | sk_write(s, &z, 1); |
91 | sk_write(s, cfg.username, strlen(cfg.username)); |
92 | sk_write(s, &z, 1); |
93 | sk_write(s, cfg.termtype, strlen(cfg.termtype)); |
94 | sk_write(s, "/", 1); |
95 | for(p = cfg.termspeed; isdigit(*p); p++); |
96 | sk_write(s, cfg.termspeed, p - cfg.termspeed); |
97 | sk_write(s, &z, 1); |
98 | } |
99 | |
100 | begin_session(); |
101 | |
102 | return NULL; |
103 | } |
104 | |
105 | /* |
106 | * Called to send data down the rlogin connection. |
107 | */ |
108 | static void rlogin_send (char *buf, int len) { |
109 | |
110 | if (s == NULL) |
111 | return; |
112 | |
113 | sk_write(s, buf, len); |
114 | } |
115 | |
116 | /* |
117 | * Called to set the size of the window |
118 | */ |
119 | static void rlogin_size(void) { |
e418595a |
120 | char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 }; |
c91409da |
121 | |
122 | b[6] = cols >> 8; b[7] = cols & 0xFF; |
123 | b[4] = rows >> 8; b[5] = rows & 0xFF; |
124 | sk_write(s, b, 12); |
125 | return; |
126 | } |
127 | |
128 | /* |
129 | * Send rlogin special codes. |
130 | */ |
131 | static void rlogin_special (Telnet_Special code) { |
132 | /* Do nothing! */ |
133 | return; |
134 | } |
135 | |
136 | static Socket rlogin_socket(void) { return s; } |
137 | |
138 | static int rlogin_sendok(void) { return 1; } |
139 | |
140 | Backend rlogin_backend = { |
141 | rlogin_init, |
142 | rlogin_send, |
143 | rlogin_size, |
144 | rlogin_special, |
145 | rlogin_socket, |
146 | rlogin_sendok, |
147 | 1 |
148 | }; |