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