]> git.lizzy.rs Git - minetest.git/blob - src/client/clientmedia.cpp
Rename “Minimal development test” to “Development Test” (#9928)
[minetest.git] / src / client / clientmedia.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "clientmedia.h"
21 #include "httpfetch.h"
22 #include "client.h"
23 #include "filecache.h"
24 #include "filesys.h"
25 #include "log.h"
26 #include "porting.h"
27 #include "settings.h"
28 #include "util/hex.h"
29 #include "util/serialize.h"
30 #include "util/sha1.h"
31 #include "util/string.h"
32
33 static std::string getMediaCacheDir()
34 {
35         return porting::path_cache + DIR_DELIM + "media";
36 }
37
38 /*
39         ClientMediaDownloader
40 */
41
42 ClientMediaDownloader::ClientMediaDownloader():
43         m_media_cache(getMediaCacheDir()),
44         m_httpfetch_caller(HTTPFETCH_DISCARD)
45 {
46 }
47
48 ClientMediaDownloader::~ClientMediaDownloader()
49 {
50         if (m_httpfetch_caller != HTTPFETCH_DISCARD)
51                 httpfetch_caller_free(m_httpfetch_caller);
52
53         for (auto &file_it : m_files)
54                 delete file_it.second;
55
56         for (auto &remote : m_remotes)
57                 delete remote;
58 }
59
60 void ClientMediaDownloader::addFile(const std::string &name, const std::string &sha1)
61 {
62         assert(!m_initial_step_done); // pre-condition
63
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 << "\""
68                                 << std::endl;
69                 return;
70         }
71
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 << "\""
76                                 << std::endl;
77                 return;
78         }
79
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 << "\""
84                                 << std::endl;
85                 return;
86         }
87
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));
93 }
94
95 void ClientMediaDownloader::addRemoteServer(const std::string &baseurl)
96 {
97         assert(!m_initial_step_done);   // pre-condition
98
99         #ifdef USE_CURL
100
101         if (g_settings->getBool("enable_remote_media_server")) {
102                 infostream << "Client: Adding remote server \""
103                         << baseurl << "\" for media download" << std::endl;
104
105                 RemoteServerStatus *remote = new RemoteServerStatus();
106                 remote->baseurl = baseurl;
107                 remote->active_count = 0;
108                 m_remotes.push_back(remote);
109         }
110
111         #else
112
113         infostream << "Client: Ignoring remote server \""
114                 << baseurl << "\" because cURL support is not compiled in"
115                 << std::endl;
116
117         #endif
118 }
119
120 void ClientMediaDownloader::step(Client *client)
121 {
122         if (!m_initial_step_done) {
123                 initialStep(client);
124                 m_initial_step_done = true;
125         }
126
127         // Remote media: check for completion of fetches
128         if (m_httpfetch_active) {
129                 bool fetched_something = false;
130                 HTTPFetchResult fetch_result;
131
132                 while (httpfetch_async_get(m_httpfetch_caller, fetch_result)) {
133                         m_httpfetch_active--;
134                         fetched_something = true;
135
136                         // Is this a hashset (index.mth) or a media file?
137                         if (fetch_result.request_id < m_remotes.size())
138                                 remoteHashSetReceived(fetch_result);
139                         else
140                                 remoteMediaReceived(fetch_result, client);
141                 }
142
143                 if (fetched_something)
144                         startRemoteMediaTransfers();
145
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;
155                         }
156                         startConventionalTransfers(client);
157                 }
158         }
159 }
160
161 void ClientMediaDownloader::initialStep(Client *client)
162 {
163         // Check media cache
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;
169
170                 std::ostringstream tmp_os(std::ios_base::binary);
171                 bool found_in_cache = m_media_cache.load(hex_encode(sha1), tmp_os);
172
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);
177                         if (success) {
178                                 filestatus->received = true;
179                                 m_uncached_count--;
180                         }
181                 }
182         }
183
184         assert(m_uncached_received_count == 0);
185
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());
189                 if (!did) {
190                         errorstream << "Client: "
191                                 << "Could not create media cache directory: "
192                                 << getMediaCacheDir()
193                                 << std::endl;
194                 }
195         }
196
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);
204         }
205         else {
206                 // Otherwise start off by requesting each server's sha1 set
207
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");
211
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);
227
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();
231
232                 // minor fixme: this loop ignores m_httpfetch_active_limit
233
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.
239
240                 for (u32 i = 0; i < m_remotes.size(); ++i) {
241                         assert(m_httpfetch_next_id == i);
242
243                         RemoteServerStatus *remote = m_remotes[i];
244                         actionstream << "Client: Contacting remote server \""
245                                 << remote->baseurl << "\"" << std::endl;
246
247                         HTTPFetchRequest fetch_request;
248                         fetch_request.url =
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");
257
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://") +
264                                 addr + ":" +
265                                 std::to_string(client->getServerAddress().getPort()));
266
267                         httpfetch_async(fetch_request);
268
269                         m_httpfetch_active++;
270                         m_httpfetch_next_id++;
271                         m_outstanding_hash_sets++;
272                 }
273         }
274 }
275
276 void ClientMediaDownloader::remoteHashSetReceived(
277                 const HTTPFetchResult &fetch_result)
278 {
279         u32 remote_id = fetch_result.request_id;
280         assert(remote_id < m_remotes.size());
281         RemoteServerStatus *remote = m_remotes[remote_id];
282
283         m_outstanding_hash_sets--;
284
285         if (fetch_result.succeeded) {
286                 try {
287                         // Server sent a list of file hashes that are
288                         // available on it, try to parse the list
289
290                         std::set<std::string> sha1_set;
291                         deSerializeHashSet(fetch_result.data, sha1_set);
292
293                         // Parsing succeeded: For every file that is
294                         // available on this server, add this server
295                         // to the available_remotes array
296
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);
303                         }
304                 }
305                 catch (SerializationError &e) {
306                         infostream << "Client: Remote server \""
307                                 << remote->baseurl << "\" sent invalid hash set: "
308                                 << e.what() << std::endl;
309                 }
310         }
311 }
312
313 void ClientMediaDownloader::remoteMediaReceived(
314                 const HTTPFetchResult &fetch_result,
315                 Client *client)
316 {
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
321
322         std::string name;
323         {
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());
327                 name = it->second;
328                 m_remote_file_transfers.erase(it);
329         }
330
331         sanity_check(m_files.count(name) != 0);
332
333         FileStatus *filestatus = m_files[name];
334         sanity_check(!filestatus->received);
335         sanity_check(filestatus->current_remote >= 0);
336
337         RemoteServerStatus *remote = m_remotes[filestatus->current_remote];
338
339         filestatus->current_remote = -1;
340         remote->active_count--;
341
342         // If fetch succeeded, try to load media file
343
344         if (fetch_result.succeeded) {
345                 bool success = checkAndLoad(name, filestatus->sha1,
346                                 fetch_result.data, false, client);
347                 if (success) {
348                         filestatus->received = true;
349                         assert(m_uncached_received_count < m_uncached_count);
350                         m_uncached_received_count++;
351                 }
352         }
353 }
354
355 s32 ClientMediaDownloader::selectRemoteServer(FileStatus *filestatus)
356 {
357         // Pre-conditions
358         assert(filestatus != NULL);
359         assert(!filestatus->received);
360         assert(filestatus->current_remote < 0);
361
362         if (filestatus->available_remotes.empty())
363                 return -1;
364
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
368
369         s32 best = 0;
370         s32 best_remote_id = filestatus->available_remotes[best];
371         s32 best_active_count = m_remotes[best_remote_id]->active_count;
372
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) {
377                         best = i;
378                         best_remote_id = remote_id;
379                         best_active_count = active_count;
380                 }
381         }
382
383         filestatus->available_remotes.erase(
384                         filestatus->available_remotes.begin() + best);
385
386         return best_remote_id;
387
388 }
389
390 void ClientMediaDownloader::startRemoteMediaTransfers()
391 {
392         bool changing_name_bound = true;
393
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) {
397
398                 // Abort if active fetch limit is exceeded
399                 if (m_httpfetch_active >= m_httpfetch_active_limit)
400                         break;
401
402                 const std::string &name = files_iter->first;
403                 FileStatus *filestatus = files_iter->second;
404
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];
413
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;
420
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 =
427                                         m_httpfetch_timeout;
428                                 httpfetch_async(fetch_request);
429
430                                 m_remote_file_transfers.insert(std::make_pair(
431                                                         m_httpfetch_next_id,
432                                                         name));
433
434                                 filestatus->current_remote = remote_id;
435                                 remote->active_count++;
436                                 m_httpfetch_active++;
437                                 m_httpfetch_next_id++;
438                         }
439                 }
440
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)
448                                 m_name_bound = name;
449                 }
450                 else
451                         changing_name_bound = false;
452         }
453
454 }
455
456 void ClientMediaDownloader::startConventionalTransfers(Client *client)
457 {
458         assert(m_httpfetch_active == 0);        // pre-condition
459
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);
467                 }
468                 assert((s32) file_requests.size() ==
469                                 m_uncached_count - m_uncached_received_count);
470                 client->request_media(file_requests);
471         }
472 }
473
474 void ClientMediaDownloader::conventionalTransferDone(
475                 const std::string &name,
476                 const std::string &data,
477                 Client *client)
478 {
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 << "\""
485                         << std::endl;
486                 return;
487         }
488         FileStatus *filestatus = file_iter->second;
489         assert(filestatus != NULL);
490
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 << "\""
495                         << std::endl;
496                 return;
497         }
498
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++;
505
506         // Check that received file matches announced checksum
507         // If so, load it
508         checkAndLoad(name, filestatus->sha1, data, false, client);
509 }
510
511 bool ClientMediaDownloader::checkAndLoad(
512                 const std::string &name, const std::string &sha1,
513                 const std::string &data, bool is_from_cache, Client *client)
514 {
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);
518
519         // Compute actual checksum of data
520         std::string data_sha1;
521         {
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);
527         }
528
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
536                         << std::endl;
537                 return false;
538         }
539
540         // Checksum is ok, try loading the file
541         bool success = client->loadMedia(data, name);
542         if (!success) {
543                 infostream << "Client: "
544                         << "Failed to load " << cached_or_received << " media: "
545                         << sha1_hex << " \"" << name << "\""
546                         << std::endl;
547                 return false;
548         }
549
550         verbosestream << "Client: "
551                 << "Loaded " << cached_or_received << " media: "
552                 << sha1_hex << " \"" << name << "\""
553                 << std::endl;
554
555         // Update cache (unless we just loaded the file from the cache)
556         if (!is_from_cache)
557                 m_media_cache.update(sha1_hex, data);
558
559         return true;
560 }
561
562
563 /*
564         Minetest Hashset File Format
565
566         All values are stored in big-endian byte order.
567         [u32] signature: 'MTHS'
568         [u16] version: 1
569         For each hash in set:
570                 [u8*20] SHA1 hash
571
572         Version changes:
573         1 - Initial version
574 */
575
576 std::string ClientMediaDownloader::serializeRequiredHashSet()
577 {
578         std::ostringstream os(std::ios::binary);
579
580         writeU32(os, MTHASHSET_FILE_SIGNATURE); // signature
581         writeU16(os, 1);                        // version
582
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;
591                 }
592         }
593
594         return os.str();
595 }
596
597 void ClientMediaDownloader::deSerializeHashSet(const std::string &data,
598                 std::set<std::string> &result)
599 {
600         if (data.size() < 6 || data.size() % 20 != 6) {
601                 throw SerializationError(
602                                 "ClientMediaDownloader::deSerializeHashSet: "
603                                 "invalid hash set file size");
604         }
605
606         const u8 *data_cstr = (const u8*) data.c_str();
607
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");
613         }
614
615         u16 version = readU16(&data_cstr[4]);
616         if (version != 1) {
617                 throw SerializationError(
618                                 "ClientMediaDownloader::deSerializeHashSet: "
619                                 "unsupported hash set file version");
620         }
621
622         for (u32 pos = 6; pos < data.size(); pos += 20) {
623                 result.insert(data.substr(pos, 20));
624         }
625 }