Created a shiny new abstraction for the socket handling. Has many
[u/mdw/putty] / putty.h
1 #ifndef PUTTY_PUTTY_H
2 #define PUTTY_PUTTY_H
3
4 #include "network.h"
5
6 #define PUTTY_REG_POS "Software\\SimonTatham\\PuTTY"
7 #define PUTTY_REG_PARENT "Software\\SimonTatham"
8 #define PUTTY_REG_PARENT_CHILD "PuTTY"
9 #define PUTTY_REG_GPARENT "Software"
10 #define PUTTY_REG_GPARENT_CHILD "SimonTatham"
11
12 /*
13 * Global variables. Most modules declare these `extern', but
14 * window.c will do `#define PUTTY_DO_GLOBALS' before including this
15 * module, and so will get them properly defined.
16 */
17 #ifdef PUTTY_DO_GLOBALS
18 #define GLOBAL
19 #else
20 #define GLOBAL extern
21 #endif
22
23 #define ATTR_ACTCURS 0x80000000UL /* active cursor (block) */
24 #define ATTR_PASCURS 0x40000000UL /* passive cursor (box) */
25 #define ATTR_INVALID 0x20000000UL
26 #define ATTR_WRAPPED 0x10000000UL
27
28 #define LATTR_NORM 0x00000000UL
29 #define LATTR_WIDE 0x01000000UL
30 #define LATTR_TOP 0x02000000UL
31 #define LATTR_BOT 0x03000000UL
32 #define LATTR_MODE 0x03000000UL
33
34 #define ATTR_ASCII 0x00000000UL /* normal ASCII charset ESC ( B */
35 #define ATTR_GBCHR 0x00100000UL /* UK variant charset ESC ( A */
36 #define ATTR_LINEDRW 0x00200000UL /* line drawing charset ESC ( 0 */
37
38 #define ATTR_BOLD 0x00000100UL
39 #define ATTR_UNDER 0x00000200UL
40 #define ATTR_REVERSE 0x00000400UL
41 #define ATTR_BLINK 0x00000800UL
42 #define ATTR_FGMASK 0x0000F000UL
43 #define ATTR_BGMASK 0x000F0000UL
44 #define ATTR_FGSHIFT 12
45 #define ATTR_BGSHIFT 16
46
47 #define ATTR_DEFAULT 0x00098000UL
48 #define ATTR_DEFFG 0x00008000UL
49 #define ATTR_DEFBG 0x00090000UL
50 #define ATTR_CUR_XOR 0x000BA000UL
51 #define ERASE_CHAR (ATTR_DEFAULT | ' ')
52 #define ATTR_MASK 0xFFFFFF00UL
53 #define CHAR_MASK 0x000000FFUL
54
55 typedef HDC Context;
56 #define SEL_NL { 13, 10 }
57
58 GLOBAL int rows, cols, savelines;
59
60 GLOBAL int font_width, font_height;
61
62 #define INBUF_SIZE 2048
63 GLOBAL unsigned char inbuf[INBUF_SIZE];
64 GLOBAL int inbuf_head;
65
66 #define OUTBUF_SIZE 2048
67 #define OUTBUF_MASK (OUTBUF_SIZE-1)
68 GLOBAL unsigned char outbuf[OUTBUF_SIZE];
69 GLOBAL int outbuf_head, outbuf_reap;
70
71 GLOBAL int has_focus;
72
73 GLOBAL int app_cursor_keys, app_keypad_keys, vt52_mode;
74 GLOBAL int repeat_off, cr_lf_return;
75
76 GLOBAL int seen_key_event;
77 GLOBAL int seen_disp_event;
78
79 GLOBAL int session_closed;
80
81 GLOBAL char *logfile;
82
83 /*
84 * I've just looked in the windows standard headr files for WM_USER, there
85 * are hundreds of flags defined using the form WM_USER+123 so I've
86 * renumbered this NETEVENT value and the two in window.c
87 */
88 #define WM_XUSER (WM_USER + 0x2000)
89 #define WM_NETEVENT (WM_XUSER + 5)
90
91 typedef enum {
92 TS_AYT, TS_BRK, TS_SYNCH, TS_EC, TS_EL, TS_GA, TS_NOP, TS_ABORT,
93 TS_AO, TS_IP, TS_SUSP, TS_EOR, TS_EOF, TS_LECHO, TS_RECHO, TS_PING
94 } Telnet_Special;
95
96 typedef enum {
97 MB_NOTHING, MB_SELECT, MB_EXTEND, MB_PASTE
98 } Mouse_Button;
99
100 typedef enum {
101 MA_NOTHING, MA_CLICK, MA_2CLK, MA_3CLK, MA_DRAG, MA_RELEASE
102 } Mouse_Action;
103
104 typedef enum {
105 VT_XWINDOWS, VT_OEMANSI, VT_OEMONLY, VT_POORMAN
106 } VT_Mode;
107
108 typedef struct {
109 char *(*init) (char *host, int port, char **realhost);
110 void (*send) (char *buf, int len);
111 void (*size) (void);
112 void (*special) (Telnet_Special code);
113 Socket (*socket) (void);
114 int (*sendok) (void);
115 int default_port;
116 } Backend;
117
118 GLOBAL Backend *back;
119
120 extern struct backend_list {
121 int protocol;
122 char *name;
123 Backend *backend;
124 } backends[];
125
126 typedef struct {
127 void (*send) (char *buf, int len);
128 } Ldisc;
129
130 GLOBAL Ldisc *ldisc;
131
132 typedef struct {
133 /* Basic options */
134 char host[512];
135 int port;
136 enum { PROT_RAW, PROT_TELNET, PROT_SSH } protocol;
137 int close_on_exit;
138 int warn_on_close;
139 int ping_interval;
140 /* SSH options */
141 char remote_cmd[512];
142 int nopty;
143 int agentfwd;
144 enum { CIPHER_3DES, CIPHER_BLOWFISH, CIPHER_DES } cipher;
145 char keyfile[FILENAME_MAX];
146 int sshprot; /* use v1 or v2 when both available */
147 int buggymac; /* MAC bug commmercial <=v2.3.x SSH2 */
148 int try_tis_auth;
149 /* Telnet options */
150 char termtype[32];
151 char termspeed[32];
152 char environmt[1024]; /* VAR\tvalue\0VAR\tvalue\0\0 */
153 char username[32];
154 int rfc_environ;
155 /* Keyboard options */
156 int bksp_is_delete;
157 int rxvt_homeend;
158 int funky_type;
159 int app_cursor;
160 int app_keypad;
161 int nethack_keypad;
162 int alt_f4; /* is it special? */
163 int alt_space; /* is it special? */
164 int alt_only; /* is it special? */
165 int ldisc_term;
166 int scroll_on_key;
167 int scroll_on_disp;
168 int compose_key;
169 char wintitle[256]; /* initial window title */
170 /* Terminal options */
171 int savelines;
172 int dec_om;
173 int wrap_mode;
174 int lfhascr;
175 int blink_cur;
176 int beep;
177 int scrollbar;
178 int locksize;
179 int bce;
180 int blinktext;
181 int win_name_always;
182 int width, height;
183 char font[64];
184 int fontisbold;
185 int fontheight;
186 int fontcharset;
187 /* Colour options */
188 int try_palette;
189 int bold_colour;
190 unsigned char colours[22][3];
191 /* Selection options */
192 int mouse_is_xterm;
193 short wordness[256];
194 /* translations */
195 VT_Mode vtmode;
196 int xlat_enablekoiwin;
197 int xlat_88592w1250;
198 int xlat_88592cp852;
199 int xlat_capslockcyr;
200 } Config;
201
202 /*
203 * You can compile with -DSSH_DEFAULT to have ssh by default.
204 */
205 #ifndef SSH_DEFAULT
206 #define DEFAULT_PROTOCOL PROT_TELNET
207 #define DEFAULT_PORT 23
208 #else
209 #define DEFAULT_PROTOCOL PROT_SSH
210 #define DEFAULT_PORT 22
211 #endif
212
213 /*
214 * Some global flags denoting the type of application.
215 *
216 * FLAG_VERBOSE is set when the user requests verbose details.
217 *
218 * FLAG_STDERR is set in command-line applications (which have a
219 * functioning stderr that it makes sense to write to) and not in
220 * GUI applications (which don't).
221 *
222 * FLAG_INTERACTIVE is set when a full interactive shell session is
223 * being run, _either_ because no remote command has been provided
224 * _or_ because the application is GUI and can't run non-
225 * interactively.
226 */
227 #define FLAG_VERBOSE 0x0001
228 #define FLAG_STDERR 0x0002
229 #define FLAG_INTERACTIVE 0x0004
230 GLOBAL int flags;
231
232 GLOBAL Config cfg;
233 GLOBAL int default_protocol;
234 GLOBAL int default_port;
235
236 struct RSAKey; /* be a little careful of scope */
237
238 /*
239 * Exports from window.c.
240 */
241 void request_resize (int, int, int);
242 void do_text (Context, int, int, char *, int, unsigned long, int);
243 void set_title (char *);
244 void set_icon (char *);
245 void set_sbar (int, int, int);
246 Context get_ctx(void);
247 void free_ctx (Context);
248 void palette_set (int, int, int, int);
249 void palette_reset (void);
250 void write_clip (void *, int, int);
251 void get_clip (void **, int *);
252 void optimised_move (int, int, int);
253 void connection_fatal(char *, ...);
254 void fatalbox (char *, ...);
255 void beep (int);
256 void begin_session(void);
257 void sys_cursor(int x, int y);
258 #define OPTIMISE_IS_SCROLL 1
259
260 /*
261 * Exports from noise.c.
262 */
263 void noise_get_heavy(void (*func) (void *, int));
264 void noise_get_light(void (*func) (void *, int));
265 void noise_ultralight(DWORD data);
266 void random_save_seed(void);
267 void random_destroy_seed(void);
268
269 /*
270 * Exports from windlg.c.
271 */
272 void defuse_showwindow(void);
273 int do_config (void);
274 int do_reconfig (HWND);
275 void do_defaults (char *, Config *);
276 void logevent (char *);
277 void showeventlog (HWND);
278 void showabout (HWND);
279 void verify_ssh_host_key(char *host, int port, char *keytype,
280 char *keystr, char *fingerprint);
281 void registry_cleanup(void);
282
283 GLOBAL int nsessions;
284 GLOBAL char **sessions;
285
286 /*
287 * Exports from settings.c.
288 */
289 void save_settings (char *section, int do_host, Config *cfg);
290 void load_settings (char *section, int do_host, Config *cfg);
291 void get_sesslist(int allocate);
292
293 /*
294 * Exports from terminal.c.
295 */
296
297 void term_init (void);
298 void term_size (int, int, int);
299 void term_out (void);
300 void term_paint (Context, int, int, int, int);
301 void term_scroll (int, int);
302 void term_pwron (void);
303 void term_clrsb (void);
304 void term_mouse (Mouse_Button, Mouse_Action, int, int);
305 void term_deselect (void);
306 void term_update (void);
307 void term_invalidate(void);
308 void term_blink(int set_cursor);
309 void term_paste(void);
310 void term_nopaste(void);
311 void from_backend(int is_stderr, char *data, int len);
312
313 /*
314 * Exports from raw.c.
315 */
316
317 extern Backend raw_backend;
318
319 /*
320 * Exports from telnet.c.
321 */
322
323 extern Backend telnet_backend;
324
325 /*
326 * Exports from ssh.c.
327 */
328
329 extern int (*ssh_get_password)(const char *prompt, char *str, int maxlen);
330 extern Backend ssh_backend;
331
332 /*
333 * Exports from ldisc.c.
334 */
335
336 extern Ldisc ldisc_term, ldisc_simple;
337
338 /*
339 * Exports from sshrand.c.
340 */
341
342 void random_add_noise(void *noise, int length);
343 void random_init(void);
344 int random_byte(void);
345 void random_get_savedata(void **data, int *len);
346
347 /*
348 * Exports from misc.c.
349 */
350
351 /* #define MALLOC_LOG do this if you suspect putty of leaking memory */
352 #ifdef MALLOC_LOG
353 #define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z))
354 #define srealloc(y,z) (mlog(__FILE__,__LINE__), saferealloc(y,z))
355 #define sfree(z) (mlog(__FILE__,__LINE__), safefree(z))
356 void mlog(char *, int);
357 #else
358 #define smalloc safemalloc
359 #define srealloc saferealloc
360 #define sfree safefree
361 #endif
362
363 void *safemalloc(size_t);
364 void *saferealloc(void *, size_t);
365 void safefree(void *);
366
367 /*
368 * Exports from version.c.
369 */
370 extern char ver[];
371
372 /*
373 * Exports from sizetip.c.
374 */
375 void UpdateSizeTip(HWND src, int cx, int cy);
376 void EnableSizeTip(int bEnable);
377
378 /*
379 * Exports from xlat.c.
380 */
381 unsigned char xlat_kbd2tty(unsigned char c);
382 unsigned char xlat_tty2scr(unsigned char c);
383 unsigned char xlat_latkbd2win(unsigned char c);
384
385 /*
386 * Exports from mscrypto.c
387 */
388 #ifdef MSCRYPTOAPI
389 int crypto_startup();
390 void crypto_wrapup();
391 #endif
392
393 /*
394 * Exports from pageantc.c
395 */
396 void agent_query(void *in, int inlen, void **out, int *outlen);
397 int agent_exists(void);
398
399 /*
400 * A debug system.
401 */
402 #ifdef DEBUG
403 #include <stdarg.h>
404 #define debug(x) (dprintf x)
405 static void dprintf(char *fmt, ...) {
406 char buf[2048];
407 DWORD dw;
408 va_list ap;
409 static int gotconsole = 0;
410
411 if (!gotconsole) {
412 AllocConsole();
413 gotconsole = 1;
414 }
415
416 va_start(ap, fmt);
417 vsprintf(buf, fmt, ap);
418 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL);
419 va_end(ap);
420 }
421 #else
422 #define debug(x)
423 #endif
424
425 #endif