blob: 69585392acf34fd82528021921f940def7cedb63 [file] [log] [blame]
/*
* Copyright (C) 2004-2023 Savoir-faire Linux Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "mapping.h"
#include "../ip_utils.h"
#include <memory>
#include <chrono>
namespace dhtnet {
class IpAddr;
}
namespace dhtnet {
namespace upnp {
class UPnPContext;
class Controller : std::enable_shared_from_this<Controller>
{
public:
Controller(const std::shared_ptr<UPnPContext>& ctx);
~Controller();
// Set known public address
void setPublicAddress(const IpAddr& addr);
// Checks if a valid IGD is available.
bool isReady() const;
// Gets the external ip of the first valid IGD in the list.
IpAddr getExternalIP() const;
// Request port mapping.
// Returns a shared pointer on the allocated mapping. The shared
// pointer may point to nothing on failure.
Mapping::sharedPtr_t reserveMapping(Mapping& map);
Mapping::sharedPtr_t reserveMapping(uint16_t port, PortType type);
// Remove port mapping.
void releaseMapping(const Mapping& map);
static uint16_t generateRandomPort(PortType);
private:
// Adds a mapping locally to the list.
void addLocalMap(const Mapping& map);
// Removes a mapping from the local list.
bool removeLocalMap(const Mapping& map);
// Removes all mappings of the given type.
void releaseAllMappings();
std::shared_ptr<UPnPContext> upnpContext_;
mutable std::mutex mapListMutex_;
std::map<Mapping::key_t, Mapping> mappingList_;
};
} // namespace upnp
} // namespace dhtnet