Owyn
May 19th, 2009, 08:00 AM
ftp::ftp() {
Connected = 0;
hListenSocket = INVALID_SOCKET;
hControlSocket = INVALID_SOCKET;
hDataSocket = INVALID_SOCKET;
bSendPort = 1;
ReadCommand = 0;
bMode=ASCII;
InitWinsock();
};
ftp::~ftp() {
};
void ftp::DoOpen( char *command)
{
char *szHost=NULL; /* remote host */
/*
* do not do anything if we are already connected.
*/
if( Connected ) {
//printf("Already connected. Close connection first.\n");
fflush(stdout);
return;
}
/*
* extract the remote host from command line (if given ).
* make a copy of the host name with strdup.
*/
if(!strcmp(command,"open") || !strcmp(command,"o")) {
//printf("Host:"); fgets(szBuffer,1024,stdin);
(void)strtok(szBuffer,"\n");
szHost = (char *)strdup(szBuffer);;
}
else if( !strncmp(command,"open ",5))
szHost = strdup(&command[5]);
else if( !strncmp(command,"o ",2) )
szHost = strdup(&command[2]);
else
szHost = strdup(command);
//printf("Connecting to %s\n",szHost);
hControlSocket = ConnectToServer(szHost,"21");
#if (defined(WIN32) || defined(_WIN32) )
Sleep(1);
#else
sleep(1);
#endif
if( hControlSocket > 0) {
//printf("Connected to %s\n",szHost);
/*#if (defined(WIN32) || defined(_WIN32) )
sprintf(command,"dftp: Connected to %s ", szHost);
SetConsoleTitle(command); // set console window title
#endif*/
Connected = 1; /* we ar now connected */
CNC=true;
GetReply(); /* get reply (welcome message) from server */
//DoLogin((char *)NULL); /* prompt for username and password */
//DoBinary(); /* default binary mode */
}
free(szHost); /* free the strdupped string */
}
int ftp::ConnectToServer(char *name, char *port)
{
int s;
unsigned int portnum;
struct sockaddr_in server;
struct hostent *hp;
while( name && *name == ' ') name++;
if( !name || ! (*name) )
return INVALID_SOCKET;
portnum = atoi(port);
bzero((char *) &server, sizeof(server));
if( isdigit(name[0])) {
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(name);
server.sin_port = htons(portnum);
}
else{
if ( (hp = gethostbyname(name)) == NULL)
{
#if (!defined( _WIN32 ) || !defined( WIN32 ))
perror("gethostbyname");
#else
//printf("gethostbyname: error code: %d\r\n", WSAGetLastError());
#endif
return INVALID_SOCKET;
}
bcopy(hp->h_addr,(char *) &server.sin_addr,hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(portnum);
}/* else */
/* create socket */
if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 1) {
#if (!defined( _WIN32 ) || !defined( WIN32 ))
perror("socket");
#else
//printf("socket: error code: %d\r\n", WSAGetLastError());
#endif
return INVALID_SOCKET;
}
if (connect(s,(struct sockaddr *)&server, sizeof(server))< 0) {
#if (!defined( _WIN32 ) || !defined( WIN32 ))
perror("connect");
#else
//printf("connect: error code: %d\r\n", WSAGetLastError());
#endif
return INVALID_SOCKET;
}
setsockopt(s,SOL_SOCKET,SO_LINGER,0,0);
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,0,0);
setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,0,0);
hDataSocket = s;
return s;
}
when i do DoOpen() it shows allways Connected == 1, and CNC == true ... where is the mistake?
Connected = 0;
hListenSocket = INVALID_SOCKET;
hControlSocket = INVALID_SOCKET;
hDataSocket = INVALID_SOCKET;
bSendPort = 1;
ReadCommand = 0;
bMode=ASCII;
InitWinsock();
};
ftp::~ftp() {
};
void ftp::DoOpen( char *command)
{
char *szHost=NULL; /* remote host */
/*
* do not do anything if we are already connected.
*/
if( Connected ) {
//printf("Already connected. Close connection first.\n");
fflush(stdout);
return;
}
/*
* extract the remote host from command line (if given ).
* make a copy of the host name with strdup.
*/
if(!strcmp(command,"open") || !strcmp(command,"o")) {
//printf("Host:"); fgets(szBuffer,1024,stdin);
(void)strtok(szBuffer,"\n");
szHost = (char *)strdup(szBuffer);;
}
else if( !strncmp(command,"open ",5))
szHost = strdup(&command[5]);
else if( !strncmp(command,"o ",2) )
szHost = strdup(&command[2]);
else
szHost = strdup(command);
//printf("Connecting to %s\n",szHost);
hControlSocket = ConnectToServer(szHost,"21");
#if (defined(WIN32) || defined(_WIN32) )
Sleep(1);
#else
sleep(1);
#endif
if( hControlSocket > 0) {
//printf("Connected to %s\n",szHost);
/*#if (defined(WIN32) || defined(_WIN32) )
sprintf(command,"dftp: Connected to %s ", szHost);
SetConsoleTitle(command); // set console window title
#endif*/
Connected = 1; /* we ar now connected */
CNC=true;
GetReply(); /* get reply (welcome message) from server */
//DoLogin((char *)NULL); /* prompt for username and password */
//DoBinary(); /* default binary mode */
}
free(szHost); /* free the strdupped string */
}
int ftp::ConnectToServer(char *name, char *port)
{
int s;
unsigned int portnum;
struct sockaddr_in server;
struct hostent *hp;
while( name && *name == ' ') name++;
if( !name || ! (*name) )
return INVALID_SOCKET;
portnum = atoi(port);
bzero((char *) &server, sizeof(server));
if( isdigit(name[0])) {
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(name);
server.sin_port = htons(portnum);
}
else{
if ( (hp = gethostbyname(name)) == NULL)
{
#if (!defined( _WIN32 ) || !defined( WIN32 ))
perror("gethostbyname");
#else
//printf("gethostbyname: error code: %d\r\n", WSAGetLastError());
#endif
return INVALID_SOCKET;
}
bcopy(hp->h_addr,(char *) &server.sin_addr,hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(portnum);
}/* else */
/* create socket */
if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 1) {
#if (!defined( _WIN32 ) || !defined( WIN32 ))
perror("socket");
#else
//printf("socket: error code: %d\r\n", WSAGetLastError());
#endif
return INVALID_SOCKET;
}
if (connect(s,(struct sockaddr *)&server, sizeof(server))< 0) {
#if (!defined( _WIN32 ) || !defined( WIN32 ))
perror("connect");
#else
//printf("connect: error code: %d\r\n", WSAGetLastError());
#endif
return INVALID_SOCKET;
}
setsockopt(s,SOL_SOCKET,SO_LINGER,0,0);
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,0,0);
setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,0,0);
hDataSocket = s;
return s;
}
when i do DoOpen() it shows allways Connected == 1, and CNC == true ... where is the mistake?