5e1a8e27 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
5e1a8e27 |
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 | |
8df7a775 |
14 | static Socket s = NULL; |
5e1a8e27 |
15 | |
5e1a8e27 |
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 | |
32874aea |
23 | static void c_write(char *buf, int len) |
24 | { |
8df7a775 |
25 | from_backend(0, buf, len); |
5e1a8e27 |
26 | } |
27 | |
32874aea |
28 | static int raw_closing(Plug plug, char *error_msg, int error_code, |
29 | int calling_back) |
30 | { |
7e78000d |
31 | sk_close(s); |
32 | s = NULL; |
33 | if (error_msg) { |
32874aea |
34 | /* A socket error has occurred. */ |
35 | connection_fatal(error_msg); |
36 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
37 | return 0; |
38 | } |
39 | |
32874aea |
40 | static int raw_receive(Plug plug, int urgent, char *data, int len) |
41 | { |
8df7a775 |
42 | c_write(data, len); |
43 | return 1; |
5e1a8e27 |
44 | } |
45 | |
46 | /* |
8df7a775 |
47 | * Called to set up the raw connection. |
48 | * |
5e1a8e27 |
49 | * Returns an error message, or NULL on success. |
50 | * |
6e1ebb76 |
51 | * Also places the canonical host name into `realhost'. It must be |
52 | * freed by the caller. |
5e1a8e27 |
53 | */ |
32874aea |
54 | static char *raw_init(char *host, int port, char **realhost) |
55 | { |
7e78000d |
56 | static struct plug_function_table fn_table = { |
57 | raw_closing, |
58 | raw_receive |
59 | }, *fn_table_ptr = &fn_table; |
60 | |
8df7a775 |
61 | SockAddr addr; |
62 | char *err; |
5e1a8e27 |
63 | |
64 | /* |
65 | * Try to find host. |
66 | */ |
8df7a775 |
67 | addr = sk_namelookup(host, realhost); |
32874aea |
68 | if ((err = sk_addr_error(addr))) |
8df7a775 |
69 | return err; |
5e1a8e27 |
70 | |
71 | if (port < 0) |
72 | port = 23; /* default telnet port */ |
73 | |
74 | /* |
75 | * Open socket. |
76 | */ |
7e78000d |
77 | s = sk_new(addr, port, 0, 1, &fn_table_ptr); |
32874aea |
78 | if ((err = sk_socket_error(s))) |
8df7a775 |
79 | return err; |
5e1a8e27 |
80 | |
8df7a775 |
81 | sk_addr_free(addr); |
5e1a8e27 |
82 | |
83 | return NULL; |
84 | } |
85 | |
86 | /* |
5e1a8e27 |
87 | * Called to send data down the raw connection. |
88 | */ |
32874aea |
89 | static void raw_send(char *buf, int len) |
90 | { |
5e1a8e27 |
91 | |
8df7a775 |
92 | if (s == NULL) |
5e1a8e27 |
93 | return; |
94 | |
8df7a775 |
95 | sk_write(s, buf, len); |
5e1a8e27 |
96 | } |
97 | |
98 | /* |
99 | * Called to set the size of the window |
100 | */ |
32874aea |
101 | static void raw_size(void) |
102 | { |
5e1a8e27 |
103 | /* Do nothing! */ |
104 | return; |
105 | } |
106 | |
107 | /* |
108 | * Send raw special codes. |
109 | */ |
32874aea |
110 | static void raw_special(Telnet_Special code) |
111 | { |
5e1a8e27 |
112 | /* Do nothing! */ |
113 | return; |
114 | } |
115 | |
32874aea |
116 | static Socket raw_socket(void) |
117 | { |
118 | return s; |
119 | } |
8ccc75b0 |
120 | |
32874aea |
121 | static int raw_sendok(void) |
122 | { |
123 | return 1; |
124 | } |
4017be6d |
125 | |
32874aea |
126 | static int raw_ldisc(int option) |
127 | { |
0965bee0 |
128 | if (option == LD_EDIT || option == LD_ECHO) |
32874aea |
129 | return 1; |
0965bee0 |
130 | return 0; |
131 | } |
132 | |
5e1a8e27 |
133 | Backend raw_backend = { |
134 | raw_init, |
5e1a8e27 |
135 | raw_send, |
136 | raw_size, |
4017be6d |
137 | raw_special, |
8ccc75b0 |
138 | raw_socket, |
97db3be4 |
139 | raw_sendok, |
0965bee0 |
140 | raw_ldisc, |
97db3be4 |
141 | 1 |
5e1a8e27 |
142 | }; |