IManager.cs 13 KB

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