Implement ethernet address determination

This commit is contained in:
RipleyTom 2020-10-11 05:02:33 +02:00 committed by Ivan
parent 1393bb2caf
commit 9e14d240a8
6 changed files with 118 additions and 7 deletions

View file

@ -107,7 +107,7 @@ if(UNIX)
endif() endif()
if(WIN32) if(WIN32)
target_link_libraries(rpcs3 ws2_32.lib Winmm.lib Psapi.lib gdi32.lib setupapi.lib) target_link_libraries(rpcs3 ws2_32.lib Iphlpapi.lib Winmm.lib Psapi.lib gdi32.lib setupapi.lib)
else() else()
target_link_libraries(rpcs3 ${CMAKE_DL_LIBS}) target_link_libraries(rpcs3 ${CMAKE_DL_LIBS})
endif() endif()

View file

@ -193,8 +193,7 @@ error_code cellNetCtlGetInfo(s32 code, vm::ptr<CellNetCtlInfo> info)
if (code == CELL_NET_CTL_INFO_ETHER_ADDR) if (code == CELL_NET_CTL_INFO_ETHER_ADDR)
{ {
// dummy values set memcpy(info->ether_addr.data, nph->get_ether_addr().data(), 6);
std::memset(info->ether_addr.data, 0xFF, sizeof(info->ether_addr.data));
return CELL_OK; return CELL_OK;
} }

View file

@ -29,6 +29,7 @@
#include <chrono> #include <chrono>
LOG_CHANNEL(sys_net); LOG_CHANNEL(sys_net);
LOG_CHANNEL(sys_net_dump);
template<> template<>
void fmt_class_string<sys_net_error>::format(std::string& out, u64 arg) void fmt_class_string<sys_net_error>::format(std::string& out, u64 arg)
@ -177,7 +178,7 @@ void windows_poll(pollfd* fds, unsigned long nfds, int timeout, bool* connecting
// Error helper functions // Error helper functions
static int get_native_error() static int get_native_error()
{ {
int native_error = 0; int native_error;
#ifdef _WIN32 #ifdef _WIN32
native_error = WSAGetLastError(); native_error = WSAGetLastError();
#else #else
@ -2247,7 +2248,7 @@ error_code sys_net_bnet_recvfrom(ppu_thread& ppu, s32 s, vm::ptr<void> buf, u32
if (native_result >= 0) if (native_result >= 0)
{ {
if (sys_net.enabled == logs::level::trace) if (sys_net_dump.enabled == logs::level::trace)
{ {
std::string datrace; std::string datrace;
const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
@ -2263,7 +2264,7 @@ error_code sys_net_bnet_recvfrom(ppu_thread& ppu, s32 s, vm::ptr<void> buf, u32
datrace += hex[(dabuf[index]) & 15]; datrace += hex[(dabuf[index]) & 15];
datrace += ' '; datrace += ' ';
} }
sys_net.trace("DNS RESULT: %s", datrace); sys_net.trace("recvfrom dump: %s", datrace);
} }
return true; return true;

View file

@ -16,14 +16,21 @@
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include <iphlpapi.h>
#else #else
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef __FreeBSD__
#include <ifaddrs.h>
#include <net/if_dl.h>
#endif
LOG_CHANNEL(sys_net); LOG_CHANNEL(sys_net);
LOG_CHANNEL(sceNp2); LOG_CHANNEL(sceNp2);
LOG_CHANNEL(sceNp); LOG_CHANNEL(sceNp);
@ -47,6 +54,13 @@ np_handler::np_handler()
is_psn_active = false; is_psn_active = false;
} }
if (!discover_ether_address())
{
nph_log.error("Failed to discover ethernet address!");
is_connected = false;
is_psn_active = false;
}
// Convert dns address // Convert dns address
// TODO: recover actual user dns through OS specific API // TODO: recover actual user dns through OS specific API
in_addr conv{}; in_addr conv{};
@ -113,9 +127,97 @@ bool np_handler::discover_ip_address()
// Set public address to local discovered address for now, may be updated later; // Set public address to local discovered address for now, may be updated later;
public_ip_addr = local_ip_addr; public_ip_addr = local_ip_addr;
nph_log.notice("IP was determined to be %s", ip_to_string(local_ip_addr));
return true; return true;
} }
bool np_handler::discover_ether_address()
{
#ifdef __FreeBSD__
ifaddrs* ifap;
if (getifaddrs(&ifap) == 0)
{
ifaddrs* p;
for (p = ifap; p; p = p->ifa_next)
{
if (p->ifa_addr->sa_family == AF_LINK)
{
sockaddr_dl* sdp = reinterpret_cast<sockaddr_dl*>(p->ifa_addr);
memcpy(ether_address.data(), sdp->sdl_data + sdp->sdl_nlen, 6);
freeifaddrs(ifap);
nph_log.notice("Determined Ethernet address to be %s", ether_to_string(ether_address));
return true;
}
}
freeifaddrs(ifap);
}
#elif defined(_WIN32)
std::vector<u8> adapter_infos(sizeof(IP_ADAPTER_INFO));
ULONG size_infos = sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo(reinterpret_cast<PIP_ADAPTER_INFO>(adapter_infos.data()), &size_infos) == ERROR_BUFFER_OVERFLOW)
adapter_infos.resize(size_infos);
if (GetAdaptersInfo(reinterpret_cast<PIP_ADAPTER_INFO>(adapter_infos.data()), &size_infos) == NO_ERROR && size_infos)
{
PIP_ADAPTER_INFO info = reinterpret_cast<PIP_ADAPTER_INFO>(adapter_infos.data());
memcpy(ether_address.data(), info[0].Address, 6);
nph_log.notice("Determined Ethernet address to be %s", ether_to_string(ether_address));
return true;
}
#else
ifreq ifr;
ifconf ifc;
char buf[1024];
int success = 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1)
return false;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
return false;
ifreq* it = ifc.ifc_req;
const ifreq* const end = it + (ifc.ifc_len / sizeof(ifreq));
for (; it != end; ++it)
{
strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0)
{
if (!(ifr.ifr_flags & IFF_LOOPBACK))
{
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0)
{
success = 1;
break;
}
}
}
}
if (success)
{
memcpy(ether_address.data(), ifr.ifr_hwaddr.sa_data, 6);
nph_log.notice("Determined Ethernet address to be %s", ether_to_string(ether_address));
return true;
}
#endif
return false;
}
const std::array<u8, 6>& np_handler::get_ether_addr() const
{
return ether_address;
}
u32 np_handler::get_local_ip_addr() const u32 np_handler::get_local_ip_addr() const
{ {
return local_ip_addr; return local_ip_addr;
@ -172,6 +274,11 @@ std::string np_handler::ip_to_string(u32 ip_addr)
return result; return result;
} }
std::string np_handler::ether_to_string(std::array<u8, 6>& ether)
{
return fmt::format("%02X:%02X:%02X:%02X:%02X:%02X", ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
}
void np_handler::string_to_npid(const char* str, SceNpId* npid) void np_handler::string_to_npid(const char* str, SceNpId* npid)
{ {
strncpy(npid->handle.data, str, sizeof(npid->handle.data)); strncpy(npid->handle.data, str, sizeof(npid->handle.data));

View file

@ -17,6 +17,7 @@ class np_handler
public: public:
np_handler(); np_handler();
const std::array<u8, 6>& get_ether_addr() const;
u32 get_local_ip_addr() const; u32 get_local_ip_addr() const;
u32 get_public_ip_addr() const; u32 get_public_ip_addr() const;
u32 get_dns_ip() const; u32 get_dns_ip() const;
@ -31,6 +32,7 @@ public:
// Public helpers // Public helpers
static std::string ip_to_string(u32 addr); static std::string ip_to_string(u32 addr);
static std::string ether_to_string(std::array<u8, 6>& ether);
// Helpers for setting various structures from string // Helpers for setting various structures from string
static void string_to_npid(const char* str, SceNpId* npid); static void string_to_npid(const char* str, SceNpId* npid);
static void string_to_online_name(const char* str, SceNpOnlineName* online_name); static void string_to_online_name(const char* str, SceNpOnlineName* online_name);
@ -250,6 +252,7 @@ public:
protected: protected:
// Various generic helpers // Various generic helpers
bool discover_ip_address(); bool discover_ip_address();
bool discover_ether_address();
bool error_and_disconnect(const std::string& error_msg); bool error_and_disconnect(const std::string& error_msg);
// Notification handlers // Notification handlers
@ -301,6 +304,7 @@ protected:
std::vector<u8> current_ticket; std::vector<u8> current_ticket;
// IP & DNS info // IP & DNS info
std::array<u8, 6> ether_address{};
be_t<u32> local_ip_addr{}; be_t<u32> local_ip_addr{};
be_t<u32> public_ip_addr{}; be_t<u32> public_ip_addr{};
be_t<u32> dns_ip = 0x08080808; be_t<u32> dns_ip = 0x08080808;

View file

@ -23,7 +23,7 @@
<AdditionalOptions>/Zc:throwingNew /constexpr:steps16777216 %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/Zc:throwingNew /constexpr:steps16777216 %(AdditionalOptions)</AdditionalOptions>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>xxhash.lib;ws2_32.lib;Bcrypt.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib</AdditionalDependencies> <AdditionalDependencies>xxhash.lib;ws2_32.lib;Iphlpapi.lib;Bcrypt.lib;avcodec.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>..\3rdparty\ffmpeg\windows\x86_64</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>..\3rdparty\ffmpeg\windows\x86_64</AdditionalLibraryDirectories>
<StackReserveSize>8388608</StackReserveSize> <StackReserveSize>8388608</StackReserveSize>
<StackCommitSize>1048576</StackCommitSize> <StackCommitSize>1048576</StackCommitSize>