]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - rlogin.c
Rethink the whole line discipline architecture. Instead of having
[PuTTY.git] / rlogin.c
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 (urgent==3) {
29         /* A socket error has occurred. */
30         connection_fatal(data);
31         len = 0;
32     }
33     if (!len) {
34         /* Connection has closed. */
35         sk_close(s);
36         s = NULL;
37         return 0;
38     }
39     if (urgent == 2) {
40         char c;
41         
42         c = *data++; len--;
43         if (c == 0x80)
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     }
52     c_write(data, len);
53     return 1;
54 }
55
56 /*
57  * Called to set up the rlogin connection.
58  * 
59  * Returns an error message, or NULL on success.
60  *
61  * Also places the canonical host name into `realhost'.
62  */
63 static char *rlogin_init (char *host, int port, char **realhost) {
64     SockAddr addr;
65     char *err;
66
67     /*
68      * Try to find host.
69      */
70     addr = sk_namelookup(host, realhost);
71     if ( (err = sk_addr_error(addr)) )
72         return err;
73
74     if (port < 0)
75         port = 513;                    /* default rlogin port */
76
77     /*
78      * Open socket.
79      */
80     s = sk_new(addr, port, 1, rlogin_receive);
81     if ( (err = sk_socket_error(s)) )
82         return err;
83
84     sk_addr_free(addr);
85
86     /*
87      * Send local username, remote username, terminal/speed
88      */
89
90     {
91         char z = 0;
92         char *p;
93         sk_write(s, &z, 1);
94         sk_write(s, cfg.localusername, strlen(cfg.localusername));
95         sk_write(s, &z, 1);
96         sk_write(s, cfg.username, strlen(cfg.username));
97         sk_write(s, &z, 1);
98         sk_write(s, cfg.termtype, strlen(cfg.termtype));
99         sk_write(s, "/", 1);
100         for(p = cfg.termspeed; isdigit(*p); p++);
101         sk_write(s, cfg.termspeed, p - cfg.termspeed);
102         sk_write(s, &z, 1);
103     }
104
105     return NULL;
106 }
107
108 /*
109  * Called to send data down the rlogin connection.
110  */
111 static void rlogin_send (char *buf, int len) {
112
113     if (s == NULL)
114         return;
115
116     sk_write(s, buf, len);
117 }
118
119 /*
120  * Called to set the size of the window
121  */
122 static void rlogin_size(void) {
123     char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
124
125     b[6] = cols >> 8; b[7] = cols & 0xFF;
126     b[4] = rows >> 8; b[5] = rows & 0xFF;
127     sk_write(s, b, 12);
128     return;
129 }
130
131 /*
132  * Send rlogin special codes.
133  */
134 static void rlogin_special (Telnet_Special code) {
135     /* Do nothing! */
136     return;
137 }
138
139 static Socket rlogin_socket(void) { return s; }
140
141 static int rlogin_sendok(void) { return 1; }
142
143 static int rlogin_ldisc(int option) {
144     return 0;
145 }
146
147 Backend rlogin_backend = {
148     rlogin_init,
149     rlogin_send,
150     rlogin_size,
151     rlogin_special,
152     rlogin_socket,
153     rlogin_sendok,
154     rlogin_ldisc,
155     1
156 };