socket.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #pragma once
  2. #include "init.h"
  3. #include <util/system/yassert.h>
  4. #include <util/system/defaults.h>
  5. #include <util/system/error.h>
  6. #include <util/stream/output.h>
  7. #include <util/stream/input.h>
  8. #include <util/generic/ptr.h>
  9. #include <util/generic/yexception.h>
  10. #include <util/generic/noncopyable.h>
  11. #include <util/datetime/base.h>
  12. #include <cerrno>
  13. #ifndef INET_ADDRSTRLEN
  14. #define INET_ADDRSTRLEN 16
  15. #endif
  16. #if defined(_unix_)
  17. #define get_host_error() h_errno
  18. #elif defined(_win_)
  19. #pragma comment(lib, "Ws2_32.lib")
  20. #if _WIN32_WINNT < 0x0600
  21. struct pollfd {
  22. SOCKET fd;
  23. short events;
  24. short revents;
  25. };
  26. #define POLLIN (1 << 0)
  27. #define POLLRDNORM (1 << 1)
  28. #define POLLRDBAND (1 << 2)
  29. #define POLLPRI (1 << 3)
  30. #define POLLOUT (1 << 4)
  31. #define POLLWRNORM (1 << 5)
  32. #define POLLWRBAND (1 << 6)
  33. #define POLLERR (1 << 7)
  34. #define POLLHUP (1 << 8)
  35. #define POLLNVAL (1 << 9)
  36. const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
  37. int poll(struct pollfd fds[], nfds_t nfds, int timeout) noexcept;
  38. #else
  39. #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
  40. #endif
  41. int inet_aton(const char* cp, struct in_addr* inp);
  42. #define get_host_error() WSAGetLastError()
  43. #define SHUT_RD SD_RECEIVE
  44. #define SHUT_WR SD_SEND
  45. #define SHUT_RDWR SD_BOTH
  46. #define INFTIM (-1)
  47. #endif
  48. template <class T>
  49. static inline int SetSockOpt(SOCKET s, int level, int optname, T opt) noexcept {
  50. return setsockopt(s, level, optname, (const char*)&opt, sizeof(opt));
  51. }
  52. template <class T>
  53. static inline int GetSockOpt(SOCKET s, int level, int optname, T& opt) noexcept {
  54. socklen_t len = sizeof(opt);
  55. return getsockopt(s, level, optname, (char*)&opt, &len);
  56. }
  57. template <class T>
  58. static inline void CheckedSetSockOpt(SOCKET s, int level, int optname, T opt, const char* err) {
  59. if (SetSockOpt<T>(s, level, optname, opt)) {
  60. ythrow TSystemError() << "setsockopt() failed for " << err;
  61. }
  62. }
  63. template <class T>
  64. static inline void CheckedGetSockOpt(SOCKET s, int level, int optname, T& opt, const char* err) {
  65. if (GetSockOpt<T>(s, level, optname, opt)) {
  66. ythrow TSystemError() << "getsockopt() failed for " << err;
  67. }
  68. }
  69. static inline void FixIPv6ListenSocket(SOCKET s) {
  70. #if defined(IPV6_V6ONLY)
  71. SetSockOpt(s, IPPROTO_IPV6, IPV6_V6ONLY, 1);
  72. #else
  73. (void)s;
  74. #endif
  75. }
  76. namespace NAddr {
  77. class IRemoteAddr;
  78. }
  79. void SetSocketTimeout(SOCKET s, long timeout);
  80. void SetSocketTimeout(SOCKET s, long sec, long msec);
  81. void SetNoDelay(SOCKET s, bool value);
  82. void SetKeepAlive(SOCKET s);
  83. void SetLinger(SOCKET s, bool on, unsigned len);
  84. void SetZeroLinger(SOCKET s);
  85. void SetKeepAlive(SOCKET s, bool value);
  86. void SetCloseOnExec(SOCKET s, bool value);
  87. void SetOutputBuffer(SOCKET s, unsigned value);
  88. void SetInputBuffer(SOCKET s, unsigned value);
  89. void SetReusePort(SOCKET s, bool value);
  90. void ShutDown(SOCKET s, int mode);
  91. bool GetRemoteAddr(SOCKET s, char* str, socklen_t size);
  92. size_t GetMaximumSegmentSize(SOCKET s);
  93. size_t GetMaximumTransferUnit(SOCKET s);
  94. void SetDeferAccept(SOCKET s);
  95. void SetSocketToS(SOCKET s, int tos);
  96. void SetSocketToS(SOCKET s, const NAddr::IRemoteAddr* addr, int tos);
  97. int GetSocketToS(SOCKET s);
  98. int GetSocketToS(SOCKET s, const NAddr::IRemoteAddr* addr);
  99. void SetSocketPriority(SOCKET s, int priority);
  100. void SetTcpFastOpen(SOCKET s, int qlen);
  101. /**
  102. * Deprecated, consider using HasSocketDataToRead instead.
  103. **/
  104. bool IsNotSocketClosedByOtherSide(SOCKET s);
  105. enum class ESocketReadStatus {
  106. HasData,
  107. NoData,
  108. SocketClosed
  109. };
  110. /**
  111. * Useful for keep-alive connections.
  112. **/
  113. ESocketReadStatus HasSocketDataToRead(SOCKET s);
  114. /**
  115. * Determines whether connection on socket is local (same machine) or not.
  116. **/
  117. bool HasLocalAddress(SOCKET socket);
  118. /**
  119. * Runtime check if current kernel supports SO_REUSEPORT option.
  120. **/
  121. extern "C" bool IsReusePortAvailable();
  122. bool IsNonBlock(SOCKET fd);
  123. void SetNonBlock(SOCKET fd, bool nonBlock = true);
  124. struct addrinfo;
  125. class TNetworkResolutionError: public yexception {
  126. public:
  127. // @param error error code (EAI_XXX) returned by getaddrinfo or getnameinfo (not errno)
  128. TNetworkResolutionError(int error);
  129. };
  130. struct TUnixSocketPath {
  131. TString Path;
  132. // Constructor for create unix domain socket path from string with path in filesystem
  133. // TUnixSocketPath("/tmp/unixsocket") -> "/tmp/unixsocket"
  134. explicit TUnixSocketPath(const TString& path)
  135. : Path(path)
  136. {
  137. }
  138. };
  139. class TNetworkAddress {
  140. friend class TSocket;
  141. public:
  142. class TIterator {
  143. public:
  144. inline TIterator(struct addrinfo* begin)
  145. : C_(begin)
  146. {
  147. }
  148. inline void Next() noexcept {
  149. C_ = C_->ai_next;
  150. }
  151. inline TIterator operator++(int) noexcept {
  152. TIterator old(*this);
  153. Next();
  154. return old;
  155. }
  156. inline TIterator& operator++() noexcept {
  157. Next();
  158. return *this;
  159. }
  160. friend inline bool operator==(const TIterator& l, const TIterator& r) noexcept {
  161. return l.C_ == r.C_;
  162. }
  163. friend inline bool operator!=(const TIterator& l, const TIterator& r) noexcept {
  164. return !(l == r);
  165. }
  166. inline struct addrinfo& operator*() const noexcept {
  167. return *C_;
  168. }
  169. inline struct addrinfo* operator->() const noexcept {
  170. return C_;
  171. }
  172. private:
  173. struct addrinfo* C_;
  174. };
  175. TNetworkAddress(ui16 port);
  176. TNetworkAddress(const TString& host, ui16 port);
  177. TNetworkAddress(const TString& host, ui16 port, int flags);
  178. TNetworkAddress(const TUnixSocketPath& unixSocketPath, int flags = 0);
  179. ~TNetworkAddress();
  180. inline TIterator Begin() const noexcept {
  181. return TIterator(Info());
  182. }
  183. inline TIterator End() const noexcept {
  184. return TIterator(nullptr);
  185. }
  186. private:
  187. struct addrinfo* Info() const noexcept;
  188. private:
  189. class TImpl;
  190. TSimpleIntrusivePtr<TImpl> Impl_;
  191. };
  192. class TSocket;
  193. class TSocketHolder: public TMoveOnly {
  194. public:
  195. inline TSocketHolder()
  196. : Fd_(INVALID_SOCKET)
  197. {
  198. }
  199. inline TSocketHolder(SOCKET fd)
  200. : Fd_(fd)
  201. {
  202. }
  203. inline TSocketHolder(TSocketHolder&& other) noexcept {
  204. Fd_ = other.Fd_;
  205. other.Fd_ = INVALID_SOCKET;
  206. }
  207. inline TSocketHolder& operator=(TSocketHolder&& other) noexcept {
  208. Close();
  209. Swap(other);
  210. return *this;
  211. }
  212. inline ~TSocketHolder() {
  213. Close();
  214. }
  215. inline SOCKET Release() noexcept {
  216. SOCKET ret = Fd_;
  217. Fd_ = INVALID_SOCKET;
  218. return ret;
  219. }
  220. void Close() noexcept;
  221. inline void ShutDown(int mode) const {
  222. ::ShutDown(Fd_, mode);
  223. }
  224. inline void Swap(TSocketHolder& r) noexcept {
  225. DoSwap(Fd_, r.Fd_);
  226. }
  227. inline bool Closed() const noexcept {
  228. return Fd_ == INVALID_SOCKET;
  229. }
  230. inline operator SOCKET() const noexcept {
  231. return Fd_;
  232. }
  233. private:
  234. SOCKET Fd_;
  235. // do not allow construction of TSocketHolder from TSocket
  236. TSocketHolder(const TSocket& fd);
  237. };
  238. class TSocket {
  239. public:
  240. using TPart = IOutputStream::TPart;
  241. class TOps {
  242. public:
  243. inline TOps() noexcept = default;
  244. virtual ~TOps() = default;
  245. virtual ssize_t Send(SOCKET fd, const void* data, size_t len) = 0;
  246. virtual ssize_t Recv(SOCKET fd, void* buf, size_t len) = 0;
  247. virtual ssize_t SendV(SOCKET fd, const TPart* parts, size_t count) = 0;
  248. };
  249. TSocket();
  250. TSocket(SOCKET fd);
  251. TSocket(SOCKET fd, TOps* ops);
  252. TSocket(const TNetworkAddress& addr);
  253. TSocket(const TNetworkAddress& addr, const TDuration& timeOut);
  254. TSocket(const TNetworkAddress& addr, const TInstant& deadLine);
  255. ~TSocket();
  256. template <class T>
  257. inline void SetSockOpt(int level, int optname, T opt) {
  258. CheckedSetSockOpt(Fd(), level, optname, opt, "TSocket");
  259. }
  260. inline void SetSocketTimeout(long timeout) {
  261. ::SetSocketTimeout(Fd(), timeout);
  262. }
  263. inline void SetSocketTimeout(long sec, long msec) {
  264. ::SetSocketTimeout(Fd(), sec, msec);
  265. }
  266. inline void SetNoDelay(bool value) {
  267. ::SetNoDelay(Fd(), value);
  268. }
  269. inline void SetLinger(bool on, unsigned len) {
  270. ::SetLinger(Fd(), on, len);
  271. }
  272. inline void SetZeroLinger() {
  273. ::SetZeroLinger(Fd());
  274. }
  275. inline void SetKeepAlive(bool value) {
  276. ::SetKeepAlive(Fd(), value);
  277. }
  278. inline void SetOutputBuffer(unsigned value) {
  279. ::SetOutputBuffer(Fd(), value);
  280. }
  281. inline void SetInputBuffer(unsigned value) {
  282. ::SetInputBuffer(Fd(), value);
  283. }
  284. inline size_t MaximumSegmentSize() const {
  285. return GetMaximumSegmentSize(Fd());
  286. }
  287. inline size_t MaximumTransferUnit() const {
  288. return GetMaximumTransferUnit(Fd());
  289. }
  290. inline void ShutDown(int mode) const {
  291. ::ShutDown(Fd(), mode);
  292. }
  293. void Close();
  294. ssize_t Send(const void* data, size_t len);
  295. ssize_t Recv(void* buf, size_t len);
  296. /*
  297. * scatter/gather io
  298. */
  299. ssize_t SendV(const TPart* parts, size_t count);
  300. inline operator SOCKET() const noexcept {
  301. return Fd();
  302. }
  303. private:
  304. SOCKET Fd() const noexcept;
  305. private:
  306. class TImpl;
  307. TSimpleIntrusivePtr<TImpl> Impl_;
  308. };
  309. class TSocketInput: public IInputStream {
  310. public:
  311. TSocketInput(const TSocket& s) noexcept;
  312. ~TSocketInput() override;
  313. TSocketInput(TSocketInput&&) noexcept = default;
  314. TSocketInput& operator=(TSocketInput&&) noexcept = default;
  315. const TSocket& GetSocket() const noexcept {
  316. return S_;
  317. }
  318. private:
  319. size_t DoRead(void* buf, size_t len) override;
  320. private:
  321. TSocket S_;
  322. };
  323. class TSocketOutput: public IOutputStream {
  324. public:
  325. TSocketOutput(const TSocket& s) noexcept;
  326. ~TSocketOutput() override;
  327. TSocketOutput(TSocketOutput&&) noexcept = default;
  328. TSocketOutput& operator=(TSocketOutput&&) noexcept = default;
  329. const TSocket& GetSocket() const noexcept {
  330. return S_;
  331. }
  332. private:
  333. void DoWrite(const void* buf, size_t len) override;
  334. void DoWriteV(const TPart* parts, size_t count) override;
  335. private:
  336. TSocket S_;
  337. };
  338. //return -(error code) if error occured, or number of ready fds
  339. ssize_t PollD(struct pollfd fds[], nfds_t nfds, const TInstant& deadLine) noexcept;