liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /************************************************************* |
| 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 | *************************************************************/ |
| 35 | typedef 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 | |
| 52 | mbtk_at_init_parameter mbtk_at_ftp_par={0}; |
| 53 | |
| 54 | /************************************************************* |
| 55 | Variables:local |
| 56 | *************************************************************/ |
| 57 | static 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 | *************************************************************/ |
| 70 | static 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 | |
| 78 | static 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 | |
| 109 | static 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. |
| 235 | static mbtk_ftp_error_enum ftp_data_sock_service_create(mbtk_ftp_sock_s *sock) |
| 236 | { |
| 237 | return FTP_ERR_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | static 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 | |
| 267 | static 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 | |
| 308 | static 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 | |
| 353 | static 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 | |
| 431 | static 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 | */ |
| 511 | static 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 | |
| 715 | static 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 | |
| 780 | static 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 | */ |
| 835 | static 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 | |
| 870 | static 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.] |
| 1202 | read_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; |
| 1236 | sock_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 | } |
| 1248 | read_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 | |
| 1283 | end: |
| 1284 | // Default is passive. |
| 1285 | info->data_mode = FTP_MODE_PASSIVE; |
| 1286 | info->is_data_sock_busy = false; |
| 1287 | |
| 1288 | return result; |
| 1289 | } |
| 1290 | |
| 1291 | static 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 | |
| 1329 | static 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 | |
| 1345 | static 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 | |
| 1368 | static 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 | |
| 1391 | static 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 | |
| 1409 | static 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 | |
| 1429 | static 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 | |
| 1458 | static 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 | |
| 1487 | static 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 | |
| 1510 | static 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 | |
| 1523 | static 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 | |
| 1611 | static 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 | |
| 1765 | login_fail: |
| 1766 | info->state = FTP_STATE_NON; |
| 1767 | return ftp_err; |
| 1768 | } |
| 1769 | |
| 1770 | static 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 | |
| 1789 | mbtk_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 | |
| 1851 | static 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 | |
| 1889 | static 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 | *************************************************************/ |
| 1902 | mbtk_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 | |
| 1979 | mbtk_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 | |
| 2024 | mbtk_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 | |
| 2047 | mbtk_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; |
r.xiao | 3b1448f | 2023-09-27 03:19:21 -0700 | [diff] [blame^] | 2059 | if(info->auth_type == FTP_AUTH_TYPE_NON) { |
| 2060 | sock_close(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], FTP_TIMEOUT, &err); |
| 2061 | sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err); |
| 2062 | } else { |
| 2063 | mbtk_sock_deinit(info->ftp_ssl_handle); |
| 2064 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 2065 | ftp_free_func(info); |
| 2066 | } |
| 2067 | |
| 2068 | return FTP_ERR_SUCCESS; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * Quit FTP service. |
| 2073 | */ |
| 2074 | mbtk_ftp_error_enum mbtk_ftp_quit(mbtk_ftp_handle handle) |
| 2075 | { |
| 2076 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2077 | if (!info) |
| 2078 | { |
| 2079 | LOGE("No such FTP handle:%d", handle); |
| 2080 | return FTP_ERR_UNKNOWN; |
| 2081 | } |
| 2082 | |
| 2083 | int err = 0; |
| 2084 | if(info->sock_info[FTP_SOCK_CTRL].fd > 0) { |
| 2085 | mbtk_ftp_error_enum ftp_err = ftp_cmd_process(info, FTP_CMD_QUIT, NULL, |
| 2086 | NULL); |
| 2087 | if (ftp_err != FTP_ERR_SUCCESS) |
| 2088 | { |
| 2089 | LOGE("FTP QUIT fail[%d].", ftp_err); |
| 2090 | //return ftp_err; |
| 2091 | } |
| 2092 | |
| 2093 | // FTP quit success. |
| 2094 | info->state = FTP_STATE_CONNECTED; |
| 2095 | } |
| 2096 | |
r.xiao | 3b1448f | 2023-09-27 03:19:21 -0700 | [diff] [blame^] | 2097 | if(info->auth_type == FTP_AUTH_TYPE_NON) { |
| 2098 | if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_CTRL], FTP_TIMEOUT, &err)) |
| 2099 | { |
| 2100 | LOGE("Close ctrl socket fail[%d].", err); |
| 2101 | return FTP_ERR_NET_CLOSE; |
| 2102 | } |
| 2103 | |
| 2104 | if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err)) |
| 2105 | { |
| 2106 | LOGE("Close data socket fail[%d].", err); |
| 2107 | return FTP_ERR_NET_CLOSE; |
| 2108 | } |
| 2109 | } else { |
| 2110 | if(mbtk_sock_close(info->ftp_ssl_handle,info->session,6000,&err)) |
| 2111 | { |
| 2112 | LOGE("Close ssl ctrl socket fail[%d].", err); |
| 2113 | //return FTP_ERR_NET_CLOSE; |
| 2114 | } |
| 2115 | |
| 2116 | if(mbtk_sock_close(info->ftp_ssl_handle,info->session_data,6000,&err)) |
| 2117 | { |
| 2118 | LOGE("Close ssl data socket fail[%d].", err); |
| 2119 | //return FTP_ERR_NET_CLOSE; |
| 2120 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 2121 | } |
| 2122 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 2123 | |
| 2124 | info->state = FTP_STATE_NON; |
| 2125 | info->data_mode = FTP_MODE_PASSIVE; |
| 2126 | memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s)); |
| 2127 | info->sock_info[FTP_SOCK_CTRL].fd = -1; |
| 2128 | info->sock_info[FTP_SOCK_DATA].fd = -1; |
| 2129 | |
| 2130 | return FTP_ERR_SUCCESS; |
| 2131 | } |
| 2132 | |
| 2133 | mbtk_ftp_error_enum mbtk_ftp_net_close(mbtk_ftp_handle handle) |
| 2134 | { |
| 2135 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2136 | if (!info) |
| 2137 | { |
| 2138 | LOGE("No such FTP handle:%d", handle); |
| 2139 | return FTP_ERR_UNKNOWN; |
| 2140 | } |
| 2141 | |
| 2142 | if(info->net_info.net_id > 0) { |
| 2143 | int err; |
| 2144 | if(sock_net_close(&info->net_info, FTP_TIMEOUT,&err)) |
| 2145 | { |
| 2146 | LOGE("sock_net_close() fail[%ld].",err); |
| 2147 | return FTP_ERR_NET_CLOSE; |
| 2148 | } |
| 2149 | |
| 2150 | info->net_info.net_id = -1; |
| 2151 | } |
| 2152 | |
| 2153 | return FTP_ERR_SUCCESS; |
| 2154 | } |
| 2155 | |
| 2156 | |
| 2157 | mbtk_ftp_info_s* mbtk_ftp_info_get(mbtk_ftp_handle handle) |
| 2158 | { |
| 2159 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2160 | if (!info) |
| 2161 | { |
| 2162 | LOGE("No such FTP handle:%d", handle); |
| 2163 | return NULL; |
| 2164 | } |
| 2165 | |
| 2166 | return info; |
| 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | * Login specified FTP service. |
| 2171 | */ |
| 2172 | mbtk_ftp_error_enum mbtk_ftp_login(mbtk_ftp_handle handle, void *name, |
| 2173 | void *pass) |
| 2174 | { |
| 2175 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2176 | if (!info) |
| 2177 | { |
| 2178 | LOGE("No such FTP handle:%d", handle); |
| 2179 | return FTP_ERR_UNKNOWN; |
| 2180 | } |
| 2181 | |
| 2182 | if (info->state == FTP_STATE_NON) |
| 2183 | { |
| 2184 | mbtk_ftp_user_info_s user; |
| 2185 | memset(&user,0x0,sizeof(mbtk_ftp_user_info_s)); |
| 2186 | if (!str_empty(name) && !str_empty(pass)) |
| 2187 | { |
| 2188 | memcpy(user.name, name, strlen(name)); |
| 2189 | memcpy(user.pass, pass, strlen(pass)); |
| 2190 | memcpy(info->user.name, name, strlen(name)); |
| 2191 | memcpy(info->user.pass, pass, strlen(pass)); |
| 2192 | } |
| 2193 | else |
| 2194 | { |
| 2195 | memcpy(user.name, FTP_ANONYMOUS_USER, strlen(FTP_ANONYMOUS_USER)); |
| 2196 | memcpy(user.pass, FTP_ANONYMOUS_PASS, strlen(FTP_ANONYMOUS_PASS)); |
| 2197 | memcpy(info->user.name, FTP_ANONYMOUS_USER, strlen(FTP_ANONYMOUS_USER)); |
| 2198 | memcpy(info->user.pass, FTP_ANONYMOUS_PASS, strlen(FTP_ANONYMOUS_PASS)); |
| 2199 | } |
| 2200 | |
| 2201 | LOGI("FTP login#user:%s,pass:%s",user.name,user.pass); |
| 2202 | |
| 2203 | if(info->auth_type != 0) |
| 2204 | { |
| 2205 | int mbtk_errno=-1; |
| 2206 | info->session = mbtk_sock_open(info->ftp_ssl_handle,info->ftp_sock_ssl_info,60000,&mbtk_errno); |
| 2207 | if(mbtk_errno!=0) |
| 2208 | { |
| 2209 | printf("mbtk_sock_open error : %d\n",mbtk_errno); |
| 2210 | } |
| 2211 | else |
| 2212 | { |
| 2213 | unsigned char mbtk_ftp_ssl_read_buf[16384 + 1]; |
| 2214 | char cmd[50]; |
| 2215 | memset(cmd,0,50); |
| 2216 | int len_ssl; |
| 2217 | |
| 2218 | |
| 2219 | memset(cmd,0,50); |
| 2220 | snprintf(cmd, 50, "PBSZ 0\r\n"); |
| 2221 | mbtk_sock_write(info->ftp_ssl_handle,info->session, |
| 2222 | cmd, |
| 2223 | sizeof(cmd), |
| 2224 | 60000, |
| 2225 | &mbtk_errno); |
| 2226 | memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf)); |
| 2227 | mbtk_sock_read(info->ftp_ssl_handle,info->session, |
| 2228 | mbtk_ftp_ssl_read_buf, |
| 2229 | sizeof(mbtk_ftp_ssl_read_buf), |
| 2230 | 60000, |
| 2231 | &mbtk_errno); |
| 2232 | printf("\nmbtk_sock_read PBSZ 0:\n%s\n",mbtk_ftp_ssl_read_buf); |
| 2233 | memset(cmd,0,50); |
| 2234 | snprintf(cmd, 50, "PROT P\r\n"); |
| 2235 | mbtk_sock_write(info->ftp_ssl_handle,info->session, |
| 2236 | cmd, |
| 2237 | sizeof(cmd), |
| 2238 | 60000, |
| 2239 | &mbtk_errno); |
| 2240 | memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf)); |
| 2241 | mbtk_sock_read(info->ftp_ssl_handle,info->session, |
| 2242 | mbtk_ftp_ssl_read_buf, |
| 2243 | sizeof(mbtk_ftp_ssl_read_buf), |
| 2244 | 60000, |
| 2245 | &mbtk_errno); |
| 2246 | printf("\nmbtk_sock_read PROT P:\n%s\n",mbtk_ftp_ssl_read_buf); |
| 2247 | memset(cmd,0,50); |
| 2248 | snprintf(cmd, 50, "USER %s\r\n",info->user.name); |
| 2249 | mbtk_sock_write(info->ftp_ssl_handle,info->session, |
| 2250 | cmd, |
| 2251 | sizeof(cmd), |
| 2252 | 60000, |
| 2253 | &mbtk_errno); |
| 2254 | memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf)); |
| 2255 | mbtk_sock_read(info->ftp_ssl_handle,info->session, |
| 2256 | mbtk_ftp_ssl_read_buf, |
| 2257 | sizeof(mbtk_ftp_ssl_read_buf), |
| 2258 | 60000, |
| 2259 | &mbtk_errno); |
| 2260 | printf("\nmbtk_sock_read USER:\n%s\n",mbtk_ftp_ssl_read_buf); |
| 2261 | memset(cmd,0,50); |
| 2262 | snprintf(cmd, 50, "PASS %s\r\n",info->user.pass); |
| 2263 | mbtk_sock_write(info->ftp_ssl_handle,info->session, |
| 2264 | cmd, |
| 2265 | sizeof(cmd), |
| 2266 | 60000, |
| 2267 | &mbtk_errno); |
| 2268 | memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf)); |
| 2269 | mbtk_sock_read(info->ftp_ssl_handle,info->session, |
| 2270 | mbtk_ftp_ssl_read_buf, |
| 2271 | sizeof(mbtk_ftp_ssl_read_buf), |
| 2272 | 60000, |
| 2273 | &mbtk_errno); |
| 2274 | printf("\nmbtk_sock_read PASS:\n%s\n",mbtk_ftp_ssl_read_buf); |
| 2275 | char *ptr = NULL; |
| 2276 | if((ptr = strstr(mbtk_ftp_ssl_read_buf,"220 ")) || (ptr = strstr(mbtk_ftp_ssl_read_buf,"230 "))) { |
| 2277 | LOGI("RSP:%s",ptr); |
| 2278 | printf("RSP:%s\n",ptr); |
| 2279 | } |
| 2280 | else |
| 2281 | { |
| 2282 | printf("\nptr error.\n"); |
| 2283 | return FTP_ERR_UNKNOWN; |
| 2284 | } |
| 2285 | int code = atoi(ptr); |
| 2286 | if (code / 100 == 2) // USER/PASS is 2xx |
| 2287 | { |
| 2288 | info->state = FTP_STATE_READY; |
| 2289 | LOGI("FTP logn in success."); |
| 2290 | printf("FTP logn in success.\n"); |
| 2291 | } |
| 2292 | else if (code == 332) // // USER/PASS is 332 |
| 2293 | { |
| 2294 | LOGW("Should set ACCT."); |
| 2295 | printf("Should set ACCT.\n"); |
| 2296 | return FTP_ERR_UNKNOWN; |
| 2297 | } |
| 2298 | else |
| 2299 | { |
| 2300 | LOGE("FTP login denied[code = %d].", code); |
| 2301 | printf("FTP login denied[code = %d].\n", code); |
| 2302 | return FTP_ERR_UNKNOWN; |
| 2303 | } |
| 2304 | |
| 2305 | memset(cmd,0,50); |
| 2306 | snprintf(cmd, 50, "PWD\r\n"); |
| 2307 | mbtk_sock_write(info->ftp_ssl_handle,info->session, |
| 2308 | cmd, |
| 2309 | sizeof(cmd), |
| 2310 | 60000, |
| 2311 | &mbtk_errno); |
| 2312 | memset(mbtk_ftp_ssl_read_buf,0,sizeof(mbtk_ftp_ssl_read_buf)); |
| 2313 | mbtk_sock_read(info->ftp_ssl_handle,info->session, |
| 2314 | mbtk_ftp_ssl_read_buf, |
| 2315 | sizeof(mbtk_ftp_ssl_read_buf), |
| 2316 | 60000, |
| 2317 | &mbtk_errno); |
| 2318 | printf("\nmbtk_sock_read PWD:\n%s\n",mbtk_ftp_ssl_read_buf); |
| 2319 | } |
| 2320 | return mbtk_errno; |
| 2321 | } |
| 2322 | |
| 2323 | return ftp_login(info,&user); |
| 2324 | } |
| 2325 | else |
| 2326 | { |
| 2327 | LOGD("Has login."); |
| 2328 | return FTP_ERR_SUCCESS; |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | /* |
| 2333 | * Get current directory's path. |
| 2334 | */ |
| 2335 | mbtk_ftp_error_enum mbtk_ftp_pwd(mbtk_ftp_handle handle, void *path) |
| 2336 | { |
| 2337 | if (!path) |
| 2338 | { |
| 2339 | LOGE("No set path"); |
| 2340 | return FTP_ERR_PARAM_SET; |
| 2341 | } |
| 2342 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2343 | if (!info) |
| 2344 | { |
| 2345 | LOGE("No such FTP handle:%d", handle); |
| 2346 | return FTP_ERR_UNKNOWN; |
| 2347 | } |
| 2348 | |
| 2349 | return ftp_cmd_process(info, FTP_CMD_PWD, NULL, path); |
| 2350 | } |
| 2351 | |
| 2352 | /* |
| 2353 | * Go to specified directory. |
| 2354 | */ |
| 2355 | mbtk_ftp_error_enum mbtk_ftp_cd(mbtk_ftp_handle handle, void *path) |
| 2356 | { |
| 2357 | if (!path) |
| 2358 | { |
| 2359 | LOGE("No set path"); |
| 2360 | return FTP_ERR_PARAM_SET; |
| 2361 | } |
| 2362 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2363 | if (!info) |
| 2364 | { |
| 2365 | LOGE("No such FTP handle:%d", handle); |
| 2366 | return FTP_ERR_UNKNOWN; |
| 2367 | } |
| 2368 | |
| 2369 | return ftp_cmd_process(info, FTP_CMD_CWD, path, NULL); |
| 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * Get the native ip and free port. |
| 2374 | */ |
| 2375 | mbtk_ftp_error_enum mbtk_ftp_get_ip_and_port(char *ipBuf_out, |
| 2376 | int *port,int iptype) |
| 2377 | { |
| 2378 | char psz_port_cmd[128]; |
| 2379 | int i=0; |
| 2380 | |
| 2381 | *port = rand() % (60000 - 50000 + 1) + 50000; |
| 2382 | sprintf(psz_port_cmd, "netstat -an | grep :%d > /dev/null", *port); |
| 2383 | |
| 2384 | char ipBuf[32] = ""; |
| 2385 | FILE *fstream=NULL; |
| 2386 | |
| 2387 | char buff[1024]; |
| 2388 | char iptype_str[8]; |
| 2389 | memset(buff,0,sizeof(buff)); |
| 2390 | /*eth0可以换成eth1、docker0、em1、lo等*/ |
| 2391 | if(iptype == MBTK_ADDR_IPV6) |
| 2392 | { |
| 2393 | |
| 2394 | if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet6 addr: 2\" | awk '{print $3}'","r"))) |
| 2395 | { |
| 2396 | snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0"); |
| 2397 | } |
| 2398 | if(NULL!=fgets(buff, sizeof(buff), fstream)) |
| 2399 | { |
| 2400 | snprintf(ipBuf, 39, "%s",buff); |
| 2401 | } |
| 2402 | else |
| 2403 | { |
| 2404 | snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0"); |
| 2405 | pclose(fstream); |
| 2406 | } |
| 2407 | } |
| 2408 | else |
| 2409 | { |
| 2410 | if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet addr:\" | awk \'{print $2}\' | cut -c 6-","r"))) |
| 2411 | { |
| 2412 | snprintf(ipBuf, 18, "%s","0.0.0.0"); |
| 2413 | } |
| 2414 | if(NULL!=fgets(buff, sizeof(buff), fstream)) |
| 2415 | { |
| 2416 | snprintf(ipBuf, 18, "%s",buff); |
| 2417 | } |
| 2418 | else |
| 2419 | { |
| 2420 | snprintf(ipBuf, 18, "%s","0.0.0.0"); |
| 2421 | pclose(fstream); |
| 2422 | } |
| 2423 | } |
| 2424 | pclose(fstream); |
| 2425 | |
| 2426 | printf("ip:%s\n", ipBuf); |
| 2427 | memcpy(ipBuf_out, ipBuf, 32); |
| 2428 | return 0; |
| 2429 | } |
| 2430 | /* |
| 2431 | * Get current directory's subdirectory. |
| 2432 | */ |
| 2433 | mbtk_ftp_error_enum mbtk_ftp_dir_ls(mbtk_ftp_handle handle, |
| 2434 | mbtk_ftp_file_info_s *list_head) |
| 2435 | { |
| 2436 | if (!list_head) |
| 2437 | { |
| 2438 | LOGE("No set file list."); |
| 2439 | return FTP_ERR_PARAM_SET; |
| 2440 | } |
| 2441 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2442 | if (!info) |
| 2443 | { |
| 2444 | LOGE("No such FTP handle:%d", handle); |
| 2445 | return FTP_ERR_UNKNOWN; |
| 2446 | } |
| 2447 | |
| 2448 | return ftp_cmd_process(info, FTP_CMD_LIST, NULL, list_head); |
| 2449 | } |
| 2450 | |
| 2451 | /* |
| 2452 | * Get specified file's size. |
| 2453 | */ |
| 2454 | uint32 mbtk_ftp_file_size(mbtk_ftp_handle handle, void *path) |
| 2455 | { |
| 2456 | if (!path) |
| 2457 | { |
| 2458 | LOGE("No set path"); |
| 2459 | return 0; |
| 2460 | } |
| 2461 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2462 | if (!info) |
| 2463 | { |
| 2464 | LOGE("No such FTP handle:%d", handle); |
| 2465 | return 0; |
| 2466 | } |
| 2467 | uint32 size = 0; |
| 2468 | if (ftp_cmd_process(info, FTP_CMD_SIZE, path, &size) != FTP_ERR_SUCCESS) |
| 2469 | return 0; |
| 2470 | |
| 2471 | return size; |
| 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * Get specified file's modify time. |
| 2476 | */ |
| 2477 | mbtk_ftp_error_enum mbtk_ftp_file_time(mbtk_ftp_handle handle, void *path, |
| 2478 | void *time) |
| 2479 | { |
| 2480 | if (!path) |
| 2481 | { |
| 2482 | LOGE("No set path"); |
| 2483 | return FTP_ERR_PARAM_SET; |
| 2484 | } |
| 2485 | |
| 2486 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2487 | if (!info) |
| 2488 | { |
| 2489 | LOGE("No such FTP handle:%d", handle); |
| 2490 | return FTP_ERR_UNKNOWN; |
| 2491 | } |
| 2492 | |
| 2493 | return ftp_cmd_process(info, FTP_CMD_MDTM, path, time); |
| 2494 | } |
| 2495 | |
| 2496 | /* |
| 2497 | * Delete specified file. |
| 2498 | */ |
| 2499 | mbtk_ftp_error_enum mbtk_ftp_file_del(mbtk_ftp_handle handle, void *path) |
| 2500 | { |
| 2501 | if (!path) |
| 2502 | { |
| 2503 | LOGE("No set path"); |
| 2504 | return FTP_ERR_PARAM_SET; |
| 2505 | } |
| 2506 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2507 | if (!info) |
| 2508 | { |
| 2509 | LOGE("No such FTP handle:%d", handle); |
| 2510 | return FTP_ERR_UNKNOWN; |
| 2511 | } |
| 2512 | |
| 2513 | return ftp_cmd_process(info, FTP_CMD_DELE, path, NULL); |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * Create specified directory. |
| 2518 | */ |
| 2519 | mbtk_ftp_error_enum mbtk_ftp_dir_mkdir(mbtk_ftp_handle handle, void *path) |
| 2520 | { |
| 2521 | if (!path) |
| 2522 | { |
| 2523 | LOGE("No set path"); |
| 2524 | return FTP_ERR_PARAM_SET; |
| 2525 | } |
| 2526 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2527 | if (!info) |
| 2528 | { |
| 2529 | LOGE("No such FTP handle:%d", handle); |
| 2530 | return FTP_ERR_UNKNOWN; |
| 2531 | } |
| 2532 | |
| 2533 | return ftp_cmd_process(info, FTP_CMD_MKD, path, NULL); |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * Delete specified directory. |
| 2538 | */ |
| 2539 | mbtk_ftp_error_enum mbtk_ftp_dir_rmdir(mbtk_ftp_handle handle, void *path) |
| 2540 | { |
| 2541 | if (!path) |
| 2542 | { |
| 2543 | LOGE("No set path"); |
| 2544 | return FTP_ERR_PARAM_SET; |
| 2545 | } |
| 2546 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2547 | if (!info) |
| 2548 | { |
| 2549 | LOGE("No such FTP handle:%d", handle); |
| 2550 | return FTP_ERR_UNKNOWN; |
| 2551 | } |
| 2552 | |
| 2553 | return ftp_cmd_process(info, FTP_CMD_RMD, path, NULL); |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * Set data type. |
| 2558 | */ |
| 2559 | mbtk_ftp_error_enum mbtk_ftp_data_type_set(mbtk_ftp_handle handle, |
| 2560 | mbtk_ftp_data_type_enum data_type) |
| 2561 | { |
| 2562 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2563 | if (!info) |
| 2564 | { |
| 2565 | LOGE("No such FTP handle:%d", handle); |
| 2566 | return FTP_ERR_UNKNOWN; |
| 2567 | } |
| 2568 | |
| 2569 | return ftp_cmd_process(info, FTP_CMD_TYPE, &data_type, NULL); |
| 2570 | } |
| 2571 | |
| 2572 | /* |
| 2573 | * Set FTP mode. |
| 2574 | */ |
| 2575 | mbtk_ftp_error_enum mbtk_ftp_mode_set(mbtk_ftp_handle handle, |
| 2576 | mbtk_ftp_mode_enum mode) |
| 2577 | { |
| 2578 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2579 | if (!info) |
| 2580 | { |
| 2581 | LOGE("No such FTP handle:%d", handle); |
| 2582 | return FTP_ERR_UNKNOWN; |
| 2583 | } |
| 2584 | |
| 2585 | info->data_mode = mode; |
| 2586 | |
| 2587 | return FTP_ERR_SUCCESS; |
| 2588 | } |
| 2589 | |
| 2590 | uint32 mbtk_ftp_download_start(mbtk_ftp_handle handle, void *remote_path, |
| 2591 | void *local_path, mbtk_data_cb_func data_cb) |
| 2592 | { |
| 2593 | if (!remote_path || (local_path && data_cb) || (!local_path && !data_cb)) |
| 2594 | { |
| 2595 | LOGE("Param set error."); |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
| 2599 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2600 | if (!info) |
| 2601 | { |
| 2602 | LOGE("No such FTP handle:%d", handle); |
| 2603 | return 0; |
| 2604 | } |
| 2605 | |
| 2606 | if (info->state >= FTP_STATE_READY && !info->is_trans) |
| 2607 | { |
| 2608 | int retry_time = 0; |
| 2609 | memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s)); |
| 2610 | |
| 2611 | // Set data type to "I" |
| 2612 | if (FTP_ERR_SUCCESS |
| 2613 | != mbtk_ftp_data_type_set(handle, FTP_DATA_TYPE_I)) |
| 2614 | { |
| 2615 | LOGE("Set data type to I fail."); |
| 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | // Get file size. |
| 2620 | info->file_trans.size_count = mbtk_ftp_file_size(handle, remote_path); |
| 2621 | if (info->file_trans.size_count > 0) |
| 2622 | { |
| 2623 | LOGI("File size:%d", info->file_trans.size_count); |
| 2624 | } |
| 2625 | else |
| 2626 | { |
| 2627 | LOGE("File not exist."); |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | //Get file modify time. |
| 2632 | if (FTP_ERR_SUCCESS |
| 2633 | != mbtk_ftp_file_time(handle, remote_path, |
| 2634 | (char*) info->file_trans.modify_time)) |
| 2635 | { |
| 2636 | LOGE("Get file modify time fail."); |
| 2637 | return 0; |
| 2638 | } |
| 2639 | |
| 2640 | memcpy(info->file_trans.remote_name, remote_path, |
| 2641 | strlen((char*) remote_path)); |
| 2642 | if (local_path) |
| 2643 | { |
| 2644 | memcpy(info->file_trans.local_name, local_path, |
| 2645 | strlen((char*) local_path)); |
| 2646 | info->file_trans.data_cb = NULL; |
| 2647 | } |
| 2648 | else |
| 2649 | { |
| 2650 | info->file_trans.data_cb = data_cb; |
| 2651 | } |
| 2652 | info->file_trans.size_send = 0; |
| 2653 | info->file_trans.is_download = true; |
| 2654 | info->file_trans.fd = -1; |
| 2655 | info->is_trans = true; |
| 2656 | |
| 2657 | if (!info->file_trans.data_cb) // Save to efs |
| 2658 | { |
| 2659 | info->file_trans.fd = file_open((const char*)info->file_trans.local_name, |
| 2660 | O_WRONLY | O_CREAT | O_TRUNC); |
| 2661 | if (info->file_trans.fd <= 0) |
| 2662 | { |
| 2663 | LOGE("Can not open EFS file[%s].", info->file_trans.local_name); |
| 2664 | return 0; |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | do { |
| 2669 | // Start download file. |
| 2670 | if (FTP_ERR_SUCCESS |
| 2671 | != ftp_data_sock_read(info, FTP_CMD_GET, NULL, NULL)) |
| 2672 | { |
| 2673 | LOGW("ftp_data_sock_read() fail."); |
| 2674 | } |
| 2675 | |
| 2676 | if(info->sock_info[FTP_SOCK_CTRL].fd <= 0) { |
| 2677 | // Download fail. |
| 2678 | LOGW("Ctrl socket error,should login angin."); |
| 2679 | break; |
| 2680 | } else if (info->file_trans.size_send == info->file_trans.size_count) { |
| 2681 | // Download success |
| 2682 | break; |
| 2683 | } |
| 2684 | |
| 2685 | // Should redownload without quit. |
| 2686 | char time[20] = { 0 }; |
| 2687 | if (FTP_ERR_SUCCESS |
| 2688 | != mbtk_ftp_file_time(handle, info->file_trans.remote_name, |
| 2689 | time)) |
| 2690 | { |
| 2691 | LOGE("Get file modify time fail."); |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | if (strcmp(time, (char*) info->file_trans.modify_time)) |
| 2696 | { |
| 2697 | LOGW("Service file changed."); |
| 2698 | break; |
| 2699 | } |
| 2700 | |
| 2701 | retry_time++; |
| 2702 | } while(retry_time < 5); |
| 2703 | |
| 2704 | |
| 2705 | if (!info->file_trans.data_cb && info->file_trans.fd > 0) // Save to efs |
| 2706 | { |
| 2707 | if (file_close(info->file_trans.fd)) |
| 2708 | { |
| 2709 | LOGE("EFS close fail."); |
| 2710 | return 0; |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | // Download success. |
| 2715 | if (info->file_trans.size_send == info->file_trans.size_count) |
| 2716 | { |
| 2717 | LOGI("Download %s success[%d].", (char* )remote_path, |
| 2718 | info->file_trans.size_count); |
| 2719 | |
| 2720 | // Reset download configs. |
| 2721 | info->is_trans = false; |
| 2722 | // memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s)); |
| 2723 | } |
| 2724 | else |
| 2725 | { |
| 2726 | LOGW("Download %s fail[%d / %d].", (char* )remote_path, |
| 2727 | info->file_trans.size_send, info->file_trans.size_count); |
| 2728 | } |
| 2729 | |
| 2730 | return info->file_trans.size_send; |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | LOGE("FTP state error[%d].", info->state); |
| 2735 | return 0; |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | uint32 mbtk_ftp_download_continue(mbtk_ftp_handle handle) |
| 2740 | { |
| 2741 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2742 | if (!info) |
| 2743 | { |
| 2744 | LOGE("No such FTP handle:%d", handle); |
| 2745 | return 0; |
| 2746 | } |
| 2747 | |
| 2748 | if(info->state == FTP_STATE_NON) { |
| 2749 | if(FTP_ERR_SUCCESS != ftp_login(info,&info->user)) { |
| 2750 | LOGE("FTP login fail."); |
| 2751 | return 0; |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | if (info->state >= FTP_STATE_READY && info->is_trans |
| 2756 | && info->file_trans.is_download |
| 2757 | && info->file_trans.size_send < info->file_trans.size_count) |
| 2758 | { |
| 2759 | // Set data type to "I" |
| 2760 | if (FTP_ERR_SUCCESS |
| 2761 | != mbtk_ftp_data_type_set(handle, FTP_DATA_TYPE_I)) |
| 2762 | { |
| 2763 | LOGE("Set data type to I fail."); |
| 2764 | return 0; |
| 2765 | } |
| 2766 | |
| 2767 | // Get file size. |
| 2768 | uint32 size = mbtk_ftp_file_size(handle, info->file_trans.remote_name); |
| 2769 | if (size > 0) |
| 2770 | { |
| 2771 | LOGI("File size:%d", size); |
| 2772 | } |
| 2773 | else |
| 2774 | { |
| 2775 | LOGE("File not exist."); |
| 2776 | return 0; |
| 2777 | } |
| 2778 | if (size != info->file_trans.size_count) |
| 2779 | { |
| 2780 | LOGW("Service file changed."); |
| 2781 | return 0; |
| 2782 | } |
| 2783 | |
| 2784 | //Get file modify time. |
| 2785 | char time[20] = { 0 }; |
| 2786 | if (FTP_ERR_SUCCESS |
| 2787 | != mbtk_ftp_file_time(handle, info->file_trans.remote_name, |
| 2788 | time)) |
| 2789 | { |
| 2790 | LOGE("Get file modify time fail."); |
| 2791 | return 0; |
| 2792 | } |
| 2793 | if (strcmp(time, (char*) info->file_trans.modify_time)) |
| 2794 | { |
| 2795 | LOGW("Service file changed."); |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | if (!info->file_trans.data_cb) // Save to efs |
| 2800 | { |
| 2801 | info->file_trans.fd = file_open((const char*)info->file_trans.local_name, |
| 2802 | O_WRONLY | O_CREAT | O_APPEND); |
| 2803 | if (info->file_trans.fd <= 0) |
| 2804 | { |
| 2805 | LOGE("Can not open EFS file[%s].", info->file_trans.local_name); |
| 2806 | return FTP_ERR_EFS_FILE; |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | uint32 size_last = info->file_trans.size_send; |
| 2811 | // Start download file. |
| 2812 | if (FTP_ERR_SUCCESS |
| 2813 | != ftp_data_sock_read(info, FTP_CMD_GET, NULL, NULL)) |
| 2814 | { |
| 2815 | LOGW("Data download fail."); |
| 2816 | } |
| 2817 | |
| 2818 | if (!info->file_trans.data_cb && info->file_trans.fd > 0) |
| 2819 | { |
| 2820 | if (file_close(info->file_trans.fd)) |
| 2821 | { |
| 2822 | LOGE("EFS close fail."); |
| 2823 | return 0; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | // Download success. |
| 2828 | if (info->file_trans.size_send == info->file_trans.size_count) |
| 2829 | { |
| 2830 | LOGI("Download %s success[%d].", info->file_trans.remote_name, |
| 2831 | info->file_trans.size_count); |
| 2832 | |
| 2833 | // Reset download configs. |
| 2834 | info->is_trans = false; |
| 2835 | //memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s)); |
| 2836 | } |
| 2837 | else |
| 2838 | { |
| 2839 | LOGW("Download %s fail[%d / %d].", info->file_trans.remote_name, |
| 2840 | info->file_trans.size_send, info->file_trans.size_count); |
| 2841 | } |
| 2842 | |
| 2843 | return info->file_trans.size_send - size_last; |
| 2844 | } |
| 2845 | else |
| 2846 | { |
| 2847 | LOGE("FTP state error[%d].", info->state); |
| 2848 | return 0; |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | /* |
| 2853 | * Upload EFS: local_path is efs path;size_byte is 0. |
| 2854 | * Upload data: local_path is NULL;size_byte is data size. |
| 2855 | */ |
| 2856 | int mbtk_ftp_upload_start(mbtk_ftp_handle handle, const void *remote_path, |
| 2857 | const void *local_path, uint32 size_byte) |
| 2858 | { |
| 2859 | if (!remote_path || (size_byte == 0 && !local_path) |
| 2860 | || (size_byte > 0 && local_path)) |
| 2861 | { |
| 2862 | LOGE("Param set error."); |
| 2863 | return -1; |
| 2864 | } |
| 2865 | |
| 2866 | int result = 0; |
| 2867 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2868 | if (!info) |
| 2869 | { |
| 2870 | LOGE("No such FTP handle:%d", handle); |
| 2871 | return -1; |
| 2872 | } |
| 2873 | |
| 2874 | LOGE("info->state:%d, info->is_trans:%d", info->state,info->is_trans); |
| 2875 | |
| 2876 | if (info->state >= FTP_STATE_READY && !info->is_trans) |
| 2877 | { |
| 2878 | // Set data type to "I" |
| 2879 | if (FTP_ERR_SUCCESS |
| 2880 | != mbtk_ftp_data_type_set(handle, FTP_DATA_TYPE_I)) |
| 2881 | { |
| 2882 | LOGE("Set data type to I fail."); |
| 2883 | return -1; |
| 2884 | } |
| 2885 | |
| 2886 | memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s)); |
| 2887 | info->file_trans.is_download = false; |
| 2888 | |
| 2889 | memcpy(info->file_trans.remote_name, remote_path, |
| 2890 | strlen((char*) remote_path)); |
| 2891 | |
| 2892 | if (local_path) |
| 2893 | { |
| 2894 | memcpy(info->file_trans.local_name, local_path, |
| 2895 | strlen((char*) local_path)); |
| 2896 | info->file_trans.fd = file_open((const char*)info->file_trans.local_name, |
| 2897 | O_RDONLY); |
| 2898 | if (info->file_trans.fd <= 0) |
| 2899 | { |
| 2900 | LOGE("Can not open EFS file[%s].", info->file_trans.local_name); |
| 2901 | return -1; |
| 2902 | } |
| 2903 | |
| 2904 | } |
| 2905 | info->file_trans.size_count = size_byte; |
| 2906 | info->file_trans.size_send = 0; |
| 2907 | // Start upload data. |
| 2908 | |
| 2909 | |
| 2910 | // Start update file. |
| 2911 | if (FTP_ERR_SUCCESS != ftp_data_sock_read(info, FTP_CMD_PUT, NULL, NULL)) |
| 2912 | { |
| 2913 | LOGW("ftp_data_sock_read() fail."); |
| 2914 | if(info->file_trans.size_count == info->file_trans.size_send && info->file_trans.size_send != 0) |
| 2915 | { |
| 2916 | result = FTP_ERR_SUCCESS; |
| 2917 | } |
| 2918 | else |
| 2919 | { |
| 2920 | mbtk_at_ftp_par.rest_size = info->file_trans.size_send; |
| 2921 | result = FTP_ERR_UNKNOWN; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | if (info->file_trans.fd > 0 ) // Save to efs |
| 2926 | { |
| 2927 | info->file_trans.size_count = 0; |
| 2928 | info->file_trans.size_send = 0; |
| 2929 | if (file_close(info->file_trans.fd)) |
| 2930 | { |
| 2931 | LOGE("EFS close fail."); |
| 2932 | return -1; |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | } |
| 2937 | else |
| 2938 | { |
| 2939 | LOGE("FTP state error[%d].", info->state); |
| 2940 | return -1; |
| 2941 | } |
| 2942 | |
| 2943 | return result; |
| 2944 | } |
| 2945 | |
| 2946 | /* |
| 2947 | * This only for upload data(No for upload efs). |
| 2948 | */ |
| 2949 | int mbtk_ftp_upload_send(mbtk_ftp_handle handle, const void *data,uint16 data_len) |
| 2950 | { |
| 2951 | if (!data || data_len == 0) |
| 2952 | { |
| 2953 | LOGE("Param set error."); |
| 2954 | return -1; |
| 2955 | } |
| 2956 | |
| 2957 | int err; |
| 2958 | int result = 0; |
| 2959 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 2960 | if (!info) |
| 2961 | { |
| 2962 | LOGE("No such FTP handle:%d", handle); |
| 2963 | return -1; |
| 2964 | } |
| 2965 | |
| 2966 | if(info->file_trans.fd > 0) // Is upload from efs. |
| 2967 | { |
| 2968 | LOGE("Not upload from EFS."); |
| 2969 | return -1; |
| 2970 | } |
| 2971 | |
| 2972 | //LOGE("1socket:%d, data:%s, data_len:%d", info->sock_info[FTP_SOCK_DATA].fd, data, data_len ); |
| 2973 | if((info->file_trans.size_send + data_len) > info->file_trans.size_count) |
| 2974 | { |
| 2975 | printf("send over set length\n"); |
| 2976 | result = FTP_ERR_UNKNOWN; |
| 2977 | goto overlong; |
| 2978 | } |
| 2979 | int len; |
| 2980 | if(info->auth_type != 0) |
| 2981 | len = mbtk_sock_write(info->ftp_ssl_handle,info->session_data,data,data_len,FTP_TIMEOUT,&err); |
| 2982 | else |
| 2983 | len = sock_write(&info->net_info, &info->sock_info[FTP_SOCK_DATA], data, data_len, |
| 2984 | FTP_TIMEOUT, &err); |
| 2985 | if(len < 0) |
| 2986 | { |
| 2987 | LOGE("EFS write fail.len:%d, err;%d", len, err); |
| 2988 | return FTP_ERR_EFS_FILE; |
| 2989 | } |
| 2990 | |
| 2991 | info->file_trans.size_send += len; |
| 2992 | mbtk_at_ftp_par.rest_size = info->file_trans.size_send; |
| 2993 | |
| 2994 | LOGE("size_count:%d, size_send:%d.", info->file_trans.size_count,info->file_trans.size_send); |
| 2995 | |
| 2996 | if((info->file_trans.size_count <= info->file_trans.size_send) ) |
| 2997 | { |
| 2998 | printf("\nClose data socket begin!\n "); |
| 2999 | // Close data socket. |
| 3000 | overlong: if (info->sock_info[FTP_SOCK_DATA].fd > 0) |
| 3001 | { |
| 3002 | if (sock_close(&info->net_info, &info->sock_info[FTP_SOCK_DATA], FTP_TIMEOUT, &err)) |
| 3003 | { |
| 3004 | LOGE("Close data socket fail[%d].", err); |
| 3005 | printf("\nClose data socket fail[%d].\n", err); |
| 3006 | result = FTP_ERR_NET_CLOSE; |
| 3007 | } |
| 3008 | else |
| 3009 | { |
| 3010 | printf("\nClose data socket ok[%d].\n", err); |
| 3011 | // info->sock_info[FTP_SOCK_DATA].fd = -1; |
| 3012 | } |
| 3013 | } |
| 3014 | if(info->auth_type != 0) |
| 3015 | { |
| 3016 | if(mbtk_sock_close(info->ftp_ssl_handle,info->session_data,6000,&err)) |
| 3017 | { |
| 3018 | LOGE("Close ssl data socket fail[%d].", err); |
| 3019 | printf("\nClose ssl data socket fail[%d].\n", err); |
| 3020 | result = FTP_ERR_NET_CLOSE; |
| 3021 | } |
| 3022 | else |
| 3023 | { |
| 3024 | printf("\nClose ssl data socket ok[%d].\n", err); |
| 3025 | // info->sock_info[FTP_SOCK_DATA].fd = -1; |
| 3026 | } |
| 3027 | } |
| 3028 | info->data_mode = FTP_MODE_PASSIVE; |
| 3029 | info->is_data_sock_busy = false; |
| 3030 | info->file_trans.size_count = 0; |
| 3031 | info->file_trans.size_send = 0; |
| 3032 | } |
| 3033 | else |
| 3034 | { |
| 3035 | LOGE("size_count:%d, size_send:%d.", info->file_trans.size_count,info->file_trans.size_send); |
| 3036 | } |
| 3037 | |
| 3038 | |
| 3039 | // Start update data. |
| 3040 | |
| 3041 | return result; |
| 3042 | } |
| 3043 | |
| 3044 | mbtk_ftp_error_enum mbtk_ftp_trans_reset(mbtk_ftp_handle handle) |
| 3045 | { |
| 3046 | mbtk_ftp_info_s *info = ftp_info_find(handle); |
| 3047 | if (!info) |
| 3048 | { |
| 3049 | LOGE("No such FTP handle:%d", handle); |
| 3050 | return FTP_ERR_UNKNOWN; |
| 3051 | } |
| 3052 | |
| 3053 | info->is_trans = false; |
| 3054 | memset(&info->file_trans, 0x0, sizeof(mbtk_ftp_file_trans_info_s)); |
| 3055 | |
| 3056 | return FTP_ERR_SUCCESS; |
| 3057 | } |
| 3058 | |
| 3059 | |