catboost 1.25.1 → 1.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. package/DEPLOYMENT.md +22 -15
  2. package/README.md +43 -27
  3. package/binding.gyp +5 -7
  4. package/build_scripts/bootstrap.js +2 -1
  5. package/build_scripts/out/build.js +46 -68
  6. package/build_scripts/out/build_model.js +1 -1
  7. package/build_scripts/out/{build_ya.js → build_native.js} +1 -1
  8. package/build_scripts/out/ci.js +5 -5
  9. package/build_scripts/out/common.js +1 -1
  10. package/build_scripts/out/config.js +32 -18
  11. package/build_scripts/out/install.js +5 -3
  12. package/build_scripts/out/package_prepublish.js +1 -1
  13. package/build_scripts/out/packaging.js +1 -19
  14. package/build_scripts/out/run_tests.js +1 -1
  15. package/build_scripts/out/test.js +8 -3
  16. package/config.json +18 -11
  17. package/inc/catboost/libs/model_interface/c_api.h +367 -5
  18. package/lib/catboost.d.ts +65 -21
  19. package/package.json +4 -4
  20. package/src/api_helpers.cpp +100 -24
  21. package/src/api_helpers.h +8 -7
  22. package/src/api_module.cpp +1 -2
  23. package/src/model.cpp +483 -83
  24. package/src/model.h +24 -9
  25. package/inc/contrib/libs/cxxsupp/system_stl/include/stlfwd +0 -14
  26. package/inc/util/charset/recode_result.h +0 -9
  27. package/inc/util/charset/unicode_table.h +0 -123
  28. package/inc/util/charset/unidata.h +0 -421
  29. package/inc/util/charset/utf8.h +0 -384
  30. package/inc/util/charset/wide.h +0 -843
  31. package/inc/util/charset/wide_specific.h +0 -22
  32. package/inc/util/datetime/base.h +0 -669
  33. package/inc/util/datetime/constants.h +0 -7
  34. package/inc/util/datetime/cputimer.h +0 -124
  35. package/inc/util/datetime/parser.h +0 -292
  36. package/inc/util/datetime/systime.h +0 -47
  37. package/inc/util/datetime/uptime.h +0 -8
  38. package/inc/util/digest/city.h +0 -88
  39. package/inc/util/digest/fnv.h +0 -73
  40. package/inc/util/digest/multi.h +0 -14
  41. package/inc/util/digest/murmur.h +0 -57
  42. package/inc/util/digest/numeric.h +0 -86
  43. package/inc/util/digest/sequence.h +0 -48
  44. package/inc/util/draft/date.h +0 -129
  45. package/inc/util/draft/datetime.h +0 -184
  46. package/inc/util/draft/enum.h +0 -136
  47. package/inc/util/draft/holder_vector.h +0 -102
  48. package/inc/util/draft/ip.h +0 -131
  49. package/inc/util/draft/matrix.h +0 -108
  50. package/inc/util/draft/memory.h +0 -40
  51. package/inc/util/folder/dirent_win.h +0 -46
  52. package/inc/util/folder/dirut.h +0 -121
  53. package/inc/util/folder/filelist.h +0 -81
  54. package/inc/util/folder/fts.h +0 -108
  55. package/inc/util/folder/iterator.h +0 -109
  56. package/inc/util/folder/lstat_win.h +0 -20
  57. package/inc/util/folder/path.h +0 -225
  58. package/inc/util/folder/pathsplit.h +0 -113
  59. package/inc/util/folder/tempdir.h +0 -42
  60. package/inc/util/generic/adaptor.h +0 -134
  61. package/inc/util/generic/algorithm.h +0 -765
  62. package/inc/util/generic/array_ref.h +0 -282
  63. package/inc/util/generic/array_size.h +0 -24
  64. package/inc/util/generic/benchmark/vector_count_ctor/f.h +0 -9
  65. package/inc/util/generic/bitmap.h +0 -1115
  66. package/inc/util/generic/bitops.h +0 -459
  67. package/inc/util/generic/bt_exception.h +0 -24
  68. package/inc/util/generic/buffer.h +0 -232
  69. package/inc/util/generic/cast.h +0 -176
  70. package/inc/util/generic/deque.h +0 -24
  71. package/inc/util/generic/explicit_type.h +0 -42
  72. package/inc/util/generic/fastqueue.h +0 -55
  73. package/inc/util/generic/flags.h +0 -244
  74. package/inc/util/generic/function.h +0 -103
  75. package/inc/util/generic/fwd.h +0 -171
  76. package/inc/util/generic/guid.h +0 -61
  77. package/inc/util/generic/hash.h +0 -2032
  78. package/inc/util/generic/hash_primes.h +0 -140
  79. package/inc/util/generic/hash_set.h +0 -490
  80. package/inc/util/generic/hide_ptr.h +0 -3
  81. package/inc/util/generic/intrlist.h +0 -876
  82. package/inc/util/generic/is_in.h +0 -53
  83. package/inc/util/generic/iterator.h +0 -137
  84. package/inc/util/generic/iterator_range.h +0 -105
  85. package/inc/util/generic/lazy_value.h +0 -66
  86. package/inc/util/generic/list.h +0 -22
  87. package/inc/util/generic/map.h +0 -44
  88. package/inc/util/generic/mapfindptr.h +0 -60
  89. package/inc/util/generic/maybe.h +0 -713
  90. package/inc/util/generic/maybe_traits.h +0 -164
  91. package/inc/util/generic/mem_copy.h +0 -55
  92. package/inc/util/generic/noncopyable.h +0 -38
  93. package/inc/util/generic/object_counter.h +0 -53
  94. package/inc/util/generic/ptr.h +0 -1113
  95. package/inc/util/generic/queue.h +0 -57
  96. package/inc/util/generic/refcount.h +0 -162
  97. package/inc/util/generic/reserve.h +0 -11
  98. package/inc/util/generic/scope.h +0 -65
  99. package/inc/util/generic/serialized_enum.h +0 -406
  100. package/inc/util/generic/set.h +0 -42
  101. package/inc/util/generic/singleton.h +0 -136
  102. package/inc/util/generic/size_literals.h +0 -65
  103. package/inc/util/generic/stack.h +0 -18
  104. package/inc/util/generic/store_policy.h +0 -120
  105. package/inc/util/generic/strbase.h +0 -612
  106. package/inc/util/generic/strbuf.h +0 -552
  107. package/inc/util/generic/strfcpy.h +0 -17
  108. package/inc/util/generic/string.h +0 -1572
  109. package/inc/util/generic/string_hash.h +0 -21
  110. package/inc/util/generic/string_ut.h +0 -1175
  111. package/inc/util/generic/type_name.h +0 -34
  112. package/inc/util/generic/typelist.h +0 -114
  113. package/inc/util/generic/typetraits.h +0 -325
  114. package/inc/util/generic/utility.h +0 -132
  115. package/inc/util/generic/va_args.h +0 -400
  116. package/inc/util/generic/variant.h +0 -631
  117. package/inc/util/generic/variant_traits.h +0 -171
  118. package/inc/util/generic/vector.h +0 -119
  119. package/inc/util/generic/xrange.h +0 -258
  120. package/inc/util/generic/yexception.h +0 -212
  121. package/inc/util/generic/yexception_ut.h +0 -14
  122. package/inc/util/generic/ylimits.h +0 -92
  123. package/inc/util/generic/ymath.h +0 -206
  124. package/inc/util/memory/addstorage.h +0 -93
  125. package/inc/util/memory/alloc.h +0 -27
  126. package/inc/util/memory/blob.h +0 -296
  127. package/inc/util/memory/mmapalloc.h +0 -8
  128. package/inc/util/memory/pool.h +0 -432
  129. package/inc/util/memory/segmented_string_pool.h +0 -194
  130. package/inc/util/memory/segpool_alloc.h +0 -118
  131. package/inc/util/memory/smallobj.h +0 -141
  132. package/inc/util/memory/tempbuf.h +0 -111
  133. package/inc/util/network/address.h +0 -136
  134. package/inc/util/network/endpoint.h +0 -61
  135. package/inc/util/network/hostip.h +0 -16
  136. package/inc/util/network/init.h +0 -60
  137. package/inc/util/network/interface.h +0 -17
  138. package/inc/util/network/iovec.h +0 -65
  139. package/inc/util/network/ip.h +0 -116
  140. package/inc/util/network/nonblock.h +0 -8
  141. package/inc/util/network/pair.h +0 -9
  142. package/inc/util/network/poller.h +0 -58
  143. package/inc/util/network/pollerimpl.h +0 -707
  144. package/inc/util/network/sock.h +0 -608
  145. package/inc/util/network/socket.h +0 -421
  146. package/inc/util/random/common_ops.h +0 -130
  147. package/inc/util/random/easy.h +0 -47
  148. package/inc/util/random/entropy.h +0 -21
  149. package/inc/util/random/fast.h +0 -101
  150. package/inc/util/random/init_atfork.h +0 -3
  151. package/inc/util/random/lcg_engine.h +0 -66
  152. package/inc/util/random/mersenne.h +0 -46
  153. package/inc/util/random/mersenne32.h +0 -50
  154. package/inc/util/random/mersenne64.h +0 -50
  155. package/inc/util/random/normal.h +0 -38
  156. package/inc/util/random/random.h +0 -30
  157. package/inc/util/random/shuffle.h +0 -39
  158. package/inc/util/str_stl.h +0 -266
  159. package/inc/util/stream/aligned.h +0 -99
  160. package/inc/util/stream/buffer.h +0 -119
  161. package/inc/util/stream/buffered.h +0 -225
  162. package/inc/util/stream/debug.h +0 -53
  163. package/inc/util/stream/direct_io.h +0 -43
  164. package/inc/util/stream/file.h +0 -108
  165. package/inc/util/stream/format.h +0 -444
  166. package/inc/util/stream/fwd.h +0 -100
  167. package/inc/util/stream/hex.h +0 -8
  168. package/inc/util/stream/holder.h +0 -44
  169. package/inc/util/stream/input.h +0 -273
  170. package/inc/util/stream/labeled.h +0 -19
  171. package/inc/util/stream/length.h +0 -100
  172. package/inc/util/stream/mem.h +0 -255
  173. package/inc/util/stream/multi.h +0 -32
  174. package/inc/util/stream/null.h +0 -61
  175. package/inc/util/stream/output.h +0 -304
  176. package/inc/util/stream/pipe.h +0 -112
  177. package/inc/util/stream/printf.h +0 -25
  178. package/inc/util/stream/str.h +0 -207
  179. package/inc/util/stream/tee.h +0 -28
  180. package/inc/util/stream/tempbuf.h +0 -21
  181. package/inc/util/stream/tokenizer.h +0 -214
  182. package/inc/util/stream/trace.h +0 -60
  183. package/inc/util/stream/walk.h +0 -35
  184. package/inc/util/stream/zerocopy.h +0 -91
  185. package/inc/util/stream/zerocopy_output.h +0 -57
  186. package/inc/util/stream/zlib.h +0 -173
  187. package/inc/util/string/ascii.h +0 -236
  188. package/inc/util/string/builder.h +0 -39
  189. package/inc/util/string/cast.h +0 -347
  190. package/inc/util/string/cstriter.h +0 -14
  191. package/inc/util/string/escape.h +0 -70
  192. package/inc/util/string/hex.h +0 -59
  193. package/inc/util/string/join.h +0 -194
  194. package/inc/util/string/printf.h +0 -13
  195. package/inc/util/string/reverse.h +0 -16
  196. package/inc/util/string/split.h +0 -1080
  197. package/inc/util/string/strip.h +0 -257
  198. package/inc/util/string/strspn.h +0 -65
  199. package/inc/util/string/subst.h +0 -56
  200. package/inc/util/string/type.h +0 -50
  201. package/inc/util/string/util.h +0 -195
  202. package/inc/util/string/vector.h +0 -132
  203. package/inc/util/system/align.h +0 -50
  204. package/inc/util/system/atexit.h +0 -22
  205. package/inc/util/system/atomic.h +0 -51
  206. package/inc/util/system/atomic_gcc.h +0 -90
  207. package/inc/util/system/atomic_ops.h +0 -189
  208. package/inc/util/system/atomic_win.h +0 -114
  209. package/inc/util/system/backtrace.h +0 -39
  210. package/inc/util/system/byteorder.h +0 -186
  211. package/inc/util/system/compat.h +0 -84
  212. package/inc/util/system/compiler.h +0 -620
  213. package/inc/util/system/condvar.h +0 -71
  214. package/inc/util/system/context.h +0 -181
  215. package/inc/util/system/context_aarch64.h +0 -8
  216. package/inc/util/system/context_i686.h +0 -9
  217. package/inc/util/system/context_x86.h +0 -12
  218. package/inc/util/system/context_x86_64.h +0 -7
  219. package/inc/util/system/cpu_id.h +0 -159
  220. package/inc/util/system/daemon.h +0 -28
  221. package/inc/util/system/datetime.h +0 -98
  222. package/inc/util/system/defaults.h +0 -149
  223. package/inc/util/system/demangle.h +0 -5
  224. package/inc/util/system/demangle_impl.h +0 -23
  225. package/inc/util/system/direct_io.h +0 -71
  226. package/inc/util/system/dynlib.h +0 -119
  227. package/inc/util/system/env.h +0 -32
  228. package/inc/util/system/error.h +0 -95
  229. package/inc/util/system/event.h +0 -122
  230. package/inc/util/system/execpath.h +0 -17
  231. package/inc/util/system/fasttime.h +0 -6
  232. package/inc/util/system/fhandle.h +0 -27
  233. package/inc/util/system/file.h +0 -210
  234. package/inc/util/system/file_lock.h +0 -34
  235. package/inc/util/system/filemap.h +0 -383
  236. package/inc/util/system/flock.h +0 -35
  237. package/inc/util/system/fs.h +0 -156
  238. package/inc/util/system/fs_win.h +0 -29
  239. package/inc/util/system/fstat.h +0 -46
  240. package/inc/util/system/getpid.h +0 -12
  241. package/inc/util/system/guard.h +0 -179
  242. package/inc/util/system/hi_lo.h +0 -139
  243. package/inc/util/system/hostname.h +0 -10
  244. package/inc/util/system/hp_timer.h +0 -36
  245. package/inc/util/system/info.h +0 -12
  246. package/inc/util/system/interrupt_signals.h +0 -22
  247. package/inc/util/system/madvise.h +0 -30
  248. package/inc/util/system/maxlen.h +0 -32
  249. package/inc/util/system/mem_info.h +0 -18
  250. package/inc/util/system/mincore.h +0 -38
  251. package/inc/util/system/mktemp.h +0 -11
  252. package/inc/util/system/mlock.h +0 -43
  253. package/inc/util/system/mutex.h +0 -67
  254. package/inc/util/system/nice.h +0 -3
  255. package/inc/util/system/pipe.h +0 -90
  256. package/inc/util/system/platform.h +0 -246
  257. package/inc/util/system/progname.h +0 -13
  258. package/inc/util/system/protect.h +0 -25
  259. package/inc/util/system/rusage.h +0 -26
  260. package/inc/util/system/rwlock.h +0 -78
  261. package/inc/util/system/sanitizers.h +0 -122
  262. package/inc/util/system/sem.h +0 -41
  263. package/inc/util/system/shellcommand.h +0 -472
  264. package/inc/util/system/shmat.h +0 -32
  265. package/inc/util/system/sigset.h +0 -78
  266. package/inc/util/system/spin_wait.h +0 -10
  267. package/inc/util/system/spinlock.h +0 -121
  268. package/inc/util/system/src_location.h +0 -25
  269. package/inc/util/system/src_root.h +0 -68
  270. package/inc/util/system/sys_alloc.h +0 -43
  271. package/inc/util/system/sysstat.h +0 -52
  272. package/inc/util/system/tempfile.h +0 -34
  273. package/inc/util/system/thread.h +0 -167
  274. package/inc/util/system/tls.h +0 -307
  275. package/inc/util/system/types.h +0 -119
  276. package/inc/util/system/unaligned_mem.h +0 -67
  277. package/inc/util/system/user.h +0 -5
  278. package/inc/util/system/utime.h +0 -6
  279. package/inc/util/system/valgrind.h +0 -48
  280. package/inc/util/system/winint.h +0 -43
  281. package/inc/util/system/yassert.h +0 -121
  282. package/inc/util/system/yield.h +0 -4
  283. package/inc/util/thread/factory.h +0 -65
  284. package/inc/util/thread/fwd.h +0 -30
  285. package/inc/util/thread/lfqueue.h +0 -406
  286. package/inc/util/thread/lfstack.h +0 -188
  287. package/inc/util/thread/pool.h +0 -388
  288. package/inc/util/thread/singleton.h +0 -42
  289. package/inc/util/ysafeptr.h +0 -427
  290. package/inc/util/ysaveload.h +0 -700
@@ -1,608 +0,0 @@
1
- #pragma once
2
-
3
- #include <util/folder/path.h>
4
- #include <util/system/defaults.h>
5
- #include <util/string/cast.h>
6
- #include <util/stream/output.h>
7
- #include <util/system/sysstat.h>
8
-
9
- #if defined(_win_) || defined(_cygwin_)
10
- #include <util/system/file.h>
11
- #else
12
- #include <sys/un.h>
13
- #include <sys/stat.h>
14
- #endif //_win_
15
-
16
- #include "init.h"
17
- #include "ip.h"
18
- #include "socket.h"
19
-
20
- constexpr ui16 DEF_LOCAL_SOCK_MODE = 00644;
21
-
22
- // Base abstract class for socket address
23
- struct ISockAddr {
24
- virtual ~ISockAddr() = default;
25
- // Max size of the address that we can store (arg of recvfrom)
26
- virtual socklen_t Size() const = 0;
27
- // Real length of the address (arg of sendto)
28
- virtual socklen_t Len() const = 0;
29
- // cast to sockaddr* to pass to any syscall
30
- virtual sockaddr* SockAddr() = 0;
31
- virtual const sockaddr* SockAddr() const = 0;
32
- // address in human readable form
33
- virtual TString ToString() const = 0;
34
-
35
- protected:
36
- // below are the implemetation methods that can be called by T*Socket classes
37
- friend class TBaseSocket;
38
- friend class TDgramSocket;
39
- friend class TStreamSocket;
40
-
41
- virtual int ResolveAddr() const {
42
- // usually it's nothing to do here
43
- return 0;
44
- }
45
- virtual int Bind(SOCKET s, ui16 mode) const = 0;
46
- };
47
-
48
- #if defined(_win_) || defined(_cygwin_)
49
- #define YAF_LOCAL AF_INET
50
- struct TSockAddrLocal: public ISockAddr {
51
- TSockAddrLocal() {
52
- Clear();
53
- }
54
-
55
- TSockAddrLocal(const char* path) {
56
- Set(path);
57
- }
58
-
59
- socklen_t Size() const {
60
- return sizeof(sockaddr_in);
61
- }
62
-
63
- socklen_t Len() const {
64
- return Size();
65
- }
66
-
67
- inline void Clear() noexcept {
68
- Zero(in);
69
- Zero(Path);
70
- }
71
-
72
- inline void Set(const char* path) noexcept {
73
- Clear();
74
- in.sin_family = AF_INET;
75
- in.sin_addr.s_addr = IpFromString("127.0.0.1");
76
- in.sin_port = 0;
77
- strlcpy(Path, path, PathSize);
78
- }
79
-
80
- sockaddr* SockAddr() {
81
- return (struct sockaddr*)(&in);
82
- }
83
-
84
- const sockaddr* SockAddr() const {
85
- return (const struct sockaddr*)(&in);
86
- }
87
-
88
- TString ToString() const {
89
- return TString(Path);
90
- }
91
-
92
- TFsPath ToPath() const {
93
- return TFsPath(Path);
94
- }
95
-
96
- int ResolveAddr() const {
97
- if (in.sin_port == 0) {
98
- int ret = 0;
99
- // 1. open file
100
- TFileHandle f(Path, OpenExisting | RdOnly);
101
- if (!f.IsOpen())
102
- return -errno;
103
-
104
- // 2. read the port from file
105
- ret = f.Read(&in.sin_port, sizeof(in.sin_port));
106
- if (ret != sizeof(in.sin_port))
107
- return -(errno ? errno : EFAULT);
108
- }
109
-
110
- return 0;
111
- }
112
-
113
- int Bind(SOCKET s, ui16 mode) const {
114
- Y_UNUSED(mode);
115
- int ret = 0;
116
- // 1. open file
117
- TFileHandle f(Path, CreateAlways | WrOnly);
118
- if (!f.IsOpen())
119
- return -errno;
120
-
121
- // 2. find port and bind to it
122
- in.sin_port = 0;
123
- ret = bind(s, SockAddr(), Len());
124
- if (ret != 0)
125
- return -WSAGetLastError();
126
-
127
- int size = Size();
128
- ret = getsockname(s, (struct sockaddr*)(&in), &size);
129
- if (ret != 0)
130
- return -WSAGetLastError();
131
-
132
- // 3. write port to file
133
- ret = f.Write(&(in.sin_port), sizeof(in.sin_port));
134
- if (ret != sizeof(in.sin_port))
135
- return -errno;
136
-
137
- return 0;
138
- }
139
-
140
- static constexpr size_t PathSize = 128;
141
- mutable struct sockaddr_in in;
142
- char Path[PathSize];
143
- };
144
- #else
145
- #define YAF_LOCAL AF_LOCAL
146
- struct TSockAddrLocal: public sockaddr_un, public ISockAddr {
147
- TSockAddrLocal() {
148
- Clear();
149
- }
150
-
151
- TSockAddrLocal(const char* path) {
152
- Set(path);
153
- }
154
-
155
- socklen_t Size() const override {
156
- return sizeof(sockaddr_un);
157
- }
158
-
159
- socklen_t Len() const override {
160
- return strlen(sun_path) + 2;
161
- }
162
-
163
- inline void Clear() noexcept {
164
- Zero(*(sockaddr_un*)this);
165
- }
166
-
167
- inline void Set(const char* path) noexcept {
168
- Clear();
169
- sun_family = AF_UNIX;
170
- strlcpy(sun_path, path, sizeof(sun_path));
171
- }
172
-
173
- sockaddr* SockAddr() override {
174
- return (struct sockaddr*)(struct sockaddr_un*)this;
175
- }
176
-
177
- const sockaddr* SockAddr() const override {
178
- return (const struct sockaddr*)(const struct sockaddr_un*)this;
179
- }
180
-
181
- TString ToString() const override {
182
- return TString(sun_path);
183
- }
184
-
185
- TFsPath ToPath() const {
186
- return TFsPath(sun_path);
187
- }
188
-
189
- int Bind(SOCKET s, ui16 mode) const override {
190
- (void)unlink(sun_path);
191
-
192
- int ret = bind(s, SockAddr(), Len());
193
- if (ret < 0)
194
- return -errno;
195
-
196
- ret = Chmod(sun_path, mode);
197
- if (ret < 0)
198
- return -errno;
199
- return 0;
200
- }
201
- };
202
- #endif // _win_
203
-
204
- struct TSockAddrInet: public sockaddr_in, public ISockAddr {
205
- TSockAddrInet() {
206
- Clear();
207
- }
208
-
209
- TSockAddrInet(TIpHost ip, TIpPort port) {
210
- Set(ip, port);
211
- }
212
-
213
- TSockAddrInet(const char* ip, TIpPort port) {
214
- Set(IpFromString(ip), port);
215
- }
216
-
217
- socklen_t Size() const override {
218
- return sizeof(sockaddr_in);
219
- }
220
-
221
- socklen_t Len() const override {
222
- return Size();
223
- }
224
-
225
- inline void Clear() noexcept {
226
- Zero(*(sockaddr_in*)this);
227
- }
228
-
229
- inline void Set(TIpHost ip, TIpPort port) noexcept {
230
- Clear();
231
- sin_family = AF_INET;
232
- sin_addr.s_addr = ip;
233
- sin_port = HostToInet(port);
234
- }
235
-
236
- sockaddr* SockAddr() override {
237
- return (struct sockaddr*)(struct sockaddr_in*)this;
238
- }
239
-
240
- const sockaddr* SockAddr() const override {
241
- return (const struct sockaddr*)(const struct sockaddr_in*)this;
242
- }
243
-
244
- TString ToString() const override {
245
- return IpToString(sin_addr.s_addr) + ":" + ::ToString(InetToHost(sin_port));
246
- }
247
-
248
- int Bind(SOCKET s, ui16 mode) const override {
249
- Y_UNUSED(mode);
250
- int ret = bind(s, SockAddr(), Len());
251
- if (ret < 0)
252
- return -errno;
253
-
254
- socklen_t len = Len();
255
- if (getsockname(s, (struct sockaddr*)(SockAddr()), &len) < 0)
256
- return -WSAGetLastError();
257
-
258
- return 0;
259
- }
260
-
261
- TIpHost GetIp() const noexcept {
262
- return sin_addr.s_addr;
263
- }
264
-
265
- TIpPort GetPort() const noexcept {
266
- return InetToHost(sin_port);
267
- }
268
-
269
- void SetPort(TIpPort port) noexcept {
270
- sin_port = HostToInet(port);
271
- }
272
- };
273
-
274
- struct TSockAddrInet6: public sockaddr_in6, public ISockAddr {
275
- TSockAddrInet6() {
276
- Clear();
277
- }
278
-
279
- TSockAddrInet6(const char* ip6, const TIpPort port) {
280
- Set(ip6, port);
281
- }
282
-
283
- socklen_t Size() const override {
284
- return sizeof(sockaddr_in6);
285
- }
286
-
287
- socklen_t Len() const override {
288
- return Size();
289
- }
290
-
291
- inline void Clear() noexcept {
292
- Zero(*(sockaddr_in6*)this);
293
- }
294
-
295
- inline void Set(const char* ip6, const TIpPort port) noexcept {
296
- Clear();
297
- sin6_family = AF_INET6;
298
- inet_pton(AF_INET6, ip6, &sin6_addr);
299
- sin6_port = HostToInet(port);
300
- }
301
-
302
- sockaddr* SockAddr() override {
303
- return (struct sockaddr*)(struct sockaddr_in6*)this;
304
- }
305
-
306
- const sockaddr* SockAddr() const override {
307
- return (const struct sockaddr*)(const struct sockaddr_in6*)this;
308
- }
309
-
310
- TString ToString() const override {
311
- return "[" + GetIp() + "]:" + ::ToString(InetToHost(sin6_port));
312
- }
313
-
314
- int Bind(SOCKET s, ui16 mode) const override {
315
- Y_UNUSED(mode);
316
- int ret = bind(s, SockAddr(), Len());
317
- if (ret < 0) {
318
- return -errno;
319
- }
320
- socklen_t len = Len();
321
- if (getsockname(s, (struct sockaddr*)(SockAddr()), &len) < 0) {
322
- return -WSAGetLastError();
323
- }
324
- return 0;
325
- }
326
-
327
- TString GetIp() const noexcept {
328
- char ip6[INET6_ADDRSTRLEN];
329
- inet_ntop(AF_INET6, (void*)&sin6_addr, ip6, INET6_ADDRSTRLEN);
330
- return TString(ip6);
331
- }
332
-
333
- TIpPort GetPort() const noexcept {
334
- return InetToHost(sin6_port);
335
- }
336
-
337
- void SetPort(TIpPort port) noexcept {
338
- sin6_port = HostToInet(port);
339
- }
340
- };
341
-
342
- using TSockAddrLocalStream = TSockAddrLocal;
343
- using TSockAddrLocalDgram = TSockAddrLocal;
344
- using TSockAddrInetStream = TSockAddrInet;
345
- using TSockAddrInetDgram = TSockAddrInet;
346
- using TSockAddrInet6Stream = TSockAddrInet6;
347
- using TSockAddrInet6Dgram = TSockAddrInet6;
348
-
349
- class TBaseSocket: public TSocketHolder {
350
- protected:
351
- TBaseSocket(SOCKET fd)
352
- : TSocketHolder(fd)
353
- {
354
- }
355
-
356
- public:
357
- int Bind(const ISockAddr* addr, ui16 mode = DEF_LOCAL_SOCK_MODE) {
358
- return addr->Bind((SOCKET) * this, mode);
359
- }
360
-
361
- void CheckSock() {
362
- if ((SOCKET) * this == INVALID_SOCKET)
363
- ythrow TSystemError() << "no socket";
364
- }
365
-
366
- static ssize_t Check(ssize_t ret, const char* op = "") {
367
- if (ret < 0)
368
- ythrow TSystemError(-(int)ret) << "socket operation " << op;
369
- return ret;
370
- }
371
- };
372
-
373
- class TDgramSocket: public TBaseSocket {
374
- protected:
375
- TDgramSocket(SOCKET fd)
376
- : TBaseSocket(fd)
377
- {
378
- }
379
-
380
- public:
381
- ssize_t SendTo(const void* msg, size_t len, const ISockAddr* toAddr) {
382
- ssize_t ret = toAddr->ResolveAddr();
383
- if (ret < 0) {
384
- return -LastSystemError();
385
- }
386
-
387
- ret = sendto((SOCKET) * this, (const char*)msg, (int)len, 0, toAddr->SockAddr(), toAddr->Len());
388
- if (ret < 0) {
389
- return -LastSystemError();
390
- }
391
-
392
- return ret;
393
- }
394
-
395
- ssize_t RecvFrom(void* buf, size_t len, ISockAddr* fromAddr) {
396
- socklen_t fromSize = fromAddr->Size();
397
- const ssize_t ret = recvfrom((SOCKET) * this, (char*)buf, (int)len, 0, fromAddr->SockAddr(), &fromSize);
398
- if (ret < 0) {
399
- return -LastSystemError();
400
- }
401
-
402
- return ret;
403
- }
404
- };
405
-
406
- class TStreamSocket: public TBaseSocket {
407
- protected:
408
- explicit TStreamSocket(SOCKET fd)
409
- : TBaseSocket(fd)
410
- {
411
- }
412
-
413
- public:
414
- TStreamSocket()
415
- : TBaseSocket(INVALID_SOCKET)
416
- {
417
- }
418
-
419
- ssize_t Send(const void* msg, size_t len, int flags = 0) {
420
- const ssize_t ret = send((SOCKET) * this, (const char*)msg, (int)len, flags);
421
- if (ret < 0)
422
- return -errno;
423
-
424
- return ret;
425
- }
426
-
427
- ssize_t Recv(void* buf, size_t len, int flags = 0) {
428
- const ssize_t ret = recv((SOCKET) * this, (char*)buf, (int)len, flags);
429
- if (ret < 0)
430
- return -errno;
431
-
432
- return ret;
433
- }
434
-
435
- int Connect(const ISockAddr* addr) {
436
- int ret = addr->ResolveAddr();
437
- if (ret < 0)
438
- return -errno;
439
-
440
- ret = connect((SOCKET) * this, addr->SockAddr(), addr->Len());
441
- if (ret < 0)
442
- return -errno;
443
-
444
- return ret;
445
- }
446
-
447
- int Listen(int backlog) {
448
- int ret = listen((SOCKET) * this, backlog);
449
- if (ret < 0)
450
- return -errno;
451
-
452
- return ret;
453
- }
454
-
455
- int Accept(TStreamSocket* acceptedSock, ISockAddr* acceptedAddr = nullptr) {
456
- SOCKET s = INVALID_SOCKET;
457
- if (acceptedAddr) {
458
- socklen_t acceptedSize = acceptedAddr->Size();
459
- s = accept((SOCKET) * this, acceptedAddr->SockAddr(), &acceptedSize);
460
- } else {
461
- s = accept((SOCKET) * this, nullptr, nullptr);
462
- }
463
-
464
- if (s == INVALID_SOCKET)
465
- return -errno;
466
-
467
- TSocketHolder sock(s);
468
- acceptedSock->Swap(sock);
469
- return 0;
470
- }
471
- };
472
-
473
- class TLocalDgramSocket: public TDgramSocket {
474
- public:
475
- TLocalDgramSocket(SOCKET fd)
476
- : TDgramSocket(fd)
477
- {
478
- }
479
-
480
- TLocalDgramSocket()
481
- : TDgramSocket(socket(YAF_LOCAL, SOCK_DGRAM, 0))
482
- {
483
- }
484
- };
485
-
486
- class TInetDgramSocket: public TDgramSocket {
487
- public:
488
- TInetDgramSocket(SOCKET fd)
489
- : TDgramSocket(fd)
490
- {
491
- }
492
-
493
- TInetDgramSocket()
494
- : TDgramSocket(socket(AF_INET, SOCK_DGRAM, 0))
495
- {
496
- }
497
- };
498
-
499
- class TInet6DgramSocket: public TDgramSocket {
500
- public:
501
- TInet6DgramSocket(SOCKET fd)
502
- : TDgramSocket(fd)
503
- {
504
- }
505
-
506
- TInet6DgramSocket()
507
- : TDgramSocket(socket(AF_INET6, SOCK_DGRAM, 0))
508
- {
509
- }
510
- };
511
-
512
- class TLocalStreamSocket: public TStreamSocket {
513
- public:
514
- TLocalStreamSocket(SOCKET fd)
515
- : TStreamSocket(fd)
516
- {
517
- }
518
-
519
- TLocalStreamSocket()
520
- : TStreamSocket(socket(YAF_LOCAL, SOCK_STREAM, 0))
521
- {
522
- }
523
- };
524
-
525
- class TInetStreamSocket: public TStreamSocket {
526
- public:
527
- TInetStreamSocket(SOCKET fd)
528
- : TStreamSocket(fd)
529
- {
530
- }
531
-
532
- TInetStreamSocket()
533
- : TStreamSocket(socket(AF_INET, SOCK_STREAM, 0))
534
- {
535
- }
536
- };
537
-
538
- class TInet6StreamSocket: public TStreamSocket {
539
- public:
540
- TInet6StreamSocket(SOCKET fd)
541
- : TStreamSocket(fd)
542
- {
543
- }
544
-
545
- TInet6StreamSocket()
546
- : TStreamSocket(socket(AF_INET6, SOCK_STREAM, 0))
547
- {
548
- }
549
- };
550
-
551
- class TStreamSocketInput: public IInputStream {
552
- public:
553
- TStreamSocketInput(TStreamSocket* socket)
554
- : Socket(socket)
555
- {
556
- }
557
- void SetSocket(TStreamSocket* socket) {
558
- Socket = socket;
559
- }
560
-
561
- protected:
562
- TStreamSocket* Socket;
563
-
564
- size_t DoRead(void* buf, size_t len) override {
565
- Y_VERIFY(Socket, "TStreamSocketInput: socket isn't set");
566
- const ssize_t ret = Socket->Recv(buf, len);
567
-
568
- if (ret >= 0) {
569
- return (size_t)ret;
570
- }
571
-
572
- ythrow TSystemError(-(int)ret) << "can not read from socket input stream";
573
- }
574
- };
575
-
576
- class TStreamSocketOutput: public IOutputStream {
577
- public:
578
- TStreamSocketOutput(TStreamSocket* socket)
579
- : Socket(socket)
580
- {
581
- }
582
- void SetSocket(TStreamSocket* socket) {
583
- Socket = socket;
584
- }
585
-
586
- TStreamSocketOutput(TStreamSocketOutput&&) noexcept = default;
587
- TStreamSocketOutput& operator=(TStreamSocketOutput&&) noexcept = default;
588
-
589
- protected:
590
- TStreamSocket* Socket;
591
-
592
- void DoWrite(const void* buf, size_t len) override {
593
- Y_VERIFY(Socket, "TStreamSocketOutput: socket isn't set");
594
-
595
- const char* ptr = (const char*)buf;
596
- while (len) {
597
- const ssize_t ret = Socket->Send(ptr, len);
598
-
599
- if (ret < 0) {
600
- ythrow TSystemError(-(int)ret) << "can not write to socket output stream";
601
- }
602
-
603
- Y_ASSERT((size_t)ret <= len);
604
- len -= (size_t)ret;
605
- ptr += (size_t)ret;
606
- }
607
- }
608
- };