?? ftpdo.c
字號:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include "NetWork.h"
#include "Public.h"
#include "DaemonCon.h"
#include "CMD.h"
/* Ftp起點 */
int FtpServer()
{
int clientSocket, serverSocket, pid;
serverSocket = NetContral(NULL, 21, -1, 2);
if (serverSocket == SERVER_LISTEN_EXCEPTION || serverSocket == SERVER_BIND_EXCEPTION)
{
fprintf(stderr, "Bind exception\n");
exit(-1);
}
memset(&State, 0, sizeof(LoginState));
State.Socket = -1;
sid = shmget(shmkey, sizeof(struct state_struct), 0666 | IPC_CREAT);
sMemory = shmat(sid, NULL, 0);
memset(sMemory, 0, sizeof(struct state_struct));
ftpState = (struct state_struct *)sMemory;
int semid = semget(semkey, 1, 0666 | IPC_CREAT);
initSem();
signal(SIGCHLD, SIG_IGN);
for (;;)
{
clientSocket = accept(serverSocket, NULL, NULL);
State.s = clientSocket;
if ((pid = fork()) == 0)
{
close(serverSocket);
char itr[MAX_BUF_SIZE];
char abuf[MAX_BUF_SIZE];
char *buf = abuf;
char *str = itr;
fd_set fdset;
struct timeval timeout;
int result;
Write(State.s, FTP_GREET, strlen(FTP_GREET));
GetSysConfig();
for (;;)
{
buf = abuf;
FD_ZERO(&fdset);
FD_SET(State.s, &fdset);
memset(itr, 0, sizeof(itr));
memset(abuf,0, sizeof(abuf));
timeout.tv_sec = State.MAX_TIME;
timeout.tv_usec = 0;
result = select(State.s+1, &fdset, NULL, NULL, &timeout);
if (result == 0)
{
if (State.Socket == -1)
{
close(State.s);
exit(0);
}
else
continue;
}
else if (result < 0)
{
if (errno == EINTR)
continue;
else
exit(1);
}
int readN = ReadLine(State.s, buf, MAX_BUF_SIZE);
if (readN < 0)
continue;
else if (readN == 0)
abuf[0] = '\0';
GetCommand(&buf, &str);
strupp(str); //將串轉為大寫
int i = 0;
while (cmdList[i].cmd)
{
if (strcmp(cmdList[i].cmd, str) == 0)
{
if (cmdList[i].check & NEED_LOGIN && !State.isLogin)
{
Write(State.s, "500 You No Login!!!!!\r\n", strlen("500 You No Login!!!!!\r\n"));
break;
}
if (cmdList[i].check & NO_LOGIN && State.isLogin)
{
Write(State.s, "500 You is Login!!!!!\r\n", strlen("500 You is Login!!!!!\r\n"));
break;
}
if (cmdList[i].check & NEED_PARAM)
{
if (strlen(buf) == 0)
{
char noParam[MAX_BUF_SIZE];
sprintf(noParam, "550 %s Command Fail, Need param!\r\n", str);
Write(clientSocket, noParam, strlen(noParam));
break;
}
}
if (cmdList[i].cmd_handler == NULL)
{
Write(clientSocket, FTP_COMMANDNOTIMPL, strlen(FTP_COMMANDNOTIMPL));
break;
}
cmdList[i].cmd_handler(buf);
break;
}
i++;
}
if (cmdList[i].cmd == NULL)
Write(State.s, FTP_BADCMD, strlen(FTP_BADCMD));
}
exit(0);
}
else if (pid == -1);
else
close(clientSocket);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -