SocketImpl.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #if (IL2CPP_TARGET_POSIX || IL2CPP_SUPPORT_SOCKETS_POSIX_API) && !RUNTIME_TINY
  3. #include <string>
  4. #include <vector>
  5. #include <stdint.h>
  6. #include <sys/socket.h>
  7. #include "os/Socket.h"
  8. #include "os/ErrorCodes.h"
  9. #include "os/WaitStatus.h"
  10. #include "utils/Expected.h"
  11. #include "utils/NonCopyable.h"
  12. #if IL2CPP_USE_NETWORK_ACCESS_HANDLER
  13. #include "os/NetworkAccessHandler.h"
  14. #else
  15. #include "NetworkAccessHandlerStub.h"
  16. #endif
  17. struct sockaddr;
  18. namespace il2cpp
  19. {
  20. namespace os
  21. {
  22. class SocketImpl : public il2cpp::utils::NonCopyable
  23. {
  24. public:
  25. typedef int SocketDescriptor;
  26. SocketImpl(ThreadStatusCallback thread_status_callback);
  27. ~SocketImpl();
  28. inline SocketDescriptor GetDescriptor()
  29. {
  30. return _fd;
  31. }
  32. ErrorCode GetLastError() const;
  33. WaitStatus Create(SocketDescriptor fd, int32_t family, int32_t type, int32_t protocol);
  34. WaitStatus Create(AddressFamily family, SocketType type, ProtocolType protocol);
  35. WaitStatus Close();
  36. bool IsClosed()
  37. {
  38. return (_fd == -1);
  39. }
  40. WaitStatus SetBlocking(bool blocking);
  41. WaitStatus Listen(int32_t blacklog);
  42. WaitStatus Bind(const char *path);
  43. WaitStatus Bind(const char *address, uint16_t port);
  44. WaitStatus Bind(uint32_t address, uint16_t port);
  45. utils::Expected<WaitStatus> Bind(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port);
  46. WaitStatus Connect(const char *path);
  47. WaitStatus Connect(uint32_t address, uint16_t port);
  48. utils::Expected<WaitStatus> Connect(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port);
  49. WaitStatus Disconnect(bool reuse);
  50. WaitStatus Shutdown(int32_t how);
  51. WaitStatus GetLocalEndPointInfo(EndPointInfo &info);
  52. WaitStatus GetRemoteEndPointInfo(EndPointInfo &info);
  53. WaitStatus Receive(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  54. WaitStatus ReceiveFromInternal(const uint8_t *data, size_t count, int32_t flags, int32_t *len, struct sockaddr *from, int32_t *fromlen);
  55. WaitStatus Send(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  56. WaitStatus SendArray(WSABuf *wsabufs, int32_t count, int32_t *sent, SocketFlags c_flags);
  57. WaitStatus ReceiveArray(WSABuf *wsabufs, int32_t count, int32_t *len, SocketFlags c_flags);
  58. WaitStatus SendTo(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  59. WaitStatus SendTo(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  60. utils::Expected<WaitStatus> SendTo(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  61. WaitStatus RecvFrom(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
  62. WaitStatus RecvFrom(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
  63. utils::Expected<WaitStatus> RecvFrom(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep);
  64. WaitStatus Available(int32_t *amount);
  65. WaitStatus Accept(os::Socket **socket);
  66. WaitStatus Ioctl(int32_t command, const uint8_t *in_data, int32_t in_len, uint8_t *out_data, int32_t out_len, int32_t *written);
  67. WaitStatus GetSocketOption(SocketOptionLevel level, SocketOptionName name, uint8_t *buffer, int32_t *length);
  68. WaitStatus GetSocketOptionFull(SocketOptionLevel level, SocketOptionName name, int32_t *first, int32_t *second);
  69. WaitStatus SetSocketOption(SocketOptionLevel level, SocketOptionName name, int32_t value);
  70. WaitStatus SetSocketOptionLinger(SocketOptionLevel level, SocketOptionName name, bool enabled, int32_t seconds);
  71. WaitStatus SetSocketOptionArray(SocketOptionLevel level, SocketOptionName name, const uint8_t *buffer, int32_t length);
  72. WaitStatus SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, uint32_t group_address, uint32_t local_address);
  73. #if IL2CPP_SUPPORT_IPV6
  74. WaitStatus SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, IPv6Address ipv6, uint64_t interfaceOffset);
  75. #endif
  76. #if IL2CPP_SUPPORT_IPV6_SUPPORT_QUERY
  77. static bool IsIPv6Supported();
  78. #endif
  79. WaitStatus SendFile(const char *filename, TransmitFileBuffers *buffers, TransmitFileOptions options);
  80. static WaitStatus Poll(std::vector<PollRequest> &requests, int32_t count, int32_t timeout, int32_t *result, int32_t *error);
  81. static WaitStatus Poll(std::vector<PollRequest> &requests, int32_t timeout, int32_t *result, int32_t *error);
  82. static WaitStatus Poll(PollRequest& request, int32_t timeout, int32_t *result, int32_t *error);
  83. static WaitStatus GetHostName(std::string &name);
  84. static WaitStatus GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addresses);
  85. static WaitStatus GetHostByName(const std::string &host, std::string &name, int32_t &family, std::vector<std::string> &aliases, std::vector<void*> &addr_list, int32_t &addr_size);
  86. static WaitStatus GetHostByAddr(const std::string &address, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list);
  87. static void Startup();
  88. static void Cleanup();
  89. static bool is_private(const struct sockaddr *sa, socklen_t sa_size);
  90. static bool is_private(const char* address);
  91. private:
  92. bool _is_valid;
  93. SocketDescriptor _fd;
  94. int32_t _domain;
  95. int32_t _type;
  96. int32_t _protocol;
  97. ErrorCode _saved_error;
  98. int32_t _still_readable;
  99. ThreadStatusCallback _thread_status_callback;
  100. NetworkAccessHandler _networkAccess;
  101. void StoreLastError();
  102. void StoreLastError(int32_t error_no);
  103. WaitStatus ConnectInternal(struct sockaddr *sa, int32_t sa_size);
  104. WaitStatus SendToInternal(struct sockaddr *sa, int32_t sa_size, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len);
  105. WaitStatus SetSocketOptionInternal(int32_t level, int32_t name, const void *value, int32_t len);
  106. };
  107. }
  108. }
  109. #endif