-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_process.c
executable file
·442 lines (352 loc) · 10.5 KB
/
client_process.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
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
#include <errno.h>
#include <sys/select.h>
//#include <unistd.h>
//#include <stdint.h>
#include <arpa/inet.h>
//#include <openssl/ssl.h>
//#include <openssl/err.h>
#include "client_process.h"
#include "ftpclient.h"
#define BUF_MAX 10
//int handle_server_ack(int sockfd, SSL *ssl);
//int handle_user_input( char *user_input, int sockfd, SSL *ssl);
//void client_process(int sockfd, SSL *ssl)
void client_process()
{
fd_set fs_read;
int max_sock;
//char buf[255];
int ret;
max_sock = sockfd > STDIN_FILENO ? sockfd : STDIN_FILENO;
// call login function login to server
//login(sockfd, ssl);
//printf("If you don't know do anything, please type 'help', it can help you :)!\n");
//while(1){
FD_ZERO( &fs_read);
FD_SET( STDIN_FILENO, &fs_read);
FD_SET( sockfd, &fs_read);
if ( select ( max_sock+1, &fs_read, NULL, NULL, NULL) <=0 ){
//continue;
}
if ( FD_ISSET( sockfd, &fs_read ) ){
ret = handle_server_ack(sockfd, ssl);
if ( SOCK_ERROR == ret ){
//break;
}
}
#if 0
if ( FD_ISSET( STDIN_FILENO, &fs_read ) ){
memset(buf, 0 ,255);
fgets( buf, 255, stdin);
buf[strlen(buf)-1] = '\0';//delete '\n'
ret = handle_user_input(buf, sockfd, ssl);
if ( SOCK_ERROR == ret ){
break;
}
}
#endif
// }
}
//int handle_server_ack(int sockfd, SSL *ssl)
char* handle_server_ack()
{
int ret;
uint32_t total_len;
msg_head_ctrl_t *msg;
#if 0
ret = recv( sockfd, &body_len , sizeof(body_len), MSG_PEEK);
if ( ret<=0 ){
printf("connectionn lost ... (handle_server1)\n");
exit(-1);
}
#endif
total_len = MAX_READ_BYTES + sizeof(msg_head_ctrl_t);
msg = malloc( total_len);
if ( NULL == msg ){
perror("out of memeory\n");
//return FAILED;
}
memset( msg, 0, total_len);
ret = SSL_read( ssl, msg , total_len);
//ret = read(sockfd, msg, total_len);
if ( ret<=0 ){
printf("connectionn lost ... (handle_server2)\n");
exit(-1);
}
msg->body_len = ntohl(msg->body_len);
msg->command = ntohl(msg->command);
if ( (COMMAND_LS==msg->command) || (COMMAND_PWD==msg->command) ){
printf("\n%s\n",msg->msg_body);
}
else if ( COMMAND_GET == msg->command ){
save_file_to_disk(msg->msg_body);
}
char *tmp;
tmp = malloc(msg->body_len+1);
memset(tmp, 0, msg->body_len);
//memcpy(tmp, msg->msg_body, sizeof(msg->msg_body));
sprintf(tmp, "%s", msg->msg_body);
free(msg);
//printf("%s\n", tmp);
return tmp;
//return SUCCESSFUL;
}
//int send_command_to_server( int sockfd, SSL *ssl, char *user_input, command_type_t type)
int send_command_to_server( char *user_input, command_type_t type)
{
ssize_t ret;
msg_head_ctrl_t *msg;
int body_len;
int total_len;
body_len = strlen(user_input);
total_len = body_len + sizeof(msg_head_ctrl_t);
msg = malloc(total_len);
if ( NULL == msg ){
printf("out of memeory!\n");
return FAILED;
}
memset(msg, 0, total_len);
memcpy(msg->msg_body, user_input, body_len);
//printf("len = %d, %s, body = %s\n", body_len, user_input, msg->msg_body);
msg->command = htonl(type);
msg->body_len = htonl(body_len);
ret = SSL_write(ssl, msg, total_len);
//ret = write(sockfd, msg, total_len);
if ( ret < total_len ){
printf("connection lost ...(send command)\n");
free(msg);
return SOCK_ERROR;
}
free(msg);
return SUCCESSFUL;
}
int put_file_to_server(int sockfd, SSL *ssl, char *user_input, command_type_t type)
{
int fd = -1;
int ret;
ssize_t read_bytes, write_bytes;
msg_head_ctrl_t *msg;
//ret = send_command_to_server(sockfd, ssl, user_input, type);
ret = send_command_to_server(user_input, type);
if( -1 == fd){
fd = open(user_input, O_RDONLY);
}
while(true){
// read content
msg = malloc(sizeof(msg_head_ctrl_t) + MAX_READ_BYTES);
read_bytes = read(fd, msg->msg_body, MAX_READ_BYTES);
if ( -1 == read_bytes ){
read_bytes = 0;
msg->body_len = 0;
msg->command = htonl(COMMAND_ERROR_FILE);
ret = FAILED;
}
else if ( 0 == read_bytes){
msg->body_len = 0;
msg->command = htonl(COMMAND_END_FILE);
ret = SUCCESSFUL;
}
else if(read_bytes < MAX_READ_BYTES){
msg->body_len = htonl(read_bytes);
msg->command = htonl(COMMAND_END_FILE);
ret = SUCCESSFUL;
}
else if ( read_bytes > 0 ){
msg->body_len = htonl(read_bytes);
msg->command = htonl(msg->command);
ret = SUCCESSFUL;
}
write_bytes = SSL_write( ssl, msg, sizeof(msg_head_ctrl_t) + read_bytes);
//write_bytes = write(sockfd, msg, sizeof(msg_head_ctrl_t) + read_bytes);
if ( write_bytes != (read_bytes + sizeof(msg_head_ctrl_t))){
ret = SOCK_ERROR;
printf("send data error!%s:%d",__FUNCTION__, __LINE__);
break;
}
if ( COMMAND_END_FILE == ntohl(msg->command)){
goto Exit;
}
}
Exit:
free(msg);
close(fd);
return ret;
}
int get_file_from_server(int sockfd, SSL *ssl, char *user_input, command_type_t type)
{
int ret;
int fd=-1;
fd_set fs_client;
struct timeval timeout;
msg_head_ctrl_t *msg;
//ret = send_command_to_server(sockfd, ssl, user_input, type);
ret = send_command_to_server(user_input, type);
if ( ret != SUCCESSFUL){
return ret;
}
msg = malloc(sizeof(msg_head_ctrl_t) + MAX_READ_BYTES);
if ( NULL == msg ){
printf("out of memory!\n");
return FAILED;
}
while(true){
FD_ZERO(&fs_client);
FD_SET( sockfd, &fs_client);
timeout.tv_sec = DEFAULT_CLIENT_TIME_OUT;
timeout.tv_usec = 0;
ret = select(sockfd+1, &fs_client, NULL, NULL, NULL);
if ( 0 == ret ){
//time out
printf("time out....\n");
ret = SOCK_ERROR;
break;
}
if ( -1 == ret ){
continue;
}
memset(msg, 0, sizeof(msg_head_ctrl_t) + MAX_READ_BYTES);
ret = SSL_read( ssl, msg, sizeof(msg_head_ctrl_t) + MAX_READ_BYTES);
//ret = read(sockfd, msg, sizeof(msg_head_ctrl_t) + MAX_READ_BYTES);
#if 0
if ( (sizeof(msg_head_ctrl_t) + len) != ret){
printf("recv msg error!%s:%d, recv %d, acut %d\n", __FUNCTION__, __LINE__,ret,len );
ret = SOCK_ERROR;
goto Exit;
}
#endif
msg->body_len = ntohl(msg->body_len);
msg->command = ntohl(msg->command);
if ( COMMAND_NO_FILE == msg->command ){
printf("File %s doesn't exist on server\n", user_input);
ret = FAILED;
goto Exit;
}
if ( -1 == fd){
fd = open(user_input, O_WRONLY|O_CREAT|O_EXCL, 0660);
if ( -1 == fd){
printf("create file %s failed! %s\n", user_input, strerror(errno));
ret = FAILED;
goto Exit;
}
}
//save to file
ret = write(fd, msg->msg_body, msg->body_len);
if ( msg->body_len != ret ){
printf("Write file failed!\n");
ret = FAILED;
goto Exit;
}
if ( COMMAND_END_FILE == msg->command ){
ret = SUCCESSFUL;
goto Exit;
}
}
Exit:
free(msg);
if (SUCCESSFUL != ret ){
unlink(user_input);
}
close(fd);
return ret;
}
//int handle_user_input( char *user_input, int sockfd, SSL *ssl)
int handle_user_input( char *user_input)
{
command_type_t type;
int ret;
bool exist;
ret = SUCCESSFUL;
user_input = trim_left_space(user_input);
type = get_command_type(user_input);
switch(type){
case COMMAND_CD:
case COMMAND_LS:
case COMMAND_PWD:
case COMMAND_MKDIR:
case COMMAND_RM:
case COMMAND_QUIT:
//ret = send_command_to_server(sockfd, ssl, user_input, type);
ret = send_command_to_server(user_input, type);
break;
case COMMAND_LPWD:
case COMMAND_PUT:
user_input = trim_all_space(user_input + 3);// skip "put" characters
exist = file_exist(user_input);
if( false == exist ){
printf("your computer haven't exist file '%s'!\n", user_input);
}else{
ret = put_file_to_server(sockfd, ssl, user_input, type);
}
break;
case COMMAND_GET:
user_input = trim_all_space(user_input + 3);//skip "get" characters
exist = file_exist(user_input);
if ( false == exist ){
ret = get_file_from_server(sockfd, ssl, user_input, type);
}
else{
printf("file %s already exist on your computer!\n", user_input);
}
break;
default:
printf("your command doesn't support!\n");
ret = FAILED;
break;
}
if ( COMMAND_QUIT == type ){
printf("See you then!\n");
SSL_free(ssl);
SSL_CTX_free(ctx);
close(sockfd);
exit(0);
}
return ret;
}
/*
* FunctionName: login()
* Description: this function finish a login action.
* Help user login to ftp-server, and tell user
* isn't succeeful.
*
*/
//void login(int sockfd, SSL *ssl)
int login(char *username, char *passwd)
{
// verify username and password
int flag = 0;
struct info{
char username[BUF_MAX];
char passwd[BUF_MAX];
};
struct info test;
memset(&test, 0, sizeof(struct info));
strncpy(test.username, username, strlen(username));
strncpy(test.passwd, passwd, strlen(passwd));
test.username[strlen(test.username)] = '\0';
test.passwd[strlen(test.passwd)] = '\0';
printf("get=%s\t%s\n", username, passwd);
//while(!flag){
#if 0
printf("Please login before operation!\n");
printf("Username:");
fgets(test.username, BUF_MAX, stdin);
test.username[strlen(test.username) -1] ='\0';
printf("Password:");
fgets(test.passwd, BUF_MAX, stdin);
test.passwd[strlen(test.passwd) -1] ='\0';
fflush(stdout);
#endif
SSL_write(ssl, &test, sizeof(struct info));
//write(sockfd, &test, sizeof(struct info));
char buff[2];
SSL_read(ssl, buff, 2);
//read(sockfd, buff, 2);
printf("buff=%s\n", buff);
flag = atoi(buff);
printf("flag=%d\n", flag);
if(1 == flag){
return 0;
}
//}
return -1;
}