IClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using Ryujinx.HLE.OsHle.Ipc;
  2. using Ryujinx.HLE.OsHle.Utilities;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading.Tasks;
  8. namespace Ryujinx.HLE.OsHle.Services.Bsd
  9. {
  10. class IClient : IpcService
  11. {
  12. private Dictionary<int, ServiceProcessRequest> m_Commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  14. private List<BsdSocket> Sockets = new List<BsdSocket>();
  15. public IClient()
  16. {
  17. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  18. {
  19. { 0, Initialize },
  20. { 1, StartMonitoring },
  21. { 2, Socket },
  22. { 6, Poll },
  23. { 8, Recv },
  24. { 10, Send },
  25. { 11, SendTo },
  26. { 12, Accept },
  27. { 13, Bind },
  28. { 14, Connect },
  29. { 18, Listen },
  30. { 21, SetSockOpt },
  31. { 26, Close }
  32. };
  33. }
  34. //(u32, u32, u32, u32, u32, u32, u32, u32, u64 pid, u64 transferMemorySize, pid, KObject) -> u32 bsd_errno
  35. public long Initialize(ServiceCtx Context)
  36. {
  37. /*
  38. typedef struct {
  39. u32 version; // Observed 1 on 2.0 LibAppletWeb, 2 on 3.0.
  40. u32 tcp_tx_buf_size; // Size of the TCP transfer (send) buffer (initial or fixed).
  41. u32 tcp_rx_buf_size; // Size of the TCP recieve buffer (initial or fixed).
  42. u32 tcp_tx_buf_max_size; // Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value.
  43. u32 tcp_rx_buf_max_size; // Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial value.
  44. u32 udp_tx_buf_size; // Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
  45. u32 udp_rx_buf_size; // Size of the UDP receive buffer (typically 0xA500 bytes).
  46. u32 sb_efficiency; // Number of buffers for each socket (standard values range from 1 to 8).
  47. } BsdBufferConfig;
  48. */
  49. Context.ResponseData.Write(0);
  50. //Todo: Stub
  51. return 0;
  52. }
  53. //(u64, pid)
  54. public long StartMonitoring(ServiceCtx Context)
  55. {
  56. //Todo: Stub
  57. return 0;
  58. }
  59. //(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
  60. public long Socket(ServiceCtx Context)
  61. {
  62. BsdSocket NewBsdSocket = new BsdSocket
  63. {
  64. Family = Context.RequestData.ReadInt32(),
  65. Type = Context.RequestData.ReadInt32(),
  66. Protocol = Context.RequestData.ReadInt32()
  67. };
  68. Sockets.Add(NewBsdSocket);
  69. NewBsdSocket.Handle = new Socket((AddressFamily)NewBsdSocket.Family,
  70. (SocketType)NewBsdSocket.Type,
  71. (ProtocolType)NewBsdSocket.Protocol);
  72. Context.ResponseData.Write(Sockets.Count - 1);
  73. Context.ResponseData.Write(0);
  74. return 0;
  75. }
  76. //(u32, u32, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
  77. public long Poll(ServiceCtx Context)
  78. {
  79. int PollCount = Context.RequestData.ReadInt32();
  80. int TimeOut = Context.RequestData.ReadInt32();
  81. //https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/poll.h
  82. //https://msdn.microsoft.com/fr-fr/library/system.net.sockets.socket.poll(v=vs.110).aspx
  83. //https://github.com/switchbrew/libnx/blob/e0457c4534b3c37426d83e1a620f82cb28c3b528/nx/source/services/bsd.c#L343
  84. //https://github.com/TuxSH/ftpd/blob/switch_pr/source/ftp.c#L1634
  85. //https://linux.die.net/man/2/poll
  86. byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
  87. Context.Request.SendBuff[0].Size);
  88. int SocketId = Get32(SentBuffer, 0);
  89. int RequestedEvents = Get16(SentBuffer, 4);
  90. int ReturnedEvents = Get16(SentBuffer, 6);
  91. //Todo: Stub - Need to implemented the Type-22 buffer.
  92. Context.ResponseData.Write(1);
  93. Context.ResponseData.Write(0);
  94. return 0;
  95. }
  96. //(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
  97. public long Recv(ServiceCtx Context)
  98. {
  99. int SocketId = Context.RequestData.ReadInt32();
  100. int SocketFlags = Context.RequestData.ReadInt32();
  101. byte[] ReceivedBuffer = new byte[Context.Request.ReceiveBuff[0].Size];
  102. try
  103. {
  104. int BytesRead = Sockets[SocketId].Handle.Receive(ReceivedBuffer);
  105. //Logging.Debug("Received Buffer:" + Environment.NewLine + Logging.HexDump(ReceivedBuffer));
  106. Context.Memory.WriteBytes(Context.Request.ReceiveBuff[0].Position, ReceivedBuffer);
  107. Context.ResponseData.Write(BytesRead);
  108. Context.ResponseData.Write(0);
  109. }
  110. catch (SocketException Ex)
  111. {
  112. Context.ResponseData.Write(-1);
  113. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  114. }
  115. return 0;
  116. }
  117. //(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
  118. public long Send(ServiceCtx Context)
  119. {
  120. int SocketId = Context.RequestData.ReadInt32();
  121. int SocketFlags = Context.RequestData.ReadInt32();
  122. byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
  123. Context.Request.SendBuff[0].Size);
  124. try
  125. {
  126. //Logging.Debug("Sent Buffer:" + Environment.NewLine + Logging.HexDump(SentBuffer));
  127. int BytesSent = Sockets[SocketId].Handle.Send(SentBuffer);
  128. Context.ResponseData.Write(BytesSent);
  129. Context.ResponseData.Write(0);
  130. }
  131. catch (SocketException Ex)
  132. {
  133. Context.ResponseData.Write(-1);
  134. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  135. }
  136. return 0;
  137. }
  138. //(u32 socket, u32 flags, buffer<i8, 0x21, 0>, buffer<sockaddr, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
  139. public long SendTo(ServiceCtx Context)
  140. {
  141. int SocketId = Context.RequestData.ReadInt32();
  142. int SocketFlags = Context.RequestData.ReadInt32();
  143. byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
  144. Context.Request.SendBuff[0].Size);
  145. byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[1].Position,
  146. Context.Request.SendBuff[1].Size);
  147. if (!Sockets[SocketId].Handle.Connected)
  148. {
  149. try
  150. {
  151. ParseAddrBuffer(SocketId, AddressBuffer);
  152. Sockets[SocketId].Handle.Connect(Sockets[SocketId].RemoteEP);
  153. }
  154. catch (SocketException Ex)
  155. {
  156. Context.ResponseData.Write(-1);
  157. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  158. }
  159. }
  160. try
  161. {
  162. //Logging.Debug("Sent Buffer:" + Environment.NewLine + Logging.HexDump(SentBuffer));
  163. int BytesSent = Sockets[SocketId].Handle.Send(SentBuffer);
  164. Context.ResponseData.Write(BytesSent);
  165. Context.ResponseData.Write(0);
  166. }
  167. catch (SocketException Ex)
  168. {
  169. Context.ResponseData.Write(-1);
  170. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  171. }
  172. return 0;
  173. }
  174. //(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<sockaddr, 0x22, 0> addr)
  175. public long Accept(ServiceCtx Context)
  176. {
  177. int SocketId = Context.RequestData.ReadInt32();
  178. long AddrBufferPtr = Context.Request.ReceiveBuff[0].Position;
  179. Socket HandleAccept = null;
  180. Task TimeOut = Task.Factory.StartNew(() =>
  181. {
  182. try
  183. {
  184. HandleAccept = Sockets[SocketId].Handle.Accept();
  185. }
  186. catch (SocketException Ex)
  187. {
  188. Context.ResponseData.Write(-1);
  189. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  190. }
  191. });
  192. TimeOut.Wait(10000);
  193. if (HandleAccept != null)
  194. {
  195. BsdSocket NewBsdSocket = new BsdSocket
  196. {
  197. IpAddress = ((IPEndPoint)Sockets[SocketId].Handle.LocalEndPoint).Address,
  198. RemoteEP = ((IPEndPoint)Sockets[SocketId].Handle.LocalEndPoint),
  199. Handle = HandleAccept
  200. };
  201. Sockets.Add(NewBsdSocket);
  202. using (MemoryStream MS = new MemoryStream())
  203. {
  204. BinaryWriter Writer = new BinaryWriter(MS);
  205. Writer.Write((byte)0);
  206. Writer.Write((byte)NewBsdSocket.Handle.AddressFamily);
  207. Writer.Write((short)((IPEndPoint)NewBsdSocket.Handle.LocalEndPoint).Port);
  208. byte[] IpAddress = NewBsdSocket.IpAddress.GetAddressBytes();
  209. Writer.Write(IpAddress);
  210. Context.Memory.WriteBytes(AddrBufferPtr, MS.ToArray());
  211. Context.ResponseData.Write(Sockets.Count - 1);
  212. Context.ResponseData.Write(0);
  213. Context.ResponseData.Write(MS.Length);
  214. }
  215. }
  216. else
  217. {
  218. Context.ResponseData.Write(-1);
  219. Context.ResponseData.Write((int)BsdError.Timeout);
  220. }
  221. return 0;
  222. }
  223. //(u32 socket, buffer<sockaddr, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
  224. public long Bind(ServiceCtx Context)
  225. {
  226. int SocketId = Context.RequestData.ReadInt32();
  227. byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
  228. Context.Request.SendBuff[0].Size);
  229. try
  230. {
  231. ParseAddrBuffer(SocketId, AddressBuffer);
  232. Context.ResponseData.Write(0);
  233. Context.ResponseData.Write(0);
  234. }
  235. catch (SocketException Ex)
  236. {
  237. Context.ResponseData.Write(-1);
  238. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  239. }
  240. return 0;
  241. }
  242. //(u32 socket, buffer<sockaddr, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
  243. public long Connect(ServiceCtx Context)
  244. {
  245. int SocketId = Context.RequestData.ReadInt32();
  246. byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
  247. Context.Request.SendBuff[0].Size);
  248. try
  249. {
  250. ParseAddrBuffer(SocketId, AddressBuffer);
  251. Sockets[SocketId].Handle.Connect(Sockets[SocketId].RemoteEP);
  252. Context.ResponseData.Write(0);
  253. Context.ResponseData.Write(0);
  254. }
  255. catch (SocketException Ex)
  256. {
  257. Context.ResponseData.Write(-1);
  258. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  259. }
  260. return 0;
  261. }
  262. //(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
  263. public long Listen(ServiceCtx Context)
  264. {
  265. int SocketId = Context.RequestData.ReadInt32();
  266. int BackLog = Context.RequestData.ReadInt32();
  267. try
  268. {
  269. Sockets[SocketId].Handle.Bind(Sockets[SocketId].RemoteEP);
  270. Sockets[SocketId].Handle.Listen(BackLog);
  271. Context.ResponseData.Write(0);
  272. Context.ResponseData.Write(0);
  273. }
  274. catch (SocketException Ex)
  275. {
  276. Context.ResponseData.Write(-1);
  277. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  278. }
  279. return 0;
  280. }
  281. //(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
  282. public long SetSockOpt(ServiceCtx Context)
  283. {
  284. int SocketId = Context.RequestData.ReadInt32();
  285. SocketOptionLevel SocketLevel = (SocketOptionLevel)Context.RequestData.ReadInt32();
  286. SocketOptionName SocketOptionName = (SocketOptionName)Context.RequestData.ReadInt32();
  287. byte[] SocketOptionValue = Context.Memory.ReadBytes(Context.Request.PtrBuff[0].Position,
  288. Context.Request.PtrBuff[0].Size);
  289. int OptionValue = Get32(SocketOptionValue, 0);
  290. try
  291. {
  292. Sockets[SocketId].Handle.SetSocketOption(SocketLevel, SocketOptionName, OptionValue);
  293. Context.ResponseData.Write(0);
  294. Context.ResponseData.Write(0);
  295. }
  296. catch (SocketException Ex)
  297. {
  298. Context.ResponseData.Write(-1);
  299. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  300. }
  301. return 0;
  302. }
  303. //(u32 socket) -> (i32 ret, u32 bsd_errno)
  304. public long Close(ServiceCtx Context)
  305. {
  306. int SocketId = Context.RequestData.ReadInt32();
  307. try
  308. {
  309. Sockets[SocketId].Handle.Close();
  310. Sockets[SocketId] = null;
  311. Context.ResponseData.Write(0);
  312. Context.ResponseData.Write(0);
  313. }
  314. catch (SocketException Ex)
  315. {
  316. Context.ResponseData.Write(-1);
  317. Context.ResponseData.Write(Ex.ErrorCode - 10000);
  318. }
  319. return 0;
  320. }
  321. public void ParseAddrBuffer(int SocketId, byte[] AddrBuffer)
  322. {
  323. using (MemoryStream MS = new MemoryStream(AddrBuffer))
  324. {
  325. BinaryReader Reader = new BinaryReader(MS);
  326. int Size = Reader.ReadByte();
  327. int Family = Reader.ReadByte();
  328. int Port = EndianSwap.Swap16(Reader.ReadInt16());
  329. string IpAddress = Reader.ReadByte().ToString() + "." +
  330. Reader.ReadByte().ToString() + "." +
  331. Reader.ReadByte().ToString() + "." +
  332. Reader.ReadByte().ToString();
  333. Sockets[SocketId].IpAddress = IPAddress.Parse(IpAddress);
  334. Sockets[SocketId].RemoteEP = new IPEndPoint(Sockets[SocketId].IpAddress, Port);
  335. }
  336. }
  337. private int Get16(byte[] Data, int Address)
  338. {
  339. return
  340. Data[Address + 0] << 0 |
  341. Data[Address + 1] << 8;
  342. }
  343. private int Get32(byte[] Data, int Address)
  344. {
  345. return
  346. Data[Address + 0] << 0 |
  347. Data[Address + 1] << 8 |
  348. Data[Address + 2] << 16 |
  349. Data[Address + 3] << 24;
  350. }
  351. }
  352. }