IClient.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.Utilities;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. namespace Ryujinx.HLE.HOS.Services.Bsd
  9. {
  10. class IClient : IpcService
  11. {
  12. private static Dictionary<WSAError, LinuxError> ErrorMap = new Dictionary<WSAError, LinuxError>
  13. {
  14. // WSAEINTR
  15. {WSAError.WSAEINTR, LinuxError.EINTR},
  16. // WSAEWOULDBLOCK
  17. {WSAError.WSAEWOULDBLOCK, LinuxError.EWOULDBLOCK},
  18. // WSAEINPROGRESS
  19. {WSAError.WSAEINPROGRESS, LinuxError.EINPROGRESS},
  20. // WSAEALREADY
  21. {WSAError.WSAEALREADY, LinuxError.EALREADY},
  22. // WSAENOTSOCK
  23. {WSAError.WSAENOTSOCK, LinuxError.ENOTSOCK},
  24. // WSAEDESTADDRREQ
  25. {WSAError.WSAEDESTADDRREQ, LinuxError.EDESTADDRREQ},
  26. // WSAEMSGSIZE
  27. {WSAError.WSAEMSGSIZE, LinuxError.EMSGSIZE},
  28. // WSAEPROTOTYPE
  29. {WSAError.WSAEPROTOTYPE, LinuxError.EPROTOTYPE},
  30. // WSAENOPROTOOPT
  31. {WSAError.WSAENOPROTOOPT, LinuxError.ENOPROTOOPT},
  32. // WSAEPROTONOSUPPORT
  33. {WSAError.WSAEPROTONOSUPPORT, LinuxError.EPROTONOSUPPORT},
  34. // WSAESOCKTNOSUPPORT
  35. {WSAError.WSAESOCKTNOSUPPORT, LinuxError.ESOCKTNOSUPPORT},
  36. // WSAEOPNOTSUPP
  37. {WSAError.WSAEOPNOTSUPP, LinuxError.EOPNOTSUPP},
  38. // WSAEPFNOSUPPORT
  39. {WSAError.WSAEPFNOSUPPORT, LinuxError.EPFNOSUPPORT},
  40. // WSAEAFNOSUPPORT
  41. {WSAError.WSAEAFNOSUPPORT, LinuxError.EAFNOSUPPORT},
  42. // WSAEADDRINUSE
  43. {WSAError.WSAEADDRINUSE, LinuxError.EADDRINUSE},
  44. // WSAEADDRNOTAVAIL
  45. {WSAError.WSAEADDRNOTAVAIL, LinuxError.EADDRNOTAVAIL},
  46. // WSAENETDOWN
  47. {WSAError.WSAENETDOWN, LinuxError.ENETDOWN},
  48. // WSAENETUNREACH
  49. {WSAError.WSAENETUNREACH, LinuxError.ENETUNREACH},
  50. // WSAENETRESET
  51. {WSAError.WSAENETRESET, LinuxError.ENETRESET},
  52. // WSAECONNABORTED
  53. {WSAError.WSAECONNABORTED, LinuxError.ECONNABORTED},
  54. // WSAECONNRESET
  55. {WSAError.WSAECONNRESET, LinuxError.ECONNRESET},
  56. // WSAENOBUFS
  57. {WSAError.WSAENOBUFS, LinuxError.ENOBUFS},
  58. // WSAEISCONN
  59. {WSAError.WSAEISCONN, LinuxError.EISCONN},
  60. // WSAENOTCONN
  61. {WSAError.WSAENOTCONN, LinuxError.ENOTCONN},
  62. // WSAESHUTDOWN
  63. {WSAError.WSAESHUTDOWN, LinuxError.ESHUTDOWN},
  64. // WSAETOOMANYREFS
  65. {WSAError.WSAETOOMANYREFS, LinuxError.ETOOMANYREFS},
  66. // WSAETIMEDOUT
  67. {WSAError.WSAETIMEDOUT, LinuxError.ETIMEDOUT},
  68. // WSAECONNREFUSED
  69. {WSAError.WSAECONNREFUSED, LinuxError.ECONNREFUSED},
  70. // WSAELOOP
  71. {WSAError.WSAELOOP, LinuxError.ELOOP},
  72. // WSAENAMETOOLONG
  73. {WSAError.WSAENAMETOOLONG, LinuxError.ENAMETOOLONG},
  74. // WSAEHOSTDOWN
  75. {WSAError.WSAEHOSTDOWN, LinuxError.EHOSTDOWN},
  76. // WSAEHOSTUNREACH
  77. {WSAError.WSAEHOSTUNREACH, LinuxError.EHOSTUNREACH},
  78. // WSAENOTEMPTY
  79. {WSAError.WSAENOTEMPTY, LinuxError.ENOTEMPTY},
  80. // WSAEUSERS
  81. {WSAError.WSAEUSERS, LinuxError.EUSERS},
  82. // WSAEDQUOT
  83. {WSAError.WSAEDQUOT, LinuxError.EDQUOT},
  84. // WSAESTALE
  85. {WSAError.WSAESTALE, LinuxError.ESTALE},
  86. // WSAEREMOTE
  87. {WSAError.WSAEREMOTE, LinuxError.EREMOTE},
  88. // WSAEINVAL
  89. {WSAError.WSAEINVAL, LinuxError.EINVAL},
  90. // WSAEFAULT
  91. {WSAError.WSAEFAULT, LinuxError.EFAULT},
  92. // NOERROR
  93. {0, 0}
  94. };
  95. private Dictionary<int, ServiceProcessRequest> m_Commands;
  96. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  97. private bool IsPrivileged;
  98. private List<BsdSocket> Sockets = new List<BsdSocket>();
  99. public IClient(bool IsPrivileged)
  100. {
  101. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  102. {
  103. { 0, RegisterClient },
  104. { 1, StartMonitoring },
  105. { 2, Socket },
  106. { 3, SocketExempt },
  107. { 4, Open },
  108. { 5, Select },
  109. { 6, Poll },
  110. { 7, Sysctl },
  111. { 8, Recv },
  112. { 9, RecvFrom },
  113. { 10, Send },
  114. { 11, SendTo },
  115. { 12, Accept },
  116. { 13, Bind },
  117. { 14, Connect },
  118. { 15, GetPeerName },
  119. { 16, GetSockName },
  120. { 17, GetSockOpt },
  121. { 18, Listen },
  122. { 19, Ioctl },
  123. { 20, Fcntl },
  124. { 21, SetSockOpt },
  125. { 22, Shutdown },
  126. { 23, ShutdownAllSockets },
  127. { 24, Write },
  128. { 25, Read },
  129. { 26, Close },
  130. { 27, DuplicateSocket },
  131. };
  132. this.IsPrivileged = IsPrivileged;
  133. }
  134. private LinuxError ConvertError(WSAError ErrorCode)
  135. {
  136. LinuxError Errno;
  137. if (!ErrorMap.TryGetValue(ErrorCode, out Errno))
  138. {
  139. Errno = (LinuxError)ErrorCode;
  140. }
  141. return Errno;
  142. }
  143. private long WriteWinSock2Error(ServiceCtx Context, WSAError ErrorCode)
  144. {
  145. return WriteBsdResult(Context, -1, ConvertError(ErrorCode));
  146. }
  147. private long WriteBsdResult(ServiceCtx Context, int Result, LinuxError ErrorCode = 0)
  148. {
  149. if (ErrorCode != LinuxError.SUCCESS)
  150. {
  151. Result = -1;
  152. }
  153. Context.ResponseData.Write(Result);
  154. Context.ResponseData.Write((int)ErrorCode);
  155. return 0;
  156. }
  157. private BsdSocket RetrieveSocket(int SocketFd)
  158. {
  159. if (SocketFd >= 0 && Sockets.Count > SocketFd)
  160. {
  161. return Sockets[SocketFd];
  162. }
  163. return null;
  164. }
  165. private LinuxError SetResultErrno(Socket Socket, int Result)
  166. {
  167. return Result == 0 && !Socket.Blocking ? LinuxError.EWOULDBLOCK : LinuxError.SUCCESS;
  168. }
  169. private AddressFamily ConvertFromBsd(int Domain)
  170. {
  171. if (Domain == 2)
  172. {
  173. return AddressFamily.InterNetwork;
  174. }
  175. // FIXME: AF_ROUTE ignored, is that really needed?
  176. return AddressFamily.Unknown;
  177. }
  178. private long SocketInternal(ServiceCtx Context, bool Exempt)
  179. {
  180. AddressFamily Domain = (AddressFamily)Context.RequestData.ReadInt32();
  181. SocketType Type = (SocketType)Context.RequestData.ReadInt32();
  182. ProtocolType Protocol = (ProtocolType)Context.RequestData.ReadInt32();
  183. if (Domain == AddressFamily.Unknown)
  184. {
  185. return WriteBsdResult(Context, -1, LinuxError.EPROTONOSUPPORT);
  186. }
  187. else if ((Type == SocketType.Seqpacket || Type == SocketType.Raw) && !IsPrivileged)
  188. {
  189. if (Domain != AddressFamily.InterNetwork || Type != SocketType.Raw || Protocol != ProtocolType.Icmp)
  190. {
  191. return WriteBsdResult(Context, -1, LinuxError.ENOENT);
  192. }
  193. }
  194. BsdSocket NewBsdSocket = new BsdSocket
  195. {
  196. Family = (int)Domain,
  197. Type = (int)Type,
  198. Protocol = (int)Protocol,
  199. Handle = new Socket(Domain, Type, Protocol)
  200. };
  201. Sockets.Add(NewBsdSocket);
  202. if (Exempt)
  203. {
  204. NewBsdSocket.Handle.Disconnect(true);
  205. }
  206. return WriteBsdResult(Context, Sockets.Count - 1);
  207. }
  208. private IPEndPoint ParseSockAddr(ServiceCtx Context, long BufferPosition, long BufferSize)
  209. {
  210. int Size = Context.Memory.ReadByte(BufferPosition);
  211. int Family = Context.Memory.ReadByte(BufferPosition + 1);
  212. int Port = EndianSwap.Swap16(Context.Memory.ReadUInt16(BufferPosition + 2));
  213. byte[] RawIp = Context.Memory.ReadBytes(BufferPosition + 4, 4);
  214. return new IPEndPoint(new IPAddress(RawIp), Port);
  215. }
  216. private void WriteSockAddr(ServiceCtx Context, long BufferPosition, IPEndPoint EndPoint)
  217. {
  218. Context.Memory.WriteByte(BufferPosition, 0);
  219. Context.Memory.WriteByte(BufferPosition + 1, (byte)EndPoint.AddressFamily);
  220. Context.Memory.WriteUInt16(BufferPosition + 2, EndianSwap.Swap16((ushort)EndPoint.Port));
  221. Context.Memory.WriteBytes(BufferPosition + 4, EndPoint.Address.GetAddressBytes());
  222. }
  223. private void WriteSockAddr(ServiceCtx Context, long BufferPosition, BsdSocket Socket, bool IsRemote)
  224. {
  225. IPEndPoint EndPoint = (IsRemote ? Socket.Handle.RemoteEndPoint : Socket.Handle.LocalEndPoint) as IPEndPoint;
  226. WriteSockAddr(Context, BufferPosition, EndPoint);
  227. }
  228. // Initialize(nn::socket::BsdBufferConfig config, u64 pid, u64 transferMemorySize, KObject<copy, transfer_memory>, pid) -> u32 bsd_errno
  229. public long RegisterClient(ServiceCtx Context)
  230. {
  231. /*
  232. typedef struct {
  233. u32 version; // Observed 1 on 2.0 LibAppletWeb, 2 on 3.0.
  234. u32 tcp_tx_buf_size; // Size of the TCP transfer (send) buffer (initial or fixed).
  235. u32 tcp_rx_buf_size; // Size of the TCP recieve buffer (initial or fixed).
  236. 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.
  237. 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.
  238. u32 udp_tx_buf_size; // Size of the UDP transfer (send) buffer (typically 0x2400 bytes).
  239. u32 udp_rx_buf_size; // Size of the UDP receive buffer (typically 0xA500 bytes).
  240. u32 sb_efficiency; // Number of buffers for each socket (standard values range from 1 to 8).
  241. } BsdBufferConfig;
  242. */
  243. // bsd_error
  244. Context.ResponseData.Write(0);
  245. Logger.PrintStub(LogClass.ServiceBsd, "Stubbed.");
  246. return 0;
  247. }
  248. // StartMonitoring(u64, pid)
  249. public long StartMonitoring(ServiceCtx Context)
  250. {
  251. ulong Unknown0 = Context.RequestData.ReadUInt64();
  252. Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed. Unknown0: {Unknown0}");
  253. return 0;
  254. }
  255. // Socket(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
  256. public long Socket(ServiceCtx Context)
  257. {
  258. return SocketInternal(Context, false);
  259. }
  260. // SocketExempt(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
  261. public long SocketExempt(ServiceCtx Context)
  262. {
  263. return SocketInternal(Context, true);
  264. }
  265. // Open(u32 flags, array<unknown, 0x21> path) -> (i32 ret, u32 bsd_errno)
  266. public long Open(ServiceCtx Context)
  267. {
  268. (long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x21();
  269. int Flags = Context.RequestData.ReadInt32();
  270. byte[] RawPath = Context.Memory.ReadBytes(BufferPosition, BufferSize);
  271. string Path = Encoding.ASCII.GetString(RawPath);
  272. WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  273. Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed. Path: {Path} - " +
  274. $"Flags: {Flags}");
  275. return 0;
  276. }
  277. // Select(u32 nfds, nn::socket::timeout timeout, buffer<nn::socket::fd_set, 0x21, 0> readfds_in, buffer<nn::socket::fd_set, 0x21, 0> writefds_in, buffer<nn::socket::fd_set, 0x21, 0> errorfds_in) -> (i32 ret, u32 bsd_errno, buffer<nn::socket::fd_set, 0x22, 0> readfds_out, buffer<nn::socket::fd_set, 0x22, 0> writefds_out, buffer<nn::socket::fd_set, 0x22, 0> errorfds_out)
  278. public long Select(ServiceCtx Context)
  279. {
  280. WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  281. Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed.");
  282. return 0;
  283. }
  284. // Poll(u32 nfds, u32 timeout, buffer<unknown, 0x21, 0> fds) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>)
  285. public long Poll(ServiceCtx Context)
  286. {
  287. int FdsCount = Context.RequestData.ReadInt32();
  288. int Timeout = Context.RequestData.ReadInt32();
  289. (long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x21();
  290. if (Timeout < -1 || FdsCount < 0 || (FdsCount * 8) > BufferSize)
  291. {
  292. return WriteBsdResult(Context, -1, LinuxError.EINVAL);
  293. }
  294. PollEvent[] Events = new PollEvent[FdsCount];
  295. for (int i = 0; i < FdsCount; i++)
  296. {
  297. int SocketFd = Context.Memory.ReadInt32(BufferPosition + i * 8);
  298. BsdSocket Socket = RetrieveSocket(SocketFd);
  299. if (Socket == null)
  300. {
  301. return WriteBsdResult(Context, -1, LinuxError.EBADF);
  302. }
  303. PollEvent.EventTypeMask InputEvents = (PollEvent.EventTypeMask)Context.Memory.ReadInt16(BufferPosition + i * 8 + 4);
  304. PollEvent.EventTypeMask OutputEvents = (PollEvent.EventTypeMask)Context.Memory.ReadInt16(BufferPosition + i * 8 + 6);
  305. Events[i] = new PollEvent(SocketFd, Socket, InputEvents, OutputEvents);
  306. }
  307. List<Socket> ReadEvents = new List<Socket>();
  308. List<Socket> WriteEvents = new List<Socket>();
  309. List<Socket> ErrorEvents = new List<Socket>();
  310. foreach (PollEvent Event in Events)
  311. {
  312. bool IsValidEvent = false;
  313. if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
  314. {
  315. ReadEvents.Add(Event.Socket.Handle);
  316. ErrorEvents.Add(Event.Socket.Handle);
  317. IsValidEvent = true;
  318. }
  319. if ((Event.InputEvents & PollEvent.EventTypeMask.UrgentInput) != 0)
  320. {
  321. ReadEvents.Add(Event.Socket.Handle);
  322. ErrorEvents.Add(Event.Socket.Handle);
  323. IsValidEvent = true;
  324. }
  325. if ((Event.InputEvents & PollEvent.EventTypeMask.Output) != 0)
  326. {
  327. WriteEvents.Add(Event.Socket.Handle);
  328. ErrorEvents.Add(Event.Socket.Handle);
  329. IsValidEvent = true;
  330. }
  331. if ((Event.InputEvents & PollEvent.EventTypeMask.Error) != 0)
  332. {
  333. ErrorEvents.Add(Event.Socket.Handle);
  334. IsValidEvent = true;
  335. }
  336. if (!IsValidEvent)
  337. {
  338. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Poll input event type: {Event.InputEvents}");
  339. return WriteBsdResult(Context, -1, LinuxError.EINVAL);
  340. }
  341. }
  342. try
  343. {
  344. System.Net.Sockets.Socket.Select(ReadEvents, WriteEvents, ErrorEvents, Timeout);
  345. }
  346. catch (SocketException Exception)
  347. {
  348. return WriteWinSock2Error(Context, (WSAError)Exception.ErrorCode);
  349. }
  350. for (int i = 0; i < FdsCount; i++)
  351. {
  352. PollEvent Event = Events[i];
  353. Context.Memory.WriteInt32(BufferPosition + i * 8, Event.SocketFd);
  354. Context.Memory.WriteInt16(BufferPosition + i * 8 + 4, (short)Event.InputEvents);
  355. PollEvent.EventTypeMask OutputEvents = 0;
  356. Socket Socket = Event.Socket.Handle;
  357. if (ErrorEvents.Contains(Socket))
  358. {
  359. OutputEvents |= PollEvent.EventTypeMask.Error;
  360. if (!Socket.Connected || !Socket.IsBound)
  361. {
  362. OutputEvents |= PollEvent.EventTypeMask.Disconnected;
  363. }
  364. }
  365. if (ReadEvents.Contains(Socket))
  366. {
  367. if ((Event.InputEvents & PollEvent.EventTypeMask.Input) != 0)
  368. {
  369. OutputEvents |= PollEvent.EventTypeMask.Input;
  370. }
  371. }
  372. if (WriteEvents.Contains(Socket))
  373. {
  374. OutputEvents |= PollEvent.EventTypeMask.Output;
  375. }
  376. Context.Memory.WriteInt16(BufferPosition + i * 8 + 6, (short)OutputEvents);
  377. }
  378. return WriteBsdResult(Context, ReadEvents.Count + WriteEvents.Count + ErrorEvents.Count, LinuxError.SUCCESS);
  379. }
  380. // Sysctl(buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
  381. public long Sysctl(ServiceCtx Context)
  382. {
  383. WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  384. Logger.PrintStub(LogClass.ServiceBsd, $"Stubbed.");
  385. return 0;
  386. }
  387. // Recv(u32 socket, u32 flags) -> (i32 ret, u32 bsd_errno, array<i8, 0x22> message)
  388. public long Recv(ServiceCtx Context)
  389. {
  390. int SocketFd = Context.RequestData.ReadInt32();
  391. SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
  392. (long ReceivePosition, long ReceiveLength) = Context.Request.GetBufferType0x22();
  393. LinuxError Errno = LinuxError.EBADF;
  394. BsdSocket Socket = RetrieveSocket(SocketFd);
  395. int Result = -1;
  396. if (Socket != null)
  397. {
  398. if (SocketFlags != SocketFlags.None && (SocketFlags & SocketFlags.OutOfBand) == 0
  399. && (SocketFlags & SocketFlags.Peek) == 0)
  400. {
  401. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {SocketFlags}");
  402. return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  403. }
  404. byte[] ReceivedBuffer = new byte[ReceiveLength];
  405. try
  406. {
  407. Result = Socket.Handle.Receive(ReceivedBuffer, SocketFlags);
  408. Errno = SetResultErrno(Socket.Handle, Result);
  409. Context.Memory.WriteBytes(ReceivePosition, ReceivedBuffer);
  410. }
  411. catch (SocketException Exception)
  412. {
  413. Errno = ConvertError((WSAError)Exception.ErrorCode);
  414. }
  415. }
  416. return WriteBsdResult(Context, Result, Errno);
  417. }
  418. // RecvFrom(u32 sock, u32 flags) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<i8, 0x22, 0> message, buffer<nn::socket::sockaddr_in, 0x22, 0x10>)
  419. public long RecvFrom(ServiceCtx Context)
  420. {
  421. int SocketFd = Context.RequestData.ReadInt32();
  422. SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
  423. (long ReceivePosition, long ReceiveLength) = Context.Request.GetBufferType0x22();
  424. (long SockAddrInPosition, long SockAddrInSize) = Context.Request.GetBufferType0x21();
  425. (long SockAddrOutPosition, long SockAddrOutSize) = Context.Request.GetBufferType0x22(1);
  426. LinuxError Errno = LinuxError.EBADF;
  427. BsdSocket Socket = RetrieveSocket(SocketFd);
  428. int Result = -1;
  429. if (Socket != null)
  430. {
  431. if (SocketFlags != SocketFlags.None && (SocketFlags & SocketFlags.OutOfBand) == 0
  432. && (SocketFlags & SocketFlags.Peek) == 0)
  433. {
  434. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Recv flags: {SocketFlags}");
  435. return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  436. }
  437. byte[] ReceivedBuffer = new byte[ReceiveLength];
  438. EndPoint EndPoint = ParseSockAddr(Context, SockAddrInPosition, SockAddrInSize);
  439. try
  440. {
  441. Result = Socket.Handle.ReceiveFrom(ReceivedBuffer, ReceivedBuffer.Length, SocketFlags, ref EndPoint);
  442. Errno = SetResultErrno(Socket.Handle, Result);
  443. Context.Memory.WriteBytes(ReceivePosition, ReceivedBuffer);
  444. WriteSockAddr(Context, SockAddrOutPosition, (IPEndPoint)EndPoint);
  445. }
  446. catch (SocketException Exception)
  447. {
  448. Errno = ConvertError((WSAError)Exception.ErrorCode);
  449. }
  450. }
  451. return WriteBsdResult(Context, Result, Errno);
  452. }
  453. // Send(u32 socket, u32 flags, buffer<i8, 0x21, 0>) -> (i32 ret, u32 bsd_errno)
  454. public long Send(ServiceCtx Context)
  455. {
  456. int SocketFd = Context.RequestData.ReadInt32();
  457. SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
  458. (long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
  459. LinuxError Errno = LinuxError.EBADF;
  460. BsdSocket Socket = RetrieveSocket(SocketFd);
  461. int Result = -1;
  462. if (Socket != null)
  463. {
  464. if (SocketFlags != SocketFlags.None && SocketFlags != SocketFlags.OutOfBand
  465. && SocketFlags != SocketFlags.Peek && SocketFlags != SocketFlags.DontRoute)
  466. {
  467. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {SocketFlags}");
  468. return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  469. }
  470. byte[] SendBuffer = Context.Memory.ReadBytes(SendPosition, SendSize);
  471. try
  472. {
  473. Result = Socket.Handle.Send(SendBuffer, SocketFlags);
  474. Errno = SetResultErrno(Socket.Handle, Result);
  475. }
  476. catch (SocketException Exception)
  477. {
  478. Errno = ConvertError((WSAError)Exception.ErrorCode);
  479. }
  480. }
  481. return WriteBsdResult(Context, Result, Errno);
  482. }
  483. // SendTo(u32 socket, u32 flags, buffer<i8, 0x21, 0>, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
  484. public long SendTo(ServiceCtx Context)
  485. {
  486. int SocketFd = Context.RequestData.ReadInt32();
  487. SocketFlags SocketFlags = (SocketFlags)Context.RequestData.ReadInt32();
  488. (long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
  489. (long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x21(1);
  490. LinuxError Errno = LinuxError.EBADF;
  491. BsdSocket Socket = RetrieveSocket(SocketFd);
  492. int Result = -1;
  493. if (Socket != null)
  494. {
  495. if (SocketFlags != SocketFlags.None && SocketFlags != SocketFlags.OutOfBand
  496. && SocketFlags != SocketFlags.Peek && SocketFlags != SocketFlags.DontRoute)
  497. {
  498. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Send flags: {SocketFlags}");
  499. return WriteBsdResult(Context, -1, LinuxError.EOPNOTSUPP);
  500. }
  501. byte[] SendBuffer = Context.Memory.ReadBytes(SendPosition, SendSize);
  502. EndPoint EndPoint = ParseSockAddr(Context, BufferPosition, BufferSize);
  503. try
  504. {
  505. Result = Socket.Handle.SendTo(SendBuffer, SendBuffer.Length, SocketFlags, EndPoint);
  506. Errno = SetResultErrno(Socket.Handle, Result);
  507. }
  508. catch (SocketException Exception)
  509. {
  510. Errno = ConvertError((WSAError)Exception.ErrorCode);
  511. }
  512. }
  513. return WriteBsdResult(Context, Result, Errno);
  514. }
  515. // Accept(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
  516. public long Accept(ServiceCtx Context)
  517. {
  518. int SocketFd = Context.RequestData.ReadInt32();
  519. (long BufferPos, long BufferSize) = Context.Request.GetBufferType0x22();
  520. LinuxError Errno = LinuxError.EBADF;
  521. BsdSocket Socket = RetrieveSocket(SocketFd);
  522. if (Socket != null)
  523. {
  524. Errno = LinuxError.SUCCESS;
  525. Socket NewSocket = null;
  526. try
  527. {
  528. NewSocket = Socket.Handle.Accept();
  529. }
  530. catch (SocketException Exception)
  531. {
  532. Errno = ConvertError((WSAError)Exception.ErrorCode);
  533. }
  534. if (NewSocket == null && Errno == LinuxError.SUCCESS)
  535. {
  536. Errno = LinuxError.EWOULDBLOCK;
  537. }
  538. else if (Errno == LinuxError.SUCCESS)
  539. {
  540. BsdSocket NewBsdSocket = new BsdSocket
  541. {
  542. Family = (int)NewSocket.AddressFamily,
  543. Type = (int)NewSocket.SocketType,
  544. Protocol = (int)NewSocket.ProtocolType,
  545. Handle = NewSocket,
  546. };
  547. Sockets.Add(NewBsdSocket);
  548. WriteSockAddr(Context, BufferPos, NewBsdSocket, true);
  549. WriteBsdResult(Context, Sockets.Count - 1, Errno);
  550. Context.ResponseData.Write(0x10);
  551. return 0;
  552. }
  553. }
  554. return WriteBsdResult(Context, -1, Errno);
  555. }
  556. // Bind(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10> addr) -> (i32 ret, u32 bsd_errno)
  557. public long Bind(ServiceCtx Context)
  558. {
  559. int SocketFd = Context.RequestData.ReadInt32();
  560. (long BufferPos, long BufferSize) = Context.Request.GetBufferType0x21();
  561. LinuxError Errno = LinuxError.EBADF;
  562. BsdSocket Socket = RetrieveSocket(SocketFd);
  563. if (Socket != null)
  564. {
  565. Errno = LinuxError.SUCCESS;
  566. try
  567. {
  568. IPEndPoint EndPoint = ParseSockAddr(Context, BufferPos, BufferSize);
  569. Socket.Handle.Bind(EndPoint);
  570. }
  571. catch (SocketException Exception)
  572. {
  573. Errno = ConvertError((WSAError)Exception.ErrorCode);
  574. }
  575. }
  576. return WriteBsdResult(Context, 0, Errno);
  577. }
  578. // Connect(u32 socket, buffer<nn::socket::sockaddr_in, 0x21, 0x10>) -> (i32 ret, u32 bsd_errno)
  579. public long Connect(ServiceCtx Context)
  580. {
  581. int SocketFd = Context.RequestData.ReadInt32();
  582. (long BufferPos, long BufferSize) = Context.Request.GetBufferType0x21();
  583. LinuxError Errno = LinuxError.EBADF;
  584. BsdSocket Socket = RetrieveSocket(SocketFd);
  585. if (Socket != null)
  586. {
  587. Errno = LinuxError.SUCCESS;
  588. try
  589. {
  590. IPEndPoint EndPoint = ParseSockAddr(Context, BufferPos, BufferSize);
  591. Socket.Handle.Connect(EndPoint);
  592. }
  593. catch (SocketException Exception)
  594. {
  595. Errno = ConvertError((WSAError)Exception.ErrorCode);
  596. }
  597. }
  598. return WriteBsdResult(Context, 0, Errno);
  599. }
  600. // GetPeerName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
  601. public long GetPeerName(ServiceCtx Context)
  602. {
  603. int SocketFd = Context.RequestData.ReadInt32();
  604. (long BufferPos, long BufferSize) = Context.Request.GetBufferType0x22();
  605. LinuxError Errno = LinuxError.EBADF;
  606. BsdSocket Socket = RetrieveSocket(SocketFd);
  607. if (Socket != null)
  608. {
  609. Errno = LinuxError.SUCCESS;
  610. WriteSockAddr(Context, BufferPos, Socket, true);
  611. WriteBsdResult(Context, 0, Errno);
  612. Context.ResponseData.Write(0x10);
  613. }
  614. return WriteBsdResult(Context, 0, Errno);
  615. }
  616. // GetSockName(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<nn::socket::sockaddr_in, 0x22, 0x10> addr)
  617. public long GetSockName(ServiceCtx Context)
  618. {
  619. int SocketFd = Context.RequestData.ReadInt32();
  620. (long BufferPos, long BufferSize) = Context.Request.GetBufferType0x22();
  621. LinuxError Errno = LinuxError.EBADF;
  622. BsdSocket Socket = RetrieveSocket(SocketFd);
  623. if (Socket != null)
  624. {
  625. Errno = LinuxError.SUCCESS;
  626. WriteSockAddr(Context, BufferPos, Socket, false);
  627. WriteBsdResult(Context, 0, Errno);
  628. Context.ResponseData.Write(0x10);
  629. }
  630. return WriteBsdResult(Context, 0, Errno);
  631. }
  632. // GetSockOpt(u32 socket, u32 level, u32 option_name) -> (i32 ret, u32 bsd_errno, u32, buffer<unknown, 0x22, 0>)
  633. public long GetSockOpt(ServiceCtx Context)
  634. {
  635. int SocketFd = Context.RequestData.ReadInt32();
  636. int Level = Context.RequestData.ReadInt32();
  637. int OptionName = Context.RequestData.ReadInt32();
  638. (long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x22();
  639. LinuxError Errno = LinuxError.EBADF;
  640. BsdSocket Socket = RetrieveSocket(SocketFd);
  641. if (Socket != null)
  642. {
  643. Errno = LinuxError.ENOPROTOOPT;
  644. if (Level == 0xFFFF)
  645. {
  646. Errno = HandleGetSocketOption(Context, Socket, (SocketOptionName)OptionName, BufferPosition, BufferSize);
  647. }
  648. else
  649. {
  650. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported GetSockOpt Level: {(SocketOptionLevel)Level}");
  651. }
  652. }
  653. return WriteBsdResult(Context, 0, Errno);
  654. }
  655. // Listen(u32 socket, u32 backlog) -> (i32 ret, u32 bsd_errno)
  656. public long Listen(ServiceCtx Context)
  657. {
  658. int SocketFd = Context.RequestData.ReadInt32();
  659. int Backlog = Context.RequestData.ReadInt32();
  660. LinuxError Errno = LinuxError.EBADF;
  661. BsdSocket Socket = RetrieveSocket(SocketFd);
  662. if (Socket != null)
  663. {
  664. Errno = LinuxError.SUCCESS;
  665. try
  666. {
  667. Socket.Handle.Listen(Backlog);
  668. }
  669. catch (SocketException Exception)
  670. {
  671. Errno = ConvertError((WSAError)Exception.ErrorCode);
  672. }
  673. }
  674. return WriteBsdResult(Context, 0, Errno);
  675. }
  676. // Ioctl(u32 fd, u32 request, u32 bufcount, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>, buffer<unknown, 0x21, 0>) -> (i32 ret, u32 bsd_errno, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>, buffer<unknown, 0x22, 0>)
  677. public long Ioctl(ServiceCtx Context)
  678. {
  679. int SocketFd = Context.RequestData.ReadInt32();
  680. BsdIoctl Cmd = (BsdIoctl)Context.RequestData.ReadInt32();
  681. int BufferCount = Context.RequestData.ReadInt32();
  682. LinuxError Errno = LinuxError.EBADF;
  683. BsdSocket Socket = RetrieveSocket(SocketFd);
  684. if (Socket != null)
  685. {
  686. switch (Cmd)
  687. {
  688. case BsdIoctl.AtMark:
  689. Errno = LinuxError.SUCCESS;
  690. (long BufferPosition, long BufferSize) = Context.Request.GetBufferType0x22();
  691. // FIXME: OOB not implemented.
  692. Context.Memory.WriteInt32(BufferPosition, 0);
  693. break;
  694. default:
  695. Errno = LinuxError.EOPNOTSUPP;
  696. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported Ioctl Cmd: {Cmd}");
  697. break;
  698. }
  699. }
  700. return WriteBsdResult(Context, 0, Errno);
  701. }
  702. // Fcntl(u32 socket, u32 cmd, u32 arg) -> (i32 ret, u32 bsd_errno)
  703. public long Fcntl(ServiceCtx Context)
  704. {
  705. int SocketFd = Context.RequestData.ReadInt32();
  706. int Cmd = Context.RequestData.ReadInt32();
  707. int Arg = Context.RequestData.ReadInt32();
  708. int Result = 0;
  709. LinuxError Errno = LinuxError.EBADF;
  710. BsdSocket Socket = RetrieveSocket(SocketFd);
  711. if (Socket != null)
  712. {
  713. Errno = LinuxError.SUCCESS;
  714. if (Cmd == 0x3)
  715. {
  716. Result = !Socket.Handle.Blocking ? 0x800 : 0;
  717. }
  718. else if (Cmd == 0x4 && Arg == 0x800)
  719. {
  720. Socket.Handle.Blocking = false;
  721. Result = 0;
  722. }
  723. else
  724. {
  725. Errno = LinuxError.EOPNOTSUPP;
  726. }
  727. }
  728. return WriteBsdResult(Context, Result, Errno);
  729. }
  730. private LinuxError HandleGetSocketOption(ServiceCtx Context, BsdSocket Socket, SocketOptionName OptionName, long OptionValuePosition, long OptionValueSize)
  731. {
  732. try
  733. {
  734. byte[] OptionValue = new byte[OptionValueSize];
  735. switch (OptionName)
  736. {
  737. case SocketOptionName.Broadcast:
  738. case SocketOptionName.DontLinger:
  739. case SocketOptionName.Debug:
  740. case SocketOptionName.Error:
  741. case SocketOptionName.KeepAlive:
  742. case SocketOptionName.OutOfBandInline:
  743. case SocketOptionName.ReceiveBuffer:
  744. case SocketOptionName.ReceiveTimeout:
  745. case SocketOptionName.SendBuffer:
  746. case SocketOptionName.SendTimeout:
  747. case SocketOptionName.Type:
  748. case SocketOptionName.Linger:
  749. Socket.Handle.GetSocketOption(SocketOptionLevel.Socket, OptionName, OptionValue);
  750. Context.Memory.WriteBytes(OptionValuePosition, OptionValue);
  751. return LinuxError.SUCCESS;
  752. case (SocketOptionName)0x200:
  753. Socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, OptionValue);
  754. Context.Memory.WriteBytes(OptionValuePosition, OptionValue);
  755. return LinuxError.SUCCESS;
  756. default:
  757. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {OptionName}");
  758. return LinuxError.EOPNOTSUPP;
  759. }
  760. }
  761. catch (SocketException Exception)
  762. {
  763. return ConvertError((WSAError)Exception.ErrorCode);
  764. }
  765. }
  766. private LinuxError HandleSetSocketOption(ServiceCtx Context, BsdSocket Socket, SocketOptionName OptionName, long OptionValuePosition, long OptionValueSize)
  767. {
  768. try
  769. {
  770. switch (OptionName)
  771. {
  772. case SocketOptionName.Broadcast:
  773. case SocketOptionName.DontLinger:
  774. case SocketOptionName.Debug:
  775. case SocketOptionName.Error:
  776. case SocketOptionName.KeepAlive:
  777. case SocketOptionName.OutOfBandInline:
  778. case SocketOptionName.ReceiveBuffer:
  779. case SocketOptionName.ReceiveTimeout:
  780. case SocketOptionName.SendBuffer:
  781. case SocketOptionName.SendTimeout:
  782. case SocketOptionName.Type:
  783. case SocketOptionName.ReuseAddress:
  784. Socket.Handle.SetSocketOption(SocketOptionLevel.Socket, OptionName, Context.Memory.ReadInt32(OptionValuePosition));
  785. return LinuxError.SUCCESS;
  786. case (SocketOptionName)0x200:
  787. Socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, Context.Memory.ReadInt32(OptionValuePosition));
  788. return LinuxError.SUCCESS;
  789. case SocketOptionName.Linger:
  790. Socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
  791. new LingerOption(Context.Memory.ReadInt32(OptionValuePosition) != 0, Context.Memory.ReadInt32(OptionValuePosition + 4)));
  792. return LinuxError.SUCCESS;
  793. default:
  794. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt OptionName: {OptionName}");
  795. return LinuxError.EOPNOTSUPP;
  796. }
  797. }
  798. catch (SocketException Exception)
  799. {
  800. return ConvertError((WSAError)Exception.ErrorCode);
  801. }
  802. }
  803. // SetSockOpt(u32 socket, u32 level, u32 option_name, buffer<unknown, 0x21, 0> option_value) -> (i32 ret, u32 bsd_errno)
  804. public long SetSockOpt(ServiceCtx Context)
  805. {
  806. int SocketFd = Context.RequestData.ReadInt32();
  807. int Level = Context.RequestData.ReadInt32();
  808. int OptionName = Context.RequestData.ReadInt32();
  809. (long BufferPos, long BufferSize) = Context.Request.GetBufferType0x21();
  810. LinuxError Errno = LinuxError.EBADF;
  811. BsdSocket Socket = RetrieveSocket(SocketFd);
  812. if (Socket != null)
  813. {
  814. Errno = LinuxError.ENOPROTOOPT;
  815. if (Level == 0xFFFF)
  816. {
  817. Errno = HandleSetSocketOption(Context, Socket, (SocketOptionName)OptionName, BufferPos, BufferSize);
  818. }
  819. else
  820. {
  821. Logger.PrintWarning(LogClass.ServiceBsd, $"Unsupported SetSockOpt Level: {(SocketOptionLevel)Level}");
  822. }
  823. }
  824. return WriteBsdResult(Context, 0, Errno);
  825. }
  826. // Shutdown(u32 socket, u32 how) -> (i32 ret, u32 bsd_errno)
  827. public long Shutdown(ServiceCtx Context)
  828. {
  829. int SocketFd = Context.RequestData.ReadInt32();
  830. int How = Context.RequestData.ReadInt32();
  831. LinuxError Errno = LinuxError.EBADF;
  832. BsdSocket Socket = RetrieveSocket(SocketFd);
  833. if (Socket != null)
  834. {
  835. Errno = LinuxError.EINVAL;
  836. if (How >= 0 && How <= 2)
  837. {
  838. Errno = LinuxError.SUCCESS;
  839. try
  840. {
  841. Socket.Handle.Shutdown((SocketShutdown)How);
  842. }
  843. catch (SocketException Exception)
  844. {
  845. Errno = ConvertError((WSAError)Exception.ErrorCode);
  846. }
  847. }
  848. }
  849. return WriteBsdResult(Context, 0, Errno);
  850. }
  851. // ShutdownAllSockets(u32 how) -> (i32 ret, u32 bsd_errno)
  852. public long ShutdownAllSockets(ServiceCtx Context)
  853. {
  854. int How = Context.RequestData.ReadInt32();
  855. LinuxError Errno = LinuxError.EINVAL;
  856. if (How >= 0 && How <= 2)
  857. {
  858. Errno = LinuxError.SUCCESS;
  859. foreach (BsdSocket Socket in Sockets)
  860. {
  861. if (Socket != null)
  862. {
  863. try
  864. {
  865. Socket.Handle.Shutdown((SocketShutdown)How);
  866. }
  867. catch (SocketException Exception)
  868. {
  869. Errno = ConvertError((WSAError)Exception.ErrorCode);
  870. break;
  871. }
  872. }
  873. }
  874. }
  875. return WriteBsdResult(Context, 0, Errno);
  876. }
  877. // Write(u32 socket, buffer<i8, 0x21, 0> message) -> (i32 ret, u32 bsd_errno)
  878. public long Write(ServiceCtx Context)
  879. {
  880. int SocketFd = Context.RequestData.ReadInt32();
  881. (long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
  882. LinuxError Errno = LinuxError.EBADF;
  883. BsdSocket Socket = RetrieveSocket(SocketFd);
  884. int Result = -1;
  885. if (Socket != null)
  886. {
  887. byte[] SendBuffer = Context.Memory.ReadBytes(SendPosition, SendSize);
  888. try
  889. {
  890. Result = Socket.Handle.Send(SendBuffer);
  891. Errno = SetResultErrno(Socket.Handle, Result);
  892. }
  893. catch (SocketException Exception)
  894. {
  895. Errno = ConvertError((WSAError)Exception.ErrorCode);
  896. }
  897. }
  898. return WriteBsdResult(Context, Result, Errno);
  899. }
  900. // Read(u32 socket) -> (i32 ret, u32 bsd_errno, buffer<i8, 0x22, 0> message)
  901. public long Read(ServiceCtx Context)
  902. {
  903. int SocketFd = Context.RequestData.ReadInt32();
  904. (long ReceivePosition, long ReceiveLength) = Context.Request.GetBufferType0x22();
  905. LinuxError Errno = LinuxError.EBADF;
  906. BsdSocket Socket = RetrieveSocket(SocketFd);
  907. int Result = -1;
  908. if (Socket != null)
  909. {
  910. byte[] ReceivedBuffer = new byte[ReceiveLength];
  911. try
  912. {
  913. Result = Socket.Handle.Receive(ReceivedBuffer);
  914. Errno = SetResultErrno(Socket.Handle, Result);
  915. }
  916. catch (SocketException Exception)
  917. {
  918. Errno = ConvertError((WSAError)Exception.ErrorCode);
  919. }
  920. }
  921. return WriteBsdResult(Context, Result, Errno);
  922. }
  923. // Close(u32 socket) -> (i32 ret, u32 bsd_errno)
  924. public long Close(ServiceCtx Context)
  925. {
  926. int SocketFd = Context.RequestData.ReadInt32();
  927. LinuxError Errno = LinuxError.EBADF;
  928. BsdSocket Socket = RetrieveSocket(SocketFd);
  929. if (Socket != null)
  930. {
  931. Socket.Handle.Close();
  932. Sockets[SocketFd] = null;
  933. Errno = LinuxError.SUCCESS;
  934. }
  935. return WriteBsdResult(Context, 0, Errno);
  936. }
  937. // DuplicateSocket(u32 socket, u64 reserved) -> (i32 ret, u32 bsd_errno)
  938. public long DuplicateSocket(ServiceCtx Context)
  939. {
  940. int SocketFd = Context.RequestData.ReadInt32();
  941. ulong Reserved = Context.RequestData.ReadUInt64();
  942. LinuxError Errno = LinuxError.ENOENT;
  943. int NewSockFd = -1;
  944. if (IsPrivileged)
  945. {
  946. Errno = LinuxError.EBADF;
  947. BsdSocket OldSocket = RetrieveSocket(SocketFd);
  948. if (OldSocket != null)
  949. {
  950. Sockets.Add(OldSocket);
  951. NewSockFd = Sockets.Count - 1;
  952. }
  953. }
  954. return WriteBsdResult(Context, NewSockFd, Errno);
  955. }
  956. }
  957. }