IManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.Exceptions;
  4. using Ryujinx.HLE.HOS.Services.Settings;
  5. using Ryujinx.HLE.HOS.Services.Sockets.Nsd.Manager;
  6. using Ryujinx.HLE.HOS.Services.Sockets.Nsd.Types;
  7. using System.Text;
  8. namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd
  9. {
  10. [Service("nsd:a")] // Max sessions: 5
  11. [Service("nsd:u")] // Max sessions: 20
  12. class IManager : IpcService
  13. {
  14. public static readonly NsdSettings NsdSettings;
  15. private readonly FqdnResolver _fqdnResolver;
  16. private bool _isInitialized = false;
  17. public IManager(ServiceCtx context)
  18. {
  19. _fqdnResolver = new FqdnResolver();
  20. _isInitialized = true;
  21. }
  22. static IManager()
  23. {
  24. // TODO: Load nsd settings through the savedata 0x80000000000000B0 (nsdsave:/).
  25. if (!NxSettings.Settings.TryGetValue("nsd!test_mode", out object testMode))
  26. {
  27. // return ResultCode.InvalidSettingsValue;
  28. }
  29. if (!NxSettings.Settings.TryGetValue("nsd!environment_identifier", out object environmentIdentifier))
  30. {
  31. // return ResultCode.InvalidSettingsValue;
  32. }
  33. NsdSettings = new NsdSettings
  34. {
  35. Initialized = true,
  36. TestMode = (bool)testMode,
  37. Environment = (string)environmentIdentifier
  38. };
  39. }
  40. [CommandHipc(5)] // 11.0.0+
  41. // GetSettingUrl() -> buffer<unknown<0x100>, 0x16>
  42. public ResultCode GetSettingUrl(ServiceCtx context)
  43. {
  44. throw new ServiceNotImplementedException(this, context);
  45. }
  46. [CommandHipc(10)]
  47. // GetSettingName() -> buffer<unknown<0x100>, 0x16>
  48. public ResultCode GetSettingName(ServiceCtx context)
  49. {
  50. throw new ServiceNotImplementedException(this, context);
  51. }
  52. [CommandHipc(11)]
  53. // GetEnvironmentIdentifier() -> buffer<bytes<8> environment_identifier, 0x16>
  54. public ResultCode GetEnvironmentIdentifier(ServiceCtx context)
  55. {
  56. (ulong outputPosition, ulong outputSize) = context.Request.GetBufferType0x22();
  57. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  58. ResultCode result = _fqdnResolver.GetEnvironmentIdentifier(out string identifier);
  59. if (result == ResultCode.Success)
  60. {
  61. byte[] identifierBuffer = Encoding.UTF8.GetBytes(identifier);
  62. context.Memory.Write(outputPosition, identifierBuffer);
  63. }
  64. return result;
  65. }
  66. [CommandHipc(12)]
  67. // GetDeviceId() -> bytes<0x10, 1>
  68. public ResultCode GetDeviceId(ServiceCtx context)
  69. {
  70. // NOTE: Stubbed in system module.
  71. return ResultCode.Success;
  72. }
  73. [CommandHipc(13)]
  74. // DeleteSettings(u32)
  75. public ResultCode DeleteSettings(ServiceCtx context)
  76. {
  77. uint unknown = context.RequestData.ReadUInt32();
  78. if (!_isInitialized)
  79. {
  80. return ResultCode.ServiceNotInitialized;
  81. }
  82. if (unknown > 1)
  83. {
  84. return ResultCode.InvalidArgument;
  85. }
  86. if (unknown == 1)
  87. {
  88. NxSettings.Settings.TryGetValue("nsd!environment_identifier", out object environmentIdentifier);
  89. if ((string)environmentIdentifier == NsdSettings.Environment)
  90. {
  91. // TODO: Call nn::fs::DeleteSystemFile() to delete the savedata file and return ResultCode.
  92. }
  93. else
  94. {
  95. // TODO: Mount the savedata file and return ResultCode.
  96. }
  97. }
  98. else
  99. {
  100. // TODO: Call nn::fs::DeleteSystemFile() to delete the savedata file and return ResultCode.
  101. }
  102. return ResultCode.Success;
  103. }
  104. [CommandHipc(14)]
  105. // ImportSettings(u32, buffer<unknown, 5>) -> buffer<unknown, 6>
  106. public ResultCode ImportSettings(ServiceCtx context)
  107. {
  108. throw new ServiceNotImplementedException(this, context);
  109. }
  110. [CommandHipc(15)] // 4.0.0+
  111. // SetChangeEnvironmentIdentifierDisabled(bytes<1>)
  112. public ResultCode SetChangeEnvironmentIdentifierDisabled(ServiceCtx context)
  113. {
  114. byte disabled = context.RequestData.ReadByte();
  115. // TODO: When sys:set service calls will be implemented
  116. /*
  117. if (((nn::settings::detail::GetServiceDiscoveryControlSettings() ^ disabled) & 1) != 0 )
  118. {
  119. nn::settings::detail::SetServiceDiscoveryControlSettings(disabled & 1);
  120. }
  121. */
  122. Logger.Stub?.PrintStub(LogClass.ServiceNsd, new { disabled });
  123. return ResultCode.Success;
  124. }
  125. [CommandHipc(20)]
  126. // Resolve(buffer<unknown<0x100>, 0x15>) -> buffer<unknown<0x100>, 0x16>
  127. public ResultCode Resolve(ServiceCtx context)
  128. {
  129. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  130. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  131. ResultCode result = _fqdnResolver.ResolveEx(context, out _, out string resolvedAddress);
  132. if ((ulong)resolvedAddress.Length > outputSize)
  133. {
  134. return ResultCode.InvalidArgument;
  135. }
  136. byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress);
  137. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  138. context.Memory.Write(outputPosition, resolvedAddressBuffer);
  139. return result;
  140. }
  141. [CommandHipc(21)]
  142. // ResolveEx(buffer<unknown<0x100>, 0x15>) -> (u32, buffer<unknown<0x100>, 0x16>)
  143. public ResultCode ResolveEx(ServiceCtx context)
  144. {
  145. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  146. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  147. ResultCode result = _fqdnResolver.ResolveEx(context, out ResultCode errorCode, out string resolvedAddress);
  148. if ((ulong)resolvedAddress.Length > outputSize)
  149. {
  150. return ResultCode.InvalidArgument;
  151. }
  152. byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress);
  153. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  154. context.Memory.Write(outputPosition, resolvedAddressBuffer);
  155. context.ResponseData.Write((int)errorCode);
  156. return result;
  157. }
  158. [CommandHipc(30)]
  159. // GetNasServiceSetting(buffer<unknown<0x10>, 0x15>) -> buffer<unknown<0x108>, 0x16>
  160. public ResultCode GetNasServiceSetting(ServiceCtx context)
  161. {
  162. throw new ServiceNotImplementedException(this, context);
  163. }
  164. [CommandHipc(31)]
  165. // GetNasServiceSettingEx(buffer<unknown<0x10>, 0x15>) -> (u32, buffer<unknown<0x108>, 0x16>)
  166. public ResultCode GetNasServiceSettingEx(ServiceCtx context)
  167. {
  168. throw new ServiceNotImplementedException(this, context);
  169. }
  170. [CommandHipc(40)]
  171. // GetNasRequestFqdn() -> buffer<unknown<0x100>, 0x16>
  172. public ResultCode GetNasRequestFqdn(ServiceCtx context)
  173. {
  174. throw new ServiceNotImplementedException(this, context);
  175. }
  176. [CommandHipc(41)]
  177. // GetNasRequestFqdnEx() -> (u32, buffer<unknown<0x100>, 0x16>)
  178. public ResultCode GetNasRequestFqdnEx(ServiceCtx context)
  179. {
  180. throw new ServiceNotImplementedException(this, context);
  181. }
  182. [CommandHipc(42)]
  183. // GetNasApiFqdn() -> buffer<unknown<0x100>, 0x16>
  184. public ResultCode GetNasApiFqdn(ServiceCtx context)
  185. {
  186. throw new ServiceNotImplementedException(this, context);
  187. }
  188. [CommandHipc(43)]
  189. // GetNasApiFqdnEx() -> (u32, buffer<unknown<0x100>, 0x16>)
  190. public ResultCode GetNasApiFqdnEx(ServiceCtx context)
  191. {
  192. throw new ServiceNotImplementedException(this, context);
  193. }
  194. [CommandHipc(50)]
  195. // GetCurrentSetting() -> buffer<unknown<0x12bf0>, 0x16>
  196. public ResultCode GetCurrentSetting(ServiceCtx context)
  197. {
  198. throw new ServiceNotImplementedException(this, context);
  199. }
  200. [CommandHipc(51)] // 9.0.0+
  201. // WriteTestParameter(buffer<?>)
  202. public ResultCode WriteTestParameter(ServiceCtx context)
  203. {
  204. // TODO: Write test parameter through the savedata 0x80000000000000B0 (nsdsave:/test_parameter).
  205. throw new ServiceNotImplementedException(this, context);
  206. }
  207. [CommandHipc(52)] // 9.0.0+
  208. // ReadTestParameter() -> buffer<?>
  209. public ResultCode ReadTestParameter(ServiceCtx context)
  210. {
  211. // TODO: Read test parameter through the savedata 0x80000000000000B0 (nsdsave:/test_parameter).
  212. throw new ServiceNotImplementedException(this, context);
  213. }
  214. [CommandHipc(60)]
  215. // ReadSaveDataFromFsForTest() -> buffer<unknown<0x12bf0>, 0x16>
  216. public ResultCode ReadSaveDataFromFsForTest(ServiceCtx context)
  217. {
  218. if (!_isInitialized)
  219. {
  220. return ResultCode.ServiceNotInitialized;
  221. }
  222. // TODO: Read the savedata 0x80000000000000B0 (nsdsave:/file) and write it inside the buffer.
  223. Logger.Stub?.PrintStub(LogClass.ServiceNsd);
  224. return ResultCode.Success;
  225. }
  226. [CommandHipc(61)]
  227. // WriteSaveDataToFsForTest(buffer<unknown<0x12bf0>, 0x15>)
  228. public ResultCode WriteSaveDataToFsForTest(ServiceCtx context)
  229. {
  230. if (!_isInitialized)
  231. {
  232. return ResultCode.ServiceNotInitialized;
  233. }
  234. // TODO: When sys:set service calls will be implemented
  235. /*
  236. if (nn::settings::detail::GetSettingsItemValueSize("nsd", "test_mode") != 1)
  237. {
  238. return ResultCode.InvalidSettingsValue;
  239. }
  240. */
  241. if (!NsdSettings.TestMode)
  242. {
  243. return ResultCode.InvalidSettingsValue;
  244. }
  245. // TODO: Write the buffer inside the savedata 0x80000000000000B0 (nsdsave:/file).
  246. Logger.Stub?.PrintStub(LogClass.ServiceNsd);
  247. return ResultCode.Success;
  248. }
  249. [CommandHipc(62)]
  250. // DeleteSaveDataOfFsForTest()
  251. public ResultCode DeleteSaveDataOfFsForTest(ServiceCtx context)
  252. {
  253. if (!_isInitialized)
  254. {
  255. return ResultCode.ServiceNotInitialized;
  256. }
  257. // TODO: When sys:set service calls will be implemented
  258. /*
  259. if (nn::settings::detail::GetSettingsItemValueSize("nsd", "test_mode") != 1)
  260. {
  261. return ResultCode.InvalidSettingsValue;
  262. }
  263. */
  264. if (!NsdSettings.TestMode)
  265. {
  266. return ResultCode.InvalidSettingsValue;
  267. }
  268. // TODO: Delete the savedata 0x80000000000000B0.
  269. Logger.Stub?.PrintStub(LogClass.ServiceNsd);
  270. return ResultCode.Success;
  271. }
  272. [CommandHipc(63)] // 4.0.0+
  273. // IsChangeEnvironmentIdentifierDisabled() -> bytes<1>
  274. public ResultCode IsChangeEnvironmentIdentifierDisabled(ServiceCtx context)
  275. {
  276. // TODO: When sys:set service calls will be implemented use nn::settings::detail::GetServiceDiscoveryControlSettings()
  277. bool disabled = false;
  278. context.ResponseData.Write(disabled);
  279. Logger.Stub?.PrintStub(LogClass.ServiceNsd, new { disabled });
  280. return ResultCode.Success;
  281. }
  282. [CommandHipc(100)] // 10.0.0+
  283. // GetApplicationServerEnvironmentType() -> bytes<1>
  284. public ResultCode GetApplicationServerEnvironmentType(ServiceCtx context)
  285. {
  286. // TODO: Mount the savedata 0x80000000000000B0 (nsdsave:/test_parameter) and returns the environment type stored inside if the mount succeed.
  287. // Returns ResultCode.NullOutputObject if failed.
  288. ResultCode result = _fqdnResolver.GetEnvironmentIdentifier(out string identifier);
  289. if (result != ResultCode.Success)
  290. {
  291. return result;
  292. }
  293. byte environmentType = identifier.Substring(0, 2) switch
  294. {
  295. "lp" => (byte)ApplicationServerEnvironmentType.Lp,
  296. "sd" => (byte)ApplicationServerEnvironmentType.Sd,
  297. "sp" => (byte)ApplicationServerEnvironmentType.Sp,
  298. "dp" => (byte)ApplicationServerEnvironmentType.Dp,
  299. _ => (byte)ApplicationServerEnvironmentType.None
  300. };
  301. context.ResponseData.Write(environmentType);
  302. return ResultCode.Success;
  303. }
  304. [CommandHipc(101)] // 10.0.0+
  305. // SetApplicationServerEnvironmentType(bytes<1>)
  306. public ResultCode SetApplicationServerEnvironmentType(ServiceCtx context)
  307. {
  308. throw new ServiceNotImplementedException(this, context);
  309. }
  310. [CommandHipc(102)] // 10.0.0+
  311. // DeleteApplicationServerEnvironmentType()
  312. public ResultCode DeleteApplicationServerEnvironmentType(ServiceCtx context)
  313. {
  314. throw new ServiceNotImplementedException(this, context);
  315. }
  316. }
  317. }