1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Basic Lan Implmentation

This commit is contained in:
DustyBagel 2024-06-21 16:33:44 -05:00
parent bc23a610d3
commit f03922f218
26 changed files with 1414 additions and 2 deletions

View file

@ -2,6 +2,7 @@ set(common_network_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/address.cpp
${CMAKE_CURRENT_SOURCE_DIR}/connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/connectionthreads.cpp
${CMAKE_CURRENT_SOURCE_DIR}/lan.cpp
${CMAKE_CURRENT_SOURCE_DIR}/networkpacket.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serverpackethandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serveropcodes.cpp

View file

@ -46,6 +46,9 @@ public:
Address(u32 address, u16 port);
Address(u8 a, u8 b, u8 c, u8 d, u16 port);
Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
Address(const in6_addr & addr, u16 port) { setAddress(addr); setPort(port); };
Address(const sockaddr_in6 & sai) { m_address.ipv6 = sai.sin6_addr; m_addr_family = sai.sin6_family; m_port = ntohs(sai.sin6_port); };
Address(const sockaddr_in & sai) { m_address.ipv4 = sai.sin_addr; m_addr_family = sai.sin_family; m_port = ntohs(sai.sin_port); };
bool operator==(const Address &address) const;
bool operator!=(const Address &address) const { return !(*this == address); }
@ -74,6 +77,7 @@ public:
void setAddress(u32 address);
void setAddress(u8 a, u8 b, u8 c, u8 d);
void setAddress(const IPv6AddressBytes *ipv6_bytes);
void setAddress(const in6_addr & addr) { m_address.ipv6 = addr; m_addr_family = AF_INET6;}
void setPort(u16 port);
private:

296
src/network/lan.cpp Normal file
View file

@ -0,0 +1,296 @@
/*
Copyright (C) 2016 proller <proler@gmail.com>
*/
/*
This file is part of Freeminer.
Freeminer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freeminer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "network/lan.h"
#include <cstdint>
#include "convert_json.h"
#include "socket.h"
#include "log.h"
#include "settings.h"
#include "version.h"
#include "networkprotocol.h"
#include "server/serverlist.h"
#include "debug.h"
#include "json/json.h"
#include "porting.h"
#include "threading/thread_vector.h"
#include "threading/concurrent_map.h"
#include "network/address.h"
//copypaste from ../socket.cpp
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
// Without this some of the network functions are not found on mingw
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define LAST_SOCKET_ERR() WSAGetLastError()
typedef SOCKET socket_t;
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#ifndef __ANDROID__
#include <ifaddrs.h>
#define HAVE_IFADDRS 1
#endif
#define LAST_SOCKET_ERR() (errno)
typedef int socket_t;
#endif
const static unsigned short int adv_port = 29998;
static std::string ask_str;
lan_adv::lan_adv() : thread_vector("lan_adv")
{
}
void lan_adv::ask()
{
reanimate();
if (ask_str.empty()) {
Json::Value j;
j["cmd"] = "ask";
ask_str = fastWriteJson(j);
}
send_string(ask_str);
}
void lan_adv::send_string(const std::string &str)
{
try {
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(adv_port);
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
UDPSocket socket_send(false);
int set_option_on = 1;
setsockopt(socket_send.GetHandle(), SOL_SOCKET, SO_BROADCAST,
(const char *)&set_option_on, sizeof(set_option_on));
socket_send.Send(Address(addr), str.c_str(), str.size());
} catch (const std::exception &e) {
verbosestream << "udp broadcast send4 fail " << e.what() << "\n";
}
std::vector<uint32_t> scopes;
// todo: windows and android
#if HAVE_IFADDRS
struct ifaddrs *ifaddr = nullptr, *ifa = nullptr;
if (getifaddrs(&ifaddr) < 0) {
} else {
for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr)
continue;
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
auto sa = *((struct sockaddr_in6 *)ifa->ifa_addr);
if (sa.sin6_scope_id)
scopes.push_back(sa.sin6_scope_id);
/*errorstream<<"in=" << ifa->ifa_name << " a="<<Address(*((struct sockaddr_in6*)ifa->ifa_addr)).serializeString()<<" ba=" << ifa->ifa_broadaddr <<" sc=" << sa.sin6_scope_id <<" fl=" << ifa->ifa_flags
//<< " bn=" << Address(*((struct sockaddr_in6*)ifa->ifa_broadaddr)).serializeString()
<<"\n"; */
}
}
freeifaddrs(ifaddr);
#endif
if (scopes.empty())
scopes.push_back(0);
struct addrinfo hints
{
};
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
struct addrinfo *result = nullptr;
if (!getaddrinfo("ff02::1", nullptr, &hints, &result)) {
for (auto info = result; info; info = info->ai_next) {
try {
sockaddr_in6 addr = *((struct sockaddr_in6 *)info->ai_addr);
addr.sin6_port = htons(adv_port);
UDPSocket socket_send(true);
int set_option_on = 1;
setsockopt(socket_send.GetHandle(), SOL_SOCKET, SO_BROADCAST,
(const char *)&set_option_on, sizeof(set_option_on));
auto use_scopes = scopes;
if (addr.sin6_scope_id) {
use_scopes.clear();
use_scopes.push_back(addr.sin6_scope_id);
}
for (auto &scope : use_scopes) {
addr.sin6_scope_id = scope;
socket_send.Send(Address(addr), str.c_str(), str.size());
}
} catch (const std::exception &e) {
verbosestream << "udp broadcast send6 fail " << e.what() << "\n";
}
}
freeaddrinfo(result);
}
}
void lan_adv::serve(unsigned short port)
{
server_port = port;
restart();
}
void *lan_adv::run()
{
BEGIN_DEBUG_EXCEPTION_HANDLER;
reg("LanAdv" + (server_port ? std::string("Server") : std::string("Client")));
UDPSocket socket_recv(true);
int set_option_off = 0, set_option_on = 1;
setsockopt(socket_recv.GetHandle(), SOL_SOCKET, SO_REUSEADDR,
(const char *)&set_option_on, sizeof(set_option_on));
#ifdef SO_REUSEPORT
setsockopt(socket_recv.GetHandle(), SOL_SOCKET, SO_REUSEPORT,
(const char *)&set_option_on, sizeof(set_option_on));
#endif
setsockopt(socket_recv.GetHandle(), SOL_SOCKET, SO_BROADCAST,
(const char *)&set_option_on, sizeof(set_option_on));
setsockopt(socket_recv.GetHandle(), IPPROTO_IPV6, IPV6_V6ONLY,
(const char *)&set_option_off, sizeof(set_option_off));
socket_recv.setTimeoutMs(200);
try {
socket_recv.Bind(Address(in6addr_any, adv_port));
} catch (const std::exception &e) {
warningstream << m_name << ": cant bind ipv6 address [" << e.what()
<< "], trying ipv4. " << std::endl;
try {
socket_recv.Bind(Address((u32)INADDR_ANY, adv_port));
} catch (const std::exception &e) {
warningstream << m_name << ": cant bind ipv4 too [" << e.what() << "]"
<< std::endl;
return nullptr;
}
}
std::unordered_map<std::string, uint64_t> limiter;
const unsigned int packet_maxsize = 16384;
char buffer[packet_maxsize];
Json::Reader reader;
std::string answer_str;
Json::Value server;
if (server_port) {
server["name"] = g_settings->get("server_name");
server["description"] = g_settings->get("server_description");
server["version"] = g_version_string;
bool strict_checking = g_settings->getBool("strict_protocol_version_checking");
server["proto_min"] =
strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MIN;
server["proto_max"] =
strict_checking ? LATEST_PROTOCOL_VERSION : SERVER_PROTOCOL_VERSION_MAX;
server["url"] = g_settings->get("server_url");
server["creative"] = g_settings->getBool("creative_mode");
server["damage"] = g_settings->getBool("enable_damage");
server["password"] = g_settings->getBool("disallow_empty_password");
server["pvp"] = g_settings->getBool("enable_pvp");
server["port"] = server_port;
server["clients"] = clients_num.load();
server["clients_max"] = g_settings->getU16("max_users");
send_string(fastWriteJson(server));
}
while (!stopRequested()) {
BEGIN_DEBUG_EXCEPTION_HANDLER;
Address addr;
int rlen = socket_recv.Receive(addr, buffer, packet_maxsize);
if (rlen <= 0)
continue;
Json::Value p;
if (!reader.parse(std::string(buffer, rlen), p)) {
//errorstream << "cant parse "<< s << "\n";
continue;
}
auto addr_str = addr.serializeString();
auto now = porting::getTimeMs();
//errorstream << " a=" << addr.serializeString() << " : " << addr.getPort() << " l=" << rlen << " b=" << p << " ; server=" << server_port << "\n";
if (server_port) {
if (p["cmd"] == "ask" && limiter[addr_str] < now) {
(clients_num.load() ? infostream : actionstream)
<< "lan: want play " << addr_str << std::endl;
server["clients"] = clients_num.load();
answer_str = fastWriteJson(server);
limiter[addr_str] = now + 3000;
UDPSocket socket_send(true);
addr.setPort(adv_port);
socket_send.Send(addr, answer_str.c_str(), answer_str.size());
}
} else {
if (p["cmd"] == "ask") {
actionstream << "lan: want play " << addr_str << std::endl;
}
if (p["port"].isInt()) {
p["address"] = addr_str;
auto key = addr_str + ":" + p["port"].asString();
if (p["cmd"].asString() == "shutdown") {
//infostream << "server shutdown " << key << "\n";
collected.erase(key);
fresh = true;
} else {
if (!collected.count(key))
actionstream << "lan server start " << key << "\n";
collected.insert_or_assign(key, p);
fresh = true;
}
}
//errorstream<<" current list: ";for (auto & i : collected) {errorstream<< i.first <<" ; ";}errorstream<<std::endl;
}
END_DEBUG_EXCEPTION_HANDLER;
}
if (server_port) {
Json::Value answer_json;
answer_json["port"] = server_port;
answer_json["cmd"] = "shutdown";
send_string(fastWriteJson(answer_json));
}
END_DEBUG_EXCEPTION_HANDLER;
return nullptr;
}

46
src/network/lan.h Normal file
View file

@ -0,0 +1,46 @@
/*
Copyright (C) 2016 proller <proler@gmail.com>
This file is part of Freeminer.
Freeminer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freeminer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <json/value.h>
#include <string>
#include <atomic>
#include "threading/thread_vector.h"
#include "threading/concurrent_map.h"
class lan_adv : public thread_vector
{
public:
void *run();
lan_adv();
void ask();
void send_string(const std::string &str);
void serve(unsigned short port);
concurrent_map<std::string, Json::Value> collected;
std::atomic_bool fresh;
std::atomic_int clients_num;
private:
unsigned short server_port = 0;
};