15 static SOCKET s
= INVALID_SOCKET
;
17 #define iswritable(x) ( (x) != IAC && (x) != CR )
19 static void raw_size(void);
21 static int sb_opt
, sb_len
;
22 static char *sb_buf
= NULL
;
23 static int sb_size
= 0;
26 static void try_write (void) {
27 while (outbuf_head
!= outbuf_reap
) {
28 int end
= (outbuf_reap
< outbuf_head ? outbuf_head
: OUTBUF_SIZE
);
29 int len
= end
- outbuf_reap
;
32 ret
= send (s
, outbuf
+outbuf_reap
, len
, 0);
34 outbuf_reap
= (outbuf_reap
+ ret
) & OUTBUF_MASK
;
40 static void s_write (void *buf
, int len
) {
41 unsigned char *p
= buf
;
43 int new_head
= (outbuf_head
+ 1) & OUTBUF_MASK
;
44 if (new_head
!= outbuf_reap
) {
45 outbuf
[outbuf_head
] = *p
++;
46 outbuf_head
= new_head
;
52 static void c_write (char *buf
, int len
) {
54 int new_head
= (inbuf_head
+ 1) & INBUF_MASK
;
55 int c
= (unsigned char) *buf
;
56 if (new_head
!= inbuf_reap
) {
57 inbuf
[inbuf_head
] = *buf
++;
58 inbuf_head
= new_head
;
64 * Called to set up the raw connection. Will arrange for
65 * WM_NETEVENT messages to be passed to the specified window, whose
66 * window procedure should then call raw_msg().
68 * Returns an error message, or NULL on success.
70 * Also places the canonical host name into `realhost'.
72 static char *raw_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
80 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
81 if ( (h
= gethostbyname(host
)) == NULL
)
82 switch (WSAGetLastError()) {
83 case WSAENETDOWN
: return "Network is down";
84 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
85 return "Host does not exist";
86 case WSATRY_AGAIN
: return "Host not found";
87 default: return "gethostbyname: unknown error";
89 memcpy (&a
, h
->h_addr
, sizeof(a
));
90 *realhost
= h
->h_name
;
96 port
= 23; /* default telnet port */
101 s
= socket(AF_INET
, SOCK_STREAM
, 0);
102 if (s
== INVALID_SOCKET
)
103 switch (WSAGetLastError()) {
104 case WSAENETDOWN
: return "Network is down";
105 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
106 default: return "socket(): unknown error";
110 * Bind to local address.
112 addr
.sin_family
= AF_INET
;
113 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
114 addr
.sin_port
= htons(0);
115 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
116 switch (WSAGetLastError()) {
117 case WSAENETDOWN
: return "Network is down";
118 default: return "bind(): unknown error";
122 * Connect to remote address.
124 addr
.sin_addr
.s_addr
= htonl(a
);
125 addr
.sin_port
= htons((short)port
);
126 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
127 switch (WSAGetLastError()) {
128 case WSAENETDOWN
: return "Network is down";
129 case WSAECONNREFUSED
: return "Connection refused";
130 case WSAENETUNREACH
: return "Network is unreachable";
131 case WSAEHOSTUNREACH
: return "No route to host";
132 default: return "connect(): unknown error";
135 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
|
136 FD_WRITE
| FD_OOB
| FD_CLOSE
) == SOCKET_ERROR
)
137 switch (WSAGetLastError()) {
138 case WSAENETDOWN
: return "Network is down";
139 default: return "WSAAsyncSelect(): unknown error";
146 * Process a WM_NETEVENT message. Will return 0 if the connection
147 * has closed, or <0 for a socket error.
149 static int raw_msg (WPARAM wParam
, LPARAM lParam
) {
153 if (s
== INVALID_SOCKET
) /* how the hell did we get here?! */
156 if (WSAGETSELECTERROR(lParam
) != 0)
157 return -WSAGETSELECTERROR(lParam
);
159 switch (WSAGETSELECTEVENT(lParam
)) {
161 ret
= recv(s
, buf
, sizeof(buf
), 0);
162 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
164 if (ret
< 0) /* any _other_ error */
165 return -10000-WSAGetLastError();
168 return 0; /* can't happen, in theory */
174 ret
= recv(s
, buf
, sizeof(buf
), 0);
178 ret
= recv(s
, buf
, 1, MSG_OOB
);
180 if (ret
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
)
181 return -30000-WSAGetLastError();
184 if (outbuf_head
!= outbuf_reap
)
191 return 1; /* shouldn't happen, but WTF */
195 * Called to send data down the raw connection.
197 static void raw_send (char *buf
, int len
) {
199 if (s
== INVALID_SOCKET
)
206 * Called to set the size of the window
208 static void raw_size(void) {
214 * Send raw special codes.
216 static void raw_special (Telnet_Special code
) {
221 Backend raw_backend
= {