-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientConnection.cpp
More file actions
399 lines (301 loc) · 11.6 KB
/
ClientConnection.cpp
File metadata and controls
399 lines (301 loc) · 11.6 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//****************************************************************************
// REDES Y SISTEMAS DISTRIBUIDOS
//
// 2ª de grado de Ingeniería Informática
//
// This class processes an FTP transactions.
// DAVID FERNANDO REDONDO DURAND
// alu0100851700@ull.edu.es
//
//****************************************************************************
#include <cstring>
#include <cstdarg>
#include <cstdio>
#include <cerrno>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <dirent.h>
#include "common.h"
#include "ClientConnection.h"
ClientConnection::ClientConnection(int s, unsigned long client_addr) {
printf("New ClientConnection\n");
int sock = (int)(s);
char buffer[MAX_BUFF];
control_socket = s;
// Check the Linux man pages to know what fdopen does.
fd = fdopen(s, "a+");
if (fd == NULL){
std::cout << "Connection closed" << std::endl;
fclose(fd);
close(control_socket);
ok = false;
return ;
}
ok = true;
data_socket = -1;
passive = false;
client_address = client_addr;
};
ClientConnection::~ClientConnection() {
fclose(fd);
close(control_socket);
}
int connect_TCP( uint32_t address, uint16_t port) {
// Implement your code to define a socket here
return -1; // You must return the socket descriptor.
}
void ClientConnection::stop() {
std::cout << "Stoping ClientConnection" << std::endl;
close(data_socket);
close(control_socket);
parar = true;
}
#define COMMAND(cmd) strcmp(command, cmd)==0
// This method processes the requests.
// Here you should implement the actions related to the FTP commands.
// See the example for the USER command.
// If you think that you have to add other commands feel free to do so. You
// are allowed to add auxiliary methods if necessary.
void ClientConnection::WaitForRequests() {
if (!ok) {
return;
}
fprintf(fd, "220 Service ready\n"); fflush(fd);
while(!parar) {
fscanf(fd, "%s", command);
///////////////////////////////////////////////////////////////////
///////////// USER /////////////
///////////////////////////////////////////////////////////////////
if (COMMAND("USER")) {
std::cout << "Setting Username" << std::endl;
fscanf(fd, "%s", arg);
fprintf(fd, "331 User name ok, need password\n"); fflush(fd);
}
///////////////////////////////////////////////////////////////////
///////////// PWD /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("PWD")) { //Print Directory
char path[MAX_BUFF];
if (getcwd(path, sizeof(path)) != NULL)
fprintf(fd, "257 \"%s\" \n", path);
}
///////////////////////////////////////////////////////////////////
///////////// PORT /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("PORT")) { //Port to be used in data connection
passive = false;
data_socket = socket(AF_INET, SOCK_STREAM, 0);
int port[2];
int ip[4];
fscanf(fd, "%d,%d,%d,%d,%d,%d",&ip[0],&ip[1],&ip[2],&ip[3],&port[0],&port[1]); //Leer campos h1,h2,h3,h4,p1,p2
char ip_decimal[40];
sprintf(ip_decimal, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); //Ip a decimal
int port_dec=port[0]*256+port[1]; //Puerto a decimal
printf("Remote Host %s:%d\n",ip_decimal,port_dec);
//Construir address
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ip_decimal);
sin.sin_port = htons(port_dec);
if (connect(data_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0){
std::cout << "No puedo hacer el connect con el puerto" << strerror(errno) << std::endl;
fprintf(fd, "421 No puedo hacer el connect con el puerto: %s\n", strerror(errno)); fflush(fd);
}
else{
std::cout << "Connection Established" << std::endl;
fprintf(fd, "200 OK Connection Established\n"); fflush(fd);
}
}
///////////////////////////////////////////////////////////////////
///////////// PASV (passive) /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("PASV")) { //Passive Mode
std::cout << "PASV" << std::endl;
int s = socket(AF_INET,SOCK_STREAM, 0);
if (s<0)
errexit("No se ha podido crear el socket: %s\n", strerror(errno));
struct sockaddr_in sin, sa;
socklen_t sa_len = sizeof(sa);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = client_address;
sin.sin_port = 0;
if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
errexit("No puedo hacer el bind con el puerto: %s\n", strerror(errno));
}
if (listen(s, 1) < 0)
errexit("Fallo en el listen: %s\n", strerror(errno));
getsockname(s, (struct sockaddr *)&sa, &sa_len);
fprintf(fd, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d).\n",
(unsigned int)(client_address & 0xff),
(unsigned int)((client_address >> 8) & 0xff),
(unsigned int)((client_address >> 16) & 0xff),
(unsigned int)((client_address >> 24) & 0xff),
(unsigned int)(sa.sin_port & 0xff),
(unsigned int)(sa.sin_port >> 8));
data_socket = s;
passive = true;
}
///////////////////////////////////////////////////////////////////
///////////// CWD (cd) /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("CWD")) { //CHANGE WORKING DIRECTORY
std::cout << "Changing working directory" << std::endl;
fscanf(fd, "%s", arg);
if(arg != NULL){
chdir(arg);
fprintf(fd,"250 Working directory changed to %s\n", arg); fflush(fd);
}
else{
fprintf(fd, "550 Failed to change directory.\n"); fflush(fd);
}
}
///////////////////////////////////////////////////////////////////
///////////// STOR (put) /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("STOR") ) { //Store File
std::cout << "STOR" << std::endl;
fscanf(fd, "%s", arg);
struct sockaddr_in fsin;
socklen_t alen;
std::ofstream file(arg);
char buffer[MAX_BUFF];
int n;
if(!file.is_open()){
fprintf(fd, "450 Requested file action not taken. File unavaible.\n");
std::cout << "File unavaible" << std::endl;
}
else{
fprintf(fd, "150 File status okay; about to open data connection.\n"); fflush(fd);
if(passive){
data_socket = accept(data_socket, (struct sockaddr *)&fsin, &alen);
}
do{
n = recv(data_socket, buffer, MAX_BUFF, 0); //Read MAX_BUFF bytes in data_socket and saves in buffer
file << buffer;
} while (n == MAX_BUFF);
fprintf(fd,"226 Closing data connection. Requested file action successful.\n");
file.close();
close(data_socket);
}
}
///////////////////////////////////////////////////////////////////
///////////// SYST /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("SYST")) { //System
fprintf(fd,"215 UNIX Type: L8.Remote system type is UNIX.Using binary mode to transfer files.\n"); fflush(fd);
}
///////////////////////////////////////////////////////////////////
///////////// TYPE /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("TYPE")) { //Specifies the representation type as described in the Section on Data Representation and Storage
fscanf(fd, "%s", arg);
if (!strcmp(arg,"A"))
fprintf(fd, "200 Switching to ASCII mode.\n");
else if (!strcmp(arg,"I"))
fprintf(fd, "200 Switching to Binary mode.\n");
else if (!strcmp(arg,"L"))
fprintf(fd, "200 Switching to Tenex mode.\n");
else
fprintf(fd, "501 Syntax error in parameters or arguments.\n");
}
///////////////////////////////////////////////////////////////////
///////////// RETR (get) /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("RETR")) {
std::cout << "RETR" << std::endl;
fscanf(fd, "%s", arg);
if(arg!=NULL){
std::ifstream file;
file.open(arg);
struct sockaddr_in fsin;
socklen_t alen;
if(passive)
data_socket = accept(data_socket, (struct sockaddr *)&fsin, &alen);
std::string line;
if (file.is_open()){
fprintf(fd,"150 File status okay; about to open data connection.\n\n");
while ( file.good() ) {
getline(file,line);
line += "\n";
send(data_socket, line.c_str(), strlen(line.c_str()), 0);
line = "";
}
file.close();
close(data_socket);
fprintf(fd,"226 Closing data connection. Requested file action successful.\n");//Cierra fichero
}
else
fprintf(fd,"450 Requested file action not taken. File unavaible.\n");
}
else
fprintf(fd,"500 Error.\n");
close(data_socket);
}
///////////////////////////////////////////////////////////////////
///////////// QUIT /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("QUIT")) {
std::cout << "Quiting connection" << std::endl;
fprintf(fd, "221 Connection closed by the FTP client\r\n\n"); fflush(fd);
stop();
}
///////////////////////////////////////////////////////////////////
///////////// LIST (ls) /////////////
///////////////////////////////////////////////////////////////////
else if (COMMAND("LIST")) {
std::cout << "Printing directory" << std::endl;
int c = fgetc(fd);
if (c != '\r')
fscanf(fd, "%s", arg);
else
strcpy(arg, "");
std::string command = "ls ";
command += arg;
command += " > lstemp.txt";
system(command.c_str());
std::ifstream lsfile;
lsfile.open("lstemp.txt");
struct sockaddr_in fsin;
socklen_t alen;
if(passive)
data_socket = accept(data_socket, (struct sockaddr *)&fsin, &alen);
std::string line;
if (lsfile.is_open()){
fprintf(fd,"125 List started OK.\n");
while ( lsfile.good() ) {
getline(lsfile,line);
line += "\n";
send(data_socket, line.c_str(), strlen(line.c_str()), 0);
}
lsfile.close();
close(data_socket);
fprintf(fd,"250 List completed successfully.\n");//Cierra fichero
system("rm lstemp.txt"); //Elimina fichero
}
else {
fprintf(fd,"500 Error.\n");
}
close(data_socket);
}
else {
fprintf(fd, "502 Command not implemented.\n"); fflush(fd);
printf("Comando : %s %s\n", command, arg);
printf("Error interno del servidor\n");
}
}
fclose(fd);
return;
};