Repository navigation
Expand file tree
/
Copy pathUserPI.cpp
More file actions
356 lines (332 loc) · 8.06 KB
/
Copy pathUserPI.cpp
File metadata and controls
356 lines (332 loc) · 8.06 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* UserPI.cpp
*
* Created on: May 4, 2012
* Author: randy
*/
#include <sys/stat.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <boost/regex.hpp>
#include "UserPI.h"
#include "helpers.h"
#include "FTPcode.h"
using namespace boost;
#define TRANSFORM_PORT 52969
#define MAX_SOCK 10
#define MAX_LIST_LEN 100000
#define MAX_TELNET_REPLY 100000
#define MAX_TELNET_READ_TIME_US 200000
#define QUIT_WAIT_READ_TIMES 20
int UserPI::do_retr(string remotePath, string localPath) {
char buffer[MAX_TELNET_REPLY];
memset(buffer, 0, sizeof(buffer));
//TODO: activate mode will be supported in alpha 2.0
this->do_pasv();
string content = "RETR ";
content = content + remotePath;
if (this->telnetSend(content)) {
return -1;
}
this->dtp.setSockfd(this->transferSockfd);
if (this->dtp.getFile(localPath)) {
return -1;
}
return 0;
}
int UserPI::do_list(string remotePath) {
//this->listenTransferConnection();
//this->telnetSend("PORT 127,0,0,1,206,233");
this->do_pasv();
string content = "LIST " + remotePath;
this->telnetSend(content);
//this->acceptTransferConnection();
//this->dtp.setSockfd(this->transferSockfd);
char buffer[MAX_LIST_LEN];
memset(buffer, 0, sizeof(buffer));
int len = this->dtp.getFile(buffer);
if (len > 0) {
printf("%s\n", buffer);
} else {
printf("get list error\n");
return -1;
}
return 0;
}
void UserPI::setTelnetSockfd(int telnetSockfd) {
this->telnetSockfd = telnetSockfd;
}
int UserPI::telnetSend(string content) {
string temp = content;
temp = temp.append("\r\n");
if (write(this->telnetSockfd, temp.c_str(), temp.length()) < 0) {
printf("telnet send error\n");
return -1;
}
printf("Client Send: %s\n", content.c_str());
return 0;
}
int UserPI::telnetRead(char *buffer, int size) {
memset(buffer, 0, sizeof(buffer));
char tempBuffer[MAX_TELNET_REPLY];
memset(tempBuffer, 0, sizeof(tempBuffer));
int totalLen = 0;
int currentLen;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = MAX_TELNET_READ_TIME_US;
setsockopt(this->telnetSockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
while ((currentLen = read(this->telnetSockfd, tempBuffer, MAX_TELNET_REPLY))
> 0) {
if (currentLen + totalLen < size) {
strncpy(buffer + totalLen, tempBuffer, currentLen);
} else {
break;
}
totalLen += currentLen;
memset(tempBuffer, 0, sizeof(tempBuffer));
}
buffer[totalLen] = 0;
return totalLen;
}
int UserPI::do_user(string name) {
string content = "USER ";
content = content.append(name);
return this->telnetSend(content);
}
int UserPI::do_pass(string pwd) {
string content = "PASS ";
content = content.append(pwd);
return this->telnetSend(content);
}
int UserPI::do_syst() {
if (this->telnetSend("SYST")) {
return 1;
}
return 0;
}
int UserPI::do_port() {
//TODO:do_port will be supported in alpha 2.0
return 0;
}
int UserPI::do_pasv() {
// if (this->status.isIPV6) {
// this->telnetSend("EPSV");
// } else {
// this->telnetSend("PASV");
// }
this->telnetSend("EPSV");
char buffer[MAX_TELNET_REPLY];
memset(buffer, 0, sizeof(buffer));
int len;
while ((len = this->telnetRead(buffer, MAX_TELNET_REPLY)) == 0) {
//keep read from telnet
}
int count = 0;
int temp = 1;
printf("get pasv reply %s\n", buffer);
for (int i = len - 1; i >= 0; i--) {
bool digit = false;
if (buffer[i] >= 48 && buffer[i] <= 57) { //a digit
digit = true;
count += temp * (buffer[i] - 48);
temp *= 10;
}
if (!digit && count != 0) {
break;
}
}
//now connect the port
struct addrinfo hints, *res, *res0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int error = getaddrinfo(this->servAddr.c_str(), int2str(count).c_str(),
&hints, &res0);
if (error) {
printf("get addr %s error\n", this->servAddr.c_str());
return 1;
}
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int s = -1;
for (res = res0; res; res = res->ai_next) {
error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
if (error) {
printf("get addr %s error\n", this->servAddr.c_str());
return 1;
}
printf("transfer connection trying %s : %s...", hbuf, sbuf);
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
printf("failed\n");
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
close(s);
s = -1;
printf("failed\n");
continue;
}
//successfully
printf("successfully\n");
break;
}
if (s != -1) {
this->transferSockfd = s;
this->dtp.setSockfd(s);
return 0;
} else {
return 1;
}
}
void UserPI::setServdddr(string addr) {
this->servAddr = addr;
}
int UserPI::do_open(string addr, int port) {
struct sockaddr_in6 sin6;
memset(&sin6, 0, sizeof(sin6));
struct addrinfo hints, *res, *res0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int error = getaddrinfo(addr.c_str(), int2str(port).c_str(), &hints, &res0);
if (error) {
printf("get addr %s error\n", addr.c_str());
return 1;
}
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int s = -1;
for (res = res0; res; res = res->ai_next) {
error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
if (error) {
printf("get addr %s error\n", addr.c_str());
return 1;
}
printf("trying %s : %s...", hbuf, sbuf);
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
printf("failed\n");
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
close(s);
s = -1;
printf("failed\n");
continue;
}
//successfully
printf("successfully\n");
break;
}
if (s != -1) {
this->telnetSockfd = s;
this->servAddr = addr;
this->isClosed = false;
if (res->ai_protocol == AF_INET) {
sin6.sin6_family = AF_INET;
inet_pton(AF_INET, addr.c_str(), (void*) &sin6.sin6_addr);
} else {
sin6.sin6_family = AF_INET6;
inet_pton(AF_INET6, addr.c_str(), (void*) &sin6.sin6_addr);
}
sin6.sin6_port = htons(port);
status.setStatus(sin6);
if (!this->initUser()) {
return 0;
} else {
return -1;
}
} else {
return -1;
}
}
int UserPI::initUser() {
int len;
char buffer[MAX_TELNET_REPLY];
memset(buffer, 0, sizeof(buffer));
while ((len = this->telnetRead(buffer, MAX_TELNET_REPLY)) == 0) {
//do nothing, keep reading
}
printf("extract code %s\n", buffer);
int code = extractCode(buffer);
if (code != SERVICE_READY_FOR_NEW_USER) {
printf("service could not be opened\n");
return -1;
}
printf("%s\n", buffer);
string input;
//now request a user name
printf("Name : ");
getline(cin, input);
this->do_user(input);
while ((len = this->telnetRead(buffer, MAX_TELNET_REPLY)) == 0) {
//do nothing, keep reading
}
printf("%s\n", buffer);
//now request a password
printf("password : ");
getline(cin, input);
this->do_pass(input);
while ((len = this->telnetRead(buffer, MAX_TELNET_REPLY)) == 0) {
//do nothing, keep reading
}
printf("%s\n", buffer);
this->do_syst();
while ((len = this->telnetRead(buffer, MAX_TELNET_REPLY)) == 0) {
//do nothing, keep reading
}
printf("%s\n", buffer);
return 0;
}
int UserPI::do_close() {
this->telnetSend("QUIT");
char buf[MAX_TELNET_REPLY];
memset(buf, 0, sizeof(buf));
for (int i = 0; i < QUIT_WAIT_READ_TIMES; i++) {
int len = this->telnetRead(buf, MAX_TELNET_REPLY);
if (len > 0) {
printf("%s", buf);
break;
}
}
this->isClosed = true;
return 0;
}
UserPI::UserPI() {
this->telnetSockfd = -1;
this->transferSockfd = -1;
}
int UserPI::do_cwd(string path)
{
this->telnetSend("CWD " + path);
return 0;
}
int UserPI::do_stor(string pathName)
{
string fileName = this->getFileName(pathName);
this->do_pasv();
this->telnetSend("STOR " + pathName);
this->dtp.sendFile(pathName);
close(this->transferSockfd);
return 0;
}
string UserPI::getFileName(string pathName)
{
int pos = pathName.find_last_of("/");
if (pos == string::npos){
return pathName;
}else{
return pathName.substr(pos + 1, pathName.length() - pos);
}
}
UserPI::~UserPI() {
if (!this->isClosed) {
do_close();
}
}