| 448 | } |
| 449 | |
| 450 | int TAbstractSocket::OpenImpl(int port) { |
| 451 | Y_ASSERT(!IsValid()); |
| 452 | const int netPort = port ? htons((u_short)port) : 0; |
| 453 | |
| 454 | #ifdef _freebsd_ |
| 455 | // alternative OS |
| 456 | if (netPort == 0) { |
| 457 | static ui64 pp = GetCycleCount(); |
| 458 | for (int attempt = 0; attempt < 100; ++attempt) { |
| 459 | const int tryPort = htons((pp & 0x3fff) + 0xc000); |
| 460 | ++pp; |
| 461 | if (CreateSocket(tryPort) != 0) { |
| 462 | Y_ASSERT(!IsValid()); |
| 463 | continue; |
| 464 | } |
| 465 | |
| 466 | if (DetectSelfAddress() != 0 || tryPort != SelfAddress.sin6_port) { |
| 467 | // FreeBSD suck! |
| 468 | CloseImpl(); |
| 469 | Y_ASSERT(!IsValid()); |
| 470 | continue; |
| 471 | } |
| 472 | break; |
| 473 | } |
| 474 | if (!IsValid()) { |
| 475 | return -1; |
| 476 | } |
| 477 | } else { |
| 478 | if (CreateSocket(netPort) != 0) { |
| 479 | Y_ASSERT(!IsValid()); |
| 480 | return -1; |
| 481 | } |
| 482 | } |
| 483 | #else |
| 484 | // regular OS |
| 485 | if (CreateSocket(netPort) != 0) { |
| 486 | Y_ASSERT(!IsValid()); |
| 487 | return -1; |
| 488 | } |
| 489 | #endif |
| 490 | |
| 491 | if (IsValid() && DetectSelfAddress() != 0) { |
| 492 | CloseImpl(); |
| 493 | Y_ASSERT(!IsValid()); |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | Y_ASSERT(IsValid()); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | void TAbstractSocket::CloseImpl() { |
| 502 | if (IsValid()) { |
nothing calls this directly
no test coverage detected