blob: 8c1340c888ece52a5456d4b2352aa1989a047f72 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*************************************************************
2 Description:
3 $file_description$
4 Author:
5 LiuBin
6 Date:
7 2020/10/28 11:49:08
8 *************************************************************/
9#include "mbtk_ftp.h"
10#include "mbtk_list.h"
11#include "mbtk_sock.h"
12#include "mbtk_str.h"
13#include "mbtk_sock2.h"
14#include "mbtk_info.h"
15#include<linux/msg.h>
16
17/*************************************************************
18 Constants and Macros
19 *************************************************************/
20#define FTP_HANDLE_MAX 5
21#define FTP_ANONYMOUS_USER "Anonymous"
22#define FTP_ANONYMOUS_PASS "@"
23
24//#define FTP_RESUME_DEBUG
25
26#ifdef MBTK_PLATFORM_QCOMM
27#define FTP_TIMEOUT 60000 // 60s
28#else
29#define FTP_TIMEOUT 3000 // 3s
30#endif
31
32/*************************************************************
33 typedef struct:local at
34 *************************************************************/
35typedef struct
36{
37 char host[64];
38 uint16 port;
39 mbtk_ftp_auth_type_enum auth_type;
40 bool ftp_type;
41 bool use_cert;
42 char name[FTP_SERVER_USER_NAME_MAX+1];
43 char pass[FTP_SERVER_USER_PASS_MAX+1];
44 mbtk_ftp_handle at_ftp_handle;
45 char remote_path[MBTK_FTP_FILE_NAME_MAX+1];
46 char local_path[MBTK_FTP_FILE_NAME_MAX+1];
47 int rest_size;
48 int read_size;
49 int put_len;
50} mbtk_at_init_parameter;
51
52mbtk_at_init_parameter mbtk_at_ftp_par={0};
53
54/*************************************************************
55 Variables:local
56 *************************************************************/
57static list_node_t *ftp_client_list = NULL;
58
59/*************************************************************
60 Variables:public
61 *************************************************************/
62
63/*************************************************************
64 Local Function Declaration
65 *************************************************************/
66
67/*************************************************************
68 Local Function Definitions
69 *************************************************************/
70static bool ftp_ch_is_space(char ch)
71{
72 if (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')
73 return true;
74
75 return false;
76}
77
78static const char* ftp_str_next(const char* str, char *result, uint32 len)
79{
80 memset(result, 0x0, len);
81 const char* ptr = str;
82 const char* ptr_result;
83 while (*ptr && ftp_ch_is_space(*ptr))
84 {
85 ptr++;
86 }
87 if (*ptr == '\0')
88 {
89 return NULL;
90 }
91 // Point to first no space char.
92 ptr_result = ptr;
93 while (*ptr && !ftp_ch_is_space(*ptr))
94 {
95 ptr++;
96 }
97
98 // Point to first space char or '\0'.
99 memcpy(result, ptr_result, ptr - ptr_result);
100
101 if (*ptr == '\0')
102 {
103 return NULL;
104 }
105
106 return ptr;
107}
108
109static int ftp_cmd_handle(mbtk_ftp_info_s *info, const void *cmd, void *rsp,
110 uint32 *rsp_len)
111{
112 int err;
113 int rsp_code;
114 int len = strlen((char*) cmd);
115 // Write cmd
116 if(info->auth_type != 0)
117 {
118 mbtk_sock_write(info->ftp_ssl_handle,info->session,cmd,len,60000,&err);
119 if(err!=0)
120 {
121 printf("\nmbtk_sock_write error:%d\n",err);
122 return err;
123 }
124 }
125 else
126 {
127 if (sock_write(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], cmd, len, 60000, &err)
128 != len)
129 {
130 LOGE("Socket write fail[err = %d].", err);
131 return -1;
132 }
133 }
134
135 // Read cmd response
136 if (rsp != NULL && *rsp_len > 0)
137 {
138 read_angin_1:
139 memset(rsp, 0x0, *rsp_len);
140 if(info->auth_type != 0)
141 {
142 unsigned char mbtk_ftp_ssl_read_buf[16384 + 1];
143 len = mbtk_sock_read(info->ftp_ssl_handle,info->session,mbtk_ftp_ssl_read_buf,sizeof(mbtk_ftp_ssl_read_buf),FTP_TIMEOUT,&err);
144 if(err != 0)
145 {
146 printf("\n mbtk_sock_read error:%d \n",err);
147 }
148 rsp_code = atoi(mbtk_ftp_ssl_read_buf);
149 printf("%s -> Code:%d; RSP:%s\n", (char*)cmd, rsp_code, (char* )mbtk_ftp_ssl_read_buf);
150 if (rsp_code == 0)
151 {
152 goto read_angin_1;
153 }
154 memcpy((char* )rsp, mbtk_ftp_ssl_read_buf, strlen(mbtk_ftp_ssl_read_buf));
155 //printf("\nrsp = %s\n",(char* )rsp);
156 return rsp_code;
157 }
158 else
159 {
160 len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], rsp, *rsp_len, FTP_TIMEOUT,&err);
161 }
162 if (len <= 0)
163 {
164 LOGE("Socket read fail[len = %d].", len);
165 return -1;
166 }
167 rsp_code = atoi(rsp);
168 LOGI("%s -> Code:%d; RSP:%s", (char*)cmd, rsp_code, (char* )rsp);
169 //log_hex("FTP_RSP",(char* )rsp,len);
170
171 if (rsp_code == 0)
172 {
173 goto read_angin_1;
174 }
175
176 *rsp_len = len;
177 }
178 else
179 {
180 char buff[1024];
181
182 read_angin_2:
183 memset(buff, 0x0, 1024);
184 if(info->auth_type != 0)
185 {
186 unsigned char mbtk_ftp_ssl_read_buf[16384 + 1];
187 len = mbtk_sock_read(info->ftp_ssl_handle,info->session,mbtk_ftp_ssl_read_buf,sizeof(mbtk_ftp_ssl_read_buf),FTP_TIMEOUT,&err);
188 if(err != 0)
189 {
190 printf("\nmbtk_sock_read error:%d\n",err);
191 }
192 rsp_code = atoi(mbtk_ftp_ssl_read_buf);
193 printf("%s -> Code:%d; RSP:%s\n", (char*)cmd, rsp_code, (char* )mbtk_ftp_ssl_read_buf);
194 char *mbtk_ftp_ssl_read_buf_p = NULL;
195 mbtk_ftp_ssl_read_buf_p = strstr(mbtk_ftp_ssl_read_buf,"\n");
196 if(mbtk_ftp_ssl_read_buf_p!=NULL);
197 {
198 if(strlen(mbtk_ftp_ssl_read_buf_p)>1)
199 {
200 mbtk_ftp_ssl_read_buf_p++;
201 rsp_code = atoi(mbtk_ftp_ssl_read_buf_p);
202 }
203 }
204 if (rsp_code == 0)
205 {
206 goto read_angin_2;
207 }
208 return rsp_code;
209 }
210 else
211 {
212 len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], buff, 1024, FTP_TIMEOUT,
213 &err);
214 }
215 if (len <= 0)
216 {
217 LOGE("Socket read fail[len = %d].", len);
218 return -1;
219 }
220
221 rsp_code = atoi(buff);
222 LOGI("%s -> Code:%d; RSP:%s", (char*)cmd, rsp_code, buff);
223 //log_hex("FTP_RSP",buff,len);
224
225 if (rsp_code == 0)
226 {
227 goto read_angin_2;
228 }
229 }
230
231 return rsp_code;
232}
233
234// Create data socket service and waitting client connect.
235static mbtk_ftp_error_enum ftp_data_sock_service_create(mbtk_ftp_sock_s *sock)
236{
237 return FTP_ERR_SUCCESS;
238}
239
240static bool ftp_internal_ipv4_check(void *ipv4)
241{
242 char *ip = (char*) ipv4;
243 int ip1 = atoi(ip);
244 ip = strstr(ip, ".");
245 if (!ip)
246 return FALSE;
247 int ip2 = atoi(ip + 1);
248
249 if (ip1 == 10) // 10.x.x.x
250 {
251 return TRUE;
252 }
253 else if (ip1 == 172 && (ip2 >= 16 && ip2 <= 31)) // 172.16.0.0 ~ 172.31.255.255
254 {
255 return TRUE;
256 }
257 else if (ip1 == 192 && ip2 == 168) // 192.168.x.x
258 {
259 return TRUE;
260 }
261 else
262 {
263 return FALSE;
264 }
265}
266
267static mbtk_ftp_error_enum ftp_sock_set_by_port(mbtk_ftp_info_s *info,
268 mbtk_ftp_sock_s *sock)
269{
270 // port = port1 * 256 + port2
271 // "PORT ip1,ip2,ip3,ip4,port1,port2\r\n"
272 int code;
273 char cmd_buff[100];
274 memset(cmd_buff, 0x0, 100);
275 if (info->sock_info[FTP_SOCK_CTRL].addr_family == MBTK_ADDR_IPV6 || sock->port <= 1024
276 || strlen((char*) sock->host) == 0)
277 {
278 LOGE("FTP params set error.");
279 return FTP_ERR_PARAM_SET;
280 }
281
282 if (ftp_data_sock_service_create(sock) != FTP_ERR_SUCCESS)
283 {
284 LOGE("FTP data services create fail.");
285 return FTP_ERR_UNKNOWN;
286 }
287
288 uint8 *ptr = sock->host;
289 while (*ptr)
290 {
291 if (*ptr == '.')
292 *ptr = ',';
293 ptr++;
294 }
295 // Waitting client connect.
296 snprintf(cmd_buff, 100, "PORT %s,%d,%d\r\n", sock->host, sock->port / 256,
297 sock->port % 256);
298 code = ftp_cmd_handle(info, cmd_buff, NULL, NULL);
299 if (code / 100 != 2) // Not 2xx
300 {
301 LOGE("PORT rsp error[code = %d].", code);
302 return FTP_ERR_UNKNOWN;
303 }
304
305 return FTP_ERR_SUCCESS;
306}
307
308static mbtk_ftp_error_enum ftp_sock_set_by_eprt(mbtk_ftp_info_s *info,
309 mbtk_ftp_sock_s *sock)
310{
311 int code;
312 char cmd_buff[100];
313 memset(cmd_buff, 0x0, 100);
314 if (sock->port <= 1024 || strlen((char*) sock->host) == 0)
315 {
316 LOGE("FTP params set error.");
317 return FTP_ERR_PARAM_SET;
318 }
319 if (ftp_data_sock_service_create(sock) != FTP_ERR_SUCCESS)
320 {
321 LOGE("FTP data services create fail.");
322 return FTP_ERR_UNKNOWN;
323 }
324
325 // "EPRT |2|1080::8:800:200C:417A|5282|"
326 if (info->sock_info[FTP_SOCK_CTRL].addr_family == MBTK_ADDR_IPV6)
327 {
328 uint8 *ptr = NULL;
329 if (sock->host[strlen((char*) sock->host) - 1] == ']')
330 sock->host[strlen((char*) sock->host) - 1] = '\0';
331
332 if (sock->host[0] == '[')
333 ptr = sock->host + 1;
334 else
335 ptr = sock->host;
336 snprintf(cmd_buff, 100, "EPRT |2|%s|%d|\r\n", ptr, sock->port);
337 }
338 else // IPV4 "EPRT |1|132.235.1.2|6275|"
339 {
340 snprintf(cmd_buff, 100, "EPRT |1|%s|%d|\r\n", sock->host, sock->port);
341 }
342
343 code = ftp_cmd_handle(info, cmd_buff, NULL, NULL);
344 if (code / 100 != 2) // Not 2xx
345 {
346 LOGE("PORT rsp error[code = %d].", code);
347 return FTP_ERR_UNKNOWN;
348 }
349
350 return FTP_ERR_SUCCESS;
351}
352
353static mbtk_ftp_error_enum ftp_sock_get_by_pasv(mbtk_ftp_info_s *info,
354 mbtk_ftp_sock_s *sock)
355{
356 int code;
357 uint8 rsp[1024];
358 uint32 rsp_len = 1024;
359 memset(sock, 0x0, sizeof(mbtk_ftp_sock_s));
360 code = ftp_cmd_handle(info, "PASV\r\n", rsp, &rsp_len);
361 /* Found reply-strings include:
362 * "227 Entering Passive Mode (127,0,0,1,4,51)"
363 * "227 Data transfer will passively listen to 127,0,0,1,4,51"
364 * "227 Entering passive mode. 127,0,0,1,4,51"
365 */
366 if (code != 227)
367 {
368 LOGE("PASV rsp error[code = %d].", code);
369 if(code == 421)
370 {
371 printf("\n code:%d\n",code);
372 return FTP_ERR_UNKNOWN+1;
373 }
374 return FTP_ERR_UNKNOWN;
375 }
376
377 // Get IPv4 and port
378 uint8 *ptr = rsp + 4;
379 while (*ptr)
380 {
381 if (isdigit(*ptr))
382 break;
383 ptr++;
384 }
385 // ptr point to first digit.
386 memcpy(sock->host, ptr, strlen((char*) ptr));
387 ptr = sock->host;
388 int count = 0;
389 while (*ptr)
390 {
391 if (*ptr == ',')
392 {
393 *ptr = '.';
394 count++;
395 }
396 if (count == 4)
397 {
398 *ptr = '\0';
399 break;
400 }
401 ptr++;
402 }
403 ptr++;
404
405 // ptr point to first port digit.
406 int port1 = atoi((char*) ptr);
407 while (*ptr)
408 {
409 if (*ptr == ',')
410 break;
411 ptr++;
412 }
413 ptr++;
414
415 // ptr point to second port digit.
416 int port2 = atoi((char*) ptr);
417 sock->port = port1 * 256 + port2;
418
419 LOGI("PASV: %s:%d", sock->host, sock->port);
420
421 if(ftp_internal_ipv4_check(sock->host))
422 {
423 memset(sock->host,0x0,MBTK_FTP_IP_MAX + 1);
424 memcpy(sock->host,info->sock_info[FTP_SOCK_CTRL].host,strlen(info->sock_info[FTP_SOCK_CTRL].host));
425 LOGI("This is internal ipv4,so use IP - %s:%d",sock->host, sock->port);
426 }
427
428 return FTP_ERR_SUCCESS;
429}
430
431static mbtk_ftp_error_enum ftp_sock_get_by_epsv(mbtk_ftp_info_s *info,
432 mbtk_ftp_sock_s *sock)
433{
434 int code;
435 char rsp[1024];
436 uint32 rsp_len = 1024;
437 memset(sock, 0x0, sizeof(mbtk_ftp_sock_s));
438 // |||6446|
439 code = ftp_cmd_handle(info, "EPSV\r\n", rsp, &rsp_len);
440 if (code != 229)
441 {
442 LOGE("EPSV rsp error[code = %d].", code);
443 return FTP_ERR_UNKNOWN;
444 }
445
446 /* positive EPSV response */
447 char *ptr = strchr(rsp, '(');
448 if (ptr)
449 {
450 unsigned int num;
451 char separator[4];
452 ptr++;
453 if (5
454 == sscanf(ptr, "%c%c%c%u%c", &separator[0], &separator[1],
455 &separator[2], &num, &separator[3]))
456 {
457 const char sep1 = separator[0];
458 int i;
459
460 /* The four separators should be identical, or else this is an oddly
461 formatted reply and we bail out immediately. */
462 for (i = 1; i < 4; i++)
463 {
464 if (separator[i] != sep1)
465 {
466 ptr = NULL; /* set to NULL to signal error */
467 break;
468 }
469 }
470 if (num > 0xffff)
471 {
472 LOGE("Illegal port number in EPSV reply");
473 return FTP_ERR_UNKNOWN;
474 }
475 if (ptr)
476 {
477 sock->port = (uint32) (num & 0xffff);
478 memcpy(sock->host, info->sock_info[FTP_SOCK_CTRL].host,
479 strlen(info->sock_info[FTP_SOCK_CTRL].host));
480 }
481 }
482 else
483 {
484 ptr = NULL;
485 }
486 }
487 if (!ptr)
488 {
489 LOGE("Weirdly formatted EPSV reply");
490 return FTP_ERR_UNKNOWN;
491 }
492
493 LOGI("PASV: %s:%d", sock->host, sock->port);
494
495 return FTP_ERR_SUCCESS;
496}
497
498/*
499 * 04-26-20 02:13PM 379193 audio_0426.zip
500 * 09-10-20 02:31PM 4660645 Log.zip
501 * 10-28-20 01:53PM <DIR> PicLibs
502 */
503
504 /*
505 *-rw-r--r-- 1 ftp ftp 2097152000 Feb 18 17:28 ASR1803_Linux.tar.gz_aa
506 *-rw-r--r-- 1 ftp ftp 2097152000 Feb 18 17:21 ASR1803_Linux.tar.gz_ab
507 *-rw-r--r-- 1 ftp ftp 1198057917 Feb 18 17:29 ASR1803_Linux.tar.gz_ac
508 *-rw-r--r-- 1 ftp ftp 445043 Apr 06 2021 data
509 *drwxr-xr-x 1 ftp ftp 0 Apr 10 2021 L306
510 */
511static mbtk_ftp_error_enum ftp_cmd_list_parse(mbtk_ftp_info_s *info,
512 mbtk_ftp_file_info_s *file_list)
513{
514 char line[1024];
515 char line_buf[1024];
516 int len, err;
517 mbtk_ftp_file_info_s file_ptr;
518 memset(file_list, 0x0, sizeof(mbtk_ftp_file_info_s)-sizeof(void *));
519
520 // Save file number.
521 file_list->size = 0;
522 len = 1;
523 int read_line_count=0;
524 while (len > 0)
525 {
526 if(info->auth_type!=0)
527 {
528 len = mbtk_sock_readline(info->ftp_ssl_handle,info->session_data,line_buf,1024,FTP_TIMEOUT,&err,&read_line_count,line);
529 }
530 else
531 {
532 len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_DATA], line, 1024,FTP_TIMEOUT, &err);
533 }
534 LOGD("Line:%s", line);
535 if(len <= 0)
536 {
537 if (err == MBTK_SOCK_SUCCESS)
538 return FTP_ERR_SUCCESS;
539 break;
540 }
541 else
542 len = strlen(line) - 1;
543 if (line[0] >= '0' && line[0] <= '9')
544 {
545 /*
546 * 04-26-20 02:13PM 379193 audio_0426.zip
547 * 09-10-20 02:31PM 4660645 Log.zip
548 * 10-28-20 01:53PM <DIR> PicLibs
549 */
550 line[len] = '\0';
551 /*
552 file_ptr = (mbtk_ftp_file_info_s*) malloc(
553 sizeof(mbtk_ftp_file_info_s));
554 if (!file_ptr)
555 {
556 LOGE("malloc() fail.");
557 return FTP_ERR_UNKNOWN;
558 }
559 */
560 memset(&file_ptr, 0x0, sizeof(mbtk_ftp_file_info_s));
561
562 char str[MBTK_FTP_FILE_NAME_MAX];
563 const char *temp = line;
564 // Data
565 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
566 if (strlen(str) > 0)
567 {
568 LOGV("Data:%s", str);
569 }
570
571 // Time
572 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
573 if (strlen(str) > 0)
574 {
575 LOGV("Time:%s", str);
576 }
577
578 // <DIR> or file size
579 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
580 if (strlen(str) > 0)
581 {
582 LOGV("<DIR>/size:%s", str);
583 if (!strcmp("<DIR>", str))
584 {
585 file_ptr.is_file = false;
586 file_ptr.size = 0;
587 }
588 else
589 {
590 file_ptr.is_file = true;
591 file_ptr.size = atoi(str);
592 }
593 }
594
595 const char *name = temp;
596 while (*name && ftp_ch_is_space(*name))
597 {
598 name++;
599 }
600
601 // Name
602 if (strlen(name) > 0)
603 {
604 LOGV("Name:%s",name);
605 memset(file_ptr.name,0,strlen(file_ptr.name)+1);
606 memcpy(file_ptr.name, name, strlen(name));
607 char *temp = (char*) file_ptr.name
608 + strlen((char*) file_ptr.name) - 1;
609 while (ftp_ch_is_space(*temp))
610 {
611 *temp = '\0';
612 temp--;
613 }
614 }
615 else
616 {
617 LOGE("Can not get name[%s].", line);
618 return FTP_ERR_UNKNOWN;
619 }
620
621 (file_list->ftp_ls_cb_typedef)(&file_ptr);
622 //file_ptr->next = file_list->next;
623 //file_list->next = file_ptr;
624 //file_list->size++;
625 //free(file_ptr);
626 } else if(!ftp_ch_is_space(line[0])) {
627 /*
628 *-rw-r--r-- 1 ftp ftp 2097152000 Feb 18 17:28 ASR1803_Linux.tar.gz_aa
629 *-rw-r--r-- 1 ftp ftp 2097152000 Feb 18 17:21 ASR1803_Linux.tar.gz_ab
630 *-rw-r--r-- 1 ftp ftp 1198057917 Feb 18 17:29 ASR1803_Linux.tar.gz_ac
631 *-rw-r--r-- 1 ftp ftp 445043 Apr 06 2021 data
632 *drwxr-xr-x 1 ftp ftp 0 Apr 10 2021 L306
633 */
634 line[len] = '\0';
635 /*
636 file_ptr = (mbtk_ftp_file_info_s*) malloc(
637 sizeof(mbtk_ftp_file_info_s));
638 if (!file_ptr)
639 {
640 LOGE("malloc() fail.");
641 return FTP_ERR_UNKNOWN;
642 }
643 */
644 memset(&file_ptr, 0x0, sizeof(mbtk_ftp_file_info_s));
645
646 char str[MBTK_FTP_FILE_NAME_MAX];
647 const char *temp = line;
648 // rwx
649 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
650 if (strlen(str) > 0)
651 {
652 LOGV("rwx:%s", str);
653 file_ptr.is_file = (str[0] == '-' ? true : false);
654 }
655
656 // 1 ftp ftp
657 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
658 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
659 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
660
661 // size
662 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
663 if (strlen(str) > 0)
664 {
665 LOGV("Size:%s", str);
666 file_ptr.size = atoi(str);
667 }
668
669 // Feb 18 17:28
670 // or
671 // Apr 10 2021
672 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
673 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
674 temp = ftp_str_next(temp, str, MBTK_FTP_FILE_NAME_MAX);
675
676 const char *name = temp;
677 while (*name && ftp_ch_is_space(*name))
678 {
679 name++;
680 }
681
682 // Name
683 if (strlen(name) > 0)
684 {
685 LOGV("Name:%s",name);
686 memset(file_ptr.name,0,strlen(file_ptr.name)+1);
687 memcpy(file_ptr.name, name, strlen(name));
688 char *temp = (char*) file_ptr.name
689 + strlen((char*) file_ptr.name) - 1;
690 while (ftp_ch_is_space(*temp))
691 {
692 *temp = '\0';
693 temp--;
694 }
695 }
696 else
697 {
698 LOGE("Can not get name[%s].", line);
699 return FTP_ERR_UNKNOWN;
700 }
701
702 (file_list->ftp_ls_cb_typedef)(&file_ptr);
703 //file_ptr->next = file_list->next;
704 //file_list->next = file_ptr;
705 //file_list->size++;
706 //free(file_ptr);
707 }else {
708 LOGE("Data error.");
709 return FTP_ERR_UNKNOWN;
710 }
711 }
712 return FTP_ERR_SUCCESS;
713}
714
715static mbtk_ftp_error_enum ftp_download_process(mbtk_ftp_info_s *info)
716{
717#define READ_BUF_SIZE 2048
718 char buff[READ_BUF_SIZE];
719 int len, err, len_read;
720 int read_count = info->file_trans.size_send;
721
722#ifdef FTP_RESUME_DEBUG
723 int count = 0;
724#endif
725
726 if (info->file_trans.size_count - read_count > READ_BUF_SIZE)
727 len_read = READ_BUF_SIZE;
728 else
729 len_read = info->file_trans.size_count - read_count;
730
731 if(info->auth_type != 0)
732 len = mbtk_sock_read(info->ftp_ssl_handle,info->session_data,buff,len_read,FTP_TIMEOUT,&err);
733 else
734 len = sock_read(&info->net_info, &info->sock_info[FTP_SOCK_DATA], buff, len_read,FTP_TIMEOUT, &err);
735 /*
736 while (len_read > 0 && (len = sock_read(&info->net_info, &info->sock_info[FTP_SOCK_DATA], buff, len_read,
737 FTP_TIMEOUT, &err)) > 0)
738 */
739 while (len_read > 0 && len > 0)
740 {
741 read_count += len;
742
743#ifdef FTP_RESUME_DEBUG
744 count++;
745 if (count <= 1000)
746 {
747#endif
748 if (info->file_trans.data_cb)
749 {
750 info->file_trans.data_cb(buff, len);
751 }
752 else // Write to efs.
753 {
754 if (len != file_write(info->file_trans.fd, buff, len))
755 {
756 LOGE("EFS write fail.");
757 return FTP_ERR_EFS_FILE;
758 }
759 }
760 memset(buff,0,sizeof(buff));
761 info->file_trans.size_send = read_count;
762#ifdef FTP_RESUME_DEBUG
763 }
764#endif
765
766 if (info->file_trans.size_count - read_count > READ_BUF_SIZE)
767 len_read = READ_BUF_SIZE;
768 else
769 len_read = info->file_trans.size_count - read_count;
770
771 if(info->auth_type != 0)
772 len = mbtk_sock_read(info->ftp_ssl_handle,info->session_data,buff,len_read,FTP_TIMEOUT,&err);
773 else
774 len = sock_read(&info->net_info, &info->sock_info[FTP_SOCK_DATA], buff, len_read,FTP_TIMEOUT, &err);
775 }
776
777 return FTP_ERR_SUCCESS;
778}
779
780static mbtk_ftp_error_enum ftp_update_process(mbtk_ftp_info_s *info)
781{
782#define READ_BUF_SIZE 2048
783 char buff[READ_BUF_SIZE];
784 int len, err, len_write;
785 int file_total_size = 0;
786 int write_count = 0;
787
788 if(info->file_trans.fd > 0)
789 {
790 file_total_size = file_length(info->file_trans.fd);
791 info->file_trans.size_count = file_total_size;
792 while((len_write = file_read(info->file_trans.fd, buff, READ_BUF_SIZE)) > 0 )
793 {
794 LOGE("file_read.len:%d, buf;%s", len_write, buff);
795 if(info->auth_type != 0)
796 len = mbtk_sock_write(info->ftp_ssl_handle,info->session_data,buff,len_write,FTP_TIMEOUT,&err);
797 else
798 len = sock_write(&info->net_info, &info->sock_info[FTP_SOCK_DATA], buff, len_write,
799 FTP_TIMEOUT, &err);
800 if(len < 0)
801 {
802 LOGE("EFS write fail.len:%d, err;%d", len, err);
803 return FTP_ERR_EFS_FILE;
804 }
805
806 write_count += len;
807 }
808
809 info->file_trans.size_send = write_count;
810 if( write_count != file_total_size)
811 {
812 LOGE("socket write fail.,file_total_size:%d, write_count:%d", file_total_size,write_count);
813 return FTP_ERR_NET_WRITE;
814 }
815 else{
816 LOGE("socket write success.,file_total_size:%d, write_count:%d", file_total_size, write_count);
817 }
818
819 }
820 else
821 {
822 LOGE("EFS write fail.");
823 return FTP_ERR_EFS_FILE;
824 }
825
826 return FTP_ERR_SUCCESS;
827}
828
829
830/*
831 * audio_0426.zip
832 * Log.zip
833 * PicLibs
834 */
835static mbtk_ftp_error_enum ftp_cmd_nlst_parse(mbtk_ftp_info_s *info,
836 mbtk_ftp_file_info_s *file_list)
837{
838 char line[1024];
839 int len, err;
840 mbtk_ftp_file_info_s *file_ptr = NULL;
841 while ((len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_DATA], line, 1024,
842 FTP_TIMEOUT, &err)) > 0)
843 {
844 if (!ftp_ch_is_space(line[0]))
845 {
846 file_ptr = (mbtk_ftp_file_info_s*) malloc(
847 sizeof(mbtk_ftp_file_info_s));
848 if (!file_ptr)
849 {
850 LOGE("malloc() fail.");
851 return FTP_ERR_UNKNOWN;
852 }
853 memset(file_ptr, 0x0, sizeof(mbtk_ftp_file_info_s));
854 memcpy(file_ptr->name, line, strlen(line));
855 char *temp = (char*) file_ptr->name + strlen((char*) file_ptr->name)
856 - 1;
857 while (ftp_ch_is_space(*temp))
858 {
859 *temp = '\0';
860 temp--;
861 }
862
863 file_ptr->next = file_list->next;
864 file_list->next = file_ptr;
865 }
866 }
867 return FTP_ERR_SUCCESS;
868}
869
870static mbtk_ftp_error_enum ftp_data_sock_read(mbtk_ftp_info_s *info,
871 mbtk_ftp_cmd_enum cmd, void *req, void *rsp)
872{
873 int err, len, code;
874 mbtk_ftp_error_enum result = FTP_ERR_SUCCESS;
875 char buff[1024];
876 if (info->is_data_sock_busy)
877 {
878 LOGW("Data socket is busy!");
879 return FTP_ERR_DATA_SOCK_BUSY;
880 }
881
882 info->is_data_sock_busy = true;
883 // Connect to service by data socket.
884 if (info->data_mode == FTP_MODE_PASSIVE)
885 {
886 mbtk_ftp_sock_s sock;
887 if(info->auth_type != FTP_AUTH_TYPE_NON) {
888 // PROT P
889 char cmd_ssl[50];
890 memset(cmd_ssl,0,50);
891 snprintf(cmd_ssl, 50, "PROT P\r\n");
892 code = ftp_cmd_handle(info, cmd_ssl, NULL,NULL);
893 if (code/100 != 2)
894 {
895 LOGE("PROT P error[code = %d].", code);
896 goto end;
897 }
898 }
899
900 if (info->sock_info[FTP_SOCK_CTRL].addr_family == MBTK_ADDR_IPV6)
901 {
902 if ((result = ftp_sock_get_by_epsv(info, &sock))
903 != FTP_ERR_SUCCESS)
904 {
905 goto end;
906 }
907 }
908 else
909 {
910 if ((result = ftp_sock_get_by_pasv(info, &sock))
911 != FTP_ERR_SUCCESS)
912 {
913 printf("\nftp_sock_get_by_pasv end.result=%d\n",result);
914 }
915 else
916 {
917 printf("\nftp_sock_get_by_pasv ok!\n");
918 }
919 }
920
921 if(info->auth_type != FTP_AUTH_TYPE_NON)
922 {
923 info->ftp_ssl_handle_data = mbtk_sock_init(&info->ftp_ssl_info_data);
924 if(info->ftp_ssl_handle_data == -1)
925 {
926 printf("mbtk_sock_init error\n");
927 }
928 info->ftp_sock_ssl_info_data = (mbtk_sock_info*) malloc(sizeof(mbtk_sock_info));
929 memset(info->ftp_sock_ssl_info_data, 0x0, sizeof(mbtk_sock_info));
930 info->ftp_sock_ssl_info_data->is_support_ssl = info->auth_type;
931 info->ftp_sock_ssl_info_data->ftp_ssl_support=1;
932 info->ftp_sock_ssl_info_data->port = sock.port;
933 info->ftp_sock_ssl_info_data->type = MBTK_SOCK_TCP;
934 info->ftp_sock_ssl_info_data->ingnore_cert = ~(info->sock_info[FTP_SOCK_CTRL].use_cert);
935 memcpy(info->ftp_sock_ssl_info_data->address, sock.host, strlen(sock.host));
936 if(info->ftp_sock_ssl_info_data->is_support_ssl != 0)
937 {
938 info->ftp_sock_ssl_info_data->is_support_ssl = 0;
939 }
940 info->session_data = mbtk_sock_open(info->ftp_ssl_handle,info->ftp_sock_ssl_info_data,60000,&err);
941 if(err!=0)
942 {
943 printf("mbtk_sock_open error : %d\n",err);
944 }
945 else
946 {
947 info->ftp_sock_ssl_info_data->is_support_ssl = info->auth_type;
948 //printf("\ninfo->session_data = %d \n",info->session_data);
949 }
950 }
951 else
952 {
953 memcpy(info->sock_info[FTP_SOCK_DATA].host, sock.host, strlen((char*)sock.host));
954 info->sock_info[FTP_SOCK_DATA].port = sock.port;
955 info->sock_info[FTP_SOCK_DATA].is_ssl = (info->auth_type != FTP_AUTH_TYPE_NON);
956 info->sock_info[FTP_SOCK_DATA].addr_family = info->sock_info[FTP_SOCK_CTRL].addr_family;
957 info->sock_info[FTP_SOCK_DATA].use_cert = info->sock_info[FTP_SOCK_CTRL].use_cert;
958 info->net_info.keep_alive = FALSE;
959 info->net_info.recv_buff_size = 32 * 1024; // 32K
960 info->sock_info[FTP_SOCK_DATA].fd = sock_open(&info->net_info, &info->sock_info[FTP_SOCK_DATA],
961 FTP_TIMEOUT, &err);
962 }
963
964 }
965 else // Active mode
966 {
967 if (req == NULL)
968 {
969 result = FTP_ERR_PARAM_SET;
970 goto end;
971 }
972 mbtk_ftp_sock_s *sock = (mbtk_ftp_sock_s*) req;
973
974 if (info->sock_info[FTP_SOCK_CTRL].addr_family == MBTK_ADDR_IPV6)
975 {
976 if ((result = ftp_sock_set_by_eprt(info, sock))
977 != FTP_ERR_SUCCESS)
978 {
979 goto end;
980 }
981 }
982 else
983 {
984 if ((result = ftp_sock_set_by_port(info, sock))
985 != FTP_ERR_SUCCESS)
986 {
987 goto end;
988 }
989 }
990
991 // Wait for client[service] connect.
992
993 }
994 if(info->auth_type != FTP_AUTH_TYPE_NON)
995 {
996 if(info->session_data < 0)
997 {
998 //printf("Socket open/connect ssl fail[err = %d].", err);
999 result = FTP_ERR_NET_CONN;
1000 goto read_end;
1001 }
1002 }
1003 else
1004 {
1005 if (info->sock_info[FTP_SOCK_DATA].fd < 0)
1006 {
1007 LOGE("Socket open/connect fail[err = %d].", err);
1008 result = FTP_ERR_NET_CONN;
1009 goto read_end;
1010 }
1011
1012 if(info->auth_type == FTP_AUTH_TYPE_IMPLICIT) {
1013 info->sock_info[FTP_SOCK_DATA].is_ssl = true;
1014 }
1015 }
1016
1017 if (cmd == FTP_CMD_GET) // Is download
1018 {
1019 char cmd[100];
1020 if (info->file_trans.size_send > 0) // Resume transmission
1021 {
1022 // REST size
1023 memset(cmd, 0x0, 100);
1024 snprintf(cmd, 100, "REST %ld\r\n", info->file_trans.size_send);
1025 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1026 if (code != 350)
1027 {
1028 LOGE("REST rsp error[code = %d].", code);
1029 result = FTP_ERR_UNKNOWN;
1030 goto end;
1031 }
1032 }
1033
1034 memset(cmd, 0x0, 100);
1035 snprintf(cmd, 100, "RETR %s\r\n", info->file_trans.remote_name);
1036 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1037 if (code != 125 && code != 150)
1038 {
1039 LOGE("RETR %s rsp error[code = %d].", info->file_trans.remote_name,
1040 code);
1041 result = FTP_ERR_UNKNOWN;
1042 goto end;
1043 }
1044
1045 int mbtk_errno;
1046 if(info->auth_type != 0)
1047 {
1048 mbtk_errno = mbtk_ssl_init_func(info->ftp_ssl_handle,info->ftp_sock_ssl_info_data->ingnore_cert,info->session_data);
1049 if(mbtk_errno != 0)
1050 {
1051 printf("\nmbtk_ssl_init_func error = %d",mbtk_errno);
1052 goto end;
1053 }
1054
1055 }
1056 result = ftp_download_process(info);
1057 if(info->auth_type != 0)
1058 {
1059 char mbtk_ftp_ssl_read_buf[256];
1060 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
1061 mbtk_sock_read(info->ftp_ssl_handle,info->session,
1062 mbtk_ftp_ssl_read_buf,
1063 sizeof(mbtk_ftp_ssl_read_buf),
1064 6000,
1065 &mbtk_errno);
1066 printf("\nmbtk_sock_read:\n%s\n",mbtk_ftp_ssl_read_buf);
1067 }
1068 if(info->auth_type != 0)
1069 goto read_end;
1070 }
1071 else if (cmd == FTP_CMD_PUT) // Is download
1072 {
1073 if(info->auth_type != 0)
1074 {
1075 int mbtk_errno;
1076 char cmd[100];
1077 memset(cmd, 0x0, 100);
1078 snprintf(cmd, 100, "STOR %s\r\n", info->file_trans.remote_name);
1079 LOGE("STOR %s .name:%s ", cmd, info->file_trans.remote_name);
1080 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1081 if (code != 125 && code != 150)
1082 {
1083 LOGE("RETR %s rsp error[code = %d].", info->file_trans.remote_name,
1084 code);
1085 //printf("RETR %s rsp error[code = %d].\n", info->file_trans.remote_name,code);
1086 result = FTP_ERR_UNKNOWN;
1087 goto end;
1088 }
1089
1090 mbtk_errno = mbtk_ssl_init_func(info->ftp_ssl_handle,info->ftp_sock_ssl_info_data->ingnore_cert,info->session_data);
1091 if(mbtk_errno != 0)
1092 {
1093 printf("\nmbtk_ssl_init_func error = %d",mbtk_errno);
1094 goto end;
1095 }
1096 if(info->file_trans.size_count == 0)
1097 {
1098 result = ftp_update_process(info);
1099 goto read_end;
1100 }
1101 else
1102 {
1103 //printf("FTP_SOCK_DATA,fd:%d\n",info->file_trans.size_count);
1104 return FTP_ERR_SUCCESS;
1105 }
1106 goto end;
1107 }
1108 else
1109 {
1110 char cmd[100];
1111 memset(cmd, 0x0, 100);
1112 snprintf(cmd, 100, "STOR %s\r\n", info->file_trans.remote_name);
1113 LOGE("STOR %s .name:%s ", cmd, info->file_trans.remote_name);
1114 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1115 if (code != 125 && code != 150)
1116 {
1117 LOGE("RETR %s rsp error[code = %d].", info->file_trans.remote_name,
1118 code);
1119 result = FTP_ERR_UNKNOWN;
1120 goto end;
1121 }
1122
1123 if(info->file_trans.size_count == 0)
1124 {
1125 result = ftp_update_process(info);
1126 goto read_end;
1127 }
1128 else
1129 {
1130 LOGE("FTP_SOCK_DATA,fd:%d", info->sock_info[FTP_SOCK_DATA].fd);
1131 return FTP_ERR_SUCCESS;
1132 }
1133 }
1134 }
1135 else if (cmd == FTP_CMD_LIST)
1136 {
1137 if(info->auth_type != 0)
1138 {
1139 int mbtk_errno;
1140 code = ftp_cmd_handle(info, "LIST\r\n", NULL, NULL);
1141 if (code != 125 && code != 150)
1142 {
1143 LOGE("LIST rsp error[code = %d].", code);
1144 result = FTP_ERR_UNKNOWN;
1145 goto read_end;
1146 }
1147
1148 mbtk_errno = mbtk_ssl_init_func(info->ftp_ssl_handle,info->ftp_sock_ssl_info_data->ingnore_cert,info->session_data);
1149 if(mbtk_errno != 0)
1150 {
1151 printf("\nmbtk_ssl_init_func error = %d",mbtk_errno);
1152 goto read_end;
1153 }
1154
1155 result = ftp_cmd_list_parse(info, (mbtk_ftp_file_info_s*) rsp);
1156 char mbtk_ftp_ssl_read_buf[1024];
1157 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
1158 mbtk_sock_read(info->ftp_ssl_handle,info->session,
1159 mbtk_ftp_ssl_read_buf,
1160 sizeof(mbtk_ftp_ssl_read_buf),
1161 6000,
1162 &mbtk_errno);
1163 printf("\nmbtk_sock_read:\n%s\n",mbtk_ftp_ssl_read_buf);
1164 int rsp_code = atoi(mbtk_ftp_ssl_read_buf);
1165 if(rsp_code / 200)
1166 result = FTP_ERR_SUCCESS;
1167 goto read_end;
1168 }
1169 else
1170 {
1171 code = ftp_cmd_handle(info, "LIST\r\n", NULL, NULL);
1172 if (code != 125 && code != 150)
1173 {
1174 LOGE("LIST rsp error[code = %d].", code);
1175 result = FTP_ERR_UNKNOWN;
1176 goto end;
1177 }
1178
1179 result = ftp_cmd_list_parse(info, (mbtk_ftp_file_info_s*) rsp);
1180 }
1181 }
1182 else if (cmd == FTP_CMD_NLST)
1183 {
1184 code = ftp_cmd_handle(info, "NLST\r\n", NULL, NULL);
1185 if (code != 125 && code != 150)
1186 {
1187 LOGE("LIST rsp error[code = %d].", code);
1188 result = FTP_ERR_UNKNOWN;
1189 goto end;
1190 }
1191
1192 result = ftp_cmd_nlst_parse(info, (mbtk_ftp_file_info_s*) rsp);
1193 }
1194 else
1195 {
1196 LOGE("No support this cmd[%d].", cmd);
1197 result = FTP_ERR_UNKNOWN;
1198 goto read_end;
1199 }
1200
1201 // Read [226 Transfer complete.]
1202read_code:
1203 memset(buff, 0x0, 1024);
1204 len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], buff, 1024, FTP_TIMEOUT,
1205 &err);
1206 if (len <= 0)
1207 {
1208 LOGE("Socket read fail[len = %d].", len);
1209 result = FTP_ERR_NET_READ;
1210 goto sock_error;
1211 }
1212 LOGI("RSP[len-%d]:%s",len,buff);
1213 //log_hex("FTP_RSP",buff,len);
1214 code = atoi(buff);
1215 if (code == 0)
1216 {
1217 goto read_code;
1218 }
1219#if 1
1220 // 426 Connection closed; aborted transfer of "/files/test"
1221 else if (code == 426)
1222 {
1223 LOGE("Connection closed,restart download...");
1224 result = FTP_ERR_UNKNOWN;
1225 goto sock_error;
1226 }
1227#endif
1228 else if (code != 226)
1229 {
1230 LOGE("Code not be 226[%s].", buff);
1231 result = FTP_ERR_UNKNOWN;
1232 goto read_end;
1233 }
1234
1235 goto read_end;
1236sock_error:
1237 {
1238 if (info->sock_info[FTP_SOCK_CTRL].fd > 0)
1239 {
1240 if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], FTP_TIMEOUT, &err))
1241 {
1242 LOGE("Close ctrl socket fail[%d].", err);
1243 }
1244 }
1245 info->state = FTP_STATE_NON;
1246 info->sock_info[FTP_SOCK_CTRL].fd = -1;
1247 }
1248read_end:
1249 // Close data socket.
1250 if (info->sock_info[FTP_SOCK_DATA].fd > 0)
1251 {
1252 if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err))
1253 {
1254 LOGE("Close data socket fail[%d].", err);
1255 result = FTP_ERR_NET_CLOSE;
1256 }
1257 else
1258 {
1259 info->sock_info[FTP_SOCK_DATA].fd = -1;
1260 }
1261 }
1262 if(info->auth_type != 0)
1263 {
1264 if(mbtk_sock_close(info->ftp_ssl_handle,info->session_data,6000,&err))
1265 {
1266 LOGE("Close ssl data socket fail[%d].", err);
1267 result = FTP_ERR_NET_CLOSE;
1268 }
1269 else
1270 {
1271 // info->sock_info[FTP_SOCK_DATA].fd = -1;
1272 }
1273 }
1274 if (info->data_mode == FTP_MODE_PASSIVE)
1275 {
1276
1277 }
1278 else
1279 {
1280
1281 }
1282
1283end:
1284 // Default is passive.
1285 info->data_mode = FTP_MODE_PASSIVE;
1286 info->is_data_sock_busy = false;
1287
1288 return result;
1289}
1290
1291static mbtk_ftp_error_enum ftp_cmd_process_pwd(mbtk_ftp_info_s *info,
1292 char *path)
1293{
1294 int code;
1295 char rsp[100];
1296 uint32 rsp_len = 100;
1297 code = ftp_cmd_handle(info, "PWD\r\n", rsp, &rsp_len);
1298 if (code != 257)
1299 {
1300 LOGE("PWD rsp error[code = %d].", code);
1301 return FTP_ERR_UNKNOWN;
1302 }
1303
1304 // "path" is current ....
1305 char *ptr = strchr(rsp, '"');
1306 if (!ptr)
1307 {
1308 LOGE("PWD rsp error[%d].", code);
1309 return FTP_ERR_UNKNOWN;
1310 }
1311
1312 ptr++;
1313 char *ptr_temp = ptr;
1314 while (*ptr_temp)
1315 {
1316 if (*ptr_temp == '"')
1317 {
1318 *ptr_temp = '\0';
1319 break;
1320 }
1321 ptr_temp++;
1322 }
1323 memcpy(path, ptr, strlen(ptr));
1324 path[strlen(ptr)] = '\0';
1325
1326 return FTP_ERR_SUCCESS;
1327}
1328
1329static mbtk_ftp_error_enum ftp_cmd_process_cwd(mbtk_ftp_info_s *info,
1330 const char *path)
1331{
1332 int code;
1333 char cmd[100] = { 0 };
1334 snprintf(cmd, 100, "CWD %s\r\n", path);
1335 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1336 if (code != 250)
1337 {
1338 LOGE("CWD rsp error[code = %d].", code);
1339 return FTP_ERR_UNKNOWN;
1340 }
1341
1342 return FTP_ERR_SUCCESS;
1343}
1344
1345static mbtk_ftp_error_enum ftp_cmd_process_mkd(mbtk_ftp_info_s *info,
1346 const char *path)
1347{
1348 int code;
1349 char cmd[100] = { 0 };
1350 snprintf(cmd, 100, "MKD %s\r\n", path);
1351 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1352 if (code == 257)
1353 {
1354 return FTP_ERR_SUCCESS;
1355 }
1356 else if (code == 550)
1357 {
1358 LOGE("Dir has exist[%s].", path);
1359 return FTP_ERR_FILE_EXIST;
1360 }
1361 else
1362 {
1363 LOGE("MKD rsp error[code = %d].", code);
1364 return FTP_ERR_UNKNOWN;
1365 }
1366}
1367
1368static mbtk_ftp_error_enum ftp_cmd_process_rmd(mbtk_ftp_info_s *info,
1369 const char *path)
1370{
1371 int code;
1372 char cmd[100] = { 0 };
1373 snprintf(cmd, 100, "RMD %s\r\n", path);
1374 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1375 if (code == 250)
1376 {
1377 return FTP_ERR_SUCCESS;
1378 }
1379 else if (code == 550)
1380 {
1381 LOGE("Dir not exist or not empty[%s].", path);
1382 return FTP_ERR_FILE_NO_FOUND;
1383 }
1384 else
1385 {
1386 LOGE("RMD rsp error[code = %d].", code);
1387 return FTP_ERR_UNKNOWN;
1388 }
1389}
1390
1391static mbtk_ftp_error_enum ftp_cmd_process_stat(mbtk_ftp_info_s *info,
1392 void *status)
1393{
1394 int code;
1395 char rsp[1024];
1396 uint32 rsp_len = 1024;
1397 code = ftp_cmd_handle(info, "STAT\r\n", rsp, &rsp_len);
1398 if (code != 211)
1399 {
1400 LOGE("STAT rsp error[code = %d].", code);
1401 return FTP_ERR_UNKNOWN;
1402 }
1403
1404 memcpy(status, rsp, rsp_len);
1405
1406 return FTP_ERR_SUCCESS;
1407}
1408
1409static mbtk_ftp_error_enum ftp_cmd_process_type(mbtk_ftp_info_s *info,
1410 mbtk_ftp_data_type_enum type)
1411{
1412 int code;
1413 if (type == FTP_DATA_TYPE_I)
1414 code = ftp_cmd_handle(info, "TYPE I\r\n", NULL, NULL);
1415 else if (type == FTP_DATA_TYPE_E)
1416 code = ftp_cmd_handle(info, "TYPE E\r\n", NULL, NULL);
1417 else
1418 code = ftp_cmd_handle(info, "TYPE A\r\n", NULL, NULL);
1419
1420 if (code != 200)
1421 {
1422 LOGE("TYPE rsp error[code = %d].", code);
1423 return FTP_ERR_UNKNOWN;
1424 }
1425
1426 return FTP_ERR_SUCCESS;
1427}
1428
1429static mbtk_ftp_error_enum ftp_cmd_process_size(mbtk_ftp_info_s *info,
1430 const char *path, uint32 *size)
1431{
1432 int code;
1433 char cmd[100] = { 0 };
1434 char rsp[100];
1435 uint32 rsp_len = 100;
1436 snprintf(cmd, 100, "SIZE %s\r\n", path);
1437 code = ftp_cmd_handle(info, cmd, rsp, &rsp_len);
1438
1439 if (code == 213)
1440 {
1441 // "213 4660645"
1442 *size = atoi(rsp + 4);
1443 }
1444 else if (code == 550)
1445 {
1446 LOGW("No found file[%s].", path);
1447 return FTP_ERR_FILE_NO_FOUND;
1448 }
1449 else
1450 {
1451 LOGE("SIZE rsp error[code = %d].", code);
1452 return FTP_ERR_UNKNOWN;
1453 }
1454
1455 return FTP_ERR_SUCCESS;
1456}
1457
1458static mbtk_ftp_error_enum ftp_cmd_process_mdtm(mbtk_ftp_info_s *info,
1459 const char *path, char *time)
1460{
1461 int code;
1462 char cmd[100] = { 0 };
1463 char rsp[100];
1464 uint32 rsp_len = 100;
1465 snprintf(cmd, 100, "MDTM %s\r\n", path);
1466 code = ftp_cmd_handle(info, cmd, rsp, &rsp_len);
1467
1468 if (code == 213)
1469 {
1470 // "213 20181017014716"
1471 memcpy(time, rsp + 4, 14);
1472 }
1473 else if (code == 550)
1474 {
1475 LOGW("No found file[%s].", path);
1476 return FTP_ERR_FILE_NO_FOUND;
1477 }
1478 else
1479 {
1480 LOGE("MDTM rsp error[code = %d].", code);
1481 return FTP_ERR_UNKNOWN;
1482 }
1483
1484 return FTP_ERR_SUCCESS;
1485}
1486
1487static mbtk_ftp_error_enum ftp_cmd_process_dele(mbtk_ftp_info_s *info,
1488 const char *path)
1489{
1490 int code;
1491 char cmd[100] = { 0 };
1492 snprintf(cmd, 100, "DELE %s\r\n", path);
1493 code = ftp_cmd_handle(info, cmd, NULL, NULL);
1494 if (code == 250)
1495 {
1496 return FTP_ERR_SUCCESS;
1497 }
1498 else if (code == 550)
1499 {
1500 LOGW("No found file[%s].", path);
1501 return FTP_ERR_FILE_NO_FOUND;
1502 }
1503 else
1504 {
1505 LOGE("DELE rsp error[code = %d].", code);
1506 return FTP_ERR_UNKNOWN;
1507 }
1508}
1509
1510static mbtk_ftp_error_enum ftp_cmd_process_quit(mbtk_ftp_info_s *info)
1511{
1512 int code;
1513 code = ftp_cmd_handle(info, "QUIT\r\n", NULL, NULL);
1514 if (code != 221)
1515 {
1516 LOGE("CWD rsp error[code = %d].", code);
1517 return FTP_ERR_UNKNOWN;
1518 }
1519
1520 return FTP_ERR_SUCCESS;
1521}
1522
1523static mbtk_ftp_error_enum ftp_cmd_process(mbtk_ftp_info_s *info,
1524 mbtk_ftp_cmd_enum cmd, void *req, void *rsp)
1525{
1526 mbtk_ftp_error_enum result = FTP_ERR_SUCCESS;
1527 if (info->state == FTP_STATE_READY) // FTP login
1528 {
1529 info->state = FTP_STATE_CMD_PROCESS;
1530 switch (cmd)
1531 {
1532 case FTP_CMD_LIST:
1533 {
1534 result = ftp_data_sock_read(info, FTP_CMD_LIST, req, rsp);
1535 break;
1536 }
1537 case FTP_CMD_NLST:
1538 {
1539 result = ftp_data_sock_read(info, FTP_CMD_NLST, req, rsp);
1540 break;
1541 }
1542 case FTP_CMD_PWD:
1543 {
1544 result = ftp_cmd_process_pwd(info, rsp);
1545 break;
1546 }
1547 case FTP_CMD_CWD:
1548 {
1549 result = ftp_cmd_process_cwd(info, (char*) req);
1550 break;
1551 }
1552 case FTP_CMD_MKD:
1553 {
1554 result = ftp_cmd_process_mkd(info, (char*) req);
1555 break;
1556 }
1557 case FTP_CMD_RMD:
1558 {
1559 result = ftp_cmd_process_rmd(info, (char*) req);
1560 break;
1561 }
1562 case FTP_CMD_STAT:
1563 {
1564 result = ftp_cmd_process_stat(info, rsp);
1565 break;
1566 }
1567 case FTP_CMD_TYPE:
1568 {
1569 mbtk_ftp_data_type_enum *type = (mbtk_ftp_data_type_enum*) req;
1570 result = ftp_cmd_process_type(info, *type);
1571 break;
1572 }
1573 case FTP_CMD_SIZE:
1574 {
1575 result = ftp_cmd_process_size(info, (char*) req, (uint32*) rsp);
1576 break;
1577 }
1578 case FTP_CMD_MDTM:
1579 {
1580 result = ftp_cmd_process_mdtm(info, (char*) req, (char*) rsp);
1581 break;
1582 }
1583 case FTP_CMD_DELE:
1584 {
1585 result = ftp_cmd_process_dele(info, (char*) req);
1586 break;
1587 }
1588 case FTP_CMD_QUIT:
1589 {
1590 result = ftp_cmd_process_quit(info);
1591 break;
1592 }
1593 default:
1594 {
1595 LOGE("Unknown cmd:%d", cmd);
1596 result = FTP_ERR_UNKNOWN;
1597 break;
1598 }
1599 }
1600 info->state = FTP_STATE_READY;
1601 }
1602 else
1603 {
1604 LOGW("FTP state error[%d].", info->state);
1605 result = FTP_ERR_UNKNOWN;
1606 }
1607
1608 return result;
1609}
1610
1611static mbtk_ftp_error_enum ftp_login(mbtk_ftp_info_s *info,mbtk_ftp_user_info_s *user)
1612{
1613 // Open net in the first.
1614 if(info->net_info.net_id <= 0) {
1615 if(sock_net_open(&info->net_info,info->sock_info[FTP_SOCK_CTRL].addr_family))
1616 {
1617 LOGE("sock_net_open() fail.");
1618 return FTP_ERR_NET_CONN;
1619 }
1620 }
1621
1622 int err;
1623 mbtk_ftp_error_enum ftp_err = FTP_ERR_SUCCESS;
1624 info->state = FTP_STATE_CONNECTING;
1625 info->net_info.keep_alive = TRUE;
1626 info->net_info.recv_buff_size = 0; // Default
1627 info->sock_info[FTP_SOCK_CTRL].fd = sock_open(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], FTP_TIMEOUT, &err);
1628 if (info->sock_info[FTP_SOCK_CTRL].fd < 0) // Fail
1629 {
1630 LOGE("Socket open/connect fail[err = %d].", err);
1631 ftp_err = FTP_ERR_NET_CONN;
1632 goto login_fail;
1633 }
1634
1635 if(info->auth_type == FTP_AUTH_TYPE_IMPLICIT)
1636 info->sock_info[FTP_SOCK_CTRL].is_ssl = true;
1637
1638 // Ctrl socket connect success.
1639 info->state = FTP_STATE_CONNECTED;
1640 LOGI("FTP ctrl socket connected.");
1641
1642 char buff[1024];
1643 int len;
1644 char *ptr = NULL;
1645
1646 // 220-FileZilla Server version 0.9.43 beta
1647 // 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
1648 // 220 Please visit http://sourceforge.net/projects/filezilla/
1649 while(TRUE) {
1650 memset(buff, 0x0, 1024);
1651 len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], buff, 1024, FTP_TIMEOUT,
1652 &err);
1653 if (len <= 0)
1654 {
1655 LOGE("Socket read fail[err = %d].", err);
1656 ftp_err = FTP_ERR_NET_READ;
1657 goto login_fail;
1658 } else {// xxx yyyyyy
1659 if((ptr = strstr(buff,"220 ")) || (ptr = strstr(buff,"230 "))) {
1660 LOGI("RSP:%s",ptr);
1661 break;
1662 }
1663 }
1664 }
1665
1666 int code = atoi(ptr);
1667 if (code == 230) // Has logged in.
1668 {
1669 info->state = FTP_STATE_READY;
1670 LOGI("FTP Has logged in.");
1671 return FTP_ERR_SUCCESS;
1672 }
1673 else if (code == 220) // Should logn in.
1674 {
1675 int len;
1676 char cmd_buff[50];
1677
1678#if 0
1679 // Read other data.
1680 char buff[1024];
1681 memset(buff,0x0,1024);
1682 while((len = sock_read(&info->net_info,info->ctrl_sock.fd, buff, 1024, info->ssl_enable, 1000,
1683 &err)) > 0)
1684 {
1685 LOGI("RSP[%d]:%s",len,buff);
1686 memset(buff,0x0,1024);
1687 }
1688#endif
1689
1690 if(info->auth_type == FTP_AUTH_TYPE_EXPLICIT_SSL
1691 || info->auth_type == FTP_AUTH_TYPE_EXPLICIT_TLS) {
1692 if(info->auth_type == FTP_AUTH_TYPE_EXPLICIT_SSL) {
1693 len = snprintf(cmd_buff, 50, "AUTH SSL\r\n");
1694 } else {
1695 len = snprintf(cmd_buff, 50, "AUTH TLS\r\n");
1696 }
1697 cmd_buff[len] = '\0';
1698 code = ftp_cmd_handle(info, cmd_buff, NULL, NULL);
1699 if (code != 234 && code != 334)
1700 {
1701 LOGE("AUTH SSL/TLS fail[code = %d].", code);
1702 ftp_err = FTP_ERR_LOGIN_DENIED;
1703 goto login_fail;
1704 }
1705
1706#if 0
1707 if(sock_ssl_enable(&info->net_info,info->ctrl_sock.fd
1708 ,info->ctrl_sock.host,info->ctrl_sock.port,info->use_cert,FTP_TIMEOUT)){
1709 LOGE("sock_ssl_enable() fail.");
1710 ftp_err = FTP_ERR_LOGIN_DENIED;
1711 goto login_fail;
1712 }
1713#endif
1714 info->sock_info[FTP_SOCK_CTRL].is_ssl = true;
1715 }
1716
1717 if(info->auth_type != FTP_AUTH_TYPE_NON) {
1718 len = snprintf(cmd_buff, 50, "PBSZ 0\r\n");
1719 cmd_buff[len] = '\0';
1720 code = ftp_cmd_handle(info, cmd_buff, NULL, NULL);
1721 if (code != 200 && code != 220)
1722 {
1723 LOGE("PBSZ 0 fail[code = %d].", code);
1724 ftp_err = FTP_ERR_LOGIN_DENIED;
1725 goto login_fail;
1726 }
1727 }
1728
1729 len = snprintf(cmd_buff, 50, "USER %s\r\n", user->name);
1730 cmd_buff[len] = '\0';
1731 code = ftp_cmd_handle(info, cmd_buff, NULL, NULL);
1732 if (code == 331) // USER is 331
1733 {
1734 len = snprintf(cmd_buff, 50, "PASS %s\r\n", user->pass);
1735 cmd_buff[len] = '\0';
1736 code = ftp_cmd_handle(info, cmd_buff, NULL, NULL);
1737 }
1738
1739 if (code / 100 == 2) // USER/PASS is 2xx
1740 {
1741 info->state = FTP_STATE_READY;
1742 LOGI("FTP logn in success.");
1743 return FTP_ERR_SUCCESS;
1744 }
1745 else if (code == 332) // // USER/PASS is 332
1746 {
1747 LOGW("Should set ACCT.");
1748 ftp_err = FTP_ERR_UNKNOWN;
1749 goto login_fail;
1750 }
1751 else
1752 {
1753 LOGE("FTP login denied[code = %d].", code);
1754 ftp_err = FTP_ERR_LOGIN_DENIED;
1755 goto login_fail;
1756 }
1757 }
1758 else
1759 {
1760 LOGE("FTP code error[%d].", code);
1761 ftp_err = FTP_ERR_UNKNOWN;
1762 goto login_fail;
1763 }
1764
1765login_fail:
1766 info->state = FTP_STATE_NON;
1767 return ftp_err;
1768}
1769
1770static mbtk_ftp_info_s* ftp_info_find(mbtk_ftp_handle handle)
1771{
1772 if (!ftp_client_list)
1773 {
1774 LOGE("FTP Client List not init.");
1775 return NULL;
1776 }
1777
1778 mbtk_ftp_info_s *result = NULL;
1779 list_first(ftp_client_list);
1780 while ((result = (mbtk_ftp_info_s*) list_next(ftp_client_list)))
1781 {
1782 if (result->handle == handle)
1783 break;
1784 }
1785
1786 return result;
1787}
1788
1789mbtk_ftp_handle mbtk_ftp_upload_end(mbtk_ftp_handle handle)
1790{
1791 mbtk_ftp_info_s *info = ftp_info_find(handle);
1792 if (!info)
1793 {
1794 printf("No such FTP handle:%d\n", handle);
1795 }
1796
1797 char buff[1024]={0};
1798 int err=0;
1799 int len=0;
1800 int result;
1801
1802 if (info->sock_info[FTP_SOCK_DATA].fd > 0)
1803 {
1804 if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err))
1805 {
1806 LOGE("Close data socket fail[%d].", err);
1807 result = FTP_ERR_NET_CLOSE;
1808 }
1809 else
1810 {
1811 // info->sock_info[FTP_SOCK_DATA].fd = -1;
1812 }
1813 }
1814 if(info->auth_type != 0)
1815 {
1816 if(mbtk_sock_close(info->ftp_ssl_handle,info->session_data,6000,&err))
1817 {
1818 LOGE("Close ssl data socket fail[%d].", err);
1819 result = FTP_ERR_NET_CLOSE;
1820 }
1821 else
1822 {
1823 // info->sock_info[FTP_SOCK_DATA].fd = -1;
1824 }
1825 }
1826 info->data_mode = FTP_MODE_PASSIVE;
1827 info->is_data_sock_busy = false;
1828 info->file_trans.size_count = 0;
1829 info->file_trans.size_send = 0;
1830
1831 char line_buf[1024];
1832 if(info->auth_type != 0)
1833 len = mbtk_sock_read(info->ftp_ssl_handle,info->session,buff,sizeof(buff),FTP_TIMEOUT,&err);
1834 else
1835 len = sock_readline(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], buff, 1024, FTP_TIMEOUT, &err);
1836 if(len > 0)
1837 {
1838 printf("\n%s\n",buff);
1839 if(err != 0)
1840 printf("socket read error: %d\n",err);
1841 }
1842 else
1843 {
1844 printf("socket_read error:%d\n",err);
1845 result = -1;
1846 }
1847
1848 return result;
1849}
1850
1851static mbtk_ftp_handle ftp_next_handle()
1852{
1853 if (!ftp_client_list)
1854 {
1855 LOGE("FTP Client List not init.");
1856 return -1;
1857 }
1858
1859 mbtk_ftp_info_s *info = NULL;
1860 mbtk_ftp_handle handle = 1;
1861 bool used;
1862 while (handle <= FTP_HANDLE_MAX)
1863 {
1864 used = false;
1865 // This handle used?
1866 list_first(ftp_client_list);
1867 while ((info = (mbtk_ftp_info_s*) list_next(ftp_client_list)))
1868 {
1869 if (handle == info->handle)
1870 {
1871 used = true;
1872 break;
1873 }
1874 }
1875
1876 if (!used)
1877 {
1878 break;
1879 }
1880
1881 handle++;
1882 }
1883
1884 LOGI("Get free handle:%d", handle);
1885
1886 return handle;
1887}
1888
1889static void ftp_free_func(void *data)
1890{
1891 if (data)
1892 {
1893 mbtk_ftp_info_s *info = (mbtk_ftp_info_s*) data;
1894 LOGI("Free FTP client[handle = %d].", info->handle);
1895 free(info);
1896 }
1897}
1898
1899/*************************************************************
1900 Public Function Definitions
1901 *************************************************************/
1902mbtk_ftp_handle mbtk_ftp_init(const void* host, uint16 port, mbtk_ftp_auth_type_enum auth_type,
1903 bool is_ipv6, bool use_cert)
1904{
1905 if (!ftp_client_list)
1906 ftp_client_list = list_create(ftp_free_func);
1907
1908 if (!ftp_client_list)
1909 {
1910 LOGE("FTP Client List not init.");
1911 return -1;
1912 }
1913
1914 if (list_size(ftp_client_list) > FTP_HANDLE_MAX)
1915 {
1916 LOGE("FTP client is full.");
1917 return -1;
1918 }
1919
1920 if (host == NULL || strlen(host) == 0)
1921 {
1922 LOGE("Host error.");
1923 return -1;
1924 }
1925
1926 if (port == 0)
1927 {
1928 port = FTP_SERVICE_PORT_DEF;
1929 }
1930
1931 mbtk_ftp_info_s *info = (mbtk_ftp_info_s*) malloc(sizeof(mbtk_ftp_info_s));
1932 if (!info)
1933 {
1934 LOGE("malloc() fail.");
1935 return -1;
1936 }
1937 memset(info, 0x0, sizeof(mbtk_ftp_info_s));
1938 list_add(ftp_client_list, info);
1939 memcpy(info->sock_info[FTP_SOCK_CTRL].host, host, strlen(host));
1940 info->sock_info[FTP_SOCK_CTRL].port = port;
1941 // info->auth_type == FTP_AUTH_TYPE_IMPLICIT, info->is_ipv6 ? MBTK_ADDR_IPV6 : MBTK_ADDR_IPV4,info->use_cert,
1942 info->sock_info[FTP_SOCK_CTRL].is_ssl = (auth_type == FTP_AUTH_TYPE_IMPLICIT);
1943 info->sock_info[FTP_SOCK_CTRL].addr_family = is_ipv6 ? MBTK_ADDR_IPV6: MBTK_ADDR_IPV4;
1944 info->sock_info[FTP_SOCK_CTRL].use_cert = use_cert;
1945
1946 info->handle = ftp_next_handle();
1947 info->state = FTP_STATE_NON;
1948 info->data_mode = FTP_MODE_PASSIVE;
1949 info->sock_info[FTP_SOCK_CTRL].fd = -1;
1950 info->sock_info[FTP_SOCK_DATA].fd = -1;
1951 //info->is_ipv6 = is_ipv6;
1952 info->auth_type = auth_type;
1953 //info->use_cert = use_cert;
1954
1955 LOGI("FTP info#host:%s,port:%d,ipv6:%d,ssl:%d,cert:%d",info->sock_info[FTP_SOCK_CTRL].host,
1956 info->sock_info[FTP_SOCK_CTRL].port,
1957 is_ipv6,info->auth_type,use_cert);
1958
1959 if(auth_type != FTP_AUTH_TYPE_NON)
1960 {
1961 info->ftp_ssl_handle = mbtk_sock_init(&info->ftp_ssl_info);
1962 if(info->ftp_ssl_handle == -1)
1963 {
1964 printf("mbtk_sock_init error\n");
1965 }
1966
1967 info->ftp_sock_ssl_info = (mbtk_sock_info*) malloc(sizeof(mbtk_sock_info));
1968 memset(info->ftp_sock_ssl_info, 0x0, sizeof(mbtk_sock_info));
1969 info->ftp_sock_ssl_info->is_support_ssl = (auth_type != FTP_AUTH_TYPE_NON);
1970 info->ftp_sock_ssl_info->ftp_ssl_support = 1;
1971 info->ftp_sock_ssl_info->port = port;
1972 info->ftp_sock_ssl_info->type = MBTK_SOCK_TCP;
1973 info->ftp_sock_ssl_info->ingnore_cert = ~use_cert;
1974 memcpy(info->ftp_sock_ssl_info->address, host, strlen(host));
1975 }
1976 return info->handle;
1977}
1978
1979mbtk_ftp_error_enum mbtk_ftp_reconfig(mbtk_ftp_handle handle,const void* host, uint16 port, mbtk_ftp_auth_type_enum auth_type,
1980 bool is_ipv6, bool use_cert)
1981{
1982 mbtk_ftp_info_s *info = ftp_info_find(handle);
1983 if (!info)
1984 {
1985 LOGE("No such FTP handle:%d", handle);
1986 return FTP_ERR_UNKNOWN;
1987 }
1988 if(auth_type != FTP_AUTH_TYPE_NON)
1989 {
1990 info->ftp_sock_ssl_info = (mbtk_sock_info*) malloc(sizeof(mbtk_sock_info));
1991 memset(info->ftp_sock_ssl_info, 0x0, sizeof(mbtk_sock_info));
1992 if(host && strlen(host) > 0)
1993 {
1994 memcpy(info->ftp_sock_ssl_info->address, host, strlen(host));
1995 info->ftp_sock_ssl_info->address[strlen(host)] = '\0';
1996 }
1997 info->ftp_sock_ssl_info->is_support_ssl = (auth_type != FTP_AUTH_TYPE_NON);
1998 info->ftp_sock_ssl_info->ftp_ssl_support = 1;
1999 info->ftp_sock_ssl_info->port = port;
2000 info->ftp_sock_ssl_info->ingnore_cert = ~use_cert;
2001 info->ftp_sock_ssl_info->type = MBTK_SOCK_TCP;
2002 }
2003 if(info->state == FTP_STATE_NON)
2004 {
2005 if(host && strlen(host) > 0)
2006 {
2007 memcpy(info->sock_info[FTP_SOCK_CTRL].host, host, strlen(host));
2008 info->sock_info[FTP_SOCK_CTRL].host[strlen(host)] = '\0';
2009 }
2010 info->sock_info[FTP_SOCK_CTRL].port = port;
2011 info->sock_info[FTP_SOCK_CTRL].addr_family = is_ipv6 ? MBTK_ADDR_IPV6 : MBTK_ADDR_IPV4;
2012 info->auth_type = auth_type;
2013 info->sock_info[FTP_SOCK_CTRL].use_cert = use_cert;
2014
2015 return FTP_ERR_SUCCESS;
2016 }
2017 else
2018 {
2019 LOGE("Not reset FTP config.The state is %d", info->state);
2020 return FTP_ERR_UNKNOWN;
2021 }
2022}
2023
2024mbtk_ftp_error_enum mbtk_ftp_user_set(mbtk_ftp_handle handle,void* user,void* pass)
2025{
2026 mbtk_ftp_info_s *info = ftp_info_find(handle);
2027 if (!info)
2028 {
2029 LOGE("No such FTP handle:%d", handle);
2030 return FTP_ERR_UNKNOWN;
2031 }
2032
2033 if(info->state == FTP_STATE_NON && !str_empty(user) && !str_empty(pass))
2034 {
2035 memset(&info->user,0x0,sizeof(mbtk_ftp_user_info_s));
2036 memcpy(info->user.name, user, strlen(user));
2037 memcpy(info->user.pass, pass, strlen(pass));
2038 return FTP_ERR_SUCCESS;
2039 }
2040 else
2041 {
2042 LOGE("Not reset FTP config.The state is %d", info->state);
2043 return FTP_ERR_UNKNOWN;
2044 }
2045}
2046
2047mbtk_ftp_error_enum mbtk_ftp_deinit(mbtk_ftp_handle handle)
2048{
2049 mbtk_ftp_info_s *info = ftp_info_find(handle);
2050 if (!info)
2051 {
2052 LOGE("No such FTP handle:%d", handle);
2053 return FTP_ERR_UNKNOWN;
2054 }
2055
2056 if (list_remove(ftp_client_list, info))
2057 {
2058 int err;
2059 sock_close(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], FTP_TIMEOUT, &err);
2060 sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err);
2061 ftp_free_func(info);
2062 }
2063
2064 return FTP_ERR_SUCCESS;
2065}
2066
2067/*
2068 * Quit FTP service.
2069 */
2070mbtk_ftp_error_enum mbtk_ftp_quit(mbtk_ftp_handle handle)
2071{
2072 mbtk_ftp_info_s *info = ftp_info_find(handle);
2073 if (!info)
2074 {
2075 LOGE("No such FTP handle:%d", handle);
2076 return FTP_ERR_UNKNOWN;
2077 }
2078
2079 int err = 0;
2080 if(info->sock_info[FTP_SOCK_CTRL].fd > 0) {
2081 mbtk_ftp_error_enum ftp_err = ftp_cmd_process(info, FTP_CMD_QUIT, NULL,
2082 NULL);
2083 if (ftp_err != FTP_ERR_SUCCESS)
2084 {
2085 LOGE("FTP QUIT fail[%d].", ftp_err);
2086 //return ftp_err;
2087 }
2088
2089 // FTP quit success.
2090 info->state = FTP_STATE_CONNECTED;
2091 }
2092
2093 if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], FTP_TIMEOUT, &err))
2094 {
2095 LOGE("Close ctrl socket fail[%d].", err);
2096 return FTP_ERR_NET_CLOSE;
2097 }
2098
2099 if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err))
2100 {
2101 LOGE("Close data socket fail[%d].", err);
2102 return FTP_ERR_NET_CLOSE;
2103 }
2104
2105 info->state = FTP_STATE_NON;
2106 info->data_mode = FTP_MODE_PASSIVE;
2107 memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s));
2108 info->sock_info[FTP_SOCK_CTRL].fd = -1;
2109 info->sock_info[FTP_SOCK_DATA].fd = -1;
2110
2111 return FTP_ERR_SUCCESS;
2112}
2113
2114mbtk_ftp_error_enum mbtk_ftp_net_close(mbtk_ftp_handle handle)
2115{
2116 mbtk_ftp_info_s *info = ftp_info_find(handle);
2117 if (!info)
2118 {
2119 LOGE("No such FTP handle:%d", handle);
2120 return FTP_ERR_UNKNOWN;
2121 }
2122
2123 if(info->net_info.net_id > 0) {
2124 int err;
2125 if(sock_net_close(&info->net_info, FTP_TIMEOUT,&err))
2126 {
2127 LOGE("sock_net_close() fail[%ld].",err);
2128 return FTP_ERR_NET_CLOSE;
2129 }
2130
2131 info->net_info.net_id = -1;
2132 }
2133
2134 return FTP_ERR_SUCCESS;
2135}
2136
2137
2138mbtk_ftp_info_s* mbtk_ftp_info_get(mbtk_ftp_handle handle)
2139{
2140 mbtk_ftp_info_s *info = ftp_info_find(handle);
2141 if (!info)
2142 {
2143 LOGE("No such FTP handle:%d", handle);
2144 return NULL;
2145 }
2146
2147 return info;
2148}
2149
2150/*
2151 * Login specified FTP service.
2152 */
2153mbtk_ftp_error_enum mbtk_ftp_login(mbtk_ftp_handle handle, void *name,
2154 void *pass)
2155{
2156 mbtk_ftp_info_s *info = ftp_info_find(handle);
2157 if (!info)
2158 {
2159 LOGE("No such FTP handle:%d", handle);
2160 return FTP_ERR_UNKNOWN;
2161 }
2162
2163 if (info->state == FTP_STATE_NON)
2164 {
2165 mbtk_ftp_user_info_s user;
2166 memset(&user,0x0,sizeof(mbtk_ftp_user_info_s));
2167 if (!str_empty(name) && !str_empty(pass))
2168 {
2169 memcpy(user.name, name, strlen(name));
2170 memcpy(user.pass, pass, strlen(pass));
2171 memcpy(info->user.name, name, strlen(name));
2172 memcpy(info->user.pass, pass, strlen(pass));
2173 }
2174 else
2175 {
2176 memcpy(user.name, FTP_ANONYMOUS_USER, strlen(FTP_ANONYMOUS_USER));
2177 memcpy(user.pass, FTP_ANONYMOUS_PASS, strlen(FTP_ANONYMOUS_PASS));
2178 memcpy(info->user.name, FTP_ANONYMOUS_USER, strlen(FTP_ANONYMOUS_USER));
2179 memcpy(info->user.pass, FTP_ANONYMOUS_PASS, strlen(FTP_ANONYMOUS_PASS));
2180 }
2181
2182 LOGI("FTP login#user:%s,pass:%s",user.name,user.pass);
2183
2184 if(info->auth_type != 0)
2185 {
2186 int mbtk_errno=-1;
2187 info->session = mbtk_sock_open(info->ftp_ssl_handle,info->ftp_sock_ssl_info,60000,&mbtk_errno);
2188 if(mbtk_errno!=0)
2189 {
2190 printf("mbtk_sock_open error : %d\n",mbtk_errno);
2191 }
2192 else
2193 {
2194 unsigned char mbtk_ftp_ssl_read_buf[16384 + 1];
2195 char cmd[50];
2196 memset(cmd,0,50);
2197 int len_ssl;
2198
2199
2200 memset(cmd,0,50);
2201 snprintf(cmd, 50, "PBSZ 0\r\n");
2202 mbtk_sock_write(info->ftp_ssl_handle,info->session,
2203 cmd,
2204 sizeof(cmd),
2205 60000,
2206 &mbtk_errno);
2207 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
2208 mbtk_sock_read(info->ftp_ssl_handle,info->session,
2209 mbtk_ftp_ssl_read_buf,
2210 sizeof(mbtk_ftp_ssl_read_buf),
2211 60000,
2212 &mbtk_errno);
2213 printf("\nmbtk_sock_read PBSZ 0:\n%s\n",mbtk_ftp_ssl_read_buf);
2214 memset(cmd,0,50);
2215 snprintf(cmd, 50, "PROT P\r\n");
2216 mbtk_sock_write(info->ftp_ssl_handle,info->session,
2217 cmd,
2218 sizeof(cmd),
2219 60000,
2220 &mbtk_errno);
2221 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
2222 mbtk_sock_read(info->ftp_ssl_handle,info->session,
2223 mbtk_ftp_ssl_read_buf,
2224 sizeof(mbtk_ftp_ssl_read_buf),
2225 60000,
2226 &mbtk_errno);
2227 printf("\nmbtk_sock_read PROT P:\n%s\n",mbtk_ftp_ssl_read_buf);
2228 memset(cmd,0,50);
2229 snprintf(cmd, 50, "USER %s\r\n",info->user.name);
2230 mbtk_sock_write(info->ftp_ssl_handle,info->session,
2231 cmd,
2232 sizeof(cmd),
2233 60000,
2234 &mbtk_errno);
2235 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
2236 mbtk_sock_read(info->ftp_ssl_handle,info->session,
2237 mbtk_ftp_ssl_read_buf,
2238 sizeof(mbtk_ftp_ssl_read_buf),
2239 60000,
2240 &mbtk_errno);
2241 printf("\nmbtk_sock_read USER:\n%s\n",mbtk_ftp_ssl_read_buf);
2242 memset(cmd,0,50);
2243 snprintf(cmd, 50, "PASS %s\r\n",info->user.pass);
2244 mbtk_sock_write(info->ftp_ssl_handle,info->session,
2245 cmd,
2246 sizeof(cmd),
2247 60000,
2248 &mbtk_errno);
2249 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
2250 mbtk_sock_read(info->ftp_ssl_handle,info->session,
2251 mbtk_ftp_ssl_read_buf,
2252 sizeof(mbtk_ftp_ssl_read_buf),
2253 60000,
2254 &mbtk_errno);
2255 printf("\nmbtk_sock_read PASS:\n%s\n",mbtk_ftp_ssl_read_buf);
2256 char *ptr = NULL;
2257 if((ptr = strstr(mbtk_ftp_ssl_read_buf,"220 ")) || (ptr = strstr(mbtk_ftp_ssl_read_buf,"230 "))) {
2258 LOGI("RSP:%s",ptr);
2259 printf("RSP:%s\n",ptr);
2260 }
2261 else
2262 {
2263 printf("\nptr error.\n");
2264 return FTP_ERR_UNKNOWN;
2265 }
2266 int code = atoi(ptr);
2267 if (code / 100 == 2) // USER/PASS is 2xx
2268 {
2269 info->state = FTP_STATE_READY;
2270 LOGI("FTP logn in success.");
2271 printf("FTP logn in success.\n");
2272 }
2273 else if (code == 332) // // USER/PASS is 332
2274 {
2275 LOGW("Should set ACCT.");
2276 printf("Should set ACCT.\n");
2277 return FTP_ERR_UNKNOWN;
2278 }
2279 else
2280 {
2281 LOGE("FTP login denied[code = %d].", code);
2282 printf("FTP login denied[code = %d].\n", code);
2283 return FTP_ERR_UNKNOWN;
2284 }
2285
2286 memset(cmd,0,50);
2287 snprintf(cmd, 50, "PWD\r\n");
2288 mbtk_sock_write(info->ftp_ssl_handle,info->session,
2289 cmd,
2290 sizeof(cmd),
2291 60000,
2292 &mbtk_errno);
2293 memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf));
2294 mbtk_sock_read(info->ftp_ssl_handle,info->session,
2295 mbtk_ftp_ssl_read_buf,
2296 sizeof(mbtk_ftp_ssl_read_buf),
2297 60000,
2298 &mbtk_errno);
2299 printf("\nmbtk_sock_read PWD:\n%s\n",mbtk_ftp_ssl_read_buf);
2300 }
2301 return mbtk_errno;
2302 }
2303
2304 return ftp_login(info,&user);
2305 }
2306 else
2307 {
2308 LOGD("Has login.");
2309 return FTP_ERR_SUCCESS;
2310 }
2311}
2312
2313/*
2314 * Get current directory's path.
2315 */
2316mbtk_ftp_error_enum mbtk_ftp_pwd(mbtk_ftp_handle handle, void *path)
2317{
2318 if (!path)
2319 {
2320 LOGE("No set path");
2321 return FTP_ERR_PARAM_SET;
2322 }
2323 mbtk_ftp_info_s *info = ftp_info_find(handle);
2324 if (!info)
2325 {
2326 LOGE("No such FTP handle:%d", handle);
2327 return FTP_ERR_UNKNOWN;
2328 }
2329
2330 return ftp_cmd_process(info, FTP_CMD_PWD, NULL, path);
2331}
2332
2333/*
2334 * Go to specified directory.
2335 */
2336mbtk_ftp_error_enum mbtk_ftp_cd(mbtk_ftp_handle handle, void *path)
2337{
2338 if (!path)
2339 {
2340 LOGE("No set path");
2341 return FTP_ERR_PARAM_SET;
2342 }
2343 mbtk_ftp_info_s *info = ftp_info_find(handle);
2344 if (!info)
2345 {
2346 LOGE("No such FTP handle:%d", handle);
2347 return FTP_ERR_UNKNOWN;
2348 }
2349
2350 return ftp_cmd_process(info, FTP_CMD_CWD, path, NULL);
2351}
2352
2353/*
2354 * Get the native ip and free port.
2355 */
2356mbtk_ftp_error_enum mbtk_ftp_get_ip_and_port(char *ipBuf_out,
2357 int *port,int iptype)
2358{
2359 char psz_port_cmd[128];
2360 int i=0;
2361
2362 *port = rand() % (60000 - 50000 + 1) + 50000;
2363 sprintf(psz_port_cmd, "netstat -an | grep :%d > /dev/null", *port);
2364
2365 char ipBuf[32] = "";
2366 FILE *fstream=NULL;
2367
2368 char buff[1024];
2369 char iptype_str[8];
2370 memset(buff,0,sizeof(buff));
2371 /*eth0可以换成eth1、docker0、em1、lo等*/
2372 if(iptype == MBTK_ADDR_IPV6)
2373 {
2374
2375 if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet6 addr: 2\" | awk '{print $3}'","r")))
2376 {
2377 snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0");
2378 }
2379 if(NULL!=fgets(buff, sizeof(buff), fstream))
2380 {
2381 snprintf(ipBuf, 39, "%s",buff);
2382 }
2383 else
2384 {
2385 snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0");
2386 pclose(fstream);
2387 }
2388 }
2389 else
2390 {
2391 if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet addr:\" | awk \'{print $2}\' | cut -c 6-","r")))
2392 {
2393 snprintf(ipBuf, 18, "%s","0.0.0.0");
2394 }
2395 if(NULL!=fgets(buff, sizeof(buff), fstream))
2396 {
2397 snprintf(ipBuf, 18, "%s",buff);
2398 }
2399 else
2400 {
2401 snprintf(ipBuf, 18, "%s","0.0.0.0");
2402 pclose(fstream);
2403 }
2404 }
2405 pclose(fstream);
2406
2407 printf("ip:%s\n", ipBuf);
2408 memcpy(ipBuf_out, ipBuf, 32);
2409 return 0;
2410}
2411/*
2412 * Get current directory's subdirectory.
2413 */
2414mbtk_ftp_error_enum mbtk_ftp_dir_ls(mbtk_ftp_handle handle,
2415 mbtk_ftp_file_info_s *list_head)
2416{
2417 if (!list_head)
2418 {
2419 LOGE("No set file list.");
2420 return FTP_ERR_PARAM_SET;
2421 }
2422 mbtk_ftp_info_s *info = ftp_info_find(handle);
2423 if (!info)
2424 {
2425 LOGE("No such FTP handle:%d", handle);
2426 return FTP_ERR_UNKNOWN;
2427 }
2428
2429 return ftp_cmd_process(info, FTP_CMD_LIST, NULL, list_head);
2430}
2431
2432/*
2433 * Get specified file's size.
2434 */
2435uint32 mbtk_ftp_file_size(mbtk_ftp_handle handle, void *path)
2436{
2437 if (!path)
2438 {
2439 LOGE("No set path");
2440 return 0;
2441 }
2442 mbtk_ftp_info_s *info = ftp_info_find(handle);
2443 if (!info)
2444 {
2445 LOGE("No such FTP handle:%d", handle);
2446 return 0;
2447 }
2448 uint32 size = 0;
2449 if (ftp_cmd_process(info, FTP_CMD_SIZE, path, &size) != FTP_ERR_SUCCESS)
2450 return 0;
2451
2452 return size;
2453}
2454
2455/*
2456 * Get specified file's modify time.
2457 */
2458mbtk_ftp_error_enum mbtk_ftp_file_time(mbtk_ftp_handle handle, void *path,
2459 void *time)
2460{
2461 if (!path)
2462 {
2463 LOGE("No set path");
2464 return FTP_ERR_PARAM_SET;
2465 }
2466
2467 mbtk_ftp_info_s *info = ftp_info_find(handle);
2468 if (!info)
2469 {
2470 LOGE("No such FTP handle:%d", handle);
2471 return FTP_ERR_UNKNOWN;
2472 }
2473
2474 return ftp_cmd_process(info, FTP_CMD_MDTM, path, time);
2475}
2476
2477/*
2478 * Delete specified file.
2479 */
2480mbtk_ftp_error_enum mbtk_ftp_file_del(mbtk_ftp_handle handle, void *path)
2481{
2482 if (!path)
2483 {
2484 LOGE("No set path");
2485 return FTP_ERR_PARAM_SET;
2486 }
2487 mbtk_ftp_info_s *info = ftp_info_find(handle);
2488 if (!info)
2489 {
2490 LOGE("No such FTP handle:%d", handle);
2491 return FTP_ERR_UNKNOWN;
2492 }
2493
2494 return ftp_cmd_process(info, FTP_CMD_DELE, path, NULL);
2495}
2496
2497/*
2498 * Create specified directory.
2499 */
2500mbtk_ftp_error_enum mbtk_ftp_dir_mkdir(mbtk_ftp_handle handle, void *path)
2501{
2502 if (!path)
2503 {
2504 LOGE("No set path");
2505 return FTP_ERR_PARAM_SET;
2506 }
2507 mbtk_ftp_info_s *info = ftp_info_find(handle);
2508 if (!info)
2509 {
2510 LOGE("No such FTP handle:%d", handle);
2511 return FTP_ERR_UNKNOWN;
2512 }
2513
2514 return ftp_cmd_process(info, FTP_CMD_MKD, path, NULL);
2515}
2516
2517/*
2518 * Delete specified directory.
2519 */
2520mbtk_ftp_error_enum mbtk_ftp_dir_rmdir(mbtk_ftp_handle handle, void *path)
2521{
2522 if (!path)
2523 {
2524 LOGE("No set path");
2525 return FTP_ERR_PARAM_SET;
2526 }
2527 mbtk_ftp_info_s *info = ftp_info_find(handle);
2528 if (!info)
2529 {
2530 LOGE("No such FTP handle:%d", handle);
2531 return FTP_ERR_UNKNOWN;
2532 }
2533
2534 return ftp_cmd_process(info, FTP_CMD_RMD, path, NULL);
2535}
2536
2537/*
2538 * Set data type.
2539 */
2540mbtk_ftp_error_enum mbtk_ftp_data_type_set(mbtk_ftp_handle handle,
2541 mbtk_ftp_data_type_enum data_type)
2542{
2543 mbtk_ftp_info_s *info = ftp_info_find(handle);
2544 if (!info)
2545 {
2546 LOGE("No such FTP handle:%d", handle);
2547 return FTP_ERR_UNKNOWN;
2548 }
2549
2550 return ftp_cmd_process(info, FTP_CMD_TYPE, &data_type, NULL);
2551}
2552
2553/*
2554 * Set FTP mode.
2555 */
2556mbtk_ftp_error_enum mbtk_ftp_mode_set(mbtk_ftp_handle handle,
2557 mbtk_ftp_mode_enum mode)
2558{
2559 mbtk_ftp_info_s *info = ftp_info_find(handle);
2560 if (!info)
2561 {
2562 LOGE("No such FTP handle:%d", handle);
2563 return FTP_ERR_UNKNOWN;
2564 }
2565
2566 info->data_mode = mode;
2567
2568 return FTP_ERR_SUCCESS;
2569}
2570
2571uint32 mbtk_ftp_download_start(mbtk_ftp_handle handle, void *remote_path,
2572 void *local_path, mbtk_data_cb_func data_cb)
2573{
2574 if (!remote_path || (local_path && data_cb) || (!local_path && !data_cb))
2575 {
2576 LOGE("Param set error.");
2577 return 0;
2578 }
2579
2580 mbtk_ftp_info_s *info = ftp_info_find(handle);
2581 if (!info)
2582 {
2583 LOGE("No such FTP handle:%d", handle);
2584 return 0;
2585 }
2586
2587 if (info->state >= FTP_STATE_READY && !info->is_trans)
2588 {
2589 int retry_time = 0;
2590 memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s));
2591
2592 // Set data type to "I"
2593 if (FTP_ERR_SUCCESS
2594 != mbtk_ftp_data_type_set(handle, FTP_DATA_TYPE_I))
2595 {
2596 LOGE("Set data type to I fail.");
2597 return 0;
2598 }
2599
2600 // Get file size.
2601 info->file_trans.size_count = mbtk_ftp_file_size(handle, remote_path);
2602 if (info->file_trans.size_count > 0)
2603 {
2604 LOGI("File size:%d", info->file_trans.size_count);
2605 }
2606 else
2607 {
2608 LOGE("File not exist.");
2609 return 0;
2610 }
2611
2612 //Get file modify time.
2613 if (FTP_ERR_SUCCESS
2614 != mbtk_ftp_file_time(handle, remote_path,
2615 (char*) info->file_trans.modify_time))
2616 {
2617 LOGE("Get file modify time fail.");
2618 return 0;
2619 }
2620
2621 memcpy(info->file_trans.remote_name, remote_path,
2622 strlen((char*) remote_path));
2623 if (local_path)
2624 {
2625 memcpy(info->file_trans.local_name, local_path,
2626 strlen((char*) local_path));
2627 info->file_trans.data_cb = NULL;
2628 }
2629 else
2630 {
2631 info->file_trans.data_cb = data_cb;
2632 }
2633 info->file_trans.size_send = 0;
2634 info->file_trans.is_download = true;
2635 info->file_trans.fd = -1;
2636 info->is_trans = true;
2637
2638 if (!info->file_trans.data_cb) // Save to efs
2639 {
2640 info->file_trans.fd = file_open((const char*)info->file_trans.local_name,
2641 O_WRONLY | O_CREAT | O_TRUNC);
2642 if (info->file_trans.fd <= 0)
2643 {
2644 LOGE("Can not open EFS file[%s].", info->file_trans.local_name);
2645 return 0;
2646 }
2647 }
2648
2649 do {
2650 // Start download file.
2651 if (FTP_ERR_SUCCESS
2652 != ftp_data_sock_read(info, FTP_CMD_GET, NULL, NULL))
2653 {
2654 LOGW("ftp_data_sock_read() fail.");
2655 }
2656
2657 if(info->sock_info[FTP_SOCK_CTRL].fd <= 0) {
2658 // Download fail.
2659 LOGW("Ctrl socket error,should login angin.");
2660 break;
2661 } else if (info->file_trans.size_send == info->file_trans.size_count) {
2662 // Download success
2663 break;
2664 }
2665
2666 // Should redownload without quit.
2667 char time[20] = { 0 };
2668 if (FTP_ERR_SUCCESS
2669 != mbtk_ftp_file_time(handle, info->file_trans.remote_name,
2670 time))
2671 {
2672 LOGE("Get file modify time fail.");
2673 break;
2674 }
2675
2676 if (strcmp(time, (char*) info->file_trans.modify_time))
2677 {
2678 LOGW("Service file changed.");
2679 break;
2680 }
2681
2682 retry_time++;
2683 } while(retry_time < 5);
2684
2685
2686 if (!info->file_trans.data_cb && info->file_trans.fd > 0) // Save to efs
2687 {
2688 if (file_close(info->file_trans.fd))
2689 {
2690 LOGE("EFS close fail.");
2691 return 0;
2692 }
2693 }
2694
2695 // Download success.
2696 if (info->file_trans.size_send == info->file_trans.size_count)
2697 {
2698 LOGI("Download %s success[%d].", (char* )remote_path,
2699 info->file_trans.size_count);
2700
2701 // Reset download configs.
2702 info->is_trans = false;
2703 // memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s));
2704 }
2705 else
2706 {
2707 LOGW("Download %s fail[%d / %d].", (char* )remote_path,
2708 info->file_trans.size_send, info->file_trans.size_count);
2709 }
2710
2711 return info->file_trans.size_send;
2712 }
2713 else
2714 {
2715 LOGE("FTP state error[%d].", info->state);
2716 return 0;
2717 }
2718}
2719
2720uint32 mbtk_ftp_download_continue(mbtk_ftp_handle handle)
2721{
2722 mbtk_ftp_info_s *info = ftp_info_find(handle);
2723 if (!info)
2724 {
2725 LOGE("No such FTP handle:%d", handle);
2726 return 0;
2727 }
2728
2729 if(info->state == FTP_STATE_NON) {
2730 if(FTP_ERR_SUCCESS != ftp_login(info,&info->user)) {
2731 LOGE("FTP login fail.");
2732 return 0;
2733 }
2734 }
2735
2736 if (info->state >= FTP_STATE_READY && info->is_trans
2737 && info->file_trans.is_download
2738 && info->file_trans.size_send < info->file_trans.size_count)
2739 {
2740 // Set data type to "I"
2741 if (FTP_ERR_SUCCESS
2742 != mbtk_ftp_data_type_set(handle, FTP_DATA_TYPE_I))
2743 {
2744 LOGE("Set data type to I fail.");
2745 return 0;
2746 }
2747
2748 // Get file size.
2749 uint32 size = mbtk_ftp_file_size(handle, info->file_trans.remote_name);
2750 if (size > 0)
2751 {
2752 LOGI("File size:%d", size);
2753 }
2754 else
2755 {
2756 LOGE("File not exist.");
2757 return 0;
2758 }
2759 if (size != info->file_trans.size_count)
2760 {
2761 LOGW("Service file changed.");
2762 return 0;
2763 }
2764
2765 //Get file modify time.
2766 char time[20] = { 0 };
2767 if (FTP_ERR_SUCCESS
2768 != mbtk_ftp_file_time(handle, info->file_trans.remote_name,
2769 time))
2770 {
2771 LOGE("Get file modify time fail.");
2772 return 0;
2773 }
2774 if (strcmp(time, (char*) info->file_trans.modify_time))
2775 {
2776 LOGW("Service file changed.");
2777 return 0;
2778 }
2779
2780 if (!info->file_trans.data_cb) // Save to efs
2781 {
2782 info->file_trans.fd = file_open((const char*)info->file_trans.local_name,
2783 O_WRONLY | O_CREAT | O_APPEND);
2784 if (info->file_trans.fd <= 0)
2785 {
2786 LOGE("Can not open EFS file[%s].", info->file_trans.local_name);
2787 return FTP_ERR_EFS_FILE;
2788 }
2789 }
2790
2791 uint32 size_last = info->file_trans.size_send;
2792 // Start download file.
2793 if (FTP_ERR_SUCCESS
2794 != ftp_data_sock_read(info, FTP_CMD_GET, NULL, NULL))
2795 {
2796 LOGW("Data download fail.");
2797 }
2798
2799 if (!info->file_trans.data_cb && info->file_trans.fd > 0)
2800 {
2801 if (file_close(info->file_trans.fd))
2802 {
2803 LOGE("EFS close fail.");
2804 return 0;
2805 }
2806 }
2807
2808 // Download success.
2809 if (info->file_trans.size_send == info->file_trans.size_count)
2810 {
2811 LOGI("Download %s success[%d].", info->file_trans.remote_name,
2812 info->file_trans.size_count);
2813
2814 // Reset download configs.
2815 info->is_trans = false;
2816 //memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s));
2817 }
2818 else
2819 {
2820 LOGW("Download %s fail[%d / %d].", info->file_trans.remote_name,
2821 info->file_trans.size_send, info->file_trans.size_count);
2822 }
2823
2824 return info->file_trans.size_send - size_last;
2825 }
2826 else
2827 {
2828 LOGE("FTP state error[%d].", info->state);
2829 return 0;
2830 }
2831}
2832
2833/*
2834* Upload EFS: local_path is efs path;size_byte is 0.
2835* Upload data: local_path is NULL;size_byte is data size.
2836*/
2837int mbtk_ftp_upload_start(mbtk_ftp_handle handle, const void *remote_path,
2838 const void *local_path, uint32 size_byte)
2839{
2840 if (!remote_path || (size_byte == 0 && !local_path)
2841 || (size_byte > 0 && local_path))
2842 {
2843 LOGE("Param set error.");
2844 return -1;
2845 }
2846
2847 int result = 0;
2848 mbtk_ftp_info_s *info = ftp_info_find(handle);
2849 if (!info)
2850 {
2851 LOGE("No such FTP handle:%d", handle);
2852 return -1;
2853 }
2854
2855 LOGE("info->state:%d, info->is_trans:%d", info->state,info->is_trans);
2856
2857 if (info->state >= FTP_STATE_READY && !info->is_trans)
2858 {
2859 // Set data type to "I"
2860 if (FTP_ERR_SUCCESS
2861 != mbtk_ftp_data_type_set(handle, FTP_DATA_TYPE_I))
2862 {
2863 LOGE("Set data type to I fail.");
2864 return -1;
2865 }
2866
2867 memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s));
2868 info->file_trans.is_download = false;
2869
2870 memcpy(info->file_trans.remote_name, remote_path,
2871 strlen((char*) remote_path));
2872
2873 if (local_path)
2874 {
2875 memcpy(info->file_trans.local_name, local_path,
2876 strlen((char*) local_path));
2877 info->file_trans.fd = file_open((const char*)info->file_trans.local_name,
2878 O_RDONLY);
2879 if (info->file_trans.fd <= 0)
2880 {
2881 LOGE("Can not open EFS file[%s].", info->file_trans.local_name);
2882 return -1;
2883 }
2884
2885 }
2886 info->file_trans.size_count = size_byte;
2887 info->file_trans.size_send = 0;
2888 // Start upload data.
2889
2890
2891 // Start update file.
2892 if (FTP_ERR_SUCCESS != ftp_data_sock_read(info, FTP_CMD_PUT, NULL, NULL))
2893 {
2894 LOGW("ftp_data_sock_read() fail.");
2895 if(info->file_trans.size_count == info->file_trans.size_send && info->file_trans.size_send != 0)
2896 {
2897 result = FTP_ERR_SUCCESS;
2898 }
2899 else
2900 {
2901 mbtk_at_ftp_par.rest_size = info->file_trans.size_send;
2902 result = FTP_ERR_UNKNOWN;
2903 }
2904 }
2905
2906 if (info->file_trans.fd > 0 ) // Save to efs
2907 {
2908 info->file_trans.size_count = 0;
2909 info->file_trans.size_send = 0;
2910 if (file_close(info->file_trans.fd))
2911 {
2912 LOGE("EFS close fail.");
2913 return -1;
2914 }
2915 }
2916
2917 }
2918 else
2919 {
2920 LOGE("FTP state error[%d].", info->state);
2921 return -1;
2922 }
2923
2924 return result;
2925}
2926
2927/*
2928* This only for upload data(No for upload efs).
2929*/
2930int mbtk_ftp_upload_send(mbtk_ftp_handle handle, const void *data,uint16 data_len)
2931{
2932 if (!data || data_len == 0)
2933 {
2934 LOGE("Param set error.");
2935 return -1;
2936 }
2937
2938 int err;
2939 int result = 0;
2940 mbtk_ftp_info_s *info = ftp_info_find(handle);
2941 if (!info)
2942 {
2943 LOGE("No such FTP handle:%d", handle);
2944 return -1;
2945 }
2946
2947 if(info->file_trans.fd > 0) // Is upload from efs.
2948 {
2949 LOGE("Not upload from EFS.");
2950 return -1;
2951 }
2952
2953//LOGE("1socket:%d, data:%s, data_len:%d", info->sock_info[FTP_SOCK_DATA].fd, data, data_len );
2954 if((info->file_trans.size_send + data_len) > info->file_trans.size_count)
2955 {
2956 printf("send over set length\n");
2957 result = FTP_ERR_UNKNOWN;
2958 goto overlong;
2959 }
2960 int len;
2961 if(info->auth_type != 0)
2962 len = mbtk_sock_write(info->ftp_ssl_handle,info->session_data,data,data_len,FTP_TIMEOUT,&err);
2963 else
2964 len = sock_write(&info->net_info, &info->sock_info[FTP_SOCK_DATA], data, data_len,
2965 FTP_TIMEOUT, &err);
2966 if(len < 0)
2967 {
2968 LOGE("EFS write fail.len:%d, err;%d", len, err);
2969 return FTP_ERR_EFS_FILE;
2970 }
2971
2972 info->file_trans.size_send += len;
2973 mbtk_at_ftp_par.rest_size = info->file_trans.size_send;
2974
2975 LOGE("size_count:%d, size_send:%d.", info->file_trans.size_count,info->file_trans.size_send);
2976
2977 if((info->file_trans.size_count <= info->file_trans.size_send) )
2978 {
2979 printf("\nClose data socket begin!\n ");
2980 // Close data socket.
2981overlong: if (info->sock_info[FTP_SOCK_DATA].fd > 0)
2982 {
2983 if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err))
2984 {
2985 LOGE("Close data socket fail[%d].", err);
2986 printf("\nClose data socket fail[%d].\n", err);
2987 result = FTP_ERR_NET_CLOSE;
2988 }
2989 else
2990 {
2991 printf("\nClose data socket ok[%d].\n", err);
2992 // info->sock_info[FTP_SOCK_DATA].fd = -1;
2993 }
2994 }
2995 if(info->auth_type != 0)
2996 {
2997 if(mbtk_sock_close(info->ftp_ssl_handle,info->session_data,6000,&err))
2998 {
2999 LOGE("Close ssl data socket fail[%d].", err);
3000 printf("\nClose ssl data socket fail[%d].\n", err);
3001 result = FTP_ERR_NET_CLOSE;
3002 }
3003 else
3004 {
3005 printf("\nClose ssl data socket ok[%d].\n", err);
3006 // info->sock_info[FTP_SOCK_DATA].fd = -1;
3007 }
3008 }
3009 info->data_mode = FTP_MODE_PASSIVE;
3010 info->is_data_sock_busy = false;
3011 info->file_trans.size_count = 0;
3012 info->file_trans.size_send = 0;
3013 }
3014 else
3015 {
3016 LOGE("size_count:%d, size_send:%d.", info->file_trans.size_count,info->file_trans.size_send);
3017 }
3018
3019
3020 // Start update data.
3021
3022 return result;
3023}
3024
3025mbtk_ftp_error_enum mbtk_ftp_trans_reset(mbtk_ftp_handle handle)
3026{
3027 mbtk_ftp_info_s *info = ftp_info_find(handle);
3028 if (!info)
3029 {
3030 LOGE("No such FTP handle:%d", handle);
3031 return FTP_ERR_UNKNOWN;
3032 }
3033
3034 info->is_trans = false;
3035 memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s));
3036
3037 return FTP_ERR_SUCCESS;
3038}
3039
3040