| 1 | #include <windows.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <winsock.h> |
| 5 | |
| 6 | #include "putty.h" |
| 7 | |
| 8 | #ifndef FALSE |
| 9 | #define FALSE 0 |
| 10 | #endif |
| 11 | #ifndef TRUE |
| 12 | #define TRUE 1 |
| 13 | #endif |
| 14 | |
| 15 | static SOCKET s = INVALID_SOCKET; |
| 16 | |
| 17 | static void raw_size(void); |
| 18 | |
| 19 | static int sb_opt, sb_len; |
| 20 | static char *sb_buf = NULL; |
| 21 | static int sb_size = 0; |
| 22 | #define SB_DELTA 1024 |
| 23 | |
| 24 | static void try_write (void) { |
| 25 | while (outbuf_head != outbuf_reap) { |
| 26 | int end = (outbuf_reap < outbuf_head ? outbuf_head : OUTBUF_SIZE); |
| 27 | int len = end - outbuf_reap; |
| 28 | int ret; |
| 29 | |
| 30 | ret = send (s, outbuf+outbuf_reap, len, 0); |
| 31 | if (ret > 0) |
| 32 | outbuf_reap = (outbuf_reap + ret) & OUTBUF_MASK; |
| 33 | if (ret < len) |
| 34 | return; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static void s_write (void *buf, int len) { |
| 39 | unsigned char *p = buf; |
| 40 | while (len--) { |
| 41 | int new_head = (outbuf_head + 1) & OUTBUF_MASK; |
| 42 | if (new_head != outbuf_reap) { |
| 43 | outbuf[outbuf_head] = *p++; |
| 44 | outbuf_head = new_head; |
| 45 | } |
| 46 | } |
| 47 | try_write(); |
| 48 | } |
| 49 | |
| 50 | static void c_write (char *buf, int len) { |
| 51 | while (len--) |
| 52 | c_write1(*buf++); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Called to set up the raw connection. Will arrange for |
| 57 | * WM_NETEVENT messages to be passed to the specified window, whose |
| 58 | * window procedure should then call raw_msg(). |
| 59 | * |
| 60 | * Returns an error message, or NULL on success. |
| 61 | * |
| 62 | * Also places the canonical host name into `realhost'. |
| 63 | */ |
| 64 | static char *raw_init (HWND hwnd, char *host, int port, char **realhost) { |
| 65 | SOCKADDR_IN addr; |
| 66 | struct hostent *h; |
| 67 | unsigned long a; |
| 68 | |
| 69 | /* |
| 70 | * Try to find host. |
| 71 | */ |
| 72 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
| 73 | if ( (h = gethostbyname(host)) == NULL) |
| 74 | switch (WSAGetLastError()) { |
| 75 | case WSAENETDOWN: return "Network is down"; |
| 76 | case WSAHOST_NOT_FOUND: case WSANO_DATA: |
| 77 | return "Host does not exist"; |
| 78 | case WSATRY_AGAIN: return "Host not found"; |
| 79 | default: return "gethostbyname: unknown error"; |
| 80 | } |
| 81 | memcpy (&a, h->h_addr, sizeof(a)); |
| 82 | *realhost = h->h_name; |
| 83 | } else |
| 84 | *realhost = host; |
| 85 | a = ntohl(a); |
| 86 | |
| 87 | if (port < 0) |
| 88 | port = 23; /* default telnet port */ |
| 89 | |
| 90 | /* |
| 91 | * Open socket. |
| 92 | */ |
| 93 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 94 | if (s == INVALID_SOCKET) |
| 95 | switch (WSAGetLastError()) { |
| 96 | case WSAENETDOWN: return "Network is down"; |
| 97 | case WSAEAFNOSUPPORT: return "TCP/IP support not present"; |
| 98 | default: return "socket(): unknown error"; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Bind to local address. |
| 103 | */ |
| 104 | addr.sin_family = AF_INET; |
| 105 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 106 | addr.sin_port = htons(0); |
| 107 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
| 108 | switch (WSAGetLastError()) { |
| 109 | case WSAENETDOWN: return "Network is down"; |
| 110 | default: return "bind(): unknown error"; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Connect to remote address. |
| 115 | */ |
| 116 | addr.sin_addr.s_addr = htonl(a); |
| 117 | addr.sin_port = htons((short)port); |
| 118 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
| 119 | switch (WSAGetLastError()) { |
| 120 | case WSAENETDOWN: return "Network is down"; |
| 121 | case WSAECONNREFUSED: return "Connection refused"; |
| 122 | case WSAENETUNREACH: return "Network is unreachable"; |
| 123 | case WSAEHOSTUNREACH: return "No route to host"; |
| 124 | default: return "connect(): unknown error"; |
| 125 | } |
| 126 | |
| 127 | if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | |
| 128 | FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR) |
| 129 | switch (WSAGetLastError()) { |
| 130 | case WSAENETDOWN: return "Network is down"; |
| 131 | default: return "WSAAsyncSelect(): unknown error"; |
| 132 | } |
| 133 | |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Process a WM_NETEVENT message. Will return 0 if the connection |
| 139 | * has closed, or <0 for a socket error. |
| 140 | */ |
| 141 | static int raw_msg (WPARAM wParam, LPARAM lParam) { |
| 142 | int ret; |
| 143 | char buf[256]; |
| 144 | |
| 145 | /* |
| 146 | * Because reading less than the whole of the available pending |
| 147 | * data can generate an FD_READ event, we need to allow for the |
| 148 | * possibility that FD_READ may arrive with FD_CLOSE already in |
| 149 | * the queue; so it's possible that we can get here even with s |
| 150 | * invalid. If so, we return 1 and don't worry about it. |
| 151 | */ |
| 152 | if (s == INVALID_SOCKET) |
| 153 | return 1; |
| 154 | |
| 155 | if (WSAGETSELECTERROR(lParam) != 0) |
| 156 | return -WSAGETSELECTERROR(lParam); |
| 157 | |
| 158 | switch (WSAGETSELECTEVENT(lParam)) { |
| 159 | case FD_READ: |
| 160 | case FD_CLOSE: |
| 161 | ret = recv(s, buf, sizeof(buf), 0); |
| 162 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
| 163 | return 1; |
| 164 | if (ret < 0) /* any _other_ error */ |
| 165 | return -10000-WSAGetLastError(); |
| 166 | if (ret == 0) { |
| 167 | s = INVALID_SOCKET; |
| 168 | return 0; |
| 169 | } |
| 170 | c_write( buf, ret ); |
| 171 | return 1; |
| 172 | case FD_OOB: |
| 173 | do { |
| 174 | ret = recv(s, buf, sizeof(buf), 0); |
| 175 | c_write( buf, ret ); |
| 176 | } while (ret > 0); |
| 177 | do { |
| 178 | ret = recv(s, buf, 1, MSG_OOB); |
| 179 | } while (ret > 0); |
| 180 | if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) |
| 181 | return -30000-WSAGetLastError(); |
| 182 | return 1; |
| 183 | case FD_WRITE: |
| 184 | if (outbuf_head != outbuf_reap) |
| 185 | try_write(); |
| 186 | return 1; |
| 187 | } |
| 188 | return 1; /* shouldn't happen, but WTF */ |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Called to send data down the raw connection. |
| 193 | */ |
| 194 | static void raw_send (char *buf, int len) { |
| 195 | |
| 196 | if (s == INVALID_SOCKET) |
| 197 | return; |
| 198 | |
| 199 | s_write( buf, len ); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Called to set the size of the window |
| 204 | */ |
| 205 | static void raw_size(void) { |
| 206 | /* Do nothing! */ |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Send raw special codes. |
| 212 | */ |
| 213 | static void raw_special (Telnet_Special code) { |
| 214 | /* Do nothing! */ |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | static SOCKET raw_socket(void) { return s; } |
| 219 | |
| 220 | static int raw_sendok(void) { return 1; } |
| 221 | |
| 222 | Backend raw_backend = { |
| 223 | raw_init, |
| 224 | raw_msg, |
| 225 | raw_send, |
| 226 | raw_size, |
| 227 | raw_special, |
| 228 | raw_socket, |
| 229 | raw_sendok |
| 230 | }; |