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