IManager.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 System.Text;
  7. namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd
  8. {
  9. [Service("nsd:a")] // Max sessions: 5
  10. [Service("nsd:u")] // Max sessions: 20
  11. class IManager : IpcService
  12. {
  13. private NsdSettings _nsdSettings;
  14. private FqdnResolver _fqdnResolver;
  15. private bool _isInitialized = false;
  16. public IManager(ServiceCtx context)
  17. {
  18. // TODO: Load nsd settings through the savedata 0x80000000000000B0 (nsdsave:/).
  19. NxSettings.Settings.TryGetValue("nsd!test_mode", out object testMode);
  20. _nsdSettings = new NsdSettings
  21. {
  22. Initialized = true,
  23. TestMode = (bool)testMode
  24. };
  25. _fqdnResolver = new FqdnResolver(_nsdSettings);
  26. _isInitialized = true;
  27. }
  28. [CommandHipc(10)]
  29. // GetSettingName() -> buffer<unknown<0x100>, 0x16>
  30. public ResultCode GetSettingName(ServiceCtx context)
  31. {
  32. (ulong outputPosition, ulong outputSize) = context.Request.GetBufferType0x22();
  33. ResultCode result = _fqdnResolver.GetSettingName(context, out string settingName);
  34. if (result == ResultCode.Success)
  35. {
  36. byte[] settingNameBuffer = Encoding.UTF8.GetBytes(settingName + '\0');
  37. context.Memory.Write(outputPosition, settingNameBuffer);
  38. }
  39. return result;
  40. }
  41. [CommandHipc(11)]
  42. // GetEnvironmentIdentifier() -> buffer<unknown<8>, 0x16>
  43. public ResultCode GetEnvironmentIdentifier(ServiceCtx context)
  44. {
  45. (ulong outputPosition, ulong outputSize) = context.Request.GetBufferType0x22();
  46. ResultCode result = _fqdnResolver.GetEnvironmentIdentifier(context, out string identifier);
  47. if (result == ResultCode.Success)
  48. {
  49. byte[] identifierBuffer = Encoding.UTF8.GetBytes(identifier + '\0');
  50. context.Memory.Write(outputPosition, identifierBuffer);
  51. }
  52. return result;
  53. }
  54. [CommandHipc(12)]
  55. // GetDeviceId() -> bytes<0x10, 1>
  56. public ResultCode GetDeviceId(ServiceCtx context)
  57. {
  58. // NOTE: Stubbed in system module.
  59. return ResultCode.Success;
  60. }
  61. [CommandHipc(13)]
  62. // DeleteSettings(u32)
  63. public ResultCode DeleteSettings(ServiceCtx context)
  64. {
  65. uint unknown = context.RequestData.ReadUInt32();
  66. if (!_isInitialized)
  67. {
  68. return ResultCode.ServiceNotInitialized;
  69. }
  70. if (unknown > 1)
  71. {
  72. return ResultCode.InvalidArgument;
  73. }
  74. if (unknown == 1)
  75. {
  76. NxSettings.Settings.TryGetValue("nsd!environment_identifier", out object environmentIdentifier);
  77. if ((string)environmentIdentifier == _nsdSettings.Environment)
  78. {
  79. // TODO: Call nn::fs::DeleteSystemFile() to delete the savedata file and return ResultCode.
  80. }
  81. else
  82. {
  83. // TODO: Mount the savedata file and return ResultCode.
  84. }
  85. }
  86. else
  87. {
  88. // TODO: Call nn::fs::DeleteSystemFile() to delete the savedata file and return ResultCode.
  89. }
  90. return ResultCode.Success;
  91. }
  92. [CommandHipc(14)]
  93. // ImportSettings(u32, buffer<unknown, 5>) -> buffer<unknown, 6>
  94. public ResultCode ImportSettings(ServiceCtx context)
  95. {
  96. throw new ServiceNotImplementedException(this, context, false);
  97. }
  98. [CommandHipc(15)]
  99. // Unknown(bytes<1>)
  100. public ResultCode Unknown(ServiceCtx context)
  101. {
  102. throw new ServiceNotImplementedException(this, context, false);
  103. }
  104. [CommandHipc(20)]
  105. // Resolve(buffer<unknown<0x100>, 0x15>) -> buffer<unknown<0x100>, 0x16>
  106. public ResultCode Resolve(ServiceCtx context)
  107. {
  108. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  109. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  110. ResultCode result = _fqdnResolver.ResolveEx(context, out _, out string resolvedAddress);
  111. if ((ulong)resolvedAddress.Length > outputSize)
  112. {
  113. return ResultCode.InvalidArgument;
  114. }
  115. byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress);
  116. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  117. context.Memory.Write(outputPosition, resolvedAddressBuffer);
  118. return result;
  119. }
  120. [CommandHipc(21)]
  121. // ResolveEx(buffer<unknown<0x100>, 0x15>) -> (u32, buffer<unknown<0x100>, 0x16>)
  122. public ResultCode ResolveEx(ServiceCtx context)
  123. {
  124. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  125. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  126. ResultCode result = _fqdnResolver.ResolveEx(context, out ResultCode errorCode, out string resolvedAddress);
  127. if ((ulong)resolvedAddress.Length > outputSize)
  128. {
  129. return ResultCode.InvalidArgument;
  130. }
  131. byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress);
  132. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  133. context.Memory.Write(outputPosition, resolvedAddressBuffer);
  134. context.ResponseData.Write((int)errorCode);
  135. return result;
  136. }
  137. [CommandHipc(30)]
  138. // GetNasServiceSetting(buffer<unknown<0x10>, 0x15>) -> buffer<unknown<0x108>, 0x16>
  139. public ResultCode GetNasServiceSetting(ServiceCtx context)
  140. {
  141. throw new ServiceNotImplementedException(this, context, false);
  142. }
  143. [CommandHipc(31)]
  144. // GetNasServiceSettingEx(buffer<unknown<0x10>, 0x15>) -> (u32, buffer<unknown<0x108>, 0x16>)
  145. public ResultCode GetNasServiceSettingEx(ServiceCtx context)
  146. {
  147. throw new ServiceNotImplementedException(this, context, false);
  148. }
  149. [CommandHipc(40)]
  150. // GetNasRequestFqdn() -> buffer<unknown<0x100>, 0x16>
  151. public ResultCode GetNasRequestFqdn(ServiceCtx context)
  152. {
  153. throw new ServiceNotImplementedException(this, context, false);
  154. }
  155. [CommandHipc(41)]
  156. // GetNasRequestFqdnEx() -> (u32, buffer<unknown<0x100>, 0x16>)
  157. public ResultCode GetNasRequestFqdnEx(ServiceCtx context)
  158. {
  159. throw new ServiceNotImplementedException(this, context, false);
  160. }
  161. [CommandHipc(42)]
  162. // GetNasApiFqdn() -> buffer<unknown<0x100>, 0x16>
  163. public ResultCode GetNasApiFqdn(ServiceCtx context)
  164. {
  165. throw new ServiceNotImplementedException(this, context, false);
  166. }
  167. [CommandHipc(43)]
  168. // GetNasApiFqdnEx() -> (u32, buffer<unknown<0x100>, 0x16>)
  169. public ResultCode GetNasApiFqdnEx(ServiceCtx context)
  170. {
  171. throw new ServiceNotImplementedException(this, context, false);
  172. }
  173. [CommandHipc(50)]
  174. // GetCurrentSetting() -> buffer<unknown<0x12bf0>, 0x16>
  175. public ResultCode GetCurrentSetting(ServiceCtx context)
  176. {
  177. throw new ServiceNotImplementedException(this, context, false);
  178. }
  179. [CommandHipc(60)]
  180. // ReadSaveDataFromFsForTest() -> buffer<unknown<0x12bf0>, 0x16>
  181. public ResultCode ReadSaveDataFromFsForTest(ServiceCtx context)
  182. {
  183. if (!_isInitialized)
  184. {
  185. return ResultCode.ServiceNotInitialized;
  186. }
  187. // TODO: Call nn::nsd::detail::fs::ReadSaveDataWithOffset() at offset 0 to write the
  188. // whole savedata inside the buffer.
  189. Logger.Stub?.PrintStub(LogClass.ServiceNsd);
  190. return ResultCode.Success;
  191. }
  192. [CommandHipc(61)]
  193. // WriteSaveDataToFsForTest(buffer<unknown<0x12bf0>, 0x15>)
  194. public ResultCode WriteSaveDataToFsForTest(ServiceCtx context)
  195. {
  196. // NOTE: Stubbed in system module.
  197. if (_isInitialized)
  198. {
  199. return ResultCode.NotImplemented;
  200. }
  201. else
  202. {
  203. return ResultCode.ServiceNotInitialized;
  204. }
  205. }
  206. [CommandHipc(62)]
  207. // DeleteSaveDataOfFsForTest()
  208. public ResultCode DeleteSaveDataOfFsForTest(ServiceCtx context)
  209. {
  210. // NOTE: Stubbed in system module.
  211. if (_isInitialized)
  212. {
  213. return ResultCode.NotImplemented;
  214. }
  215. else
  216. {
  217. return ResultCode.ServiceNotInitialized;
  218. }
  219. }
  220. [CommandHipc(63)]
  221. // IsChangeEnvironmentIdentifierDisabled() -> bytes<1>
  222. public ResultCode IsChangeEnvironmentIdentifierDisabled(ServiceCtx context)
  223. {
  224. throw new ServiceNotImplementedException(this, context, false);
  225. }
  226. }
  227. }