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