| 1 | /* |
| 2 | * Copyright (C) 1999,2001 Ross Combs ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #include <cstdio> |
| 20 | #include <cstdlib> |
| 21 | #include <ctime> |
| 22 | #include <cstring> |
| 23 | #include <cctype> |
| 24 | #ifdef HAVE_SYS_STAT_H |
| 25 | # include <sys/stat.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_TERMIOS_H |
| 28 | # include <termios.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "compat/termios.h" |
| 32 | #include "compat/psock.h" |
| 33 | #include "common/packet.h" |
| 34 | #include "common/field_sizes.h" |
| 35 | #include "common/bnettime.h" |
| 36 | #include "common/hexdump.h" |
| 37 | #include "common/util.h" |
| 38 | #include "common/tag.h" |
| 39 | #include "common/bn_type.h" |
| 40 | #include "common/xalloc.h" |
| 41 | #include "common/file_protocol.h" |
| 42 | #include "common/init_protocol.h" |
| 43 | #ifdef CLIENTDEBUG |
| 44 | # include "common/eventlog.h" |
| 45 | #endif |
| 46 | #include "client.h" |
| 47 | |
| 48 | #ifdef HAVE_ARPA_INET_H |
| 49 | # include <arpa/inet.h> |
| 50 | #endif |
| 51 | #ifdef HAVE_WS2TCPIP_H |
| 52 | # include <Ws2tcpip.h> |
| 53 | #endif |
| 54 | |
| 55 | #include "common/setup_after.h" |
| 56 | |
| 57 | using namespace pvpgn; |
| 58 | using namespace pvpgn::client; |
| 59 | |
| 60 | namespace |
| 61 | { |
| 62 | |
| 63 | void usage(char const * progname) |
| 64 | { |
| 65 | std::fprintf(stderr, "usage: %s [<options>] [<servername> [<TCP portnumber>]]\n", progname); |
| 66 | std::fprintf(stderr, |
| 67 | " -b, --client=SEXP report client as Brood Wars\n" |
| 68 | " -d, --client=DRTL report client as Diablo Retail\n" |
| 69 | " --client=DSHR report client as Diablo Shareware\n" |
| 70 | " -s, --client=STAR report client as Starcraft (default)\n"); |
| 71 | std::fprintf(stderr, |
| 72 | " --client=SSHR report client as Starcraft Shareware\n" |
| 73 | " -w, --client=W2BN report client as Warcraft II BNE\n" |
| 74 | " --client=D2DV report client as Diablo II\n" |
| 75 | " --client=D2XP report client as Diablo II: LoD\n" |
| 76 | " --client=WAR3 report client as Warcraft III\n" |
| 77 | " --client=W3XP report client as Warcraft III: FT\n" |
| 78 | " --hexdump=FILE do hex dump of packets into FILE\n"); |
| 79 | std::fprintf(stderr, |
| 80 | " --arch=IX86 report architecture as Windows (x86)\n" |
| 81 | " --arch=PMAC report architecture as Macintosh\n" |
| 82 | " --arch=XMAC report architecture as Macintosh OSX\n" |
| 83 | ); |
| 84 | std::fprintf(stderr, |
| 85 | " --startoffset=OFFSET force offset to be OFFSET\n" |
| 86 | " --exists=ACTION Ask/Overwrite/Backup/Resume if the file exists\n" |
| 87 | " --file=FILENAME use FILENAME instead of asking\n" |
| 88 | " -h, --help, --usage show this information and exit\n" |
| 89 | " -v, --version print version number and exit\n"); |
| 90 | std::exit(EXIT_FAILURE); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | extern int main(int argc, char * argv[]) |
| 96 | { |
| 97 | int a; |
| 98 | int sd; |
| 99 | struct sockaddr_in saddr; |
| 100 | t_packet * packet; |
| 101 | t_packet * rpacket; |
| 102 | t_packet * fpacket; |
| 103 | char const * clienttag = NULL; |
| 104 | char const * archtag = NULL; |
| 105 | char const * servname = NULL; |
| 106 | unsigned short servport = 0; |
| 107 | char const * hexfile = NULL; |
| 108 | char text[MAX_MESSAGE_LEN]; |
| 109 | char const * reqfile = NULL; |
| 110 | struct hostent * host; |
| 111 | unsigned int commpos; |
| 112 | struct termios in_attr_old; |
| 113 | struct termios in_attr_new; |
| 114 | int changed_in; |
| 115 | unsigned int currsize; |
| 116 | unsigned int filelen; |
| 117 | unsigned int startoffset; |
| 118 | int startoffsetoverride = 0; |
| 119 | #define EXIST_ACTION_UNSPEC -1 |
| 120 | #define EXIST_ACTION_ASK 0 |
| 121 | #define EXIST_ACTION_OVERWRITE 1 |
| 122 | #define EXIST_ACTION_BACKUP 2 |
| 123 | #define EXIST_ACTION_RESUME 3 |
| 124 | int exist_action = EXIST_ACTION_UNSPEC; |
| 125 | struct stat exist_buf; |
| 126 | char const * filename; |
| 127 | std::FILE * fp; |
| 128 | std::FILE * hexstrm = NULL; |
| 129 | int fd_stdin; |
| 130 | t_bnettime bntime; |
| 131 | std::time_t tm; |
| 132 | char timestr[FILE_TIME_MAXLEN]; |
| 133 | unsigned int screen_width, screen_height; |
| 134 | int munged; |
| 135 | unsigned int newproto = 0; |
| 136 | |
| 137 | if (argc < 1 || !argv || !argv[0]) |
| 138 | { |
| 139 | std::fprintf(stderr, "bad arguments\n"); |
| 140 | return EXIT_FAILURE; |
| 141 | } |
| 142 | |
| 143 | for (a = 1; a < argc; a++) |
| 144 | if (servname && std::isdigit((int)argv[a][0]) && a + 1 >= argc) |
| 145 | { |
| 146 | if (str_to_ushort(argv[a], &servport) < 0) |
| 147 | { |
| 148 | std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]); |
| 149 | usage(argv[0]); |
| 150 | } |
| 151 | } |
| 152 | else if (!servname && argv[a][0] != '-' && a + 2 >= argc) |
| 153 | servname = argv[a]; |
| 154 | else if (std::strcmp(argv[a], "-b") == 0 || std::strcmp(argv[a], "--client=SEXP") == 0) |
| 155 | { |
| 156 | if (clienttag) |
| 157 | { |
| 158 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 159 | usage(argv[0]); |
| 160 | } |
| 161 | clienttag = CLIENTTAG_BROODWARS; |
| 162 | } |
| 163 | else if (std::strcmp(argv[a], "-d") == 0 || std::strcmp(argv[a], "--client=DRTL") == 0) |
| 164 | { |
| 165 | if (clienttag) |
| 166 | { |
| 167 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 168 | usage(argv[0]); |
| 169 | } |
| 170 | clienttag = CLIENTTAG_DIABLORTL; |
| 171 | } |
| 172 | else if (std::strcmp(argv[a], "--client=DSHR") == 0) |
| 173 | { |
| 174 | if (clienttag) |
| 175 | { |
| 176 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 177 | usage(argv[0]); |
| 178 | } |
| 179 | clienttag = CLIENTTAG_DIABLOSHR; |
| 180 | } |
| 181 | else if (std::strcmp(argv[a], "-s") == 0 || std::strcmp(argv[a], "--client=STAR") == 0) |
| 182 | { |
| 183 | if (clienttag) |
| 184 | { |
| 185 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 186 | usage(argv[0]); |
| 187 | } |
| 188 | clienttag = CLIENTTAG_STARCRAFT; |
| 189 | } |
| 190 | else if (std::strcmp(argv[a], "--client=SSHR") == 0) |
| 191 | { |
| 192 | if (clienttag) |
| 193 | { |
| 194 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 195 | usage(argv[0]); |
| 196 | } |
| 197 | clienttag = CLIENTTAG_SHAREWARE; |
| 198 | } |
| 199 | else if (std::strcmp(argv[a], "-w") == 0 || std::strcmp(argv[a], "--client=W2BN") == 0) |
| 200 | { |
| 201 | if (clienttag) |
| 202 | { |
| 203 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 204 | usage(argv[0]); |
| 205 | } |
| 206 | clienttag = CLIENTTAG_WARCIIBNE; |
| 207 | } |
| 208 | else if (std::strcmp(argv[a], "--client=D2DV") == 0) |
| 209 | { |
| 210 | if (clienttag) |
| 211 | { |
| 212 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 213 | usage(argv[0]); |
| 214 | } |
| 215 | clienttag = CLIENTTAG_DIABLO2DV; |
| 216 | } |
| 217 | else if (std::strcmp(argv[a], "--client=D2XP") == 0) |
| 218 | { |
| 219 | if (clienttag) |
| 220 | { |
| 221 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 222 | usage(argv[0]); |
| 223 | } |
| 224 | clienttag = CLIENTTAG_DIABLO2XP; |
| 225 | } |
| 226 | else if (std::strcmp(argv[a], "--client=WAR3") == 0) |
| 227 | { |
| 228 | if (clienttag) |
| 229 | { |
| 230 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 231 | usage(argv[0]); |
| 232 | } |
| 233 | clienttag = CLIENTTAG_WARCRAFT3; |
| 234 | newproto = 1; |
| 235 | } |
| 236 | else if (std::strcmp(argv[a], "--client=W3XP") == 0) |
| 237 | { |
| 238 | if (clienttag) |
| 239 | { |
| 240 | std::fprintf(stderr, "%s: client type was already specified as \"%s\"\n", argv[0], clienttag); |
| 241 | usage(argv[0]); |
| 242 | } |
| 243 | clienttag = CLIENTTAG_WAR3XP; |
| 244 | newproto = 1; |
| 245 | } |
| 246 | else if (std::strncmp(argv[a], "--client=", 9) == 0) |
| 247 | { |
| 248 | std::fprintf(stderr, "%s: unknown client tag \"%s\"\n", argv[0], &argv[a][9]); |
| 249 | usage(argv[0]); |
| 250 | } |
| 251 | else if (std::strcmp(argv[a], "--arch=IX86") == 0) |
| 252 | { |
| 253 | if (archtag) |
| 254 | { |
| 255 | std::fprintf(stderr, "%s: architecture type was already specified as \"%s\"\n", argv[0], archtag); |
| 256 | usage(argv[0]); |
| 257 | } |
| 258 | archtag = ARCHTAG_WINX86; |
| 259 | } |
| 260 | else if (std::strcmp(argv[a], "--arch=PMAC") == 0) |
| 261 | { |
| 262 | if (archtag) |
| 263 | { |
| 264 | std::fprintf(stderr, "%s: architecture type was already specified as \"%s\"\n", argv[0], archtag); |
| 265 | usage(argv[0]); |
| 266 | } |
| 267 | archtag = ARCHTAG_MACPPC; |
| 268 | } |
| 269 | else if (std::strcmp(argv[a], "--arch=XMAC") == 0) |
| 270 | { |
| 271 | if (archtag) |
| 272 | { |
| 273 | std::fprintf(stderr, "%s: architecture type was already specified as \"%s\"\n", argv[0], archtag); |
| 274 | usage(argv[0]); |
| 275 | } |
| 276 | archtag = ARCHTAG_OSXPPC; |
| 277 | } |
| 278 | else if (std::strncmp(argv[a], "--arch=", 9) == 0) |
| 279 | { |
| 280 | std::fprintf(stderr, "%s: unknown architecture tag \"%s\"\n", argv[0], &argv[a][9]); |
| 281 | usage(argv[0]); |
| 282 | } |
| 283 | else if (std::strncmp(argv[a], "--hexdump=", 10) == 0) |
| 284 | { |
| 285 | if (hexfile) |
| 286 | { |
| 287 | std::fprintf(stderr, "%s: hexdump file was already specified as \"%s\"\n", argv[0], hexfile); |
| 288 | usage(argv[0]); |
| 289 | } |
| 290 | hexfile = &argv[a][10]; |
| 291 | } |
| 292 | else if (std::strncmp(argv[a], "--startoffset=", 14) == 0) |
| 293 | { |
| 294 | if (startoffsetoverride) |
| 295 | { |
| 296 | std::fprintf(stderr, "%s: startoffset was already specified as %u\n", argv[0], startoffset); |
| 297 | usage(argv[0]); |
| 298 | } |
| 299 | if (str_to_uint(&argv[a][14], &startoffset) < 0) |
| 300 | { |
| 301 | std::fprintf(stderr, "%s: startoffset \"%s\" should be a positive integer\n", argv[0], &argv[a][14]); |
| 302 | usage(argv[0]); |
| 303 | } |
| 304 | startoffsetoverride = 1; |
| 305 | } |
| 306 | else if (std::strncmp(argv[a], "--exists=", 9) == 0) |
| 307 | { |
| 308 | if (exist_action != EXIST_ACTION_UNSPEC) |
| 309 | { |
| 310 | std::fprintf(stderr, "%s: exists was already specified\n", argv[0]); |
| 311 | usage(argv[0]); |
| 312 | } |
| 313 | if (argv[a][9] == 'o' || argv[a][9] == 'O') |
| 314 | exist_action = EXIST_ACTION_OVERWRITE; |
| 315 | else if (argv[a][9] == 'a' || argv[a][9] == 'A') |
| 316 | exist_action = EXIST_ACTION_ASK; |
| 317 | else if (argv[a][9] == 'b' || argv[a][9] == 'B') |
| 318 | exist_action = EXIST_ACTION_BACKUP; |
| 319 | else if (argv[a][9] == 'r' || argv[a][9] == 'R') |
| 320 | exist_action = EXIST_ACTION_RESUME; |
| 321 | else { |
| 322 | std::fprintf(stderr, "%s: exists must begin with a,A,o,O,b,B,r or R", argv[0]); |
| 323 | usage(argv[0]); |
| 324 | } |
| 325 | } |
| 326 | else if (std::strncmp(argv[a], "--file=", 7) == 0) |
| 327 | { |
| 328 | if (reqfile) |
| 329 | { |
| 330 | std::fprintf(stderr, "%s: file was already specified as \"%s\"\n", argv[0], reqfile); |
| 331 | usage(argv[0]); |
| 332 | } |
| 333 | reqfile = &argv[a][7]; |
| 334 | } |
| 335 | else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0) |
| 336 | { |
| 337 | std::printf("version " PVPGN_VERSION "\n"); |
| 338 | return 0; |
| 339 | } |
| 340 | else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage") == 0) |
| 341 | usage(argv[0]); |
| 342 | else if (std::strcmp(argv[a], "--client") == 0 || std::strcmp(argv[a], "--hexdump") == 0) |
| 343 | { |
| 344 | std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]); |
| 345 | usage(argv[0]); |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]); |
| 350 | usage(argv[0]); |
| 351 | } |
| 352 | |
| 353 | if (servport == 0) |
| 354 | servport = BNETD_SERV_PORT; |
| 355 | if (!clienttag) |
| 356 | clienttag = CLIENTTAG_STARCRAFT; |
| 357 | if (!archtag) |
| 358 | archtag = ARCHTAG_WINX86; |
| 359 | if (!servname) |
| 360 | servname = BNETD_DEFAULT_HOST; |
| 361 | if (exist_action == EXIST_ACTION_UNSPEC) |
| 362 | exist_action = EXIST_ACTION_ASK; |
| 363 | |
| 364 | if (hexfile) |
| 365 | { |
| 366 | if (!(hexstrm = std::fopen(hexfile, "w"))) |
| 367 | std::fprintf(stderr, "%s: could not open file \"%s\" for writing the hexdump (std::fopen: %s)", argv[0], hexfile, std::strerror(errno)); |
| 368 | else |
| 369 | { |
| 370 | std::fprintf(hexstrm, "# dump generated by bnftp version " PVPGN_VERSION "\n"); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (psock_init() < 0) |
| 375 | { |
| 376 | std::fprintf(stderr, "%s: could not inialialize socket functions\n", argv[0]); |
| 377 | return EXIT_FAILURE; |
| 378 | } |
| 379 | |
| 380 | if (!(host = gethostbyname(servname))) |
| 381 | { |
| 382 | std::fprintf(stderr, "%s: unknown host \"%s\"\n", argv[0], servname); |
| 383 | return EXIT_FAILURE; |
| 384 | } |
| 385 | |
| 386 | fd_stdin = fileno(stdin); |
| 387 | if (tcgetattr(fd_stdin, &in_attr_old) >= 0) |
| 388 | { |
| 389 | in_attr_new = in_attr_old; |
| 390 | in_attr_new.c_lflag &= ~(ECHO | ICANON); /* turn off ECHO and ICANON */ |
| 391 | in_attr_new.c_cc[VMIN] = 1; /* require reads to return at least one byte */ |
| 392 | in_attr_new.c_cc[VTIME] = 0; /* no timeout */ |
| 393 | tcsetattr(fd_stdin, TCSANOW, &in_attr_new); |
| 394 | changed_in = 1; |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | std::fprintf(stderr, "%s: could not get terminal attributes for stdin\n", argv[0]); |
| 399 | changed_in = 0; |
| 400 | } |
| 401 | |
| 402 | if (client_get_termsize(fd_stdin, &screen_width, &screen_height) < 0) |
| 403 | { |
| 404 | std::fprintf(stderr, "%s: could not determine screen size\n", argv[0]); |
| 405 | if (changed_in) |
| 406 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 407 | return EXIT_FAILURE; |
| 408 | } |
| 409 | |
| 410 | if ((sd = psock_socket(PSOCK_PF_INET, PSOCK_SOCK_STREAM, PSOCK_IPPROTO_TCP)) < 0) |
| 411 | { |
| 412 | std::fprintf(stderr, "%s: could not create socket (psock_socket: %s)\n", argv[0], std::strerror(psock_errno())); |
| 413 | if (changed_in) |
| 414 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 415 | return EXIT_FAILURE; |
| 416 | } |
| 417 | |
| 418 | std::memset(&saddr, 0, sizeof(saddr)); |
| 419 | saddr.sin_family = PSOCK_AF_INET; |
| 420 | saddr.sin_port = htons(servport); |
| 421 | std::memcpy(&saddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); |
| 422 | if (psock_connect(sd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
| 423 | { |
| 424 | std::fprintf(stderr, "%s: could not connect to server \"%s\" port %hu (psock_connect: %s)\n", argv[0], servname, servport, std::strerror(psock_errno())); |
| 425 | if (changed_in) |
| 426 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 427 | return EXIT_FAILURE; |
| 428 | } |
| 429 | |
| 430 | char addrstr[INET_ADDRSTRLEN] = { 0 }; |
| 431 | inet_ntop(AF_INET, &(saddr.sin_addr), addrstr, sizeof(addrstr)); |
| 432 | std::printf("Connected to %s:%hu.\n", addrstr, servport); |
| 433 | |
| 434 | #ifdef CLIENTDEBUG |
| 435 | eventlog_set(stderr); |
| 436 | #endif |
| 437 | |
| 438 | if (!(packet = packet_create(packet_class_init))) |
| 439 | { |
| 440 | std::fprintf(stderr, "%s: could not create packet\n", argv[0]); |
| 441 | if (changed_in) |
| 442 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 443 | return EXIT_FAILURE; |
| 444 | } |
| 445 | bn_byte_set(&packet->u.client_initconn.cclass, CLIENT_INITCONN_CLASS_FILE); |
| 446 | if (hexstrm) |
| 447 | { |
| 448 | std::fprintf(hexstrm, "%d: send class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 449 | sd, |
| 450 | packet_get_class_str(packet), (unsigned int)packet_get_class(packet), |
| 451 | packet_get_type_str(packet, packet_dir_from_client), packet_get_type(packet), |
| 452 | packet_get_size(packet)); |
| 453 | hexdump(hexstrm, packet_get_raw_data(packet, 0), packet_get_size(packet)); |
| 454 | } |
| 455 | client_blocksend_packet(sd, packet); |
| 456 | packet_del_ref(packet); |
| 457 | |
| 458 | if (!(rpacket = packet_create(packet_class_file))) |
| 459 | { |
| 460 | std::fprintf(stderr, "%s: could not create packet\n", argv[0]); |
| 461 | if (changed_in) |
| 462 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 463 | return EXIT_FAILURE; |
| 464 | } |
| 465 | |
| 466 | if (!(fpacket = packet_create(packet_class_raw))) |
| 467 | { |
| 468 | std::fprintf(stderr, "%s: could not create packet\n", argv[0]); |
| 469 | if (changed_in) |
| 470 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 471 | packet_del_ref(rpacket); |
| 472 | return EXIT_FAILURE; |
| 473 | } |
| 474 | |
| 475 | if (!reqfile) /* if not specified on the command line then prompt for it */ |
| 476 | { |
| 477 | munged = 1; |
| 478 | commpos = 0; |
| 479 | text[0] = '\0'; |
| 480 | |
| 481 | for (;;) |
| 482 | { |
| 483 | switch (client_get_comm("filename: ", text, sizeof(text), &commpos, 1, munged, screen_width)) |
| 484 | { |
| 485 | case -1: /* cancel or error */ |
| 486 | std::printf("\n"); |
| 487 | if (changed_in) |
| 488 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 489 | packet_del_ref(fpacket); |
| 490 | packet_del_ref(rpacket); |
| 491 | return EXIT_FAILURE; |
| 492 | |
| 493 | case 0: /* timeout */ |
| 494 | munged = 0; |
| 495 | continue; |
| 496 | |
| 497 | case 1: |
| 498 | munged = 0; |
| 499 | if (text[0] == '\0') |
| 500 | continue; |
| 501 | std::printf("\n"); |
| 502 | } |
| 503 | break; |
| 504 | } |
| 505 | reqfile = text; |
| 506 | } |
| 507 | |
| 508 | if (stat(reqfile, &exist_buf) == 0) /* check if the file exists */ |
| 509 | { |
| 510 | char text2[MAX_MESSAGE_LEN]; |
| 511 | |
| 512 | munged = 1; |
| 513 | commpos = 0; |
| 514 | text2[0] = '\0'; |
| 515 | |
| 516 | while (exist_action == EXIST_ACTION_ASK) |
| 517 | { |
| 518 | switch (client_get_comm("File exists [O]verwrite, [B]ackup or [R]esume?: ", text2, sizeof(text2), &commpos, 1, munged, screen_width)) |
| 519 | { |
| 520 | case -1: /* cancel or error */ |
| 521 | std::printf("\n"); |
| 522 | if (changed_in) |
| 523 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 524 | packet_del_ref(fpacket); |
| 525 | packet_del_ref(rpacket); |
| 526 | return EXIT_FAILURE; |
| 527 | |
| 528 | case 0: /* timeout */ |
| 529 | munged = 0; |
| 530 | continue; |
| 531 | |
| 532 | case 1: |
| 533 | munged = 0; |
| 534 | if (text2[0] == '\0') |
| 535 | continue; |
| 536 | std::printf("\n"); |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | switch (text2[0]) |
| 541 | { |
| 542 | case 'o': |
| 543 | case 'O': |
| 544 | exist_action = EXIST_ACTION_OVERWRITE; |
| 545 | break; |
| 546 | case 'b': |
| 547 | case 'B': |
| 548 | exist_action = EXIST_ACTION_BACKUP; |
| 549 | break; |
| 550 | case 'r': |
| 551 | case 'R': |
| 552 | exist_action = EXIST_ACTION_RESUME; |
| 553 | break; |
| 554 | default: |
| 555 | std::printf("Please answer with o,O,b,B,r or R.\n"); |
| 556 | munged = 1; |
| 557 | continue; |
| 558 | } |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | switch (exist_action) |
| 563 | { |
| 564 | case EXIST_ACTION_OVERWRITE: |
| 565 | if (!startoffsetoverride) |
| 566 | startoffset = 0; |
| 567 | break; |
| 568 | case EXIST_ACTION_BACKUP: |
| 569 | { |
| 570 | char * bakfile; |
| 571 | unsigned int bnr; |
| 572 | int renamed = 0; |
| 573 | |
| 574 | bakfile = (char*)xmalloc(std::strlen(reqfile) + 1 + 2 + 1); /* assuming we go up to bnr 99 we need reqfile+'.'+'99'+'\0' */ |
| 575 | for (bnr = 0; bnr < 100; bnr++) |
| 576 | { |
| 577 | std::sprintf(bakfile, "%s.%d", reqfile, bnr); |
| 578 | if (stat(bakfile, &exist_buf) == 0) |
| 579 | continue; /* backup exists */ |
| 580 | /* backup does not exist */ |
| 581 | if (std::rename(reqfile, bakfile) < 0) /* just std::rename the existing file to the backup */ |
| 582 | std::fprintf(stderr, "%s: could not create backup file \"%s\" (std::rename: %s)\n", argv[0], bakfile, std::strerror(errno)); |
| 583 | else |
| 584 | { |
| 585 | renamed = 1; |
| 586 | std::printf("Renaming \"%s\" to \"%s\".\n", reqfile, bakfile); |
| 587 | } |
| 588 | break; |
| 589 | } |
| 590 | xfree(bakfile); |
| 591 | if (!renamed) |
| 592 | { |
| 593 | std::fprintf(stderr, "%s: could not create backup for \"%s\".\n", argv[0], reqfile); |
| 594 | if (changed_in) |
| 595 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 596 | packet_del_ref(fpacket); |
| 597 | packet_del_ref(rpacket); |
| 598 | return EXIT_FAILURE; |
| 599 | } |
| 600 | if (!startoffsetoverride) |
| 601 | startoffset = 0; |
| 602 | } |
| 603 | break; |
| 604 | case EXIST_ACTION_RESUME: |
| 605 | if (!startoffsetoverride) |
| 606 | startoffset = exist_buf.st_size; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | else |
| 611 | if (!startoffsetoverride) |
| 612 | startoffset = 0; |
| 613 | |
| 614 | if (changed_in) |
| 615 | tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old); |
| 616 | |
| 617 | if (!(packet = packet_create(packet_class_file))) |
| 618 | { |
| 619 | std::fprintf(stderr, "%s: could not create packet\n", argv[0]); |
| 620 | packet_del_ref(fpacket); |
| 621 | packet_del_ref(rpacket); |
| 622 | return EXIT_FAILURE; |
| 623 | } |
| 624 | if (newproto) { |
| 625 | /* first send ARCH/CTAG */ |
| 626 | packet_set_size(packet, sizeof(t_client_file_req2)); |
| 627 | packet_set_type(packet, CLIENT_FILE_REQ2); |
| 628 | bn_int_tag_set(&packet->u.client_file_req2.archtag, archtag); |
| 629 | bn_int_tag_set(&packet->u.client_file_req2.clienttag, clienttag); |
| 630 | bn_long_set_a_b(&packet->u.client_file_req2.unknown1, 0, 0); |
| 631 | } |
| 632 | else { |
| 633 | packet_set_size(packet, sizeof(t_client_file_req)); |
| 634 | packet_set_type(packet, CLIENT_FILE_REQ); |
| 635 | bn_int_tag_set(&packet->u.client_file_req.archtag, archtag); |
| 636 | bn_int_tag_set(&packet->u.client_file_req.clienttag, clienttag); |
| 637 | bn_int_set(&packet->u.client_file_req.adid, 0); |
| 638 | bn_int_set(&packet->u.client_file_req.extensiontag, 0); |
| 639 | bn_int_set(&packet->u.client_file_req.startoffset, startoffset); |
| 640 | bn_long_set_a_b(&packet->u.client_file_req.timestamp, 0x00000000, 0x00000000); |
| 641 | packet_append_string(packet, reqfile); |
| 642 | } |
| 643 | if (hexstrm) |
| 644 | { |
| 645 | std::fprintf(hexstrm, "%d: send class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 646 | sd, |
| 647 | packet_get_class_str(packet), (unsigned int)packet_get_class(packet), |
| 648 | packet_get_type_str(packet, packet_dir_from_client), packet_get_type(packet), |
| 649 | packet_get_size(packet)); |
| 650 | hexdump(hexstrm, packet_get_raw_data(packet, 0), packet_get_size(packet)); |
| 651 | } |
| 652 | if (newproto) std::printf("\nSending ARCH/CTAG info..."); |
| 653 | else std::printf("\nRequesting info..."); |
| 654 | std::fflush(stdout); |
| 655 | client_blocksend_packet(sd, packet); |
| 656 | packet_del_ref(packet); |
| 657 | |
| 658 | if (newproto) { |
| 659 | /* received the stupid 4 bytes unknown "packet" */ |
| 660 | packet_set_size(fpacket, sizeof(t_server_file_unknown1)); |
| 661 | if (client_blockrecv_packet(sd, fpacket) < 0) |
| 662 | { |
| 663 | std::fprintf(stderr, "%s: server closed connection\n", argv[0]); |
| 664 | packet_del_ref(fpacket); |
| 665 | packet_del_ref(rpacket); |
| 666 | return EXIT_FAILURE; |
| 667 | } |
| 668 | if (hexstrm) |
| 669 | { |
| 670 | std::fprintf(hexstrm, "%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 671 | sd, |
| 672 | packet_get_class_str(fpacket), (unsigned int)packet_get_class(fpacket), |
| 673 | packet_get_type_str(fpacket, packet_dir_from_server), packet_get_type(fpacket), |
| 674 | packet_get_size(fpacket)); |
| 675 | hexdump(hexstrm, packet_get_raw_data(fpacket, 0), packet_get_size(fpacket)); |
| 676 | } |
| 677 | |
| 678 | /* send file info request */ |
| 679 | packet_set_size(fpacket, sizeof(t_client_file_req3)); |
| 680 | bn_int_set(&fpacket->u.client_file_req3.unknown1, 0); |
| 681 | bn_long_set_a_b(&fpacket->u.client_file_req3.timestamp, 0, 0); |
| 682 | bn_long_set_a_b(&fpacket->u.client_file_req3.unknown2, 0, 0); |
| 683 | bn_long_set_a_b(&fpacket->u.client_file_req3.unknown3, 0, 0); |
| 684 | bn_long_set_a_b(&fpacket->u.client_file_req3.unknown4, 0, 0); |
| 685 | bn_long_set_a_b(&fpacket->u.client_file_req3.unknown5, 0, 0); |
| 686 | bn_long_set_a_b(&fpacket->u.client_file_req3.unknown6, 0, 0); |
| 687 | packet_append_string(fpacket, reqfile); |
| 688 | std::printf("\nRequesting info..."); |
| 689 | std::fflush(stdout); |
| 690 | client_blocksend_packet(sd, fpacket); |
| 691 | if (hexstrm) |
| 692 | { |
| 693 | std::fprintf(hexstrm, "%d: send class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 694 | sd, |
| 695 | packet_get_class_str(fpacket), (unsigned int)packet_get_class(fpacket), |
| 696 | packet_get_type_str(fpacket, packet_dir_from_client), packet_get_type(fpacket), |
| 697 | packet_get_size(fpacket)); |
| 698 | hexdump(hexstrm, packet_get_raw_data(fpacket, 0), packet_get_size(fpacket)); |
| 699 | } |
| 700 | } |
| 701 | do |
| 702 | { |
| 703 | if (client_blockrecv_packet(sd, rpacket) < 0) |
| 704 | { |
| 705 | std::fprintf(stderr, "%s: server closed connection\n", argv[0]); |
| 706 | packet_del_ref(fpacket); |
| 707 | packet_del_ref(rpacket); |
| 708 | return EXIT_FAILURE; |
| 709 | } |
| 710 | if (hexstrm) |
| 711 | { |
| 712 | std::fprintf(hexstrm, "%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 713 | sd, |
| 714 | packet_get_class_str(rpacket), (unsigned int)packet_get_class(rpacket), |
| 715 | packet_get_type_str(rpacket, packet_dir_from_server), packet_get_type(rpacket), |
| 716 | packet_get_size(rpacket)); |
| 717 | hexdump(hexstrm, packet_get_raw_data(rpacket, 0), packet_get_size(rpacket)); |
| 718 | } |
| 719 | } while (packet_get_type(rpacket) != SERVER_FILE_REPLY); |
| 720 | |
| 721 | filelen = bn_int_get(rpacket->u.server_file_reply.filelen); |
| 722 | bn_long_to_bnettime(rpacket->u.server_file_reply.timestamp, &bntime); |
| 723 | tm = bnettime_to_time(bntime); |
| 724 | std::strftime(timestr, FILE_TIME_MAXLEN, FILE_TIME_FORMAT, std::localtime(&tm)); |
| 725 | filename = packet_get_str_const(rpacket, sizeof(t_server_file_reply), MAX_FILENAME_STR); |
| 726 | |
| 727 | if (exist_action == EXIST_ACTION_RESUME) |
| 728 | { |
| 729 | if (!(fp = std::fopen(reqfile, "ab"))) |
| 730 | { |
| 731 | std::fprintf(stderr, "%s: could not open file \"%s\" for appending (std::fopen: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 732 | packet_del_ref(fpacket); |
| 733 | packet_del_ref(rpacket); |
| 734 | return EXIT_FAILURE; |
| 735 | } |
| 736 | } |
| 737 | else |
| 738 | { |
| 739 | if (!(fp = std::fopen(reqfile, "wb"))) |
| 740 | { |
| 741 | std::fprintf(stderr, "%s: could not open file \"%s\" for writing (std::fopen: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 742 | packet_del_ref(fpacket); |
| 743 | packet_del_ref(rpacket); |
| 744 | return EXIT_FAILURE; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | std::printf("\n name: \""); |
| 749 | str_print_term(stdout, filename, 0, 0); |
| 750 | std::printf("\"\n changed: %s\n length: %u bytes\n", timestr, filelen); |
| 751 | std::fflush(stdout); |
| 752 | |
| 753 | if (startoffset > 0) { |
| 754 | filelen -= startoffset; /* for resuming files */ |
| 755 | std::printf("Resuming at position %u (%u bytes remaining).\n", startoffset, filelen); |
| 756 | } |
| 757 | |
| 758 | std::printf("\nSaving to \"%s\"...", reqfile); |
| 759 | |
| 760 | for (currsize = 0; currsize + MAX_PACKET_SIZE <= filelen; currsize += MAX_PACKET_SIZE) |
| 761 | { |
| 762 | std::printf("."); |
| 763 | std::fflush(stdout); |
| 764 | |
| 765 | if (client_blockrecv_raw_packet(sd, fpacket, MAX_PACKET_SIZE) < 0) |
| 766 | { |
| 767 | std::printf("error\n"); |
| 768 | std::fprintf(stderr, "%s: server closed connection\n", argv[0]); |
| 769 | if (std::fclose(fp) < 0) |
| 770 | std::fprintf(stderr, "%s: could not close file \"%s\" after writing (std::fclose: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 771 | packet_del_ref(fpacket); |
| 772 | packet_del_ref(rpacket); |
| 773 | return EXIT_FAILURE; |
| 774 | } |
| 775 | if (hexstrm) |
| 776 | { |
| 777 | std::fprintf(hexstrm, "%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 778 | sd, |
| 779 | packet_get_class_str(fpacket), (unsigned int)packet_get_class(fpacket), |
| 780 | packet_get_type_str(fpacket, packet_dir_from_server), packet_get_type(fpacket), |
| 781 | packet_get_size(fpacket)); |
| 782 | hexdump(hexstrm, packet_get_raw_data(fpacket, 0), packet_get_size(fpacket)); |
| 783 | } |
| 784 | if (std::fwrite(packet_get_raw_data_const(fpacket, 0), 1, MAX_PACKET_SIZE, fp) < MAX_PACKET_SIZE) |
| 785 | { |
| 786 | std::printf("error\n"); |
| 787 | std::fprintf(stderr, "%s: could not write to file \"%s\" (std::fwrite: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 788 | if (std::fclose(fp) < 0) |
| 789 | std::fprintf(stderr, "%s: could not close file \"%s\" after writing (std::fclose: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 790 | packet_del_ref(fpacket); |
| 791 | packet_del_ref(rpacket); |
| 792 | return EXIT_FAILURE; |
| 793 | } |
| 794 | } |
| 795 | filelen -= currsize; |
| 796 | if (filelen) |
| 797 | { |
| 798 | std::printf("."); |
| 799 | std::fflush(stdout); |
| 800 | |
| 801 | if (client_blockrecv_raw_packet(sd, fpacket, filelen) < 0) |
| 802 | { |
| 803 | std::printf("error\n"); |
| 804 | std::fprintf(stderr, "%s: server closed connection\n", argv[0]); |
| 805 | if (std::fclose(fp) < 0) |
| 806 | std::fprintf(stderr, "%s: could not close file \"%s\" after writing (std::fclose: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 807 | packet_del_ref(fpacket); |
| 808 | packet_del_ref(rpacket); |
| 809 | return EXIT_FAILURE; |
| 810 | } |
| 811 | if (hexstrm) |
| 812 | { |
| 813 | std::fprintf(hexstrm, "%d: recv class=%s[0x%02hx] type=%s[0x%04hx] length=%u\n", |
| 814 | sd, |
| 815 | packet_get_class_str(fpacket), (unsigned int)packet_get_class(fpacket), |
| 816 | packet_get_type_str(fpacket, packet_dir_from_server), packet_get_type(fpacket), |
| 817 | packet_get_size(fpacket)); |
| 818 | hexdump(hexstrm, packet_get_raw_data(fpacket, 0), packet_get_size(fpacket)); |
| 819 | } |
| 820 | if (std::fwrite(packet_get_raw_data_const(fpacket, 0), 1, filelen, fp) < filelen) |
| 821 | { |
| 822 | std::printf("error\n"); |
| 823 | std::fprintf(stderr, "%s: could not write to file \"%s\"\n", argv[0], reqfile); |
| 824 | if (std::fclose(fp) < 0) |
| 825 | std::fprintf(stderr, "%s: could not close file \"%s\" after writing (std::fclose: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 826 | packet_del_ref(fpacket); |
| 827 | packet_del_ref(rpacket); |
| 828 | return EXIT_FAILURE; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | packet_del_ref(fpacket); |
| 833 | packet_del_ref(rpacket); |
| 834 | |
| 835 | if (hexstrm) |
| 836 | { |
| 837 | std::fprintf(hexstrm, "# end of dump\n"); |
| 838 | if (std::fclose(hexstrm) < 0) |
| 839 | std::fprintf(stderr, "%s: could not close hexdump file \"%s\" after writing (std::fclose: %s)", argv[0], hexfile, std::strerror(errno)); |
| 840 | } |
| 841 | |
| 842 | if (std::fclose(fp) < 0) |
| 843 | { |
| 844 | std::fprintf(stderr, "%s: could not close file \"%s\" after writing (std::fclose: %s)\n", argv[0], reqfile, std::strerror(errno)); |
| 845 | return EXIT_FAILURE; |
| 846 | } |
| 847 | |
| 848 | std::printf("done\n"); |
| 849 | return EXIT_FAILURE; |
| 850 | } |