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