summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/sockets/sfdnsres.cpp
blob: 2e5666ca83cb664cd00cf04c60e0312bf04532ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <string_view>
#include <utility>
#include <vector>

#include "common/string_util.h"
#include "common/swap.h"
#include "core/core.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/service/sockets/sfdnsres.h"
#include "core/memory.h"

#ifdef _WIN32
#include <ws2tcpip.h>
#elif YUZU_UNIX
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#endif

namespace Service::Sockets {

SFDNSRES::SFDNSRES(Core::System& system_) : ServiceFramework{system_, "sfdnsres"} {
    static const FunctionInfo functions[] = {
        {0, nullptr, "SetDnsAddressesPrivateRequest"},
        {1, nullptr, "GetDnsAddressPrivateRequest"},
        {2, nullptr, "GetHostByNameRequest"},
        {3, nullptr, "GetHostByAddrRequest"},
        {4, nullptr, "GetHostStringErrorRequest"},
        {5, nullptr, "GetGaiStringErrorRequest"},
        {6, &SFDNSRES::GetAddrInfoRequest, "GetAddrInfoRequest"},
        {7, nullptr, "GetNameInfoRequest"},
        {8, nullptr, "RequestCancelHandleRequest"},
        {9, nullptr, "CancelRequest"},
        {10, nullptr, "GetHostByNameRequestWithOptions"},
        {11, nullptr, "GetHostByAddrRequestWithOptions"},
        {12, &SFDNSRES::GetAddrInfoRequestWithOptions, "GetAddrInfoRequestWithOptions"},
        {13, nullptr, "GetNameInfoRequestWithOptions"},
        {14, nullptr, "ResolverSetOptionRequest"},
        {15, nullptr, "ResolverGetOptionRequest"},
    };
    RegisterHandlers(functions);
}

SFDNSRES::~SFDNSRES() = default;

enum class NetDbError : s32 {
    Internal = -1,
    Success = 0,
    HostNotFound = 1,
    TryAgain = 2,
    NoRecovery = 3,
    NoData = 4,
};

static NetDbError AddrInfoErrorToNetDbError(s32 result) {
    // Best effort guess to map errors
    switch (result) {
    case 0:
        return NetDbError::Success;
    case EAI_AGAIN:
        return NetDbError::TryAgain;
    case EAI_NODATA:
        return NetDbError::NoData;
    default:
        return NetDbError::HostNotFound;
    }
}

static std::vector<u8> SerializeAddrInfo(const addrinfo* addrinfo, s32 result_code,
                                         std::string_view host) {
    // Adapted from
    // https://github.com/switchbrew/libnx/blob/c5a9a909a91657a9818a3b7e18c9b91ff0cbb6e3/nx/source/runtime/resolver.c#L190
    std::vector<u8> data;

    auto* current = addrinfo;
    while (current != nullptr) {
        struct SerializedResponseHeader {
            u32 magic;
            s32 flags;
            s32 family;
            s32 socket_type;
            s32 protocol;
            u32 address_length;
        };
        static_assert(sizeof(SerializedResponseHeader) == 0x18,
                      "Response header size must be 0x18 bytes");

        constexpr auto header_size = sizeof(SerializedResponseHeader);
        const auto addr_size =
            current->ai_addr && current->ai_addrlen > 0 ? current->ai_addrlen : 4;
        const auto canonname_size = current->ai_canonname ? strlen(current->ai_canonname) + 1 : 1;

        const auto last_size = data.size();
        data.resize(last_size + header_size + addr_size + canonname_size);

        // Header in network byte order
        SerializedResponseHeader header{};

        constexpr auto HEADER_MAGIC = 0xBEEFCAFE;
        header.magic = htonl(HEADER_MAGIC);
        header.family = htonl(current->ai_family);
        header.flags = htonl(current->ai_flags);
        header.socket_type = htonl(current->ai_socktype);
        header.protocol = htonl(current->ai_protocol);
        header.address_length = current->ai_addr ? htonl((u32)current->ai_addrlen) : 0;

        auto* header_ptr = data.data() + last_size;
        std::memcpy(header_ptr, &header, header_size);

        if (header.address_length == 0) {
            std::memset(header_ptr + header_size, 0, 4);
        } else {
            switch (current->ai_family) {
            case AF_INET: {
                struct SockAddrIn {
                    s16 sin_family;
                    u16 sin_port;
                    u32 sin_addr;
                    u8 sin_zero[8];
                };

                SockAddrIn serialized_addr{};
                const auto addr = *reinterpret_cast<sockaddr_in*>(current->ai_addr);
                serialized_addr.sin_port = htons(addr.sin_port);
                serialized_addr.sin_family = htons(addr.sin_family);
                serialized_addr.sin_addr = htonl(addr.sin_addr.s_addr);
                std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn));

                char addr_string_buf[64]{};
                inet_ntop(AF_INET, &addr.sin_addr, addr_string_buf, std::size(addr_string_buf));
                LOG_INFO(Service, "Resolved host '{}' to IPv4 address {}", host, addr_string_buf);
                break;
            }
            case AF_INET6: {
                struct SockAddrIn6 {
                    s16 sin6_family;
                    u16 sin6_port;
                    u32 sin6_flowinfo;
                    u8 sin6_addr[16];
                    u32 sin6_scope_id;
                };

                SockAddrIn6 serialized_addr{};
                const auto addr = *reinterpret_cast<sockaddr_in6*>(current->ai_addr);
                serialized_addr.sin6_family = htons(addr.sin6_family);
                serialized_addr.sin6_port = htons(addr.sin6_port);
                serialized_addr.sin6_flowinfo = htonl(addr.sin6_flowinfo);
                serialized_addr.sin6_scope_id = htonl(addr.sin6_scope_id);
                std::memcpy(serialized_addr.sin6_addr, &addr.sin6_addr,
                            sizeof(SockAddrIn6::sin6_addr));
                std::memcpy(header_ptr + header_size, &serialized_addr, sizeof(SockAddrIn6));

                char addr_string_buf[64]{};
                inet_ntop(AF_INET6, &addr.sin6_addr, addr_string_buf, std::size(addr_string_buf));
                LOG_INFO(Service, "Resolved host '{}' to IPv6 address {}", host, addr_string_buf);
                break;
            }
            default:
                std::memcpy(header_ptr + header_size, current->ai_addr, addr_size);
                break;
            }
        }
        if (current->ai_canonname) {
            std::memcpy(header_ptr + addr_size, current->ai_canonname, canonname_size);
        } else {
            *(header_ptr + header_size + addr_size) = 0;
        }

        current = current->ai_next;
    }

    // 4-byte sentinel value
    data.push_back(0);
    data.push_back(0);
    data.push_back(0);
    data.push_back(0);

    return data;
}

static std::pair<u32, s32> GetAddrInfoRequestImpl(Kernel::HLERequestContext& ctx) {
    struct Parameters {
        u8 use_nsd_resolve;
        u32 unknown;
        u64 process_id;
    };

    IPC::RequestParser rp{ctx};
    const auto parameters = rp.PopRaw<Parameters>();

    LOG_WARNING(Service,
                "called with ignored parameters: use_nsd_resolve={}, unknown={}, process_id={}",
                parameters.use_nsd_resolve, parameters.unknown, parameters.process_id);

    const auto host_buffer = ctx.ReadBuffer(0);
    const std::string host = Common::StringFromBuffer(host_buffer);

    const auto service_buffer = ctx.ReadBuffer(1);
    const std::string service = Common::StringFromBuffer(service_buffer);

    addrinfo* addrinfo;
    // Pass null for hints. Serialized hints are also passed in a buffer, but are ignored for now
    s32 result_code = getaddrinfo(host.c_str(), service.c_str(), nullptr, &addrinfo);

    u32 data_size = 0;
    if (result_code == 0 && addrinfo != nullptr) {
        const std::vector<u8>& data = SerializeAddrInfo(addrinfo, result_code, host);
        data_size = static_cast<u32>(data.size());
        freeaddrinfo(addrinfo);

        ctx.WriteBuffer(data, 0);
    }

    return std::make_pair(data_size, result_code);
}

void SFDNSRES::GetAddrInfoRequest(Kernel::HLERequestContext& ctx) {
    auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx);

    IPC::ResponseBuilder rb{ctx, 4};
    rb.Push(ResultSuccess);
    rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode
    rb.Push(result_code);                                              // errno
    rb.Push(data_size);                                                // serialized size
}

void SFDNSRES::GetAddrInfoRequestWithOptions(Kernel::HLERequestContext& ctx) {
    // Additional options are ignored
    auto [data_size, result_code] = GetAddrInfoRequestImpl(ctx);

    IPC::ResponseBuilder rb{ctx, 5};
    rb.Push(ResultSuccess);
    rb.Push(data_size);                                                // serialized size
    rb.Push(result_code);                                              // errno
    rb.Push(static_cast<s32>(AddrInfoErrorToNetDbError(result_code))); // NetDBErrorCode
    rb.Push(0);
}

} // namespace Service::Sockets