Debian Codegraph @

src/bnetd/file.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998,1999 Ross Combs ([email protected])
3 * Copyright (C) 1999 Rob Crittenden ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
20 #include "file.h"cxx . . $ `<file>/src/bnetd/file.h`/
21
22 #include <cstring>
23 #include <cerrno>
24 #include <string>
25
26 #ifdef HAVE_SYS_TYPES_Hcxx . . $ `build/config.h:16:9`!
27 # include <sys/types.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_Hcxx . . $ `build/config.h:21:9`!
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_UNISTD_Hcxx . . $ `build/config.h:7:9`!
33 # include <unistd.h>
34 #endif
35
36 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
37 #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/
38 #include "common/bnettime.h"cxx . . $ `<file>/src/common/bnettime.h`/
39 #include "common/packet.h"cxx . . $ `<file>/src/common/packet.h`/
40 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
41 #include "common/bn_type.h"cxx . . $ `<file>/src/common/bn_type.h`/
42 #include "common/tag.h"cxx . . $ `<file>/src/common/tag.h`/
43
44 #include "prefs.h"cxx . . $ `<file>/src/bnetd/prefs.h`/
45 #include "connection.h"cxx . . $ `<file>/src/bnetd/connection.h`/
46 #include "i18n.h"cxx . . $ `<file>/src/bnetd/i18n.h`/
47 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
48
49 namespace pvpgncxx . . $ pvpgn/
50 {
51
52 namespace bnetdcxx . . $ pvpgn/bnetd/
53 {
54
55 static stdcxx . . $ std/::stringcxx . . $ std/string# file_get_infocxx . . $ pvpgn/bnetd/file_get_info(34f3218e0dee5245).(t_connection * c, char const * rawname, unsigned int * len, bn_longcxx . . $ pvpgn/bn_long# * modtime);
56
57 /* Requested files aliases */
58 const char * requestfilescxx . . $ pvpgn/bnetd/requestfiles.[] = {
59 "termsofservice-", "termsofservice.txt",
60 "newaccount-", "newaccount.txt", // used in warcraft 3 after agree with TOS
61 "chathelp-war3-", "chathelp-war3.txt",
62 "matchmaking-war3-", "matchmaking-war3.dat", // FIXME: (HarpyWar) this file should be in files, not in i18n
63 "tos_", "termsofservice.txt",
64 "tos-unicode_", "termsofservice.txt",
65 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`! };
66
67
68 static stdcxx . . $ std/::stringcxx . . $ std/string# file_find_localizedcxx . . $ pvpgn/bnetd/file_find_localized(8ab6eb58fbbf30b8).(t_connection * c, const char *rawname)
69 {
70 const char ** pattern, **alias;
71 size_t rawnamelen = stdcxx . . $ std/::strlen(rawname);
72
73 for (pattern = requestfilescxx . . $ pvpgn/bnetd/requestfiles., alias = requestfilescxx . . $ pvpgn/bnetd/requestfiles. + 1; *pattern; pattern += 2, alias += 2)
74 {
75 // Check if there is an alias file available for this kind of file
76 if (!stdcxx . . $ std/::strncmp(rawname, *pattern, stdcxx . . $ std/::strlen(*pattern)))
77 {
78 t_gamelangcxx . . $ pvpgn/t_gamelang# gamelang;
79 if (gamelang = conn_get_gamelang_localizedcxx . . $ pvpgn/bnetd/conn_get_gamelang_localized(c9be5e844f0af884).(c))
80 return i18n_filenamecxx . . $ pvpgn/bnetd/i18n_filename(caf975da75400ef4).(*alias, gamelang);
81
82 // FIXME: when file is transferring by bnftp protofol client doesn't provide a language
83 // but we can extract it from the filename, so do it in next code
84
85 // if there is no country tag in the file (just in case to prevent crash from invalid filename)
86 if ((strlen(*pattern) + 4) > rawnamelen)
87 return stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#();
88
89 // get language tag from the file name (like "termsofservice-ruRU.txt")
90 // (it used in War3)
91 char langstr[5];
92 strncpy(langstr, rawname + stdcxx . . $ std/::strlen(*pattern), 4);
93 langstr[4] = 0;
94 gamelang = tag_str_to_uint(langstr);
95
96 // if language is invalid then try find it by country (like "tos_USA.txt")
97 // (it used in D1, SC, War2)
98 bool found;
99 language_find_by_tagcxx . . $ pvpgn/bnetd/language_find_by_tag(4ad94360b135822).(gamelang, found);
100 if (!found)
101 {
102 strncpy(langstr, rawname + stdcxx . . $ std/::strlen(*pattern), 3);
103 langstr[3] = 0;
104 gamelang = gamelang_get_by_countrycxx . . $ pvpgn/bnetd/gamelang_get_by_country(c61c63047ce273de).(langstr);
105 }
106 return i18n_filenamecxx . . $ pvpgn/bnetd/i18n_filename(caf975da75400ef4).(*alias, gamelang);
107 }
108 }
109
110 // if not found return source file
111 return stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#();
112 }
113
114 static stdcxx . . $ std/::stringcxx . . $ std/string# file_get_infocxx . . $ pvpgn/bnetd/file_get_info(34f3218e0dee5245).(t_connection * c, char const * rawname, unsigned int * len, bn_longcxx . . $ pvpgn/bn_long# * modtime)
115 {
116 if (!rawname)
117 {
118 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("NULL rawname");
119 }
120
121 if (!len)
122 {
123 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("NULL len");
124 }
125
126 if (!modtime)
127 {
128 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("NULL modtime");
129 }
130
131 if (stdcxx . . $ std/::strchr(rawname, '/') || stdcxx . . $ std/::strchr(rawname, '\\'))
132 {
133 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(fmtcxx . . $ fmt/::formatcxx . . $ fmt/format(9dfaebe7421e19ae).("got rawname containing '/' or '\\' \"{}\""cxx . . $ fmt/BasicCStringRef#BasicCStringRef(88376428bb603c08)., rawname));
134 }
135
136 struct stat sfile;
137 stdcxx . . $ std/::stringcxx . . $ std/string# filename = file_find_localizedcxx . . $ pvpgn/bnetd/file_find_localized(8ab6eb58fbbf30b8).(c, rawname);
138
139 // if localized file not found in "i18n"
140 if (filename.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).() || stat(filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &sfile) != 0)
141 {
142 // try find it in "files"
143 filename =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). fmtcxx . . $ fmt/::formatcxx . . $ fmt/format(9dfaebe7421e19ae).("{}/{}"cxx . . $ fmt/BasicCStringRef#BasicCStringRef(88376428bb603c08)., prefs_get_filedircxx . . $ pvpgn/bnetd/prefs_get_filedir(33a1f659e94feb42).(), rawname).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).();
144 if (stat(filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &sfile) < 0) /* try again */
145 {
146 /* FIXME: check for lower-case version of filename */
147 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(fmtcxx . . $ fmt/::formatcxx . . $ fmt/format(9dfaebe7421e19ae).("file {} not found"cxx . . $ fmt/BasicCStringRef#BasicCStringRef(88376428bb603c08)., filename));
148 }
149 }
150
151 *len = (unsigned int)sfile.st_size;
152 t_bnettimecxx . . $ pvpgn/t_bnettime# bt = time_to_bnettimecxx . . $ pvpgn/time_to_bnettime(f96d2c3d06d968c9).(sfile.st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`!, 0);
153 bnettime_to_bn_longcxx . . $ pvpgn/bnettime_to_bn_long(5cee0b6c516bd57b).(bt, modtime);
154
155 return filenamecxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
156 }
157
158
159 extern int file_to_mod_timecxx . . $ pvpgn/bnetd/file_to_mod_time(58ff0399a75a534d).(t_connection * c, char const * rawname, bn_longcxx . . $ pvpgn/bn_long# * modtime)
160 {
161 if (!rawname)
162 {
163 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL rawname");
164 return -1;
165 }
166
167 if (!modtime)
168 {
169 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL modtime");
170 return -1;
171 }
172
173 try
174 {
175 unsigned int len = 0;
176 file_get_infocxx . . $ pvpgn/bnetd/file_get_info(34f3218e0dee5245).(c, rawname, &len, modtime);
177 }
178 catch (const stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#& e)
179 {
180 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "{}", e.whatcxx . . $ std/runtime_error#what(ba32b354807b2b81).());
181 return -1;
182 }
183
184 return 0;
185 }
186
187
188 /* Send a file. If the file doesn't exist we still need to respond
189 * to the file request. This will set filelen to 0 and send the server
190 * reply message and the client will be happy and not hang.
191 */
192 extern int file_sendcxx . . $ pvpgn/bnetd/file_send(bcadffea2cd735b4).(t_connection * c, char const * rawname, unsigned int adid, unsigned int etag, unsigned int startoffset, int need_header)
193 {
194 t_packetcxx . . $ pvpgn/t_packet# * rpacket;
195 stdcxx . . $ std/::FILEcxx . . $ std/FILE# * fp;
196
197 int nbytes;
198
199 if (!c)
200 {
201 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL connection");
202 return -1;
203 }
204 if (!rawname)
205 {
206 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL rawname");
207 return -1;
208 }
209
210 if (!(rpacket = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_filecxx . . $ pvpgn/packet_class_file.)))
211 {
212 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not create file packet");
213 return -1;
214 }
215 packet_set_sizecxx . . $ pvpgn/packet_set_size(a12d8724e4c46208).(rpacket, sizeof(t_server_file_replycxx . . $ pvpgn/t_server_file_reply#));
216 packet_set_typecxx . . $ pvpgn/packet_set_type(a12d8724e4c46208).(rpacket, SERVER_FILE_REPLYcxx . . $ `src/common/file_protocol.h:130:9`!);
217
218 unsigned int filelen = 0;
219 stdcxx . . $ std/::stringcxx . . $ std/string# filenamecxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
220 try
221 {
222 filename =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). file_get_infocxx . . $ pvpgn/bnetd/file_get_info(34f3218e0dee5245).(c, rawname, &filelen, &rpacket->ucxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#u..server_file_replycxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#$anonymous_type_3#server_file_reply..timestampcxx . . $ pvpgn/$anonymous_type_d2bac4e24f922c3_5#timestamp.);
223 }
224 catch (const stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#& e)
225 {
226 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "{}", e.whatcxx . . $ std/runtime_error#what(ba32b354807b2b81).());
227 return -1;
228 }
229
230 if (!(fp = stdcxx . . $ std/::fopencxx . . $ std/fopen(82ec0787cd37e0a3).(filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), "rb")))
231 {
232 /* FIXME: check for lower-case version of filename */
233 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "stat() succeeded yet could not open file \"{}\" for reading (std::fopen: {})", filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
234 filelen = 0;
235 }
236
237 if (fp)
238 {
239 if (startoffset < filelen) {
240 stdcxx . . $ std/::fseekcxx . . $ std/fseek(543fae30d249f6b1).(fp, startoffset, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
241 }
242 else {
243 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_warncxx . . $ pvpgn/eventlog_level_warn., __FUNCTION__, "[{}] startoffset is beyond end of file ({}>{})", conn_get_socketcxx . . $ pvpgn/bnetd/conn_get_socket(b5160de3e9803af4).(c), startoffset, filelen);
244 /* Keep the real filesize. Battle.net does it the same way ... */
245 stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(fp);
246 fp = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
247 }
248 }
249
250 if (need_header)
251 {
252 /* send the header from the server with the rawname and length. */
253 bn_int_set(&rpacket->ucxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#u..server_file_replycxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#$anonymous_type_3#server_file_reply..filelencxx . . $ pvpgn/$anonymous_type_d2bac4e24f922c3_5#filelen., filelen);
254 bn_int_set(&rpacket->ucxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#u..server_file_replycxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#$anonymous_type_3#server_file_reply..adidcxx . . $ pvpgn/$anonymous_type_d2bac4e24f922c3_5#adid., adid);
255 bn_int_set(&rpacket->ucxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#u..server_file_replycxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#$anonymous_type_3#server_file_reply..extensiontagcxx . . $ pvpgn/$anonymous_type_d2bac4e24f922c3_5#extensiontag., etag);
256 /* rpacket->u.server_file_reply.timestamp is set above */
257 packet_append_stringcxx . . $ pvpgn/packet_append_string(41801851180b7cbb).(rpacket, rawname);
258 conn_push_outqueuecxx . . $ pvpgn/bnetd/conn_push_outqueue(212cf84cd7032d49).(c, rpacket);
259 }
260 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(rpacket);
261
262 /* Now send the data. Since it may be longer than a packet; we use
263 * the raw packet class.
264 */
265 if (!fp)
266 {
267 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_warncxx . . $ pvpgn/eventlog_level_warn., __FUNCTION__, "[{}] sending no data for file \"{}\" (\"{}\")", conn_get_socketcxx . . $ pvpgn/bnetd/conn_get_socket(b5160de3e9803af4).(c), rawname, filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).());
268 return -1;
269 }
270
271 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info., __FUNCTION__, "[{}] sending file \"{}\" (\"{}\") of length {}", conn_get_socketcxx . . $ pvpgn/bnetd/conn_get_socket(b5160de3e9803af4).(c), rawname, filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), filelen);
272 for (;;)
273 {
274 if (!(rpacket = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_rawcxx . . $ pvpgn/packet_class_raw.)))
275 {
276 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not create raw packet");
277 if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(fp) < 0)
278 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not close file \"{}\" after reading (std::fclose: {})", filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
279 return -1;
280 }
281 if ((nbytes = stdcxx . . $ std/::freadcxx . . $ std/fread(738fc8ac1d711e58).(packet_get_raw_data_buildcxx . . $ pvpgn/packet_get_raw_data_build(ed324b086ed1b636).(rpacket, 0), 1, MAX_PACKET_SIZEcxx . . $ MAX_PACKET_SIZE., fp))<(int)MAX_PACKET_SIZEcxx . . $ MAX_PACKET_SIZE.)
282 {
283 if (nbytes>0) /* send out last portion */
284 {
285 packet_set_sizecxx . . $ pvpgn/packet_set_size(a12d8724e4c46208).(rpacket, nbytes);
286 conn_push_outqueuecxx . . $ pvpgn/bnetd/conn_push_outqueue(212cf84cd7032d49).(c, rpacket);
287 }
288 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(rpacket);
289 if (stdcxx . . $ std/::ferrorcxx . . $ std/ferror(7a688d7338757b98).(fp))
290 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "read failed before EOF on file \"{}\" (std::fread: {})", rawname, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
291 break;
292 }
293 packet_set_sizecxx . . $ pvpgn/packet_set_size(a12d8724e4c46208).(rpacket, nbytes);
294 conn_push_outqueuecxx . . $ pvpgn/bnetd/conn_push_outqueue(212cf84cd7032d49).(c, rpacket);
295 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(rpacket);
296 }
297
298 if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(fp) < 0)
299 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not close file \"{}\" after reading (std::fclose: {})", rawname, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
300 return 0;
301 }
302
303 }
304
305 }