2017-07-07 20:34:15 +01:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-07-09 09:43:53 +01:00
|
|
|
#include <algorithm>
|
2017-07-08 14:24:47 +01:00
|
|
|
#include <atomic>
|
2017-11-19 18:52:37 +00:00
|
|
|
#include <iomanip>
|
2017-08-19 18:14:33 +01:00
|
|
|
#include <mutex>
|
2017-07-08 15:47:24 +01:00
|
|
|
#include <random>
|
2018-11-02 12:17:25 +00:00
|
|
|
#include <regex>
|
2017-11-19 18:52:37 +00:00
|
|
|
#include <sstream>
|
2017-07-08 14:24:47 +01:00
|
|
|
#include <thread>
|
2018-03-11 10:58:43 +00:00
|
|
|
#include "common/logging/log.h"
|
2017-07-07 20:34:15 +01:00
|
|
|
#include "enet/enet.h"
|
2017-07-08 15:47:24 +01:00
|
|
|
#include "network/packet.h"
|
2017-07-07 20:34:15 +01:00
|
|
|
#include "network/room.h"
|
2018-10-27 08:35:01 +01:00
|
|
|
#include "network/verify_user.h"
|
2017-07-07 20:34:15 +01:00
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
|
|
|
class Room::RoomImpl {
|
|
|
|
public:
|
2017-07-08 15:47:24 +01:00
|
|
|
// This MAC address is used to generate a 'Nintendo' like Mac address.
|
2017-07-26 20:06:40 +01:00
|
|
|
const MacAddress NintendoOUI;
|
2017-07-08 15:47:24 +01:00
|
|
|
std::mt19937 random_gen; ///< Random number generator. Used for GenerateMacAddress
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
ENetHost* server = nullptr; ///< Network interface.
|
|
|
|
|
|
|
|
std::atomic<State> state{State::Closed}; ///< Current state of the room.
|
|
|
|
RoomInformation room_information; ///< Information about this room.
|
2017-07-08 14:24:47 +01:00
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
std::string verify_UID; ///< A GUID which may be used for verfication.
|
|
|
|
mutable std::mutex verify_UID_mutex; ///< Mutex for verify_UID
|
|
|
|
|
2017-11-19 18:52:37 +00:00
|
|
|
std::string password; ///< The password required to connect to this room.
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
struct Member {
|
2018-10-31 15:07:03 +00:00
|
|
|
std::string nickname; ///< The nickname of the member.
|
|
|
|
std::string console_id_hash; ///< A hash of the console ID of the member.
|
|
|
|
GameInfo game_info; ///< The current game of the member
|
|
|
|
MacAddress mac_address; ///< The assigned mac address of the member.
|
2018-10-27 08:35:01 +01:00
|
|
|
/// Data of the user, often including authenticated forum username.
|
|
|
|
VerifyUser::UserData user_data;
|
|
|
|
ENetPeer* peer; ///< The remote peer.
|
2017-07-08 15:47:24 +01:00
|
|
|
};
|
|
|
|
using MemberList = std::vector<Member>;
|
2017-08-19 18:14:33 +01:00
|
|
|
MemberList members; ///< Information about the members of this room
|
|
|
|
mutable std::mutex member_mutex; ///< Mutex for locking the members list
|
|
|
|
/// This should be a std::shared_mutex as soon as C++17 is supported
|
2017-07-08 15:47:24 +01:00
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
UsernameBanList username_ban_list; ///< List of banned usernames
|
|
|
|
IPBanList ip_ban_list; ///< List of banned IP addresses
|
|
|
|
mutable std::mutex ban_list_mutex; ///< Mutex for the ban lists
|
|
|
|
|
2017-07-26 20:06:40 +01:00
|
|
|
RoomImpl()
|
|
|
|
: random_gen(std::random_device()()), NintendoOUI{0x00, 0x1F, 0x32, 0x00, 0x00, 0x00} {}
|
2017-07-08 15:47:24 +01:00
|
|
|
|
2017-07-08 14:24:47 +01:00
|
|
|
/// Thread that receives and dispatches network packets
|
|
|
|
std::unique_ptr<std::thread> room_thread;
|
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
/// Verification backend of the room
|
|
|
|
std::unique_ptr<VerifyUser::Backend> verify_backend;
|
|
|
|
|
2017-07-08 14:24:47 +01:00
|
|
|
/// Thread function that will receive and dispatch messages until the room is destroyed.
|
|
|
|
void ServerLoop();
|
|
|
|
void StartLoop();
|
2017-07-08 15:47:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses and answers a room join request from a client.
|
|
|
|
* Validates the uniqueness of the username and assigns the MAC address
|
|
|
|
* that the client will use for the remainder of the connection.
|
|
|
|
*/
|
|
|
|
void HandleJoinRequest(const ENetEvent* event);
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
/**
|
|
|
|
* Parses and answers a kick request from a client.
|
|
|
|
* Validates the permissions and that the given user exists and then kicks the member.
|
|
|
|
*/
|
|
|
|
void HandleModKickPacket(const ENetEvent* event);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses and answers a ban request from a client.
|
|
|
|
* Validates the permissions and bans the user (by forum username or IP).
|
|
|
|
*/
|
|
|
|
void HandleModBanPacket(const ENetEvent* event);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses and answers a unban request from a client.
|
|
|
|
* Validates the permissions and unbans the address.
|
|
|
|
*/
|
|
|
|
void HandleModUnbanPacket(const ENetEvent* event);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses and answers a get ban list request from a client.
|
|
|
|
* Validates the permissions and returns the ban list.
|
|
|
|
*/
|
|
|
|
void HandleModGetBanListPacket(const ENetEvent* event);
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
/**
|
|
|
|
* Returns whether the nickname is valid, ie. isn't already taken by someone else in the room.
|
|
|
|
*/
|
|
|
|
bool IsValidNickname(const std::string& nickname) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the MAC address is valid, ie. isn't already taken by someone else in the
|
|
|
|
* room.
|
|
|
|
*/
|
|
|
|
bool IsValidMacAddress(const MacAddress& address) const;
|
|
|
|
|
2018-10-31 15:07:03 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the console ID (hash) is valid, ie. isn't already taken by someone else in
|
|
|
|
* the room.
|
|
|
|
*/
|
|
|
|
bool IsValidConsoleId(const std::string& console_id_hash) const;
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
/**
|
|
|
|
* Returns whether a user has mod permissions.
|
|
|
|
*/
|
|
|
|
bool HasModPermission(const ENetPeer* client) const;
|
|
|
|
|
2018-04-20 08:34:37 +01:00
|
|
|
/**
|
|
|
|
* Sends a ID_ROOM_IS_FULL message telling the client that the room is full.
|
|
|
|
*/
|
|
|
|
void SendRoomIsFull(ENetPeer* client);
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
/**
|
|
|
|
* Sends a ID_ROOM_NAME_COLLISION message telling the client that the name is invalid.
|
|
|
|
*/
|
|
|
|
void SendNameCollision(ENetPeer* client);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a ID_ROOM_MAC_COLLISION message telling the client that the MAC is invalid.
|
|
|
|
*/
|
|
|
|
void SendMacCollision(ENetPeer* client);
|
|
|
|
|
2018-10-31 15:07:03 +00:00
|
|
|
/**
|
|
|
|
* Sends a IdConsoleIdCollison message telling the client that another member with the same
|
|
|
|
* console ID exists.
|
|
|
|
*/
|
|
|
|
void SendConsoleIdCollision(ENetPeer* client);
|
|
|
|
|
2017-07-14 08:20:39 +01:00
|
|
|
/**
|
2017-07-15 20:24:11 +01:00
|
|
|
* Sends a ID_ROOM_VERSION_MISMATCH message telling the client that the version is invalid.
|
2017-07-14 08:20:39 +01:00
|
|
|
*/
|
|
|
|
void SendVersionMismatch(ENetPeer* client);
|
|
|
|
|
2017-11-19 18:52:37 +00:00
|
|
|
/**
|
|
|
|
* Sends a ID_ROOM_WRONG_PASSWORD message telling the client that the password is wrong.
|
|
|
|
*/
|
|
|
|
void SendWrongPassword(ENetPeer* client);
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
/**
|
|
|
|
* Notifies the member that its connection attempt was successful,
|
|
|
|
* and it is now part of the room.
|
|
|
|
*/
|
|
|
|
void SendJoinSuccess(ENetPeer* client, MacAddress mac_address);
|
|
|
|
|
2018-12-15 09:13:46 +00:00
|
|
|
/**
|
|
|
|
* Notifies the member that its connection attempt was successful,
|
|
|
|
* and it is now part of the room, and it has been granted mod permissions.
|
|
|
|
*/
|
|
|
|
void SendJoinSuccessAsMod(ENetPeer* client, MacAddress mac_address);
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
/**
|
|
|
|
* Sends a IdHostKicked message telling the client that they have been kicked.
|
|
|
|
*/
|
|
|
|
void SendUserKicked(ENetPeer* client);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a IdHostBanned message telling the client that they have been banned.
|
|
|
|
*/
|
|
|
|
void SendUserBanned(ENetPeer* client);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a IdModPermissionDenied message telling the client that they do not have mod
|
|
|
|
* permission.
|
|
|
|
*/
|
|
|
|
void SendModPermissionDenied(ENetPeer* client);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a IdModNoSuchUser message telling the client that the given user could not be found.
|
|
|
|
*/
|
|
|
|
void SendModNoSuchUser(ENetPeer* client);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the ban list in response to a client's request for getting ban list.
|
|
|
|
*/
|
|
|
|
void SendModBanListResponse(ENetPeer* client);
|
|
|
|
|
2017-07-15 10:39:27 +01:00
|
|
|
/**
|
|
|
|
* Notifies the members that the room is closed,
|
|
|
|
*/
|
|
|
|
void SendCloseMessage();
|
|
|
|
|
2018-11-09 13:55:57 +00:00
|
|
|
/**
|
|
|
|
* Sends a system message to all the connected clients.
|
|
|
|
*/
|
|
|
|
void SendStatusMessage(StatusMessageTypes type, const std::string& nickname,
|
|
|
|
const std::string& username);
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
/**
|
|
|
|
* Sends the information about the room, along with the list of members
|
|
|
|
* to every connected client in the room.
|
|
|
|
* The packet has the structure:
|
|
|
|
* <MessageID>ID_ROOM_INFORMATION
|
|
|
|
* <String> room_name
|
2018-04-30 08:40:51 +01:00
|
|
|
* <String> room_description
|
2017-07-09 14:06:02 +01:00
|
|
|
* <u32> member_slots: The max number of clients allowed in this room
|
2017-11-19 18:52:37 +00:00
|
|
|
* <String> uid
|
|
|
|
* <u16> port
|
2017-07-09 14:06:02 +01:00
|
|
|
* <u32> num_members: the number of currently joined clients
|
2017-07-08 15:47:24 +01:00
|
|
|
* This is followed by the following three values for each member:
|
|
|
|
* <String> nickname of that member
|
|
|
|
* <MacAddress> mac_address of that member
|
|
|
|
* <String> game_name of that member
|
|
|
|
*/
|
|
|
|
void BroadcastRoomInformation();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a free MAC address to assign to a new client.
|
|
|
|
* The first 3 bytes are the NintendoOUI 0x00, 0x1F, 0x32
|
|
|
|
*/
|
|
|
|
MacAddress GenerateMacAddress();
|
2017-07-09 09:40:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcasts this packet to all members except the sender.
|
|
|
|
* @param event The ENet event containing the data
|
|
|
|
*/
|
|
|
|
void HandleWifiPacket(const ENetEvent* event);
|
2017-07-09 09:43:53 +01:00
|
|
|
|
2017-07-09 11:26:03 +01:00
|
|
|
/**
|
|
|
|
* Extracts a chat entry from a received ENet packet and adds it to the chat queue.
|
|
|
|
* @param event The ENet event that was received.
|
|
|
|
*/
|
|
|
|
void HandleChatPacket(const ENetEvent* event);
|
|
|
|
|
2017-07-09 14:06:02 +01:00
|
|
|
/**
|
|
|
|
* Extracts the game name from a received ENet packet and broadcasts it.
|
|
|
|
* @param event The ENet event that was received.
|
|
|
|
*/
|
|
|
|
void HandleGameNamePacket(const ENetEvent* event);
|
|
|
|
|
2017-07-09 09:43:53 +01:00
|
|
|
/**
|
|
|
|
* Removes the client from the members list if it was in it and announces the change
|
|
|
|
* to all other clients.
|
|
|
|
*/
|
|
|
|
void HandleClientDisconnection(ENetPeer* client);
|
2017-07-07 20:34:15 +01:00
|
|
|
};
|
|
|
|
|
2017-07-08 14:24:47 +01:00
|
|
|
// RoomImpl
|
|
|
|
void Room::RoomImpl::ServerLoop() {
|
|
|
|
while (state != State::Closed) {
|
|
|
|
ENetEvent event;
|
2017-11-19 18:52:37 +00:00
|
|
|
if (enet_host_service(server, &event, 50) > 0) {
|
2017-07-08 14:24:47 +01:00
|
|
|
switch (event.type) {
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
2017-07-08 15:47:24 +01:00
|
|
|
switch (event.packet->data[0]) {
|
|
|
|
case IdJoinRequest:
|
|
|
|
HandleJoinRequest(&event);
|
|
|
|
break;
|
2017-08-19 18:14:33 +01:00
|
|
|
case IdSetGameInfo:
|
2017-07-09 14:06:02 +01:00
|
|
|
HandleGameNamePacket(&event);
|
|
|
|
break;
|
2017-07-09 09:40:11 +01:00
|
|
|
case IdWifiPacket:
|
|
|
|
HandleWifiPacket(&event);
|
|
|
|
break;
|
2017-07-09 11:26:03 +01:00
|
|
|
case IdChatMessage:
|
|
|
|
HandleChatPacket(&event);
|
|
|
|
break;
|
2018-11-24 08:08:32 +00:00
|
|
|
// Moderation
|
|
|
|
case IdModKick:
|
|
|
|
HandleModKickPacket(&event);
|
|
|
|
break;
|
|
|
|
case IdModBan:
|
|
|
|
HandleModBanPacket(&event);
|
|
|
|
break;
|
|
|
|
case IdModUnban:
|
|
|
|
HandleModUnbanPacket(&event);
|
|
|
|
break;
|
|
|
|
case IdModGetBanList:
|
|
|
|
HandleModGetBanListPacket(&event);
|
|
|
|
break;
|
2017-07-08 15:47:24 +01:00
|
|
|
}
|
2017-07-08 14:24:47 +01:00
|
|
|
enet_packet_destroy(event.packet);
|
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
2017-07-09 09:43:53 +01:00
|
|
|
HandleClientDisconnection(event.peer);
|
2017-07-08 14:24:47 +01:00
|
|
|
break;
|
2017-11-29 02:09:53 +00:00
|
|
|
case ENET_EVENT_TYPE_NONE:
|
|
|
|
case ENET_EVENT_TYPE_CONNECT:
|
|
|
|
break;
|
2017-07-08 14:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-15 10:39:27 +01:00
|
|
|
// Close the connection to all members:
|
|
|
|
SendCloseMessage();
|
2017-07-08 14:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::StartLoop() {
|
|
|
|
room_thread = std::make_unique<std::thread>(&Room::RoomImpl::ServerLoop, this);
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
|
2018-04-20 08:34:37 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
if (members.size() >= room_information.member_slots) {
|
|
|
|
SendRoomIsFull(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 15:47:24 +01:00
|
|
|
Packet packet;
|
|
|
|
packet.Append(event->packet->data, event->packet->dataLength);
|
2018-08-12 23:55:15 +01:00
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
2017-07-08 15:47:24 +01:00
|
|
|
std::string nickname;
|
|
|
|
packet >> nickname;
|
|
|
|
|
2018-10-31 15:07:03 +00:00
|
|
|
std::string console_id_hash;
|
|
|
|
packet >> console_id_hash;
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
MacAddress preferred_mac;
|
|
|
|
packet >> preferred_mac;
|
|
|
|
|
2017-07-14 08:20:39 +01:00
|
|
|
u32 client_version;
|
|
|
|
packet >> client_version;
|
|
|
|
|
2017-11-19 18:52:37 +00:00
|
|
|
std::string pass;
|
|
|
|
packet >> pass;
|
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
std::string token;
|
|
|
|
packet >> token;
|
|
|
|
|
2017-11-19 18:52:37 +00:00
|
|
|
if (pass != password) {
|
|
|
|
SendWrongPassword(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
if (!IsValidNickname(nickname)) {
|
|
|
|
SendNameCollision(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preferred_mac != NoPreferredMac) {
|
|
|
|
// Verify if the preferred mac is available
|
|
|
|
if (!IsValidMacAddress(preferred_mac)) {
|
|
|
|
SendMacCollision(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Assign a MAC address of this client automatically
|
|
|
|
preferred_mac = GenerateMacAddress();
|
|
|
|
}
|
|
|
|
|
2018-10-31 15:07:03 +00:00
|
|
|
if (!IsValidConsoleId(console_id_hash)) {
|
|
|
|
SendConsoleIdCollision(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:20:39 +01:00
|
|
|
if (client_version != network_version) {
|
|
|
|
SendVersionMismatch(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
// At this point the client is ready to be added to the room.
|
|
|
|
Member member{};
|
|
|
|
member.mac_address = preferred_mac;
|
2018-10-31 15:07:03 +00:00
|
|
|
member.console_id_hash = console_id_hash;
|
2017-07-08 15:47:24 +01:00
|
|
|
member.nickname = nickname;
|
|
|
|
member.peer = event->peer;
|
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
std::string uid;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(verify_UID_mutex);
|
|
|
|
uid = verify_UID;
|
|
|
|
}
|
|
|
|
member.user_data = verify_backend->LoadUserData(uid, token);
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
|
|
|
|
|
|
|
// Check username ban
|
|
|
|
if (!member.user_data.username.empty() &&
|
|
|
|
std::find(username_ban_list.begin(), username_ban_list.end(),
|
|
|
|
member.user_data.username) != username_ban_list.end()) {
|
|
|
|
|
|
|
|
SendUserBanned(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check IP ban
|
|
|
|
char ip_raw[256];
|
|
|
|
enet_address_get_host_ip(&event->peer->address, ip_raw, sizeof(ip_raw) - 1);
|
|
|
|
std::string ip = ip_raw;
|
|
|
|
|
|
|
|
if (std::find(ip_ban_list.begin(), ip_ban_list.end(), ip) != ip_ban_list.end()) {
|
|
|
|
SendUserBanned(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 13:55:57 +00:00
|
|
|
// Notify everyone that the user has joined.
|
|
|
|
SendStatusMessage(IdMemberJoin, member.nickname, member.user_data.username);
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
members.push_back(std::move(member));
|
|
|
|
}
|
2017-07-08 15:47:24 +01:00
|
|
|
|
|
|
|
// Notify everyone that the room information has changed.
|
|
|
|
BroadcastRoomInformation();
|
2018-12-15 09:13:46 +00:00
|
|
|
if (HasModPermission(event->peer)) {
|
|
|
|
SendJoinSuccessAsMod(event->peer, preferred_mac);
|
|
|
|
} else {
|
|
|
|
SendJoinSuccess(event->peer, preferred_mac);
|
|
|
|
}
|
2017-07-08 15:47:24 +01:00
|
|
|
}
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
void Room::RoomImpl::HandleModKickPacket(const ENetEvent* event) {
|
|
|
|
if (!HasModPermission(event->peer)) {
|
|
|
|
SendModPermissionDenied(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet packet;
|
|
|
|
packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
|
|
|
|
|
|
|
std::string nickname;
|
|
|
|
packet >> nickname;
|
|
|
|
|
|
|
|
std::string username;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
const auto target_member =
|
|
|
|
std::find_if(members.begin(), members.end(),
|
|
|
|
[&nickname](const auto& member) { return member.nickname == nickname; });
|
|
|
|
if (target_member == members.end()) {
|
|
|
|
SendModNoSuchUser(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the kicked member
|
|
|
|
SendUserKicked(target_member->peer);
|
|
|
|
|
|
|
|
username = target_member->user_data.username;
|
|
|
|
|
|
|
|
enet_peer_disconnect(target_member->peer, 0);
|
|
|
|
members.erase(target_member);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Announce the change to all clients.
|
|
|
|
SendStatusMessage(IdMemberKicked, nickname, username);
|
|
|
|
BroadcastRoomInformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::HandleModBanPacket(const ENetEvent* event) {
|
|
|
|
if (!HasModPermission(event->peer)) {
|
|
|
|
SendModPermissionDenied(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet packet;
|
|
|
|
packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
|
|
|
|
|
|
|
std::string nickname;
|
|
|
|
packet >> nickname;
|
|
|
|
|
|
|
|
std::string username;
|
|
|
|
std::string ip;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
const auto target_member =
|
|
|
|
std::find_if(members.begin(), members.end(),
|
|
|
|
[&nickname](const auto& member) { return member.nickname == nickname; });
|
|
|
|
if (target_member == members.end()) {
|
|
|
|
SendModNoSuchUser(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the banned member
|
|
|
|
SendUserBanned(target_member->peer);
|
|
|
|
|
|
|
|
nickname = target_member->nickname;
|
|
|
|
username = target_member->user_data.username;
|
|
|
|
|
|
|
|
char ip_raw[256];
|
|
|
|
enet_address_get_host_ip(&target_member->peer->address, ip_raw, sizeof(ip_raw) - 1);
|
|
|
|
ip = ip_raw;
|
|
|
|
|
|
|
|
enet_peer_disconnect(target_member->peer, 0);
|
|
|
|
members.erase(target_member);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
|
|
|
|
|
|
|
if (!username.empty()) {
|
|
|
|
// Ban the forum username
|
|
|
|
if (std::find(username_ban_list.begin(), username_ban_list.end(), username) ==
|
|
|
|
username_ban_list.end()) {
|
|
|
|
|
|
|
|
username_ban_list.emplace_back(username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ban the member's IP as well
|
|
|
|
if (std::find(ip_ban_list.begin(), ip_ban_list.end(), ip) == ip_ban_list.end()) {
|
|
|
|
ip_ban_list.emplace_back(ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Announce the change to all clients.
|
|
|
|
SendStatusMessage(IdMemberBanned, nickname, username);
|
|
|
|
BroadcastRoomInformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::HandleModUnbanPacket(const ENetEvent* event) {
|
|
|
|
if (!HasModPermission(event->peer)) {
|
|
|
|
SendModPermissionDenied(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet packet;
|
|
|
|
packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
|
|
|
|
|
|
|
std::string address;
|
|
|
|
packet >> address;
|
|
|
|
|
|
|
|
bool unbanned = false;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
|
|
|
|
|
|
|
auto it = std::find(username_ban_list.begin(), username_ban_list.end(), address);
|
|
|
|
if (it != username_ban_list.end()) {
|
|
|
|
unbanned = true;
|
|
|
|
username_ban_list.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
it = std::find(ip_ban_list.begin(), ip_ban_list.end(), address);
|
|
|
|
if (it != ip_ban_list.end()) {
|
|
|
|
unbanned = true;
|
|
|
|
ip_ban_list.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unbanned) {
|
|
|
|
SendStatusMessage(IdAddressUnbanned, address, "");
|
|
|
|
} else {
|
|
|
|
SendModNoSuchUser(event->peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::HandleModGetBanListPacket(const ENetEvent* event) {
|
|
|
|
if (!HasModPermission(event->peer)) {
|
|
|
|
SendModPermissionDenied(event->peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendModBanListResponse(event->peer);
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
bool Room::RoomImpl::IsValidNickname(const std::string& nickname) const {
|
2018-11-02 12:17:25 +00:00
|
|
|
// A nickname is valid if it matches the regex and is not already taken by anybody else in the
|
|
|
|
// room.
|
|
|
|
const std::regex nickname_regex("^[ a-zA-Z0-9._-]{4,20}$");
|
|
|
|
if (!std::regex_match(nickname, nickname_regex))
|
|
|
|
return false;
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2017-07-09 14:06:02 +01:00
|
|
|
return std::all_of(members.begin(), members.end(),
|
|
|
|
[&nickname](const auto& member) { return member.nickname != nickname; });
|
2017-07-08 15:47:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Room::RoomImpl::IsValidMacAddress(const MacAddress& address) const {
|
|
|
|
// A MAC address is valid if it is not already taken by anybody else in the room.
|
2017-08-19 18:14:33 +01:00
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2017-07-09 14:06:02 +01:00
|
|
|
return std::all_of(members.begin(), members.end(),
|
|
|
|
[&address](const auto& member) { return member.mac_address != address; });
|
2017-07-08 15:47:24 +01:00
|
|
|
}
|
|
|
|
|
2018-10-31 15:07:03 +00:00
|
|
|
bool Room::RoomImpl::IsValidConsoleId(const std::string& console_id_hash) const {
|
|
|
|
// A Console ID is valid if it is not already taken by anybody else in the room.
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
return std::all_of(members.begin(), members.end(), [&console_id_hash](const auto& member) {
|
|
|
|
return member.console_id_hash != console_id_hash;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
bool Room::RoomImpl::HasModPermission(const ENetPeer* client) const {
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
const auto sending_member =
|
|
|
|
std::find_if(members.begin(), members.end(),
|
|
|
|
[client](const auto& member) { return member.peer == client; });
|
|
|
|
if (sending_member == members.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-15 14:47:07 +00:00
|
|
|
if (room_information.enable_citra_mods &&
|
|
|
|
sending_member->user_data.moderator) { // Community moderator
|
|
|
|
|
2018-12-15 09:13:46 +00:00
|
|
|
return true;
|
2018-12-15 14:47:07 +00:00
|
|
|
}
|
|
|
|
if (!room_information.host_username.empty() &&
|
|
|
|
sending_member->user_data.username == room_information.host_username) { // Room host
|
|
|
|
|
2018-12-15 09:13:46 +00:00
|
|
|
return true;
|
2018-12-15 14:47:07 +00:00
|
|
|
}
|
2018-12-15 09:13:46 +00:00
|
|
|
return false;
|
2018-11-24 08:08:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
|
|
|
|
Packet packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
packet << static_cast<u8>(IdNameCollision);
|
2017-07-08 15:47:24 +01:00
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
|
|
|
|
Packet packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
packet << static_cast<u8>(IdMacCollision);
|
2017-07-08 15:47:24 +01:00
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2018-10-31 15:07:03 +00:00
|
|
|
void Room::RoomImpl::SendConsoleIdCollision(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdConsoleIdCollision);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:52:37 +00:00
|
|
|
void Room::RoomImpl::SendWrongPassword(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdWrongPassword);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2018-04-20 08:34:37 +01:00
|
|
|
void Room::RoomImpl::SendRoomIsFull(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdRoomIsFull);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:20:39 +01:00
|
|
|
void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
|
|
|
|
Packet packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
packet << static_cast<u8>(IdVersionMismatch);
|
2017-07-14 08:20:39 +01:00
|
|
|
packet << network_version;
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
|
|
|
|
Packet packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
packet << static_cast<u8>(IdJoinSuccess);
|
2017-07-08 15:47:24 +01:00
|
|
|
packet << mac_address;
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2018-12-15 09:13:46 +00:00
|
|
|
void Room::RoomImpl::SendJoinSuccessAsMod(ENetPeer* client, MacAddress mac_address) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdJoinSuccessAsMod);
|
|
|
|
packet << mac_address;
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
void Room::RoomImpl::SendUserKicked(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdHostKicked);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::SendUserBanned(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdHostBanned);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::SendModPermissionDenied(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdModPermissionDenied);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::SendModNoSuchUser(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdModNoSuchUser);
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Room::RoomImpl::SendModBanListResponse(ENetPeer* client) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdModBanListResponse);
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
|
|
|
packet << username_ban_list;
|
|
|
|
packet << ip_ban_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-07-15 10:39:27 +01:00
|
|
|
void Room::RoomImpl::SendCloseMessage() {
|
|
|
|
Packet packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
packet << static_cast<u8>(IdCloseRoom);
|
2017-08-19 18:14:33 +01:00
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2018-03-11 10:58:43 +00:00
|
|
|
if (!members.empty()) {
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
for (auto& member : members) {
|
|
|
|
enet_peer_send(member.peer, 0, enet_packet);
|
|
|
|
}
|
2017-07-15 10:39:27 +01:00
|
|
|
}
|
|
|
|
enet_host_flush(server);
|
|
|
|
for (auto& member : members) {
|
|
|
|
enet_peer_disconnect(member.peer, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 13:55:57 +00:00
|
|
|
void Room::RoomImpl::SendStatusMessage(StatusMessageTypes type, const std::string& nickname,
|
|
|
|
const std::string& username) {
|
|
|
|
Packet packet;
|
|
|
|
packet << static_cast<u8>(IdStatusMessage);
|
|
|
|
packet << static_cast<u8>(type);
|
|
|
|
packet << nickname;
|
|
|
|
packet << username;
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
if (!members.empty()) {
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
for (auto& member : members) {
|
|
|
|
enet_peer_send(member.peer, 0, enet_packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:47:24 +01:00
|
|
|
void Room::RoomImpl::BroadcastRoomInformation() {
|
|
|
|
Packet packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
packet << static_cast<u8>(IdRoomInformation);
|
2017-07-08 15:47:24 +01:00
|
|
|
packet << room_information.name;
|
2018-04-30 08:40:51 +01:00
|
|
|
packet << room_information.description;
|
2017-07-08 15:47:24 +01:00
|
|
|
packet << room_information.member_slots;
|
2017-11-19 18:52:37 +00:00
|
|
|
packet << room_information.port;
|
|
|
|
packet << room_information.preferred_game;
|
2018-11-24 08:08:32 +00:00
|
|
|
packet << room_information.host_username;
|
2017-07-08 15:47:24 +01:00
|
|
|
|
2017-07-09 14:06:02 +01:00
|
|
|
packet << static_cast<u32>(members.size());
|
2017-08-19 18:14:33 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
for (const auto& member : members) {
|
|
|
|
packet << member.nickname;
|
|
|
|
packet << member.mac_address;
|
|
|
|
packet << member.game_info.name;
|
|
|
|
packet << member.game_info.id;
|
2018-10-27 08:35:01 +01:00
|
|
|
packet << member.user_data.username;
|
|
|
|
packet << member.user_data.display_name;
|
|
|
|
packet << member.user_data.avatar_url;
|
2017-08-19 18:14:33 +01:00
|
|
|
}
|
2017-07-08 15:47:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ENetPacket* enet_packet =
|
|
|
|
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_host_broadcast(server, 0, enet_packet);
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
MacAddress Room::RoomImpl::GenerateMacAddress() {
|
2017-07-09 14:06:02 +01:00
|
|
|
MacAddress result_mac =
|
|
|
|
NintendoOUI; // The first three bytes of each MAC address will be the NintendoOUI
|
2017-07-08 15:47:24 +01:00
|
|
|
std::uniform_int_distribution<> dis(0x00, 0xFF); // Random byte between 0 and 0xFF
|
|
|
|
do {
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 3; i < result_mac.size(); ++i) {
|
2017-07-08 15:47:24 +01:00
|
|
|
result_mac[i] = dis(random_gen);
|
|
|
|
}
|
|
|
|
} while (!IsValidMacAddress(result_mac));
|
|
|
|
return result_mac;
|
|
|
|
}
|
|
|
|
|
2017-07-09 09:40:11 +01:00
|
|
|
void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
|
2017-07-09 14:06:02 +01:00
|
|
|
Packet in_packet;
|
|
|
|
in_packet.Append(event->packet->data, event->packet->dataLength);
|
2017-07-15 20:24:11 +01:00
|
|
|
in_packet.IgnoreBytes(sizeof(u8)); // Message type
|
2017-07-09 14:06:02 +01:00
|
|
|
in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Type
|
|
|
|
in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel
|
|
|
|
in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address
|
|
|
|
MacAddress destination_address;
|
|
|
|
in_packet >> destination_address;
|
|
|
|
|
|
|
|
Packet out_packet;
|
|
|
|
out_packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
|
|
|
|
if (destination_address == BroadcastMac) { // Send the data to everyone except the sender
|
2017-08-19 18:14:33 +01:00
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2018-03-11 10:58:43 +00:00
|
|
|
bool sent_packet = false;
|
2017-07-09 14:06:02 +01:00
|
|
|
for (const auto& member : members) {
|
2018-03-11 10:58:43 +00:00
|
|
|
if (member.peer != event->peer) {
|
|
|
|
sent_packet = true;
|
2017-07-14 08:20:39 +01:00
|
|
|
enet_peer_send(member.peer, 0, enet_packet);
|
2018-03-11 10:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sent_packet) {
|
|
|
|
enet_packet_destroy(enet_packet);
|
2017-07-09 14:06:02 +01:00
|
|
|
}
|
|
|
|
} else { // Send the data only to the destination client
|
2017-08-19 18:14:33 +01:00
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2017-07-09 14:06:02 +01:00
|
|
|
auto member = std::find_if(members.begin(), members.end(),
|
|
|
|
[destination_address](const Member& member) -> bool {
|
|
|
|
return member.mac_address == destination_address;
|
|
|
|
});
|
|
|
|
if (member != members.end()) {
|
|
|
|
enet_peer_send(member->peer, 0, enet_packet);
|
2018-03-11 10:58:43 +00:00
|
|
|
} else {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_ERROR(Network,
|
2018-06-29 14:56:12 +01:00
|
|
|
"Attempting to send to unknown MAC address: "
|
|
|
|
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
|
|
|
|
destination_address[0], destination_address[1], destination_address[2],
|
|
|
|
destination_address[3], destination_address[4], destination_address[5]);
|
2018-03-11 10:58:43 +00:00
|
|
|
enet_packet_destroy(enet_packet);
|
2017-07-09 14:06:02 +01:00
|
|
|
}
|
2017-07-09 09:40:11 +01:00
|
|
|
}
|
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-07-09 11:26:03 +01:00
|
|
|
void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
|
|
|
|
Packet in_packet;
|
|
|
|
in_packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
|
2018-08-12 23:55:15 +01:00
|
|
|
in_packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
2017-07-09 11:26:03 +01:00
|
|
|
std::string message;
|
|
|
|
in_packet >> message;
|
2017-07-09 14:06:02 +01:00
|
|
|
auto CompareNetworkAddress = [event](const Member member) -> bool {
|
2017-07-09 11:26:03 +01:00
|
|
|
return member.peer == event->peer;
|
|
|
|
};
|
2017-08-19 18:14:33 +01:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2017-07-09 11:26:03 +01:00
|
|
|
const auto sending_member = std::find_if(members.begin(), members.end(), CompareNetworkAddress);
|
|
|
|
if (sending_member == members.end()) {
|
|
|
|
return; // Received a chat message from a unknown sender
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:52:37 +00:00
|
|
|
// Limit the size of chat messages to MaxMessageSize
|
|
|
|
message.resize(MaxMessageSize);
|
|
|
|
|
2017-07-09 11:26:03 +01:00
|
|
|
Packet out_packet;
|
2017-07-15 20:24:11 +01:00
|
|
|
out_packet << static_cast<u8>(IdChatMessage);
|
2017-07-09 11:26:03 +01:00
|
|
|
out_packet << sending_member->nickname;
|
2018-10-27 08:35:01 +01:00
|
|
|
out_packet << sending_member->user_data.username;
|
2017-07-09 11:26:03 +01:00
|
|
|
out_packet << message;
|
|
|
|
|
|
|
|
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
2018-03-11 10:58:43 +00:00
|
|
|
bool sent_packet = false;
|
2017-07-14 08:20:39 +01:00
|
|
|
for (const auto& member : members) {
|
2018-03-11 10:58:43 +00:00
|
|
|
if (member.peer != event->peer) {
|
|
|
|
sent_packet = true;
|
2017-07-14 08:20:39 +01:00
|
|
|
enet_peer_send(member.peer, 0, enet_packet);
|
2018-03-11 10:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sent_packet) {
|
|
|
|
enet_packet_destroy(enet_packet);
|
2017-07-09 11:26:03 +01:00
|
|
|
}
|
2018-03-11 10:58:43 +00:00
|
|
|
|
2017-07-09 11:26:03 +01:00
|
|
|
enet_host_flush(server);
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:06:02 +01:00
|
|
|
void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) {
|
|
|
|
Packet in_packet;
|
|
|
|
in_packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
|
2018-08-12 23:55:15 +01:00
|
|
|
in_packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
2017-08-19 18:14:33 +01:00
|
|
|
GameInfo game_info;
|
|
|
|
in_packet >> game_info.name;
|
|
|
|
in_packet >> game_info.id;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
|
|
|
auto member =
|
|
|
|
std::find_if(members.begin(), members.end(), [event](const Member& member) -> bool {
|
|
|
|
return member.peer == event->peer;
|
|
|
|
});
|
|
|
|
if (member != members.end()) {
|
|
|
|
member->game_info = game_info;
|
|
|
|
}
|
2017-07-09 14:06:02 +01:00
|
|
|
}
|
2017-08-19 18:14:33 +01:00
|
|
|
BroadcastRoomInformation();
|
2017-07-09 14:06:02 +01:00
|
|
|
}
|
|
|
|
|
2017-07-09 09:43:53 +01:00
|
|
|
void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) {
|
|
|
|
// Remove the client from the members list.
|
2018-11-09 13:55:57 +00:00
|
|
|
std::string nickname, username;
|
2017-08-19 18:14:33 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(member_mutex);
|
2018-11-09 13:55:57 +00:00
|
|
|
auto member = std::find_if(members.begin(), members.end(), [client](const Member& member) {
|
|
|
|
return member.peer == client;
|
|
|
|
});
|
|
|
|
if (member != members.end()) {
|
|
|
|
nickname = member->nickname;
|
|
|
|
username = member->user_data.username;
|
|
|
|
members.erase(member);
|
|
|
|
}
|
2017-08-19 18:14:33 +01:00
|
|
|
}
|
2017-07-09 09:43:53 +01:00
|
|
|
|
|
|
|
// Announce the change to all clients.
|
2017-07-09 14:06:02 +01:00
|
|
|
enet_peer_disconnect(client, 0);
|
2018-11-09 13:55:57 +00:00
|
|
|
if (!nickname.empty())
|
|
|
|
SendStatusMessage(IdMemberLeave, nickname, username);
|
2017-07-09 09:43:53 +01:00
|
|
|
BroadcastRoomInformation();
|
|
|
|
}
|
|
|
|
|
2017-07-08 14:24:47 +01:00
|
|
|
// Room
|
2017-07-07 20:34:15 +01:00
|
|
|
Room::Room() : room_impl{std::make_unique<RoomImpl>()} {}
|
|
|
|
|
|
|
|
Room::~Room() = default;
|
|
|
|
|
2018-04-30 08:40:51 +01:00
|
|
|
bool Room::Create(const std::string& name, const std::string& description,
|
|
|
|
const std::string& server_address, u16 server_port, const std::string& password,
|
2018-11-24 08:08:32 +00:00
|
|
|
const u32 max_connections, const std::string& host_username,
|
|
|
|
const std::string& preferred_game, u64 preferred_game_id,
|
|
|
|
std::unique_ptr<VerifyUser::Backend> verify_backend,
|
2018-12-15 14:47:07 +00:00
|
|
|
const Room::BanList& ban_list, bool enable_citra_mods) {
|
2017-07-07 20:34:15 +01:00
|
|
|
ENetAddress address;
|
|
|
|
address.host = ENET_HOST_ANY;
|
2017-07-14 08:20:39 +01:00
|
|
|
if (!server_address.empty()) {
|
|
|
|
enet_address_set_host(&address, server_address.c_str());
|
|
|
|
}
|
2017-07-07 20:34:15 +01:00
|
|
|
address.port = server_port;
|
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
// In order to send the room is full message to the connecting client, we need to leave one
|
|
|
|
// slot open so enet won't reject the incoming connection without telling us
|
2018-04-20 08:34:37 +01:00
|
|
|
room_impl->server = enet_host_create(&address, max_connections + 1, NumChannels, 0, 0);
|
2017-11-19 18:52:37 +00:00
|
|
|
if (!room_impl->server) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-07 20:34:15 +01:00
|
|
|
room_impl->state = State::Open;
|
|
|
|
|
|
|
|
room_impl->room_information.name = name;
|
2018-04-30 08:40:51 +01:00
|
|
|
room_impl->room_information.description = description;
|
2017-11-19 18:52:37 +00:00
|
|
|
room_impl->room_information.member_slots = max_connections;
|
|
|
|
room_impl->room_information.port = server_port;
|
|
|
|
room_impl->room_information.preferred_game = preferred_game;
|
|
|
|
room_impl->room_information.preferred_game_id = preferred_game_id;
|
2018-11-24 08:08:32 +00:00
|
|
|
room_impl->room_information.host_username = host_username;
|
2018-12-15 14:47:07 +00:00
|
|
|
room_impl->room_information.enable_citra_mods = enable_citra_mods;
|
2017-11-19 18:52:37 +00:00
|
|
|
room_impl->password = password;
|
2018-10-27 08:35:01 +01:00
|
|
|
room_impl->verify_backend = std::move(verify_backend);
|
2018-11-24 08:08:32 +00:00
|
|
|
room_impl->username_ban_list = ban_list.first;
|
|
|
|
room_impl->ip_ban_list = ban_list.second;
|
2017-11-19 18:52:37 +00:00
|
|
|
|
2017-07-08 14:24:47 +01:00
|
|
|
room_impl->StartLoop();
|
2017-11-19 18:52:37 +00:00
|
|
|
return true;
|
2017-07-07 20:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Room::State Room::GetState() const {
|
|
|
|
return room_impl->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RoomInformation& Room::GetRoomInformation() const {
|
|
|
|
return room_impl->room_information;
|
|
|
|
}
|
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
std::string Room::GetVerifyUID() const {
|
|
|
|
std::lock_guard<std::mutex> lock(room_impl->verify_UID_mutex);
|
|
|
|
return room_impl->verify_UID;
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:08:32 +00:00
|
|
|
Room::BanList Room::GetBanList() const {
|
|
|
|
std::lock_guard<std::mutex> lock(room_impl->ban_list_mutex);
|
|
|
|
return {room_impl->username_ban_list, room_impl->ip_ban_list};
|
|
|
|
}
|
|
|
|
|
2017-08-19 18:14:33 +01:00
|
|
|
std::vector<Room::Member> Room::GetRoomMemberList() const {
|
|
|
|
std::vector<Room::Member> member_list;
|
|
|
|
std::lock_guard<std::mutex> lock(room_impl->member_mutex);
|
|
|
|
for (const auto& member_impl : room_impl->members) {
|
|
|
|
Member member;
|
|
|
|
member.nickname = member_impl.nickname;
|
2018-10-27 08:35:01 +01:00
|
|
|
member.username = member_impl.user_data.username;
|
|
|
|
member.display_name = member_impl.user_data.display_name;
|
|
|
|
member.avatar_url = member_impl.user_data.avatar_url;
|
2017-08-19 18:14:33 +01:00
|
|
|
member.mac_address = member_impl.mac_address;
|
|
|
|
member.game_info = member_impl.game_info;
|
|
|
|
member_list.push_back(member);
|
|
|
|
}
|
|
|
|
return member_list;
|
2017-11-19 18:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Room::HasPassword() const {
|
|
|
|
return !room_impl->password.empty();
|
|
|
|
}
|
2017-08-19 18:14:33 +01:00
|
|
|
|
2018-10-27 08:35:01 +01:00
|
|
|
void Room::SetVerifyUID(const std::string& uid) {
|
|
|
|
std::lock_guard<std::mutex> lock(room_impl->verify_UID_mutex);
|
|
|
|
room_impl->verify_UID = uid;
|
|
|
|
}
|
|
|
|
|
2017-07-07 20:34:15 +01:00
|
|
|
void Room::Destroy() {
|
|
|
|
room_impl->state = State::Closed;
|
2017-07-08 14:24:47 +01:00
|
|
|
room_impl->room_thread->join();
|
|
|
|
room_impl->room_thread.reset();
|
2017-07-07 20:34:15 +01:00
|
|
|
|
|
|
|
if (room_impl->server) {
|
|
|
|
enet_host_destroy(room_impl->server);
|
|
|
|
}
|
|
|
|
room_impl->room_information = {};
|
|
|
|
room_impl->server = nullptr;
|
2017-08-19 18:14:33 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(room_impl->member_mutex);
|
|
|
|
room_impl->members.clear();
|
|
|
|
}
|
2017-07-09 14:06:02 +01:00
|
|
|
room_impl->room_information.member_slots = 0;
|
|
|
|
room_impl->room_information.name.clear();
|
2017-07-07 20:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Network
|