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