2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-04-30 15:15:02 +03:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-01-03 23:38:48 +03:00
|
|
|
#ifndef __UPNP_H__
|
|
|
|
#define __UPNP_H__
|
|
|
|
|
|
|
|
#ifdef USE_UPNP
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2016-07-31 17:22:41 +03:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2016-07-18 21:01:58 +03:00
|
|
|
#include <memory>
|
2015-01-03 23:38:48 +03:00
|
|
|
|
|
|
|
#include <miniupnpc/miniwget.h>
|
|
|
|
#include <miniupnpc/miniupnpc.h>
|
|
|
|
#include <miniupnpc/upnpcommands.h>
|
|
|
|
#include <miniupnpc/upnperrors.h>
|
2014-02-10 00:15:47 +04:00
|
|
|
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2015-05-06 19:19:20 +03:00
|
|
|
namespace transport
|
2015-01-03 23:38:48 +03:00
|
|
|
{
|
2019-09-05 21:58:28 +03:00
|
|
|
const int UPNP_RESPONSE_TIMEOUT = 2000; // in milliseconds
|
2024-04-30 15:15:02 +03:00
|
|
|
const int UPNP_PORT_FORWARDING_INTERVAL = 20; // in minutes
|
|
|
|
|
2019-09-05 21:58:28 +03:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
UPNP_IGD_NONE = 0,
|
|
|
|
UPNP_IGD_VALID_CONNECTED = 1,
|
|
|
|
UPNP_IGD_VALID_NOT_CONNECTED = 2,
|
|
|
|
UPNP_IGD_INVALID = 3
|
|
|
|
};
|
|
|
|
|
2014-02-10 00:15:47 +04:00
|
|
|
class UPnP
|
|
|
|
{
|
2019-09-05 21:58:28 +03:00
|
|
|
public:
|
2015-01-03 23:38:48 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
UPnP ();
|
|
|
|
~UPnP ();
|
|
|
|
void Close ();
|
2015-01-03 23:38:48 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2015-01-03 23:38:48 +03:00
|
|
|
|
2019-09-05 21:58:28 +03:00
|
|
|
private:
|
2016-07-18 21:01:58 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
void Discover ();
|
2022-05-20 19:56:05 +03:00
|
|
|
int CheckMapping (const char* port, const char* type);
|
2020-03-01 13:25:50 +03:00
|
|
|
void PortMapping ();
|
|
|
|
void TryPortMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address);
|
|
|
|
void CloseMapping ();
|
|
|
|
void CloseMapping (std::shared_ptr<i2p::data::RouterInfo::Address> address);
|
2016-07-18 21:01:58 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
void Run ();
|
|
|
|
std::string GetProto (std::shared_ptr<i2p::data::RouterInfo::Address> address);
|
2015-01-03 23:38:48 +03:00
|
|
|
|
2019-09-05 21:58:28 +03:00
|
|
|
private:
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
bool m_IsRunning;
|
|
|
|
std::unique_ptr<std::thread> m_Thread;
|
|
|
|
std::condition_variable m_Started;
|
|
|
|
std::mutex m_StartedMutex;
|
|
|
|
boost::asio::io_service m_Service;
|
|
|
|
boost::asio::deadline_timer m_Timer;
|
|
|
|
bool m_upnpUrlsInitialized = false;
|
|
|
|
struct UPNPUrls m_upnpUrls;
|
|
|
|
struct IGDdatas m_upnpData;
|
|
|
|
|
|
|
|
// For miniupnpc
|
|
|
|
struct UPNPDev * m_Devlist = 0;
|
|
|
|
char m_NetworkAddr[64];
|
|
|
|
char m_externalIPAddress[40];
|
2015-01-03 23:38:48 +03:00
|
|
|
};
|
2014-02-10 00:15:47 +04:00
|
|
|
}
|
2015-01-03 23:38:48 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:56:05 +03:00
|
|
|
#else // USE_UPNP
|
2016-07-12 03:00:00 +03:00
|
|
|
namespace i2p {
|
|
|
|
namespace transport {
|
2019-09-05 21:58:28 +03:00
|
|
|
/* class stub */
|
|
|
|
class UPnP {
|
|
|
|
public:
|
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
UPnP () {};
|
|
|
|
~UPnP () {};
|
|
|
|
void Start () { LogPrint(eLogWarning, "UPnP: this module was disabled at compile-time"); }
|
|
|
|
void Stop () {};
|
2019-09-05 21:58:28 +03:00
|
|
|
};
|
2016-07-12 03:00:00 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 03:00:00 +03:00
|
|
|
#endif // USE_UPNP
|
|
|
|
#endif // __UPNP_H__
|