diff options
Diffstat (limited to 'methods/connect.cc')
-rw-r--r-- | methods/connect.cc | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/methods/connect.cc b/methods/connect.cc index 57dfb6299..d513a4540 100644 --- a/methods/connect.cc +++ b/methods/connect.cc @@ -45,7 +45,7 @@ /*}}}*/ static std::string LastHost; -static int LastPort = 0; +static std::string LastService; static struct addrinfo *LastHostAddr = 0; static struct addrinfo *LastUsed = 0; @@ -349,17 +349,14 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port, { if (ConnectionAllowed(Service, Host) == false) return ResultState::FATAL_ERROR; - // Convert the port name/number - char ServStr[300]; - if (Port != 0) - snprintf(ServStr,sizeof(ServStr),"%i", Port); - else - snprintf(ServStr,sizeof(ServStr),"%s", Service); + + // Used by getaddrinfo(); prefer port if given, else fallback to service + std::string ServiceNameOrPort = Port != 0 ? std::to_string(Port) : Service; /* We used a cached address record.. Yes this is against the spec but the way we have setup our rotating dns suggests that this is more sensible */ - if (LastHost != Host || LastPort != Port) + if (LastHost != Host || LastService != ServiceNameOrPort) { Owner->Status(_("Connecting to %s"),Host.c_str()); @@ -405,14 +402,14 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port, while (1) { int Res; - if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 || + if ((Res = getaddrinfo(Host.c_str(), ServiceNameOrPort.c_str(), &Hints, &LastHostAddr)) != 0 || LastHostAddr == 0) { if (Res == EAI_NONAME || Res == EAI_SERVICE) { if (DefPort != 0) { - snprintf(ServStr, sizeof(ServStr), "%i", DefPort); + ServiceNameOrPort = std::to_string(DefPort); DefPort = 0; continue; } @@ -431,17 +428,17 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port, } if (Res == EAI_SYSTEM) _error->Errno("getaddrinfo", _("System error resolving '%s:%s'"), - Host.c_str(), ServStr); + Host.c_str(), ServiceNameOrPort.c_str()); else _error->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"), - Host.c_str(), ServStr, Res, gai_strerror(Res)); + Host.c_str(), ServiceNameOrPort.c_str(), Res, gai_strerror(Res)); return ResultState::TRANSIENT_ERROR; } break; } LastHost = Host; - LastPort = Port; + LastService = ServiceNameOrPort; } // When we have an IP rotation stay with the last IP. @@ -469,7 +466,7 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port, return Result; if (_error->PendingError() == true) return ResultState::FATAL_ERROR; - _error->Error(_("Unable to connect to %s:%s:"), Host.c_str(), ServStr); + _error->Error(_("Unable to connect to %s:%s:"), Host.c_str(), ServiceNameOrPort.c_str()); return ResultState::TRANSIENT_ERROR; } /*}}}*/ @@ -486,7 +483,10 @@ ResultState Connect(std::string Host, int Port, const char *Service, if (ConnectionAllowed(Service, Host) == false) return ResultState::FATAL_ERROR; - if(LastHost != Host || LastPort != Port) + // Used by getaddrinfo(); prefer port if given, else fallback to service + std::string ServiceNameOrPort = Port != 0 ? std::to_string(Port) : Service; + + if(LastHost != Host || LastService != ServiceNameOrPort) { SrvRecords.clear(); if (_config->FindB("Acquire::EnableSrvRecords", true) == true) |