2019-10-19 00:47:50 +02:00
|
|
|
using Ryujinx.Common;
|
2018-10-17 19:15:50 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2022-11-09 20:22:43 +01:00
|
|
|
using Ryujinx.Common.Utilities;
|
2019-09-19 02:45:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nifm.StaticService.GeneralService;
|
2019-10-19 00:47:50 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nifm.StaticService.Types;
|
2018-07-19 03:06:45 +02:00
|
|
|
using System;
|
|
|
|
using System.Net.NetworkInformation;
|
2021-04-13 03:04:18 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Text;
|
2018-08-17 01:47:36 +02:00
|
|
|
|
2019-09-19 02:45:11 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService
|
2018-02-28 04:31:52 +01:00
|
|
|
{
|
2021-06-29 19:37:13 +02:00
|
|
|
class IGeneralService : DisposableIpcService
|
2018-02-28 04:31:52 +01:00
|
|
|
{
|
2019-09-04 18:09:20 +02:00
|
|
|
private GeneralServiceDetail _generalServiceDetail;
|
|
|
|
|
2022-03-15 03:49:35 +01:00
|
|
|
private IPInterfaceProperties _targetPropertiesCache = null;
|
|
|
|
private UnicastIPAddressInformation _targetAddressInfoCache = null;
|
|
|
|
|
2019-09-04 18:09:20 +02:00
|
|
|
public IGeneralService()
|
|
|
|
{
|
|
|
|
_generalServiceDetail = new GeneralServiceDetail
|
|
|
|
{
|
|
|
|
ClientId = GeneralServiceManager.Count,
|
|
|
|
IsAnyInternetRequestAccepted = true // NOTE: Why not accept any internet request?
|
|
|
|
};
|
|
|
|
|
2022-03-15 03:49:35 +01:00
|
|
|
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(LocalInterfaceCacheHandler);
|
|
|
|
|
2019-09-04 18:09:20 +02:00
|
|
|
GeneralServiceManager.Add(_generalServiceDetail);
|
|
|
|
}
|
|
|
|
|
2023-04-15 01:00:34 +02:00
|
|
|
[CommandCmif(1)]
|
2019-09-04 18:09:20 +02:00
|
|
|
// GetClientId() -> buffer<nn::nifm::ClientId, 0x1a, 4>
|
|
|
|
public ResultCode GetClientId(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
ulong position = context.Request.RecvListBuff[0].Position;
|
2021-02-19 20:18:13 +01:00
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(sizeof(int));
|
2019-09-04 18:09:20 +02:00
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
context.Memory.Write(position, _generalServiceDetail.ClientId);
|
2019-09-04 18:09:20 +02:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
2018-02-28 04:31:52 +01:00
|
|
|
|
2023-04-15 01:00:34 +02:00
|
|
|
[CommandCmif(4)]
|
2019-09-04 18:09:20 +02:00
|
|
|
// CreateRequest(u32 version) -> object<nn::nifm::detail::IRequest>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode CreateRequest(ServiceCtx context)
|
2018-02-28 04:31:52 +01:00
|
|
|
{
|
2019-09-04 18:09:20 +02:00
|
|
|
uint version = context.RequestData.ReadUInt32();
|
2018-02-28 04:31:52 +01:00
|
|
|
|
2019-09-04 18:09:20 +02:00
|
|
|
MakeObject(context, new IRequest(context.Device.System, version));
|
2018-02-28 04:31:52 +01:00
|
|
|
|
2019-09-04 18:09:20 +02:00
|
|
|
// Doesn't occur in our case.
|
|
|
|
// return ResultCode.ObjectIsNull;
|
|
|
|
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNifm, new { version });
|
2018-02-28 04:31:52 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-28 04:31:52 +01:00
|
|
|
}
|
2018-07-19 03:06:45 +02:00
|
|
|
|
2023-04-15 01:00:34 +02:00
|
|
|
[CommandCmif(5)]
|
2021-04-13 03:04:18 +02:00
|
|
|
// GetCurrentNetworkProfile() -> buffer<nn::nifm::detail::sf::NetworkProfileData, 0x1a, 0x17c>
|
|
|
|
public ResultCode GetCurrentNetworkProfile(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
ulong networkProfileDataPosition = context.Request.RecvListBuff[0].Position;
|
2021-04-13 03:04:18 +02:00
|
|
|
|
|
|
|
(IPInterfaceProperties interfaceProperties, UnicastIPAddressInformation unicastAddress) = GetLocalInterface();
|
|
|
|
|
|
|
|
if (interfaceProperties == null || unicastAddress == null)
|
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Unsafe.SizeOf<NetworkProfileData>());
|
2021-04-13 03:04:18 +02:00
|
|
|
|
|
|
|
NetworkProfileData networkProfile = new NetworkProfileData
|
|
|
|
{
|
2022-11-09 20:22:43 +01:00
|
|
|
Uuid = UInt128Utils.CreateRandom()
|
2021-04-13 03:04:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
networkProfile.IpSettingData.IpAddressSetting = new IpAddressSetting(interfaceProperties, unicastAddress);
|
|
|
|
networkProfile.IpSettingData.DnsSetting = new DnsSetting(interfaceProperties);
|
|
|
|
|
2022-11-16 20:30:12 +01:00
|
|
|
"RyujinxNetwork"u8.CopyTo(networkProfile.Name.AsSpan());
|
2021-04-13 03:04:18 +02:00
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
context.Memory.Write(networkProfileDataPosition, networkProfile);
|
2021-04-13 03:04:18 +02:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2023-04-15 01:00:34 +02:00
|
|
|
[CommandCmif(12)]
|
2019-07-12 03:13:43 +02:00
|
|
|
// GetCurrentIpAddress() -> nn::nifm::IpV4Address
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetCurrentIpAddress(ServiceCtx context)
|
2018-07-19 03:06:45 +02:00
|
|
|
{
|
2020-02-17 16:28:41 +01:00
|
|
|
(_, UnicastIPAddressInformation unicastAddress) = GetLocalInterface();
|
|
|
|
|
|
|
|
if (unicastAddress == null)
|
2018-07-19 03:06:45 +02:00
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.NoInternetConnection;
|
2018-07-19 03:06:45 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:28:41 +01:00
|
|
|
context.ResponseData.WriteStruct(new IpV4Address(unicastAddress.Address));
|
|
|
|
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
2020-02-17 16:28:41 +01:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2023-04-15 01:00:34 +02:00
|
|
|
[CommandCmif(15)]
|
2020-02-17 16:28:41 +01:00
|
|
|
// GetCurrentIpConfigInfo() -> (nn::nifm::IpAddressSetting, nn::nifm::DnsSetting)
|
|
|
|
public ResultCode GetCurrentIpConfigInfo(ServiceCtx context)
|
|
|
|
{
|
|
|
|
(IPInterfaceProperties interfaceProperties, UnicastIPAddressInformation unicastAddress) = GetLocalInterface();
|
2018-08-17 01:47:36 +02:00
|
|
|
|
2021-04-13 03:04:18 +02:00
|
|
|
if (interfaceProperties == null || unicastAddress == null)
|
2020-02-17 16:28:41 +01:00
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
2018-07-19 03:06:45 +02:00
|
|
|
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
2018-07-19 03:06:45 +02:00
|
|
|
|
2020-02-17 16:28:41 +01:00
|
|
|
context.ResponseData.WriteStruct(new IpAddressSetting(interfaceProperties, unicastAddress));
|
|
|
|
context.ResponseData.WriteStruct(new DnsSetting(interfaceProperties));
|
2018-07-19 03:06:45 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-07-19 03:06:45 +02:00
|
|
|
}
|
2019-09-04 18:09:20 +02:00
|
|
|
|
2023-04-15 01:00:34 +02:00
|
|
|
[CommandCmif(18)]
|
2019-10-19 00:47:50 +02: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-15 01:00:34 +02:00
|
|
|
[CommandCmif(21)]
|
2019-09-04 18:09:20 +02:00
|
|
|
// IsAnyInternetRequestAccepted(buffer<nn::nifm::ClientId, 0x19, 4>) -> bool
|
|
|
|
public ResultCode IsAnyInternetRequestAccepted(ServiceCtx context)
|
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
ulong position = context.Request.PtrBuff[0].Position;
|
|
|
|
ulong size = context.Request.PtrBuff[0].Size;
|
2019-09-04 18:09:20 +02:00
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
int clientId = context.Memory.Read<int>(position);
|
2019-09-04 18:09:20 +02:00
|
|
|
|
|
|
|
context.ResponseData.Write(GeneralServiceManager.Get(clientId).IsAnyInternetRequestAccepted);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:28:41 +01:00
|
|
|
private (IPInterfaceProperties, UnicastIPAddressInformation) GetLocalInterface()
|
|
|
|
{
|
|
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
|
|
{
|
|
|
|
return (null, null);
|
|
|
|
}
|
|
|
|
|
2022-03-15 03:49:35 +01:00
|
|
|
if (_targetPropertiesCache != null && _targetAddressInfoCache != null)
|
|
|
|
{
|
|
|
|
return (_targetPropertiesCache, _targetAddressInfoCache);
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:28:41 +01:00
|
|
|
IPInterfaceProperties targetProperties = null;
|
|
|
|
UnicastIPAddressInformation targetAddressInfo = null;
|
|
|
|
|
|
|
|
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
|
|
|
|
|
|
|
foreach (NetworkInterface adapter in interfaces)
|
|
|
|
{
|
|
|
|
// Ignore loopback and non IPv4 capable interface.
|
2021-04-13 03:04:18 +02:00
|
|
|
if (targetProperties == null && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback && adapter.Supports(NetworkInterfaceComponent.IPv4))
|
2020-02-17 16:28:41 +01:00
|
|
|
{
|
|
|
|
IPInterfaceProperties properties = adapter.GetIPProperties();
|
|
|
|
|
2021-04-13 03:04:18 +02:00
|
|
|
if (properties.GatewayAddresses.Count > 0 && properties.DnsAddresses.Count > 0)
|
2020-02-17 16:28:41 +01:00
|
|
|
{
|
|
|
|
foreach (UnicastIPAddressInformation info in properties.UnicastAddresses)
|
|
|
|
{
|
|
|
|
// Only accept an IPv4 address
|
|
|
|
if (info.Address.GetAddressBytes().Length == 4)
|
|
|
|
{
|
|
|
|
targetProperties = properties;
|
|
|
|
targetAddressInfo = info;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 03:49:35 +01:00
|
|
|
_targetPropertiesCache = targetProperties;
|
|
|
|
_targetAddressInfoCache = targetAddressInfo;
|
|
|
|
|
2020-02-17 16:28:41 +01:00
|
|
|
return (targetProperties, targetAddressInfo);
|
|
|
|
}
|
|
|
|
|
2022-03-15 03:49:35 +01: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 19:37:13 +02:00
|
|
|
protected override void Dispose(bool isDisposing)
|
2019-09-04 18:09:20 +02:00
|
|
|
{
|
2021-06-29 19:37:13 +02:00
|
|
|
if (isDisposing)
|
|
|
|
{
|
2022-03-15 03:49:35 +01:00
|
|
|
NetworkChange.NetworkAddressChanged -= LocalInterfaceCacheHandler;
|
|
|
|
|
2021-06-29 19:37:13 +02:00
|
|
|
GeneralServiceManager.Remove(_generalServiceDetail.ClientId);
|
|
|
|
}
|
2019-09-04 18:09:20 +02:00
|
|
|
}
|
2018-02-28 04:31:52 +01:00
|
|
|
}
|
2019-07-12 03:13:43 +02:00
|
|
|
}
|