blob: 236c0c7fde2a5212e0f7deeaa983e1a91e284ce3 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
4 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
5 * Author: Eden Abitbol <eden.abitbol@savoirfairelinux.com>
6 * Author: Mohamed Chibani <mohamed.chibani@savoirfairelinux.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#pragma once
24
Adrien Béraud33a03ba2023-06-21 11:35:54 -040025#include "mapping.h"
Adrien Bérauddbb06862023-07-08 09:18:39 -040026#include "../ip_utils.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040027
28#include <memory>
29#include <chrono>
30
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040031namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040032class IpAddr;
33}
34
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040035namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040036namespace upnp {
37
38class UPnPContext;
39
40class Controller
41{
42public:
Adrien Béraud25c30c42023-07-05 13:46:54 -040043 Controller(const std::shared_ptr<UPnPContext>& ctx);
Adrien Béraud612b55b2023-05-29 10:42:04 -040044 ~Controller();
45
46 // Set known public address
47 void setPublicAddress(const IpAddr& addr);
48 // Checks if a valid IGD is available.
49 bool isReady() const;
50 // Gets the external ip of the first valid IGD in the list.
51 IpAddr getExternalIP() const;
52
53 // Request port mapping.
54 // Returns a shared pointer on the allocated mapping. The shared
55 // pointer may point to nothing on failure.
56 Mapping::sharedPtr_t reserveMapping(Mapping& map);
57 Mapping::sharedPtr_t reserveMapping(uint16_t port, PortType type);
58
59 // Remove port mapping.
60 void releaseMapping(const Mapping& map);
61 static uint16_t generateRandomPort(PortType);
62
63private:
64 // Adds a mapping locally to the list.
65 void addLocalMap(const Mapping& map);
66 // Removes a mapping from the local list.
67 bool removeLocalMap(const Mapping& map);
68 // Removes all mappings of the given type.
69 void releaseAllMappings();
70
71 std::shared_ptr<UPnPContext> upnpContext_;
72
73 mutable std::mutex mapListMutex_;
74 std::map<Mapping::key_t, Mapping> mappingList_;
75};
76
77} // namespace upnp
78} // namespace jami