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"
20 #include "file.h"
21
22 #include <cstring>
23 #include <cerrno>
24 #include <string>
25
26 #ifdef HAVE_SYS_TYPES_H
27 # include <sys/types.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35
36 #include "common/eventlog.h"
37 #include "common/xalloc.h"
38 #include "common/bnettime.h"
39 #include "common/packet.h"
40 #include "common/util.h"
41 #include "common/bn_type.h"
42 #include "common/tag.h"
43
44 #include "prefs.h"
45 #include "connection.h"
46 #include "i18n.h"
47 #include "common/setup_after.h"
48
49 namespace pvpgn
50 {
51
52 namespace bnetd
53 {
54
55 static std::string file_get_info(t_connection * c, char const * rawname, unsigned int * len, bn_long * modtime);
56
57 /* Requested files aliases */
58 const char * 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 NULL, NULL };
66
67
68 static std::string file_find_localized(t_connection * c, const char *rawname)
69 {
70 const char ** pattern, **alias;
71 size_t rawnamelen = std::strlen(rawname);
72
73 for (pattern = requestfiles, alias = requestfiles + 1; *pattern; pattern += 2, alias += 2)
74 {
75 // Check if there is an alias file available for this kind of file
76 if (!std::strncmp(rawname, *pattern, std::strlen(*pattern)))
77 {
78 t_gamelang gamelang;
79 if (gamelang = conn_get_gamelang_localized(c))
80 return i18n_filename(*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 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 + 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_tag(gamelang, found);
100 if (!found)
101 {
102 strncpy(langstr, rawname + std::strlen(*pattern), 3);
103 langstr[3] = 0;
104 gamelang = gamelang_get_by_country(langstr);
105 }
106 return i18n_filename(*alias, gamelang);
107 }
108 }
109
110 // if not found return source file
111 return std::string();
112 }
113
114 static std::string file_get_info(t_connection * c, char const * rawname, unsigned int * len, bn_long * modtime)
115 {
116 if (!rawname)
117 {
118 throw std::runtime_error("NULL rawname");
119 }
120
121 if (!len)
122 {
123 throw std::runtime_error("NULL len");
124 }
125
126 if (!modtime)
127 {
128 throw std::runtime_error("NULL modtime");
129 }
130
131 if (std::strchr(rawname, '/') || std::strchr(rawname, '\\'))
132 {
133 throw std::runtime_error(fmt::format("got rawname containing '/' or '\\' \"{}\"", rawname));
134 }
135
136 struct stat sfile;
137 std::string filename = file_find_localized(c, rawname);
138
139 // if localized file not found in "i18n"
140 if (filename.empty() || stat(filename.c_str(), &sfile) != 0)
141 {
142 // try find it in "files"
143 filename = fmt::format("{}/{}", prefs_get_filedir(), rawname).c_str();
144 if (stat(filename.c_str(), &sfile) < 0) /* try again */
145 {
146 /* FIXME: check for lower-case version of filename */
147 throw std::runtime_error(fmt::format("file {} not found", filename));
148 }
149 }
150
151 *len = (unsigned int)sfile.st_size;
152 t_bnettime bt = time_to_bnettime(sfile.st_mtime, 0);
153 bnettime_to_bn_long(bt, modtime);
154
155 return filename;
156 }
157
158
159 extern int file_to_mod_time(t_connection * c, char const * rawname, bn_long * modtime)
160 {
161 if (!rawname)
162 {
163 eventlog(eventlog_level_error, __FUNCTION__, "got NULL rawname");
164 return -1;
165 }
166
167 if (!modtime)
168 {
169 eventlog(eventlog_level_error, __FUNCTION__, "got NULL modtime");
170 return -1;
171 }
172
173 try
174 {
175 unsigned int len = 0;
176 file_get_info(c, rawname, &len, modtime);
177 }
178 catch (const std::runtime_error& e)
179 {
180 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
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_send(t_connection * c, char const * rawname, unsigned int adid, unsigned int etag, unsigned int startoffset, int need_header)
193 {
194 t_packet * rpacket;
195 std::FILE * fp;
196
197 int nbytes;
198
199 if (!c)
200 {
201 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
202 return -1;
203 }
204 if (!rawname)
205 {
206 eventlog(eventlog_level_error, __FUNCTION__, "got NULL rawname");
207 return -1;
208 }
209
210 if (!(rpacket = packet_create(packet_class_file)))
211 {
212 eventlog(eventlog_level_error, __FUNCTION__, "could not create file packet");
213 return -1;
214 }
215 packet_set_size(rpacket, sizeof(t_server_file_reply));
216 packet_set_type(rpacket, SERVER_FILE_REPLY);
217
218 unsigned int filelen = 0;
219 std::string filename;
220 try
221 {
222 filename = file_get_info(c, rawname, &filelen, &rpacket->u.server_file_reply.timestamp);
223 }
224 catch (const std::runtime_error& e)
225 {
226 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
227 return -1;
228 }
229
230 if (!(fp = std::fopen(filename.c_str(), "rb")))
231 {
232 /* FIXME: check for lower-case version of filename */
233 eventlog(eventlog_level_error, __FUNCTION__, "stat() succeeded yet could not open file \"{}\" for reading (std::fopen: {})", filename.c_str(), std::strerror(errno));
234 filelen = 0;
235 }
236
237 if (fp)
238 {
239 if (startoffset < filelen) {
240 std::fseek(fp, startoffset, SEEK_SET);
241 }
242 else {
243 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] startoffset is beyond end of file ({}>{})", conn_get_socket(c), startoffset, filelen);
244 /* Keep the real filesize. Battle.net does it the same way ... */
245 std::fclose(fp);
246 fp = NULL;
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->u.server_file_reply.filelen, filelen);
254 bn_int_set(&rpacket->u.server_file_reply.adid, adid);
255 bn_int_set(&rpacket->u.server_file_reply.extensiontag, etag);
256 /* rpacket->u.server_file_reply.timestamp is set above */
257 packet_append_string(rpacket, rawname);
258 conn_push_outqueue(c, rpacket);
259 }
260 packet_del_ref(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 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] sending no data for file \"{}\" (\"{}\")", conn_get_socket(c), rawname, filename.c_str());
268 return -1;
269 }
270
271 eventlog(eventlog_level_info, __FUNCTION__, "[{}] sending file \"{}\" (\"{}\") of length {}", conn_get_socket(c), rawname, filename.c_str(), filelen);
272 for (;;)
273 {
274 if (!(rpacket = packet_create(packet_class_raw)))
275 {
276 eventlog(eventlog_level_error, __FUNCTION__, "could not create raw packet");
277 if (std::fclose(fp) < 0)
278 eventlog(eventlog_level_error, __FUNCTION__, "could not close file \"{}\" after reading (std::fclose: {})", filename.c_str(), std::strerror(errno));
279 return -1;
280 }
281 if ((nbytes = std::fread(packet_get_raw_data_build(rpacket, 0), 1, MAX_PACKET_SIZE, fp))<(int)MAX_PACKET_SIZE)
282 {
283 if (nbytes>0) /* send out last portion */
284 {
285 packet_set_size(rpacket, nbytes);
286 conn_push_outqueue(c, rpacket);
287 }
288 packet_del_ref(rpacket);
289 if (std::ferror(fp))
290 eventlog(eventlog_level_error, __FUNCTION__, "read failed before EOF on file \"{}\" (std::fread: {})", rawname, std::strerror(errno));
291 break;
292 }
293 packet_set_size(rpacket, nbytes);
294 conn_push_outqueue(c, rpacket);
295 packet_del_ref(rpacket);
296 }
297
298 if (std::fclose(fp) < 0)
299 eventlog(eventlog_level_error, __FUNCTION__, "could not close file \"{}\" after reading (std::fclose: {})", rawname, std::strerror(errno));
300 return 0;
301 }
302
303 }
304
305 }