ftp data connection problem!!
hello punters,
I am trying to code an FTP indexer in C. here is the code i have written, it works fine till the point of the connection for the data socket. Could some one tell me what the problem is here.
Thanx for your help, very much.
I have traced it and the point where it gets stuck has been highlighted in color
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netdb.h>
#include<ctype.h>
#define DEST_PORT 21
#define MAX 1000
void send_recv(int,char *command);
void cleanup();
void parse();
int getport();
int cmdftp_connect(int);
char recv_buf[1000];
char hostname[1000];
int sockfd,dtsockfd;//socket file descriptor
char ip_addr[20];
int port;
struct hostent *server;
int main()
{
// struct sockaddr_in dest_addr;//destination soket addr
strcpy(hostname,"ftp.microsoft.com");
sockfd=cmdftp_connect(DEST_PORT);
char send_buf[MAX];
char temp_buf[3];
if(recv(sockfd,recv_buf,MAX-1,0)==-1)
{
perror("recieve error\n");
exit(1);
}
printf(recv_buf);
cleanup();
printf("\n");
int i;
send_recv(sockfd,"user anonymous\012");
cleanup();
send_recv(sockfd,"pass [email protected]\012");
cleanup();
send_recv(sockfd,"type a\012");
cleanup();
// send_recv(sockfd,"pasv \012");
port=getport();
cleanup();
------------>dtsockfd=cmdftp_connect(port);//when it enters here
if(recv(dtsockfd,recv_buf,MAX-1,0)==-1)
{
perror("recieve error\n");
exit(1);
}
printf(recv_buf);
cleanup();
printf("\n");
send_recv(dtsockfd,"list\012");
cleanup();
//parse and retrive port address to connect to(ip is same!!)
send_recv(sockfd,"quit\012");
}
void cleanup()
{
int i=0;
int len=strlen(recv_buf);
while(i!=len)
recv_buf[i++]='\0';
}
void send_recv(int sockfd,char *command)
{
if(send(sockfd,command,strlen(command),0)==-1)
{
perror("send error\n");
exit(1);
}
if(recv(sockfd,recv_buf,MAX-1,0)==-1)
{
perror("recieve error\n");
exit(1);
}
printf(recv_buf);
printf("\n");
}
int cmdftp_connect(int port)
{
struct sockaddr_in address; int s, opvalue; socklen_t slen;
opvalue = 8; slen = sizeof(opvalue);
memset(&address, 0, sizeof(address));
if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) ||
((setsockopt(s, IPPROTO_IP, IP_TOS, &opvalue, slen)) < 0) )
return 0;
address.sin_family = AF_INET;
address.sin_port = htons((unsigned short)port);
if (!server)
if (!(server = gethostbyname(hostname)))
return 0;
memcpy(&address.sin_addr.s_addr, server->h_addr, server->h_length);
if (connect(s, (struct sockaddr*) &address, sizeof(address)) == -1)
return 0;//gets stuck here when it enters there!!!
return s;
}
int getport()
{
unsigned int b[6];
int i, answer; size_t len; char* port_str;
//if ((answer = send_command("PASV")) != 227) return 0;
send_recv(sockfd,"pasv\012");
port_str = recv_buf + 4;
len = strlen(port_str += 4);
for (i = 0; i < len; i++)
if (!isdigit(port_str[i])) port_str[i] = ' ';
if (sscanf(port_str, "%u %u %u %u %u %u",
b, b + 1, b + 2, b + 3, b + 4, b + 5) != 6)
return 0;
return b[4] * 256 + b[5];
}
Once again thanx!!!