blob: 87220ee175e5263ed27e99ae7bd916a38b70aec8 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#include "ftp/lynq_ftp.h"
2
3int lynq_ftp_login(lynq_ftp_socker_info *FTP)
4{
5 struct sockaddr_in serv_addr;
6 char recvdate;
7 char sendline[MAXSIZE] = "", recvline[MAXSIZE] = "";
8 struct hostent *host = NULL;
9
10 char name[MAXSIZE] = "", password[MAXSIZE] = "";
11 LYDBGLOG("[%s-%d] ftp,enter the hostname = %s\n", __FUNCTION__, __LINE__, FTP->sevname);
12
13 host = gethostbyname(FTP->sevname);
14
15 if(host == NULL)
16 {
17 error = 35;
18 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
19 login_yes = 0;
20 }
21 else
22 {
23 FTP->control_sockfd = socket(AF_INET, SOCK_STREAM, 0);
24 if(FTP->control_sockfd < 0)
25 {
26 error = 50;
27 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
28 LYDBGLOG("[%s-%d]\n", __FUNCTION__, __LINE__);
29 login_yes = 0;
30 }
31
32 bzero(&serv_addr, sizeof(serv_addr));
33
34 memset(&serv_addr, 0, sizeof(struct sockaddr_in));
35 memcpy(&serv_addr.sin_addr.s_addr, host->h_addr, host->h_length);
36 serv_addr.sin_family = AF_INET;
37 serv_addr.sin_port = htons(FTP->portnum);
38
39 if((connect(FTP->control_sockfd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr))) < 0)
40 {
41 error = 33;
42 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
43 login_yes = 0;
44 }
45
46
47 recvdate = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
48 if(recvdate == -1)
49 {
50 error = 33;
51 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
52 login_yes = 0;
53 }
54 else if(strncmp(recvline, "220", 3) == 0)
55 {
56 LYDBGLOG("ftp,connect success\n");
57 login_yes = 1;
58 }
59 else
60 {
61 error = 33;
62 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
63 login_yes = 0;
64 }
65 if(login_yes == 1)
66 {
67 int sendbytes, recvbytes;
68
69 zeromery(name, 1024);
70 zeromery(password, 1024);
71 zeromery(recvline, 1024);
72 zeromery(sendline, 1024);
73 strcat(sendline, "USER ");
74 strcat(sendline, FTP->username);
75 strcat(sendline, "\r\n");
76 //printf("[%s %d] --->%s\n",__FUNCTION__ , __LINE__,sendline);
77 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
78 if(sendbytes == -1)
79 {
80 error = 38;
81 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
82 login_yes = 0;
83 //return FTP_USER_ERROR;
84 }
85 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
86 if(strncmp(recvline, "331", 3) == 0)
87 {
88 LYDBGLOG("[%s %d] ftp,331 please specify the password.\n", __FUNCTION__ , __LINE__);
89 }
90 else
91 {
92 error = 38;
93 LYDBGLOG("[%s-%d] ftp,recv date is error.%d\n", __FUNCTION__ , __LINE__, error);
94 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
95 login_yes = 0;
96 }
97 zeromery(sendline, 1024);
98 zeromery(recvline, 1024);
99 //printf("ftp-> ");
100 strcat(sendline, "PASS ");
101 strcat(sendline, FTP->pw);
102 strcat(sendline, "\r\n");
103 //printf("[%s %d]--->%s\n",__FUNCTION__, __LINE__, sendline);
104 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
105 if(sendbytes == -1)
106 {
107 error = 39;
108 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
109 login_yes = 0;
110 }
111 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
112 if(strncmp(recvline, "230", 3) == 0)
113 {
114 LYVERBLOG("+[ftp][login][session%d]: ok!!\n", FTP->session);
115 login_yes = 1;
116 }
117 else
118 {
119 error = 39;
120 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
121 login_yes = 0;
122 }
123
124#if 1
125 //support rest
126 char str[255];
127 sprintf(str, "%d", FTP->rest);
128 zeromery(sendline, 1024);
129 zeromery(recvline, 1024);
130 strcat(sendline, "REST ");
131 strcat(sendline, str);
132 strcat(sendline, "\r\n");
133 //printf("[%s-%d]--->%s\n", __FUNCTION__, __LINE__, sendline);
134 sendbytes = send(FTP->control_sockfd,sendline,strlen(sendline),0);
135 if(sendbytes == -1)
136 {
137 error = 41;
138 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
139 login_yes = 0;
140 }
141 recvbytes = recv(FTP->control_sockfd, recvline,sizeof(recvline),0);
142 if(recvbytes == -1)
143 {
144 error = 41;
145 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
146 login_yes = 0;
147 }
148 if(strncmp(recvline, "350",3) == 0)
149 {
150 LYDBGLOG("[%s-%d] support rest\n", __FUNCTION__, __LINE__);
151 login_yes = 1;
152 }
153 else
154 {
155 error = 41;
156 LYDBGLOG("[%s-%d] ftp,not support rest%d\n", __FUNCTION__, __LINE__, error);
157 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
158 login_yes = 0;
159 }
160#endif
161 }
162
163 }
164 return login_yes;
165}
166
167void zeromery(char *a,int len)
168{
169 int i;
170 len = sizeof(a);
171 for(i = 0; i < len; i++)
172 {
173 a[i] = 0;
174 }
175}
176
177void lynq_ftp_pwd(int control_sockfd)
178{
179 int recvbytes,sendbytes;
180 char sendline[1024], recvline[1024];
181 zeromery(sendline, 1024);
182 zeromery(recvline, 1024);
183 strcat(sendline, "PWD");
184 strcat(sendline, "\r\n");
185 sendbytes = send(FTP->control_sockfd,sendline,strlen(sendline),0);
186 if(sendbytes < 0)
187 {
188 LYDBGLOG("[%s-%d] ftp,pwd,send is error\n", __FUNCTION__, __LINE__);
189 error = 52;
190 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
191 }
192 recvbytes = recv(FTP->control_sockfd, recvline,sizeof(recvline),0);
193 if(strncmp(recvline, "257",3) == 0)
194 {
195 LYDBGLOG("[%s-%d] ftp,current directory is:%s\n", __FUNCTION__, __LINE__, recvline);
196 }
197 else
198 {
199 error = 51;
200 LYDBGLOG("[%s-%d] ftp,pwd,recv is error!\n", __FUNCTION__, __LINE__);
201 LYVERBLOG("+[ftp][login][session%d]: error num = %d\n", FTP->session, error);
202 }
203}
204
205//rest
206void lynq_ftp_rest(int control_sockfd)
207{
208
209 int recvbytes,sendbytes;
210 char sendline[1024], recvline[1024];
211 zeromery(sendline, 1024);
212 zeromery(recvline, 1024);
213 strcat(sendline, "REST ");
214 strcat(sendline, "500");
215 strcat(sendline, "\r\n");
216 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
217 if(sendbytes < 0)
218 {
219 error = 52;
220 LYDBGLOG("[%s-%d] ftp,stru send is error!\n", __FUNCTION__, __LINE__);
221 LYVERBLOG("+[ftp][reset][session%d]: error num = %d\n", FTP->session, error);
222 }
223 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
224 if(recvbytes < 0)
225 {
226 error = 51;
227 LYVERBLOG("+[ftp][reset][session%d]: error num = %d\n", FTP->session, error);
228 }
229 if(strncmp(recvline, "350",3) == 0)
230 {
231 LYDBGLOG("[%s-%d] ftp,%s\n", __FUNCTION__, __LINE__, recvline);
232 }
233}
234
235int strtosrv(char *str)
236{
237
238 int addr[6];
239 //printf("%s\n",str);
240 sscanf(str, "%*[^(](%d,%d,%d,%d,%d,%d)", &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]);
241
242 LYDBGLOG("[%s-%d] ftp,%d,%d,%d,%d\n", __FUNCTION__, __LINE__, addr[0],addr[1],addr[2],addr[3]);
243 bzero(hoster, strlen(hoster));
244 sprintf(hoster, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
245
246 LYDBGLOG("[%s-%d] ftp,hoster = %s", __FUNCTION__, __LINE__, hoster);
247 int port = addr[4]*256 + addr[5];
248 LYDBGLOG("[%s-%d] ftp,port = %d\n", __FUNCTION__, __LINE__, port);
249 return port;
250}
251
252
253int lynq_ftp_download(lynq_ftp_socker_info* FTP)
254{
255 char catbuf[1024];
256 int data_sock;
257 int recvbytes, sendbytes;
258 char sendline[1024] = "", recvline[1024] = "";
259 char getfilepath[FTP_MAX_ASCII_LEN+5] = "/tmp/";
260
261#if 1
262 //type
263
264 zeromery(recvline, 1024);
265 zeromery(sendline, 1024);
266 strcat(sendline, "TYPE ");
267 strcat(sendline, FTP->file_type);
268 strcat(sendline, "\r\n");
269 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline),0);
270 if(sendbytes < 0)
271 {
272 error = 52;//FTP_SEND_ERROR
273 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
274 return FTP_SEND_ERROR;
275 }
276 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
277 LYDBGLOG("[%s-%d] ftp,recvline = %s\n",__FUNCTION__, __LINE__, recvline);
278 if(strncmp(recvline, "200", 3) == 0)
279 {
280 LYDBGLOG("[%s-%d] ftp,binary transmisson\n",__FUNCTION__, __LINE__);
281 }
282 else
283 {
284 error = 46; //FTP_DOWNLOAD_ERROR
285 LYDBGLOG("[%s-%d] ftp,download error\n",__FUNCTION__, __LINE__);
286 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
287 return FTP_DOWNLOAD_ERROR;
288 }
289#endif
290
291 zeromery(recvline, 1024);
292 zeromery(sendline, 1024);
293 strcat(sendline, FTP->is_pasv_mode);
294 strcat(sendline, " \r\n");
295
296 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
297 if(sendbytes < 0)
298 {
299 error = 52; //FTP_SEND_ERROR
300 LYDBGLOG("[%s-%d] type send is error!\n", __FUNCTION__, __LINE__);
301 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
302 return FTP_SEND_ERROR;
303 }
304
305 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
306 if(strncmp(recvline, "227", 3) == 0)
307 {
308 LYDBGLOG("[%s-%d] ftp,binary transmisson\n", __FUNCTION__, __LINE__);
309 }
310 else
311 {
312 error = 43;//FTP_ACTIVE_ERROR
313 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
314 LYDBGLOG("[%s-%d] type recv is error!\n", __FUNCTION__, __LINE__);
315 return FTP_ACTIVE_ERROR;
316 }
317
318 LYDBGLOG("-------- [%s-%d] recvline = %s\n", __FUNCTION__, __LINE__, recvline);
319 int port1 = strtosrv(recvline);
320 data_sock = cliopen(hoster, port1);
321
322 LYDBGLOG("[%s-%d] ftp,data_sock = %d\n", __FUNCTION__, __LINE__, data_sock);
323 LYDBGLOG("[%s-%d] FTP->getfilename = %s\n", __FUNCTION__, __LINE__, FTP->getfilename);
324 LYDBGLOG("[%s-%d] FTP->getfilename_path = %s\n", __FUNCTION__, __LINE__, FTP->getfilename_path);
325
326 //send the command retr;
327 zeromery(sendline, 1024);
328 zeromery(recvline, 1024);
329 strcat(sendline, "RETR ");
330 strcat(sendline, FTP->getfilename_path);
331 strcat(sendline, "/");
332 strcat(sendline, FTP->getfilename);
333 strcat(sendline, "\r\n");
334 //printf("%s\n", sendline);
335
336 LYDBGLOG("[%s-%d] ftp,%ld\n", __FUNCTION__, __LINE__, write(FTP->control_sockfd,sendline,strlen(sendline)));
337 LYDBGLOG("[%s-%d] getfilename= %s FTP->control_sockfd = %d\n", __FUNCTION__, __LINE__, FTP->getfilename, FTP->control_sockfd);
338#if 1
339 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
340
341 if(recvbytes < 0)
342 {
343 error = 52;//FTP_SEND_ERROR
344 LYDBGLOG("[%s-%d] retr recv is error!\n", __FUNCTION__, __LINE__);
345 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
346
347 return FTP_SEND_ERROR;
348 }
349 LYDBGLOG("[%s-%d] recvline = %s\n", __FUNCTION__, __LINE__, recvline);
350 if(strncmp(recvline, "400", 3) > 0)
351 {
352 error = 51;//FTP_RCV_ERROR
353
354 LYDBGLOG("[%s-%d] return is error!\n", __FUNCTION__, __LINE__);
355 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
356 return FTP_RCV_ERROR;
357 }
358
359 strcat(getfilepath, FTP->getfilename);
360 lynq_ftp_get(data_sock, getfilepath, FTP->session);
361
362 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
363
364 strcpy(FTP->respond, recvline);
365
366 LYDBGLOG("[%s-%d] FTP->respond = %s\n", __FUNCTION__, __LINE__, FTP->respond);
367
368 if(strncmp(FTP->respond, "550", 3) == 0)
369 {
370 error = 46; //FTP_DOWNLOAD_ERROR
371 LYDBGLOG("[%s-%d] download is error %d \n", __FUNCTION__, __LINE__, error);
372 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
373
374 return FTP_DOWNLOAD_ERROR;
375 }
376
377 if(strncmp(FTP->respond, "421",3) == 0)
378 {
379 error = 34;//FTP_TIMEROUT
380 LYDBGLOG("[%s-%d] download is error %d \n", __FUNCTION__, __LINE__, error);
381 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", FTP->session, error);
382
383 return FTP_TIMEROUT;
384 }
385
386 if((strncmp(FTP->respond, "226", 3) == 0) || (strncmp(FTP->respond, "125",3) == 0))
387 {
388 LYVERBLOG("+[ftp][get][session%d]: ok!!\n", FTP->session);
389 }
390#endif
391 return 0;
392
393}
394
395int lynq_ftp_put(int sck, char *pUploadFileName_s)
396{
397 int handle = open(pUploadFileName_s, O_RDWR);
398 int nread;
399 if(handle == -1)
400 return -1;
401
402 while(1)
403 {
404 if((nread = read(handle, rbuf1, 1024)) < 0)
405 {
406 error = 54;//FTP_READ_ERROR
407 LYDBGLOG("[%s-%d] read error!", __FUNCTION__, __LINE__);
408 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, error);
409 }
410 else if(nread == 0)
411 break;
412 LYDBGLOG("[%s-%d] rbuf1 = %s \n", __FUNCTION__, __LINE__, rbuf1);
413
414 if(write(sck, rbuf1, nread) != nread){
415 error = 54;
416 LYDBGLOG("[%s-%d] send error!", __FUNCTION__, __LINE__);
417 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, error);
418 }
419 memset(rbuf1,0,1024);
420 }
421 if(close(sck) < 0)
422 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, FTP_CLOSE_ERROR);
423
424 return 0;
425}
426
427
428int lynq_ftp_up(lynq_ftp_socker_info* FTP)
429{
430 char catbuf[1024];
431 int data_sock;
432 int recvbytes,sendbytes;
433 char sendline[1024], recvline[1024];
434 int put_ret = -1;
435
436 if((access(FTP->putfilename, F_OK)) == -1)
437 {
438 //FTP_SOCK_ERROR
439 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, FTP_SOCK_ERROR);
440 LYDBGLOG("[%s-%d] %s file does not exist\n", __FUNCTION__, __LINE__, FTP->putfilename);
441 return FTP_SOCK_ERROR;
442 }
443
444#if 1
445 zeromery(recvline, 1024);
446 zeromery(sendline, 1024);
447 strcat(sendline, "TYPE ");
448 strcat(sendline, FTP->file_type);
449 strcat(sendline, "\r\n");
450 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
451 if(sendbytes < 0)
452 {
453 LYDBGLOG("[%s-%d] ftp type send is error!\n", __FUNCTION__, __LINE__);
454 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, FTP_SEND_ERROR);
455 return FTP_SEND_ERROR;
456 }
457 recvbytes = recv(FTP->control_sockfd, recvline,sizeof(recvline),0);
458 if(strncmp(recvline, "200",3) == 0)
459 {
460 LYDBGLOG("[%s-%d] initalize\n", __FUNCTION__, __LINE__);
461 }
462 else
463 {
464 LYDBGLOG("[%s-%d] type recv is error!\n", __FUNCTION__, __LINE__);
465 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, FTP_RCV_ERROR);
466 return FTP_RCV_ERROR;
467 }
468#endif
469
470 //PASV
471 zeromery(recvline, 1024);
472 zeromery(sendline, 1024);
473 strcat(sendline, FTP->is_pasv_mode);
474 strcat(sendline, " \r\n");
475 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
476 if(sendbytes < 0)
477 {
478 LYDBGLOG("[%s-%d] type send is error!\n", __FUNCTION__, __LINE__);
479 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, FTP_SEND_ERROR);
480 }
481 recvbytes = recv(FTP->control_sockfd, recvline,sizeof(recvline), 0);
482 if(strncmp(recvline, "227", 3) == 0)
483 {
484 LYDBGLOG("[%s-%d] binary transmisson\n", __FUNCTION__, __LINE__);
485 }
486 else
487 {
488 error = 43;//FTP_ACTIVE_ERROR
489 LYDBGLOG("[%s-%d] type recv is error!\n", __FUNCTION__, __LINE__);
490 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, error);
491 return FTP_ACTIVE_ERROR;
492 }
493
494 //init datelink server port information
495 int port1 = strtosrv(recvline);
496 LYDBGLOG("[%s-%d] ftp,hoster =%s,port1 = %d\n", __FUNCTION__, __LINE__, hoster,port1);
497 data_sock = cliopen(hoster, port1);
498
499 LYDBGLOG("[%s-%d] ftp,data_sock = %d\n", __FUNCTION__, __LINE__, data_sock);
500 zeromery(sendline, 1024);
501 zeromery(recvline, 1024);
502 strcat(sendline, FTP->put_opt);
503 strcat(sendline, " ");
504 strcat(sendline, FTP->putfilename_path);
505 strcat(sendline, "/");
506 strcat(sendline, FTP->putfilename);
507 strcat(sendline, "\r\n");
508
509 LYDBGLOG("[%s-%d] ftp,%s\n", __FUNCTION__, __LINE__, sendline);
510 LYDBGLOG("%ld\n", write(FTP->control_sockfd, sendline, strlen(sendline)));
511 LYDBGLOG("[%s-%d] ftp,filename = %s\n", __FUNCTION__, __LINE__, FTP->putfilename);
512
513
514 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
515
516 LYDBGLOG("RETR RECVLINE = %s\n", recvline);
517 if(recvbytes < 0)
518 {
519 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, FTP_RCV_ERROR);
520 LYDBGLOG("[%s-%d] retr recv is error!\n", __FUNCTION__, __LINE__);
521 return FTP_RCV_ERROR;
522 }
523 if(strncmp(recvline, "400", 3) > 0)
524 {
525 error = 400;
526 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, error);
527 }
528
529 lynq_ftp_put(data_sock, FTP->putfilename);
530
531 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
532
533 strcpy(FTP->respond, recvline);
534
535 if(strncmp(FTP->respond, "226",3) == 0)
536 {
537 LYDBGLOG("[%s-%d] FTP->respond = %s\n", __FUNCTION__, __LINE__, FTP->respond);
538 LYVERBLOG("+[ftp][up][session%d]: ok!!\n", FTP->session);
539 }
540 else
541 {
542 error = 45;//FTP_UPLOAD_ERROR
543 LYVERBLOG("+[ftp][up][session%d]: error num = %d\n", FTP->session, error);
544 return FTP_UPLOAD_ERROR;
545 }
546 return 0;
547
548}
549
550
551int lynq_ftp_ls(lynq_ftp_socker_info* FTP)
552{
553 char catbuf[1024];
554 int data_sock;
555 int recvbytes, sendbytes;
556 char sendline[1024], recvline[1024];
557
558
559 //PASV
560 zeromery(recvline, 1024);
561 zeromery(sendline, 1024);
562 strcat(sendline, FTP->is_pasv_mode);
563 strcat(sendline, " \r\n");
564 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
565 if(sendbytes < 0)
566 {
567 LYDBGLOG("[%s-%d] type send is error!\n", __FUNCTION__, __LINE__);
568 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", FTP->session, FTP_SEND_ERROR);
569 return FTP_SEND_ERROR;
570 }
571 recvbytes = recv(FTP->control_sockfd, recvline,sizeof(recvline), 0);
572 if(strncmp(recvline, "227",3) == 0)
573 {
574 LYDBGLOG("[%s-%d] binary transmit\n", __FUNCTION__, __LINE__);
575 }
576 else
577 {
578 error = 43;
579 LYDBGLOG("[%s-%d] type recv is error!\n", __FUNCTION__, __LINE__);
580 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", FTP->session, error);
581 return error;
582 }
583
584 //init datelink server port information
585 int port1 = strtosrv(recvline);
586 LYDBGLOG("[%s-%d] ftp,hoster =%s,port1 = %d\n", __FUNCTION__, __LINE__, hoster,port1);
587 data_sock = cliopen(hoster, port1);
588
589 //printf("data_sock = %d\n",data_sock);
590
591 zeromery(sendline, 1024);
592 zeromery(recvline, 1024);
593 strcat(sendline, "LIST -al ");
594 strcat(sendline, FTP->dir);
595 strcat(sendline, "\r\n");
596
597 LYDBGLOG("%ld\n", write(FTP->control_sockfd, sendline, strlen(sendline)));
598
599 LYDBGLOG("[%s-%d] ftp,%s\n", __FUNCTION__, __LINE__,sendline);
600
601 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
602
603 //printf("RETR RECVLINE = %s\n",recvline);
604 LYDBGLOG("[%s-%d] recvline = %s\n", __FUNCTION__, __LINE__, recvline);
605
606 if(recvbytes < 0)
607 {
608 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", FTP->session, FTP_SEND_ERROR);
609 }
610 if(strncmp(recvline, "400", 3)>0)
611 {
612 error = 400;
613 LYDBGLOG("[%s-%d] type recv is error!\n", __FUNCTION__, __LINE__);
614 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", FTP->session, error);
615 return error;
616 }
617
618 ftp_list(data_sock, FTP->session);
619
620 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
621
622 LYDBGLOG("[%s-%d] recvline = %s\n", __FUNCTION__, __LINE__, recvline);
623
624 strcpy(FTP->respond, recvline);
625
626 if((strncmp(FTP->respond, "226", 3) == 0) || (strncmp(FTP->respond, "150",3) == 0) || strncmp(FTP->respond, "125", 3) == 0)
627 {
628 if(strstr(FTP->respond, ". 0 bytes")){//rita add @2021.07.19 for ls
629 LYDBGLOG("[%s-%d] FTP->respond = %s\n", __FUNCTION__, __LINE__, FTP->respond);
630 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", FTP->session, FTP_LS_ERROR);
631 return FTP_LS_ERROR;
632 }
633 else
634 LYVERBLOG("+[ftp][ls][session%d]: ok!!\n", FTP->session);
635 }
636 else{
637 error = 44;
638 LYDBGLOG("[%s-%d] ls error!\n", __FUNCTION__, __LINE__);
639 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", FTP->session, error);
640 return error;
641 }
642
643 return 0;
644}
645
646
647void lynq_ftp_get(int sck, char *pDownloadFileName, int session)
648{
649 int handle = open(pDownloadFileName, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
650 int nread;
651
652 while(1)
653 {
654 //printf("%d\n",sck);
655 //printf("%d\n",handle);
656 if((nread = recv(sck, rbuf1, 1024, 0)) < 0)
657 {
658 error = 51;
659 //LYDBGLOG("[%s-%d] ftp,rbuf1 = %s\n", __FUNCTION__, __LINE__, rbuf1);
660 LYDBGLOG("[%s-%d] receive error\n", __FUNCTION__, __LINE__);
661 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", session, error);
662 return;
663 }
664 else if(nread == 0)
665 {
666 LYDBGLOG("[%s-%d] over\n", __FUNCTION__, __LINE__);
667 break;
668 }
669 if(write(handle, rbuf1, nread) != nread)
670 {
671 LYDBGLOG("[%s-%d] receive error from server!\n", __FUNCTION__, __LINE__);
672 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", session, FTP_WRITE_ERROR);
673 }
674
675 //LYDBGLOG("[%s-%d] rbuf1 = %s \n", __FUNCTION__, __LINE__, rbuf1);
676
677 memset(rbuf1,0,1024);
678 }
679 if(close(sck) < 0){
680 LYDBGLOG("[%s-%d] close error\n", __FUNCTION__, __LINE__);
681 LYVERBLOG("+[ftp][get][session%d]: error num = %d\n", session, FTP_CLOSE_ERROR);
682 }
683}
684
685
686
687int cliopen(char *hoster, int port)
688{
689
690 int control_sockfd;
691
692 struct sockaddr_in serv_addr;
693 struct hostent *host = NULL;
694
695 //get hostent argument
696 char name[MAXSIZE];
697 //printf("please enter the hostname\n");
698 //printf("ftp-> ");
699
700 strcpy(name, hoster);
701 host = gethostbyname(name);
702 if(host == NULL)
703 {
704 LYDBGLOG("[%s-%d] ftp,get host by name is error!\n", __FUNCTION__, __LINE__);
705 login_yes = 0;
706 }
707 else
708 {
709 //creact socket
710 control_sockfd = socket(AF_INET, SOCK_STREAM, 0);
711 if(control_sockfd < 0)
712 {
713 //printf("socket is error\n");
714 LYDBGLOG("[%s-%d] ftp,socket is error\n", __FUNCTION__, __LINE__);
715 login_yes = 0;
716 }
717
718 //set sockaddr_in
719 bzero(&serv_addr, sizeof(serv_addr));
720
721 memset(&serv_addr, 0, sizeof(struct sockaddr_in));
722 memcpy(&serv_addr.sin_addr.s_addr, host->h_addr, host->h_length);
723 serv_addr.sin_family = AF_INET;
724 serv_addr.sin_port = htons(port);
725
726 //printf("line port = %d\n",port);
727 if((connect(control_sockfd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr))) < 0)
728 {
729 //printf("connect is error\n");
730 LYDBGLOG("[%s-%d] ftp,connect is error\n", __FUNCTION__, __LINE__);
731 login_yes = 0;
732 }
733 }
734 return control_sockfd;
735}
736
737void ftp_list(int sockfd, int session)
738{
739 int nread;
740
741 for(;;)
742 {
743 if((nread = recv(sockfd, rbuf1, 1024, 0)) < 0)
744 {
745 LYDBGLOG("[%s-%d] recv error\n", __FUNCTION__, __LINE__);
746 }
747 else if(nread == 0)
748 {
749 LYDBGLOG("[%s-%d] disconnect \n", __FUNCTION__, __LINE__);
750 break;
751 }
752 LYVERBLOG("+[ftp][ls][session%d]: data = %s\n", session, rbuf1);
753 LYVERBLOG("+[ftp][ls][session%d]: ok!! \n", session);
754 memset(rbuf1, 0, 1024);
755 }
756 if(close(sockfd) < 0){
757 LYDBGLOG("[%s-%d] close error\n", __FUNCTION__, __LINE__);
758 LYVERBLOG("+[ftp][ls][session%d]: error num = %d\n", session, FTP_CLOSE_ERROR);
759 }
760}
761
762void lynq_ftp_cd(lynq_ftp_socker_info* FTP)
763{
764 char catbuf[1024];
765 char sendline[1024];
766 char recvline[1024];
767
768 zeromery(sendline, 1024);
769 zeromery(recvline, 1024);
770 int recvbytes, sendbytes;
771 int issuccess;
772
773 if(strcmp(FTP->del_mkr_filename, "..") == 0)
774 {
775 sprintf(sendline, "CDUP %s\n", FTP->del_mkr_filename);
776 }
777 else
778 {
779 sprintf(sendline, "CWD %s\n", FTP->del_mkr_filename);
780 }
781 strcat(sendline, "\r\n");
782
783 LYDBGLOG("[%s-%d] %s\n", __FUNCTION__, __LINE__, sendline);
784 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
785 if(sendbytes < 0)
786 {
787 LYDBGLOG("cd send is error!");
788 exit(1);
789 }
790
791
792 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
793 if(strncmp(recvline, "257", 3) == 0)
794 {
795 issuccess = 1;
796 }
797 else
798 {
799 error = 44;
800 issuccess = 0;
801 }
802}
803
804void lynq_ftp_creat_mkd(lynq_ftp_socker_info* FTP)
805{
806 char catbuf[1024];
807 char sendline[1024];
808 char recvline[1024];
809
810 zeromery(sendline, 1024);
811 zeromery(recvline, 1024);
812 int recvbytes, sendbytes;
813 int issuccess;
814 strcat(sendline, "MKD ");
815 strcat(sendline, FTP->del_mkr_filename);
816 strcat(sendline, "\r\n");
817 LYDBGLOG("[%s-%d] %s\n", __FUNCTION__, __LINE__, sendline);
818 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
819 if(sendbytes < 0)
820 {
821 LYDBGLOG("[%s-%d] mkd send is error!", __FUNCTION__, __LINE__);
822 LYVERBLOG("+[ftp][mkdir][session%d]: error num = %s\n", FTP->session, FTP_SEND_ERROR);
823 exit(1);
824 }
825 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
826 if(strncmp(recvline, "257", 3) == 0)
827 {
828 issuccess = 1;
829 LYVERBLOG("+[ftp][mkdir][session%d]: ok!!\n", FTP->session);
830 }
831 else
832 {
833 error = 44;
834 LYVERBLOG("+[ftp][mkdir][session%d]: error num = %d\n", FTP->session, error);
835 issuccess = 0;
836 }
837}
838
839
840void lynq_ftp_delete_mkd(lynq_ftp_socker_info* FTP)
841{
842 char sendline[1024];
843 char recvline[1024];
844 zeromery(sendline, 1024);
845 zeromery(recvline, 1024);
846 int recvbytes, sendbytes;
847 int issuccess;
848 char catbuf[1024];
849
850 strcat(sendline, "RMD ");
851 strcat(sendline, FTP->del_mkr_filename);
852 strcat(sendline, "\r\n");
853 LYDBGLOG("[%s-%d] %s\n", __FUNCTION__, __LINE__,sendline);
854 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
855 if(sendbytes < 0)
856 {
857 LYDBGLOG("[%s-%d] rmd send is error!", __FUNCTION__, __LINE__);
858 LYVERBLOG("+[ftp][rmd][session%d]: error num = %s\n", FTP->session, FTP_SEND_ERROR);
859 exit(1);
860 }
861 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
862 if(strncmp(recvline, "250", 3) == 0)
863 {
864 issuccess = 1;
865 LYVERBLOG("+[ftp][rmd][session%d]: ok!!\n", FTP->session);
866 }
867 else
868 {
869 error = 44;
870 issuccess = 0;
871 LYVERBLOG("+[ftp][rmd][session%d]: error num = %s\n", FTP->session, error);
872 }
873}
874
875void lynq_ftp_quit(lynq_ftp_socker_info* FTP)
876{
877
878 int recvbytes, sendbytes;
879 char sendline[1024], recvline[1024];
880 zeromery(sendline, 1024);
881 zeromery(recvline, 1024);
882
883
884 strcat(sendline, "bye ");
885 strcat(sendline, "\n");
886 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
887 if(sendbytes < 0)
888 {
889 LYDBGLOG("[%s-%d] stru send is error!\n", __FUNCTION__, __LINE__);
890 LYVERBLOG("+[ftp][quit][session%d]: error num = %s\n", FTP->session, FTP_SEND_ERROR);
891 }
892 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
893
894 LYDBGLOG("[%s-%d] quit = %s\n", __FUNCTION__, __LINE__, recvline);
895 if(recvbytes < 0)
896 {
897 LYDBGLOG("[%s-%d] stru recv is error!\n", __FUNCTION__, __LINE__);
898 LYVERBLOG("+[ftp][quit][session%d]: error num = %s\n", FTP->session, FTP_RCV_ERROR);
899 }
900 if(strncmp(recvline, "221", 3) == 0)
901 {
902 LYVERBLOG("+[ftp][quit][session%d]: ok!!\n", FTP->session);
903 }
904}
905
906void lynq_ftp_deletefile_mkd(lynq_ftp_socker_info* FTP)
907{
908 char sendline[1024];
909 char recvline[1024];
910
911 zeromery(sendline, 1024);
912 zeromery(recvline, 1024);
913 int recvbytes, sendbytes;
914 int issuccess;
915 char catbuf[1024];
916 strcat(sendline, "DELE ");
917 strcat(sendline, FTP->del_mkr_filename);
918 strcat(sendline, "\r\n");
919 LYDBGLOG("[%s-%d] %s\n", __FUNCTION__, __LINE__,sendline);
920 sendbytes = send(FTP->control_sockfd, sendline, strlen(sendline), 0);
921 if(sendbytes < 0)
922 {
923 LYVERBLOG("+[ftp][delfile][session%d]: error num = %d\n", FTP->session, FTP_SEND_ERROR);
924 exit(1);
925 }
926 recvbytes = recv(FTP->control_sockfd, recvline, sizeof(recvline), 0);
927 if(strncmp(recvline, "250", 3) == 0)
928 {
929 issuccess = 1;
930 LYVERBLOG("+[ftp][delfile][session%d]: ok!!\n", FTP->session);
931 }
932 else
933 {
934 error = 44;
935 issuccess = 0;
936 LYVERBLOG("+[ftp][delfile][session%d]: error num = %d\n", FTP->session, error);
937 }
938}
939