IClient.cs 44 KB

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