1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
/*
mpg321 - a fully free clone of mpg123.
Copyright (C) 2001 Joe Drew
Network code based heavily upon:
plaympeg - Sample MPEG player using the SMPEG library
Copyright (C) 1999 Loki Entertainment Software
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <limits.h>
#include "mpg321.h"
#define MAX_HOSTNAME 256
#define IFVERB if(options.opt & MPG321_VERBOSE_PLAY)
int proxy_enable = 0;
char *proxy_server;
int auth_enable = 0;
int auth_enable_var = 0;
extern char authstring[80];
int http_file_length = 0;
int shoutcast(int);
int is_address_multicast(unsigned long address)
{
if ((address & 255) >= 224 && (address & 255) <= 239)
return (1);
return (0);
}
#ifndef USE_IPV6
/* =================== IPV4 ================== */
int tcp_open(char *address, int port)
{
struct sockaddr_in stAddr;
struct hostent *host;
int sock;
struct linger l;
memset(&stAddr, 0, sizeof(stAddr));
stAddr.sin_family = AF_INET;
stAddr.sin_port = htons(port);
if ((host = gethostbyname(address)) == NULL)
return (0);
stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
return (0);
l.l_onoff = 1;
l.l_linger = 5;
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)) < 0)
return (0);
if (connect(sock, (struct sockaddr *)&stAddr, sizeof(stAddr)) < 0)
return (0);
return (sock);
}
int udp_open(char *address, int port)
{
int enable = 1L;
struct sockaddr_in stAddr;
struct sockaddr_in stLclAddr;
struct ip_mreq stMreq;
struct hostent *host;
int sock;
stAddr.sin_family = AF_INET;
stAddr.sin_port = htons(port);
if ((host = gethostbyname(address)) == NULL)
return (0);
stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
/* Create a UDP socket */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return (0);
/* Allow multiple instance of the client to share the same address and port */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&enable, sizeof(unsigned long int)) < 0)
return (0);
/* If the address is multicast, register to the multicast group */
if (is_address_multicast(stAddr.sin_addr.s_addr))
{
/* Bind the socket to port */
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = stAddr.sin_port;
if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0)
return (0);
/* Register to a multicast address */
stMreq.imr_multiaddr.s_addr = stAddr.sin_addr.s_addr;
stMreq.imr_interface.s_addr = INADDR_ANY;
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)) < 0)
return (0);
}
else
{
/* Bind the socket to port */
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = htons(0);
if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0)
return (0);
}
return (sock);
}
#else
/* =================== IPV6 ================== */
int tcp_open(char *address, int port)
{
struct addrinfo hints, *res, *res_tmp;
int sock;
struct linger l;
char ipstring[MAX_HOSTNAME];
char portstring[MAX_HOSTNAME];
snprintf(portstring, sizeof(portstring), "%d", port);
memset(&hints, 0, sizeof(hints));
/*
* hints.ai_protocol = 0;
* hints.ai_addrlen = 0;
* hints.ai_canonname = NULL;
* hints.ai_addr = NULL;
* hints.ai_next = NULL;
*/
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ( getaddrinfo(address, portstring, &hints, &res) != 0)
return(0);
for (res_tmp = res; res_tmp != NULL; res_tmp = res_tmp->ai_next) {
if ( (res_tmp->ai_family != AF_INET) &&
(res_tmp->ai_family != AF_INET6) )
continue;
if ( (sock = socket(res_tmp->ai_family, res_tmp->ai_socktype,
res_tmp->ai_protocol)) == -1) {
sock = 0;
continue;
}
l.l_onoff = 1;
l.l_linger = 5;
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)) < 0) {
/* return (0); */
sock = 0;
continue;
}
if ( connect(sock, res_tmp->ai_addr, res_tmp->ai_addrlen)
== -1 )
{
close(sock);
sock = 0;
continue;
}
ipstring[0] = 0;
getnameinfo(res_tmp->ai_addr, res_tmp->ai_addrlen, ipstring,
sizeof(ipstring), NULL, 0, NI_NUMERICHOST);
if (ipstring == NULL)
sock = 0;
break;
}
freeaddrinfo(res);
return sock;
}
int udp_open(char *address, int port)
{
int enable = 1L;
struct addrinfo hints, *res, *res_tmp;
int sock;
struct linger l;
char ipstring[MAX_HOSTNAME];
char portstring[MAX_HOSTNAME];
struct ipv6_mreq imr6;
struct ip_mreq imr;
snprintf(portstring, sizeof(portstring), "%d", port);
memset(&hints, 0, sizeof(hints));
/*
* hints.ai_protocol = 0;
* hints.ai_addrlen = 0;
* hints.ai_canonname = NULL;
* hints.ai_addr = NULL;
* hints.ai_next = NULL;
*/
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ( getaddrinfo(address, portstring, &hints, &res) != 0)
return(0);
for (res_tmp = res; res_tmp != NULL; res_tmp = res_tmp->ai_next) {
if ( (res_tmp->ai_family != AF_INET) &&
(res_tmp->ai_family != AF_INET6) )
continue;
if ( (sock = socket(res_tmp->ai_family, res_tmp->ai_socktype,
res_tmp->ai_protocol)) < 0) {
sock = 0;
continue;
}
/* Allow multiple instance of the client to share the same address and port */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&enable, sizeof(unsigned long int)) < 0) {
sock = 0;
continue;
}
/* If the address is multicast, register to the multicast group */
if ( (res_tmp->ai_family == AF_INET6) &&
IN6_IS_ADDR_MULTICAST(&(((struct sockaddr_in6 *)res_tmp->ai_addr)->
sin6_addr) ) ) {
/* Bind the socket to port */
if ( bind(sock, res_tmp->ai_addr, res_tmp->ai_addrlen) < 0) {
close(sock);
sock = 0;
continue;
}
imr6.ipv6mr_multiaddr =
((struct sockaddr_in6 *)res_tmp->ai_addr)->
sin6_addr;
imr6.ipv6mr_interface = INADDR_ANY;
if ( setsockopt(
sock,
IPPROTO_IPV6,
IPV6_ADD_MEMBERSHIP,
(char *) &imr6,
sizeof(struct ipv6_mreq))
< 0)
return(0);
}
else if ( (res_tmp->ai_family == AF_INET) &&
IN_MULTICAST(ntohl((((struct sockaddr_in *)res_tmp->
ai_addr)->sin_addr.s_addr)) ) ){
/* Bind the socket to port */
if ( bind(sock, res_tmp->ai_addr, res_tmp->ai_addrlen) < 0) {
close(sock);
sock = 0;
continue;
}
imr.imr_multiaddr =
((struct sockaddr_in *)res_tmp->ai_addr)->
sin_addr;
imr.imr_interface.s_addr = INADDR_ANY;
if ( setsockopt(sock,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
(char *) &imr,
sizeof(struct ip_mreq))
< 0)
return(0);
}
else {
/* Bind the socket to port */
if ( bind(sock, res_tmp->ai_addr, res_tmp->ai_addrlen) < 0) {
close(sock);
sock = 0;
continue;
}
}
}
freeaddrinfo(res);
return (sock);
}
#endif
int raw_open(char *arg)
{
char *host;
int port;
int sock;
/* Check for URL syntax */
if (strncmp(arg, "raw://", strlen("raw://")))
return (0);
/* Parse URL */
port = 0;
host = arg + strlen("raw://");
if (strchr(host, ':') != NULL) /* port is specified */
{
port = atoi(strchr(host, ':') + 1);
*strchr(host, ':') = 0;
}
/* Open a UDP socket */
if (!(sock = udp_open(host, port)))
perror("raw_open");
return (sock);
}
/**
* Read a http line header up to and excluding '\r\n'.
* This function read character by character.
* @param tcp_sock the socket use to read the stream
* @param buf a buffer to receive the data
* @param size size of the buffer
* @return the size of the stream read or -1 if an error occured
*/
static int http_read_header(int tcp_sock, char *buf, int size)
{
int offset = -1;
do
{
offset++;
if (read(tcp_sock, buf + offset, 1) < 0)
return -1;
}
while (offset<size-1 && !(offset>0 && buf[offset-1]=='\r' && buf[offset]=='\n'));
buf[offset-1] = 0;
buf[offset] = 0;
return offset-1;
}
/**
* Read a line from the tcp receive buffer up to and excluding '\n'
* This function read character by character.
* @param tcp_sock the socket use to read the stream
* @param buf a buffer to receive the data
* @param size size of the buffer
* @return the size of the stream read or -1 if an error occured
*/
static int http_read_content_line(int tcp_sock, char *buf, int size)
{
int offset = -1;
do
{
offset++;
if (read(tcp_sock, buf + offset, 1) < 0)
return -1;
}
while (offset<size-1 && !(offset>=0 && buf[offset]=='\n'));
buf[offset] = 0;
return offset;
}
int http_open(char *arg)
{
char *host;
int port;
char *request;
int tcp_sock;
char http_request[PATH_MAX];
char http_response[PATH_MAX];
char path[PATH_MAX];
int len;
int ofs;
char phost[16];
int pport;
/* Check for URL syntax */
if (strncmp(arg, "http://", 7))
return (0);
/* Parse URL */
port = 80;
host = arg + 7;
path[0] = 0;
if ((request = strchr(host, '/')))
{
*request++ = 0;
snprintf(path, sizeof(path) - strlen(host) - 75, "%s", request);
}
if (strchr(host, ':') != NULL) /* port is specified */
{
port = atoi(strchr(host, ':') + 1);
*strchr(host, ':') = 0;
}
/* Open a TCP socket */
if(proxy_enable)
{
char *t = strchr(proxy_server,':');
pport = atoi(strchr(proxy_server,':')+1);
snprintf(phost,(int)(t-proxy_server)+1,"%s",proxy_server);
if (!(tcp_sock = tcp_open(phost,pport)))
{
perror("http_open");
return (0);
}
}else{
if (!(tcp_sock = tcp_open(host, port)))
{
perror("http_open");
return (0);
}
}
/* Send HTTP GET request */
/* Please don't use a Agent know by shoutcast (Lynx, Mozilla) seems to be reconized and print
* a html page and not the stream */
/* ml> that's right, but it's just a playlist, so you've got to extract the URLs! */
if(!proxy_enable)
{
snprintf(http_request, sizeof(http_request),
"GET /%s HTTP/1.0\r\n"
"Pragma: no-cache\r\n"
"Host: %s\r\n"
"User-Agent: xmms/1.2.7\r\n" /* to make ShoutCast happy */
"Accept: * / *\r\n"
"\r\n", path, host);
}else{
if(auth_enable || auth_enable_var)
{
do_basicauth();
snprintf(http_request, sizeof(http_request), "GET http://%s/%s HTTP/1.0\r\nProxy-authorization: Basic %s \r\n"
"Pragma: no-cache\r\nUser-Agent: xmms/1.2.7\r\n\r\n",host,path,authstring);
}else{
snprintf(http_request, sizeof(http_request), "GET http://%s/%s HTTP/1.0\r\nnPragma: no-cache\r\nUser-Agent: xmms/1.2.7\r\n\r\n",host,path);
}
}
send(tcp_sock, http_request, strlen(http_request), 0);
/* Parse server reply */
len = http_read_header(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "HTTP header: >%s<\n", http_response );
if(!strncmp(http_response, "HTTP/1.0 ", 9) || !strncmp(http_response, "HTTP/1.1 ", 9))
{
IFVERB printf( "this is an HTTP/1.x header\n" );
ofs = 9;
if(!strncmp(http_response+ofs, "200 ", 4))
{
do /* check Content-Type header */
{
len = http_read_header(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "HTTP header: >%s<\n", http_response );
if(!strncmp(http_response, "Content-Length: ", 16))
{
char http_length[20];
sprintf(http_length,"%s",http_response+16);
http_file_length = atoi(http_length);
}
if(!strncmp(http_response, "Content-Type: ", 14))
{
IFVERB printf( "Content-Type: >%s<\n", http_response+14 );
if(!strncmp(http_response+14, "audio/x-scpls", 13))
{
return(shoutcast(tcp_sock));
}
else /* any other content types */
{
return(tcp_sock);
}
}
}
while(len>0);
fprintf(stderr, "http_open: 200 contains no Content-Type header.\n");
return 0;
}
else if (!strncmp(http_response+ofs, "302 ", 4)) /* redirect */
{
do
{
len = http_read_header(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "HTTP header: >%s<\n", http_response );
if(!strncmp(http_response, "Location: ", 10))
{
IFVERB printf( "Redirecting to:>%s<\n", http_response+10 );
close(tcp_sock);
return http_open(http_response+10);
}
}
while(len>0);
fprintf(stderr, "http_open: 302 contains no Location header.\n");
return 0;
}
else /* it's neither 302 nor 200 */
{
fprintf(stderr, "http_open: %s\n", http_response);
return 0;
}
}
else if(!strncmp(http_response, "ICY ", 4))
{
/* This is icecast streaming */
IFVERB printf( "this is an ICY header\n" );
do
{
len = http_read_header(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "ICY header: >%s<\n", http_response );
}
while(len>0);
return(tcp_sock);
}
else /* neither HTTP nor ICY */
{
fprintf(stderr, "http_open: %s\n", http_response);
return 0;
}
}
int shoutcast(int tcp_sock)
{
int len;
int noe, en;
int j;
char ** pl;
char http_response[PATH_MAX];
do
{
len = http_read_header(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "Shoutcast: >%s<\n", http_response );
}
while(len>0);
len = http_read_content_line(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "Shoutcast: >%s<\n", http_response );
if (strncmp(http_response, "[playlist]", 10) != 0)
{
fprintf(stderr, "Server response does not conform to audio/x-scpls MIME type format\n" );
return 0;
}
len = http_read_content_line(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
IFVERB printf( "Shoutcast: >%s<\n", http_response );
if (strncmp(http_response, "numberofentries=", 16) != 0)
{
fprintf(stderr, "Server response does not conform to audio/x-scpls MIME type format\n" );
return 0;
}
sscanf( http_response+16, "%d", &noe );
if(noe<1)
{
printf( "The ShoutCast playlist contains no entries.\n" );
return(0);
}
printf( "The ShoutCast playlist contains %d entries:\n", noe );
pl = (char**)calloc(noe,sizeof(char*));
j = 0;
while(j<noe)
{
len = http_read_content_line(tcp_sock, http_response, PATH_MAX);
if (len == -1)
{
fprintf(stderr, "http_open: %s\n", strerror(errno));
return 0;
}
if (strncmp(http_response, "File", 4) == 0)
{
char * equ;
equ = strchr(http_response, '=')+1;
pl[j] = (char*)calloc(strlen(equ),sizeof(char));
strcpy(pl[j],equ);
printf( "[%2d] %s\n", ++j, equ );
}
}
en = 1;
if(noe>1)
{
do
{
printf( "Please choose from the list: " );
scanf( "%d", &en );
}
while(en<1 || en>noe);
}
close(tcp_sock);
return http_open(pl[en-1]);
}
int ftp_get_reply(int tcp_sock)
{
int i;
char c;
char answer[1024];
do
{
/* Read a line */
for (i = 0, c = 0; i < 1023 && c != '\n'; i++)
{
read(tcp_sock, &c, sizeof(char));
answer[i] = c;
}
answer[i] = 0;
fprintf(stderr, "%s", answer + 4);
}
while (answer[3] == '-');
answer[3] = 0;
return (atoi(answer));
}
int ftp_open(char *arg)
{
char *host;
int port;
char *dir;
char *file;
int tcp_sock;
int data_sock;
char ftp_request[PATH_MAX];
struct sockaddr_in stLclAddr;
socklen_t namelen;
int i;
/* Check for URL syntax */
if (strncmp(arg, "ftp://", strlen("ftp://")))
return (0);
/* Parse URL */
port = 21;
host = arg + strlen("ftp://");
if ((dir = strchr(host, '/')) == NULL)
return (0);
*dir++ = 0;
if ((file = strrchr(dir, '/')) == NULL)
{
file = dir;
dir = NULL;
}
else
*file++ = 0;
if (strchr(host, ':') != NULL) /* port is specified */
{
port = atoi(strchr(host, ':') + 1);
*strchr(host, ':') = 0;
}
/* Open a TCP socket */
if (!(tcp_sock = tcp_open(host, port)))
{
perror("ftp_open");
return (0);
}
/* Send FTP USER and PASS request */
ftp_get_reply(tcp_sock);
snprintf(ftp_request, sizeof(ftp_request), "USER anonymous\r\n");
send(tcp_sock, ftp_request, strlen(ftp_request), 0);
if (ftp_get_reply(tcp_sock) != 331)
return (0);
snprintf(ftp_request, sizeof(ftp_request), "PASS smpeguser@\r\n");
send(tcp_sock, ftp_request, strlen(ftp_request), 0);
if (ftp_get_reply(tcp_sock) != 230)
return (0);
snprintf(ftp_request, sizeof(ftp_request), "TYPE I\r\n");
send(tcp_sock, ftp_request, strlen(ftp_request), 0);
if (ftp_get_reply(tcp_sock) != 200)
return (0);
if (dir != NULL)
{
snprintf(ftp_request, sizeof(ftp_request), "CWD %s\r\n", dir);
send(tcp_sock, ftp_request, strlen(ftp_request), 0);
if (ftp_get_reply(tcp_sock) != 250)
return (0);
}
/* Get interface address */
namelen = sizeof(stLclAddr);
if (getsockname(tcp_sock, (struct sockaddr *)&stLclAddr, &namelen) < 0)
return (0);
/* Open data socket */
if ((data_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
return (0);
stLclAddr.sin_family = AF_INET;
/* Get the first free port */
for (i = 0; i < 0xC000; i++)
{
stLclAddr.sin_port = htons(0x4000 + i);
if (bind(data_sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) >= 0)
break;
}
port = 0x4000 + i;
if (listen(data_sock, 1) < 0)
return (0);
i = ntohl(stLclAddr.sin_addr.s_addr);
snprintf(ftp_request, sizeof(ftp_request), "PORT %d,%d,%d,%d,%d,%d\r\n",
(i >> 24) & 0xFF, (i >> 16) & 0xFF,
(i >> 8) & 0xFF, i & 0xFF, (port >> 8) & 0xFF, port & 0xFF);
send(tcp_sock, ftp_request, strlen(ftp_request), 0);
if (ftp_get_reply(tcp_sock) != 200)
return (0);
snprintf(ftp_request, sizeof(ftp_request), "RETR %s\r\n", file);
send(tcp_sock, ftp_request, strlen(ftp_request), 0);
if (ftp_get_reply(tcp_sock) != 150)
return (0);
return (accept(data_sock, NULL, NULL));
}
|