1 /***********************************************************************
2  *    ftp.c: for downloading via ftp (File Transfer Protocol)
3  ***********************************************************************
4  * Copyright (C) 2007 metro <me_t_ro@yahoo.com>
5  *
6  * This file is part of msdl, media stream downloader
7  *
8  * This is just an very simple ftp implementation.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *
25  ***********************************************************************/
26 
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <time.h>
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <arpa/inet.h>
40 
41 #include "msdl.h"
42 #include "msdllib.h"
43 #include "display.h"
44 #include "network.h"
45 #include "ftp.h"
46 
47 
48 
49 static int is_lf_terminated_line(char *buf);
50 static inline int is_ftp_OK(int code);
51 static int ftp_recv_response(struct stream_t *stream,struct ftp_response_t *fres);
52 static int ftp_recv_response_ignore_message(struct stream_t  *stream);
53 
54 
55 static int ftp_send_command(struct stream_t *stream,char *msg);
56 static int ftp_read_welcome(struct stream_t *stream);
57 static int ftp_user(struct stream_t *stream,const char *username);
58 static int ftp_pass(struct stream_t *stream,const char *password);
59 static int ftp_epsv(struct stream_t *stream,const char *connect_host,int *newsock);
60 static int ftp_pasv(struct stream_t *stream,int *newsock);
61 static int ftp_passive_mode(struct stream_t *stream,
62 			    const char *current_host,int *newsock);
63 static int ftp_tell_port(struct stream_t *stream,int *newsock);
64 static int ftp_bin_mode(struct stream_t *stream);
65 static int ftp_ask_size(struct stream_t *stream,const char *filepath,uint64_t *size);
66 static int ftp_retr(struct stream_t *stream,const char *filepath);
67 
68 static int ftp_interpret_byterange(const char *str,uint64_t *begin,uint64_t *end,
69 				   char **reason_ret);
70 
71 
72 static const char ftp_default_passwd[] = "hugahuga@huhun.com";
73 
74 
new_ftp_ctrl_t(void)75 struct ftp_ctrl_t *new_ftp_ctrl_t(void)
76 {
77     struct ftp_ctrl_t *fctrl = xmalloc(sizeof(struct ftp_ctrl_t));
78     memset(fctrl,0,sizeof(struct ftp_ctrl_t));
79     fctrl->mode = PASSIVE_FTP; /* defualt passive mode (this is easyer)*/
80     return fctrl;
81 }
82 
83 
84 
free_ftp_ctrl_t(struct ftp_ctrl_t * fctrl)85 void free_ftp_ctrl_t(struct ftp_ctrl_t *fctrl)
86 {
87     if(!fctrl) return;
88 
89     if(fctrl->command_sock > 0)   close(fctrl->command_sock);
90     if(fctrl->data_wait_sock > 0) close(fctrl->data_wait_sock);
91     if(fctrl->data_sock > 0)      close(fctrl->data_sock);
92 
93     free(fctrl);
94 }
95 
96 
97 
new_ftp_response_t(void)98 struct ftp_response_t *new_ftp_response_t(void)
99 {
100     struct ftp_response_t *fres = xmalloc(sizeof(struct ftp_response_t));
101     fres->num_lines = 0;
102     fres->lines = NULL;
103 
104     return fres;
105 }
106 
107 
108 
free_ftp_response_t(struct ftp_response_t * fres)109 void free_ftp_response_t(struct ftp_response_t *fres)
110 {
111     if(fres->lines) free_list_h(fres->lines,*free);
112     free(fres);
113 }
114 
115 
116 
is_lf_terminated_line(char * buf)117 static int is_lf_terminated_line(char *buf)
118 {
119     if(buf == NULL) return 0;
120 
121     if(strchr(buf,'\n')) {
122 	return 1;
123     }
124     return 0;
125 }
126 
127 
128 
ftp_recv_response(struct stream_t * stream,struct ftp_response_t * fres)129 static int ftp_recv_response(struct stream_t *stream,struct ftp_response_t *fres)
130 {
131 
132     size_t linebuflen = 0;
133     char *linebuffer = NULL;
134     int i;
135     int total = 0;
136     int status_code;
137 
138     do {
139 	linebuflen = 0;
140 	linebuffer = NULL;
141 	total = 0;
142 	char *lineend;
143 
144 
145 	do {
146 	    linebuflen += BUFSIZE_1K;
147 	    linebuffer = (char *)xrealloc(linebuffer,
148 					  linebuflen + 1);
149 
150 	    /*
151 	      set zero to realloc()ed region
152 	      +1 for NULL char at the end of buffer.
153 	    */
154 	    memset(linebuffer + total,0,linebuflen + 1 - total);
155 
156 
157 	    i = recv_data(stream,linebuffer + total,linebuflen - total);
158 	    if(i <= 0) {
159 		display(MSDL_ERR,"xrecv error: xrecv() returned %d\n",i);
160 		goto failed;
161 	    }
162 
163 	    total += i;
164 	} while(!is_lf_terminated_line(linebuffer));
165 
166 	lineend = strchr(linebuffer,'\n');
167 	lineend++;
168 
169 	stream_data_push_back(stream,lineend,total - (lineend - linebuffer));
170 
171 	memset(lineend,0,total - (lineend - linebuffer));
172 	list_h_append(&fres->lines,linebuffer);
173 
174 	fres->num_lines++;
175 
176 	display(MSDL_DBG,"FTP RESPONSE line ==================\n"
177 		"%s"
178 		"==(%d bytes)\n",linebuffer,lineend - linebuffer);
179 
180 	status_code = (linebuffer[0] - '0') * 100 +
181 	    (linebuffer[1] - '0') * 10 + (linebuffer[2] - '0');
182 
183     } while(isdigit(linebuffer[0]) && isdigit(linebuffer[1]) &&
184 	    isdigit(linebuffer[2]) && (linebuffer[3] == '-') );
185 
186     return status_code;
187 
188   failed:
189     if(linebuffer) free(linebuffer);
190     return 0;
191 }
192 
193 
194 
195 /*
196  * return 1 if ftp message is positive
197  *        0 if negative (not OK message)
198  */
is_ftp_OK(int code)199 static inline int is_ftp_OK(int code)
200 {
201     return ((100 <= code) && (code < 400)) ? 1 : 0;
202 }
203 
204 
205 
206 /*
207  *  get rtsp response but ignore its message
208  *              return value:     status code
209  */
ftp_recv_response_ignore_message(struct stream_t * stream)210 static int ftp_recv_response_ignore_message(struct stream_t  *stream)
211 {
212     struct ftp_response_t *ftp_response;
213     int status_code;
214 
215     ftp_response = new_ftp_response_t();
216     status_code = ftp_recv_response(stream,ftp_response);
217     if(!is_ftp_OK(status_code) && ftp_response->lines) {
218 	display(MSDL_ERR,"%s",ftp_response->lines->p);
219     }
220     free_ftp_response_t(ftp_response);
221 
222     return status_code;
223 }
224 
225 
226 
227 /*
228  * send ftp command string.
229  *              return value:     what xsend returned
230  */
ftp_send_command(struct stream_t * stream,char * msg)231 static int ftp_send_command(struct stream_t *stream,char *msg)
232 {
233     int ret;
234 
235     display(MSDL_DBG,"--------------------------\nsent: %s\n",msg);
236     ret = xsend(stream->stream_ctrl->ftp_ctrl->command_sock,msg,strlen(msg));
237 
238     return ret;
239 }
240 
241 
242 
243 /*
244  * read first messgae from ftp server
245  *              return value:     status code: normal case
246  */
ftp_read_welcome(struct stream_t * stream)247 static int ftp_read_welcome(struct stream_t *stream)
248 {
249     struct ftp_response_t *ftp_response;
250     int status_code;
251 
252     ftp_response = new_ftp_response_t();
253     status_code = ftp_recv_response(stream,ftp_response);
254     if(ftp_response->lines) {
255 	display(MSDL_VER,"%s\n",ftp_response->lines->p);
256     }
257     free_ftp_response_t(ftp_response);
258 
259     return status_code;
260 }
261 
262 
263 
264 /*
265  * user name
266  *              return value:     status code: normal case
267  */
ftp_user(struct stream_t * stream,const char * username)268 static int ftp_user(struct stream_t *stream,const char *username)
269 {
270     char *sendbuffer = xmalloc(strlen(username) + 16);
271     int status_code;
272 
273     snprintf(sendbuffer,BUFSIZE_1K,"USER %s\n",username);
274     ftp_send_command(stream,sendbuffer);
275 
276     status_code = ftp_recv_response_ignore_message(stream);
277 
278     free(sendbuffer);
279     return status_code;
280 }
281 
282 
283 
284 /*
285  * password verification
286  *              return value:     status code: normal case
287  */
ftp_pass(struct stream_t * stream,const char * password)288 static int ftp_pass(struct stream_t *stream,const char *password)
289 {
290     struct ftp_response_t *ftp_response;
291     char *sendbuffer = xmalloc(strlen(password) + 16);
292     int status_code;
293 
294     snprintf(sendbuffer,BUFSIZE_1K,"PASS %s\n",password);
295     ftp_send_command(stream,sendbuffer);
296 
297     ftp_response = new_ftp_response_t();
298     status_code = ftp_recv_response(stream,ftp_response);
299     if(!is_ftp_OK(status_code) && ftp_response->lines) {
300 	display(MSDL_ERR,"FTP login failed: %s\n",ftp_response->lines->p);
301     }
302     free_ftp_response_t(ftp_response);
303 
304     free(sendbuffer);
305     return status_code;
306 }
307 
308 
309 
310 /*
311  * switch to extended passive mode. 'newsock' is new data_socket opened by
312  * entering passive mode. needs current conneting host as argument
313  *              return value:   status code
314  */
ftp_epsv(struct stream_t * stream,const char * connect_host,int * newsock)315 static int ftp_epsv(struct stream_t *stream,const char *connect_host,int *newsock)
316 {
317     struct stream_ctrl_t *stream_ctrl = stream->stream_ctrl;
318     struct ftp_response_t *ftp_response;
319     int status_code;
320     int data_sock = 0;
321 
322     ftp_send_command(stream,"EPSV\n");
323 
324     ftp_response = new_ftp_response_t();
325     status_code = ftp_recv_response(stream,ftp_response);
326     if(!is_ftp_OK(status_code)) {
327 	/*ACTIVE mode*/
328 	stream_ctrl->ftp_ctrl->mode = ACTIVE_FTP;
329 	free_ftp_response_t(ftp_response);
330 	*newsock = 0;
331 	return status_code;
332     }
333     else {
334 	/*
335 	  rfc 2428
336 	  <text indicating server is entering extended passive mode>	\
337 	  (<d><d><d><tcp-port><d>)
338 	*/
339 	char *line = ftp_response->lines->p;
340 	char *p;
341 
342 	p = strchr(line,')');
343 	if(p) {
344 	    int epsvport = 0;
345 	    char delimiter;
346 	    p--; /* backword skip ')' */
347 	    while(*p == ' ') p--;
348 	    delimiter = *p;
349 	    p--; /* backword skip '|' */
350 	    for(; (p >= line) && (*p != delimiter) ; p--);
351 	    p++;
352 	    for(; isdigit(*p) ; p++) {
353 		epsvport *= 10;
354 		epsvport += *p - '0';
355 	    }
356 
357 	    data_sock = server_connect(connect_host,epsvport);
358 	    free_ftp_response_t(ftp_response);
359 	    *newsock = data_sock;
360 	    return status_code;
361 	}
362     }
363 
364     free_ftp_response_t(ftp_response);
365     *newsock = data_sock;
366     return status_code;
367 }
368 
369 
370 
371 /*
372  * switch to passive mode. 'newsock' is new data_socket opened by
373  * entering passive mode.
374  *              return value:   status code
375  */
ftp_pasv(struct stream_t * stream,int * newsock)376 static int ftp_pasv(struct stream_t *stream,int *newsock)
377 {
378     struct stream_ctrl_t *stream_ctrl = stream->stream_ctrl;
379     struct ftp_response_t *ftp_response;
380     int data_sock = 0;
381     int status_code;
382 
383     ftp_send_command(stream,"PASV\n");
384 
385     ftp_response = new_ftp_response_t();
386     status_code = ftp_recv_response(stream,ftp_response);
387     if(!is_ftp_OK(status_code)) {
388 	/*ACTIVE mode*/
389 	stream_ctrl->ftp_ctrl->mode = ACTIVE_FTP;
390 	free_ftp_response_t(ftp_response);
391 	*newsock = 0;
392 	return status_code;
393     }
394     else {
395 	char connect_host[INET6_ADDRSTRLEN + 4];
396 	/* PASSIVE mode */
397 	int pasvport;
398 	int i;
399 	int value[6] = {0,0,0,0,0,0};
400 	char *line = ftp_response->lines->p;
401 	char *p;
402 
403 	p = strrchr(line,',');
404 	if(p) {
405 	    for( ; (p >= line) && (isdigit(*p) || *p == ',') ; p--);
406 	    if(p <= line) {
407 		free_ftp_response_t(ftp_response);
408 		return 0;
409 	    }
410 	    p++;
411 
412 	    for(i = 0; i < 6 ; i++) {
413 		value[i] = 0;
414 		for(; p && *p && isdigit(*p) ; p++) {
415 		    value[i] *= 10;
416 		    value[i] += *p - '0';
417 		}
418 		value[i] &= 0xff;
419 		if(!p || !(*p)) {
420 		    break;
421 		}
422 		else {
423 		    p++;
424 		}
425 	    }
426 
427 	    if(p && *p) {
428 		snprintf(connect_host,sizeof(connect_host) - 1,
429 		         "%d.%d.%d.%d",value[0],value[1],value[2],value[3]);
430 		pasvport = (value[4] << 8) + value[5];
431 		data_sock = server_connect(connect_host,pasvport);
432 		free_ftp_response_t(ftp_response);
433 		*newsock = data_sock;
434 		return status_code;
435 	    }
436 	}
437     }
438 
439     /*cannot get port number from recv reply*/
440     display(MSDL_ERR,"cannot get port number from recv reply\n");
441     stream_ctrl->ftp_ctrl->mode = ACTIVE_FTP;
442     free_ftp_response_t(ftp_response);
443     *newsock = 0;
444     return status_code;
445 }
446 
447 
448 
449 /*
450  * switch to passive mode. 'newsock' is new data_socket opened by
451  * current_host is needed by ftp_epsv.
452  *              return value:   status code
453  */
ftp_passive_mode(struct stream_t * stream,const char * current_host,int * newsock)454 static int ftp_passive_mode(struct stream_t *stream,const char *current_host,int *newsock)
455 {
456     struct sockaddr_storage ss;
457     socklen_t sslen;
458     int command_sock = stream->stream_ctrl->ftp_ctrl->command_sock;
459 
460     /* choose same protocol family as command_sock */
461     memset(&ss,0,sizeof(ss));
462     sslen = sizeof(ss);
463     if(getsockname(command_sock,(struct sockaddr *)&ss,&sslen) < 0) {
464 	perror("getsockname() failed");
465 	*newsock = 0;
466 	return -1;
467     }
468 
469     if(ss.ss_family == AF_INET) {       /* IPv4 */
470 	/*return ftp_epsv(stream,current_host,newsock);*/
471 	return ftp_pasv(stream,newsock);
472     }
473     else if(ss.ss_family == AF_INET6) { /* IPv6 */
474 	if(!current_host) {
475 	    return -1;
476 	}
477 	return ftp_epsv(stream,current_host,newsock);
478     }
479     else {
480 	display(MSDL_ERR,"unknown protocol family %d\n",ss.ss_family);
481     }
482 
483     return -1;
484 }
485 
486 
487 
488 /*
489  * tell waiting port to ftp server, only used in active mode.
490  *             return value:     status code: normal case
491  *                                        -1: internal error
492  */
ftp_tell_port(struct stream_t * stream,int * newsock)493 static int ftp_tell_port(struct stream_t *stream,int *newsock)
494 {
495     struct stream_ctrl_t *stream_ctrl = stream->stream_ctrl;
496     int command_sock = stream_ctrl->ftp_ctrl->command_sock;
497     int data_wait_sock;
498 
499     char sendbuffer[INET6_ADDRSTRLEN + 32];
500     struct sockaddr_storage ss;
501     socklen_t sslen;
502     int local_port;
503 
504     /* get ip address of myself */
505     memset(&ss,0,sizeof(ss));
506     sslen = sizeof(ss);
507     if(getsockname(command_sock,(struct sockaddr *)&ss,&sslen) < 0) {
508 	perror("getsockname() failed");
509 	*newsock = 0;
510 	return -1;
511     }
512 
513 
514     /* for random port number */
515     srand(time(NULL));
516     local_port = 49152 + (rand() % (65535 - 49152));
517 
518     /* use same protocol family as command_socket */
519     data_wait_sock = waiting_socket(ss.ss_family,local_port);
520     if(data_wait_sock < 0) {
521 	*newsock = 0;
522 	return -1;
523     }
524 
525     if(ss.ss_family == AF_INET) {
526 	struct sockaddr_in *sinp;
527 	uint32_t local_ipv4addr;
528 
529 	sinp = (struct sockaddr_in *)&ss;
530 
531 	local_ipv4addr = ntohl(sinp->sin_addr.s_addr);
532 	snprintf(sendbuffer,sizeof(sendbuffer) - 1,"PORT %d,%d,%d,%d,%d,%d\n",
533 		 (local_ipv4addr >> 24) & 0xff,
534 		 (local_ipv4addr >> 16) & 0xff,
535 		 (local_ipv4addr >>  8) & 0xff,
536 		 (local_ipv4addr      ) & 0xff,
537 		 (local_port >> 8) & 0xff,
538 		 (local_port     ) & 0xff);
539 
540 	/*
541 	  {
542 	  char local_ipv4addrstr[INET6_ADDRSTRLEN + 5];
543 	  inet_ntop(sinp->sin_family,&(sinp->sin_addr.s_addr),
544 	  local_ipv4addrstr,INET6_ADDRSTRLEN + 4);
545 	  snprintf(sendbuffer,sizeof(sendbuffer) - 1,
546 	  "EPRT  |1|%s|%d|\r\n",local_ipv4addrstr,local_port);
547 	  }
548 	*/
549 
550     }
551     else if(ss.ss_family == AF_INET6) {
552 	struct sockaddr_in6 *sin6p;
553 	char local_ipv6addr[INET6_ADDRSTRLEN + 4];
554 
555 	sin6p = (struct sockaddr_in6 *)&ss;
556 	inet_ntop(sin6p->sin6_family,&(sin6p->sin6_addr.s6_addr),
557 		  local_ipv6addr,INET6_ADDRSTRLEN + 4);
558 
559 	snprintf(sendbuffer,sizeof(sendbuffer) - 1,
560 		 "EPRT |2|%s|%d|\n",local_ipv6addr,local_port);
561 
562     }
563     else {
564 	display(MSDL_ERR,"unknown protocol family: %d\n",
565 		ss.ss_family);
566 	*newsock = 0;
567 	return -1;
568     }
569 
570     ftp_send_command(stream,sendbuffer);
571 
572     *newsock = data_wait_sock;
573 
574     return ftp_recv_response_ignore_message(stream);
575 }
576 
577 
578 
579 /*
580  * switch to ftp binary mode
581  *             return value:     status code
582  */
ftp_bin_mode(struct stream_t * stream)583 static int ftp_bin_mode(struct stream_t *stream)
584 {
585     ftp_send_command(stream,"TYPE I\n");
586     return ftp_recv_response_ignore_message(stream);
587 }
588 
589 
590 
591 /*
592  * seek to requested position using REST request
593  * set dlopts->byterange
594  *             return value:     1 : success
595  *                              -1 : no file name specified, or no local file
596  */
ftp_prepare_resuming(struct stream_t * stream)597 static int ftp_prepare_resuming(struct stream_t *stream)
598 {
599     uint64_t filesize = 0;
600     int ret = 0;
601 
602     /*
603      * find same file name
604      */
605 
606     ret = get_filesize(stream->localfile,&filesize);
607     if(ret < 0) {
608 	display(MSDL_ERR,
609 		"ftp resume: no such file \"%s\", not resuming\n",stream->localfile);
610 	goto failed;
611     }
612 
613     if(stream->dlopts->byterange) { /* free old byterange */
614 	free(stream->dlopts->byterange);
615     }
616 
617     stream->dlopts->byterange = make_byterange_from_filesize(filesize);
618     stream->resumeinfo->resume_start_offset = filesize;
619 
620     display(MSDL_DBG,
621 	    "ftp resume: start pos: %lld [%llx]\n",
622 	    filesize,filesize);
623 
624     return 1;
625 
626   failed:
627     stream->resumeinfo->resume_start_offset = 0;
628     stream->resumeinfo->resume_req_success = 0;
629     return -1;
630 }
631 
632 
633 
634 /*
635  * ftp seek by REST req, from stream->dlopts->byterange
636  * return value:   status code ... network transfer OK (does not mean success)
637  *                          -1 ... error, or did nothing
638  */
ftp_seek_pos(struct stream_t * stream)639 static int ftp_seek_pos(struct stream_t *stream)
640 {
641     if(stream->dlopts->byterange) {
642 	struct ftp_response_t *ftp_response = NULL;
643 	char sendbuffer[128]; /* this is enough */
644 	int status_code = 0;
645 
646 	uint64_t begin = 0,end = 0;
647 	char *reason = NULL;
648 	int byterange_valid = 0;
649 
650 	byterange_valid = ftp_interpret_byterange(stream->dlopts->byterange,&begin,&end,&reason);
651 	if(!byterange_valid) {
652 	    display(MSDL_ERR,
653 		    "ftp_seek_pos() error: range string \"%s\" not valid\n"
654 		    "%s\n",stream->dlopts->byterange,reason);
655 	    return 0;
656 	}
657 
658 	snprintf(sendbuffer,127,"REST %llu\n",(unsigned long long)begin);
659 	ftp_send_command(stream,sendbuffer);
660 
661 	ftp_response = new_ftp_response_t();
662 	status_code = ftp_recv_response(stream,ftp_response);
663 	if(is_ftp_OK(status_code)) {
664 	    display(MSDL_DBG,"seek OK\n");
665 	    if(end && (end > begin)) { /* finish offset sepcified and length > 0 */
666 		stream->stream_ctrl->ftp_ctrl->transfer_force_end_size = end - begin;
667 	    }
668 	}
669 	else {
670 	    display(MSDL_ERR,"ftp REST failed:\n");
671 	    if(ftp_response->lines) {
672 		display(MSDL_ERR,
673 			"%s\n",(char *)ftp_response->lines->p);
674 	    }
675 	}
676 	free_ftp_response_t(ftp_response);
677 	return status_code;
678     }
679 
680     return -1;
681 }
682 
683 
684 
685 /*
686  * ask file size of 'filepath' and it goes to 'size'
687  *             return value:     status code
688  */
ftp_ask_size(struct stream_t * stream,const char * filepath,uint64_t * size)689 static int ftp_ask_size(struct stream_t *stream,const char *filepath,uint64_t *size)
690 {
691     struct ftp_response_t *ftp_response;
692     char *sendbuffer = xmalloc(strlen(filepath) + 16);
693     int status_code;
694 
695     snprintf(sendbuffer,BUFSIZE_1K,"SIZE %s\n",filepath);
696     ftp_send_command(stream,sendbuffer);
697 
698     ftp_response = new_ftp_response_t();
699     status_code = ftp_recv_response(stream,ftp_response);
700     if(is_ftp_OK(status_code)) {
701 	char *p = (char *)ftp_response->lines->p;
702 	uint64_t filesize = 0;
703 
704 	while(isdigit(*p)) p++; /* skip status code */
705 	while((*p) == ' ') p++; /* skip white space */
706 	while(isdigit(*p)) {
707 	    filesize *= 10;
708 	    filesize += *p - '0';
709 	    p++;
710 	}
711 	display(MSDL_DBG,"file size %d bytes\n",filesize);
712 
713 	*size = filesize;
714     }
715     else {
716 	if(ftp_response->lines) {
717 	    display(MSDL_ERR,"%s",(char *)ftp_response->lines->p);
718 	}
719 	*size = 0;
720     }
721 
722     free_ftp_response_t(ftp_response);
723     free(sendbuffer);
724     return status_code;
725 }
726 
727 
728 
729 /*
730  * read ftp byterange [0-9]+
731  * return value: 1 ... valid  0 ... invalid
732  */
ftp_byterange_read(const char * str,uint64_t * value,char ** reason_ret)733 static int ftp_byterange_read(const char *str,uint64_t *value,char **reason_ret)
734 {
735     const char *p = str;
736     uint64_t val = 0;
737     for(; *p != '\0' ;p++) {
738 	if(!isdigit(*p)) {
739 	    if(reason_ret) {
740 		*reason_ret = "invalid character in range string";
741 	    }
742 	    *value = 0;
743 	    return 0;
744 	}
745 	val *= 10;
746 	val += *p - '0';
747     }
748 
749     *reason_ret = NULL;
750     *value = val;
751     return 1;
752 }
753 
754 
755 /*
756  * intepret range xxx-xxx
757  *
758  * return value:    1 ... interpreted successfully
759  *                  0 ... string error (not valid str)
760  */
ftp_interpret_byterange(const char * str,uint64_t * begin,uint64_t * end,char ** reason_ret)761 static int ftp_interpret_byterange(const char *str,uint64_t *begin,uint64_t *end,
762 				   char **reason_ret)
763 {
764     char *p = NULL;
765 
766     int valid = 0;
767     char *reason = NULL;
768 
769     p = strchr(str,'-');
770     if(p == NULL) { /* have to have exactly one '-' */
771 	reason = "must use \'-\' to tell range";
772 	if(reason_ret) {
773 	    *reason_ret = reason;
774 	}
775 	goto failed;
776     }
777     else if(p == str) { /* ( "-" npt-time ) */
778 	valid = ftp_byterange_read(p + 1,end,reason_ret);
779 	if(!valid) {
780 	    goto failed;
781 	}
782 	return valid; /* 1 */
783     }
784     else { /* 00.23- */
785 	int start_str_len = p - str;
786 	char *range_str = NULL;
787 
788 	if(*(p+1) != '\0') { /* not 00.23- */
789 	    valid = ftp_byterange_read(p + 1,end,reason_ret);
790 	    if(!valid) {
791 		goto failed;
792 	    }
793 	}
794 	/* end string OK */
795 
796 	/* check start string */
797 	range_str = xmalloc(start_str_len + 1);
798 	strncpy(range_str,str,start_str_len);
799 	range_str[start_str_len] = '\0';
800 	valid = ftp_byterange_read(range_str,begin,reason_ret);
801 	free(range_str);
802 	if(!valid) {
803 	    goto failed;
804 	}
805 
806 	return valid;
807     }
808 
809   failed:
810     *begin = 0;
811     *end = 0;
812     return 0;
813 }
814 
815 
816 
817 
818 /*
819  * send RETR request for downloading file.
820  */
ftp_retr(struct stream_t * stream,const char * filepath)821 static int ftp_retr(struct stream_t *stream,const char *filepath)
822 {
823     char *sendbuffer = xmalloc(strlen(filepath) + 16);
824     struct ftp_response_t *ftp_response;
825     int status_code;
826 
827     snprintf(sendbuffer,BUFSIZE_1K,"RETR %s\n",filepath);
828     ftp_send_command(stream,sendbuffer);
829 
830     ftp_response = new_ftp_response_t();
831     status_code = ftp_recv_response(stream,ftp_response);
832     if(!is_ftp_OK(status_code)) {
833 	display(MSDL_ERR,"%s",(char *)ftp_response->lines->p);
834     }
835 
836     free_ftp_response_t(ftp_response);
837     free(sendbuffer);
838     return status_code;
839 }
840 
841 
842 
843 /*
844  * starts ftp streaming(actually this is downlaoding).
845  *
846  *    return value :   negative or 0  ... error
847  *                                 1  ... success
848  */
ftp_streaming_start(struct stream_t * stream)849 int ftp_streaming_start(struct stream_t *stream)
850 {
851     struct stream_ctrl_t *stream_ctrl = stream->stream_ctrl;
852     struct url_t *url = stream->url;
853     struct download_opts_t *dlopts = stream->dlopts;
854 
855     int command_sock = 0;
856     int data_wait_sock = 0;
857     int data_sock = 0;
858 
859     int status_code = 0;
860     uint64_t file_size = 0;
861 
862 
863     stream_ctrl->status = STREAMING_HANDSHAKING;
864 
865     if(stream->dlopts->no_passive_ftp) {
866 	stream->stream_ctrl->ftp_ctrl->mode = ACTIVE_FTP;
867     }
868 
869     set_serverinfo(stream->serverinfo,url->hostname,url->port,NULL,0,FTP_PORT);
870 
871     command_sock = server_connect(stream->serverinfo->connect_host,
872 				  stream->serverinfo->connect_port);
873     if(command_sock < 0) {
874 	goto failed;
875     }
876 
877     /*
878       stream_sock is CURRENT using socket, so now it's command_sock
879       ( later changed to data_sock, as it will be active )
880     */
881     stream->netsock->sock = command_sock;
882     stream_ctrl->ftp_ctrl->command_sock = command_sock;
883 
884 
885     status_code = ftp_read_welcome(stream);
886     if(!is_ftp_OK(status_code)) {
887 	goto failed;
888     }
889 
890 
891     /*
892       USER, default is anonymous login.
893       priority:  url->username --> dlopts->username --> "anonymous"
894     */
895     status_code = ftp_user(stream,
896 			   (url->username) ? url->username :
897 			   ((dlopts->username) ? dlopts->username :
898 			    "anonymous"));
899 
900     if(status_code != 230) {
901 	/*
902 	  PASS (don't need to send this if user was already logged in with USER)
903 	*/
904 	status_code =ftp_pass(stream,
905 			      (url->password) ? url->password :
906 			      ((dlopts->password) ? dlopts->password :
907 			       ftp_default_passwd));
908 	if(!is_ftp_OK(status_code)) {
909 	    goto failed;
910 	}
911     }
912 
913 
914     /*
915       PASV (if not ACTIVE mode specified)
916 
917       this may fail, and in that case fall back to ACTIVE.
918     */
919     if(stream_ctrl->ftp_ctrl->mode == PASSIVE_FTP) {
920 	status_code = ftp_passive_mode(stream,stream->serverinfo->host,&data_sock);
921 	if((!is_ftp_OK(status_code)) || (data_sock <= 0)) {
922 	    stream_ctrl->ftp_ctrl->mode = ACTIVE_FTP;
923 	}
924     }
925 
926 
927     /*
928       PORT
929     */
930     if(stream_ctrl->ftp_ctrl->mode == ACTIVE_FTP) {
931 	status_code = ftp_tell_port(stream,&data_wait_sock);
932 	if((!is_ftp_OK(status_code)) || (data_wait_sock <= 0)) {
933 	    goto failed;
934 	}
935     }
936 
937 
938     if(stream_ctrl->ftp_ctrl->mode == ACTIVE_FTP) {
939 	data_sock = accept_connection(data_wait_sock);
940 	if(data_sock < 0) {
941 	    display(MSDL_ERR,"could not establish data connection\n");
942 	    goto failed;
943 	}
944     }
945 
946 
947     /*
948       TYPE
949     */
950     status_code = ftp_bin_mode(stream);
951 
952 
953     /*
954       REST
955      */
956     if(stream->dlopts->resume_download) { /* try resume, by setting byterange */
957 	ftp_prepare_resuming(stream);
958     }
959 
960     if(stream->dlopts->byterange) {
961 	status_code = ftp_seek_pos(stream);
962 
963 	if(is_ftp_OK(status_code)) {
964 	    if(stream->dlopts->resume_download) {
965 		stream->resumeinfo->resume_req_success = 1;
966 		display(MSDL_VER,"ftp resume OK\n");
967 	    }
968 	    else {
969 		display(MSDL_VER,"ftp seek OK\n");
970 	    }
971 	}
972 	else {
973 	    display(MSDL_ERR,"ftp resume failed\n");
974 	    if(stream->dlopts->resume_download) {
975 		stream->resumeinfo->resume_start_offset = 0; /* starting from beginning */
976 		stream->resumeinfo->resume_req_success = 0;
977 		display(MSDL_ERR,"ftp: download file from beginning\n");
978 	    }
979 	}
980     }
981 
982 
983     /*
984       SIZE
985     */
986     status_code = ftp_ask_size(stream,url->filepath,&file_size);
987     if(file_size) {
988 	stream_ctrl->file_size = stream_ctrl->ftp_ctrl->file_size = file_size;
989     }
990 
991 
992 
993     /*
994       RETR
995     */
996     status_code = ftp_retr(stream,url->filepath);
997     if(!is_ftp_OK(status_code)) {
998 	goto failed;
999     }
1000 
1001 
1002 
1003     stream->stream_ctrl->protocol = FTP;
1004 
1005     stream_ctrl->ftp_ctrl->command_sock = command_sock; /* just to make sure */
1006     stream_ctrl->ftp_ctrl->data_wait_sock = data_wait_sock;
1007     stream_ctrl->ftp_ctrl->data_sock = data_sock;
1008 
1009     stream_ctrl->ftp_ctrl->down_size = 0;
1010     stream_ctrl->status = STREAMING_DOWNLOADING;
1011 
1012     /* stream_sock is CURRENT using socket, so now it's changed to data_sock */
1013     stream->netsock->sock = data_sock;
1014 
1015     return 1;
1016 
1017   failed:
1018     return 0;
1019 }
1020 
1021 
1022 
1023 /*
1024  * ftp stream. filles buffer, and buffer size is 'size'
1025  * ftp is special. it read from socket directory, as we don't need
1026  * complicated buffering mechanism, such as in RTSP or so.
1027  *
1028  *  return value: bytes written to buffer.
1029  */
ftp_streaming_read(struct stream_t * stream,uint8_t * buffer,size_t buffer_size)1030 int ftp_streaming_read(struct stream_t *stream,
1031 		       uint8_t *buffer, size_t buffer_size)
1032 {
1033     struct ftp_ctrl_t *ftp_ctrl = stream->stream_ctrl->ftp_ctrl;
1034     size_t read_len = 0;
1035     size_t ask_size = 0;
1036 
1037 
1038     /*transfer_force_end_size*/
1039     if(ftp_ctrl->transfer_force_end_size) {
1040 	if(ftp_ctrl->down_size >= ftp_ctrl->transfer_force_end_size) { /* already finished */
1041 	    stream->stream_ctrl->status = STREAMING_FINISHED;
1042 	    return 0;
1043 	}
1044 	else {
1045 	    size_t size_diff = (size_t)(ftp_ctrl->transfer_force_end_size - ftp_ctrl->down_size);
1046 	    ask_size = (size_diff > buffer_size) ? buffer_size : size_diff; /* min */
1047 	}
1048     }
1049     else {
1050 	ask_size = buffer_size;
1051     }
1052 
1053 
1054     read_len = xrecv(ftp_ctrl->data_sock,buffer,ask_size);
1055     if(read_len < 0) {
1056 	perror("recv() error");
1057 	return -1;
1058     }
1059     if(read_len == 0) {
1060 	stream->stream_ctrl->status = STREAMING_FINISHED;
1061     }
1062 
1063     ftp_ctrl->down_size += read_len;
1064 
1065     return read_len;
1066 }
1067 
1068 
1069 
ftp_streaming_init()1070 struct stream_t *ftp_streaming_init()
1071 {
1072     struct stream_t *stream = streaming_init_common();
1073     stream->stream_ctrl->ftp_ctrl = new_ftp_ctrl_t();
1074 
1075     stream->start = ftp_streaming_start;
1076     stream->read  = ftp_streaming_read;
1077     stream->close = ftp_streaming_close;
1078 
1079     return stream;
1080 }
1081 
1082 
1083 
ftp_streaming_close(struct stream_t * stream)1084 void ftp_streaming_close(struct stream_t *stream)
1085 {
1086     /*
1087       FTP uses 2(or 3 in active) sockets, and they are closed in free_ftp_ctrl_t
1088     */
1089     ftp_send_command(stream,"QUIT\n"); /* to be nice to server. */
1090 
1091     free_ftp_ctrl_t(stream->stream_ctrl->ftp_ctrl);
1092     streaming_close_common(stream);
1093 }
1094