Fixed the god-awful indentation

This commit is contained in:
mathusummut
2018-05-06 00:01:36 +02:00
parent 643e3820f5
commit 35b7b1f1a6
459 changed files with 204893 additions and 217545 deletions

View File

@@ -30,14 +30,13 @@
#include "ftp.h"
/**
* @brief User account data
*/
typedef struct
{
char name[MAXLEN_USERNAME+1]; ///< user name
char passw[MAXLEN_PASSWORD+1]; ///< password of the account
char ftpRoot[MAX_PATH_LEN+1]; ///< root path of the user account on the server
/**
* @brief User account data
*/
typedef struct {
char name[MAXLEN_USERNAME + 1]; ///< user name
char passw[MAXLEN_PASSWORD + 1]; ///< password of the account
char ftpRoot[MAX_PATH_LEN + 1]; ///< root path of the user account on the server
int ftpRootLen; ///< length of ftpRoot
int accRights; ///< access rights of a account
@@ -49,13 +48,11 @@ typedef struct
LOCAL ftpUserAccount_S ftpUsers[MAX_USERS];
int ftpDeleteAccount(const char* name)
{
int ftpDeleteAccount(const char* name) {
int n;
n = ftpFindAccount(name); // check if account already exists
if(n > 0)
{
if (n > 0) {
ftpUsers[n - 1].name[0] = '\0'; // delete account
return 0;
}
@@ -82,24 +79,20 @@ int ftpDeleteAccount(const char* name)
*
* @return 0 on success; -1 if MAX_USERS is reached
*/
int ftpCreateAccount(const char* name, const char* passw, const char* root, int acc)
{
int ftpCreateAccount(const char* name, const char* passw, const char* root, int acc) {
int n;
n = ftpFindAccount(name); // check if account already exists
if(n > 0)
{
if (n > 0) {
ftpUsers[n - 1].name[0] = '\0'; // delete account
}
for(n = 0; n < MAX_USERS; n++)
{
if(ftpUsers[n].name[0] == '\0')
{
for (n = 0; n < MAX_USERS; n++) {
if (ftpUsers[n].name[0] == '\0') {
strncpy(ftpUsers[n].name, name, MAXLEN_USERNAME);
strncpy(ftpUsers[n].passw, passw, MAXLEN_PASSWORD);
strncpy(ftpUsers[n].ftpRoot, root, MAX_PATH_LEN);
ftpUsers[n].ftpRootLen = (int)strlen(root);
ftpUsers[n].ftpRootLen = (int) strlen(root);
ftpUsers[n].accRights = acc;
return 0;
}
@@ -117,12 +110,11 @@ int ftpCreateAccount(const char* name, const char* passw, const char* root, int
*
* @return 0 if user is not found; 1 to MAX_USERS+1 if user is found
*/
int ftpFindAccount(const char* name)
{
if(name[0] != '\0') {
int ftpFindAccount(const char* name) {
if (name[0] != '\0') {
int n;
for(n = 0; n < MAX_USERS; n++) {
if(!strncmp(ftpUsers[n].name, name, MAXLEN_USERNAME)) {
for (n = 0; n < MAX_USERS; n++) {
if (!strncmp(ftpUsers[n].name, name, MAXLEN_USERNAME)) {
return n + 1;
}
}
@@ -130,12 +122,10 @@ int ftpFindAccount(const char* name)
return 0;
}
const char * ftpFindAccountById(int userid)
{
if(userid == 0) {
const char * ftpFindAccountById(int userid) {
if (userid == 0) {
return 0;
}
else if(ftpUsers[userid - 1].name[0] == '\0') {
} else if (ftpUsers[userid - 1].name[0] == '\0') {
return 0;
}
@@ -154,15 +144,12 @@ const char * ftpFindAccountById(int userid)
* - -1: invalid user account id
* - else: incorrect password
*/
int ftpCheckPassword(int userId, const char* passw)
{
if(userId == 0) {
int ftpCheckPassword(int userId, const char* passw) {
if (userId == 0) {
return -1;
}
else if(ftpUsers[userId - 1].passw[0] == '\0') {
} else if (ftpUsers[userId - 1].passw[0] == '\0') {
return 0;
}
else {
} else {
return strncmp(ftpUsers[userId - 1].passw, passw, MAXLEN_PASSWORD);
}
}
@@ -178,12 +165,11 @@ int ftpCheckPassword(int userId, const char* passw)
* @return - 0: the needed access rights are fulfilled
* - -1: invalid user account id
*/
int ftpCheckAccRights(int userId, int accRights)
{
if(!userId)
int ftpCheckAccRights(int userId, int accRights) {
if (!userId)
return -1;
if((ftpUsers[userId - 1].accRights & accRights) == accRights)
if ((ftpUsers[userId - 1].accRights & accRights) == accRights)
return 0;
return -1;
@@ -197,11 +183,10 @@ int ftpCheckAccRights(int userId, int accRights)
*
* @return root directory name or NULL if the user account id is invalid
*/
const char* ftpGetRoot(int userId, int* len)
{
if(!userId)
const char* ftpGetRoot(int userId, int* len) {
if (!userId)
return NULL;
if(len)
if (len)
*len = ftpUsers[userId - 1].ftpRootLen;
return ftpUsers[userId - 1].ftpRoot;

File diff suppressed because it is too large Load Diff

View File

@@ -27,24 +27,22 @@
#include "ftp.h"
#include "ftpIfc.h"
/**
* @brief Removes the trailing slash of a directory path
*
* @param path directory path
*
* @return len of path
*/
int ftpRemoveTrailingSlash(char* path)
{
/**
* @brief Removes the trailing slash of a directory path
*
* @param path directory path
*
* @return len of path
*/
int ftpRemoveTrailingSlash(char* path) {
size_t len = strlen(path);
if(len > 1)
{
if (len > 1) {
len--;
if(path[len] == '/')
if (path[len] == '/')
path[len] = '\0';
}
return (int)len;
return (int) len;
}
/**
@@ -54,36 +52,33 @@ int ftpRemoveTrailingSlash(char* path)
*
* @param path directory path
*/
void ftpRemoveDoubleSlash(char* path)
{
char *p;
void ftpRemoveDoubleSlash(char* path) {
char *p;
p = path;
p = path;
while(*p != '\0')
{
if((p[0] == '/') && (p[1] == '/'))
ftpStrcpy(p, &p[1]);
else
p++;
}
while (*p != '\0') {
if ((p[0] == '/') && (p[1] == '/'))
ftpStrcpy(p, &p[1]);
else
p++;
}
}
/**
* @brief Merges multiple path strings
*
* The function catenates all passed strings to a new string and assures that
* The function catenates all passed strings to a new string and assures that
* the result does not exceed MAX_PATH_LEN. The last parameter has to be
* NULL.
* Not all embedded environments support variadic functions, or they are
* too expensive.
* too expensive.
*
* @param dest user name
*
* @return Pointer to dest
*/
char* ftpMergePaths(char* dest, ...)
{
char* ftpMergePaths(char* dest, ...) {
const char* src;
char* dst = dest;
int len = 0;
@@ -91,10 +86,8 @@ char* ftpMergePaths(char* dest, ...)
va_start(args, dest);
while((src = va_arg(args, const char*)))
{
while((*src != '\0') && (len < MAX_PATH_LEN-1))
{
while ((src = va_arg(args, const char*))) {
while ((*src != '\0') && (len < MAX_PATH_LEN - 1)) {
*dst++ = *src++;
len++;
}
@@ -109,41 +102,40 @@ char* ftpMergePaths(char* dest, ...)
/**
* @brief Determine the unix-time
*
* The function reads the clock from the target-layer and converts it to the
* The function reads the clock from the target-layer and converts it to the
* unix-time. The code is taken from http://de.wikipedia.org/wiki/Unixzeit
*
* @return seconds since 1. Januar 1970 00:00
* @return seconds since 1. Januar 1970 00:00
*/
uint32_t ftpGetUnixTime(void)
{
uint32_t ftpGetUnixTime(void) {
ftpTime_S t;
uint32_t unixTime = 0;
int j;
ftpGetLocalTime(&t);
for(j=1970;j<t.year;j++) // leap years
for (j = 1970; j < t.year; j++) // leap years
{
if(j%4==0 && (j%100!=0 || j%400==0))
unixTime+=(366*24*60*60);
if (j % 4 == 0 && (j % 100 != 0 || j % 400 == 0))
unixTime += (366 * 24 * 60 * 60);
else
unixTime+=(365*24*60*60);
unixTime += (365 * 24 * 60 * 60);
}
for(j=1;j<t.month;j++) // days per month 31/30/29/28
for (j = 1; j < t.month; j++) // days per month 31/30/29/28
{
if(j==1 || j==3 || j==5 || j==7 || j==8 || j==10 || j==12)
unixTime+=(31*24*60*60); // months with 31 days
if(j==4 || j==6 || j==9 || j==11)
unixTime+=(30*24*60*60); // months with 30 days
if( (j==2) && (t.year%4==0 && (t.year%100!=0 || t.year%400==0)) )
unixTime+=(29*24*60*60);
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 10 || j == 12)
unixTime += (31 * 24 * 60 * 60); // months with 31 days
if (j == 4 || j == 6 || j == 9 || j == 11)
unixTime += (30 * 24 * 60 * 60); // months with 30 days
if ((j == 2) && (t.year % 4 == 0 && (t.year % 100 != 0 || t.year % 400 == 0)))
unixTime += (29 * 24 * 60 * 60);
else
unixTime+=(28*24*60*60);
unixTime += (28 * 24 * 60 * 60);
}
unixTime+=((t.day-1)*24*60*60);
unixTime+=(t.hour*60*60);
unixTime+=t.minute*60;
unixTime+=t.second;
unixTime += ((t.day - 1) * 24 * 60 * 60);
unixTime += (t.hour * 60 * 60);
unixTime += t.minute * 60;
unixTime += t.second;
return unixTime;
}
@@ -155,17 +147,16 @@ uint32_t ftpGetUnixTime(void)
* part of dest and src > dest. Because ANSI-C explicitly declares that case
* as undefined our strcpy guarantees to copy from lower to higher
* addresses.
*
*
* @param dest destination string
* @param src Null-terminated source string
*
* @return destination string
* @return destination string
*/
char *ftpStrcpy(char *dest, const char *src)
{
char *d = dest;
const char *s = src;
char *ftpStrcpy(char *dest, const char *src) {
char *d = dest;
const char *s = src;
while ((*d++ = *s++));
return dest;
while ((*d++ = *s++));
return dest;
}

View File

@@ -31,32 +31,30 @@
#include "ftpMessages.h"
/**
* @brief server-sockets that listens for incoming connections
*/
/**
* @brief server-sockets that listens for incoming connections
*/
LOCAL socket_t server;
LOCAL int serverListenPort;
LOCAL int serverPassiveListenPort;
//LOCAL socket_t serverPassivePort;
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2,
ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4,
ftpIsClientAllowedToGetFileType cb5) {
ftpFindExternalFTPServerIp = cb1;
ftpAddUPNPPortForward = cb2;
ftpRemoveUPNPPortForward = cb3;
ftpIsValidClient = cb4;
ftpIsClientAllowedToGetFile = cb5;
ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4,
ftpIsClientAllowedToGetFileType cb5) {
ftpFindExternalFTPServerIp = cb1;
ftpAddUPNPPortForward = cb2;
ftpRemoveUPNPPortForward = cb3;
ftpIsValidClient = cb4;
ftpIsClientAllowedToGetFile = cb5;
}
int ftpGetListenPort()
{
return serverListenPort;
int ftpGetListenPort() {
return serverListenPort;
}
int ftpGetPassivePort()
{
return serverPassiveListenPort;
int ftpGetPassivePort() {
return serverPassiveListenPort;
}
//socket_t ftpGetServerPassivePortListenSocket()
@@ -71,29 +69,27 @@ int ftpGetPassivePort()
* @return - 0: server started successfully
* - -1: could not create server socket
*/
int ftpStart(int portNumber)
{
serverListenPort = portNumber;
serverPassiveListenPort = portNumber + 1;
server = -1; // set server socket to invalid value
int ftpStart(int portNumber) {
serverListenPort = portNumber;
serverPassiveListenPort = portNumber + 1;
server = -1; // set server socket to invalid value
//serverPassivePort = -1;
if(VERBOSE_MODE_ENABLED) printf("Feathery FTP-Server\n");
if (VERBOSE_MODE_ENABLED) printf("Feathery FTP-Server\n");
ftpArchInit();
if(VERBOSE_MODE_ENABLED) printf("Creating server socket");
if (VERBOSE_MODE_ENABLED) printf("Creating server socket");
server = ftpCreateServerSocket(serverListenPort); // create main listener socket
if(server < 0)
{
if(VERBOSE_MODE_ENABLED) printf("\t\terror\n");
if (server < 0) {
if (VERBOSE_MODE_ENABLED) printf("\t\terror\n");
return -1;
}
if(VERBOSE_MODE_ENABLED) printf("\t\tok\n");
if(VERBOSE_MODE_ENABLED) printf("Server successfully started\n");
if (VERBOSE_MODE_ENABLED) printf("\t\tok\n");
if (VERBOSE_MODE_ENABLED) printf("Server successfully started\n");
ftpTrackSocket(server); // add socket to "watchlist"
@@ -126,8 +122,7 @@ if(VERBOSE_MODE_ENABLED) printf("Server passive port successfully started\n");
*
* @return 0 noting to do; else server has received some data
*/
int ftpState(void)
{
int ftpState(void) {
return 0;
}
@@ -139,46 +134,41 @@ int ftpState(void)
* all received control-strings are dispatched to the command-module.
* Note, the function blocks if there is nothing to do.
*/
int ftpExecute(void)
{
int processedWork=0;
int n=0;
int socksRdy=0;
ftpSession_S *pSession=NULL;
int sessionId=0;
int ftpExecute(void) {
int processedWork = 0;
int n = 0;
int socksRdy = 0;
ftpSession_S *pSession = NULL;
int sessionId = 0;
//int activeJobs=0;
int len;
int bufLen;
int activeJobs = ftpGetActiveTransCnt(); // are there any active transmitions?
//for(n = 0; (activeJobs > 0) && (n < MAX_CONNECTIONS); n++)
for(n = 0; n < MAX_CONNECTIONS; n++)
{
for (n = 0; n < MAX_CONNECTIONS; n++) {
pSession = ftpGetSession(n);
if(pSession->activeTrans.op) // has this session an active transmition?
if (pSession->activeTrans.op) // has this session an active transmition?
{
processedWork = 1;
processedWork = 1;
ftpExecTransmission(n); // do the job
activeJobs--;
}
}
if(ftpGetActiveTransCnt()) // don't block if there's still something to do
if (ftpGetActiveTransCnt()) // don't block if there's still something to do
{
socksRdy = ftpSelect(TRUE);
}
else
{
} else {
//if(VERBOSE_MODE_ENABLED) printf("ftpExecute calling blocking select\n");
socksRdy = ftpSelect(FALSE);
}
if(socksRdy > 0)
{
if(VERBOSE_MODE_ENABLED) printf("ftpExecute socksRdy = %d\n",socksRdy);
if (socksRdy > 0) {
if (VERBOSE_MODE_ENABLED) printf("ftpExecute socksRdy = %d\n", socksRdy);
processedWork = 1;
if(ftpTestSocket(server)) // server listner-socket signaled?
processedWork = 1;
if (ftpTestSocket(server)) // server listner-socket signaled?
{
socket_t clientSocket;
ip_t remoteIP;
@@ -186,19 +176,15 @@ int ftpExecute(void)
socksRdy--;
clientSocket = ftpAcceptServerConnection(server, &remoteIP, &remotePort);
if(clientSocket >= 0)
{
if(VERBOSE_MODE_ENABLED) printf("ftpExecute ftpAcceptServerConnection = %d\n",clientSocket);
if (clientSocket >= 0) {
if (VERBOSE_MODE_ENABLED) printf("ftpExecute ftpAcceptServerConnection = %d\n", clientSocket);
sessionId = ftpOpenSession(clientSocket, remoteIP, remotePort);
if(sessionId >= 0)
{
if (sessionId >= 0) {
ftpTrackSocket(clientSocket);
ftpSendMsg(MSG_NORMAL, sessionId, 220, ftpMsg037);
}
else
{
if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection refused; Session limit reached, about to close socket = %d\n",clientSocket);
} else {
if (VERBOSE_MODE_ENABLED) printf("ERROR: Connection refused; Session limit reached, about to close socket = %d\n", clientSocket);
ftpUntrackSocket(clientSocket);
ftpCloseSocket(&clientSocket);
@@ -207,45 +193,39 @@ int ftpExecute(void)
}
//socksRdy = ftpSelect(TRUE);
for(n = 0; (socksRdy > 0) && (n < MAX_CONNECTIONS); n++)
{
for (n = 0; (socksRdy > 0) && (n < MAX_CONNECTIONS); n++) {
pSession = ftpGetSession(n);
if(pSession->open)
{
socket_t ctrlSocket = pSession->ctrlSocket;
if (pSession->open) {
socket_t ctrlSocket = pSession->ctrlSocket;
if(ftpTestSocket(ctrlSocket))
{
if(VERBOSE_MODE_ENABLED) printf("ftpExecute signaled socket = %d, session = %d\n",ctrlSocket,n);
if (ftpTestSocket(ctrlSocket)) {
if (VERBOSE_MODE_ENABLED) printf("ftpExecute signaled socket = %d, session = %d\n", ctrlSocket, n);
socksRdy--;
bufLen = (LEN_RXBUF - pSession->rxBufWriteIdx);
len = ftpReceive(ctrlSocket,
&pSession->rxBuf[pSession->rxBufWriteIdx],
bufLen);
if(len <= 0) // has client shutdown the connection?
&pSession->rxBuf[pSession->rxBufWriteIdx],
bufLen);
if (len <= 0) // has client shutdown the connection?
{
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("In ftpExecute ERROR ON RECEIVE session = %d for socket = %d, data len = %d index = %d, len = %d, error = %d [%s]\n",n,ctrlSocket,bufLen,pSession->rxBufWriteIdx,len,errorNumber,errText);
if (VERBOSE_MODE_ENABLED) printf("In ftpExecute ERROR ON RECEIVE session = %d for socket = %d, data len = %d index = %d, len = %d, error = %d [%s]\n", n, ctrlSocket, bufLen, pSession->rxBufWriteIdx, len, errorNumber, errText);
ftpUntrackSocket(ctrlSocket);
ftpCloseSession(n);
}
else
{
} else {
pSession->rxBufWriteIdx += len;
ftpParseCmd(n);
}
}
/// @bug Session-Timeout-Management doesn't work
if((ftpGetUnixTime() - pSession->timeLastCmd) > SESSION_TIMEOUT)
{
if(VERBOSE_MODE_ENABLED) printf("\nIn ftpExecute ERROR: SESSION TIMED OUT for socket = %d\n",ctrlSocket);
if ((ftpGetUnixTime() - pSession->timeLastCmd) > SESSION_TIMEOUT) {
if (VERBOSE_MODE_ENABLED) printf("\nIn ftpExecute ERROR: SESSION TIMED OUT for socket = %d\n", ctrlSocket);
ftpSendMsg(MSG_NORMAL, n, 421, ftpMsg036);
ftpSendMsg(MSG_NORMAL, n, 421, ftpMsg036);
ftpUntrackSocket(ctrlSocket);
ftpCloseSession(n);
}
}
}
}
}
@@ -261,27 +241,24 @@ int ftpExecute(void)
* @return 0
*/
int ftpShutdown(void)
{
int ftpShutdown(void) {
int n;
if(VERBOSE_MODE_ENABLED) printf("About to Shutdown Feathery FTP-Server server [%d]\n",server);
if (VERBOSE_MODE_ENABLED) printf("About to Shutdown Feathery FTP-Server server [%d]\n", server);
ftpUntrackSocket(server);
ftpCloseSocket(&server);
//ftpCloseSocket(serverPassivePort);
if(VERBOSE_MODE_ENABLED) printf("About to Shutdown clients\n");
if (VERBOSE_MODE_ENABLED) printf("About to Shutdown clients\n");
for(n = 0; n < MAX_CONNECTIONS; n++)
{
if(ftpGetSession(n)->open)
{
for (n = 0; n < MAX_CONNECTIONS; n++) {
if (ftpGetSession(n)->open) {
ftpUntrackSocket(ftpGetSession(n)->ctrlSocket);
}
ftpCloseSession(n);
}
if(VERBOSE_MODE_ENABLED) printf("About to Shutdown stack\n");
if (VERBOSE_MODE_ENABLED) printf("About to Shutdown stack\n");
ftpArchCleanup();
return 0;

View File

@@ -19,17 +19,16 @@
#if 0
#include "ftpIfc.h"
int main(void)
{
int main(void) {
ftpCreateAccount("anonymous", "", "./", FTP_ACC_RD | FTP_ACC_LS | FTP_ACC_DIR);
ftpCreateAccount("nothing", "", "./", 0);
ftpCreateAccount("reader", "", "./", FTP_ACC_RD);
ftpCreateAccount("writer", "", "./", FTP_ACC_WR);
ftpCreateAccount("lister", "", "./", FTP_ACC_LS);
ftpCreateAccount("admin", "xxx", "./", FTP_ACC_RD | FTP_ACC_WR | FTP_ACC_LS | FTP_ACC_DIR);
ftpCreateAccount("admin", "xxx", "./", FTP_ACC_RD | FTP_ACC_WR | FTP_ACC_LS | FTP_ACC_DIR);
ftpStart();
while(1)
while (1)
ftpExecute();
ftpShutdown();

View File

@@ -32,11 +32,11 @@
#include "ftpConfig.h"
#include "ftp.h"
/**
* @brief array that holds the data of all ftp sessions
*
* The index represents the session ID.
*/
/**
* @brief array that holds the data of all ftp sessions
*
* The index represents the session ID.
*/
LOCAL ftpSession_S sessions[MAX_CONNECTIONS];
/**
@@ -62,32 +62,29 @@ LOCAL char pathScratchBuf[MAX_PATH_LEN];
*
* @return session id or -1 if session limit is reached
*/
int ftpOpenSession(socket_t ctrlSocket, ip_t remoteIp, port_t remotePort)
{
int ftpOpenSession(socket_t ctrlSocket, ip_t remoteIp, port_t remotePort) {
int n;
for(n = 0; n < MAX_CONNECTIONS; n++)
{
if(!sessions[n].open)
{
sessions[n].open = TRUE;
sessions[n].authenticated = FALSE;
sessions[n].userId = 0;
sessions[n].workingDir[0] = '\0';
sessions[n].remoteIp = remoteIp;
sessions[n].remotePort = remotePort;
sessions[n].remoteDataPort = 0;
sessions[n].passive = FALSE;
sessions[n].binary = TRUE;
sessions[n].timeLastCmd = ftpGetUnixTime();
sessions[n].ctrlSocket = ctrlSocket;
sessions[n].passiveDataSocket = -1;
sessions[n].rxBufWriteIdx = 0;
sessions[n].activeTrans.op = OP_NOP;
for (n = 0; n < MAX_CONNECTIONS; n++) {
if (!sessions[n].open) {
sessions[n].open = TRUE;
sessions[n].authenticated = FALSE;
sessions[n].userId = 0;
sessions[n].workingDir[0] = '\0';
sessions[n].remoteIp = remoteIp;
sessions[n].remotePort = remotePort;
sessions[n].remoteDataPort = 0;
sessions[n].passive = FALSE;
sessions[n].binary = TRUE;
sessions[n].timeLastCmd = ftpGetUnixTime();
sessions[n].ctrlSocket = ctrlSocket;
sessions[n].passiveDataSocket = -1;
sessions[n].rxBufWriteIdx = 0;
sessions[n].activeTrans.op = OP_NOP;
sessions[n].activeTrans.fsHandle = NULL;
sessions[n].activeTrans.dataSocket = -1;
sessions[n].activeTrans.fileSize = 0;
if(VERBOSE_MODE_ENABLED) printf("ftpOpenSession started for ctrlSocket: %d\n",ctrlSocket);
if (VERBOSE_MODE_ENABLED) printf("ftpOpenSession started for ctrlSocket: %d\n", ctrlSocket);
return n;
}
@@ -102,8 +99,7 @@ int ftpOpenSession(socket_t ctrlSocket, ip_t remoteIp, port_t remotePort)
*
* @return 0
*/
int ftpAuthSession(int id)
{
int ftpAuthSession(int id) {
sessions[id].authenticated = TRUE;
strcpy(sessions[id].workingDir, "/");
@@ -119,41 +115,39 @@ int ftpAuthSession(int id)
*
* @return 0
*/
int ftpCloseSession(int id)
{
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d, ctrlSocket = %d\n",
id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort,sessions[id].ctrlSocket);
int ftpCloseSession(int id) {
if (VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d, ctrlSocket = %d\n",
id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort, sessions[id].ctrlSocket);
if(ftpFindExternalFTPServerIp != NULL && ftpFindExternalFTPServerIp(sessions[id].remoteIp) != 0)
{
//if(ftpRemoveUPNPPortForward)
//{
//if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort);
if (ftpFindExternalFTPServerIp != NULL && ftpFindExternalFTPServerIp(sessions[id].remoteIp) != 0) {
//if(ftpRemoveUPNPPortForward)
//{
//if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort);
//ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort);
sessions[id].remoteFTPServerPassivePort = 0;
//}
}
//if(sessions[id].open) {
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession about to Close socket = %d, dataSocket = %d, activeDataSocket = %d, for sessionId = %d\n",sessions[id].ctrlSocket,sessions[id].passiveDataSocket,sessions[id].activeTrans.dataSocket,id);
//ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort);
sessions[id].remoteFTPServerPassivePort = 0;
//}
}
//if(sessions[id].open) {
if (VERBOSE_MODE_ENABLED) printf("In ftpCloseSession about to Close socket = %d, dataSocket = %d, activeDataSocket = %d, for sessionId = %d\n", sessions[id].ctrlSocket, sessions[id].passiveDataSocket, sessions[id].activeTrans.dataSocket, id);
ftpUntrackSocket(sessions[id].ctrlSocket);
ftpCloseSocket(&sessions[id].ctrlSocket);
ftpCloseTransmission(id);
ftpUntrackSocket(sessions[id].passiveDataSocket);
ftpCloseSocket(&sessions[id].passiveDataSocket);
//}
sessions[id].remoteIp = 0;
sessions[id].ctrlSocket = 0;
sessions[id].passiveDataSocket = 0;
sessions[id].passiveIp = 0;
sessions[id].passivePort = 0;
sessions[id].activeTrans.dataSocket = 0;
sessions[id].activeTrans.op = OP_NOP;
sessions[id].activeTrans.fileSize = 0;
sessions[id].open = FALSE;
//}
sessions[id].remoteIp = 0;
sessions[id].ctrlSocket = 0;
sessions[id].passiveDataSocket = 0;
sessions[id].passiveIp = 0;
sessions[id].passivePort = 0;
sessions[id].activeTrans.dataSocket = 0;
sessions[id].activeTrans.op = OP_NOP;
sessions[id].activeTrans.fileSize = 0;
sessions[id].open = FALSE;
if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
if (VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
return 0;
}
@@ -165,8 +159,7 @@ if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
*
* @return pointer to session data
*/
ftpSession_S* ftpGetSession(int id)
{
ftpSession_S* ftpGetSession(int id) {
return &sessions[id];
}
@@ -180,58 +173,49 @@ ftpSession_S* ftpGetSession(int id)
*
* @return 0
*/
LOCAL int normalizePath(char* path)
{
char *in;
char *r = path;
LOCAL int normalizePath(char* path) {
char *in;
char *r = path;
in = path;
in = path;
while((in = strchr(in, '/')))
{
if(!strncmp(in, "/./", 3))
{
ftpStrcpy(in, &in[2]);
continue;
}
if(!strcmp(in, "/."))
{
in[1] = '\0';
continue;
}
if(!strncmp(in, "/../", 4))
{
*in = '\0';
r = strrchr(path, '/');
if(r == NULL)
r = path;
ftpStrcpy(r, &in[3]);
in = r;
continue;
}
if(!strcmp(in, "/.."))
{
*in = '\0';
r = strrchr(path, '/');
if(r)
{
if(r == path)
r[1] = '\0';
else
*r = '\0';
}
else
{
in[0] = '/';
in[1] = '\0';
}
while ((in = strchr(in, '/'))) {
if (!strncmp(in, "/./", 3)) {
ftpStrcpy(in, &in[2]);
continue;
}
if (!strcmp(in, "/.")) {
in[1] = '\0';
continue;
}
if (!strncmp(in, "/../", 4)) {
*in = '\0';
r = strrchr(path, '/');
if (r == NULL)
r = path;
ftpStrcpy(r, &in[3]);
in = r;
continue;
}
if (!strcmp(in, "/..")) {
*in = '\0';
r = strrchr(path, '/');
if (r) {
if (r == path)
r[1] = '\0';
else
*r = '\0';
} else {
in[0] = '/';
in[1] = '\0';
}
continue;
}
in++;
}
continue;
}
in++;
}
return 0;
return 0;
}
@@ -251,41 +235,38 @@ LOCAL int normalizePath(char* path)
*
* @return translated absolute server path
*/
const char* ftpGetRealPath(int id, const char* path, int normalize)
{
char ftpRoot[2048]="";
const char* ftpGetRealPath(int id, const char* path, int normalize) {
char ftpRoot[2048] = "";
int ftpRootLen;
int len;
const char *ftp_rootget = ftpGetRoot(sessions[id].userId, &len);
snprintf(ftpRoot,2047,"%s",ftp_rootget);
ftpRootLen = (int)strlen(ftpRoot);
if(ftpRootLen > 0 && ftpRoot[ftpRootLen-1] != '/') {
strcat(ftpRoot,"/");
snprintf(ftpRoot, 2047, "%s", ftp_rootget);
ftpRootLen = (int) strlen(ftpRoot);
if (ftpRootLen > 0 && ftpRoot[ftpRootLen - 1] != '/') {
strcat(ftpRoot, "/");
}
if(VERBOSE_MODE_ENABLED) printf("#1 ftpGetRealPath id = %d path [%s] ftpRoot [%s] sessions[id].workingDir [%s] normalize = %d\n", id, path, ftpRoot, sessions[id].workingDir,normalize);
if (VERBOSE_MODE_ENABLED) printf("#1 ftpGetRealPath id = %d path [%s] ftpRoot [%s] sessions[id].workingDir [%s] normalize = %d\n", id, path, ftpRoot, sessions[id].workingDir, normalize);
pathScratchBuf[0]='\0';
if(path[0] == '/' || strcmp(path,sessions[id].workingDir) == 0) // absolute path?
{
ftpMergePaths(pathScratchBuf, ftpRoot, path, NULL);
}
else
pathScratchBuf[0] = '\0';
if (path[0] == '/' || strcmp(path, sessions[id].workingDir) == 0) // absolute path?
{
ftpMergePaths(pathScratchBuf, ftpRoot, path, NULL);
} else {
ftpMergePaths(pathScratchBuf, ftpRoot, sessions[id].workingDir, "/", path, NULL);
//ftpMergePaths(pathScratchBuf, ftpRoot, path, NULL);
}
if(VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathScratchBuf [%s]\n", path, ftpRoot, pathScratchBuf);
if (VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathScratchBuf [%s]\n", path, ftpRoot, pathScratchBuf);
ftpRemoveDoubleSlash(pathScratchBuf);
if(normalize) {
if (normalize) {
normalizePath(pathScratchBuf);
}
ftpRemoveTrailingSlash(pathScratchBuf);
if(VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathScratchBuf [%s]\n", path, ftpRoot, pathScratchBuf);
if (VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathScratchBuf [%s]\n", path, ftpRoot, pathScratchBuf);
return pathScratchBuf;
}
@@ -298,27 +279,26 @@ if(VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathSc
*
* @return 0 on success; -2 if the directory doesn't exist
*/
int ftpChangeDir(int id, const char* path)
{
int ftpChangeDir(int id, const char* path) {
ftpPathInfo_S fileInfo;
const char* realPath = ftpGetRealPath(id, path, TRUE);
int len;
ftpGetRoot(sessions[id].userId, &len); // determine len of root-path
if(len == 1) // if len == 1 root-path == '/'
if (len == 1) // if len == 1 root-path == '/'
len = 0;
if(VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] sessions[id].workingDir [%s]\n", path, realPath, sessions[id].workingDir);
if (VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] sessions[id].workingDir [%s]\n", path, realPath, sessions[id].workingDir);
if(ftpStat(realPath, &fileInfo) || (fileInfo.type != TYPE_DIR)) // directory accessible?
if (ftpStat(realPath, &fileInfo) || (fileInfo.type != TYPE_DIR)) // directory accessible?
return -2;
strncpy(sessions[id].workingDir, &realPath[len], MAX_PATH_LEN-1); // apply path
if(sessions[id].workingDir[0] == '\0')
strncpy(sessions[id].workingDir, &realPath[len], MAX_PATH_LEN - 1); // apply path
if (sessions[id].workingDir[0] == '\0')
strcpy(sessions[id].workingDir, "/");
if(VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] NEW sessions[id].workingDir [%s]\n", path, realPath, sessions[id].workingDir);
if (VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] NEW sessions[id].workingDir [%s]\n", path, realPath, sessions[id].workingDir);
return 0;
}
@@ -326,37 +306,31 @@ if(VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] NEW sessio
/**
* Open an ftp transmission
*/
void ftpOpenTransmission(int id, operation_E op, void* fsHandle, socket_t dataSocket, uint32_t fileSize)
{
void ftpOpenTransmission(int id, operation_E op, void* fsHandle, socket_t dataSocket, uint32_t fileSize) {
actTransCnt++;
sessions[id].activeTrans.op = op;
sessions[id].activeTrans.fsHandle = fsHandle;
sessions[id].activeTrans.op = op;
sessions[id].activeTrans.fsHandle = fsHandle;
sessions[id].activeTrans.dataSocket = dataSocket;
sessions[id].activeTrans.fileSize = fileSize;
sessions[id].activeTrans.fileSize = fileSize;
}
/**
* Close an ftp transmission
*/
void ftpCloseTransmission(int id)
{
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseTransmission about to Close socket = %d, for sessionId = %d, fsHandle [%p] op = %d\n",
sessions[id].activeTrans.dataSocket, id,sessions[id].activeTrans.fsHandle,sessions[id].activeTrans.op);
void ftpCloseTransmission(int id) {
if (VERBOSE_MODE_ENABLED) printf("In ftpCloseTransmission about to Close socket = %d, for sessionId = %d, fsHandle [%p] op = %d\n",
sessions[id].activeTrans.dataSocket, id, sessions[id].activeTrans.fsHandle, sessions[id].activeTrans.op);
if(sessions[id].activeTrans.dataSocket > 0)
{
if (sessions[id].activeTrans.dataSocket > 0) {
ftpUntrackSocket(sessions[id].activeTrans.dataSocket);
ftpCloseSocket(&sessions[id].activeTrans.dataSocket);
}
if(sessions[id].activeTrans.op != OP_NOP) // is thera an active transmission?
if (sessions[id].activeTrans.op != OP_NOP) // is thera an active transmission?
{
if(sessions[id].activeTrans.op == OP_LIST)
{
if (sessions[id].activeTrans.op == OP_LIST) {
ftpCloseDir(sessions[id].activeTrans.fsHandle);
}
else
{
} else {
ftpCloseFile(sessions[id].activeTrans.fsHandle);
}
sessions[id].activeTrans.fsHandle = NULL;
@@ -369,7 +343,6 @@ void ftpCloseTransmission(int id)
/**
* Get the active transaction count
*/
int ftpGetActiveTransCnt(void)
{
int ftpGetActiveTransCnt(void) {
return actTransCnt;
}

View File

@@ -34,9 +34,9 @@
#include <unistd.h>
#if defined(__FreeBSD__)
#include <sys/uio.h>
#include <sys/uio.h>
#else
//#include <sys/sendfile.h>
//#include <sys/sendfile.h>
#endif
#include <fcntl.h>
@@ -57,33 +57,29 @@ LOCAL fd_set signaledSockets;
LOCAL int maxSockNr;
void ftpArchInit()
{
void ftpArchInit() {
ownIp = 0;
maxSockNr = 0;
FD_ZERO(&watchedSockets);
FD_ZERO(&signaledSockets);
}
void ftpArchCleanup(void)
{
void ftpArchCleanup(void) {
}
int ftpGetLocalTime(ftpTime_S *t)
{
int ftpGetLocalTime(ftpTime_S *t) {
struct tm *pTime;
time_t tim;
time(&tim);
pTime = localtime(&tim);
if(pTime)
{
t->year = (uint16_t)pTime->tm_year + 1900;
t->month = (uint8_t)pTime->tm_mon + 1;
t->day = (uint8_t)pTime->tm_mday;
t->hour = (uint8_t)pTime->tm_hour;
t->minute = (uint8_t)pTime->tm_min;
t->second = (uint8_t)pTime->tm_sec;
if (pTime) {
t->year = (uint16_t) pTime->tm_year + 1900;
t->month = (uint8_t) pTime->tm_mon + 1;
t->day = (uint8_t) pTime->tm_mday;
t->hour = (uint8_t) pTime->tm_hour;
t->minute = (uint8_t) pTime->tm_min;
t->second = (uint8_t) pTime->tm_sec;
return 0;
}
@@ -91,88 +87,80 @@ int ftpGetLocalTime(ftpTime_S *t)
return -1;
}
void* ftpOpenDir(const char* path)
{
void* ftpOpenDir(const char* path) {
return opendir(path);
}
const char* ftpReadDir(void* dirHandle)
{
const char* ftpReadDir(void* dirHandle) {
struct dirent* dirEntry;
dirEntry = readdir(dirHandle);
if(dirEntry)
if (dirEntry)
return dirEntry->d_name;
else
return NULL;
}
int ftpCloseDir(void* dirHandle)
{
int ftpCloseDir(void* dirHandle) {
return closedir(dirHandle);
}
int ftpStat(const char* path, ftpPathInfo_S *info)
{
struct stat fileInfo = {0};
int ftpStat(const char* path, ftpPathInfo_S *info) {
struct stat fileInfo = { 0 };
struct tm *pTime;
struct passwd *pw;
struct group *gr;
if(!stat(path, &fileInfo))
{
if(S_ISREG(fileInfo.st_mode))
if (!stat(path, &fileInfo)) {
if (S_ISREG(fileInfo.st_mode))
info->type = TYPE_FILE;
else if(S_ISDIR(fileInfo.st_mode))
else if (S_ISDIR(fileInfo.st_mode))
info->type = TYPE_DIR;
else
info->type = TYPE_LINK;
pTime = localtime(&fileInfo.st_mtime);
if(pTime)
{
info->mTime.year = (uint16_t)pTime->tm_year + 1900;
info->mTime.month = (uint8_t)pTime->tm_mon + 1;
info->mTime.day = (uint8_t)pTime->tm_mday;
info->mTime.hour = (uint8_t)pTime->tm_hour;
info->mTime.minute = (uint8_t)pTime->tm_min;
info->mTime.second = (uint8_t)pTime->tm_sec;
if (pTime) {
info->mTime.year = (uint16_t) pTime->tm_year + 1900;
info->mTime.month = (uint8_t) pTime->tm_mon + 1;
info->mTime.day = (uint8_t) pTime->tm_mday;
info->mTime.hour = (uint8_t) pTime->tm_hour;
info->mTime.minute = (uint8_t) pTime->tm_min;
info->mTime.second = (uint8_t) pTime->tm_sec;
}
pTime = localtime(&fileInfo.st_ctime);
if(pTime)
{
info->cTime.year = (uint16_t)pTime->tm_year + 1900;
info->cTime.month = (uint8_t)pTime->tm_mon + 1;
info->cTime.day = (uint8_t)pTime->tm_mday;
info->cTime.hour = (uint8_t)pTime->tm_hour;
info->cTime.minute = (uint8_t)pTime->tm_min;
info->cTime.second = (uint8_t)pTime->tm_sec;
if (pTime) {
info->cTime.year = (uint16_t) pTime->tm_year + 1900;
info->cTime.month = (uint8_t) pTime->tm_mon + 1;
info->cTime.day = (uint8_t) pTime->tm_mday;
info->cTime.hour = (uint8_t) pTime->tm_hour;
info->cTime.minute = (uint8_t) pTime->tm_min;
info->cTime.second = (uint8_t) pTime->tm_sec;
}
pTime = localtime(&fileInfo.st_atime);
if(pTime)
{
info->aTime.year = (uint16_t)pTime->tm_year + 1900;
info->aTime.month = (uint8_t)pTime->tm_mon + 1;
info->aTime.day = (uint8_t)pTime->tm_mday;
info->aTime.hour = (uint8_t)pTime->tm_hour;
info->aTime.minute = (uint8_t)pTime->tm_min;
info->aTime.second = (uint8_t)pTime->tm_sec;
if (pTime) {
info->aTime.year = (uint16_t) pTime->tm_year + 1900;
info->aTime.month = (uint8_t) pTime->tm_mon + 1;
info->aTime.day = (uint8_t) pTime->tm_mday;
info->aTime.hour = (uint8_t) pTime->tm_hour;
info->aTime.minute = (uint8_t) pTime->tm_min;
info->aTime.second = (uint8_t) pTime->tm_sec;
}
info->size = (uint32_t)fileInfo.st_size;
info->links = (int)fileInfo.st_nlink;
info->size = (uint32_t) fileInfo.st_size;
info->links = (int) fileInfo.st_nlink;
pw = getpwuid(fileInfo.st_uid);
if(pw)
strncpy(info->user, pw->pw_name, sizeof(info->user)-1);
if (pw)
strncpy(info->user, pw->pw_name, sizeof(info->user) - 1);
else
snprintf(info->user, 20,"%04d", fileInfo.st_uid);
snprintf(info->user, 20, "%04d", fileInfo.st_uid);
gr = getgrgid(fileInfo.st_gid);
if(gr)
strncpy(info->group, gr->gr_name, sizeof(info->group)-1);
if (gr)
strncpy(info->group, gr->gr_name, sizeof(info->group) - 1);
else
snprintf(info->group, 20,"%04d", fileInfo.st_gid);
snprintf(info->group, 20, "%04d", fileInfo.st_gid);
return 0;
}
@@ -180,125 +168,108 @@ int ftpStat(const char* path, ftpPathInfo_S *info)
return -1;
}
int ftpMakeDir(const char* path)
{
int ftpMakeDir(const char* path) {
return mkdir(path, S_IRUSR | S_IWUSR);
}
int ftpRemoveDir(const char* path)
{
int ftpRemoveDir(const char* path) {
return rmdir(path);
}
int ftpCloseSocket(socket_t *s)
{
if(VERBOSE_MODE_ENABLED) printf("\nClosing socket: %d\n",*s);
int ftpCloseSocket(socket_t *s) {
if (VERBOSE_MODE_ENABLED) printf("\nClosing socket: %d\n", *s);
int ret = 0;
if(*s > 0) {
shutdown(*s,2);
ret = close(*s);
*s = 0;
}
int ret = 0;
if (*s > 0) {
shutdown(*s, 2);
ret = close(*s);
*s = 0;
}
return ret;
}
int ftpSend(socket_t s, const void *data, int len)
{
int ftpSend(socket_t s, const void *data, int len) {
int currLen = 0;
do
{
do {
#if defined(__APPLE__) && defined(SO_NOSIGPIPE)
currLen = send(s, data, len, SO_NOSIGPIPE);
#elif defined(MSG_NOSIGNAL)
currLen = send(s, data, len, MSG_NOSIGNAL);
currLen = send(s, data, len, MSG_NOSIGNAL);
#else
currLen = send(s, data, len, 0);
currLen = send(s, data, len, 0);
#endif
if(currLen >= 0)
{
if(currLen == 0)
{
if (currLen >= 0) {
if (currLen == 0) {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #1 ftpExecute ERROR ON SEND for socket = %d, data len = %d, error = %d [%s]\n",s,len,errorNumber,errText);
if (VERBOSE_MODE_ENABLED) printf("\nERROR #1 ftpExecute ERROR ON SEND for socket = %d, data len = %d, error = %d [%s]\n", s, len, errorNumber, errText);
}
len -= currLen;
data = (uint8_t*)data + currLen;
}
else
{
data = (uint8_t*) data + currLen;
} else {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #2 ftpExecute ERROR ON SEND for socket = %d, data len = %d, currLen = %d, error = %d [%s]\n",s,len,currLen,errorNumber,errText);
if (VERBOSE_MODE_ENABLED) printf("\nERROR #2 ftpExecute ERROR ON SEND for socket = %d, data len = %d, currLen = %d, error = %d [%s]\n", s, len, currLen, errorNumber, errText);
return -1;
}
} while(len > 0);
} while (len > 0);
return 0;
}
int ftpReceive(socket_t s, void *data, int len)
{
int ftpReceive(socket_t s, void *data, int len) {
return recv(s, data, len, 0);
}
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId)
{
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId) {
socket_t dataSocket;
struct sockaddr_in clientAddr;
struct sockaddr_in myAddr;
int on = 1;
unsigned len;
dataSocket = socket(AF_INET, SOCK_STREAM, 0);
if(dataSocket < 0)
if (dataSocket < 0)
return -1;
if(!passive)
{
if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
{
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
if (!passive) {
if (setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
if (VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
myAddr.sin_family = AF_INET;
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY;
myAddr.sin_port = htons(20);
myAddr.sin_zero[0] = 0;
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
myAddr.sin_port = htons(20);
myAddr.sin_zero[0] = 0;
if (bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) {
if (VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
clientAddr.sin_family = AF_INET;
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl(*ip);
clientAddr.sin_port = htons(*port);
clientAddr.sin_zero[0] = 0;
clientAddr.sin_port = htons(*port);
clientAddr.sin_zero[0] = 0;
if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr)))
{
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
if (connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr))) {
if (VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
}
else
{
int passivePort = ftpGetPassivePort() + sessionId;
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d for socket = %d\n",sessionId,passivePort,dataSocket);
} else {
int passivePort = ftpGetPassivePort() + sessionId;
if (VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d for socket = %d\n", sessionId, passivePort, dataSocket);
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY;
@@ -306,96 +277,87 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
myAddr.sin_zero[0] = 0;
//myAddr.sin_port = htons(ftpGetPassivePort() + sessionId);
if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
{
if(VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
if (setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
if (VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive bind failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
if (bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive bind failed for sessionId = %d using port #: %d about to close socket = %d\n", sessionId, passivePort, dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
}
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok for socket = %d\n",sessionId,passivePort,dataSocket);
if (VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok for socket = %d\n", sessionId, passivePort, dataSocket);
len = sizeof(myAddr);
if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive getsockname failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
len = sizeof(myAddr);
if (getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
{
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive getsockname failed for sessionId = %d using port #: %d about to close socket = %d\n", sessionId, passivePort, dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
return -1;
}
*port = ntohs(myAddr.sin_port);
*ip = ownIp;
*ip = ownIp;
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d about to listen on port: %d using listener socket: %d\n",sessionId,passivePort,*port,dataSocket);
if (VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d about to listen on port: %d using listener socket: %d\n", sessionId, passivePort, *port, dataSocket);
if(listen(dataSocket, 100))
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive listen failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
if (listen(dataSocket, 100)) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive listen failed for sessionId = %d using port #: %d about to close socket = %d\n", sessionId, passivePort, dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
//*port = ftpGetPassivePort();
//*port = ftpGetPassivePort();
//*ip = ownIp;
//dataSocket = ftpGetServerPassivePortListenSocket();
//dataSocket = ftpGetServerPassivePortListenSocket();
}
return dataSocket;
}
socket_t ftpAcceptDataConnection(socket_t listner)
{
socket_t ftpAcceptDataConnection(socket_t listner) {
struct sockaddr_in clientinfo;
unsigned len;
socket_t dataSocket;
unsigned len;
socket_t dataSocket;
len = sizeof(clientinfo);
len = sizeof(clientinfo);
dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len);
if(dataSocket < 0)
{
if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpAcceptDataConnection accept failed, dataSocket = %d, listner = %d\n", dataSocket,listner);
if (dataSocket < 0) {
if (VERBOSE_MODE_ENABLED) printf("ERROR In ftpAcceptDataConnection accept failed, dataSocket = %d, listner = %d\n", dataSocket, listner);
dataSocket = -1;
}
else
{
if(VERBOSE_MODE_ENABLED) printf("In ftpAcceptDataConnection accept new socket = %d, listner =%d\n", dataSocket,listner);
} else {
if (VERBOSE_MODE_ENABLED) printf("In ftpAcceptDataConnection accept new socket = %d, listner =%d\n", dataSocket, listner);
}
if(VERBOSE_MODE_ENABLED) printf("\nIn ftpAcceptDataConnection about to close listener socket = %d\n",listner);
if (VERBOSE_MODE_ENABLED) printf("\nIn ftpAcceptDataConnection about to close listener socket = %d\n", listner);
//ftpUntrackSocket(listner);
//ftpCloseSocket(&listner); // Server-Socket wird nicht mehr gebrauch deshalb schließen
ip_t remoteIP = ntohl(clientinfo.sin_addr.s_addr);
if(ftpIsValidClient && ftpIsValidClient(remoteIP) == 0)
{
if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection with %s is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(clientinfo.sin_addr),dataSocket);
ip_t remoteIP = ntohl(clientinfo.sin_addr.s_addr);
if (ftpIsValidClient && ftpIsValidClient(remoteIP) == 0) {
if (VERBOSE_MODE_ENABLED) printf("ERROR: Connection with %s is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(clientinfo.sin_addr), dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
dataSocket = -1;
}
dataSocket = -1;
}
return dataSocket;
}
socket_t ftpCreateServerSocket(int portNumber)
{
socket_t ftpCreateServerSocket(int portNumber) {
int theServer;
struct sockaddr_in serverinfo;
unsigned len;
@@ -403,7 +365,7 @@ socket_t ftpCreateServerSocket(int portNumber)
int opt_result = 0;
theServer = socket(AF_INET, SOCK_STREAM, 0);
if(theServer < 0)
if (theServer < 0)
return -1;
serverinfo.sin_family = AF_INET;
@@ -414,29 +376,26 @@ socket_t ftpCreateServerSocket(int portNumber)
opt_result = setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if(bind(theServer, (struct sockaddr *)&serverinfo, len))
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d opt_result = %d\n",theServer,opt_result);
if (bind(theServer, (struct sockaddr *)&serverinfo, len)) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d opt_result = %d\n", theServer, opt_result);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer);
return -2;
}
if(listen(theServer, 100))
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n",theServer);
if (listen(theServer, 100)) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d\n", theServer);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer);
return -3;
}
return (socket_t)theServer;
return (socket_t) theServer;
}
socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remotePort)
{
socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remotePort) {
struct sockaddr_in sockinfo;
socket_t clientSocket;
unsigned len;
@@ -445,79 +404,68 @@ socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remo
clientSocket = accept(server, (struct sockaddr *)&sockinfo, &len);
if(clientSocket < 0) {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed for socket = %d\n", clientSocket);
}
else {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept new socket = %d\n", clientSocket);
if (clientSocket < 0) {
if (VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed for socket = %d\n", clientSocket);
} else {
if (VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept new socket = %d\n", clientSocket);
}
*remoteIP = ntohl(sockinfo.sin_addr.s_addr);
*remotePort = ntohs(sockinfo.sin_port);
*remoteIP = ntohl(sockinfo.sin_addr.s_addr);
*remotePort = ntohs(sockinfo.sin_port);
if(!ownIp) // kennen wir schon die eigene IP?
if (!ownIp) // kennen wir schon die eigene IP?
{
len = sizeof(sockinfo);
if(clientSocket >= 0 && getsockname(clientSocket, (struct sockaddr *)&sockinfo, &len))
{
if(VERBOSE_MODE_ENABLED) printf("getsockname error\n");
if (clientSocket >= 0 && getsockname(clientSocket, (struct sockaddr *)&sockinfo, &len)) {
if (VERBOSE_MODE_ENABLED) printf("getsockname error\n");
}
ownIp = ntohl(sockinfo.sin_addr.s_addr); // eigene IP-Adresse abspeichern (wird dir PASV benätigt)
}
if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", inet_ntoa(sockinfo.sin_addr), *remotePort);
if (VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", inet_ntoa(sockinfo.sin_addr), *remotePort);
if(ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0)
{
if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(sockinfo.sin_addr), *remotePort,clientSocket);
if (ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0) {
if (VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(sockinfo.sin_addr), *remotePort, clientSocket);
ftpUntrackSocket(clientSocket);
ftpCloseSocket(&clientSocket);
clientSocket = -1;
}
ftpCloseSocket(&clientSocket);
clientSocket = -1;
}
return clientSocket;
}
int ftpTrackSocket(socket_t s)
{
if(s > maxSockNr)
int ftpTrackSocket(socket_t s) {
if (s > maxSockNr)
maxSockNr = s;
FD_SET(s, &watchedSockets);
return 0;
}
int ftpUntrackSocket(socket_t s)
{
if(s > 0)
{
int ftpUntrackSocket(socket_t s) {
if (s > 0) {
FD_CLR(s, &watchedSockets);
}
return 0;
}
int ftpTestSocket(socket_t s)
{
int ftpTestSocket(socket_t s) {
return FD_ISSET(s, &signaledSockets);
}
int ftpSelect(int poll)
{
int ftpSelect(int poll) {
signaledSockets = watchedSockets;
if(poll)
{
struct timeval t = {0};
if (poll) {
struct timeval t = { 0 };
//t.tv_usec = 100;
return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t);
}
else
{
struct timeval t = {0};
return select(maxSockNr + 1, &signaledSockets, NULL, NULL, &t);
} else {
struct timeval t = { 0 };
t.tv_usec = 1000;
return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t);
return select(maxSockNr + 1, &signaledSockets, NULL, NULL, &t);
}
}

View File

@@ -39,44 +39,39 @@ ip_t ownIp;
LOCAL fd_set watchedSockets;
LOCAL fd_set signaledSockets;
LOCAL int maxSockNr;
int wsaInitCount=0;
int wsaInitCount = 0;
void ftpArchInit()
{
void ftpArchInit() {
WSADATA wsaData;
ownIp = 0;
maxSockNr = 0;
FD_ZERO(&watchedSockets);
if(VERBOSE_MODE_ENABLED) printf("Feathery calling WSAStartup...\n");
WSAStartup(MAKEWORD(2, 0),&wsaData);
if (VERBOSE_MODE_ENABLED) printf("Feathery calling WSAStartup...\n");
WSAStartup(MAKEWORD(2, 0), &wsaData);
wsaInitCount++;
}
void ftpArchCleanup(void)
{
if(VERBOSE_MODE_ENABLED) printf("Feathery calling WSACleanup...[%d]\n",wsaInitCount);
if(wsaInitCount > 0)
{
void ftpArchCleanup(void) {
if (VERBOSE_MODE_ENABLED) printf("Feathery calling WSACleanup...[%d]\n", wsaInitCount);
if (wsaInitCount > 0) {
WSACleanup(); // WINSOCK freigeben
wsaInitCount--;
}
}
int ftpGetLocalTime(ftpTime_S *t)
{
int ftpGetLocalTime(ftpTime_S *t) {
struct tm *pTime;
time_t tim;
time(&tim);
pTime = localtime(&tim);
if(pTime)
{
t->year = (uint16_t)pTime->tm_year + 1900;
t->month = (uint8_t)pTime->tm_mon + 1;
t->day = (uint8_t)pTime->tm_mday;
t->hour = (uint8_t)pTime->tm_hour;
t->minute = (uint8_t)pTime->tm_min;
t->second = (uint8_t)pTime->tm_sec;
if (pTime) {
t->year = (uint16_t) pTime->tm_year + 1900;
t->month = (uint8_t) pTime->tm_mon + 1;
t->day = (uint8_t) pTime->tm_mday;
t->hour = (uint8_t) pTime->tm_hour;
t->minute = (uint8_t) pTime->tm_min;
t->second = (uint8_t) pTime->tm_sec;
return 0;
}
@@ -84,51 +79,42 @@ int ftpGetLocalTime(ftpTime_S *t)
return -1;
}
typedef struct
{
typedef struct {
HANDLE findHandle;
char path[MAX_PATH_LEN];
}readDir_S;
void* ftpOpenDir(const char* path)
{
void* ftpOpenDir(const char* path) {
readDir_S* p = malloc(sizeof(readDir_S));
if(p)
{
if (p) {
p->findHandle = INVALID_HANDLE_VALUE;
strncpy(p->path, path,strlen(path));
strncpy(p->path, path, strlen(path));
}
return p;
}
const char* ftpReadDir(void* dirHandle)
{
WIN32_FIND_DATA findData;
readDir_S* p = (readDir_S *)dirHandle;
const char* ftpReadDir(void* dirHandle) {
WIN32_FIND_DATA findData;
readDir_S* p = (readDir_S *) dirHandle;
if(dirHandle == NULL)
if (dirHandle == NULL)
return NULL;
if(p->findHandle == INVALID_HANDLE_VALUE)
{
if(strcmp(p->path, "/"))
strcat(p->path, "/*");
else
strcat(p->path, "*");
if (p->findHandle == INVALID_HANDLE_VALUE) {
if (strcmp(p->path, "/"))
strcat(p->path, "/*");
else
strcat(p->path, "*");
p->findHandle = FindFirstFile(p->path, &findData);
if(p->findHandle != INVALID_HANDLE_VALUE)
{
if (p->findHandle != INVALID_HANDLE_VALUE) {
strcpy(p->path, findData.cFileName);
return p->path;
}
return NULL;
}
else
{
if(FindNextFile(p->findHandle, &findData))
{
} else {
if (FindNextFile(p->findHandle, &findData)) {
strcpy(p->path, findData.cFileName);
return p->path;
}
@@ -136,65 +122,58 @@ const char* ftpReadDir(void* dirHandle)
}
}
int ftpCloseDir(void* dirHandle)
{
int ftpCloseDir(void* dirHandle) {
readDir_S* p = dirHandle;
if(dirHandle)
{
if (dirHandle) {
FindClose(p->findHandle);
free(p);
}
return 0;
}
int ftpStat(const char* path, ftpPathInfo_S *info)
{
struct stat fileInfo = {0};
int ftpStat(const char* path, ftpPathInfo_S *info) {
struct stat fileInfo = { 0 };
struct tm *pTime;
if(!stat(path, &fileInfo))
{
if(fileInfo.st_mode & _S_IFREG)
if (!stat(path, &fileInfo)) {
if (fileInfo.st_mode & _S_IFREG)
info->type = TYPE_FILE;
else if(fileInfo.st_mode & _S_IFDIR)
else if (fileInfo.st_mode & _S_IFDIR)
info->type = TYPE_DIR;
else
info->type = TYPE_LINK;
pTime = localtime(&fileInfo.st_mtime);
if(pTime)
{
info->mTime.year = (uint16_t)pTime->tm_year + 1900;
info->mTime.month = (uint8_t)pTime->tm_mon + 1;
info->mTime.day = (uint8_t)pTime->tm_mday;
info->mTime.hour = (uint8_t)pTime->tm_hour;
info->mTime.minute = (uint8_t)pTime->tm_min;
info->mTime.second = (uint8_t)pTime->tm_sec;
if (pTime) {
info->mTime.year = (uint16_t) pTime->tm_year + 1900;
info->mTime.month = (uint8_t) pTime->tm_mon + 1;
info->mTime.day = (uint8_t) pTime->tm_mday;
info->mTime.hour = (uint8_t) pTime->tm_hour;
info->mTime.minute = (uint8_t) pTime->tm_min;
info->mTime.second = (uint8_t) pTime->tm_sec;
}
pTime = localtime(&fileInfo.st_ctime);
if(pTime)
{
info->cTime.year = (uint16_t)pTime->tm_year + 1900;
info->cTime.month = (uint8_t)pTime->tm_mon + 1;
info->cTime.day = (uint8_t)pTime->tm_mday;
info->cTime.hour = (uint8_t)pTime->tm_hour;
info->cTime.minute = (uint8_t)pTime->tm_min;
info->cTime.second = (uint8_t)pTime->tm_sec;
if (pTime) {
info->cTime.year = (uint16_t) pTime->tm_year + 1900;
info->cTime.month = (uint8_t) pTime->tm_mon + 1;
info->cTime.day = (uint8_t) pTime->tm_mday;
info->cTime.hour = (uint8_t) pTime->tm_hour;
info->cTime.minute = (uint8_t) pTime->tm_min;
info->cTime.second = (uint8_t) pTime->tm_sec;
}
pTime = localtime(&fileInfo.st_atime);
if(pTime)
{
info->aTime.year = (uint16_t)pTime->tm_year + 1900;
info->aTime.month = (uint8_t)pTime->tm_mon + 1;
info->aTime.day = (uint8_t)pTime->tm_mday;
info->aTime.hour = (uint8_t)pTime->tm_hour;
info->aTime.minute = (uint8_t)pTime->tm_min;
info->aTime.second = (uint8_t)pTime->tm_sec;
if (pTime) {
info->aTime.year = (uint16_t) pTime->tm_year + 1900;
info->aTime.month = (uint8_t) pTime->tm_mon + 1;
info->aTime.day = (uint8_t) pTime->tm_mday;
info->aTime.hour = (uint8_t) pTime->tm_hour;
info->aTime.minute = (uint8_t) pTime->tm_min;
info->aTime.second = (uint8_t) pTime->tm_sec;
}
info->size = (uint32_t)fileInfo.st_size;
info->links = (int)fileInfo.st_nlink;
info->size = (uint32_t) fileInfo.st_size;
info->links = (int) fileInfo.st_nlink;
strncpy(info->user, "ftp", sizeof(info->user));
strncpy(info->group, "ftp", sizeof(info->group));
@@ -205,125 +184,108 @@ int ftpStat(const char* path, ftpPathInfo_S *info)
return -1;
}
int ftpMakeDir(const char* path)
{
int ftpMakeDir(const char* path) {
return !CreateDirectory(path, NULL);
}
int ftpRemoveDir(const char* path)
{
int ftpRemoveDir(const char* path) {
return !RemoveDirectory(path);
}
int ftpCloseSocket(socket_t *s)
{
int ftpCloseSocket(socket_t *s) {
int ret = 0;
if(VERBOSE_MODE_ENABLED) printf("\nClosing socket: %d\n",*s);
if(*s > 0) {
shutdown((SOCKET)*s,2);
ret = closesocket((SOCKET)*s);
*s = 0;
}
if (VERBOSE_MODE_ENABLED) printf("\nClosing socket: %d\n", *s);
if (*s > 0) {
shutdown((SOCKET) *s, 2);
ret = closesocket((SOCKET) *s);
*s = 0;
}
return ret;
}
int ftpSend(socket_t s, const void *data, int len)
{
int ftpSend(socket_t s, const void *data, int len) {
int currLen = 0;
do
{
do {
#ifdef __APPLE__
currLen = send((SOCKET)s, data, len, SO_NOSIGPIPE);
currLen = send((SOCKET) s, data, len, SO_NOSIGPIPE);
#else
currLen = send((SOCKET)s, data, len, MSG_NOSIGNAL);
currLen = send((SOCKET) s, data, len, MSG_NOSIGNAL);
#endif
if(currLen >= 0)
{
if(currLen == 0)
{
if (currLen >= 0) {
if (currLen == 0) {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #1 ftpExecute ERROR ON SEND for socket = %d, data len = %d, error = %d [%s]\n",s,len,errorNumber,errText);
if (VERBOSE_MODE_ENABLED) printf("\nERROR #1 ftpExecute ERROR ON SEND for socket = %d, data len = %d, error = %d [%s]\n", s, len, errorNumber, errText);
}
len -= currLen;
data = (uint8_t*)data + currLen;
}
else
{
data = (uint8_t*) data + currLen;
} else {
int errorNumber = getLastSocketError();
const char *errText = getLastSocketErrorText(&errorNumber);
if(VERBOSE_MODE_ENABLED) printf("\nERROR #2 ftpExecute ERROR ON SEND for socket = %d, data len = %d, currLen = %d, error = %d [%s]\n",s,len,currLen,errorNumber,errText);
if (VERBOSE_MODE_ENABLED) printf("\nERROR #2 ftpExecute ERROR ON SEND for socket = %d, data len = %d, currLen = %d, error = %d [%s]\n", s, len, currLen, errorNumber, errText);
return -1;
}
} while(len > 0);
} while (len > 0);
return 0;
}
int ftpReceive(socket_t s, void *data, int len)
{
int ftpReceive(socket_t s, void *data, int len) {
return recv(s, data, len, 0);
}
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId)
{
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId) {
SOCKET dataSocket;
struct sockaddr_in clientAddr;
struct sockaddr_in myAddr;
BOOL on = 1;
unsigned len;
dataSocket = socket(AF_INET, SOCK_STREAM, 0);
if(dataSocket == INVALID_SOCKET)
if (dataSocket == INVALID_SOCKET)
return -1;
if(!passive)
{
if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)))
{
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
if (!passive) {
if (setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on))) {
if (VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #1 about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
myAddr.sin_family = AF_INET;
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY;
myAddr.sin_port = htons(20);
myAddr.sin_zero[0] = 0;
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
myAddr.sin_port = htons(20);
myAddr.sin_zero[0] = 0;
if (bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) {
if (VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
clientAddr.sin_family = AF_INET;
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl(*ip);
clientAddr.sin_port = htons(*port);
clientAddr.sin_port = htons(*port);
clientAddr.sin_zero[0] = 0;
if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr)))
{
if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
if (connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr))) {
if (VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
}
else
{
int passivePort = ftpGetPassivePort() + sessionId;
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d for socket = %d\n",sessionId,passivePort,dataSocket);
} else {
int passivePort = ftpGetPassivePort() + sessionId;
if (VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d for socket = %d\n", sessionId, passivePort, dataSocket);
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = INADDR_ANY;
@@ -331,97 +293,88 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses
myAddr.sin_zero[0] = 0;
//myAddr.sin_port = htons(ftpGetPassivePort() + sessionId);
if(setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)))
{
if(VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId);
if (setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on))) {
if (VERBOSE_MODE_ENABLED) printf("PASSIVE CONNECTION In ftpEstablishDataConnection setsockopt failed about to Close socket = %d, for sessionId = %d\n", dataSocket, sessionId);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive bind failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
if (bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive bind failed for sessionId = %d using port #: %d about to close socket = %d\n", sessionId, passivePort, dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
}
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok for socket = %d\n",sessionId,passivePort,dataSocket);
if (VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok for socket = %d\n", sessionId, passivePort, dataSocket);
len = sizeof(myAddr);
if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive getsockname failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
len = sizeof(myAddr);
if (getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
{
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive getsockname failed for sessionId = %d using port #: %d about to close socket = %d\n", sessionId, passivePort, dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
return -1;
}
*port = ntohs(myAddr.sin_port);
*ip = ownIp;
*ip = ownIp;
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d about to listen on port: %d using listener socket: %d\n",sessionId,passivePort,*port,dataSocket);
if (VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d about to listen on port: %d using listener socket: %d\n", sessionId, passivePort, *port, dataSocket);
if(listen(dataSocket, 100))
{
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive listen failed for sessionId = %d using port #: %d about to close socket = %d\n",sessionId,passivePort,dataSocket);
if (listen(dataSocket, 100)) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpEstablishDataConnection passive listen failed for sessionId = %d using port #: %d about to close socket = %d\n", sessionId, passivePort, dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
return -1;
}
//*port = ftpGetPassivePort();
//*port = ftpGetPassivePort();
//*ip = ownIp;
//dataSocket = ftpGetServerPassivePortListenSocket();
//dataSocket = ftpGetServerPassivePortListenSocket();
}
return (socket_t)dataSocket;
return (socket_t) dataSocket;
}
socket_t ftpAcceptDataConnection(socket_t listner)
{
socket_t ftpAcceptDataConnection(socket_t listner) {
struct sockaddr_in clientinfo;
unsigned len;
SOCKET dataSocket;
unsigned len;
SOCKET dataSocket;
ip_t remoteIP;
len = sizeof(clientinfo);
len = sizeof(clientinfo);
dataSocket = accept(listner, (struct sockaddr *)&clientinfo, &len);
if(dataSocket == INVALID_SOCKET)
{
if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpAcceptDataConnection accept failed, dataSocket = %d, listner = %d\n", dataSocket,listner);
if (dataSocket == INVALID_SOCKET) {
if (VERBOSE_MODE_ENABLED) printf("ERROR In ftpAcceptDataConnection accept failed, dataSocket = %d, listner = %d\n", dataSocket, listner);
//dataSocket = -1;
}
else
{
if(VERBOSE_MODE_ENABLED) printf("In ftpAcceptDataConnection accept new socket = %d, listner =%d\n", dataSocket,listner);
} else {
if (VERBOSE_MODE_ENABLED) printf("In ftpAcceptDataConnection accept new socket = %d, listner =%d\n", dataSocket, listner);
}
if(VERBOSE_MODE_ENABLED) printf("\nIn ftpAcceptDataConnection about to close listener socket = %d\n",listner);
if (VERBOSE_MODE_ENABLED) printf("\nIn ftpAcceptDataConnection about to close listener socket = %d\n", listner);
//ftpUntrackSocket(listner);
//ftpCloseSocket(&listner); // Server-Socket wird nicht mehr gebrauch deshalb schließen
remoteIP = ntohl(clientinfo.sin_addr.s_addr);
if(ftpIsValidClient && ftpIsValidClient(remoteIP) == 0)
{
if(VERBOSE_MODE_ENABLED) printf("ERROR: Connection with %s is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(clientinfo.sin_addr),dataSocket);
remoteIP = ntohl(clientinfo.sin_addr.s_addr);
if (ftpIsValidClient && ftpIsValidClient(remoteIP) == 0) {
if (VERBOSE_MODE_ENABLED) printf("ERROR: Connection with %s is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(clientinfo.sin_addr), dataSocket);
ftpUntrackSocket(dataSocket);
ftpCloseSocket(&dataSocket);
dataSocket = INVALID_SOCKET;
}
dataSocket = INVALID_SOCKET;
}
return (socket_t)dataSocket;
return (socket_t) dataSocket;
}
socket_t ftpCreateServerSocket(int portNumber)
{
socket_t ftpCreateServerSocket(int portNumber) {
SOCKET theServer;
struct sockaddr_in serverinfo;
unsigned len;
@@ -429,7 +382,7 @@ socket_t ftpCreateServerSocket(int portNumber)
int opt_result = 0;
theServer = socket(AF_INET, SOCK_STREAM, 0);
if(theServer == INVALID_SOCKET)
if (theServer == INVALID_SOCKET)
return -1;
serverinfo.sin_family = AF_INET;
@@ -438,29 +391,28 @@ socket_t ftpCreateServerSocket(int portNumber)
serverinfo.sin_zero[0] = 0;
len = sizeof(serverinfo);
opt_result = setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
opt_result = setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
if(bind(theServer, (struct sockaddr *)&serverinfo, len)) {
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d opt_result = %d\n",theServer,opt_result);
if (bind(theServer, (struct sockaddr *)&serverinfo, len)) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket bind FAILED about to close listener socket = %d opt_result = %d\n", theServer, opt_result);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer);
return -2;
}
if(listen(theServer, 100)) {
if(VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d opt_result = %d\n",theServer,opt_result);
if (listen(theServer, 100)) {
if (VERBOSE_MODE_ENABLED) printf("\nERROR In ftpCreateServerSocket listen FAILED about to close listener socket = %d opt_result = %d\n", theServer, opt_result);
ftpUntrackSocket(theServer);
ftpCloseSocket(&theServer);
return -3;
}
return (socket_t)theServer;
return (socket_t) theServer;
}
socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remotePort)
{
socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remotePort) {
struct sockaddr_in sockinfo;
socket_t clientSocket;
int len;
@@ -469,79 +421,68 @@ socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remo
clientSocket = accept(server, (struct sockaddr *)&sockinfo, &len);
if(clientSocket < 0) {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed for socket = %d\n", clientSocket);
}
else {
if(VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept new socket = %d\n", clientSocket);
if (clientSocket < 0) {
if (VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept failed for socket = %d\n", clientSocket);
} else {
if (VERBOSE_MODE_ENABLED) printf("ftpAcceptServerConnection accept new socket = %d\n", clientSocket);
}
*remoteIP = ntohl(sockinfo.sin_addr.s_addr);
*remotePort = ntohs(sockinfo.sin_port);
*remoteIP = ntohl(sockinfo.sin_addr.s_addr);
*remotePort = ntohs(sockinfo.sin_port);
if(!ownIp) // kennen wir schon die eigene IP?
if (!ownIp) // kennen wir schon die eigene IP?
{
len = sizeof(sockinfo);
if(getsockname(clientSocket, (struct sockaddr *)&sockinfo, &len))
{
if(VERBOSE_MODE_ENABLED) printf("getsockname error\n");
if (getsockname(clientSocket, (struct sockaddr *)&sockinfo, &len)) {
if (VERBOSE_MODE_ENABLED) printf("getsockname error\n");
}
ownIp = ntohl(sockinfo.sin_addr.s_addr); // eigene IP-Adresse abspeichern (wird dir PASV benätigt)
}
if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", inet_ntoa(sockinfo.sin_addr), *remotePort);
if (VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d accepted.\n", inet_ntoa(sockinfo.sin_addr), *remotePort);
if(ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0)
{
if(VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(sockinfo.sin_addr), *remotePort,clientSocket);
if (ftpIsValidClient && ftpIsValidClient(*remoteIP) == 0) {
if (VERBOSE_MODE_ENABLED) printf("Connection with %s on Port %d is NOT a valid trusted client, dropping connection closing socket = %d.\n", inet_ntoa(sockinfo.sin_addr), *remotePort, clientSocket);
ftpUntrackSocket(clientSocket);
ftpCloseSocket(&clientSocket);
clientSocket = -1;
}
ftpCloseSocket(&clientSocket);
clientSocket = -1;
}
return clientSocket;
}
int ftpTrackSocket(socket_t s)
{
if(s > maxSockNr)
int ftpTrackSocket(socket_t s) {
if (s > maxSockNr)
maxSockNr = s;
FD_SET(s, &watchedSockets);
return 0;
}
int ftpUntrackSocket(socket_t s)
{
if(s > 0)
{
int ftpUntrackSocket(socket_t s) {
if (s > 0) {
FD_CLR(s, &watchedSockets);
}
return 0;
}
int ftpTestSocket(socket_t s)
{
int ftpTestSocket(socket_t s) {
return FD_ISSET(s, &signaledSockets);
}
int ftpSelect(int poll)
{
int ftpSelect(int poll) {
signaledSockets = watchedSockets;
if(poll)
{
struct timeval t = {0};
if (poll) {
struct timeval t = { 0 };
//t.tv_usec = 100;
return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t);
}
else
{
struct timeval t = {0};
return select(maxSockNr + 1, &signaledSockets, NULL, NULL, &t);
} else {
struct timeval t = { 0 };
t.tv_usec = 1000;
return select(maxSockNr+1, &signaledSockets, NULL, NULL, &t);
return select(maxSockNr + 1, &signaledSockets, NULL, NULL, &t);
}
}