29 #include <miniupnpc/miniwget.h> 30 #include <miniupnpc/miniupnpc.h> 31 #include <miniupnpc/upnpcommands.h> 44 m_urls = make_shared<UPNPUrls>();
45 m_data = make_shared<IGDdatas>();
49 struct UPNPDev* devlist;
54 memset(m_urls.get(), 0,
sizeof(
struct UPNPUrls));
55 memset(m_data.get(), 0,
sizeof(
struct IGDdatas));
56 #if MINIUPNPC_API_VERSION >= 14 57 devlist = upnpDiscover(2000, NULL, NULL, 0, 0, 2, &upnperror);
59 devlist = upnpDiscover(2000, NULL, NULL, 0, 0, &upnperror);
66 if (strstr (dev->st,
"InternetGatewayDevice"))
73 cnote <<
"UPnP device:" << dev->descURL <<
"[st:" << dev->st <<
"]";
74 #if MINIUPNPC_API_VERSION >= 16 75 int responsecode = 200;
76 descXML = (
char*)miniwget(dev->descURL, &descXMLsize, 0, &responsecode);
77 #elif MINIUPNPC_API_VERSION >= 9 78 descXML = (
char*)miniwget(dev->descURL, &descXMLsize, 0);
80 descXML = (
char*)miniwget(dev->descURL, &descXMLsize);
84 parserootdesc (descXML, descXMLsize, m_data.get());
85 free (descXML); descXML = 0;
86 #if MINIUPNPC_API_VERSION >= 9 87 GetUPNPUrls (m_urls.get(), m_data.get(), dev->descURL, 0);
89 GetUPNPUrls (m_urls.get(), m_data.get(), dev->descURL);
93 freeUPNPDevlist(devlist);
98 cnote <<
"UPnP device not found.";
99 BOOST_THROW_EXCEPTION(NoUPnPDevice());
110 string UPnP::externalIP()
114 if (!UPNP_GetExternalIPAddress(m_urls->controlURL, m_data->first.servicetype, addr))
121 int UPnP::addRedirect(
char const* _addr,
int _port)
126 if (m_urls->controlURL[0] ==
'\0')
128 cwarn <<
"UPnP::addRedirect() called without proper initialisation?";
134 char ext_port_str[16];
135 sprintf(port_str,
"%d", _port);
136 if (!UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, port_str, port_str, _addr,
"ethereum",
"TCP", NULL, NULL))
141 for (
unsigned i = 0; i < 10; ++i)
143 _port = rand() % (32768 - 1024) + 1024;
144 sprintf(ext_port_str,
"%d", _port);
145 if (!UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, ext_port_str, port_str, _addr,
"ethereum",
"TCP", NULL, NULL))
150 if (UPNP_AddPortMapping(m_urls->controlURL, m_data->first.servicetype, port_str, NULL, _addr,
"ethereum",
"TCP", NULL, NULL))
156 UPNP_GetPortMappingNumberOfEntries(m_urls->controlURL, m_data->first.servicetype, &num);
157 for (
unsigned i = 0; i < num; ++i)
167 UPNP_GetGenericPortMappingEntry(m_urls->controlURL, m_data->first.servicetype,
toString(i).c_str(), extPort, intClient, intPort, protocol, desc, enabled, rHost, duration);
168 if (
string(
"ethereum") == desc)
170 m_reg.insert(
atoi(extPort));
171 return atoi(extPort);
174 cerr <<
"ERROR: Mapped port not found." << endl;
179 void UPnP::removeRedirect(
int _port)
185 printf(
"TB : upnp_rem_redir (%d)\n", _port);
186 if (m_urls->controlURL[0] ==
'\0')
188 printf(
"TB : the init was not done !\n");
191 sprintf(port_str,
"%d", _port);
192 UPNP_DeletePortMapping(m_urls->controlURL, m_data->first.servicetype, port_str,
"TCP", NULL);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::hash for asio::adress
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
int atoi(const std::string &str)