-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcmd.c
228 lines (202 loc) · 4.7 KB
/
rcmd.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* rcmd.c --- rcmd for winsock
*
* Time-stamp: <04/01/01 09:42:15 keuchel@lightning>
*
*/
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <errno.h>
#include "celib.h"
#include "cesocket.h"
#define bcopy(from, to, len) memcpy(to, from, len)
#define SENDFLAG 0
#define RECVFLAG 0
#define IPPORT_RESERVED 1024
static int wserrno;
SOCKET rresvport(int *p);
#ifdef UNDER_CE
#define wserror xcewserror
#endif
SOCKET
xcercmd (char **ahost, unsigned short rport, const char *locuser,
const char *remuser, const char *cmd, int *fd2p)
{
SOCKET s, timo = 1;
struct sockaddr_in sin, from;
char c;
int lport = IPPORT_RESERVED - 1;
struct hostent *hp;
int cc;
hp = gethostbyname (*ahost);
if (hp == 0)
{
wserror("%s", *ahost);
return (INVALID_SOCKET);
}
*ahost = hp->h_name;
for (;;)
{
s = rresvport (&lport);
if (s == INVALID_SOCKET)
{
if (wserrno == EAGAIN)
fprintf (stderr, "All ports in use\n");
else
wserror ("socket");
return (INVALID_SOCKET);
}
sin.sin_family = hp->h_addrtype;
bcopy (hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
sin.sin_port = rport;
/* when connected, exit loop */
if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) >= 0)
{
//printf("Connected\n");
break;
}
else
{
//printf("NotConnected: %d\n", WSAGetLastError());
}
wserrno = WSAGetLastError ();
closesocket (s);
if (wserrno == WSAEADDRINUSE)
{
lport--;
continue;
}
if (wserrno == WSAECONNREFUSED && timo <= 16)
{
Sleep (timo * 1000);
timo *= 2;
continue;
}
if (hp->h_addr_list[1] != NULL)
{
fprintf (stderr, "connect to address %s: ",
inet_ntoa (sin.sin_addr));
hp->h_addr_list++;
bcopy (hp->h_addr_list[0], &sin.sin_addr,
hp->h_length);
fprintf (stderr, "Trying %s...\n", inet_ntoa (sin.sin_addr));
continue;
}
WSASetLastError(wserrno);
return (INVALID_SOCKET);
}
/* we are connected */
lport--;
if (fd2p == 0)
{
send (s, "", 1, SENDFLAG);
lport = 0;
}
else
{
char num[8];
SOCKET s2 = rresvport (&lport), s3;
int len = sizeof (from);
if (s2 == INVALID_SOCKET)
goto bad;
listen (s2, 1);
sprintf (num, "%d", lport);
if (send (s, num, strlen (num) + 1, SENDFLAG) != strlen (num) + 1)
{
wserror ("rcmd:write: setting up stderr");
closesocket (s2);
goto bad;
}
s3 = accept (s2, (struct sockaddr *) &from, &len);
closesocket (s2);
if (s3 == INVALID_SOCKET)
{
wserror ("rcmd:accept failed");
lport = 0;
goto bad;
}
*fd2p = s3;
from.sin_port = ntohs ((u_short) from.sin_port);
if (from.sin_family != AF_INET ||
from.sin_port >= IPPORT_RESERVED)
{
fprintf (stderr,
"socket: protocol failure in circuit setup.\n");
goto bad2;
}
}
/* these are null terminated */
if((cc = send (s, locuser, strlen (locuser) + 1, SENDFLAG))
== SOCKET_ERROR || cc == 0)
wserror("rcmd: send");
send (s, remuser, strlen (remuser) + 1, SENDFLAG);
send (s, cmd, strlen (cmd) + 1, SENDFLAG);
if ((cc = recv (s, &c, 1, RECVFLAG)) != 1)
{
if(cc == 0)
fprintf(stderr, "Connection closed by foreign host\n");
else
wserror ("rcmd: recv");
goto bad2;
}
/* get one line from server */
if (c != 0)
{
while (recv (s, &c, 1, RECVFLAG) == 1)
{
#ifdef UNDER_CE
fprintf(stderr, "%c", c);
#else
write (2, &c, 1);
#endif
if (c == '\n')
break;
}
goto bad2;
}
/* return socket to caller */
return (s);
bad2:
if (lport)
closesocket (*fd2p);
bad:
closesocket (s);
return (INVALID_SOCKET);
}
/* the undocumented windows version of this function returns more than
an int in alport! */
static SOCKET
rresvport (int *alport)
{
struct sockaddr_in sin;
SOCKET s;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == INVALID_SOCKET)
{
wserrno = WSAGetLastError ();
return (INVALID_SOCKET);
}
for (;;)
{
sin.sin_port = htons ((u_short) * alport);
if (bind (s, (struct sockaddr *) &sin, sizeof (sin)) == 0)
return (s);
wserrno = WSAGetLastError ();
if (wserrno != WSAEADDRINUSE)
{
closesocket (s);
return (INVALID_SOCKET);
}
(*alport)--;
if (*alport == IPPORT_RESERVED / 2)
{
closesocket (s);
wserrno = EAGAIN;
return (INVALID_SOCKET);
}
}
}