void CNetBan::ConUnbanRange(IConsole::IResult *pResult, void *pUser) { CNetBan *pThis = static_cast<CNetBan *>(pUser); const char *pStr1 = pResult->GetString(0); const char *pStr2 = pResult->GetString(1); CNetRange Range; if(net_addr_from_str(&Range.m_LB, pStr1) == 0 && net_addr_from_str(&Range.m_UB, pStr2) == 0) pThis->UnbanByRange(&Range); else pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "unban error (invalid range)"); }
void CNetBan::ConBanRange(IConsole::IResult *pResult, void *pUser) { CNetBan *pThis = static_cast<CNetBan *>(pUser); const char *pStr1 = pResult->GetString(0); const char *pStr2 = pResult->GetString(1); int Minutes = pResult->NumArguments()>2 ? clamp(pResult->GetInteger(2), 0, 44640) : 30; const char *pReason = pResult->NumArguments()>3 ? pResult->GetString(3) : "No reason given"; CNetRange Range; if(net_addr_from_str(&Range.m_LB, pStr1) == 0 && net_addr_from_str(&Range.m_UB, pStr2) == 0) pThis->BanRange(&Range, Minutes*60, pReason); else pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (invalid range)"); }
// mute through ip, arguments reversed to workaround parsing void CGameContext::ConMuteIP(IConsole::IResult *pResult, void *pUserData) { CGameContext *pSelf = (CGameContext *) pUserData; NETADDR Addr; if (net_addr_from_str(&Addr, pResult->GetString(0))) { pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "mutes", "Invalid network address to mute"); } pSelf->Mute(pResult, &Addr, clamp(pResult->GetInteger(1), 1, 86400), pResult->GetString(0)); }
virtual int Load() { if(!m_pStorage) return -1; // try to open file IOHANDLE File = m_pStorage->OpenFile("masters.cfg", IOFLAG_READ, IStorage::TYPE_SAVE); if(!File) return -1; CLineReader LineReader; LineReader.Init(File); while(1) { CMasterInfo Info = {{0}}; const char *pLine = LineReader.Get(); if(!pLine) break; // parse line char aAddrStr[NETADDR_MAXSTRSIZE]; if(sscanf(pLine, "%127s %47s", Info.m_aHostname, aAddrStr) == 2 && net_addr_from_str(&Info.m_Addr, aAddrStr) == 0) { Info.m_Addr.port = 8300; bool Added = false; for(int i = 0; i < MAX_MASTERSERVERS; ++i) if(str_comp(m_aMasterServers[i].m_aHostname, Info.m_aHostname) == 0) { m_aMasterServers[i] = Info; Added = true; break; } if(!Added) { for(int i = 0; i < MAX_MASTERSERVERS; ++i) if(m_aMasterServers[i].m_Addr.type == NETTYPE_INVALID) { m_aMasterServers[i] = Info; Added = true; break; } } if(!Added) break; } } io_close(File); return 0; }
void ConBan(IConsole::IResult *pResult, void *pUser, int ClientID) { NETADDR Addr; const char *pStr = pResult->GetString(0); const char *pReason = ""; if(pResult->NumArguments() > 1) pReason = pResult->GetString(1); if(!net_addr_from_str(&Addr, pStr)) AddBan(&Addr, pReason); else dbg_msg("banmaster", "invalid network address to ban"); }
void CNetBan::ConBan(IConsole::IResult *pResult, void *pUser) { CNetBan *pThis = static_cast<CNetBan *>(pUser); const char *pStr = pResult->GetString(0); int Minutes = pResult->NumArguments()>1 ? clamp(pResult->GetInteger(1), 0, 44640) : 30; const char *pReason = pResult->NumArguments()>2 ? pResult->GetString(2) : "No reason given"; NETADDR Addr; if(net_addr_from_str(&Addr, pStr) == 0) pThis->BanAddr(&Addr, Minutes*60, pReason); else pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (invalid network address)"); }
void CGameControllerOpenFNG::DoRagequit() { if (*m_aRagequitAddr) { NETADDR Addr; if (net_addr_from_str(&Addr, m_aRagequitAddr) == 0) { Addr.port = 0; char aBan[128]; str_format(aBan, sizeof aBan, "ban %s 1 Forcefully left the server while being frozen.", m_aRagequitAddr); GS->Console()->ExecuteLine(aBan); } *m_aRagequitAddr = '\0'; } }
void CNetBan::ConUnban(IConsole::IResult *pResult, void *pUser) { CNetBan *pThis = static_cast<CNetBan *>(pUser); const char *pStr = pResult->GetString(0); if(StrAllnum(pStr)) pThis->UnbanByIndex(str_toint(pStr)); else { NETADDR Addr; if(net_addr_from_str(&Addr, pStr) == 0) pThis->UnbanByAddr(&Addr); else pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "unban error (invalid network address)"); } }
bool CServerBrowserFavorites::AddFavoriteEx(const char *pHostname, const NETADDR *pAddr, bool DoCheck) { if(m_NumFavoriteServers == MAX_FAVORITES || FindFavoriteByHostname(pHostname, 0)) return false; bool Result = false; // check if hostname is a net address string if(net_addr_from_str(&m_aFavoriteServers[m_NumFavoriteServers].m_Addr, pHostname) == 0) { // make sure that we don't already have the server in our list if(FindFavoriteByAddr(m_aFavoriteServers[m_NumFavoriteServers].m_Addr, 0) != 0) return false; // check if hostname does not match given address if(DoCheck && net_addr_comp(&m_aFavoriteServers[m_NumFavoriteServers].m_Addr, pAddr) != 0) return false; // add the server to the list m_aFavoriteServers[m_NumFavoriteServers].m_State = FAVSTATE_ADDR; Result = true; } else { // prepare for hostname lookup if(DoCheck) { m_aFavoriteServers[m_NumFavoriteServers].m_State = FAVSTATE_LOOKUPCHECK; m_aFavoriteServers[m_NumFavoriteServers].m_Addr = *pAddr; } else m_aFavoriteServers[m_NumFavoriteServers].m_State = FAVSTATE_LOOKUP; ++m_FavLookup.m_LookupCount; } str_copy(m_aFavoriteServers[m_NumFavoriteServers].m_aHostname, pHostname, sizeof(m_aFavoriteServers[m_NumFavoriteServers].m_aHostname)); ++m_NumFavoriteServers; if(g_Config.m_Debug) { char aBuf[256]; str_format(aBuf, sizeof(aBuf), "added fav '%s' (%s)", pHostname); m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "client_srvbrowse", aBuf); } return Result; }
virtual int Load() { CLineReader LineReader; IOHANDLE File; int Count = 0; if(!m_pStorage) return -1; // try to open file File = m_pStorage->OpenFile("masters.cfg", IOFLAG_READ, IStorage::TYPE_SAVE); if(!File) return -1; LineReader.Init(File); while(1) { CMasterInfo Info = {{0}}; const char *pLine = LineReader.Get(); if(!pLine) break; // parse line char aAddrStr[NETADDR_MAXSTRSIZE]; if(sscanf(pLine, "%s %s", Info.m_aHostname, aAddrStr) == 2 && net_addr_from_str(&Info.m_Addr, aAddrStr) == 0) { Info.m_Addr.port = 8300; if(Count != MAX_MASTERSERVERS) { m_aMasterServers[Count] = Info; Count++; } //else // dbg_msg("engine/mastersrv", "warning: skipped master server '%s' due to limit of %d", pLine, MAX_MASTERSERVERS); } //else // dbg_msg("engine/mastersrv", "warning: couldn't parse master server '%s'", pLine); } io_close(File); return 0; }
void CQueryRecent::OnData() { while(Next()) // process everything { if(m_paRecentList) // we only have this when writing to the db { CServerBrowser::RecentServer e; mem_zero(&e, sizeof(CServerBrowser::RecentServer)); e.m_ID = GetInt(GetID("id")); const char *pAddrStr = GetText(GetID("addr")); if(pAddrStr) net_addr_from_str(&e.m_Addr, pAddrStr); const char *pLastJoined = GetText(GetID("last_joined")); if(pLastJoined) str_copy(e.m_LastJoined, pLastJoined, sizeof(e.m_LastJoined)); m_paRecentList->add(e); } } }
void CServerBrowser::LoadCacheThread(void *pUser) { CServerBrowser *pSelf = (CServerBrowser *)pUser; IStorageTW *pStorage = pSelf->Kernel()->RequestInterface<IStorageTW>(); int64 StartTime = time_get(); // clear out everything pSelf->m_ServerlistHeap.Reset(); pSelf->m_NumServers = 0; pSelf->m_NumSortedServers = 0; mem_zero(pSelf->m_aServerlistIp, sizeof(pSelf->m_aServerlistIp)); pSelf->m_pFirstReqServer = 0; pSelf->m_pLastReqServer = 0; pSelf->m_NumRequests = 0; pSelf->m_CurrentMaxRequests = g_Config.m_BrMaxRequests; pSelf->m_CurrentToken = (pSelf->m_CurrentToken+1)&0xff; pSelf->m_ServerlistType = IServerBrowser::TYPE_INTERNET; // open file IOHANDLE File = pStorage->OpenFile("tmp/cache/serverlist", IOFLAG_READ, IStorageTW::TYPE_ALL); if(!File) { dbg_msg("browser", "opening cache file failed."); pSelf->m_CacheExists = false; pSelf->m_ServerdataLocked = false; return;// false; } // get version { char v; io_read(File, &v, 1); if(g_Config.m_Debug) dbg_msg("browser", "loading serverlist from cache..."); if(v != CACHE_VERSION) dbg_msg("cache", "file version doesn't match, we may fail! (%i != %i)", v, CACHE_VERSION); } // get number of servers int NumServers = 0; io_read(File, &NumServers, sizeof(NumServers)); //dbg_msg("browser", "serverlist cache entries: %i", NumServers); mem_zero(pSelf->m_ppServerlist, pSelf->m_NumServerCapacity); // get length of array io_read(File, &pSelf->m_NumServerCapacity, sizeof(pSelf->m_NumServerCapacity)); // get rid of current serverlist and create a new one mem_free(pSelf->m_ppServerlist); pSelf->m_ppServerlist = (CServerEntry **)mem_alloc(pSelf->m_NumServerCapacity*sizeof(CServerEntry*), 1); // read the data from the file into the serverlist CServerInfo *pServerInfos = (CServerInfo*)mem_alloc(sizeof(CServerInfo)*NumServers, 0); io_read(File, pServerInfos, sizeof(CServerInfo)*NumServers); io_close(File); for(int i = 0; i < NumServers; i++) { NETADDR Addr; net_addr_from_str(&Addr, pServerInfos[i].m_aAddress); //dbg_msg("browser", "loading %i %s %s", i, Info.m_aAddress, Info.m_aName); pSelf->Set(Addr, IServerBrowser::SET_TOKEN, pSelf->m_CurrentToken, &pServerInfos[i]); } mem_free(pServerInfos); if(g_Config.m_Debug) dbg_msg("browser", "successfully loaded serverlist cache with %i entries (total %i), took %.2fms", pSelf->m_NumServers, NumServers, ((time_get()-StartTime)*1000)/(float)time_freq()); // TODO: check if saving actually succeeded //m_NeedUpgrade = true; // disabled due to sending our ip out to the whole universe pSelf->m_ServerdataLocked = false; pSelf->Sort(true); return;// true; }
void CServerBrowser::LoadDDNet() { // reset servers / countries m_NumDDNetCountries = 0; m_NumDDNetTypes = 0; // load ddnet server list IStorageTW *pStorage = Kernel()->RequestInterface<IStorageTW>(); IOHANDLE File = pStorage->OpenFile("tmp/cache/ddnet-servers.json", IOFLAG_READ, IStorageTW::TYPE_ALL); if(File) { char aBuf[4096*4]; mem_zero(aBuf, sizeof(aBuf)); io_read(File, aBuf, sizeof(aBuf)); io_close(File); // parse JSON json_value *pCountries = json_parse(aBuf); if (pCountries && pCountries->type == json_array) { for (int i = 0; i < json_array_length(pCountries) && m_NumDDNetCountries < MAX_DDNET_COUNTRIES; i++) { // pSrv - { name, flagId, servers } const json_value *pSrv = json_array_get(pCountries, i); const json_value *pTypes = json_object_get(pSrv, "servers"); const json_value *pName = json_object_get(pSrv, "name"); const json_value *pFlagID = json_object_get(pSrv, "flagId"); if (pSrv->type != json_object || pTypes->type != json_object || pName->type != json_string || pFlagID->type != json_integer) { dbg_msg("client_srvbrowse", "invalid attributes"); continue; } // build structure CDDNetCountry *pCntr = &m_aDDNetCountries[m_NumDDNetCountries]; pCntr->Reset(); str_copy(pCntr->m_aName, json_string_get(pName), sizeof(pCntr->m_aName)); pCntr->m_FlagID = json_int_get(pFlagID); // add country for (unsigned int t = 0; t < pTypes->u.object.length; t++) { const char *pType = pTypes->u.object.values[t].name; const json_value *pAddrs = pTypes->u.object.values[t].value; // add type if(json_array_length(pAddrs) > 0 && m_NumDDNetTypes < MAX_DDNET_TYPES) { int pos; for(pos = 0; pos < m_NumDDNetTypes; pos++) { if(!str_comp(m_aDDNetTypes[pos], pType)) break; } if(pos == m_NumDDNetTypes) { str_copy(m_aDDNetTypes[m_NumDDNetTypes], pType, sizeof(m_aDDNetTypes[m_NumDDNetTypes])); m_NumDDNetTypes++; } } // add addresses for (int g = 0; g < json_array_length(pAddrs); g++, pCntr->m_NumServers++) { const json_value *pAddr = json_array_get(pAddrs, g); const char* pStr = json_string_get(pAddr); net_addr_from_str(&pCntr->m_aServers[pCntr->m_NumServers], pStr); str_copy(pCntr->m_aTypes[pCntr->m_NumServers], pType, sizeof(pCntr->m_aTypes[pCntr->m_NumServers])); } } m_NumDDNetCountries++; } } if (pCountries) json_value_free(pCountries); } }
int main(int argc, const char **argv) // ignore_convention { int64 LastUpdate = time_get(); dbg_logger_stdout(); net_init(); IKernel *pKernel = IKernel::Create(); IStorage *pStorage = CreateStorage("Teeworlds", argc, argv); // ignore_convention m_pConsole = CreateConsole(CFGFLAG_BANMASTER); m_pConsole->RegisterPrintCallback(StandardOutput, 0); m_pConsole->Register("ban", "s?r", CFGFLAG_BANMASTER, ConBan, 0, "Bans the specified ip", 0); m_pConsole->Register("unban_all", "", CFGFLAG_BANMASTER, ConUnbanAll, 0, "Unbans all ips", 0); m_pConsole->Register("bind", "s", CFGFLAG_BANMASTER, ConSetBindAddr, 0, "Binds to the specified address", 0); { bool RegisterFail = false; RegisterFail = RegisterFail || !pKernel->RegisterInterface(m_pConsole); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage); if(RegisterFail) return -1; } m_pConsole->ExecuteFile(BANMASTER_BANFILE); NETADDR BindAddr; if(m_aBindAddr[0] && net_host_lookup(m_aBindAddr, &BindAddr, NETTYPE_IPV4) == 0) { if(BindAddr.port == 0) BindAddr.port = BANMASTER_PORT; } else { mem_zero(&BindAddr, sizeof(BindAddr)); BindAddr.port = BANMASTER_PORT; } m_Net.Open(BindAddr, 0); // TODO: DDRace: heinrich5991: check socket for errors dbg_msg("banmaster", "started"); while(1) { m_Net.Update(); // process m_aPackets CNetChunk p; while(m_Net.Recv(&p)) { if(p.m_DataSize >= (int)sizeof(BANMASTER_IPCHECK) && !mem_comp(p.m_pData, BANMASTER_IPCHECK, sizeof(BANMASTER_IPCHECK))) { char *pAddr = (char*)p.m_pData + sizeof(BANMASTER_IPCHECK); NETADDR CheckAddr; if(net_addr_from_str(&CheckAddr, pAddr)) { dbg_msg("banmaster", "dropped weird message ip=%d.%d.%d.%d checkaddr='%s'", p.m_Address.ip[0], p.m_Address.ip[1], p.m_Address.ip[2], p.m_Address.ip[3], pAddr); } else { int Banned = SendResponse(&p.m_Address, &CheckAddr); dbg_msg("banmaster", "responded to checkmsg ip=%d.%d.%d.%d checkaddr=%d.%d.%d.%d result=%s", p.m_Address.ip[0], p.m_Address.ip[1], p.m_Address.ip[2], p.m_Address.ip[3], CheckAddr.ip[0], CheckAddr.ip[1], CheckAddr.ip[2], CheckAddr.ip[3], (Banned) ? "ban" : "ok"); } } } if(time_get() - LastUpdate > time_freq() * BAN_REREAD_TIME) { ClearBans(); LastUpdate = time_get(); m_pConsole->ExecuteFile(BANMASTER_BANFILE); } // be nice to the CPU thread_sleep(1); } return 0; }