21 static SOCKET s
= INVALID_SOCKET
;
23 static void raw_size(void);
25 static int sb_opt
, sb_len
;
26 static char *sb_buf
= NULL
;
27 static int sb_size
= 0;
30 static void try_write (void) {
31 while (outbuf_head
!= outbuf_reap
) {
32 int end
= (outbuf_reap
< outbuf_head ? outbuf_head
: OUTBUF_SIZE
);
33 int len
= end
- outbuf_reap
;
36 ret
= send (s
, outbuf
+outbuf_reap
, len
, 0);
38 outbuf_reap
= (outbuf_reap
+ ret
) & OUTBUF_MASK
;
44 static void s_write (void *buf
, int len
) {
45 unsigned char *p
= buf
;
47 int new_head
= (outbuf_head
+ 1) & OUTBUF_MASK
;
48 if (new_head
!= outbuf_reap
) {
49 outbuf
[outbuf_head
] = *p
++;
50 outbuf_head
= new_head
;
56 static void c_write (char *buf
, int len
) {
62 * Called to set up the raw connection. Will arrange for
63 * WM_NETEVENT messages to be passed to the specified window, whose
64 * window procedure should then call raw_msg().
66 * Returns an error message, or NULL on success.
68 * Also places the canonical host name into `realhost'.
70 static char *raw_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
78 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
79 if ( (h
= gethostbyname(host
)) == NULL
)
80 switch (WSAGetLastError()) {
81 case WSAENETDOWN
: return "Network is down";
82 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
83 return "Host does not exist";
84 case WSATRY_AGAIN
: return "Host not found";
85 default: return "gethostbyname: unknown error";
87 memcpy (&a
, h
->h_addr
, sizeof(a
));
88 *realhost
= h
->h_name
;
94 port
= 23; /* default telnet port */
99 s
= socket(AF_INET
, SOCK_STREAM
, 0);
100 if (s
== INVALID_SOCKET
)
101 switch (WSAGetLastError()) {
102 case WSAENETDOWN
: return "Network is down";
103 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
104 default: return "socket(): unknown error";
108 * Bind to local address.
110 addr
.sin_family
= AF_INET
;
111 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
112 addr
.sin_port
= htons(0);
113 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
114 switch (WSAGetLastError()) {
115 case WSAENETDOWN
: return "Network is down";
116 default: return "bind(): unknown error";
120 * Connect to remote address.
122 addr
.sin_addr
.s_addr
= htonl(a
);
123 addr
.sin_port
= htons((short)port
);
124 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
125 switch (WSAGetLastError()) {
126 case WSAENETDOWN
: return "Network is down";
127 case WSAECONNREFUSED
: return "Connection refused";
128 case WSAENETUNREACH
: return "Network is unreachable";
129 case WSAEHOSTUNREACH
: return "No route to host";
130 default: return "connect(): unknown error";
133 if (hwnd
&& WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
|
134 FD_WRITE
| FD_OOB
| FD_CLOSE
) == SOCKET_ERROR
)
135 switch (WSAGetLastError()) {
136 case WSAENETDOWN
: return "Network is down";
137 default: return "WSAAsyncSelect(): unknown error";
141 * We have no pre-session phase.
149 * Process a WM_NETEVENT message. Will return 0 if the connection
150 * has closed, or <0 for a socket error.
152 static int raw_msg (WPARAM wParam
, LPARAM lParam
) {
157 * Because reading less than the whole of the available pending
158 * data can generate an FD_READ event, we need to allow for the
159 * possibility that FD_READ may arrive with FD_CLOSE already in
160 * the queue; so it's possible that we can get here even with s
161 * invalid. If so, we return 1 and don't worry about it.
163 if (s
== INVALID_SOCKET
) {
169 if (WSAGETSELECTERROR(lParam
) != 0)
170 return -WSAGETSELECTERROR(lParam
);
172 switch (WSAGETSELECTEVENT(lParam
)) {
175 ret
= recv(s
, buf
, sizeof(buf
), 0);
176 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
178 if (ret
< 0) { /* any _other_ error */
181 return -10000-WSAGetLastError();
191 ret
= recv(s
, buf
, sizeof(buf
), 0);
195 ret
= recv(s
, buf
, 1, MSG_OOB
);
197 if (ret
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
)
198 return -30000-WSAGetLastError();
201 if (outbuf_head
!= outbuf_reap
)
205 return 1; /* shouldn't happen, but WTF */
209 * Called to send data down the raw connection.
211 static void raw_send (char *buf
, int len
) {
213 if (s
== INVALID_SOCKET
)
220 * Called to set the size of the window
222 static void raw_size(void) {
228 * Send raw special codes.
230 static void raw_special (Telnet_Special code
) {
235 static SOCKET
raw_socket(void) { return s
; }
237 static int raw_sendok(void) { return 1; }
239 Backend raw_backend
= {