Yet another attempt at OOB handling in the network abstraction. This
[u/mdw/putty] / raw.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 raw_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 raw_receive (Socket skt, int urgent, char *data, int len) {
28 if (urgent==3) {
29 /* A socket error has occurred. */
30 sk_close(s);
31 s = NULL;
32 connection_fatal(data);
33 len = 0;
34 return 0;
35 } else if (!len) {
36 /* Connection has closed. */
37 sk_close(s);
38 s = NULL;
39 return 0;
40 }
41 c_write(data, len);
42 return 1;
43 }
44
45 /*
46 * Called to set up the raw connection.
47 *
48 * Returns an error message, or NULL on success.
49 *
50 * Also places the canonical host name into `realhost'.
51 */
52 static char *raw_init (char *host, int port, char **realhost) {
53 SockAddr addr;
54 char *err;
55
56 /*
57 * Try to find host.
58 */
59 addr = sk_namelookup(host, realhost);
60 if ( (err = sk_addr_error(addr)) )
61 return err;
62
63 if (port < 0)
64 port = 23; /* default telnet port */
65
66 /*
67 * Open socket.
68 */
69 s = sk_new(addr, port, 0, 1, raw_receive);
70 if ( (err = sk_socket_error(s)) )
71 return err;
72
73 sk_addr_free(addr);
74
75 return NULL;
76 }
77
78 /*
79 * Called to send data down the raw connection.
80 */
81 static void raw_send (char *buf, int len) {
82
83 if (s == NULL)
84 return;
85
86 sk_write(s, buf, len);
87 }
88
89 /*
90 * Called to set the size of the window
91 */
92 static void raw_size(void) {
93 /* Do nothing! */
94 return;
95 }
96
97 /*
98 * Send raw special codes.
99 */
100 static void raw_special (Telnet_Special code) {
101 /* Do nothing! */
102 return;
103 }
104
105 static Socket raw_socket(void) { return s; }
106
107 static int raw_sendok(void) { return 1; }
108
109 static int raw_ldisc(int option) {
110 if (option == LD_EDIT || option == LD_ECHO)
111 return 1;
112 return 0;
113 }
114
115 Backend raw_backend = {
116 raw_init,
117 raw_send,
118 raw_size,
119 raw_special,
120 raw_socket,
121 raw_sendok,
122 raw_ldisc,
123 1
124 };