#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#define FTP_DEFAULT_PORT 21
#define SUCCESS 0
#define MAX_STR_LEN 64
#define MAX_BUF_LEN 4096
#define MAX_FILE_NAME 255
/* Global Variables */
struct sockaddr_in Ftp_Server_ctrl_addr; /*server's address for ctrl connection*/
struct sockaddr_in Ftp_Server_data_addr; /*server's address for data connection*/
int sockfd_ctrl; /* socket for control connection */
int sockfd_data; /* socket for data connection */
char ServerIP[MAX_STR_LEN]; /* Server's ip */
int ServerPort; /* Server's port */
char User[MAX_STR_LEN]; /* user's account for logining*/
char Password[MAX_STR_LEN]; /* user's password for logining*/
char MsgBuffer[MAX_BUF_LEN];
char DataBuffer[MAX_BUF_LEN];
static struct termios stored_settings;
/* print error imformation and exit program */
int ErrorExit(const char* error)
{
printf("Error: %s\n",error);
exit(-1);
}
/*
void echo_off(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr(0,TCSANOW,&new_settings);
return;
}
void echo_on(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
*/
/*********************************************send and receive message************************************************/
/* send control command to server with specified socket descriptor
* COMMAND<SP><PARAMETER><CRLF> or COMMAND<CRLF>
* example: "USER anonymous\r\n" or "PWD\r\n"
*/
int SendControlCommand(const char* cmd_code, const char* cmd_para)// test ok by myself
{
char SendBuffer[MAX_BUF_LEN];
int Length;
int SendRes;
memset(SendBuffer, 0, MAX_BUF_LEN);
if(cmd_code)
{
strcpy(SendBuffer,cmd_code);
if(cmd_para)
{
strcat(SendBuffer," ");
strcat(SendBuffer,cmd_para);
strcat(SendBuffer,"\r\n");
}
else
strcat(SendBuffer,"\r\n");
Length=strlen(SendBuffer);
SendRes = write(sockfd_ctrl,SendBuffer,Length);
}
return SendRes;
}
/* get reply message from server */ //test ok
int GetReplyMsg(void)
{
int count;
int reply;
memset(MsgBuffer, 0, MAX_BUF_LEN);
count = read(sockfd_ctrl,MsgBuffer,MAX_BUF_LEN);
reply = atoi(MsgBuffer);
printf("[Server reply] %s", MsgBuffer);
return reply;
}
/*************************************** login************************************/
/* get user's account from stdin */
void GetUserAccount(void) //test ok
{
char buf[MAX_BUF_LEN];
printf("User:(Press Enter for anonymous):");
fgets(buf,sizeof(buf),stdin);
if(buf[0]=='\n')
strncpy(User,"anonymous",9);
else
strncpy(User,buf,strlen(buf)-1);
// printf("User:%s\n",User);
}
/* get user's password from stdin*/
void GetUserPasswd(void) // test ok
{
char buf[MAX_BUF_LEN];
printf("Password:(Press Enter for anonymous):");
fgets(buf,sizeof(buf),stdin);
if(buf[0]=='\n')
strncpy(Password,"anonymous",9);
else
strncpy(Password,buf,strlen(buf)-1);
// printf("Password:%s\n",Password);
}
int FTP_Login(void) // test ok
{
GetUserAccount();
SendControlCommand("USER",User);
GetReplyMsg();
GetUserPasswd();
SendControlCommand("PASS",Password);
if(230 == GetReplyMsg())
return SUCCESS;
else
ErrorExit("can't login");
}
/*********************************************connect************************************************/
/* get Server's ip from stdin */
void GetServerIP(void) //test ok
{
char buf[MAX_BUF_LEN];
printf("Please input the Server's IP :");
fgets(buf,sizeof(buf),stdin);
if(buf[0]=='\n')
ErrorExit("did not input IP address!");
else
strncpy(ServerIP,buf,strlen(buf)-1);
}
/* get Server's port from stin */
void GetServerPort(void) //test ok
{
char buf[MAX_BUF_LEN];
printf("Please input the Server Port:");
fgets(buf,sizeof(buf),stdin);
if(buf[0]=='\n')
ErrorExit("did not input Server's Port!");
else
ServerPort = atoi(buf);
}
/* fill sockaddr_in with Main Function's arguments */ //test ok
void FillServerAddr(const char* ip, unsigned short int port, struct sockaddr_in* pServer_addr)
{
memset(pServer_addr, 0, sizeof(struct sockaddr_in));
pServer_addr->sin_family = AF_INET;
pServer_addr->sin_port = htons(port);
pServer_addr->sin_addr.s_addr = inet_addr(ip);
}
/* control connection */
int CreateControlConnection() //test ok
{
/* create socket descriptor*/
sockfd_ctrl = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sockfd_ctrl)
ErrorExit("Create socket failed!");
/* connect to the server */
if(connect(sockfd_ctrl,(struct sockaddr *)&Ftp_Server_ctrl_addr,sizeof(struct sockaddr_in)) == 0)
{
printf("Successfully connect to server:ip:%s,port:%d\n",
inet_ntoa(Ftp_Server_ctrl_addr.sin_addr) ,ntohs(Ftp_Server_ctrl_addr.sin_port));
}
else
ErrorExit("control connect failed!");
return 0;
}
/* Enter passive mode, and return server's data connection port */
int passive()
{
char PasvReply[MAX_BUF_LEN];
char* tmp;
int count, port;
SendControlCommand("PASV", NULL);
count = read(sockfd_ctrl, PasvReply, MAX_BUF_LEN);
printf("%s", PasvReply);
if(count <= 0)
return 0;
PasvReply[count] = '\0';
if(227 == atoi(PasvReply)) // Enter passive mode
{
tmp = strrchr(PasvReply, ',');
port = atoi(tmp+1);
*tmp = '\0';
tmp = strrchr(PasvReply, ',');
port += atoi(tmp+1)*256;
return port;
}
return 0;
}
int CreateDataConnection()
{
sockfd_data = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sockfd_data)
ErrorExit("Create socket failed!");
/* connect to the server */
if(connect(sockfd_data,(struct sockaddr *)&Ftp_Server_data_addr,sizeof(struct sockaddr_in)) == 0)
{
printf("Successfully connect to server:ip:%s,port:%d\n",
inet_ntoa(Ftp_Server_data_addr.sin_addr) ,ntohs(Ftp_Server_data_addr.sin_port));
}
else
ErrorExit("data connect failed!");
return 0;
}
/****************************FTP COMMADN*****************************************/
void FTP_CMD_START(void)
{
GetServerIP();
GetServerPort();
FillServerAddr(ServerIP, ServerPort, &Ftp_Server_ctrl_addr);
CreateControlConnection();
GetReplyMsg();
FTP_Login();
}
void FTP_CMD_QUIT(void)
{
SendControlCommand("QUIT", NULL);
GetReplyMsg();
close(sockfd_ctrl);
}
void FTP_CMD_PWD(void) // test ok
{
SendControlCommand("PWD", NULL);
GetReplyMsg();
}
void FTP_CMD_CWD(void) //test ok
{
char pathname[MAX_FILE_NAME];
char temp[MAX_FILE_NAME];
printf("Change to which directory:");
memset(pathname, 0, sizeof(pathname));
fgets(temp,sizeof(temp),stdin);
if(pathname[0]=='\n')
{
printf("please input the path name!");
return;
}
else
strncpy(pathname, temp, strlen(temp)-1);
SendControlCommand("CWD",pathname);
GetReplyMsg();
}
/* print local work directory */
void FTP_CMD_PLWD(void) //TEST OK
{
char localDir[MAX_FILE_NAME];
if(getcwd(localDir,MAX_FILE_NAME) != NULL)
printf("Current local directory: %s\n", localDir);
else
printf("Can't get local directory!");
}
/* change local work directory */
void FTP_CMD_CLD(void) //TEST OK
{
char pathname[MAX_FILE_NAME];
char temp[MAX_FILE_NAME];
printf("Change to which directory:");
fgets(temp,sizeof(temp),stdin);
if(pathname[0]=='\n')
{
printf("please input the path name!");
return;
}
else
strncpy(pathname, temp, strlen(temp)-1);
if(chdir(pathname) < 0)
printf("change local work directory failed!\n");
else
printf("change local directory to %s s
- 1
- 2
- 3
- 4
- 5
前往页