1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "stdinc.h"
#include "MappingManager.h"
#include "ConnectionManager.h"
#include "SearchManager.h"
#include "LogManager.h"
#include "format.h"
#include "version.h"
#include "ConnectivityManager.h"
#ifdef USE_MINIUPNP
#include "extra/upnpc.h"
#endif
#ifdef WITH_DHT
#include "dht/DHT.h"
#endif
namespace dcpp {
void MappingManager::addImplementation(UPnP* impl) {
impls.push_back(std::unique_ptr<UPnP>(impl));
}
bool MappingManager::open() {
if(opened)
return false;
if(impls.empty()) {
log(_("No UPnP implementation available"));
return false;
}
if(portMapping.exchange(true) == true) {
log(_("Another UPnP port mapping attempt is in progress..."));
return false;
}
start();
return true;
}
void MappingManager::close() {
for(auto &i : impls) {
close(*i);
}
opened = false;
}
int MappingManager::run() {
// cache these
const string
conn_port = ConnectionManager::getInstance()->getPort(),
secure_port = ConnectionManager::getInstance()->getSecurePort(),
search_port = SearchManager::getInstance()->getPort();
#ifdef WITH_DHT
const string dht_port = dht::DHT::getInstance()->getPort();
#endif
for(auto &i : impls) {
UPnP& impl = *i;
close(impl);
if(!impl.init()){
log(str(F_("Failed to initialize the %1% interface") % impl.getName()));
continue;
}
if(!conn_port.empty() && !impl.open(conn_port, UPnP::PROTOCOL_TCP, str(F_(APPNAME " Transfer Port (%1% TCP)") % conn_port))){
log(str(F_("The %1% interface has failed to map the %2% %3% port") % impl.getName() % "TCP" % conn_port));
continue;
}
if(!secure_port.empty() && !impl.open(secure_port, UPnP::PROTOCOL_TCP, str(F_(APPNAME " Encrypted Transfer Port (%1% TCP)") % secure_port))){
log(str(F_("The %1% interface has failed to map the %2% %3% port") % impl.getName() % "TLS" % secure_port));
continue;
}
if(!search_port.empty() && !impl.open(search_port, UPnP::PROTOCOL_UDP, str(F_(APPNAME " Search Port (%1% UDP)") % search_port))){
log(str(F_("The %1% interface has failed to map the %2% %3% port") % impl.getName() % "UDP" % search_port));
continue;
}
#ifdef WITH_DHT
if(!dht_port.empty() && !impl.open(dht_port, UPnP::PROTOCOL_UDP, str(F_(APPNAME " DHT Port (%1% UDP)") % dht_port))){
log(str(F_("The %1% interface has failed to map the %2% %3% port") % impl.getName() % "UDP" % dht_port));
continue;
}
#endif
opened = true;
#ifdef WITH_DHT
if(!dht_port.empty())
log(str(F_("Successfully created port mappings (TCP: %1%, UDP: %2%, TLS: %3%, DHT: %4%), mapped using the %5% interface") % conn_port % search_port % secure_port % dht_port % impl.getName()));
else
log(str(F_("Successfully created port mappings (TCP: %1%, UDP: %2%, TLS: %3%), mapped using the %4% interface") % conn_port % search_port % secure_port % impl.getName()));
#else
log(str(F_("Successfully created port mappings (TCP: %1%, UDP: %2%, TLS: %3%), mapped using the %4% interface") % conn_port % search_port % secure_port % impl.getName()));
#endif
if(!BOOLSETTING(NO_IP_OVERRIDE)) {
// now lets configure the external IP (connect to me) address
string ExternalIP = impl.getExternalIP();
if(!ExternalIP.empty()) {
// woohoo, we got the external IP from the UPnP framework
SettingsManager::getInstance()->set(SettingsManager::EXTERNAL_IP, ExternalIP);
} else {
//:-( Looks like we have to rely on the user setting the external IP manually
// no need to do cleanup here because the mappings work
log(_("Failed to get external IP"));
}
}
ConnectivityManager::getInstance()->mappingFinished(true);
break;
}
if(!opened) {
log(_("Failed to create port mappings"));
ConnectivityManager::getInstance()->mappingFinished(false);
}
portMapping = false;
return 0;
}
void MappingManager::close(UPnP& impl) {
if(impl.hasRules()) {
log(impl.close() ? str(F_("Successfully removed port mappings with the %1% interface") % impl.getName()) :
str(F_("Failed to remove port mappings with the %1% interface") % impl.getName()));
}
}
void MappingManager::log(const string& message) {
ConnectivityManager::getInstance()->log(str(F_("UPnP: %1%") % message));
}
#ifdef USE_MINIUPNP
void MappingManager::runMiniUPnP() {
addImplementation(new UPnPc());
}
#endif
} // namespace dcpp
|