blob: 69585392acf34fd82528021921f940def7cedb63 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Adrien Béraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#pragma once
18
Adrien Béraud33a03ba2023-06-21 11:35:54 -040019#include "mapping.h"
Adrien Bérauddbb06862023-07-08 09:18:39 -040020#include "../ip_utils.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040021
22#include <memory>
23#include <chrono>
24
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040025namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040026class IpAddr;
27}
28
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040029namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040030namespace upnp {
31
32class UPnPContext;
33
Adrien Beraud753c8192023-08-17 17:21:24 -040034class Controller : std::enable_shared_from_this<Controller>
Adrien Béraud612b55b2023-05-29 10:42:04 -040035{
36public:
Adrien Béraud25c30c42023-07-05 13:46:54 -040037 Controller(const std::shared_ptr<UPnPContext>& ctx);
Adrien Béraud612b55b2023-05-29 10:42:04 -040038 ~Controller();
39
40 // Set known public address
41 void setPublicAddress(const IpAddr& addr);
42 // Checks if a valid IGD is available.
43 bool isReady() const;
44 // Gets the external ip of the first valid IGD in the list.
45 IpAddr getExternalIP() const;
46
47 // Request port mapping.
48 // Returns a shared pointer on the allocated mapping. The shared
49 // pointer may point to nothing on failure.
50 Mapping::sharedPtr_t reserveMapping(Mapping& map);
51 Mapping::sharedPtr_t reserveMapping(uint16_t port, PortType type);
52
53 // Remove port mapping.
54 void releaseMapping(const Mapping& map);
55 static uint16_t generateRandomPort(PortType);
56
57private:
58 // Adds a mapping locally to the list.
59 void addLocalMap(const Mapping& map);
60 // Removes a mapping from the local list.
61 bool removeLocalMap(const Mapping& map);
62 // Removes all mappings of the given type.
63 void releaseAllMappings();
64
65 std::shared_ptr<UPnPContext> upnpContext_;
66
67 mutable std::mutex mapListMutex_;
68 std::map<Mapping::key_t, Mapping> mappingList_;
69};
70
71} // namespace upnp
Sébastien Blin464bdff2023-07-19 08:02:53 -040072} // namespace dhtnet