| 22 | } |
| 23 | |
| 24 | static bool ParseInetName(TUdpAddress* pRes, const char* name, int nDefaultPort, EUdpAddressType addressType) { |
| 25 | int nPort = nDefaultPort; |
| 26 | |
| 27 | TString host; |
| 28 | if (name[0] == '[') { |
| 29 | ++name; |
| 30 | const char* nameFin = name; |
| 31 | for (; *nameFin; ++nameFin) { |
| 32 | if (nameFin[0] == ']') |
| 33 | break; |
| 34 | } |
| 35 | host.assign(name, nameFin); |
| 36 | Y_ASSERT(IsValidIPv6(host.c_str())); |
| 37 | name = *nameFin ? nameFin + 1 : nameFin; |
| 38 | if (name[0] == ':') { |
| 39 | char* endPtr = nullptr; |
| 40 | nPort = strtol(name + 1, &endPtr, 10); |
| 41 | if (!endPtr || *endPtr != '\0') |
| 42 | return false; |
| 43 | } |
| 44 | } else { |
| 45 | host = name; |
| 46 | if (!IsValidIPv6(name)) { |
| 47 | size_t nIdx = host.find(':'); |
| 48 | if (nIdx != (size_t)TString::npos) { |
| 49 | const char* pszPort = host.c_str() + nIdx + 1; |
| 50 | char* endPtr = nullptr; |
| 51 | nPort = strtol(pszPort, &endPtr, 10); |
| 52 | if (!endPtr || *endPtr != '\0') |
| 53 | return false; |
| 54 | host.resize(nIdx); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | addrinfo aiHints; |
| 60 | Zero(aiHints); |
| 61 | aiHints.ai_family = AF_UNSPEC; |
| 62 | aiHints.ai_socktype = SOCK_DGRAM; |
| 63 | aiHints.ai_protocol = IPPROTO_UDP; |
| 64 | |
| 65 | // Do not use TMutex here: it has a non-trivial destructor which will be called before |
| 66 | // destruction of current thread, if its TThread declared as global/static variable. |
| 67 | static TAdaptiveLock cs; |
| 68 | TGuard lock(cs); |
| 69 | |
| 70 | addrinfo* aiList = nullptr; |
| 71 | for (int attempt = 0; attempt < 1000; ++attempt) { |
| 72 | int rv = getaddrinfo(host.c_str(), "1313", &aiHints, &aiList); |
| 73 | if (rv == 0) |
| 74 | break; |
| 75 | if (aiList) { |
| 76 | freeaddrinfo(aiList); |
| 77 | } |
| 78 | if (rv != EAI_AGAIN) { |
| 79 | return false; |
| 80 | } |
| 81 | usleep(100 * 1000); |
no test coverage detected