IClient.cs 45 KB

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