diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 5f6ee1108..4da364d14 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -1394,12 +1394,11 @@ void Game::ReplaceDisconnectedNetworkPlayersWithAI(bool isNetworkGame, NetworkRo server->isClientConnected(faction->getStartLocationIndex()) == false) { faction->setFactionDisconnectHandled(true); - char szBuf[255]=""; + char szBuf[1024]=""; if(faction->getType()->getPersonalityType() != fpt_Observer) { faction->setControlType(ctCpu); aiInterfaces[i] = new AiInterface(*this, i, faction->getTeam(), faction->getStartLocationIndex()); - char szBuf[1024]=""; sprintf(szBuf,Lang::getInstance().get("LogScreenGameLoadingCreatingAIFaction","",true).c_str(),i); logger.add(szBuf, true); diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 0806d4568..68f981a52 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -218,6 +218,56 @@ int ServerInterface::isValidClientType(uint32 clientIp) { return result; } +int ServerInterface::isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename) { + int result = 1; + + if(username != NULL && strlen(username) > 0 && filename != NULL && strlen(filename) > 0) { + string user = username; + string file = filename; + + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d username [%s] file [%s]\n",__FILE__,__FUNCTION__,__LINE__,username,filename); + + if(StartsWith(user,"tilesets") == true && EndsWith(file,"7z") == false) { + if(Config::getInstance().getBool("DisableFTPServerXferUncompressedTilesets","false") == true) { + result = 0; + } + else { + char szIP[100]=""; + Ip::Inet_NtoA(clientIp,szIP); + string clientIP = szIP; + //string serverIP = serverSocket.getIp(); + std::vector serverList = Socket::getLocalIPAddressList(); + + result = 0; + for(unsigned int i = 0; i < serverList.size(); ++i) { + string serverIP = serverList[i]; + + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d clientIP [%s] serverIP [%s] %d / %d\n",__FILE__,__FUNCTION__,__LINE__,clientIP.c_str(),serverIP.c_str(),i,serverList.size()); + + vector clientTokens; + Tokenize(clientIP,clientTokens,"."); + + vector serverTokens; + Tokenize(serverIP,serverTokens,"."); + + if(clientTokens.size() == 4 && serverTokens.size() == 4) { + if( clientTokens[0] == serverTokens[0] || + clientTokens[1] == serverTokens[1] || + clientTokens[2] == serverTokens[2]) { + result = 1; + + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d clientIP [%s] IS NOT BLOCKED\n",__FILE__,__FUNCTION__,__LINE__,clientIP.c_str()); + + break; + } + } + } + } + } + } + return result; +} + void ServerInterface::addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp) { FTPServerThread::addClientToServerIPAddress(clientIp, ServerIp); } @@ -340,6 +390,8 @@ void ServerInterface::removeSlot(int playerIndex, int lockedSlotIndex) { for(unsigned int j = 0; j < languageList.size(); ++j) { bool localEcho = lang.isLanguageLocal(languageList[j]); queueTextMessage(msgList[j],-1, localEcho, languageList[j]); + + //printf("j = %d [%s] localEcho = %d [%s]\n",j,msgList[j].c_str(),localEcho,languageList[j].c_str()); } } if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); @@ -1461,6 +1513,9 @@ void ServerInterface::processTextMessageQueue() { void ServerInterface::queueTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage) { + + //printf("Line: %d text [%s]\n",__LINE__,text.c_str()); + MutexSafeWrapper safeMutexSlot(&textMessageQueueThreadAccessor,CODE_AT_LINE); TextMessageQueue item; item.text = text; @@ -1477,6 +1532,9 @@ void ServerInterface::sendTextMessage(const string & text, int teamIndex, void ServerInterface::sendTextMessage(const string& text, int teamIndex, bool echoLocal, string targetLanguage, int lockedSlotIndex) { + //printf("Line: %d text [%s] echoLocal = %d\n",__LINE__,text.c_str(),echoLocal); + //assert(text.length() > 0); + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] text [%s] teamIndex = %d, echoLocal = %d, lockedSlotIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,text.c_str(),teamIndex,echoLocal,lockedSlotIndex); NetworkMessageText networkMessageText(text, teamIndex, getHumanPlayerIndex(), targetLanguage); broadcastMessage(&networkMessageText, -1, lockedSlotIndex); diff --git a/source/glest_game/network/server_interface.h b/source/glest_game/network/server_interface.h index b79a2924a..8bfb9ea31 100644 --- a/source/glest_game/network/server_interface.h +++ b/source/glest_game/network/server_interface.h @@ -190,6 +190,8 @@ public: virtual void simpleTask(BaseThread *callingThread); void addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp); virtual int isValidClientType(uint32 clientIp); + virtual int isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename); + void notifyBadClientConnectAttempt(string ipAddress); private: diff --git a/source/shared_lib/include/feathery_ftp/ftp.h b/source/shared_lib/include/feathery_ftp/ftp.h index 081758139..6d26d33dd 100644 --- a/source/shared_lib/include/feathery_ftp/ftp.h +++ b/source/shared_lib/include/feathery_ftp/ftp.h @@ -139,6 +139,8 @@ extern void ftpCloseTransmission(int id); extern int ftpGetActiveTransCnt(void); extern int ftpFindAccount(const char* name); +extern const char * ftpFindAccountById(int userid); + extern int ftpCheckPassword(int userId, const char* passw); extern int ftpCheckAccRights(int userId, int accRights); extern const char* ftpGetRoot(int userId, int* len); diff --git a/source/shared_lib/include/feathery_ftp/ftpIfc.h b/source/shared_lib/include/feathery_ftp/ftpIfc.h index cf245e5b8..fd9dce47c 100644 --- a/source/shared_lib/include/feathery_ftp/ftpIfc.h +++ b/source/shared_lib/include/feathery_ftp/ftpIfc.h @@ -38,7 +38,9 @@ extern "C" { #endif -void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4); +void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, + ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4, + ftpIsClientAllowedToGetFileType cb5); int ftpCreateAccount(const char* name, const char* passw, const char* root, int accRights); int ftpDeleteAccount(const char* name); int ftpStart(int portNumber); diff --git a/source/shared_lib/include/feathery_ftp/ftpTypes.h b/source/shared_lib/include/feathery_ftp/ftpTypes.h index 9f7bdd925..4fea7d2cf 100644 --- a/source/shared_lib/include/feathery_ftp/ftpTypes.h +++ b/source/shared_lib/include/feathery_ftp/ftpTypes.h @@ -70,11 +70,13 @@ typedef ip_t (*ftpFindExternalFTPServerIpType)(ip_t clientIp); typedef void (*ftpAddUPNPPortForwardType)(int internalPort, int externalPort); typedef void (*ftpRemoveUPNPPortForwardType)(int internalPort, int externalPort); typedef int (*ftpIsValidClientType)(ip_t clientIp); +typedef int (*ftpIsClientAllowedToGetFileType)(ip_t clientIp, const char *username, const char *filename); ftpFindExternalFTPServerIpType ftpFindExternalFTPServerIp; ftpAddUPNPPortForwardType ftpAddUPNPPortForward; ftpRemoveUPNPPortForwardType ftpRemoveUPNPPortForward; ftpIsValidClientType ftpIsValidClient; +ftpIsClientAllowedToGetFileType ftpIsClientAllowedToGetFile; #ifdef __cplusplus } diff --git a/source/shared_lib/include/platform/posix/socket.h b/source/shared_lib/include/platform/posix/socket.h index 8bf406a95..8e164ac5f 100644 --- a/source/shared_lib/include/platform/posix/socket.h +++ b/source/shared_lib/include/platform/posix/socket.h @@ -54,6 +54,7 @@ namespace Shared { namespace Platform { class FTPClientValidationInterface { public: virtual int isValidClientType(uint32 clientIp) = 0; + virtual int isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename) = 0; }; @@ -83,6 +84,7 @@ public: Ip(); Ip(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned char byte3); Ip(const string& ipString); + static void Inet_NtoA(uint32 addr, char * ipbuf); unsigned char getByte(int byteIndex) {return bytes[byteIndex];} string getString() const; diff --git a/source/shared_lib/sources/feathery_ftp/ftpAccount.c b/source/shared_lib/sources/feathery_ftp/ftpAccount.c index 2bf59bd11..c388e6eba 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpAccount.c +++ b/source/shared_lib/sources/feathery_ftp/ftpAccount.c @@ -129,6 +129,18 @@ int ftpFindAccount(const char* name) return 0; } +const char * ftpFindAccountById(int userid) +{ + if(userid == 0) { + return 0; + } + else if(ftpUsers[userid - 1].name[0] == '\0') { + return 0; + } + + return ftpUsers[userid - 1].name; +} + /** * @brief Checks the password of a user account * @@ -143,12 +155,15 @@ int ftpFindAccount(const char* name) */ int ftpCheckPassword(int userId, const char* passw) { - if(!userId) + 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); + } } /** diff --git a/source/shared_lib/sources/feathery_ftp/ftpCmds.c b/source/shared_lib/sources/feathery_ftp/ftpCmds.c index 50dff8853..e66377791 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpCmds.c +++ b/source/shared_lib/sources/feathery_ftp/ftpCmds.c @@ -524,6 +524,16 @@ LOCAL int ftpCmdRetr(int sessionId, const char* args, int len) return 2; } + if(ftpIsClientAllowedToGetFile != NULL) { + if(ftpIsClientAllowedToGetFile(ftpGetSession(sessionId)->remoteIp,ftpFindAccountById(ftpGetSession(sessionId)->userId),realPath) != 1) { + if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpCmdRetr FILE DISALLOWED By MGserver [file not available] args [%s] realPath [%s]\n", args, realPath); + + ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg032); + return 2; + } + } + + if(ftpGetSession(sessionId)->passive == FALSE) { s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId); diff --git a/source/shared_lib/sources/feathery_ftp/ftpRuntime.c b/source/shared_lib/sources/feathery_ftp/ftpRuntime.c index ee98208d1..5c1945ade 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpRuntime.c +++ b/source/shared_lib/sources/feathery_ftp/ftpRuntime.c @@ -39,11 +39,14 @@ LOCAL int serverListenPort; LOCAL int serverPassiveListenPort; //LOCAL socket_t serverPassivePort; -void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4) { +void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, + ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4, + ftpIsClientAllowedToGetFileType cb5) { ftpFindExternalFTPServerIp = cb1; ftpAddUPNPPortForward = cb2; ftpRemoveUPNPPortForward = cb3; ftpIsValidClient = cb4; + ftpIsClientAllowedToGetFile = cb5; } int ftpGetListenPort() diff --git a/source/shared_lib/sources/feathery_ftp/ftpSession.c b/source/shared_lib/sources/feathery_ftp/ftpSession.c index 257d9c4fe..75cab7e03 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpSession.c +++ b/source/shared_lib/sources/feathery_ftp/ftpSession.c @@ -254,12 +254,17 @@ LOCAL int normalizePath(char* path) */ const char* ftpGetRealPath(int id, const char* path, int normalize) { - const char* ftpRoot; + char ftpRoot[2048]=""; + int ftpRootLen; int len; - ftpRoot = ftpGetRoot(sessions[id].userId, &len); + strcpy(ftpRoot,ftpGetRoot(sessions[id].userId, &len)); + ftpRootLen = 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? diff --git a/source/shared_lib/sources/platform/posix/miniftpserver.cpp b/source/shared_lib/sources/platform/posix/miniftpserver.cpp index 8da56e881..ece83e598 100644 --- a/source/shared_lib/sources/platform/posix/miniftpserver.cpp +++ b/source/shared_lib/sources/platform/posix/miniftpserver.cpp @@ -46,6 +46,17 @@ int isValidClientType(ip_t clientIp) { return result; } +int isClientAllowedToGetFile(ip_t clientIp, const char *username, const char *filename) { + int result = 1; + + //printf("In [%s::%s] Line: %d username [%s] file [%s]\n",__FILE__,__FUNCTION__,__LINE__,username,filename); + if(FTPServerThread::getFtpValidationIntf() != NULL) { + result = FTPServerThread::getFtpValidationIntf()->isClientAllowedToGetFile(clientIp,username,filename); + //printf("In [%s::%s] Line: %d username [%s] file [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,username,filename,result); + } + return result; +} + FTPServerThread::FTPServerThread(std::pair mapsPath, std::pair tilesetsPath, std::pair techtreesPath, bool internetEnabledFlag, @@ -62,7 +73,8 @@ FTPServerThread::FTPServerThread(std::pair mapsPath, this->maxPlayers = maxPlayers; this->ftpValidationIntf = ftpValidationIntf; - ftpInit(&FindExternalFTPServerIp,&UPNP_Tools::AddUPNPPortForward,&UPNP_Tools::RemoveUPNPPortForward, &isValidClientType); + ftpInit(&FindExternalFTPServerIp,&UPNP_Tools::AddUPNPPortForward,&UPNP_Tools::RemoveUPNPPortForward, + &isValidClientType, &isClientAllowedToGetFile); VERBOSE_MODE_ENABLED = SystemFlags::VERBOSE_MODE_ENABLED; if(SystemFlags::VERBOSE_MODE_ENABLED) printf("***FTP SERVER STARTED [%p]\n",this); diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index ec2f92c0e..a80030ae5 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -342,7 +342,7 @@ static uint32 SockAddrToUint32(struct sockaddr * a) { } // convert a numeric IP address into its string representation -static void Inet_NtoA(uint32 addr, char * ipbuf) +void Ip::Inet_NtoA(uint32 addr, char * ipbuf) { sprintf(ipbuf, "%d.%d.%d.%d", (addr>>24)&0xFF, (addr>>16)&0xFF, (addr>>8)&0xFF, (addr>>0)&0xFF); } @@ -384,9 +384,9 @@ static void PrintNetworkInterfaceInfos() uint32 dstAddr = SockAddrToUint32(p->ifa_dstaddr); if (ifaAddr > 0) { - char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr); - char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr); - char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr); + char ifaAddrStr[32]; Ip::Inet_NtoA(ifaAddr, ifaAddrStr); + char maskAddrStr[32]; Ip::Inet_NtoA(maskAddr, maskAddrStr); + char dstAddrStr[32]; Ip::Inet_NtoA(dstAddr, dstAddrStr); printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr); } p = p->ifa_next; @@ -485,9 +485,9 @@ static void PrintNetworkInterfaceInfos() uint32 baddr = ipAddr & netmask; if (row.dwBCastAddr) baddr |= ~netmask; - char ifaAddrStr[32]; Inet_NtoA(ipAddr, ifaAddrStr); - char maskAddrStr[32]; Inet_NtoA(netmask, maskAddrStr); - char dstAddrStr[32]; Inet_NtoA(baddr, dstAddrStr); + char ifaAddrStr[32]; Ip::Inet_NtoA(ipAddr, ifaAddrStr); + char maskAddrStr[32]; Ip::Inet_NtoA(netmask, maskAddrStr); + char dstAddrStr[32]; Ip::Inet_NtoA(baddr, dstAddrStr); printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr); } @@ -522,9 +522,9 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr); - char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr); - char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr); + char ifaAddrStr[32]; Ip::Inet_NtoA(ifaAddr, ifaAddrStr); + char maskAddrStr[32]; Ip::Inet_NtoA(maskAddr, maskAddrStr); + char dstAddrStr[32]; Ip::Inet_NtoA(dstAddr, dstAddrStr); //printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr); if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) { broadCastAddress = dstAddrStr; @@ -628,9 +628,9 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress) uint32 baddr = ipAddr & netmask; if (row.dwBCastAddr) baddr |= ~netmask; - char ifaAddrStr[32]; Inet_NtoA(ipAddr, ifaAddrStr); - char maskAddrStr[32]; Inet_NtoA(netmask, maskAddrStr); - char dstAddrStr[32]; Inet_NtoA(baddr, dstAddrStr); + char ifaAddrStr[32]; Ip::Inet_NtoA(ipAddr, ifaAddrStr); + char maskAddrStr[32]; Ip::Inet_NtoA(netmask, maskAddrStr); + char dstAddrStr[32]; Ip::Inet_NtoA(baddr, dstAddrStr); //printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr); if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) { broadCastAddress = dstAddrStr; @@ -682,7 +682,7 @@ std::vector Socket::getLocalIPAddressList() { //memcpy(&(SockAddr.sin_addr),&myhostent->h_addr[ipIdx],myhostent->h_length); //SockAddr.sin_family = myhostent->h_addrtype; //Inet_NtoA(SockAddrToUint32((sockaddr *)&SockAddr), myhostaddr); - Inet_NtoA(SockAddrToUint32((struct in_addr *)myhostent->h_addr_list[ipIdx]), myhostaddr); + Ip::Inet_NtoA(SockAddrToUint32((struct in_addr *)myhostent->h_addr_list[ipIdx]), myhostaddr); //printf("ipIdx = %d [%s]\n",ipIdx,myhostaddr); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr); @@ -738,7 +738,7 @@ std::vector Socket::getLocalIPAddressList() { struct sockaddr_in *pSockAddr = (struct sockaddr_in *)&ifr.ifr_addr; if(pSockAddr != NULL) { //sprintf(myhostaddr, "%s",inet_ntoa(pSockAddr->sin_addr)); - Inet_NtoA(SockAddrToUint32(&pSockAddr->sin_addr), myhostaddr); + Ip::Inet_NtoA(SockAddrToUint32(&pSockAddr->sin_addr), myhostaddr); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] szBuf [%s], myhostaddr = [%s], ifr.ifr_flags = %d, ifrA.ifr_flags = %d, ifr.ifr_name [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf,myhostaddr,ifr.ifr_flags,ifrA.ifr_flags,ifr.ifr_name); // Now only include interfaces that are both UP and running @@ -1855,8 +1855,8 @@ void BroadCastClientSocketThread::execute() { struct sockaddr_in bcaddr; // The broadcast address for the receiver. PLATFORM_SOCKET bcfd; // The file descriptor used for the broadcast. //bool one = true; // Parameter for "setscokopt". - char buff[10024]; // Buffers the data to be broadcasted. - socklen_t alen; + char buff[10024]=""; // Buffers the data to be broadcasted. + socklen_t alen=0; port = htons( Socket::getBroadCastPort() ); @@ -1892,16 +1892,18 @@ void BroadCastClientSocketThread::execute() { // Keep getting packets forever. for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 5; ) { alen = sizeof(struct sockaddr); - int nb=0; // The number of bytes read. + int nb=0;// The number of bytes read. bool gotData = (nb = recvfrom(bcfd, buff, 10024, 0, (struct sockaddr *) &bcSender, &alen)) > 0; + //printf("Broadcasting client nb = %d buff [%s] gotData = %d\n",nb,buff,gotData); + if(gotData == false) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"recvfrom failed: %s\n", getLastSocketErrorFormattedText().c_str()); } else { //string fromIP = inet_ntoa(bcSender.sin_addr); char szHostFrom[100]=""; - Inet_NtoA(SockAddrToUint32(&bcSender.sin_addr), szHostFrom); + Ip::Inet_NtoA(SockAddrToUint32(&bcSender.sin_addr), szHostFrom); //printf("Client szHostFrom [%s]\n",szHostFrom); string fromIP = szHostFrom; @@ -2190,7 +2192,7 @@ Socket *ServerSocket::accept() { } else { - Inet_NtoA(SockAddrToUint32((struct sockaddr *)&cli_addr), client_host); + Ip::Inet_NtoA(SockAddrToUint32((struct sockaddr *)&cli_addr), client_host); //printf("client_host [%s]\n",client_host); //sprintf(client_host, "%s",inet_ntoa(cli_addr.sin_addr)); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got connection, newSock = %d client_host [%s]\n",__FILE__,__FUNCTION__,__LINE__,newSock,client_host); @@ -2547,7 +2549,7 @@ void UPNP_Tools::NETremRedirects(int ext_port) { // BroadCastSocketThread::BroadCastSocketThread() : BaseThread() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - + setPauseBroadcast(false); //printf("new broadcast thread [%p]\n",this); } @@ -2666,16 +2668,21 @@ void BroadCastSocketThread::execute() { if(difftime(time(NULL),elapsed) >= 1 && getQuitStatus() == false) { elapsed = time(NULL); - if(getPauseBroadcast() == false) { + ssize_t send_res = 0; + bool pauseBroadCast = getPauseBroadcast(); + if(pauseBroadCast == false) { // Broadcast the packet to the subnet //if( sendto( bcfd, buff, sizeof(buff) + 1, 0 , (struct sockaddr *)&bcaddr, sizeof(struct sockaddr_in) ) != sizeof(buff) + 1 ) - if( sendto( bcfd[idx], buff, buffMaxSize, 0 , (struct sockaddr *)&bcLocal[idx], sizeof(struct sockaddr_in) ) != buffMaxSize ) { + send_res = sendto( bcfd[idx], buff, buffMaxSize, 0 , (struct sockaddr *)&bcLocal[idx], sizeof(struct sockaddr_in) ); + if( send_res != buffMaxSize ) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Sendto error: %s\n", getLastSocketErrorFormattedText().c_str()); } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcasting on port [%d] the message: [%s]\n",Socket::getBroadCastPort(),buff); } } + //printf("Broadcasting server send_res = %d buff [%s] ip [%s] getPauseBroadcast() = %d\n",send_res,buff,ipSubnetMaskList[idx].c_str(),pauseBroadCast); + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); }