aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Sockets/Sfdnsres/IResolver.cs
blob: 39af90383f9711d622f62d42392061bdeb80ac74 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Services.Sockets.Nsd.Manager;
using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Proxy;
using Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres.Types;
using Ryujinx.Memory;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
{
    [Service("sfdnsres")]
    class IResolver : IpcService
    {
        public IResolver(ServiceCtx context)
        {
            DnsMitmResolver.Instance.ReloadEntries(context);
        }

        [CommandCmif(0)]
        // SetDnsAddressesPrivateRequest(u32, buffer<unknown, 5, 0>)
        public ResultCode SetDnsAddressesPrivateRequest(ServiceCtx context)
        {
            uint cancelHandleRequest = context.RequestData.ReadUInt32();
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            ulong bufferPosition = context.Request.SendBuff[0].Position;
            ulong bufferSize = context.Request.SendBuff[0].Size;
#pragma warning restore IDE0059

            // TODO: This is stubbed in 2.0.0+, reverse 1.0.0 version for the sake of completeness.
            Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });

            return ResultCode.NotAllocated;
        }

        [CommandCmif(1)]
        // GetDnsAddressPrivateRequest(u32) -> buffer<unknown, 6, 0>
        public ResultCode GetDnsAddressPrivateRequest(ServiceCtx context)
        {
            uint cancelHandleRequest = context.RequestData.ReadUInt32();
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
            ulong bufferSize = context.Request.ReceiveBuff[0].Size;
#pragma warning restore IDE0059

            // TODO: This is stubbed in 2.0.0+, reverse 1.0.0 version for the sake of completeness.
            Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });

            return ResultCode.NotAllocated;
        }

        [CommandCmif(2)]
        // GetHostByNameRequest(u8, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
        public ResultCode GetHostByNameRequest(ServiceCtx context)
        {
            ulong inputBufferPosition = context.Request.SendBuff[0].Position;
            ulong inputBufferSize = context.Request.SendBuff[0].Size;

            ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
            ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;

            return GetHostByNameRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, false, 0, 0);
        }

        [CommandCmif(3)]
        // GetHostByAddrRequest(u32, u32, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
        public ResultCode GetHostByAddrRequest(ServiceCtx context)
        {
            ulong inputBufferPosition = context.Request.SendBuff[0].Position;
            ulong inputBufferSize = context.Request.SendBuff[0].Size;

            ulong outputBufferPosition = context.Request.ReceiveBuff[0].Position;
            ulong outputBufferSize = context.Request.ReceiveBuff[0].Size;

            return GetHostByAddrRequestImpl(context, inputBufferPosition, inputBufferSize, outputBufferPosition, outputBufferSize, false, 0, 0);
        }

        [CommandCmif(4)]
        // GetHostStringErrorRequest(u32) -> buffer<unknown, 6, 0>
        public ResultCode GetHostStringErrorRequest(ServiceCtx context)
        {
            ResultCode resultCode = ResultCode.NotAllocated;
            NetDbError errorCode = (NetDbError)context.RequestData.ReadInt32();

            string errorString = errorCode switch
            {
                NetDbError.Success => "Resolver Error 0 (no error)",
                NetDbError.HostNotFound => "Unknown host",
                NetDbError.TryAgain => "Host name lookup failure",
                NetDbError.NoRecovery => "Unknown server error",
                NetDbError.NoData => "No address associated with name",
                _ => (errorCode <= NetDbError.Internal) ? "Resolver internal error" : "Unknown resolver error",
            };

            ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
            ulong bufferSize = context.Request.ReceiveBuff[0].Size;

            if ((ulong)(errorString.Length + 1) <= bufferSize)
            {
                context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));

                resultCode = ResultCode.Success;
            }

            return resultCode;
        }

        [CommandCmif(5)]
        // GetGaiStringErrorRequest(u32) -> buffer<byte, 6, 0>
        public ResultCode GetGaiStringErrorRequest(ServiceCtx context)
        {
            ResultCode resultCode = ResultCode.NotAllocated;
            GaiError errorCode = (GaiError)context.RequestData.ReadInt32();

            if (errorCode > GaiError.Max)
            {
                errorCode = GaiError.Max;
            }

            string errorString = errorCode switch
            {
                GaiError.AddressFamily => "Address family for hostname not supported",
                GaiError.Again => "Temporary failure in name resolution",
                GaiError.BadFlags => "Invalid value for ai_flags",
                GaiError.Fail => "Non-recoverable failure in name resolution",
                GaiError.Family => "ai_family not supported",
                GaiError.Memory => "Memory allocation failure",
                GaiError.NoData => "No address associated with hostname",
                GaiError.NoName => "hostname nor servname provided, or not known",
                GaiError.Service => "servname not supported for ai_socktype",
                GaiError.SocketType => "ai_socktype not supported",
                GaiError.System => "System error returned in errno",
                GaiError.BadHints => "Invalid value for hints",
                GaiError.Protocol => "Resolved protocol is unknown",
                GaiError.Overflow => "Argument buffer overflow",
                GaiError.Max => "Unknown error",
                _ => "Success",
            };

            ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
            ulong bufferSize = context.Request.ReceiveBuff[0].Size;

            if ((ulong)(errorString.Length + 1) <= bufferSize)
            {
                context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(errorString + '\0'));

                resultCode = ResultCode.Success;
            }

            return resultCode;
        }

        [CommandCmif(6)]
        // GetAddrInfoRequest(bool enable_nsd_resolve, u32, u64 pid_placeholder, pid, buffer<i8, 5, 0> host, buffer<i8, 5, 0> service, buffer<packed_addrinfo, 5, 0> hints) -> (i32 ret, u32 bsd_errno, u32 packed_addrinfo_size, buffer<packed_addrinfo, 6, 0> response)
        public ResultCode GetAddrInfoRequest(ServiceCtx context)
        {
            ulong responseBufferPosition = context.Request.ReceiveBuff[0].Position;
            ulong responseBufferSize = context.Request.ReceiveBuff[0].Size;

            return GetAddrInfoRequestImpl(context, responseBufferPosition, responseBufferSize, false, 0, 0);
        }

        [CommandCmif(8)]
        // GetCancelHandleRequest(u64, pid) -> u32
        public ResultCode GetCancelHandleRequest(ServiceCtx context)
        {
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            ulong pidPlaceHolder = context.RequestData.ReadUInt64();
#pragma warning restore IDE0059
            uint cancelHandleRequest = 0;

            context.ResponseData.Write(cancelHandleRequest);

            Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });

            return ResultCode.Success;
        }

        [CommandCmif(9)]
        // CancelRequest(u32, u64, pid)
        public ResultCode CancelRequest(ServiceCtx context)
        {
            uint cancelHandleRequest = context.RequestData.ReadUInt32();
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            ulong pidPlaceHolder = context.RequestData.ReadUInt64();
#pragma warning restore IDE0059

            Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { cancelHandleRequest });

            return ResultCode.Success;
        }

        [CommandCmif(10)] // 5.0.0+
        // GetHostByNameRequestWithOptions(u8, u32, u64, pid, buffer<unknown, 21, 0>, buffer<unknown, 21, 0>) -> (u32, u32, u32, buffer<unknown, 22, 0>)
        public ResultCode GetHostByNameRequestWithOptions(ServiceCtx context)
        {
            (ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
            (ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
            (ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();

            return GetHostByNameRequestImpl(
                context,
                inputBufferPosition,
                inputBufferSize,
                outputBufferPosition,
                outputBufferSize,
                true,
                optionsBufferPosition,
                optionsBufferSize);
        }

        [CommandCmif(11)] // 5.0.0+
        // GetHostByAddrRequestWithOptions(u32, u32, u32, u64, pid, buffer<unknown, 21, 0>, buffer<unknown, 21, 0>) -> (u32, u32, u32, buffer<unknown, 22, 0>)
        public ResultCode GetHostByAddrRequestWithOptions(ServiceCtx context)
        {
            (ulong inputBufferPosition, ulong inputBufferSize) = context.Request.GetBufferType0x21();
            (ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
            (ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();

            return GetHostByAddrRequestImpl(
                context,
                inputBufferPosition,
                inputBufferSize,
                outputBufferPosition,
                outputBufferSize,
                true,
                optionsBufferPosition,
                optionsBufferSize);
        }

        [CommandCmif(12)] // 5.0.0+
        // GetAddrInfoRequestWithOptions(bool enable_nsd_resolve, u32, u64 pid_placeholder, pid, buffer<i8, 5, 0> host, buffer<i8, 5, 0> service, buffer<packed_addrinfo, 5, 0> hints, buffer<unknown, 21, 0>) -> (i32 ret, u32 bsd_errno, u32 unknown, u32 packed_addrinfo_size, buffer<packed_addrinfo, 22, 0> response)
        public ResultCode GetAddrInfoRequestWithOptions(ServiceCtx context)
        {
            (ulong outputBufferPosition, ulong outputBufferSize) = context.Request.GetBufferType0x22();
            (ulong optionsBufferPosition, ulong optionsBufferSize) = context.Request.GetBufferType0x21();

            return GetAddrInfoRequestImpl(context, outputBufferPosition, outputBufferSize, true, optionsBufferPosition, optionsBufferSize);
        }

        [CommandCmif(14)] // 5.0.0+
        // ResolverSetOptionRequest(buffer<unknown, 5, 0>, u64 unknown, u64 pid_placeholder, pid) -> (i32 ret, u32 bsd_errno)
        public ResultCode ResolverSetOptionRequest(ServiceCtx context)
        {
            ulong bufferPosition = context.Request.SendBuff[0].Position;
            ulong bufferSize = context.Request.SendBuff[0].Size;

            ulong unknown = context.RequestData.ReadUInt64();

            byte[] buffer = new byte[bufferSize];

            context.Memory.Read(bufferPosition, buffer);

            // TODO: Parse and use options.

            Logger.Stub?.PrintStub(LogClass.ServiceSfdnsres, new { unknown });

            NetDbError netDbErrorCode = NetDbError.Success;
            GaiError errno = GaiError.Success;

            context.ResponseData.Write((int)errno);
            context.ResponseData.Write((int)netDbErrorCode);

            return ResultCode.Success;
        }

        // Atmosphère extension for dns_mitm
        [CommandCmif(65000)]
        // AtmosphereReloadHostsFile()
        public ResultCode AtmosphereReloadHostsFile(ServiceCtx context)
        {
            DnsMitmResolver.Instance.ReloadEntries(context);

            return ResultCode.Success;
        }

        private static ResultCode GetHostByNameRequestImpl(
            ServiceCtx context,
            ulong inputBufferPosition,
            ulong inputBufferSize,
            ulong outputBufferPosition,
            ulong outputBufferSize,
            bool withOptions,
            ulong optionsBufferPosition,
            ulong optionsBufferSize)
        {
            string host = MemoryHelper.ReadAsciiString(context.Memory, inputBufferPosition, (int)inputBufferSize);

            if (!context.Device.Configuration.EnableInternetAccess)
            {
                Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked: {host}");

                WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);

                return ResultCode.Success;
            }

            // TODO: Use params.
            bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            int timeOut = context.RequestData.ReadInt32();
            ulong pidPlaceholder = context.RequestData.ReadUInt64();
#pragma warning restore IDE0059

            if (withOptions)
            {
                // TODO: Parse and use options.
            }

            IPHostEntry hostEntry = null;

            NetDbError netDbErrorCode = NetDbError.Success;
            GaiError errno = GaiError.Overflow;
            int serializedSize = 0;

            if (host.Length <= byte.MaxValue)
            {
                if (enableNsdResolve)
                {
                    if (FqdnResolver.Resolve(host, out string newAddress) == Nsd.ResultCode.Success)
                    {
                        host = newAddress;
                    }
                }

                string targetHost = host;

                if (DnsBlacklist.IsHostBlocked(host))
                {
                    Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {host}");

                    netDbErrorCode = NetDbError.HostNotFound;
                    errno = GaiError.NoData;
                }
                else
                {
                    Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {host}");

                    try
                    {
                        hostEntry = DnsMitmResolver.Instance.ResolveAddress(targetHost);
                    }
                    catch (SocketException exception)
                    {
                        netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
                        errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
                    }
                }
            }
            else
            {
                netDbErrorCode = NetDbError.HostNotFound;
            }

            if (hostEntry != null)
            {
                IEnumerable<IPAddress> addresses = GetIpv4Addresses(hostEntry);

                if (!addresses.Any())
                {
                    errno = GaiError.NoData;
                    netDbErrorCode = NetDbError.NoAddress;
                }
                else
                {
                    errno = GaiError.Success;
                    serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, addresses);
                }
            }

            WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);

            return ResultCode.Success;
        }

        private static ResultCode GetHostByAddrRequestImpl(
            ServiceCtx context,
            ulong inputBufferPosition,
            ulong inputBufferSize,
            ulong outputBufferPosition,
            ulong outputBufferSize,
            bool withOptions,
            ulong optionsBufferPosition,
            ulong optionsBufferSize)
        {
            if (!context.Device.Configuration.EnableInternetAccess)
            {
                Logger.Info?.Print(LogClass.ServiceSfdnsres, "Guest network access disabled, DNS Blocked.");

                WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);

                return ResultCode.Success;
            }

            byte[] rawIp = new byte[inputBufferSize];

            context.Memory.Read(inputBufferPosition, rawIp);

            // TODO: Use params.
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            uint socketLength = context.RequestData.ReadUInt32();
            uint type = context.RequestData.ReadUInt32();
            int timeOut = context.RequestData.ReadInt32();
            ulong pidPlaceholder = context.RequestData.ReadUInt64();
#pragma warning restore IDE0059

            if (withOptions)
            {
                // TODO: Parse and use options.
            }

            IPHostEntry hostEntry = null;

            NetDbError netDbErrorCode = NetDbError.Success;
            GaiError errno = GaiError.AddressFamily;
            int serializedSize = 0;

            if (rawIp.Length == 4)
            {
                try
                {
                    IPAddress address = new(rawIp);

                    hostEntry = Dns.GetHostEntry(address);
                }
                catch (SocketException exception)
                {
                    netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
                    errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
                }
            }
            else
            {
                netDbErrorCode = NetDbError.NoAddress;
            }

            if (hostEntry != null)
            {
                errno = GaiError.Success;
                serializedSize = SerializeHostEntries(context, outputBufferPosition, outputBufferSize, hostEntry, GetIpv4Addresses(hostEntry));
            }

            WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);

            return ResultCode.Success;
        }

        private static int SerializeHostEntries(ServiceCtx context, ulong outputBufferPosition, ulong outputBufferSize, IPHostEntry hostEntry, IEnumerable<IPAddress> addresses = null)
        {
            ulong originalBufferPosition = outputBufferPosition;
            ulong bufferPosition = originalBufferPosition;

            string hostName = hostEntry.HostName + '\0';

            // h_name
            context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(hostName));
            bufferPosition += (ulong)hostName.Length;

            // h_aliases list size
            context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(hostEntry.Aliases.Length));
            bufferPosition += sizeof(int);

            // Actual aliases
            foreach (string alias in hostEntry.Aliases)
            {
                context.Memory.Write(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0'));
                bufferPosition += (ulong)(alias.Length + 1);
            }

            // h_addrtype but it's a short (also only support IPv4)
            context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)AddressFamily.InterNetwork));
            bufferPosition += sizeof(short);

            // h_length but it's a short
            context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness((short)4));
            bufferPosition += sizeof(short);

            // Ip address count, we can only support ipv4 (blame Nintendo)
            context.Memory.Write(bufferPosition, addresses != null ? BinaryPrimitives.ReverseEndianness(addresses.Count()) : 0);
            bufferPosition += sizeof(int);

            if (addresses != null)
            {
                foreach (IPAddress ip in addresses)
                {
                    context.Memory.Write(bufferPosition, BinaryPrimitives.ReverseEndianness(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
                    bufferPosition += sizeof(int);
                }
            }

            return (int)(bufferPosition - originalBufferPosition);
        }

        private static ResultCode GetAddrInfoRequestImpl(
            ServiceCtx context,
            ulong responseBufferPosition,
            ulong responseBufferSize,
            bool withOptions,
            ulong optionsBufferPosition,
            ulong optionsBufferSize)
        {
            bool enableNsdResolve = (context.RequestData.ReadInt32() & 1) != 0;
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            uint cancelHandle = context.RequestData.ReadUInt32();
#pragma warning restore IDE0059

            string host = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[0].Position, (long)context.Request.SendBuff[0].Size);
            string service = MemoryHelper.ReadAsciiString(context.Memory, context.Request.SendBuff[1].Position, (long)context.Request.SendBuff[1].Size);

            if (!context.Device.Configuration.EnableInternetAccess)
            {
                Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Guest network access disabled, DNS Blocked: {host}");

                WriteResponse(context, withOptions, 0, GaiError.NoData, NetDbError.HostNotFound);

                return ResultCode.Success;
            }

            // NOTE: We ignore hints for now.
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            List<AddrInfoSerialized> hints = DeserializeAddrInfos(context.Memory, context.Request.SendBuff[2].Position, context.Request.SendBuff[2].Size);
#pragma warning restore IDE0059

            if (withOptions)
            {
                // TODO: Find unknown, Parse and use options.
#pragma warning disable IDE0059 // Remove unnecessary value assignment
                uint unknown = context.RequestData.ReadUInt32();
#pragma warning restore IDE0059
            }

#pragma warning disable IDE0059 // Remove unnecessary value assignment
            ulong pidPlaceHolder = context.RequestData.ReadUInt64();
#pragma warning restore IDE0059

            IPHostEntry hostEntry = null;

            NetDbError netDbErrorCode = NetDbError.Success;
            GaiError errno = GaiError.AddressFamily;
            int serializedSize = 0;

            if (host.Length <= byte.MaxValue)
            {
                if (enableNsdResolve)
                {
                    if (FqdnResolver.Resolve(host, out string newAddress) == Nsd.ResultCode.Success)
                    {
                        host = newAddress;
                    }
                }

                string targetHost = host;

                if (DnsBlacklist.IsHostBlocked(host))
                {
                    Logger.Info?.Print(LogClass.ServiceSfdnsres, $"DNS Blocked: {host}");

                    netDbErrorCode = NetDbError.HostNotFound;
                    errno = GaiError.NoData;
                }
                else
                {
                    Logger.Info?.Print(LogClass.ServiceSfdnsres, $"Trying to resolve: {host}");

                    try
                    {
                        hostEntry = DnsMitmResolver.Instance.ResolveAddress(targetHost);
                    }
                    catch (SocketException exception)
                    {
                        netDbErrorCode = ConvertSocketErrorCodeToNetDbError(exception.ErrorCode);
                        errno = ConvertSocketErrorCodeToGaiError(exception.ErrorCode, errno);
                    }
                }
            }
            else
            {
                netDbErrorCode = NetDbError.NoAddress;
            }

            if (hostEntry != null)
            {
                if (int.TryParse(service, out int port) || string.IsNullOrEmpty(service))
                {
                    errno = GaiError.Success;
                    serializedSize = SerializeAddrInfos(context, responseBufferPosition, responseBufferSize, hostEntry, port);
                }
                else
                {
                    errno = GaiError.Service;
                }
            }

            WriteResponse(context, withOptions, serializedSize, errno, netDbErrorCode);

            return ResultCode.Success;
        }

        private static List<AddrInfoSerialized> DeserializeAddrInfos(IVirtualMemoryManager memory, ulong address, ulong size)
        {
            List<AddrInfoSerialized> result = new();

            ReadOnlySpan<byte> data = memory.GetSpan(address, (int)size);

            while (!data.IsEmpty)
            {
                AddrInfoSerialized info = AddrInfoSerialized.Read(data, out data);

                if (info == null)
                {
                    break;
                }

                result.Add(info);
            }

            return result;
        }

        private static int SerializeAddrInfos(ServiceCtx context, ulong responseBufferPosition, ulong responseBufferSize, IPHostEntry hostEntry, int port)
        {
            ulong originalBufferPosition = responseBufferPosition;
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            ulong bufferPosition = originalBufferPosition;

            byte[] hostName = Encoding.ASCII.GetBytes(hostEntry.HostName + '\0');
#pragma warning restore IDE0059

            using WritableRegion region = context.Memory.GetWritableRegion(responseBufferPosition, (int)responseBufferSize);

            Span<byte> data = region.Memory.Span;

            for (int i = 0; i < hostEntry.AddressList.Length; i++)
            {
                IPAddress ip = hostEntry.AddressList[i];

                if (ip.AddressFamily != AddressFamily.InterNetwork)
                {
                    continue;
                }

                // NOTE: 0 = Any
                AddrInfoSerializedHeader header = new(ip, 0);
                AddrInfo4 addr = new(ip, (short)port);
                AddrInfoSerialized info = new(header, addr, null, hostEntry.HostName);

                data = info.Write(data);
            }

            uint sentinel = 0;
            MemoryMarshal.Write(data, in sentinel);
            data = data[sizeof(uint)..];

            return region.Memory.Span.Length - data.Length;
        }

        private static void WriteResponse(
            ServiceCtx context,
            bool withOptions,
            int serializedSize,
            GaiError errno,
            NetDbError netDbErrorCode)
        {
            if (withOptions)
            {
                context.ResponseData.Write(serializedSize);
                context.ResponseData.Write((int)errno);
                context.ResponseData.Write((int)netDbErrorCode);
                context.ResponseData.Write(0);
            }
            else
            {
                context.ResponseData.Write((int)netDbErrorCode);
                context.ResponseData.Write((int)errno);
                context.ResponseData.Write(serializedSize);
            }
        }

        private static IEnumerable<IPAddress> GetIpv4Addresses(IPHostEntry hostEntry)
        {
            return hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork);
        }

        private static NetDbError ConvertSocketErrorCodeToNetDbError(int errorCode)
        {
            return errorCode switch
            {
                11001 => NetDbError.HostNotFound,
                11002 => NetDbError.TryAgain,
                11003 => NetDbError.NoRecovery,
                11004 => NetDbError.NoData,
                _ => NetDbError.Internal,
            };
        }

        private static GaiError ConvertSocketErrorCodeToGaiError(int errorCode, GaiError errno)
        {
            return errorCode switch
            {
                11001 => GaiError.NoData,
                10060 => GaiError.Again,
                _ => errno,
            };
        }
    }
}