3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "clientmedia.h"
21 #include "httpfetch.h"
23 #include "filecache.h"
29 #include "util/serialize.h"
30 #include "util/sha1.h"
31 #include "util/string.h"
33 static std::string getMediaCacheDir()
35 return porting::path_cache + DIR_DELIM + "media";
42 ClientMediaDownloader::ClientMediaDownloader():
43 m_media_cache(getMediaCacheDir()),
44 m_httpfetch_caller(HTTPFETCH_DISCARD)
48 ClientMediaDownloader::~ClientMediaDownloader()
50 if (m_httpfetch_caller != HTTPFETCH_DISCARD)
51 httpfetch_caller_free(m_httpfetch_caller);
53 for (auto &file_it : m_files)
54 delete file_it.second;
56 for (auto &remote : m_remotes)
60 void ClientMediaDownloader::addFile(const std::string &name, const std::string &sha1)
62 assert(!m_initial_step_done); // pre-condition
64 // if name was already announced, ignore the new announcement
65 if (m_files.count(name) != 0) {
66 errorstream << "Client: ignoring duplicate media announcement "
67 << "sent by server: \"" << name << "\""
72 // if name is empty or contains illegal characters, ignore the file
73 if (name.empty() || !string_allowed(name, TEXTURENAME_ALLOWED_CHARS)) {
74 errorstream << "Client: ignoring illegal file name "
75 << "sent by server: \"" << name << "\""
80 // length of sha1 must be exactly 20 (160 bits), else ignore the file
81 if (sha1.size() != 20) {
82 errorstream << "Client: ignoring illegal SHA1 sent by server: "
83 << hex_encode(sha1) << " \"" << name << "\""
88 FileStatus *filestatus = new FileStatus();
89 filestatus->received = false;
90 filestatus->sha1 = sha1;
91 filestatus->current_remote = -1;
92 m_files.insert(std::make_pair(name, filestatus));
95 void ClientMediaDownloader::addRemoteServer(const std::string &baseurl)
97 assert(!m_initial_step_done); // pre-condition
101 if (g_settings->getBool("enable_remote_media_server")) {
102 infostream << "Client: Adding remote server \""
103 << baseurl << "\" for media download" << std::endl;
105 RemoteServerStatus *remote = new RemoteServerStatus();
106 remote->baseurl = baseurl;
107 remote->active_count = 0;
108 m_remotes.push_back(remote);
113 infostream << "Client: Ignoring remote server \""
114 << baseurl << "\" because cURL support is not compiled in"
120 void ClientMediaDownloader::step(Client *client)
122 if (!m_initial_step_done) {
124 m_initial_step_done = true;
127 // Remote media: check for completion of fetches
128 if (m_httpfetch_active) {
129 bool fetched_something = false;
130 HTTPFetchResult fetch_result;
132 while (httpfetch_async_get(m_httpfetch_caller, fetch_result)) {
133 m_httpfetch_active--;
134 fetched_something = true;
136 // Is this a hashset (index.mth) or a media file?
137 if (fetch_result.request_id < m_remotes.size())
138 remoteHashSetReceived(fetch_result);
140 remoteMediaReceived(fetch_result, client);
143 if (fetched_something)
144 startRemoteMediaTransfers();
146 // Did all remote transfers end and no new ones can be started?
147 // If so, request still missing files from the minetest server
148 // (Or report that we have all files.)
149 if (m_httpfetch_active == 0) {
150 if (m_uncached_received_count < m_uncached_count) {
151 infostream << "Client: Failed to remote-fetch "
152 << (m_uncached_count-m_uncached_received_count)
153 << " files. Requesting them"
154 << " the usual way." << std::endl;
156 startConventionalTransfers(client);
161 void ClientMediaDownloader::initialStep(Client *client)
164 m_uncached_count = m_files.size();
165 for (auto &file_it : m_files) {
166 std::string name = file_it.first;
167 FileStatus *filestatus = file_it.second;
168 const std::string &sha1 = filestatus->sha1;
170 std::ostringstream tmp_os(std::ios_base::binary);
171 bool found_in_cache = m_media_cache.load(hex_encode(sha1), tmp_os);
173 // If found in cache, try to load it from there
174 if (found_in_cache) {
175 bool success = checkAndLoad(name, sha1,
176 tmp_os.str(), true, client);
178 filestatus->received = true;
184 assert(m_uncached_received_count == 0);
186 // Create the media cache dir if we are likely to write to it
187 if (m_uncached_count != 0) {
188 bool did = fs::CreateAllDirs(getMediaCacheDir());
190 errorstream << "Client: "
191 << "Could not create media cache directory: "
192 << getMediaCacheDir()
197 // If we found all files in the cache, report this fact to the server.
198 // If the server reported no remote servers, immediately start
199 // conventional transfers. Note: if cURL support is not compiled in,
200 // m_remotes is always empty, so "!USE_CURL" is redundant but may
201 // reduce the size of the compiled code
202 if (!USE_CURL || m_uncached_count == 0 || m_remotes.empty()) {
203 startConventionalTransfers(client);
206 // Otherwise start off by requesting each server's sha1 set
208 // This is the first time we use httpfetch, so alloc a caller ID
209 m_httpfetch_caller = httpfetch_caller_alloc();
210 m_httpfetch_timeout = g_settings->getS32("curl_timeout");
212 // Set the active fetch limit to curl_parallel_limit or 84,
213 // whichever is greater. This gives us some leeway so that
214 // inefficiencies in communicating with the httpfetch thread
215 // don't slow down fetches too much. (We still want some limit
216 // so that when the first remote server returns its hash set,
217 // not all files are requested from that server immediately.)
218 // One such inefficiency is that ClientMediaDownloader::step()
219 // is only called a couple times per second, while httpfetch
220 // might return responses much faster than that.
221 // Note that httpfetch strictly enforces curl_parallel_limit
222 // but at no inter-thread communication cost. This however
223 // doesn't help with the aforementioned inefficiencies.
224 // The signifance of 84 is that it is 2*6*9 in base 13.
225 m_httpfetch_active_limit = g_settings->getS32("curl_parallel_limit");
226 m_httpfetch_active_limit = MYMAX(m_httpfetch_active_limit, 84);
228 // Write a list of hashes that we need. This will be POSTed
229 // to the server using Content-Type: application/octet-stream
230 std::string required_hash_set = serializeRequiredHashSet();
232 // minor fixme: this loop ignores m_httpfetch_active_limit
234 // another minor fixme, unlikely to matter in normal usage:
235 // these index.mth fetches do (however) count against
236 // m_httpfetch_active_limit when starting actual media file
237 // requests, so if there are lots of remote servers that are
238 // not responding, those will stall new media file transfers.
240 for (u32 i = 0; i < m_remotes.size(); ++i) {
241 assert(m_httpfetch_next_id == i);
243 RemoteServerStatus *remote = m_remotes[i];
244 actionstream << "Client: Contacting remote server \""
245 << remote->baseurl << "\"" << std::endl;
247 HTTPFetchRequest fetch_request;
249 remote->baseurl + MTHASHSET_FILE_NAME;
250 fetch_request.caller = m_httpfetch_caller;
251 fetch_request.request_id = m_httpfetch_next_id; // == i
252 fetch_request.timeout = m_httpfetch_timeout;
253 fetch_request.connect_timeout = m_httpfetch_timeout;
254 fetch_request.post_data = required_hash_set;
255 fetch_request.extra_headers.emplace_back(
256 "Content-Type: application/octet-stream");
258 // Encapsulate possible IPv6 plain address in []
259 std::string addr = client->getAddressName();
260 if (addr.find(':', 0) != std::string::npos)
261 addr = '[' + addr + ']';
262 fetch_request.extra_headers.emplace_back(
263 std::string("Referer: minetest://") +
265 std::to_string(client->getServerAddress().getPort()));
267 httpfetch_async(fetch_request);
269 m_httpfetch_active++;
270 m_httpfetch_next_id++;
271 m_outstanding_hash_sets++;
276 void ClientMediaDownloader::remoteHashSetReceived(
277 const HTTPFetchResult &fetch_result)
279 u32 remote_id = fetch_result.request_id;
280 assert(remote_id < m_remotes.size());
281 RemoteServerStatus *remote = m_remotes[remote_id];
283 m_outstanding_hash_sets--;
285 if (fetch_result.succeeded) {
287 // Server sent a list of file hashes that are
288 // available on it, try to parse the list
290 std::set<std::string> sha1_set;
291 deSerializeHashSet(fetch_result.data, sha1_set);
293 // Parsing succeeded: For every file that is
294 // available on this server, add this server
295 // to the available_remotes array
297 for(std::map<std::string, FileStatus*>::iterator
298 it = m_files.upper_bound(m_name_bound);
299 it != m_files.end(); ++it) {
300 FileStatus *f = it->second;
301 if (!f->received && sha1_set.count(f->sha1))
302 f->available_remotes.push_back(remote_id);
305 catch (SerializationError &e) {
306 infostream << "Client: Remote server \""
307 << remote->baseurl << "\" sent invalid hash set: "
308 << e.what() << std::endl;
313 void ClientMediaDownloader::remoteMediaReceived(
314 const HTTPFetchResult &fetch_result,
317 // Some remote server sent us a file.
318 // -> decrement number of active fetches
319 // -> mark file as received if fetch succeeded
320 // -> try to load media
324 std::unordered_map<unsigned long, std::string>::iterator it =
325 m_remote_file_transfers.find(fetch_result.request_id);
326 assert(it != m_remote_file_transfers.end());
328 m_remote_file_transfers.erase(it);
331 sanity_check(m_files.count(name) != 0);
333 FileStatus *filestatus = m_files[name];
334 sanity_check(!filestatus->received);
335 sanity_check(filestatus->current_remote >= 0);
337 RemoteServerStatus *remote = m_remotes[filestatus->current_remote];
339 filestatus->current_remote = -1;
340 remote->active_count--;
342 // If fetch succeeded, try to load media file
344 if (fetch_result.succeeded) {
345 bool success = checkAndLoad(name, filestatus->sha1,
346 fetch_result.data, false, client);
348 filestatus->received = true;
349 assert(m_uncached_received_count < m_uncached_count);
350 m_uncached_received_count++;
355 s32 ClientMediaDownloader::selectRemoteServer(FileStatus *filestatus)
358 assert(filestatus != NULL);
359 assert(!filestatus->received);
360 assert(filestatus->current_remote < 0);
362 if (filestatus->available_remotes.empty())
365 // Of all servers that claim to provide the file (and haven't
366 // been unsuccessfully tried before), find the one with the
367 // smallest number of currently active transfers
370 s32 best_remote_id = filestatus->available_remotes[best];
371 s32 best_active_count = m_remotes[best_remote_id]->active_count;
373 for (u32 i = 1; i < filestatus->available_remotes.size(); ++i) {
374 s32 remote_id = filestatus->available_remotes[i];
375 s32 active_count = m_remotes[remote_id]->active_count;
376 if (active_count < best_active_count) {
378 best_remote_id = remote_id;
379 best_active_count = active_count;
383 filestatus->available_remotes.erase(
384 filestatus->available_remotes.begin() + best);
386 return best_remote_id;
390 void ClientMediaDownloader::startRemoteMediaTransfers()
392 bool changing_name_bound = true;
394 for (std::map<std::string, FileStatus*>::iterator
395 files_iter = m_files.upper_bound(m_name_bound);
396 files_iter != m_files.end(); ++files_iter) {
398 // Abort if active fetch limit is exceeded
399 if (m_httpfetch_active >= m_httpfetch_active_limit)
402 const std::string &name = files_iter->first;
403 FileStatus *filestatus = files_iter->second;
405 if (!filestatus->received && filestatus->current_remote < 0) {
406 // File has not been received yet and is not currently
407 // being transferred. Choose a server for it.
408 s32 remote_id = selectRemoteServer(filestatus);
409 if (remote_id >= 0) {
410 // Found a server, so start fetching
411 RemoteServerStatus *remote =
412 m_remotes[remote_id];
414 std::string url = remote->baseurl +
415 hex_encode(filestatus->sha1);
416 verbosestream << "Client: "
417 << "Requesting remote media file "
418 << "\"" << name << "\" "
419 << "\"" << url << "\"" << std::endl;
421 HTTPFetchRequest fetch_request;
422 fetch_request.url = url;
423 fetch_request.caller = m_httpfetch_caller;
424 fetch_request.request_id = m_httpfetch_next_id;
425 fetch_request.timeout = 0; // no data timeout!
426 fetch_request.connect_timeout =
428 httpfetch_async(fetch_request);
430 m_remote_file_transfers.insert(std::make_pair(
434 filestatus->current_remote = remote_id;
435 remote->active_count++;
436 m_httpfetch_active++;
437 m_httpfetch_next_id++;
441 if (filestatus->received ||
442 (filestatus->current_remote < 0 &&
443 !m_outstanding_hash_sets)) {
444 // If we arrive here, we conclusively know that we
445 // won't fetch this file from a remote server in the
446 // future. So update the name bound if possible.
447 if (changing_name_bound)
451 changing_name_bound = false;
456 void ClientMediaDownloader::startConventionalTransfers(Client *client)
458 assert(m_httpfetch_active == 0); // pre-condition
460 if (m_uncached_received_count != m_uncached_count) {
461 // Some media files have not been received yet, use the
462 // conventional slow method (minetest protocol) to get them
463 std::vector<std::string> file_requests;
464 for (auto &file : m_files) {
465 if (!file.second->received)
466 file_requests.push_back(file.first);
468 assert((s32) file_requests.size() ==
469 m_uncached_count - m_uncached_received_count);
470 client->request_media(file_requests);
474 void ClientMediaDownloader::conventionalTransferDone(
475 const std::string &name,
476 const std::string &data,
479 // Check that file was announced
480 std::map<std::string, FileStatus*>::iterator
481 file_iter = m_files.find(name);
482 if (file_iter == m_files.end()) {
483 errorstream << "Client: server sent media file that was"
484 << "not announced, ignoring it: \"" << name << "\""
488 FileStatus *filestatus = file_iter->second;
489 assert(filestatus != NULL);
491 // Check that file hasn't already been received
492 if (filestatus->received) {
493 errorstream << "Client: server sent media file that we already"
494 << "received, ignoring it: \"" << name << "\""
499 // Mark file as received, regardless of whether loading it works and
500 // whether the checksum matches (because at this point there is no
501 // other server that could send a replacement)
502 filestatus->received = true;
503 assert(m_uncached_received_count < m_uncached_count);
504 m_uncached_received_count++;
506 // Check that received file matches announced checksum
508 checkAndLoad(name, filestatus->sha1, data, false, client);
511 bool ClientMediaDownloader::checkAndLoad(
512 const std::string &name, const std::string &sha1,
513 const std::string &data, bool is_from_cache, Client *client)
515 const char *cached_or_received = is_from_cache ? "cached" : "received";
516 const char *cached_or_received_uc = is_from_cache ? "Cached" : "Received";
517 std::string sha1_hex = hex_encode(sha1);
519 // Compute actual checksum of data
520 std::string data_sha1;
522 SHA1 data_sha1_calculator;
523 data_sha1_calculator.addBytes(data.c_str(), data.size());
524 unsigned char *data_tmpdigest = data_sha1_calculator.getDigest();
525 data_sha1.assign((char*) data_tmpdigest, 20);
526 free(data_tmpdigest);
529 // Check that received file matches announced checksum
530 if (data_sha1 != sha1) {
531 std::string data_sha1_hex = hex_encode(data_sha1);
532 infostream << "Client: "
533 << cached_or_received_uc << " media file "
534 << sha1_hex << " \"" << name << "\" "
535 << "mismatches actual checksum " << data_sha1_hex
540 // Checksum is ok, try loading the file
541 bool success = client->loadMedia(data, name);
543 infostream << "Client: "
544 << "Failed to load " << cached_or_received << " media: "
545 << sha1_hex << " \"" << name << "\""
550 verbosestream << "Client: "
551 << "Loaded " << cached_or_received << " media: "
552 << sha1_hex << " \"" << name << "\""
555 // Update cache (unless we just loaded the file from the cache)
557 m_media_cache.update(sha1_hex, data);
564 Minetest Hashset File Format
566 All values are stored in big-endian byte order.
567 [u32] signature: 'MTHS'
569 For each hash in set:
576 std::string ClientMediaDownloader::serializeRequiredHashSet()
578 std::ostringstream os(std::ios::binary);
580 writeU32(os, MTHASHSET_FILE_SIGNATURE); // signature
581 writeU16(os, 1); // version
583 // Write list of hashes of files that have not been
584 // received (found in cache) yet
585 for (std::map<std::string, FileStatus*>::iterator
586 it = m_files.begin();
587 it != m_files.end(); ++it) {
588 if (!it->second->received) {
589 FATAL_ERROR_IF(it->second->sha1.size() != 20, "Invalid SHA1 size");
590 os << it->second->sha1;
597 void ClientMediaDownloader::deSerializeHashSet(const std::string &data,
598 std::set<std::string> &result)
600 if (data.size() < 6 || data.size() % 20 != 6) {
601 throw SerializationError(
602 "ClientMediaDownloader::deSerializeHashSet: "
603 "invalid hash set file size");
606 const u8 *data_cstr = (const u8*) data.c_str();
608 u32 signature = readU32(&data_cstr[0]);
609 if (signature != MTHASHSET_FILE_SIGNATURE) {
610 throw SerializationError(
611 "ClientMediaDownloader::deSerializeHashSet: "
612 "invalid hash set file signature");
615 u16 version = readU16(&data_cstr[4]);
617 throw SerializationError(
618 "ClientMediaDownloader::deSerializeHashSet: "
619 "unsupported hash set file version");
622 for (u32 pos = 6; pos < data.size(); pos += 20) {
623 result.insert(data.substr(pos, 20));