2019-10-18 22:47:50 +00:00
|
|
|
using Ryujinx.Common;
|
2018-10-17 17:15:50 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2022-11-09 19:22:43 +00:00
|
|
|
using Ryujinx.Common.Utilities;
|
2019-09-19 00:45:11 +00:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nifm.StaticService.GeneralService;
|
2019-10-18 22:47:50 +00:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nifm.StaticService.Types;
|
2018-07-19 01:06:45 +00:00
|
|
|
using System;
|
|
|
|
using System.Net.NetworkInformation;
|
2021-04-13 01:04:18 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2019-09-19 00:45:11 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService
|
2018-02-28 03:31:52 +00:00
|
|
|
{
|
2021-06-29 17:37:13 +00:00
|
|
|
class IGeneralService : DisposableIpcService
|
2018-02-28 03:31:52 +00:00
|
|
|
{
|
2019-09-04 16:09:20 +00:00
|
|
|
private GeneralServiceDetail _generalServiceDetail;
|
|
|
|
|
2022-03-15 02:49:35 +00:00
|
|
|
private IPInterfaceProperties _targetPropertiesCache = null;
|
|
|
|
private UnicastIPAddressInformation _targetAddressInfoCache = null;
|
2023-04-16 15:25:20 +00:00
|
|
|
private string _cacheChosenInterface = null;
|
2022-03-15 02:49:35 +00:00
|
|
|
|
2019-09-04 16:09:20 +00:00
|
|
|
public IGeneralService()
|
|
|
|
{
|
|
|
|
_generalServiceDetail = new GeneralServiceDetail
|
|
|
|
{
|
|
|
|
ClientId = GeneralServiceManager.Count,
|
|
|
|
IsAnyInternetRequestAccepted = true // NOTE: Why not accept any internet request?
|
|
|
|
};
|
|
|
|
|
2022-03-15 02:49:35 +00:00
|
|
|
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(LocalInterfaceCacheHandler);
|
|
|
|
|
2019-09-04 16:09:20 +00:00
|
|
|
GeneralServiceManager.Add(_generalServiceDetail);
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(1)]
|
2019-09-04 16:09:20 +00:00
|
|
|
// GetClientId() -> buffer<nn::nifm::ClientId, 0x1a, 4>
|
|
|
|
public ResultCode GetClientId(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong position = context.Request.RecvListBuff[0].Position;
|
2021-02-19 19:18:13 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(sizeof(int));
|
2019-09-04 16:09:20 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(position, _generalServiceDetail.ClientId);
|
2019-09-04 16:09:20 +00:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
2018-02-28 03:31:52 +00:00
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(4)]
|
2019-09-04 16:09:20 +00:00
|
|
|
// CreateRequest(u32 version) -> object<nn::nifm::detail::IRequest>
|
2019-07-14 19:04:38 +00:00
|
|
|
public ResultCode CreateRequest(ServiceCtx context)
|
2018-02-28 03:31:52 +00:00
|
|
|
{
|
2019-09-04 16:09:20 +00:00
|
|
|
uint version = context.RequestData.ReadUInt32();
|
2018-02-28 03:31:52 +00:00
|
|
|
|
2019-09-04 16:09:20 +00:00
|
|
|
MakeObject(context, new IRequest(context.Device.System, version));
|
2018-02-28 03:31:52 +00:00
|
|
|
|
2019-09-04 16:09:20 +00:00
|
|
|
// Doesn't occur in our case.
|
|
|
|
// return ResultCode.ObjectIsNull;
|
|
|
|
|
2020-08-03 23:32:53 +00:00
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNifm, new { version });
|
2018-02-28 03:31:52 +00:00
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.Success;
|
2018-02-28 03:31:52 +00:00
|
|
|
}
|
2018-07-19 01:06:45 +00:00
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(5)]
|
2021-04-13 01:04:18 +00:00
|
|
|
// GetCurrentNetworkProfile() -> buffer<nn::nifm::detail::sf::NetworkProfileData, 0x1a, 0x17c>
|
|
|
|
public ResultCode GetCurrentNetworkProfile(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong networkProfileDataPosition = context.Request.RecvListBuff[0].Position;
|
2021-04-13 01:04:18 +00:00
|
|
|
|
2023-04-16 15:25:20 +00:00
|
|
|
(IPInterfaceProperties interfaceProperties, UnicastIPAddressInformation unicastAddress) = GetLocalInterface(context);
|
2021-04-13 01:04:18 +00:00
|
|
|
|
|
|
|
if (interfaceProperties == null || unicastAddress == null)
|
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Unsafe.SizeOf<NetworkProfileData>());
|
2021-04-13 01:04:18 +00:00
|
|
|
|
|
|
|
NetworkProfileData networkProfile = new NetworkProfileData
|
|
|
|
{
|
2022-11-09 19:22:43 +00:00
|
|
|
Uuid = UInt128Utils.CreateRandom()
|
2021-04-13 01:04:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
networkProfile.IpSettingData.IpAddressSetting = new IpAddressSetting(interfaceProperties, unicastAddress);
|
|
|
|
networkProfile.IpSettingData.DnsSetting = new DnsSetting(interfaceProperties);
|
|
|
|
|
2022-11-16 19:30:12 +00:00
|
|
|
"RyujinxNetwork"u8.CopyTo(networkProfile.Name.AsSpan());
|
2021-04-13 01:04:18 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
context.Memory.Write(networkProfileDataPosition, networkProfile);
|
2021-04-13 01:04:18 +00:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(12)]
|
2019-07-12 01:13:43 +00:00
|
|
|
// GetCurrentIpAddress() -> nn::nifm::IpV4Address
|
2019-07-14 19:04:38 +00:00
|
|
|
public ResultCode GetCurrentIpAddress(ServiceCtx context)
|
2018-07-19 01:06:45 +00:00
|
|
|
{
|
2023-04-16 15:25:20 +00:00
|
|
|
(_, UnicastIPAddressInformation unicastAddress) = GetLocalInterface(context);
|
2020-02-17 15:28:41 +00:00
|
|
|
|
|
|
|
if (unicastAddress == null)
|
2018-07-19 01:06:45 +00:00
|
|
|
{
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.NoInternetConnection;
|
2018-07-19 01:06:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:28:41 +00:00
|
|
|
context.ResponseData.WriteStruct(new IpV4Address(unicastAddress.Address));
|
|
|
|
|
2020-08-03 23:32:53 +00:00
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
2020-02-17 15:28:41 +00:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(15)]
|
2020-02-17 15:28:41 +00:00
|
|
|
// GetCurrentIpConfigInfo() -> (nn::nifm::IpAddressSetting, nn::nifm::DnsSetting)
|
|
|
|
public ResultCode GetCurrentIpConfigInfo(ServiceCtx context)
|
|
|
|
{
|
2023-04-16 15:25:20 +00:00
|
|
|
(IPInterfaceProperties interfaceProperties, UnicastIPAddressInformation unicastAddress) = GetLocalInterface(context);
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2021-04-13 01:04:18 +00:00
|
|
|
if (interfaceProperties == null || unicastAddress == null)
|
2020-02-17 15:28:41 +00:00
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
2018-07-19 01:06:45 +00:00
|
|
|
|
2020-08-03 23:32:53 +00:00
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
2018-07-19 01:06:45 +00:00
|
|
|
|
2020-02-17 15:28:41 +00:00
|
|
|
context.ResponseData.WriteStruct(new IpAddressSetting(interfaceProperties, unicastAddress));
|
|
|
|
context.ResponseData.WriteStruct(new DnsSetting(interfaceProperties));
|
2018-07-19 01:06:45 +00:00
|
|
|
|
2019-07-14 19:04:38 +00:00
|
|
|
return ResultCode.Success;
|
2018-07-19 01:06:45 +00:00
|
|
|
}
|
2019-09-04 16:09:20 +00:00
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(18)]
|
2019-10-18 22:47:50 +00:00
|
|
|
// GetInternetConnectionStatus() -> nn::nifm::detail::sf::InternetConnectionStatus
|
|
|
|
public ResultCode GetInternetConnectionStatus(ServiceCtx context)
|
|
|
|
{
|
|
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
InternetConnectionStatus internetConnectionStatus = new InternetConnectionStatus
|
|
|
|
{
|
|
|
|
Type = InternetConnectionType.WiFi,
|
|
|
|
WifiStrength = 3,
|
|
|
|
State = InternetConnectionState.Connected,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.ResponseData.WriteStruct(internetConnectionStatus);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:00:34 +00:00
|
|
|
[CommandCmif(21)]
|
2019-09-04 16:09:20 +00:00
|
|
|
// IsAnyInternetRequestAccepted(buffer<nn::nifm::ClientId, 0x19, 4>) -> bool
|
|
|
|
public ResultCode IsAnyInternetRequestAccepted(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
ulong position = context.Request.PtrBuff[0].Position;
|
|
|
|
ulong size = context.Request.PtrBuff[0].Size;
|
2019-09-04 16:09:20 +00:00
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
int clientId = context.Memory.Read<int>(position);
|
2019-09-04 16:09:20 +00:00
|
|
|
|
|
|
|
context.ResponseData.Write(GeneralServiceManager.Get(clientId).IsAnyInternetRequestAccepted);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2023-04-16 15:25:20 +00:00
|
|
|
private (IPInterfaceProperties, UnicastIPAddressInformation) GetLocalInterface(ServiceCtx context)
|
2020-02-17 15:28:41 +00:00
|
|
|
{
|
|
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
|
|
{
|
|
|
|
return (null, null);
|
|
|
|
}
|
|
|
|
|
2023-04-16 15:25:20 +00:00
|
|
|
string chosenInterface = context.Device.Configuration.MultiplayerLanInterfaceId;
|
2020-02-17 15:28:41 +00:00
|
|
|
|
2023-04-16 15:25:20 +00:00
|
|
|
if (_targetPropertiesCache == null || _targetAddressInfoCache == null || _cacheChosenInterface != chosenInterface)
|
2020-02-17 15:28:41 +00:00
|
|
|
{
|
2023-04-16 15:25:20 +00:00
|
|
|
_cacheChosenInterface = chosenInterface;
|
2020-02-17 15:28:41 +00:00
|
|
|
|
2023-04-16 15:25:20 +00:00
|
|
|
(_targetPropertiesCache, _targetAddressInfoCache) = NetworkHelpers.GetLocalInterface(chosenInterface);
|
|
|
|
}
|
2022-03-15 02:49:35 +00:00
|
|
|
|
2023-04-16 15:25:20 +00:00
|
|
|
return (_targetPropertiesCache, _targetAddressInfoCache);
|
2020-02-17 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 02:49:35 +00:00
|
|
|
private void LocalInterfaceCacheHandler(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"NetworkAddress changed, invalidating cached data.");
|
|
|
|
|
|
|
|
_targetPropertiesCache = null;
|
|
|
|
_targetAddressInfoCache = null;
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:37:13 +00:00
|
|
|
protected override void Dispose(bool isDisposing)
|
2019-09-04 16:09:20 +00:00
|
|
|
{
|
2021-06-29 17:37:13 +00:00
|
|
|
if (isDisposing)
|
|
|
|
{
|
2022-03-15 02:49:35 +00:00
|
|
|
NetworkChange.NetworkAddressChanged -= LocalInterfaceCacheHandler;
|
|
|
|
|
2021-06-29 17:37:13 +00:00
|
|
|
GeneralServiceManager.Remove(_generalServiceDetail.ClientId);
|
|
|
|
}
|
2019-09-04 16:09:20 +00:00
|
|
|
}
|
2018-02-28 03:31:52 +00:00
|
|
|
}
|
2019-07-12 01:13:43 +00:00
|
|
|
}
|