Skip to content

Commit e4c9760

Browse files
committed
Server: "k" command now kicks specified socket/s based on IP and port
If only an IP is provided, any sockets originating from the IP will be kicked. If a port is specified, only a specific socket, if found. The IP and port in the command are separated with ":" just like in the address string.
1 parent bc74040 commit e4c9760

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

InternetGamesServer/Command.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ DWORD WINAPI CommandHandler(void*)
3737
<< " - 'c {key} {value}': Prints or configures the option with the specified key. For a list of valid options, type 'c'." << std::endl
3838
<< " - 'lc': Lists all connected client sockets." << std::endl
3939
<< " - 'lm': Lists all active matches." << std::endl
40-
<< " - 'k {ip}': Kicks any connected client sockets, originating from the provided IP." << std::endl
40+
<< " - 'k {ip}:{port [optional]}': Kicks any connected client sockets, originating from the provided IP and port." << std::endl
41+
<< " If no port is specified, any clients that originate from the IP will be kicked." << std::endl
4142
<< " - 'b {ip}': Bans any client sockets, originating from the provided IP, from connecting to this server." << std::endl
4243
<< " - 'd {index}': Destroys (disbands) the match with the specified index." << std::endl;
4344
}
@@ -61,15 +62,36 @@ DWORD WINAPI CommandHandler(void*)
6162
}
6263
else if (cmd[0] == 'k' || cmd[0] == 'K')
6364
{
64-
std::string ip;
65-
inputStr >> ip;
66-
if (ip.empty())
65+
std::string address;
66+
inputStr >> address;
67+
if (address.empty())
6768
{
68-
std::cout << "No client IP provided!" << std::endl;
69+
std::cout << "No client address provided!" << std::endl;
6970
continue;
7071
}
71-
72-
std::cout << "TODO! Kick " << ip << std::endl;
72+
std::stringstream addrStr(address);
73+
std::string ip, portStr;
74+
std::getline(addrStr, ip, ':');
75+
if (std::getline(addrStr, portStr, ':'))
76+
{
77+
const USHORT port = static_cast<USHORT>(std::stoi(portStr));
78+
Socket* socket = Socket::GetSocketByIP(ip, port);
79+
if (!socket)
80+
throw std::runtime_error("No socket with IP \"" + ip + "\" and port " + std::to_string(port) + " found!");
81+
socket->Disconnect();
82+
std::cout << "Disconnected socket \"" << socket->GetAddressString() << "\"!" << std::endl;
83+
}
84+
else
85+
{
86+
const std::vector<Socket*> sockets = Socket::GetSocketsByIP(ip);
87+
if (sockets.empty())
88+
throw std::runtime_error("No sockets with IP \"" + ip + "\" found!");
89+
for (Socket* socket : sockets)
90+
{
91+
socket->Disconnect();
92+
std::cout << "Disconnected socket \"" << socket->GetAddressString() << "\"!" << std::endl;
93+
}
94+
}
7395
}
7496
else if (cmd[0] == 'b' || cmd[0] == 'B')
7597
{

InternetGamesServer/Socket.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,36 @@ Socket::GetAddressString(SOCKET socket, const char portSeparator)
237237
}
238238

239239

240+
std::vector<Socket*>
241+
Socket::GetSocketsByIP(const std::string& ip)
242+
{
243+
sockaddr_in socketInfo;
244+
int socketInfoSize = sizeof(socketInfo);
245+
246+
std::vector<Socket*> sockets;
247+
for (Socket* socket : s_socketList)
248+
{
249+
if (socket->m_ip == ip)
250+
sockets.push_back(socket);
251+
}
252+
return sockets;
253+
}
254+
255+
Socket*
256+
Socket::GetSocketByIP(const std::string& ip, USHORT port)
257+
{
258+
sockaddr_in socketInfo;
259+
int socketInfoSize = sizeof(socketInfo);
260+
261+
for (Socket* socket : s_socketList)
262+
{
263+
if (socket->m_ip == ip && socket->m_port == port)
264+
return socket;
265+
}
266+
return nullptr;
267+
}
268+
269+
240270
Socket::Socket(SOCKET socket, std::ostream& log) :
241271
m_socket(socket),
242272
m_log(log),

InternetGamesServer/Socket.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class Socket final
3535
public:
3636
static inline const std::vector<Socket*>& GetList() { return s_socketList; }
3737

38+
static std::vector<Socket*> GetSocketsByIP(const std::string& ip);
39+
static Socket* GetSocketByIP(const std::string& ip, USHORT port);
40+
3841
// Handler for the thread of a socket
3942
static DWORD WINAPI SocketHandler(void* socket);
4043

0 commit comments

Comments
 (0)