IClient.cs 45 KB

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