UPnP.h
1 /* 2 * Copyright (c) 2013-2024, The PurpleI2P Project 3 * 4 * This file is part of Purple i2pd project and licensed under BSD3 5 * 6 * See full license text in LICENSE file at top of project tree 7 */ 8 9 #ifndef __UPNP_H__ 10 #define __UPNP_H__ 11 12 #ifdef USE_UPNP 13 #include <string> 14 #include <thread> 15 #include <condition_variable> 16 #include <mutex> 17 #include <memory> 18 19 #include <miniupnpc/miniwget.h> 20 #include <miniupnpc/miniupnpc.h> 21 #include <miniupnpc/upnpcommands.h> 22 #include <miniupnpc/upnperrors.h> 23 24 #include <boost/asio.hpp> 25 26 namespace i2p 27 { 28 namespace transport 29 { 30 const int UPNP_RESPONSE_TIMEOUT = 2000; // in milliseconds 31 const int UPNP_PORT_FORWARDING_INTERVAL = 20; // in minutes 32 33 enum 34 { 35 UPNP_IGD_NONE = 0, 36 UPNP_IGD_VALID_CONNECTED = 1, 37 UPNP_IGD_VALID_NOT_CONNECTED = 2, 38 UPNP_IGD_INVALID = 3 39 }; 40 41 class UPnP 42 { 43 public: 44 45 UPnP (); 46 ~UPnP (); 47 void Close (); 48 49 void Start (); 50 void Stop (); 51 52 private: 53 54 void Discover (); 55 int CheckMapping (const char* port, const char* type); 56 void PortMapping (); 57 void TryPortMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address); 58 void CloseMapping (); 59 void CloseMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address); 60 61 void Run (); 62 std::string GetProto (std::shared_ptr<i2p::data::RouterInfo::Address> address); 63 64 private: 65 66 bool m_IsRunning; 67 std::unique_ptr<std::thread> m_Thread; 68 std::condition_variable m_Started; 69 std::mutex m_StartedMutex; 70 boost::asio::io_context m_Service; 71 boost::asio::deadline_timer m_Timer; 72 bool m_upnpUrlsInitialized = false; 73 struct UPNPUrls m_upnpUrls; 74 struct IGDdatas m_upnpData; 75 76 // For miniupnpc 77 struct UPNPDev * m_Devlist = 0; 78 char m_NetworkAddr[64]; 79 char m_externalIPAddress[40]; 80 }; 81 } 82 } 83 84 #else // USE_UPNP 85 namespace i2p { 86 namespace transport { 87 /* class stub */ 88 class UPnP { 89 public: 90 91 UPnP () {}; 92 ~UPnP () {}; 93 void Start () { LogPrint(eLogWarning, "UPnP: this module was disabled at compile-time"); } 94 void Stop () {}; 95 }; 96 } 97 } 98 #endif // USE_UPNP 99 #endif // __UPNP_H__