mirror of
https://github.com/glest/glest-source.git
synced 2025-10-02 18:36:48 +02:00
- updated FTP code to use specific ports for MG and not random FTP ports
This commit is contained in:
@@ -1145,6 +1145,7 @@ int glestMain(int argc, char** argv) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerSocket::setMaxPlayerCount(GameConstants::maxPlayers);
|
||||||
SystemFlags::VERBOSE_MODE_ENABLED = false;
|
SystemFlags::VERBOSE_MODE_ENABLED = false;
|
||||||
if(hasCommandArgument(argc, argv,GAME_ARGS[GAME_ARG_VERBOSE_MODE]) == true) {
|
if(hasCommandArgument(argc, argv,GAME_ARGS[GAME_ARG_VERBOSE_MODE]) == true) {
|
||||||
SystemFlags::VERBOSE_MODE_ENABLED = true;
|
SystemFlags::VERBOSE_MODE_ENABLED = true;
|
||||||
|
@@ -112,7 +112,7 @@ ServerInterface::ServerInterface() : GameNetworkInterface() {
|
|||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||||
int portNumber = Config::getInstance().getInt("FTPServerPort",intToStr(ServerSocket::getFTPServerPort()).c_str());
|
int portNumber = Config::getInstance().getInt("FTPServerPort",intToStr(ServerSocket::getFTPServerPort()).c_str());
|
||||||
ServerSocket::setFTPServerPort(portNumber);
|
ServerSocket::setFTPServerPort(portNumber);
|
||||||
ftpServer = new FTPServerThread(mapsPath,tilesetsPath,portNumber);
|
ftpServer = new FTPServerThread(mapsPath,tilesetsPath,portNumber,GameConstants::maxPlayers);
|
||||||
ftpServer->start();
|
ftpServer->start();
|
||||||
}
|
}
|
||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||||
|
@@ -177,7 +177,7 @@ extern int ftpRemoveDir(const char* path);
|
|||||||
extern int ftpCloseSocket(socket_t s);
|
extern int ftpCloseSocket(socket_t s);
|
||||||
extern int ftpSend(socket_t s, const void *data, int len);
|
extern int ftpSend(socket_t s, const void *data, int len);
|
||||||
extern int ftpReceive(socket_t s, void *data, int len);
|
extern int ftpReceive(socket_t s, void *data, int len);
|
||||||
extern socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port);
|
extern socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId);
|
||||||
extern socket_t ftpAcceptDataConnection(socket_t listner);
|
extern socket_t ftpAcceptDataConnection(socket_t listner);
|
||||||
extern socket_t ftpCreateServerSocket(int portNumber);
|
extern socket_t ftpCreateServerSocket(int portNumber);
|
||||||
extern socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remotePort);
|
extern socket_t ftpAcceptServerConnection(socket_t server, ip_t *remoteIP, port_t *remotePort);
|
||||||
@@ -186,4 +186,7 @@ extern int ftpUntrackSocket(socket_t s);
|
|||||||
extern int ftpTestSocket(socket_t s);
|
extern int ftpTestSocket(socket_t s);
|
||||||
extern int ftpSelect(int poll);
|
extern int ftpSelect(int poll);
|
||||||
|
|
||||||
|
extern int ftpGetPassivePort();
|
||||||
|
extern int ftpGetListenPort();
|
||||||
|
|
||||||
#endif /* FTP_H_ */
|
#endif /* FTP_H_ */
|
||||||
|
@@ -33,10 +33,11 @@ protected:
|
|||||||
std::pair<string,string> mapsPath;
|
std::pair<string,string> mapsPath;
|
||||||
std::pair<string,string> tilesetsPath;
|
std::pair<string,string> tilesetsPath;
|
||||||
int portNumber;
|
int portNumber;
|
||||||
|
int maxPlayers;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
FTPServerThread(std::pair<string,string> mapsPath, std::pair<string,string> tilesetsPath, int portNumber);
|
FTPServerThread(std::pair<string,string> mapsPath, std::pair<string,string> tilesetsPath, int portNumber,int maxPlayers);
|
||||||
~FTPServerThread();
|
~FTPServerThread();
|
||||||
virtual void execute();
|
virtual void execute();
|
||||||
virtual void signalQuit();
|
virtual void signalQuit();
|
||||||
|
@@ -215,6 +215,8 @@ protected:
|
|||||||
static int ftpServerPort;
|
static int ftpServerPort;
|
||||||
SDL_Thread *upnpdiscoverThread;
|
SDL_Thread *upnpdiscoverThread;
|
||||||
|
|
||||||
|
static int maxPlayerCount;
|
||||||
|
|
||||||
virtual void UPNPInitStatus(bool result);
|
virtual void UPNPInitStatus(bool result);
|
||||||
BroadCastSocketThread *broadCastThread;
|
BroadCastSocketThread *broadCastThread;
|
||||||
void startBroadCastThread();
|
void startBroadCastThread();
|
||||||
@@ -241,6 +243,8 @@ public:
|
|||||||
virtual void disconnectSocket();
|
virtual void disconnectSocket();
|
||||||
|
|
||||||
void NETdiscoverUPnPDevices();
|
void NETdiscoverUPnPDevices();
|
||||||
|
|
||||||
|
static int setMaxPlayerCount(int value) { maxPlayerCount=value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// =====================================================
|
// =====================================================
|
||||||
@@ -258,7 +262,7 @@ public:
|
|||||||
static bool upnp_add_redirect(int ports[2]);
|
static bool upnp_add_redirect(int ports[2]);
|
||||||
static void upnp_rem_redirect(int ext_port);
|
static void upnp_rem_redirect(int ext_port);
|
||||||
|
|
||||||
static void NETaddRedirects(int ports[4]);
|
static void NETaddRedirects(std::vector<int> UPNPPortForwardList);
|
||||||
static void NETremRedirects(int ext_port);
|
static void NETremRedirects(int ext_port);
|
||||||
|
|
||||||
static void AddUPNPPortForward(int internalPort, int externalPort);
|
static void AddUPNPPortForward(int internalPort, int externalPort);
|
||||||
|
@@ -228,6 +228,7 @@ LOCAL int ftpCmdPort(int sessionId, const char* args, int len)
|
|||||||
ftpCloseSocket(ftpGetSession(sessionId)->passiveDataSocket);
|
ftpCloseSocket(ftpGetSession(sessionId)->passiveDataSocket);
|
||||||
ftpGetSession(sessionId)->passiveDataSocket = -1;
|
ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||||
}
|
}
|
||||||
|
//ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||||
ftpGetSession(sessionId)->remoteDataPort = clientPort;
|
ftpGetSession(sessionId)->remoteDataPort = clientPort;
|
||||||
ftpGetSession(sessionId)->passive = FALSE;
|
ftpGetSession(sessionId)->passive = FALSE;
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 200, ftpMsg007);
|
ftpSendMsg(MSG_NORMAL, sessionId, 200, ftpMsg007);
|
||||||
@@ -419,7 +420,7 @@ LOCAL int ftpCmdList(int sessionId, const char* args, int len)
|
|||||||
|
|
||||||
if(ftpGetSession(sessionId)->passive == FALSE)
|
if(ftpGetSession(sessionId)->passive == FALSE)
|
||||||
{
|
{
|
||||||
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort);
|
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId);
|
||||||
if(s < 0)
|
if(s < 0)
|
||||||
{
|
{
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
||||||
@@ -454,7 +455,7 @@ LOCAL int ftpCmdNlst(int sessionId, const char* args, int len)
|
|||||||
|
|
||||||
if(ftpGetSession(sessionId)->passive == FALSE)
|
if(ftpGetSession(sessionId)->passive == FALSE)
|
||||||
{
|
{
|
||||||
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort);
|
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId);
|
||||||
if(s < 0)
|
if(s < 0)
|
||||||
{
|
{
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
||||||
@@ -497,7 +498,7 @@ if(VERBOSE_MODE_ENABLED) printf("stat() = %d fileInfo.type = %d\n", statResult,f
|
|||||||
|
|
||||||
if(ftpGetSession(sessionId)->passive == FALSE)
|
if(ftpGetSession(sessionId)->passive == FALSE)
|
||||||
{
|
{
|
||||||
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort);
|
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId);
|
||||||
if(s < 0)
|
if(s < 0)
|
||||||
{
|
{
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
||||||
@@ -539,7 +540,7 @@ LOCAL int ftpCmdStor(int sessionId, const char* args, int len)
|
|||||||
|
|
||||||
if(ftpGetSession(sessionId)->passive == FALSE)
|
if(ftpGetSession(sessionId)->passive == FALSE)
|
||||||
{
|
{
|
||||||
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort);
|
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId);
|
||||||
if(s < 0)
|
if(s < 0)
|
||||||
{
|
{
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
||||||
@@ -663,7 +664,8 @@ LOCAL int ftpCmdPasv(int sessionId, const char* args, int len)
|
|||||||
ftpCloseSocket(ftpGetSession(sessionId)->passiveDataSocket);
|
ftpCloseSocket(ftpGetSession(sessionId)->passiveDataSocket);
|
||||||
ftpGetSession(sessionId)->passiveDataSocket = -1;
|
ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||||
}
|
}
|
||||||
s = ftpEstablishDataConnection(TRUE, &ip, &port);
|
//ftpGetSession(sessionId)->passiveDataSocket = -1;
|
||||||
|
s = ftpEstablishDataConnection(TRUE, &ip, &port,sessionId);
|
||||||
if(s < 0)
|
if(s < 0)
|
||||||
{
|
{
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012);
|
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg012);
|
||||||
@@ -680,7 +682,7 @@ if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, client IP = %u, r
|
|||||||
|
|
||||||
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, adding UPNP port forward\n", sessionId);
|
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, adding UPNP port forward\n", sessionId);
|
||||||
|
|
||||||
ftpAddUPNPPortForward(port, port);
|
//ftpAddUPNPPortForward(port, port);
|
||||||
|
|
||||||
remoteFTPServerIp = ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp);
|
remoteFTPServerIp = ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp);
|
||||||
|
|
||||||
@@ -805,7 +807,7 @@ LOCAL int ftpCmdMlsd(int sessionId, const char* args, int len)
|
|||||||
|
|
||||||
if(ftpGetSession(sessionId)->passive == FALSE)
|
if(ftpGetSession(sessionId)->passive == FALSE)
|
||||||
{
|
{
|
||||||
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort);
|
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId);
|
||||||
if(s < 0)
|
if(s < 0)
|
||||||
{
|
{
|
||||||
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
ftpSendMsg(MSG_NORMAL, sessionId, 425, ftpMsg011);
|
||||||
|
@@ -35,6 +35,9 @@
|
|||||||
* @brief server-sockets that listens for incoming connections
|
* @brief server-sockets that listens for incoming connections
|
||||||
*/
|
*/
|
||||||
LOCAL socket_t server;
|
LOCAL socket_t server;
|
||||||
|
LOCAL int serverListenPort;
|
||||||
|
LOCAL int serverPassiveListenPort;
|
||||||
|
//LOCAL socket_t serverPassivePort;
|
||||||
|
|
||||||
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, ftpRemoveUPNPPortForwardType cb3) {
|
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, ftpRemoveUPNPPortForwardType cb3) {
|
||||||
ftpFindExternalFTPServerIp = cb1;
|
ftpFindExternalFTPServerIp = cb1;
|
||||||
@@ -42,6 +45,21 @@ void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2,
|
|||||||
ftpRemoveUPNPPortForward = cb3;
|
ftpRemoveUPNPPortForward = cb3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ftpGetListenPort()
|
||||||
|
{
|
||||||
|
return serverListenPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ftpGetPassivePort()
|
||||||
|
{
|
||||||
|
return serverPassiveListenPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
//socket_t ftpGetServerPassivePortListenSocket()
|
||||||
|
//{
|
||||||
|
// return serverPassivePort;
|
||||||
|
//}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initializes and starts the server
|
* @brief Initializes and starts the server
|
||||||
*
|
*
|
||||||
@@ -51,7 +69,10 @@ void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2,
|
|||||||
*/
|
*/
|
||||||
int ftpStart(int portNumber)
|
int ftpStart(int portNumber)
|
||||||
{
|
{
|
||||||
server = -1; // set server socket to invalid value
|
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");
|
||||||
|
|
||||||
@@ -59,7 +80,7 @@ if(VERBOSE_MODE_ENABLED) printf("Feathery FTP-Server\n");
|
|||||||
|
|
||||||
if(VERBOSE_MODE_ENABLED) printf("Creating server socket");
|
if(VERBOSE_MODE_ENABLED) printf("Creating server socket");
|
||||||
|
|
||||||
server = ftpCreateServerSocket(portNumber); // create main listener socket
|
server = ftpCreateServerSocket(serverListenPort); // create main listener socket
|
||||||
if(server < 0)
|
if(server < 0)
|
||||||
{
|
{
|
||||||
if(VERBOSE_MODE_ENABLED) printf("\t\terror\n");
|
if(VERBOSE_MODE_ENABLED) printf("\t\terror\n");
|
||||||
@@ -71,6 +92,23 @@ if(VERBOSE_MODE_ENABLED) printf("\t\tok\n");
|
|||||||
if(VERBOSE_MODE_ENABLED) printf("Server successfully started\n");
|
if(VERBOSE_MODE_ENABLED) printf("Server successfully started\n");
|
||||||
|
|
||||||
ftpTrackSocket(server); // add socket to "watchlist"
|
ftpTrackSocket(server); // add socket to "watchlist"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("Creating server PASSIVE socket");
|
||||||
|
|
||||||
|
serverPassivePort = ftpCreateServerSocket(serverPassiveListenPort); // create main listener socket
|
||||||
|
if(serverPassivePort < 0)
|
||||||
|
{
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\t\tpassive port error\n");
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\t\tok\n");
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("Server passive port successfully started\n");
|
||||||
|
*/
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,6 +237,7 @@ int ftpShutdown(void)
|
|||||||
int n;
|
int n;
|
||||||
ftpUntrackSocket(server);
|
ftpUntrackSocket(server);
|
||||||
ftpCloseSocket(server);
|
ftpCloseSocket(server);
|
||||||
|
//ftpCloseSocket(serverPassivePort);
|
||||||
|
|
||||||
for(n = 0; n < MAX_CONNECTIONS; n++)
|
for(n = 0; n < MAX_CONNECTIONS; n++)
|
||||||
{
|
{
|
||||||
|
@@ -128,16 +128,21 @@ if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP =
|
|||||||
{
|
{
|
||||||
if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort);
|
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);
|
//ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort);
|
||||||
sessions[id].remoteFTPServerPassivePort = 0;
|
sessions[id].remoteFTPServerPassivePort = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(sessions[id].open) {
|
if(sessions[id].open) {
|
||||||
ftpCloseSocket(sessions[id].ctrlSocket);
|
ftpCloseSocket(sessions[id].ctrlSocket);
|
||||||
ftpCloseTransmission(id);
|
ftpCloseTransmission(id);
|
||||||
|
if(sessions[id].passiveDataSocket > 0)
|
||||||
|
{
|
||||||
|
ftpCloseSocket(sessions[id].passiveDataSocket);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sessions[id].remoteIp = 0;
|
sessions[id].remoteIp = 0;
|
||||||
sessions[id].ctrlSocket = 0;
|
sessions[id].ctrlSocket = 0;
|
||||||
|
sessions[id].passiveDataSocket = 0;
|
||||||
sessions[id].open = FALSE;
|
sessions[id].open = FALSE;
|
||||||
|
|
||||||
if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
|
if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
|
||||||
|
@@ -179,6 +179,7 @@ int ftpRemoveDir(const char* path)
|
|||||||
|
|
||||||
int ftpCloseSocket(socket_t s)
|
int ftpCloseSocket(socket_t s)
|
||||||
{
|
{
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\nClosing socket: %d\n",s);
|
||||||
return close(s);
|
return close(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,7 +208,7 @@ int ftpReceive(socket_t s, void *data, int len)
|
|||||||
return recv(s, data, len, 0);
|
return recv(s, data, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId)
|
||||||
{
|
{
|
||||||
socket_t dataSocket;
|
socket_t dataSocket;
|
||||||
struct sockaddr_in clientAddr;
|
struct sockaddr_in clientAddr;
|
||||||
@@ -244,14 +245,27 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
int passivePort = ftpGetPassivePort() + sessionId;
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d\n",sessionId,passivePort);
|
||||||
|
|
||||||
myAddr.sin_family = AF_INET;
|
myAddr.sin_family = AF_INET;
|
||||||
myAddr.sin_addr.s_addr = INADDR_ANY;
|
myAddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
myAddr.sin_port = htons(0);
|
myAddr.sin_port = htons(passivePort);
|
||||||
|
//myAddr.sin_port = htons(ftpGetPassivePort() + sessionId);
|
||||||
|
|
||||||
|
int val = 1;
|
||||||
|
setsockopt(dataSocket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
||||||
|
|
||||||
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
|
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
|
||||||
{
|
{
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d FAILED: %d\n",sessionId,passivePort,dataSocket);
|
||||||
|
|
||||||
close(dataSocket);
|
close(dataSocket);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d bound ok\n",sessionId,passivePort);
|
||||||
|
|
||||||
len = sizeof(myAddr);
|
len = sizeof(myAddr);
|
||||||
if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
|
if(getsockname(dataSocket, (struct sockaddr *)&myAddr, &len)) // Port des Server-Sockets ermitteln
|
||||||
{
|
{
|
||||||
@@ -262,11 +276,19 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
|||||||
*port = ntohs(myAddr.sin_port);
|
*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(listen(dataSocket, 1))
|
if(listen(dataSocket, 1))
|
||||||
{
|
{
|
||||||
|
if(VERBOSE_MODE_ENABLED) printf("\nPASSIVE CONNECTION for sessionId = %d using port #: %d FAILED #2: %d\n",sessionId,passivePort,dataSocket);
|
||||||
|
|
||||||
close(dataSocket);
|
close(dataSocket);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//*port = ftpGetPassivePort();
|
||||||
|
//*ip = ownIp;
|
||||||
|
//dataSocket = ftpGetServerPassivePortListenSocket();
|
||||||
}
|
}
|
||||||
return dataSocket;
|
return dataSocket;
|
||||||
}
|
}
|
||||||
@@ -292,6 +314,7 @@ socket_t ftpCreateServerSocket(int portNumber)
|
|||||||
int theServer;
|
int theServer;
|
||||||
struct sockaddr_in serverinfo;
|
struct sockaddr_in serverinfo;
|
||||||
unsigned len;
|
unsigned len;
|
||||||
|
int val = 1;
|
||||||
|
|
||||||
theServer = socket(AF_INET, SOCK_STREAM, 0);
|
theServer = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if(theServer < 0)
|
if(theServer < 0)
|
||||||
@@ -302,13 +325,15 @@ socket_t ftpCreateServerSocket(int portNumber)
|
|||||||
serverinfo.sin_port = htons(portNumber);
|
serverinfo.sin_port = htons(portNumber);
|
||||||
len = sizeof(serverinfo);
|
len = sizeof(serverinfo);
|
||||||
|
|
||||||
|
setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
||||||
|
|
||||||
if(bind(theServer, (struct sockaddr *)&serverinfo, len))
|
if(bind(theServer, (struct sockaddr *)&serverinfo, len))
|
||||||
{
|
{
|
||||||
ftpCloseSocket(theServer);
|
ftpCloseSocket(theServer);
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(listen(theServer, 3))
|
if(listen(theServer, 16))
|
||||||
{
|
{
|
||||||
ftpCloseSocket(theServer);
|
ftpCloseSocket(theServer);
|
||||||
return -3;
|
return -3;
|
||||||
|
@@ -237,7 +237,7 @@ int ftpReceive(socket_t s, void *data, int len)
|
|||||||
return recv(s, data, len, 0);
|
return recv(s, data, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int sessionId)
|
||||||
{
|
{
|
||||||
SOCKET dataSocket;
|
SOCKET dataSocket;
|
||||||
struct sockaddr_in clientAddr;
|
struct sockaddr_in clientAddr;
|
||||||
@@ -258,6 +258,7 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
|||||||
myAddr.sin_family = AF_INET;
|
myAddr.sin_family = AF_INET;
|
||||||
myAddr.sin_addr.s_addr = INADDR_ANY;
|
myAddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
myAddr.sin_port = htons(20);
|
myAddr.sin_port = htons(20);
|
||||||
|
|
||||||
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
|
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
|
||||||
{
|
{
|
||||||
closesocket(dataSocket);
|
closesocket(dataSocket);
|
||||||
@@ -276,7 +277,12 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
|||||||
{
|
{
|
||||||
myAddr.sin_family = AF_INET;
|
myAddr.sin_family = AF_INET;
|
||||||
myAddr.sin_addr.s_addr = INADDR_ANY;
|
myAddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
myAddr.sin_port = htons(0);
|
//myAddr.sin_port = htons(0);
|
||||||
|
myAddr.sin_port = htons(ftpGetPassivePort() + sessionId);
|
||||||
|
|
||||||
|
int val = 1;
|
||||||
|
setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
|
||||||
|
|
||||||
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
|
if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr)))
|
||||||
{
|
{
|
||||||
closesocket(dataSocket);
|
closesocket(dataSocket);
|
||||||
@@ -297,6 +303,10 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port)
|
|||||||
closesocket(dataSocket);
|
closesocket(dataSocket);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//*port = ftpGetPassivePort();
|
||||||
|
//*ip = ownIp;
|
||||||
|
//dataSocket = ftpGetServerPassivePortListenSocket();
|
||||||
}
|
}
|
||||||
return (socket_t)dataSocket;
|
return (socket_t)dataSocket;
|
||||||
}
|
}
|
||||||
@@ -333,11 +343,7 @@ socket_t ftpCreateServerSocket(int portNumber)
|
|||||||
serverinfo.sin_port = htons(portNumber);
|
serverinfo.sin_port = htons(portNumber);
|
||||||
len = sizeof(serverinfo);
|
len = sizeof(serverinfo);
|
||||||
|
|
||||||
#ifndef WIN32
|
|
||||||
setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
|
||||||
#else
|
|
||||||
setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
|
setsockopt(theServer, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
|
||||||
#endif
|
|
||||||
|
|
||||||
if(bind(theServer, (struct sockaddr *)&serverinfo, len))
|
if(bind(theServer, (struct sockaddr *)&serverinfo, len))
|
||||||
{
|
{
|
||||||
@@ -345,7 +351,7 @@ socket_t ftpCreateServerSocket(int portNumber)
|
|||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(listen(theServer, 3))
|
if(listen(theServer, 16))
|
||||||
{
|
{
|
||||||
ftpCloseSocket(theServer);
|
ftpCloseSocket(theServer);
|
||||||
return -3;
|
return -3;
|
||||||
|
@@ -36,17 +36,22 @@ ip_t FindExternalFTPServerIp(ip_t clientIp) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FTPServerThread::FTPServerThread(std::pair<string,string> mapsPath,std::pair<string,string> tilesetsPath, int portNumber) : BaseThread() {
|
FTPServerThread::FTPServerThread(std::pair<string,string> mapsPath,std::pair<string,string> tilesetsPath, int portNumber, int maxPlayers) : BaseThread() {
|
||||||
this->mapsPath = mapsPath;
|
this->mapsPath = mapsPath;
|
||||||
this->tilesetsPath = tilesetsPath;
|
this->tilesetsPath = tilesetsPath;
|
||||||
this->portNumber = portNumber;
|
this->portNumber = portNumber;
|
||||||
|
this->maxPlayers = maxPlayers;
|
||||||
|
|
||||||
ftpInit(&FindExternalFTPServerIp,&UPNP_Tools::AddUPNPPortForward,&UPNP_Tools::RemoveUPNPPortForward);
|
ftpInit(&FindExternalFTPServerIp,&UPNP_Tools::AddUPNPPortForward,&UPNP_Tools::RemoveUPNPPortForward);
|
||||||
VERBOSE_MODE_ENABLED = SystemFlags::VERBOSE_MODE_ENABLED;
|
VERBOSE_MODE_ENABLED = SystemFlags::VERBOSE_MODE_ENABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
FTPServerThread::~FTPServerThread() {
|
FTPServerThread::~FTPServerThread() {
|
||||||
|
// Remove any UPNP port forwarded ports
|
||||||
UPNP_Tools::upnp_rem_redirect(ServerSocket::getFTPServerPort());
|
UPNP_Tools::upnp_rem_redirect(ServerSocket::getFTPServerPort());
|
||||||
|
for(int clientIndex = 1; clientIndex <= maxPlayers; ++clientIndex) {
|
||||||
|
UPNP_Tools::upnp_rem_redirect(ServerSocket::getFTPServerPort() + clientIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FTPServerThread::signalQuit() {
|
void FTPServerThread::signalQuit() {
|
||||||
|
@@ -63,6 +63,7 @@ namespace Shared{ namespace Platform{
|
|||||||
|
|
||||||
int Socket::broadcast_portno = 61357;
|
int Socket::broadcast_portno = 61357;
|
||||||
int ServerSocket::ftpServerPort = 61358;
|
int ServerSocket::ftpServerPort = 61358;
|
||||||
|
int ServerSocket::maxPlayerCount = -1;
|
||||||
int ServerSocket::externalPort = Socket::broadcast_portno;
|
int ServerSocket::externalPort = Socket::broadcast_portno;
|
||||||
BroadCastClientSocketThread *ClientSocket::broadCastClientThread = NULL;
|
BroadCastClientSocketThread *ClientSocket::broadCastClientThread = NULL;
|
||||||
|
|
||||||
@@ -1875,8 +1876,24 @@ void ServerSocket::UPNPInitStatus(bool result) {
|
|||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] result = %d\n",__FILE__,__FUNCTION__,__LINE__,result);
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] result = %d\n",__FILE__,__FUNCTION__,__LINE__,result);
|
||||||
|
|
||||||
if(result == true) {
|
if(result == true) {
|
||||||
int ports[4] = { this->getExternalPort(), this->getBindPort(), this->getFTPServerPort(), this->getFTPServerPort() };
|
//int ports[4] = { this->getExternalPort(), this->getBindPort(), this->getFTPServerPort(), this->getFTPServerPort() };
|
||||||
UPNP_Tools::NETaddRedirects(ports);
|
std::vector<int> UPNPPortForwardList;
|
||||||
|
|
||||||
|
// Glest Game Server port
|
||||||
|
UPNPPortForwardList.push_back(this->getExternalPort());
|
||||||
|
UPNPPortForwardList.push_back(this->getBindPort());
|
||||||
|
|
||||||
|
// Glest mini FTP Server Listen Port
|
||||||
|
UPNPPortForwardList.push_back(this->getFTPServerPort());
|
||||||
|
UPNPPortForwardList.push_back(this->getFTPServerPort());
|
||||||
|
|
||||||
|
// GLest mini FTP Server Passive file TRansfer ports (1 per game player)
|
||||||
|
for(int clientIndex = 1; clientIndex <= ServerSocket::maxPlayerCount; ++clientIndex) {
|
||||||
|
UPNPPortForwardList.push_back(this->getFTPServerPort() + clientIndex);
|
||||||
|
UPNPPortForwardList.push_back(this->getFTPServerPort() + clientIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
UPNP_Tools::NETaddRedirects(UPNPPortForwardList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2023,13 +2040,25 @@ void UPNP_Tools::upnp_rem_redirect(int ext_port) {
|
|||||||
UPNP_DeletePortMapping(urls.controlURL, data.servicetype, ext_port_str, "TCP", 0);
|
UPNP_DeletePortMapping(urls.controlURL, data.servicetype, ext_port_str, "TCP", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPNP_Tools::NETaddRedirects(int ports[4]) {
|
void UPNP_Tools::NETaddRedirects(std::vector<int> UPNPPortForwardList) {
|
||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] upnp_rem_redir(%d)\n",__FILE__,__FUNCTION__,__LINE__);
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] upnp_rem_redir(%d)\n",__FILE__,__FUNCTION__,__LINE__);
|
||||||
|
|
||||||
|
if(UPNPPortForwardList.size() % 2 != 0) {
|
||||||
|
// We need groups of 2 ports.. one external and one internal for opening ports on UPNP router
|
||||||
|
throw runtime_error("UPNPPortForwardList.size() MUST BE divisable by 2");
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int clientIndex = 0; clientIndex < UPNPPortForwardList.size(); clientIndex += 2) {
|
||||||
|
int ports[2] = { UPNPPortForwardList[clientIndex], UPNPPortForwardList[clientIndex+1] };
|
||||||
|
upnp_add_redirect(ports);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
int portsA[2] = { ports[0], ports[1] };
|
int portsA[2] = { ports[0], ports[1] };
|
||||||
upnp_add_redirect(portsA);
|
upnp_add_redirect(portsA);
|
||||||
int portsB[2] = { ports[2], ports[3] };
|
int portsB[2] = { ports[2], ports[3] };
|
||||||
upnp_add_redirect(portsB);
|
upnp_add_redirect(portsB);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void UPNP_Tools::NETremRedirects(int ext_port) {
|
void UPNP_Tools::NETremRedirects(int ext_port) {
|
||||||
|
Reference in New Issue
Block a user