Improve socket error handling so that a socket error isn't an
[u/mdw/putty] / raw.c
CommitLineData
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 14static Socket s = NULL;
5e1a8e27 15
5e1a8e27 16static void raw_size(void);
17
18static int sb_opt, sb_len;
19static char *sb_buf = NULL;
20static int sb_size = 0;
21#define SB_DELTA 1024
22
8df7a775 23static void c_write (char *buf, int len) {
24 from_backend(0, buf, len);
5e1a8e27 25}
26
8df7a775 27static int raw_receive (Socket s, int urgent, char *data, int len) {
2c94fd1c 28 if (urgent==3) {
29 /* A socket error has occurred. */
30 connection_fatal(data);
31 len = 0;
32 }
8df7a775 33 if (!len) {
34 /* Connection has closed. */
35 sk_close(s);
36 s = NULL;
37 return 0;
5e1a8e27 38 }
8df7a775 39 c_write(data, len);
40 return 1;
5e1a8e27 41}
42
43/*
8df7a775 44 * Called to set up the raw connection.
45 *
5e1a8e27 46 * Returns an error message, or NULL on success.
47 *
48 * Also places the canonical host name into `realhost'.
49 */
8df7a775 50static char *raw_init (char *host, int port, char **realhost) {
51 SockAddr addr;
52 char *err;
5e1a8e27 53
54 /*
55 * Try to find host.
56 */
8df7a775 57 addr = sk_namelookup(host, realhost);
58 if ( (err = sk_addr_error(addr)) )
59 return err;
5e1a8e27 60
61 if (port < 0)
62 port = 23; /* default telnet port */
63
64 /*
65 * Open socket.
66 */
c91409da 67 s = sk_new(addr, port, 0, raw_receive);
8df7a775 68 if ( (err = sk_socket_error(s)) )
69 return err;
5e1a8e27 70
8df7a775 71 sk_addr_free(addr);
5e1a8e27 72
6f34e365 73 /*
74 * We have no pre-session phase.
75 */
76 begin_session();
77
5e1a8e27 78 return NULL;
79}
80
81/*
5e1a8e27 82 * Called to send data down the raw connection.
83 */
84static void raw_send (char *buf, int len) {
85
8df7a775 86 if (s == NULL)
5e1a8e27 87 return;
88
8df7a775 89 sk_write(s, buf, len);
5e1a8e27 90}
91
92/*
93 * Called to set the size of the window
94 */
95static void raw_size(void) {
96 /* Do nothing! */
97 return;
98}
99
100/*
101 * Send raw special codes.
102 */
103static void raw_special (Telnet_Special code) {
104 /* Do nothing! */
105 return;
106}
107
8df7a775 108static Socket raw_socket(void) { return s; }
8ccc75b0 109
110static int raw_sendok(void) { return 1; }
4017be6d 111
5e1a8e27 112Backend raw_backend = {
113 raw_init,
5e1a8e27 114 raw_send,
115 raw_size,
4017be6d 116 raw_special,
8ccc75b0 117 raw_socket,
97db3be4 118 raw_sendok,
119 1
5e1a8e27 120};