MCPcopy Create free account
hub / github.com/catboost/catboost / CreateSocket

Method CreateSocket

library/cpp/netliba/socket/socket.cpp:180–239  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

178#endif
179
180 int TAbstractSocket::CreateSocket(int netPort) {
181 if (IsValid()) {
182 Y_ASSERT(0);
183 return 0;
184 }
185 S = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
186 if (S == INVALID_SOCKET) {
187 return -1;
188 }
189 {
190 int flag = 0;
191 Y_ABORT_UNLESS(SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&flag, sizeof(flag)) == 0, "IPV6_V6ONLY failed");
192 }
193 {
194 int flag = 1;
195 Y_ABORT_UNLESS(SetSockOpt(SOL_SOCKET, SO_REUSEADDR, (const char*)&flag, sizeof(flag)) == 0, "SO_REUSEADDR failed");
196 }
197#if defined(_win_)
198 unsigned long dummy = 1;
199 ioctlsocket(S, FIONBIO, &dummy);
200#else
201 Y_ABORT_UNLESS(fcntl(S, F_SETFL, O_NONBLOCK) == 0, "fnctl failed: %s (errno = %d)", LastSystemErrorText(), LastSystemError());
202 Y_ABORT_UNLESS(fcntl(S, F_SETFD, FD_CLOEXEC) == 0, "fnctl failed: %s (errno = %d)", LastSystemErrorText(), LastSystemError());
203 {
204 int flag = 1;
205#ifndef IPV6_RECVPKTINFO /* Darwin platforms require this */
206 Y_ABORT_UNLESS(SetSockOpt(IPPROTO_IPV6, IPV6_PKTINFO, (const char*)&flag, sizeof(flag)) == 0, "IPV6_PKTINFO failed");
207#else
208 Y_ABORT_UNLESS(SetSockOpt(IPPROTO_IPV6, IPV6_RECVPKTINFO, (const char*)&flag, sizeof(flag)) == 0, "IPV6_RECVPKTINFO failed");
209#endif
210 }
211#endif
212
213 Poller.WaitRead(S, nullptr);
214
215 {
216 // bind socket
217 sockaddr_in6 name;
218 Zero(name);
219 name.sin6_family = AF_INET6;
220 name.sin6_addr = in6addr_any;
221 name.sin6_port = netPort;
222 if (bind(S, (sockaddr*)&name, sizeof(name)) != 0) {
223 fprintf(stderr, "netliba_socket could not bind to port %d: %s (errno = %d)\n", InetToHost((ui16)netPort), LastSystemErrorText(), LastSystemError());
224 CloseImpl(); // we call this CloseImpl after Poller initialization
225 return -1;
226 }
227 }
228 //Default behavior is allowing fragmentation (according to netliba v6 behavior)
229 //If we want to sent packet with DF flag we have to use SendMsg()
230 EnableFragmentation();
231
232 {
233 socklen_t sz = sizeof(SendSysSocketSize);
234 if (GetSockOpt(SOL_SOCKET, SO_SNDBUF, &SendSysSocketSize, &sz)) {
235 fprintf(stderr, "Can`t get SO_SNDBUF");
236 }
237 }

Callers

nothing calls this directly

Calls 8

IsValidFunction · 0.85
SetSockOptFunction · 0.85
LastSystemErrorFunction · 0.85
ZeroFunction · 0.85
InetToHostFunction · 0.85
GetSockOptFunction · 0.85
WaitReadMethod · 0.80
LastSystemErrorTextFunction · 0.50

Tested by

no test coverage detected