Adrien BĂ©raud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | #include "upnp_context.h" |
| 26 | #include "ip_utils.h" |
| 27 | |
| 28 | #include <memory> |
| 29 | #include <chrono> |
| 30 | |
| 31 | namespace jami { |
| 32 | class IpAddr; |
| 33 | } |
| 34 | |
| 35 | namespace jami { |
| 36 | namespace upnp { |
| 37 | |
| 38 | class UPnPContext; |
| 39 | |
| 40 | class Controller |
| 41 | { |
| 42 | public: |
| 43 | Controller(); |
| 44 | ~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 | |
| 63 | private: |
| 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 |