diff options
Diffstat (limited to 'src/network/room_member.cpp')
| -rw-r--r-- | src/network/room_member.cpp | 123 |
1 files changed, 67 insertions, 56 deletions
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp index e4f823e98..9f08bf611 100644 --- a/src/network/room_member.cpp +++ b/src/network/room_member.cpp | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | #include <set> | 7 | #include <set> |
| 8 | #include <thread> | 8 | #include <thread> |
| 9 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| 10 | #include "common/socket_types.h" | ||
| 10 | #include "enet/enet.h" | 11 | #include "enet/enet.h" |
| 11 | #include "network/packet.h" | 12 | #include "network/packet.h" |
| 12 | #include "network/room_member.h" | 13 | #include "network/room_member.h" |
| @@ -38,7 +39,7 @@ public: | |||
| 38 | std::string username; ///< The username of this member. | 39 | std::string username; ///< The username of this member. |
| 39 | mutable std::mutex username_mutex; ///< Mutex for locking username. | 40 | mutable std::mutex username_mutex; ///< Mutex for locking username. |
| 40 | 41 | ||
| 41 | MacAddress mac_address; ///< The mac_address of this member. | 42 | IPv4Address fake_ip; ///< The fake ip of this member. |
| 42 | 43 | ||
| 43 | std::mutex network_mutex; ///< Mutex that controls access to the `client` variable. | 44 | std::mutex network_mutex; ///< Mutex that controls access to the `client` variable. |
| 44 | /// Thread that receives and dispatches network packets | 45 | /// Thread that receives and dispatches network packets |
| @@ -56,7 +57,7 @@ public: | |||
| 56 | CallbackSet<T>& Get(); | 57 | CallbackSet<T>& Get(); |
| 57 | 58 | ||
| 58 | private: | 59 | private: |
| 59 | CallbackSet<WifiPacket> callback_set_wifi_packet; | 60 | CallbackSet<ProxyPacket> callback_set_proxy_packet; |
| 60 | CallbackSet<ChatEntry> callback_set_chat_messages; | 61 | CallbackSet<ChatEntry> callback_set_chat_messages; |
| 61 | CallbackSet<StatusMessageEntry> callback_set_status_messages; | 62 | CallbackSet<StatusMessageEntry> callback_set_status_messages; |
| 62 | CallbackSet<RoomInformation> callback_set_room_information; | 63 | CallbackSet<RoomInformation> callback_set_room_information; |
| @@ -78,15 +79,15 @@ public: | |||
| 78 | 79 | ||
| 79 | /** | 80 | /** |
| 80 | * Sends a request to the server, asking for permission to join a room with the specified | 81 | * Sends a request to the server, asking for permission to join a room with the specified |
| 81 | * nickname and preferred mac. | 82 | * nickname and preferred fake ip. |
| 82 | * @params nickname The desired nickname. | 83 | * @params nickname The desired nickname. |
| 83 | * @params console_id_hash A hash of the Console ID. | 84 | * @params preferred_fake_ip The preferred IP address to use in the room, the NoPreferredIP |
| 84 | * @params preferred_mac The preferred MAC address to use in the room, the NoPreferredMac tells | 85 | * tells |
| 85 | * @params password The password for the room | 86 | * @params password The password for the room |
| 86 | * the server to assign one for us. | 87 | * the server to assign one for us. |
| 87 | */ | 88 | */ |
| 88 | void SendJoinRequest(const std::string& nickname_, const std::string& console_id_hash, | 89 | void SendJoinRequest(const std::string& nickname_, |
| 89 | const MacAddress& preferred_mac = NoPreferredMac, | 90 | const IPv4Address& preferred_fake_ip = NoPreferredIP, |
| 90 | const std::string& password = "", const std::string& token = ""); | 91 | const std::string& password = "", const std::string& token = ""); |
| 91 | 92 | ||
| 92 | /** | 93 | /** |
| @@ -101,10 +102,10 @@ public: | |||
| 101 | void HandleRoomInformationPacket(const ENetEvent* event); | 102 | void HandleRoomInformationPacket(const ENetEvent* event); |
| 102 | 103 | ||
| 103 | /** | 104 | /** |
| 104 | * Extracts a WifiPacket from a received ENet packet. | 105 | * Extracts a ProxyPacket from a received ENet packet. |
| 105 | * @param event The ENet event that was received. | 106 | * @param event The ENet event that was received. |
| 106 | */ | 107 | */ |
| 107 | void HandleWifiPackets(const ENetEvent* event); | 108 | void HandleProxyPackets(const ENetEvent* event); |
| 108 | 109 | ||
| 109 | /** | 110 | /** |
| 110 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. | 111 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. |
| @@ -158,12 +159,12 @@ void RoomMember::RoomMemberImpl::MemberLoop() { | |||
| 158 | while (IsConnected()) { | 159 | while (IsConnected()) { |
| 159 | std::lock_guard lock(network_mutex); | 160 | std::lock_guard lock(network_mutex); |
| 160 | ENetEvent event; | 161 | ENetEvent event; |
| 161 | if (enet_host_service(client, &event, 16) > 0) { | 162 | if (enet_host_service(client, &event, 100) > 0) { |
| 162 | switch (event.type) { | 163 | switch (event.type) { |
| 163 | case ENET_EVENT_TYPE_RECEIVE: | 164 | case ENET_EVENT_TYPE_RECEIVE: |
| 164 | switch (event.packet->data[0]) { | 165 | switch (event.packet->data[0]) { |
| 165 | case IdWifiPacket: | 166 | case IdProxyPacket: |
| 166 | HandleWifiPackets(&event); | 167 | HandleProxyPackets(&event); |
| 167 | break; | 168 | break; |
| 168 | case IdChatMessage: | 169 | case IdChatMessage: |
| 169 | HandleChatPacket(&event); | 170 | HandleChatPacket(&event); |
| @@ -198,13 +199,9 @@ void RoomMember::RoomMemberImpl::MemberLoop() { | |||
| 198 | SetState(State::Idle); | 199 | SetState(State::Idle); |
| 199 | SetError(Error::NameCollision); | 200 | SetError(Error::NameCollision); |
| 200 | break; | 201 | break; |
| 201 | case IdMacCollision: | 202 | case IdIpCollision: |
| 202 | SetState(State::Idle); | 203 | SetState(State::Idle); |
| 203 | SetError(Error::MacCollision); | 204 | SetError(Error::IpCollision); |
| 204 | break; | ||
| 205 | case IdConsoleIdCollision: | ||
| 206 | SetState(State::Idle); | ||
| 207 | SetError(Error::ConsoleIdCollision); | ||
| 208 | break; | 205 | break; |
| 209 | case IdVersionMismatch: | 206 | case IdVersionMismatch: |
| 210 | SetState(State::Idle); | 207 | SetState(State::Idle); |
| @@ -275,15 +272,13 @@ void RoomMember::RoomMemberImpl::Send(Packet&& packet) { | |||
| 275 | } | 272 | } |
| 276 | 273 | ||
| 277 | void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname_, | 274 | void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname_, |
| 278 | const std::string& console_id_hash, | 275 | const IPv4Address& preferred_fake_ip, |
| 279 | const MacAddress& preferred_mac, | ||
| 280 | const std::string& password, | 276 | const std::string& password, |
| 281 | const std::string& token) { | 277 | const std::string& token) { |
| 282 | Packet packet; | 278 | Packet packet; |
| 283 | packet.Write(static_cast<u8>(IdJoinRequest)); | 279 | packet.Write(static_cast<u8>(IdJoinRequest)); |
| 284 | packet.Write(nickname_); | 280 | packet.Write(nickname_); |
| 285 | packet.Write(console_id_hash); | 281 | packet.Write(preferred_fake_ip); |
| 286 | packet.Write(preferred_mac); | ||
| 287 | packet.Write(network_version); | 282 | packet.Write(network_version); |
| 288 | packet.Write(password); | 283 | packet.Write(password); |
| 289 | packet.Write(token); | 284 | packet.Write(token); |
| @@ -317,7 +312,7 @@ void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* ev | |||
| 317 | 312 | ||
| 318 | for (auto& member : member_information) { | 313 | for (auto& member : member_information) { |
| 319 | packet.Read(member.nickname); | 314 | packet.Read(member.nickname); |
| 320 | packet.Read(member.mac_address); | 315 | packet.Read(member.fake_ip); |
| 321 | packet.Read(member.game_info.name); | 316 | packet.Read(member.game_info.name); |
| 322 | packet.Read(member.game_info.id); | 317 | packet.Read(member.game_info.id); |
| 323 | packet.Read(member.username); | 318 | packet.Read(member.username); |
| @@ -342,29 +337,38 @@ void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) { | |||
| 342 | packet.IgnoreBytes(sizeof(u8)); // Ignore the message type | 337 | packet.IgnoreBytes(sizeof(u8)); // Ignore the message type |
| 343 | 338 | ||
| 344 | // Parse the MAC Address from the packet | 339 | // Parse the MAC Address from the packet |
| 345 | packet.Read(mac_address); | 340 | packet.Read(fake_ip); |
| 346 | } | 341 | } |
| 347 | 342 | ||
| 348 | void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) { | 343 | void RoomMember::RoomMemberImpl::HandleProxyPackets(const ENetEvent* event) { |
| 349 | WifiPacket wifi_packet{}; | 344 | ProxyPacket proxy_packet{}; |
| 350 | Packet packet; | 345 | Packet packet; |
| 351 | packet.Append(event->packet->data, event->packet->dataLength); | 346 | packet.Append(event->packet->data, event->packet->dataLength); |
| 352 | 347 | ||
| 353 | // Ignore the first byte, which is the message id. | 348 | // Ignore the first byte, which is the message id. |
| 354 | packet.IgnoreBytes(sizeof(u8)); // Ignore the message type | 349 | packet.IgnoreBytes(sizeof(u8)); // Ignore the message type |
| 355 | 350 | ||
| 356 | // Parse the WifiPacket from the packet | 351 | // Parse the ProxyPacket from the packet |
| 357 | u8 frame_type; | 352 | u8 local_family; |
| 358 | packet.Read(frame_type); | 353 | packet.Read(local_family); |
| 359 | WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type); | 354 | proxy_packet.local_endpoint.family = static_cast<Domain>(local_family); |
| 355 | packet.Read(proxy_packet.local_endpoint.ip); | ||
| 356 | packet.Read(proxy_packet.local_endpoint.portno); | ||
| 357 | |||
| 358 | u8 remote_family; | ||
| 359 | packet.Read(remote_family); | ||
| 360 | proxy_packet.remote_endpoint.family = static_cast<Domain>(remote_family); | ||
| 361 | packet.Read(proxy_packet.remote_endpoint.ip); | ||
| 362 | packet.Read(proxy_packet.remote_endpoint.portno); | ||
| 363 | |||
| 364 | u8 protocol_type; | ||
| 365 | packet.Read(protocol_type); | ||
| 366 | proxy_packet.protocol = static_cast<Protocol>(protocol_type); | ||
| 360 | 367 | ||
| 361 | wifi_packet.type = type; | 368 | packet.Read(proxy_packet.broadcast); |
| 362 | packet.Read(wifi_packet.channel); | 369 | packet.Read(proxy_packet.data); |
| 363 | packet.Read(wifi_packet.transmitter_address); | ||
| 364 | packet.Read(wifi_packet.destination_address); | ||
| 365 | packet.Read(wifi_packet.data); | ||
| 366 | 370 | ||
| 367 | Invoke<WifiPacket>(wifi_packet); | 371 | Invoke<ProxyPacket>(proxy_packet); |
| 368 | } | 372 | } |
| 369 | 373 | ||
| 370 | void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) { | 374 | void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) { |
| @@ -440,8 +444,8 @@ void RoomMember::RoomMemberImpl::Disconnect() { | |||
| 440 | } | 444 | } |
| 441 | 445 | ||
| 442 | template <> | 446 | template <> |
| 443 | RoomMember::RoomMemberImpl::CallbackSet<WifiPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() { | 447 | RoomMember::RoomMemberImpl::CallbackSet<ProxyPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() { |
| 444 | return callback_set_wifi_packet; | 448 | return callback_set_proxy_packet; |
| 445 | } | 449 | } |
| 446 | 450 | ||
| 447 | template <> | 451 | template <> |
| @@ -525,19 +529,18 @@ const std::string& RoomMember::GetUsername() const { | |||
| 525 | return room_member_impl->username; | 529 | return room_member_impl->username; |
| 526 | } | 530 | } |
| 527 | 531 | ||
| 528 | const MacAddress& RoomMember::GetMacAddress() const { | 532 | const IPv4Address& RoomMember::GetFakeIpAddress() const { |
| 529 | ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected"); | 533 | ASSERT_MSG(IsConnected(), "Tried to get fake ip address while not connected"); |
| 530 | return room_member_impl->mac_address; | 534 | return room_member_impl->fake_ip; |
| 531 | } | 535 | } |
| 532 | 536 | ||
| 533 | RoomInformation RoomMember::GetRoomInformation() const { | 537 | RoomInformation RoomMember::GetRoomInformation() const { |
| 534 | return room_member_impl->room_information; | 538 | return room_member_impl->room_information; |
| 535 | } | 539 | } |
| 536 | 540 | ||
| 537 | void RoomMember::Join(const std::string& nick, const std::string& console_id_hash, | 541 | void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port, |
| 538 | const char* server_addr, u16 server_port, u16 client_port, | 542 | u16 client_port, const IPv4Address& preferred_fake_ip, |
| 539 | const MacAddress& preferred_mac, const std::string& password, | 543 | const std::string& password, const std::string& token) { |
| 540 | const std::string& token) { | ||
| 541 | // If the member is connected, kill the connection first | 544 | // If the member is connected, kill the connection first |
| 542 | if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) { | 545 | if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) { |
| 543 | Leave(); | 546 | Leave(); |
| @@ -571,7 +574,7 @@ void RoomMember::Join(const std::string& nick, const std::string& console_id_has | |||
| 571 | if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { | 574 | if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { |
| 572 | room_member_impl->nickname = nick; | 575 | room_member_impl->nickname = nick; |
| 573 | room_member_impl->StartLoop(); | 576 | room_member_impl->StartLoop(); |
| 574 | room_member_impl->SendJoinRequest(nick, console_id_hash, preferred_mac, password, token); | 577 | room_member_impl->SendJoinRequest(nick, preferred_fake_ip, password, token); |
| 575 | SendGameInfo(room_member_impl->current_game_info); | 578 | SendGameInfo(room_member_impl->current_game_info); |
| 576 | } else { | 579 | } else { |
| 577 | enet_peer_disconnect(room_member_impl->server, 0); | 580 | enet_peer_disconnect(room_member_impl->server, 0); |
| @@ -584,14 +587,22 @@ bool RoomMember::IsConnected() const { | |||
| 584 | return room_member_impl->IsConnected(); | 587 | return room_member_impl->IsConnected(); |
| 585 | } | 588 | } |
| 586 | 589 | ||
| 587 | void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) { | 590 | void RoomMember::SendProxyPacket(const ProxyPacket& proxy_packet) { |
| 588 | Packet packet; | 591 | Packet packet; |
| 589 | packet.Write(static_cast<u8>(IdWifiPacket)); | 592 | packet.Write(static_cast<u8>(IdProxyPacket)); |
| 590 | packet.Write(static_cast<u8>(wifi_packet.type)); | 593 | |
| 591 | packet.Write(wifi_packet.channel); | 594 | packet.Write(static_cast<u8>(proxy_packet.local_endpoint.family)); |
| 592 | packet.Write(wifi_packet.transmitter_address); | 595 | packet.Write(proxy_packet.local_endpoint.ip); |
| 593 | packet.Write(wifi_packet.destination_address); | 596 | packet.Write(proxy_packet.local_endpoint.portno); |
| 594 | packet.Write(wifi_packet.data); | 597 | |
| 598 | packet.Write(static_cast<u8>(proxy_packet.remote_endpoint.family)); | ||
| 599 | packet.Write(proxy_packet.remote_endpoint.ip); | ||
| 600 | packet.Write(proxy_packet.remote_endpoint.portno); | ||
| 601 | |||
| 602 | packet.Write(static_cast<u8>(proxy_packet.protocol)); | ||
| 603 | packet.Write(proxy_packet.broadcast); | ||
| 604 | packet.Write(proxy_packet.data); | ||
| 605 | |||
| 595 | room_member_impl->Send(std::move(packet)); | 606 | room_member_impl->Send(std::move(packet)); |
| 596 | } | 607 | } |
| 597 | 608 | ||
| @@ -645,8 +656,8 @@ RoomMember::CallbackHandle<RoomMember::Error> RoomMember::BindOnError( | |||
| 645 | return room_member_impl->Bind(callback); | 656 | return room_member_impl->Bind(callback); |
| 646 | } | 657 | } |
| 647 | 658 | ||
| 648 | RoomMember::CallbackHandle<WifiPacket> RoomMember::BindOnWifiPacketReceived( | 659 | RoomMember::CallbackHandle<ProxyPacket> RoomMember::BindOnProxyPacketReceived( |
| 649 | std::function<void(const WifiPacket&)> callback) { | 660 | std::function<void(const ProxyPacket&)> callback) { |
| 650 | return room_member_impl->Bind(callback); | 661 | return room_member_impl->Bind(callback); |
| 651 | } | 662 | } |
| 652 | 663 | ||
| @@ -685,7 +696,7 @@ void RoomMember::Leave() { | |||
| 685 | room_member_impl->client = nullptr; | 696 | room_member_impl->client = nullptr; |
| 686 | } | 697 | } |
| 687 | 698 | ||
| 688 | template void RoomMember::Unbind(CallbackHandle<WifiPacket>); | 699 | template void RoomMember::Unbind(CallbackHandle<ProxyPacket>); |
| 689 | template void RoomMember::Unbind(CallbackHandle<RoomMember::State>); | 700 | template void RoomMember::Unbind(CallbackHandle<RoomMember::State>); |
| 690 | template void RoomMember::Unbind(CallbackHandle<RoomMember::Error>); | 701 | template void RoomMember::Unbind(CallbackHandle<RoomMember::Error>); |
| 691 | template void RoomMember::Unbind(CallbackHandle<RoomInformation>); | 702 | template void RoomMember::Unbind(CallbackHandle<RoomInformation>); |