Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Change-Id: Id59bce284de0a8a683c62d5eb4388fe228f4d950
  • Loading branch information
ovari committed Dec 22, 2024
1 parent 67e7147 commit 385acfc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
20 changes: 10 additions & 10 deletions src/upnp/protocol/natpmp/nat_pmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ NatPmp::initNatPmp()

// Local address must be valid.
if (not getHostAddress() or getHostAddress().isLoopback()) {
if (logger_) logger_->warn("NAT-PMP: Does not have a valid local address!");
if (logger_) logger_->warn("NAT-PMP: No valid local address!");
return;
}

Expand Down Expand Up @@ -94,7 +94,7 @@ NatPmp::initNatPmp()
}

if (err < 0) {
if (logger_) logger_->error("NAT-PMP: Unable to initialize libnatpmp -> {}", getNatPmpErrorStr(err));
if (logger_) logger_->error("NAT-PMP: Unable to initialize libnatpmp {}", getNatPmpErrorStr(err));
return;
}

Expand All @@ -105,8 +105,8 @@ NatPmp::initNatPmp()

// Set the local (gateway) address.
igd_->setLocalIp(igdAddr);
// NAT-PMP protocol does not have UID, but we will set generic
// one debugging purposes.
// NAT-PMP protocol does not have UID, setting genetic one
// for debugging purposes.
igd_->setUID("NAT-PMP Gateway");

// Search and set the public address.
Expand Down Expand Up @@ -226,7 +226,7 @@ bool
NatPmp::isReady() const
{
if (observer_ == nullptr) {
if (logger_) logger_->error("NAT-PMP: the observer is not set!");
if (logger_) logger_->error("NAT-PMP: The observer is not set!");
return false;
}

Expand Down Expand Up @@ -408,7 +408,7 @@ NatPmp::sendMappingRequest(Mapping& mapping, uint32_t& lifetime)
if (response.type == NATPMP_RESPTYPE_PUBLICADDRESS) {
responseValid = false;
if (logger_)
logger_->error("NAT-PMP: unexpected response to request for mapping {} from IGD {} [type: PUBLICADDRESS]",
logger_->error("NAT-PMP: Unexpected response to request for mapping {} from IGD {} [type: PUBLICADDRESS]",
mapping.toString(),
igd_->toString());
} else {
Expand All @@ -425,7 +425,7 @@ NatPmp::sendMappingRequest(Mapping& mapping, uint32_t& lifetime)
response.pnu.newportmapping.privateport != expectedPrivatePort) {
responseValid = false;
if (logger_)
logger_->error("NAT-PMP: unexpected response to request for mapping {} from IGD {}"
logger_->error("NAT-PMP: Unexpected response to request for mapping {} from IGD {}"
" [type={}, resultcode={}, privateport={}, mappedpublicport={}, lifetime={}]",
mapping.toString(),
igd_->toString(),
Expand All @@ -451,8 +451,8 @@ NatPmp::sendMappingRequest(Mapping& mapping, uint32_t& lifetime)
// We requested the creation/renewal of a mapping and didn't get an error, so at this point
// newExternalPort and newLifetime should both be nonzero.
if (newExternalPort == 0 || newLifetime == 0) {
if (logger_) logger_->error("NAT-PMP: response from IGD {} to request for mapping {}"
" indicates that the mapping was deleted [external port: {}, lifetime: {}]",
if (logger_) logger_->error("NAT-PMP: Response from IGD {} to request for mapping {} "
"indicates that the mapping was deleted [external port: {}, lifetime: {}]",
igd_->toString(),
mapping.toString(),
newExternalPort,
Expand Down Expand Up @@ -553,7 +553,7 @@ NatPmp::getIgdPublicAddress()
int err = sendpublicaddressrequest(&natpmpHdl_);

if (err < 0) {
if (logger_) logger_->error("NAT-PMP: send public address request on IGD {} failed with error: {}",
if (logger_) logger_->error("NAT-PMP: Send public address request on IGD {} failed with error: {}",
igd_->toString(),
getNatPmpErrorStr(err));

Expand Down
38 changes: 19 additions & 19 deletions src/upnp/upnp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ UPnPContext::createIoContext(const std::shared_ptr<asio::io_context>& ctx, const
if (ctx) {
return ctx;
} else {
if (logger) logger->debug("UPnPContext: starting dedicated io_context thread");
if (logger) logger->debug("UPnPContext: Starting dedicated io_context thread");
auto ioCtx = std::make_shared<asio::io_context>();
ioContextRunner_ = std::make_unique<std::thread>([ioCtx, l=logger]() {
try {
Expand Down Expand Up @@ -120,7 +120,7 @@ UPnPContext::shutdown()
if (cv.wait_for(lk, std::chrono::seconds(30), [this] { return shutdownComplete_; })) {
if (logger_) logger_->debug("Shutdown completed");
} else {
if (logger_) logger_->error("Shutdown timed out");
if (logger_) logger_->error("Shutdown timed-out");
shutdownTimedOut_ = true;
}
// NOTE: It's important to unlock mappingMutex_ here, otherwise we get a
Expand All @@ -129,11 +129,11 @@ UPnPContext::shutdown()
lk.unlock();

if (ioContextRunner_) {
if (logger_) logger_->debug("UPnPContext: stopping io_context thread {}", fmt::ptr(this));
if (logger_) logger_->debug("UPnPContext: Stopping io_context thread {}", fmt::ptr(this));
ctx->stop();
ioContextRunner_->join();
ioContextRunner_.reset();
if (logger_) logger_->debug("UPnPContext: stopping io_context thread - finished {}", fmt::ptr(this));
if (logger_) logger_->debug("UPnPContext: Stopping io_context thread - finished {}", fmt::ptr(this));
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ UPnPContext::_connectivityChanged(const asio::error_code& ec)

auto hostAddr = ip_utils::getLocalAddr(AF_INET);

if (logger_) logger_->debug("Connectivity change check: host address {}", hostAddr.toString());
if (logger_) logger_->debug("Connectivity change check: Host address {}", hostAddr.toString());

auto restartUpnp = false;

Expand Down Expand Up @@ -417,7 +417,7 @@ UPnPContext::registerController(void* controller)
{
std::lock_guard lock(mappingMutex_);
if (shutdownComplete_) {
if (logger_) logger_->warn("UPnPContext already shut down");
if (logger_) logger_->warn("UPnPContext already shutdown");
return;
}
auto ret = controllerList_.emplace(controller);
Expand Down Expand Up @@ -488,7 +488,7 @@ UPnPContext::getAvailablePortNumber(PortType type)
}

// Very unlikely to get here.
if (logger_) logger_->error("Unable to find an available port after {} attempts", MAX_REQUEST_RETRIES);
if (logger_) logger_->error("Unable to find an available port after {} attempt(s)", MAX_REQUEST_RETRIES);
return 0;
}

Expand Down Expand Up @@ -534,7 +534,7 @@ UPnPContext::provisionNewMappings(PortType type, int portCount)
registerMapping(map);
} else {
// Very unlikely to get here!
if (logger_) logger_->error("Unable to provision port: no available port number");
if (logger_) logger_->error("Unable to provision port: No available port number");
return;
}
}
Expand All @@ -543,7 +543,7 @@ UPnPContext::provisionNewMappings(PortType type, int portCount)
void
UPnPContext::deleteUnneededMappings(PortType type, int portCount)
{
if (logger_) logger_->debug("Remove {:d} unneeded mapping of type [{}]", portCount, Mapping::getTypeStr(type));
if (logger_) logger_->debug("Remove {:d} unneeded mapping(s) of type [{}]", portCount, Mapping::getTypeStr(type));

std::lock_guard lock(mappingMutex_);
auto& mappingList = getMappingList(type);
Expand All @@ -563,7 +563,7 @@ UPnPContext::deleteUnneededMappings(PortType type, int portCount)
it = mappingList.erase(it);
portCount--;
} else if (map->getState() != MappingState::OPEN) {
// If this methods is called, it means there are more open
// If this method is called, it means there are more open
// mappings than required. So, all mappings in a state other
// than "OPEN" state (typically in in-progress state) will
// be deleted as well.
Expand Down Expand Up @@ -609,7 +609,7 @@ UPnPContext::updateCurrentIgd()
currentIgd_->getUID(),
currentIgd_->toString());
} else {
if (logger_) logger_->warn("Unable to update current IGD: no valid IGD was found");
if (logger_) logger_->warn("Unable to update current IGD: No valid IGD was found");
}
}

Expand Down Expand Up @@ -680,7 +680,7 @@ UPnPContext::renewMappings()

const auto& igd = getCurrentIgd();
if (!igd) {
if (logger_) logger_->debug("Unable to renew mappings: no valid IGD available");
if (logger_) logger_->debug("Unable to renew mappings: No valid IGD available");
return;
}

Expand Down Expand Up @@ -712,15 +712,15 @@ UPnPContext::renewMappings()
}

if (!toRenew.empty()) {
if (logger_) logger_->debug("Sending renewal requests for {} mappings", toRenew.size());
if (logger_) logger_->debug("Sending renewal requests for {} mapping(s)", toRenew.size());
}
for (const auto& map : toRenew) {
const auto& protocol = protocolList_.at(map->getIgd()->getProtocol());
protocol->requestMappingRenew(*map);
}
if (toRenewLaterCount > 0) {
nextRenewalTime += MAPPING_RENEWAL_THROTTLING_DELAY;
if (logger_) logger_->debug("{} mappings didn't need to be renewed (next renewal scheduled for {:%Y-%m-%d %H:%M:%S})",
if (logger_) logger_->debug("{} mapping(s) didn't need to be renewed (next renewal scheduled for {:%Y-%m-%d %H:%M:%S})",
toRenewLaterCount,
fmt::localtime(sys_clock::to_time_t(nextRenewalTime)));
mappingRenewalTimer_.expires_at(nextRenewalTime);
Expand Down Expand Up @@ -943,7 +943,7 @@ UPnPContext::processPendingRequests()
const auto& mappingList = getMappingList(type);
for (const auto& [_, map] : mappingList) {
if (map->getState() == MappingState::PENDING) {
if (logger_) logger_->debug("Will attempt to send a request for pending mapping {}",
if (logger_) logger_->debug("Attempting to send a request for pending mapping {}",
map->toString());
requestsList.emplace_back(map);
}
Expand Down Expand Up @@ -994,8 +994,8 @@ UPnPContext::onIgdUpdated(const std::shared_ptr<IGD>& igd, UpnpIgdEvent event)
{
std::lock_guard lock(publicAddressMutex_);
if (knownPublicAddress_ and igd->getPublicIp() != knownPublicAddress_) {
if (logger_) logger_->warn("[{}] IGD external address [{}] does not match known public address [{}]."
" The mapped addresses might not be reachable",
if (logger_) logger_->warn("[{}] IGD external address [{}] does not match known public address [{}]. "
"The mapped addresses might not be reachable",
protocolName,
igd->getPublicIp().toString(),
knownPublicAddress_.toString());
Expand Down Expand Up @@ -1068,7 +1068,7 @@ UPnPContext::onMappingRenewed(const std::shared_ptr<IGD>& igd, const Mapping& ma
return;
}
if (!mapPtr->isValid() || mapPtr->getState() != MappingState::OPEN) {
if (logger_) logger_->warn("Renewed mapping {} from IGD {} [{}] is in unexpected state",
if (logger_) logger_->warn("Renewed mapping {} from IGD {} [{}] is in an unexpected state",
mapPtr->toString(),
igd->toString(),
mapPtr->getProtocolName());
Expand Down Expand Up @@ -1165,7 +1165,7 @@ UPnPContext::registerMapping(Mapping& map, bool available)
if (map.getExternalPort() == 0) {
auto port = getAvailablePortNumber(map.getType());
if (port == 0) {
if (logger_) logger_->error("Unable to register mapping: no available port number");
if (logger_) logger_->error("Unable to register mapping: No available port number");
return mapPtr;
}
map.setExternalPort(port);
Expand Down

0 comments on commit 385acfc

Please sign in to comment.