IClient.cs 45 KB

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