-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTohCommon.c
More file actions
205 lines (188 loc) · 7.14 KB
/
TohCommon.c
File metadata and controls
205 lines (188 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (C) 2003 Fujio Nobori (toh@fuji-climb.org)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: TohCommon.c,v 1.7 2005/02/28 03:39:52 toh Exp $
*/
#include "PortForwarder_inc.h"
extern HANDLE g_hWnd;
/* Sleeps. Zzz... */
unsigned int sleep(unsigned int seconds)
{
Sleep(seconds * 1000);
return 0;
}
/* Gets error message from system. Seems not to work on WinCE?? */
char* GetErrMsg()
{
DWORD rtn;
LPVOID lpMsgBuf;
static BOOL firstTime = TRUE;
static char errMsg[4096];
if (firstTime) {
firstTime = FALSE;
strcpy(errMsg, "Something happens, but what?");
}
rtn = WSAGetLastError();
if (rtn == 0) {
/* sometimes what we need is the error before last.
* for example, in ssh_connect() in sshconnect.c,
* the real error is overwritten by close().
*/
return errMsg;
}
rtn = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL);
if (rtn) {
strcpy(errMsg, (LPTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
}
else {
strcpy(errMsg, TEXT("An error has been encountered."));
}
return errMsg;
}
#ifdef _PFPROXY_
BOOL isProxy = FALSE; /* TRUE = using proxy command. FALSE = not */
HANDLE hProxy; /* process handle of the proxy command */
HANDLE hWriteToProxy; /* pipe handle: PF --> proxy's stdin */
HANDLE hReadFromProxy; /* pipe handle: PF <-- proxy's stdout */
extern void fatal(const char *, ...);
/* pipedatalen returns actual length of data in the pipe from the proxy */
static DWORD pipedatalen(void)
{
DWORD len = 0;
if (!PeekNamedPipe(hReadFromProxy, NULL, 0, NULL, &len, NULL)) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
/* PIPE source is closed */
/* ReadFile() will detects EOF */
len = 1;
}
else {
fatal("PeekNamedPipe() failed, GetLastError()=%lu\n", GetLastError());
}
}
return len;
}
#endif /* _PFPROXY_ */
int write(SOCKET s, void *buf, int len)
{
#ifdef _PFPROXY_
if (isProxy && s == PIPE_OUT_DUMMY_SOCKET) { /* request for writing to proxy? */
DWORD n = 0;
if (WriteFile(hWriteToProxy, buf, (DWORD)len, &n, NULL))
return (int)n;
else
return -1;
}
#endif /* _PFPROXY_ */
return send(s, buf, len, 0);
}
int read(SOCKET s, void *buf, int len)
{
#ifdef _PFPROXY_
if (isProxy && s == PIPE_IN_DUMMY_SOCKET) { /* request for reading from proxy? */
DWORD n = 0, l = (DWORD)len;
if (l > 1) { /* if len == 1 then read one byte. */
DWORD m = pipedatalen(); /* otherwise read actually pipe containing data */
if (l > m) l = m; /* for preventing ReadFile() will be blocked. */
}
if (ReadFile(hReadFromProxy, buf, l, &n, NULL))
return (int)n;
else
return -1;
}
#endif /* _PFPROXY_ */
return recv(s, buf, len, 0);
}
int isatty(int handle)
{
// always false!
return 0;
}
#ifdef _PFPROXY_
/* myselect is pipe operation added select(). */
/* if not using proxy command, then only call real select(). */
/* otherwise... */
/* if rfds contains PIPE_IN_DUMMY_SOCKET then */
/* repeat 10ms-wait select() and peeking at the pipe. */
/* if select() returns positive and/or pipe contains data then return. */
/* if wfds contains PIPE_OUT_DUMMY_SOCKET then */
/* call once select() and peek once at the pipe. */
/* return positive because writing the pipe is always possible. */
int myselect(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds,
const struct timeval *timeout)
{
if (isProxy) {
BOOL rflag = rfds && FD_ISSET(PIPE_IN_DUMMY_SOCKET, rfds);
BOOL wflag = wfds && FD_ISSET(PIPE_OUT_DUMMY_SOCKET, wfds);
if (rflag || wflag) {
struct timeval tmo;
int nloop = timeout ? timeout->tv_sec * 100 : -1;
int r = 0, w = 0;
if (rflag) FD_CLR(PIPE_IN_DUMMY_SOCKET, rfds);
if (wflag) FD_CLR(PIPE_OUT_DUMMY_SOCKET, wfds);
for(;;) {
fd_set rs, *prs = rfds;
int ret;
tmo.tv_sec = 0;
tmo.tv_usec = 10*1000; /* 10 ms */
if (rfds) {
rs = *rfds;
prs = &rs;
}
ret = (select)(nfds, prs, wfds, efds, &tmo);
if (nloop > 0) nloop--;
if (rfds && (nloop == 0 || ret > 0)) *rfds = rs;
if (rflag && pipedatalen() > 0) {
*rfds = rs;
if (ret <= 0) FD_ZERO(rfds);
FD_SET(PIPE_IN_DUMMY_SOCKET, rfds);
r = 1;
}
if (wflag) {
if (ret <= 0) {
FD_ZERO(wfds);
if (!r)
FD_ZERO(rfds);
}
FD_SET(PIPE_OUT_DUMMY_SOCKET, wfds);
w = 1;
}
if (wflag || nloop == 0 || ret > 0 || r || w)
return ret >= 0 ? ret+r+w : r || w ? r+w : nloop == 0 ? 0 : ret;
}
}
}
return (select)(nfds, rfds, wfds, efds, timeout);
}
#endif /* _PFPROXY_ */