-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientConnection.cpp
More file actions
561 lines (377 loc) · 16.2 KB
/
ClientConnection.cpp
File metadata and controls
561 lines (377 loc) · 16.2 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//****************************************************************************
// REDES Y SISTEMAS DISTRIBUIDOS
//
// 2ª de grado de Ingeniería Informática
//
// Clase que atiende una petición FTP.
//
//****************************************************************************
// COMANDOS IMPLEMENTADOS
//
// Línea 136 USER nombre de usuario
// " " 153 PASS contraseña
// " " 170 SYST información del sistema
// " " 182 PWD ubicación actual
// " " 198 CWD cambiar de directorio
// " " 223 RNFR renombrar archivo
// " " 237 RNTO renombrar archivo
// " " 254 DELE eliminar archivo
// " " 271 MKD crear directorio
// " " 288 RMD eliminar directorio
// " " 305 LIST mostrar archivos
// " " 348 TYPE tipo de conexión
// " " 372 PORT modo activo
// " " 395 PASV modo pasivo
// " " 438 RETR descargar archivo del servidor
// " " 480 STORE subir archivo al servidor
// " " 523 QUIT salir
//****************************************************************************
#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 <sys/stat.h>
#include <iostream>
#include <dirent.h>
#include <unistd.h>
#include "common.h"
#include "ClientConnection.h"
#define COMMAND(cmd) strcmp(command, cmd)==0
int connect_TCP(uint32_t address, uint16_t port){
struct sockaddr_in sin;
int s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s<0)
errexit("No se ha podido crear el socket: %s\n", strerror(errno));
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = address;
sin.sin_port = htons(port);
if (connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
errexit("No se ha podido conectar con %s: %s\n", address, strerror(errno));
return s;
}
int Random(int start, int end)
{
return start+(int)(rand()%(end-start));
}
ClientConnection::ClientConnection(int s, unsigned long ip){
int sock = (int)(s);
char buffer[MAX_BUFF];
control_socket = s;
server_address = ip;
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;
}
ClientConnection::~ClientConnection(){
fclose(fd);
close(control_socket);
}
// Esta función es la que atiende las peticiones
// Aquí es donde se implementan las acciones asociadas a los comandos.
// Véase el ejemplo del comando USER.
// Si considera que debe añadir algún otro comando siéntase libre
// de hacerlo. Asimismo, puede añadir tantos métodos auxiliares como
// sea necesario.
void ClientConnection::WaitForRequests(){
if (!ok)
return;
fprintf(fd, "220 Service ready.\n");
printf("\n**************\n* USER: pepe *\n* PASS: 1234 *\n**************\n\n");
while(!parar){
fscanf(fd, "%s", command);
//***************************************************************//
// USER //
//***************************************************************//
if (COMMAND("USER")){
fscanf(fd, "%s", arg);
printf("(USER):%s\n", arg);
if (!strcmp(arg,"pepe"))
fprintf(fd, "331 User name ok, need password.\n");
else
fprintf(fd, "332 Need account for login.\n");
}
//***************************************************************//
// PASS //
//***************************************************************//
else if (COMMAND("PASS")){
fscanf(fd, "%s", arg);
printf("(PASS):%s\n", arg);
if (!strcmp(arg,"1234"))
fprintf(fd, "230 User logged in, proceed.\n");
else
fprintf(fd, "530 Not logged in.\n");
}
//***************************************************************//
// SYST //
//***************************************************************//
else if (COMMAND("SYST")){
printf("(SYST):SHOW\n");
fprintf(fd, "215 UNIX Type: L8.\n");
}
//***************************************************************//
// ṔWD //
//***************************************************************//
else if (COMMAND("PWD")){
printf("(PWD):SHOW\n");
char path[MAX_BUFF];
if (getcwd(path, sizeof(path)) != NULL)
fprintf(fd, "257 \"%s\" \n", path);
}
//***************************************************************//
// CWD (cd) //
//***************************************************************//
else if (COMMAND("CWD")){
fscanf(fd, "%s", arg);
printf("(CWD):%s\n", arg);
char path[MAX_BUFF];
if (getcwd(path, sizeof(path)) != NULL){
strcat(path,"/");
strcat(path,arg);
if (chdir(path) < 0)
fprintf(fd, "550 Failed to change directory.\n");
else
fprintf (fd, "250 Directory successfully changed.\n");
}
}
//***************************************************************//
// RNFR (rename) //
//***************************************************************//
else if (COMMAND("RNFR")){
fscanf(fd, "%s", arg);
printf("(RNFR):%s\n", arg);
fprintf(fd, "350 Requested file action pending further information.\n");
}
//***************************************************************//
// RNTO (rename) //
//***************************************************************//
else if (COMMAND("RNTO")){
fscanf(fd, "%s", arg2);
printf("(RNTO):%s\n", arg2);
if (rename(arg,arg2) < 0)
fprintf(fd, "550 Failed to rename file.\n");
else
fprintf (fd, "250 File successfully renamed.\n");
}
//***************************************************************//
// DELE (del) //
//***************************************************************//
else if (COMMAND("DELE")){
fscanf(fd, "%s", arg);
printf("(DELE):%s\n", arg);
if (remove(arg) < 0)
fprintf(fd, "550 Failed to remove file.\n");
else
fprintf (fd, "250 File successfully removed.\n");
}
//***************************************************************//
// MKD (mkdir) //
//***************************************************************//
else if (COMMAND("MKD")){
fscanf(fd, "%s", arg);
printf("(MKD):%s\n", arg);
if (mkdir(arg, 0755) < 0)
fprintf(fd, "550 Failed to create directory.\n");
else
fprintf (fd, "257 Directory successfully created.\n");
}
//***************************************************************//
// RMD (rmdir) //
//***************************************************************//
else if (COMMAND("RMD")){
fscanf(fd, "%s", arg);
printf("(RMD):%s\n", arg);
if (rmdir(arg) < 0)
fprintf(fd, "550 Failed to remove directory.\n");
else
fprintf (fd, "250 Directory successfully removed.\n");
}
//***************************************************************//
// LIST (ls) //
//***************************************************************//
else if (COMMAND("LIST")){
printf("(LIST):SHOW\n");
fprintf(fd, "125 Data connection already open; transfer starting\n");
struct sockaddr_in sa;
socklen_t sa_len = sizeof(sa);
char buffer[MAX_BUFF];
std::string mostrar;
std::string ls = "ls -l";
ls.append(" 2>&1");
FILE* file = popen(ls.c_str(), "r");
if (!file){
fprintf(fd, "450 Requested file action not taken. File unavaible.\n");
close(data_socket);
}
else{
if (passive)
data_socket = accept(data_socket,(struct sockaddr *)&sa, &sa_len);
while (!feof(file))
if (fgets(buffer, MAX_BUFF, file) != NULL)
mostrar.append(buffer);
send(data_socket, mostrar.c_str(), mostrar.size(), 0);
fprintf(fd, "250 Closing data connection. Requested file action successful.\n");
pclose(file);
close(data_socket);
}
}
//***************************************************************//
// TYPE //
//***************************************************************//
else if (COMMAND("TYPE")){
fscanf(fd, "%s", arg);
printf("(TYPE):%s\n", 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");
}
//***************************************************************//
// PORT //
//***************************************************************//
else if (COMMAND("PORT")){
passive = false;
unsigned int ip[4];
unsigned int port[2];
fscanf(fd, "%d,%d,%d,%d,%d,%d", &ip[0], &ip[1], &ip[2], &ip[3], &port[0], &port[1]);
uint32_t ip_addr = ip[3]<<24 | ip[2]<<16 | ip[1]<<8 | ip[0];
uint16_t port_v = port[0] << 8 | port[1];
data_socket = connect_TCP(ip_addr,port_v);
fprintf(fd, "200 Okey\n");
}
//***************************************************************//
// PASV (pass) //
//***************************************************************//
else if (COMMAND("PASV")){
passive = true;
struct sockaddr_in sin, sa;
socklen_t sa_len = sizeof(sa);
int s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s<0)
errexit("No se ha podido crear el socket: %s\n", strerror(errno));
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);//server_address;//htonl(INADDR_ANY);
srand((unsigned int) time(NULL));
sin.sin_port = Random(1024, 65535);
if (bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
errexit("No se ha podido hacer bind con el puerto: %s\n", strerror(errno));
if (listen(s,5) < 0)
errexit("Fallo en listen: %s\n", strerror(errno));
getsockname(control_socket, (struct sockaddr *)&sa, &sa_len);
//getsockname(s, (struct sockaddr *)&sa, &sa_len);
fprintf(fd, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d).\n",
(unsigned int)(sa.sin_addr.s_addr & 0xff),
(unsigned int)((sa.sin_addr.s_addr >> 8) & 0xff),
(unsigned int)((sa.sin_addr.s_addr >> 16) & 0xff),
(unsigned int)((sa.sin_addr.s_addr >> 24) & 0xff),
(unsigned int)(sin.sin_port & 0xff),
(unsigned int)(sin.sin_port >> 8));
data_socket = s;
}
//***************************************************************//
// RETR (get) //
//***************************************************************//
else if (COMMAND("RETR")){
fscanf(fd, "%s", arg);
printf("(RETR):%s\n", arg);
FILE* file = fopen(arg,"rb");
if (!file){
fprintf(fd, "450 Requested file action not taken. File unavaible.\n");
close(data_socket);
}
else{
printf("(RETR)11111:\n");
fprintf(fd, "150 File status okay; about to open data connection.\n");
printf("(RETR)22222:\n");
struct sockaddr_in sa;
socklen_t sa_len = sizeof(sa);
char buffer[MAX_BUFF];
int n;
if (passive)
data_socket = accept(data_socket,(struct sockaddr *)&sa, &sa_len);
do{
n = fread(buffer, sizeof(char), MAX_BUFF, file);
send(data_socket, buffer, n, 0);
} while (n == MAX_BUFF);
fprintf(fd,"226 Closing data connection. Requested file action successful.\n");
fclose(file);
close(data_socket);
}
}
//***************************************************************//
// STOR (put) //
//***************************************************************//
else if (COMMAND("STOR")){
fscanf(fd, "%s", arg);
printf("(STOR):%s\n", arg);
FILE* file = fopen(arg,"wb");
if (!file){
fprintf(fd, "450 Requested file action not taken. File unavaible.\n");
close(data_socket);
}
else{
fprintf(fd, "150 File status okay; about to open data connection.\n");
fflush(fd);
struct sockaddr_in sa;
socklen_t sa_len = sizeof(sa);
char buffer[MAX_BUFF];
int n;
if (passive)
data_socket = accept(data_socket,(struct sockaddr *)&sa, &sa_len);
do{
n = recv(data_socket, buffer, MAX_BUFF, 0);
fwrite(buffer, sizeof(char) , n, file);
} while (n == MAX_BUFF);
fprintf(fd,"226 Closing data connection. Requested file action successful.\n");
fclose(file);
close(data_socket);
}
}
//***************************************************************//
// QUIT //
//***************************************************************//
else if (COMMAND("QUIT")){
fprintf(fd, "221 Service closing control connection.\n");
stop();
}
else{
fprintf(fd, "502 Command not implemented.\n");
fflush(fd);
printf("Comando: %s | Argumentos: %s\n", command, arg);
printf("Error interno del servidor\n");
}
}
fclose(fd);
return;
}
void ClientConnection::stop(){
close(data_socket);
close(control_socket);
parar = true;
}