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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* Copyright (C) 2001-2006 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdinc.h"
#include "DCPlusPlus.h"
#include "SearchManager.h"
#include "UploadManager.h"
#include "ClientManager.h"
#include "ShareManager.h"
#include "ResourceManager.h"
SearchResult::SearchResult(Types aType, int64_t aSize, const string& aFile, const TTHValue& aTTH) :
file(aFile), user(ClientManager::getInstance()->getMe()), size(aSize), type(aType), slots(SETTING(SLOTS)),
freeSlots(UploadManager::getInstance()->getFreeSlots()),
tth(aTTH), ref(1) { }
string SearchResult::toSR(const Client& c) const {
// File: "$SR %s %s%c%s %d/%d%c%s (%s)|"
// Directory: "$SR %s %s %d/%d%c%s (%s)|"
string tmp;
tmp.reserve(128);
tmp.append("$SR ", 4);
tmp.append(Text::fromUtf8(c.getMyNick(), c.getEncoding()));
tmp.append(1, ' ');
string acpFile = Text::fromUtf8(file, c.getEncoding());
if(type == TYPE_FILE) {
tmp.append(acpFile);
tmp.append(1, '\x05');
tmp.append(Util::toString(size));
} else {
tmp.append(acpFile, 0, acpFile.length() - 1);
}
tmp.append(1, ' ');
tmp.append(Util::toString(freeSlots));
tmp.append(1, '/');
tmp.append(Util::toString(slots));
tmp.append(1, '\x05');
tmp.append("TTH:" + getTTH().toBase32());
tmp.append(" (", 2);
tmp.append(c.getIpPort());
tmp.append(")|", 2);
return tmp;
}
AdcCommand SearchResult::toRES(char type) const {
AdcCommand cmd(AdcCommand::CMD_RES, type);
cmd.addParam("SI", Util::toString(size));
cmd.addParam("SL", Util::toString(freeSlots));
cmd.addParam("FN", Util::toAdcFile(file));
cmd.addParam("TR", getTTH().toBase32());
return cmd;
}
void SearchManager::search(const string& aName, int64_t aSize, TypeModes aTypeMode /* = TYPE_ANY */, SizeModes aSizeMode /* = SIZE_ATLEAST */, const string& aToken /* = Util::emptyString */) {
if(okToSearch()) {
ClientManager::getInstance()->search(aSizeMode, aSize, aTypeMode, aName, aToken);
lastSearch = GET_TICK();
}
}
void SearchManager::search(StringList& who, const string& aName, int64_t aSize /* = 0 */, TypeModes aTypeMode /* = TYPE_ANY */, SizeModes aSizeMode /* = SIZE_ATLEAST */, const string& aToken /* = Util::emptyString */) {
if(okToSearch()) {
ClientManager::getInstance()->search(who, aSizeMode, aSize, aTypeMode, aName, aToken);
lastSearch = GET_TICK();
}
}
string SearchResult::getFileName() const {
if(getType() == TYPE_FILE)
return Util::getFileName(getFile());
if(getFile().size() < 2)
return getFile();
string::size_type i = getFile().rfind('\\', getFile().length() - 2);
if(i == string::npos)
return getFile();
return getFile().substr(i + 1);
}
void SearchManager::listen() throw(SocketException) {
disconnect();
socket = new Socket();
socket->create(Socket::TYPE_UDP);
port = socket->bind(static_cast<uint16_t>(SETTING(UDP_PORT)), SETTING(BIND_ADDRESS));
start();
}
void SearchManager::disconnect() throw() {
if(socket != NULL) {
stop = true;
socket->disconnect();
port = 0;
join();
stop = false;
}
}
#define BUFSIZE 8192
int SearchManager::run() {
AutoArray<uint8_t> buf(BUFSIZE);
int len;
while(true) {
string remoteAddr;
try {
while( (len = socket->read((uint8_t*)buf, BUFSIZE, remoteAddr)) != 0) {
onData(buf, len, remoteAddr);
}
} catch(const SocketException& e) {
dcdebug("SearchManager::run Error: %s\n", e.getError().c_str());
}
if(stop) {
return 0;
}
try {
socket->disconnect();
socket->create(Socket::TYPE_UDP);
socket->bind(port, SETTING(BIND_ADDRESS));
} catch(const SocketException& e) {
// Oops, fatal this time...
dcdebug("SearchManager::run Stopped listening: %s\n", e.getError().c_str());
return 1;
}
}
return 0;
}
void SearchManager::onData(const uint8_t* buf, size_t aLen, const string& remoteIp) {
string x((char*)buf, aLen);
if(x.compare(0, 4, "$SR ") == 0) {
string::size_type i, j;
// Directories: $SR <nick><0x20><directory><0x20><free slots>/<total slots><0x05><Hubname><0x20>(<Hubip:port>)
// Files: $SR <nick><0x20><filename><0x05><filesize><0x20><free slots>/<total slots><0x05><Hubname><0x20>(<Hubip:port>)
i = 4;
if( (j = x.find(' ', i)) == string::npos) {
return;
}
string nick = x.substr(i, j-i);
i = j + 1;
// A file has 2 0x05, a directory only one
size_t cnt = count(x.begin() + j, x.end(), 0x05);
SearchResult::Types type = SearchResult::TYPE_FILE;
string file;
int64_t size = 0;
if(cnt == 1) {
// We have a directory...find the first space beyond the first 0x05 from the back
// (dirs might contain spaces as well...clever protocol, eh?)
type = SearchResult::TYPE_DIRECTORY;
// Get past the hubname that might contain spaces
if((j = x.rfind(0x05)) == string::npos) {
return;
}
// Find the end of the directory info
if((j = x.rfind(' ', j-1)) == string::npos) {
return;
}
if(j < i + 1) {
return;
}
file = x.substr(i, j-i) + '\\';
} else if(cnt == 2) {
if( (j = x.find((char)5, i)) == string::npos) {
return;
}
file = x.substr(i, j-i);
i = j + 1;
if( (j = x.find(' ', i)) == string::npos) {
return;
}
size = Util::toInt64(x.substr(i, j-i));
}
i = j + 1;
if( (j = x.find('/', i)) == string::npos) {
return;
}
int freeSlots = Util::toInt(x.substr(i, j-i));
i = j + 1;
if( (j = x.find((char)5, i)) == string::npos) {
return;
}
int slots = Util::toInt(x.substr(i, j-i));
i = j + 1;
if( (j = x.rfind(" (")) == string::npos) {
return;
}
string hubName = x.substr(i, j-i);
i = j + 2;
if( (j = x.rfind(')')) == string::npos) {
return;
}
string hubIpPort = x.substr(i, j-i);
string url = ClientManager::getInstance()->findHub(hubIpPort);
string encoding = ClientManager::getInstance()->findHubEncoding(url);
nick = Text::toUtf8(nick, encoding);
file = Text::toUtf8(file, encoding);
hubName = Text::toUtf8(hubName, encoding);
User::Ptr user = ClientManager::getInstance()->findUser(nick, url);
if(!user) {
// Could happen if hub has multiple URLs / IPs
user = ClientManager::getInstance()->findLegacyUser(nick);
if(!user)
return;
}
string tth;
if(hubName.compare(0, 4, "TTH:") == 0) {
tth = hubName.substr(4);
StringList names = ClientManager::getInstance()->getHubNames(user->getCID());
hubName = names.empty() ? STRING(OFFLINE) : Util::toString(names);
}
if(tth.empty() && type == SearchResult::TYPE_FILE) {
return;
}
SearchResult* sr = new SearchResult(user, type, slots, freeSlots, size,
file, hubName, url, remoteIp, TTHValue(tth), Util::emptyString);
fire(SearchManagerListener::SR(), sr);
sr->decRef();
} else if(x.compare(1, 4, "RES ") == 0 && x[x.length() - 1] == 0x0a) {
AdcCommand c(x.substr(0, x.length()-1));
if(c.getParameters().empty())
return;
string cid = c.getParam(0);
if(cid.size() != 39)
return;
User::Ptr user = ClientManager::getInstance()->findUser(CID(cid));
if(!user)
return;
// This should be handled by AdcCommand really...
c.getParameters().erase(c.getParameters().begin());
onRES(c, user, remoteIp);
} /*else if(x.compare(1, 4, "SCH ") == 0 && x[x.length() - 1] == 0x0a) {
try {
respond(AdcCommand(x.substr(0, x.length()-1)));
} catch(ParseException& ) {
}
}*/ // Needs further DoS investigation
}
void SearchManager::onRES(const AdcCommand& cmd, const User::Ptr& from, const string& remoteIp) {
int freeSlots = -1;
int64_t size = -1;
string file;
string tth;
string token;
for(StringIterC i = cmd.getParameters().begin(); i != cmd.getParameters().end(); ++i) {
const string& str = *i;
if(str.compare(0, 2, "FN") == 0) {
file = Util::toNmdcFile(str.substr(2));
} else if(str.compare(0, 2, "SL") == 0) {
freeSlots = Util::toInt(str.substr(2));
} else if(str.compare(0, 2, "SI") == 0) {
size = Util::toInt64(str.substr(2));
} else if(str.compare(0, 2, "TR") == 0) {
tth = str.substr(2);
} else if(str.compare(0, 2, "TO") == 0) {
token = str.substr(2);
}
}
if(!file.empty() && freeSlots != -1 && size != -1) {
StringList names = ClientManager::getInstance()->getHubNames(from->getCID());
string hubName = names.empty() ? STRING(OFFLINE) : Util::toString(names);
StringList hubs = ClientManager::getInstance()->getHubs(from->getCID());
string hub = hubs.empty() ? STRING(OFFLINE) : Util::toString(hubs);
SearchResult::Types type = (file[file.length() - 1] == '\\' ? SearchResult::TYPE_DIRECTORY : SearchResult::TYPE_FILE);
if(type == SearchResult::TYPE_FILE && tth.empty())
return;
/// @todo Something about the slots
SearchResult* sr = new SearchResult(from, type, 0, freeSlots, size,
file, hubName, hub, remoteIp, TTHValue(tth), token);
fire(SearchManagerListener::SR(), sr);
sr->decRef();
}
}
void SearchManager::respond(const AdcCommand& adc, const CID& from) {
// Filter own searches
if(from == ClientManager::getInstance()->getMe()->getCID())
return;
User::Ptr p = ClientManager::getInstance()->findUser(from);
if(!p)
return;
SearchResult::List results;
ShareManager::getInstance()->search(results, adc.getParameters(), 10);
string token;
adc.getParam("TO", 0, token);
if(results.empty())
return;
for(SearchResult::Iter i = results.begin(); i != results.end(); ++i) {
AdcCommand cmd = (*i)->toRES(AdcCommand::TYPE_UDP);
if(!token.empty())
cmd.addParam("TO", token);
ClientManager::getInstance()->send(cmd, from);
(*i)->decRef();
}
}
string SearchManager::clean(const string& aSearchString) {
static const char* badChars = "$|.[]()-_+";
string::size_type i = aSearchString.find_first_of(badChars);
if(i == string::npos)
return aSearchString;
string tmp = aSearchString;
// Remove all strange characters from the search string
do {
tmp[i] = ' ';
} while ( (i = tmp.find_first_of(badChars, i)) != string::npos);
return tmp;
}
|