Debian Codegraph @

src/client/client_connect.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998,1999,2000,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 "client_connect.h"
20
21 #include <cstring>
22 #include <cstdio>
23 #include <cerrno>
24 #include <ctime>
25
26 #include "compat/gethostname.h"
27 #include "common/tag.h"
28 #include "common/packet.h"
29 #include "common/init_protocol.h"
30 #include "common/bnet_protocol.h"
31 #include "common/bn_type.h"
32 #include "common/bnethash.h"
33 #include "common/bnethashconv.h"
34 #include "common/bnettime.h"
35 #ifdef CLIENTDEBUG
36 # include "common/eventlog.h"
37 #endif
38 #include "udptest.h"
39 #include "client.h"
40
41 #ifdef HAVE_ARPA_INET_H
42 # include <arpa/inet.h>
43 #endif
44 #ifdef HAVE_WS2TCPIP_H
45 # include <Ws2tcpip.h>
46 #endif
47
48 #include "common/setup_after.h"
49
50
51 #define COMPNAMELEN 128
52
53 #ifdef CLIENTDEBUG
54 # define dprintf printf
55 #else
56 # define dprintf if (0) printf
57 #endif
58
59
60 namespace {
61
62 int key_interpret(char const * cdkey, unsigned int * productid, unsigned int * keyvalue1, unsigned int * keyvalue2)
63 {
64 /* FIXME: implement... have example code from Eurijk! */
65 *productid = 0;
66 *keyvalue1 = 0;
67 *keyvalue2 = 0;
68 return 0;
69 }
70
71
72 int get_defversioninfo(char const * progname, char const * clienttag, unsigned int * versionid, unsigned int * gameversion, char const * * exeinfo, unsigned int * checksum)
73 {
74 if (std::strcmp(clienttag, CLIENTTAG_DIABLORTL) == 0)
75 {
76 *versionid = CLIENT_VERSIONID_DRTL;
77 *gameversion = CLIENT_GAMEVERSION_DRTL;
78 *exeinfo = CLIENT_EXEINFO_DRTL;
79 *checksum = CLIENT_CHECKSUM_DRTL;
80 return 0;
81 }
82
83 if (std::strcmp(clienttag, CLIENTTAG_STARCRAFT) == 0)
84 {
85 *versionid = CLIENT_VERSIONID_STAR;
86 *gameversion = CLIENT_GAMEVERSION_STAR;
87 *exeinfo = CLIENT_EXEINFO_STAR;
88 *checksum = CLIENT_CHECKSUM_STAR;
89 return 0;
90 }
91
92 if (std::strcmp(clienttag, CLIENTTAG_SHAREWARE) == 0)
93 {
94 *versionid = CLIENT_VERSIONID_SSHR;
95 *gameversion = CLIENT_GAMEVERSION_SSHR;
96 *exeinfo = CLIENT_EXEINFO_SSHR;
97 *checksum = CLIENT_CHECKSUM_SSHR;
98 return 0;
99 }
100
101 if (std::strcmp(clienttag, CLIENTTAG_BROODWARS) == 0)
102 {
103 *versionid = CLIENT_VERSIONID_SEXP;
104 *gameversion = CLIENT_GAMEVERSION_SEXP;
105 *exeinfo = CLIENT_EXEINFO_SEXP;
106 *checksum = CLIENT_CHECKSUM_SEXP;
107 return 0;
108 }
109
110 if (std::strcmp(clienttag, CLIENTTAG_WARCIIBNE) == 0)
111 {
112 *versionid = CLIENT_VERSIONID_W2BN;
113 *gameversion = CLIENT_GAMEVERSION_W2BN;
114 *exeinfo = CLIENT_EXEINFO_W2BN;
115 *checksum = CLIENT_CHECKSUM_W2BN;
116 return 0;
117 }
118
119 if (std::strcmp(clienttag, CLIENTTAG_DIABLO2DV) == 0)
120 {
121 *versionid = CLIENT_VERSIONID_D2DV;
122 *gameversion = CLIENT_GAMEVERSION_D2DV;
123 *exeinfo = CLIENT_EXEINFO_D2DV;
124 *checksum = CLIENT_CHECKSUM_D2DV;
125 return 0;
126 }
127
128 if (std::strcmp(clienttag, CLIENTTAG_DIABLO2XP) == 0)
129 {
130 *versionid = CLIENT_VERSIONID_D2XP;
131 *gameversion = CLIENT_GAMEVERSION_D2XP;
132 *exeinfo = CLIENT_EXEINFO_D2XP;
133 *checksum = CLIENT_CHECKSUM_D2XP;
134 return 0;
135 }
136
137 if (std::strcmp(clienttag, CLIENTTAG_WARCRAFT3) == 0 ||
138 std::strcmp(clienttag, CLIENTTAG_WAR3XP) == 0)
139 {
140 *versionid = CLIENT_VERSIONID_WAR3;
141 *gameversion = CLIENT_GAMEVERSION_WAR3;
142 *exeinfo = CLIENT_EXEINFO_WAR3;
143 *checksum = CLIENT_CHECKSUM_WAR3;
144 return 0;
145 }
146
147 *versionid = 0;
148 *gameversion = 0;
149 *exeinfo = "";
150 *checksum = 0;
151
152 if (std::strcmp(clienttag, CLIENTTAG_BNCHATBOT) == 0)
153 return 0;
154
155 std::fprintf(stderr, "%s: unsupported clienttag \"%s\"\n", progname, clienttag);
156 // aaron: dunno what we should return in case of this.. but returning nothing was definetly wrong
157 return -1;
158 }
159
160 }
161
162 namespace pvpgn
163 {
164
165 namespace client
166 {
167
168 extern int client_connect(char const * progname, char const * servname, unsigned short servport, char const * cdowner, char const * cdkey, char const * clienttag, int ignoreversion, struct sockaddr_in * saddr, unsigned int * sessionkey, unsigned int * sessionnum, char const * archtag, char const * gamelang)
169 {
170 struct hostent * host;
171 char const * username;
172 int sd;
173 t_packet * packet;
174 t_packet * rpacket;
175 char compname[COMPNAMELEN];
176 int lsock;
177 unsigned short lsock_port;
178 unsigned int versionid;
179 unsigned int gameversion;
180 char const * exeinfo;
181 unsigned int checksum;
182
183 if (!progname)
184 {
185 std::fprintf(stderr, "got NULL progname\n");
186 return -1;
187 }
188 if (!saddr)
189 {
190 std::fprintf(stderr, "%s: got NULL saddr\n", progname);
191 return -1;
192 }
193 if (!sessionkey)
194 {
195 std::fprintf(stderr, "%s: got NULL sessionkey\n", progname);
196 return -1;
197 }
198 if (!sessionnum)
199 {
200 std::fprintf(stderr, "%s: got NULL sessionnum\n", progname);
201 return -1;
202 }
203
204 if (psock_init() < 0)
205 {
206 std::fprintf(stderr, "%s: could not inialialize socket functions\n", progname);
207 return -1;
208 }
209
210 if (!(host = gethostbyname(servname)))
211 {
212 std::fprintf(stderr, "%s: unknown host \"%s\"\n", progname, servname);
213 return -1;
214 }
215 if (host->h_addrtype != PSOCK_AF_INET)
216 std::fprintf(stderr, "%s: host \"%s\" is not in IPv4 address family\n", progname, servname);
217
218 if (gethostname(compname, COMPNAMELEN) < 0)
219 {
220 std::fprintf(stderr, "%s: could not get host name (gethostname: %s)\n", progname, std::strerror(errno));
221 return -1;
222 }
223 #ifdef HAVE_GETLOGIN
224 if (!(username = getlogin()))
225 #endif
226 {
227 username = "unknown";
228 dprintf("%s: could not get login name, using \"%s\" (getlogin: %s)\n", progname, username, std::strerror(errno));
229 }
230
231 if ((sd = psock_socket(PSOCK_PF_INET, PSOCK_SOCK_STREAM, PSOCK_IPPROTO_TCP)) < 0)
232 {
233 std::fprintf(stderr, "%s: could not create socket (psock_socket: %s)\n", progname, std::strerror(psock_errno()));
234 return -1;
235 }
236
237 std::memset(saddr, 0, sizeof(*saddr));
238 saddr->sin_family = PSOCK_AF_INET;
239 saddr->sin_port = htons(servport);
240 std::memcpy(&saddr->sin_addr.s_addr, host->h_addr_list[0], host->h_length);
241 if (psock_connect(sd, (struct sockaddr *)saddr, sizeof(*saddr)) < 0)
242 {
243 std::fprintf(stderr, "%s: could not connect to server \"%s\" port %hu (psock_connect: %s)\n", progname, servname, servport, std::strerror(psock_errno()));
244 psock_close(sd);
245 return -1;
246 }
247
248 char addrstr[INET_ADDRSTRLEN] = { 0 };
249 inet_ntop(AF_INET, &(saddr->sin_addr), addrstr, sizeof(addrstr));
250 std::printf("Connected to %s:%hu.\n", addrstr, servport);
251
252 #ifdef CLIENTDEBUG
253 eventlog_set(stderr);
254 #endif
255
256 if ((lsock = client_udptest_setup(progname, &lsock_port)) >= 0)
257 dprintf("Got UDP data on port %hu\n", lsock_port);
258
259 if (!(packet = packet_create(packet_class_init)))
260 {
261 std::fprintf(stderr, "%s: could not create packet\n", progname);
262 if (lsock >= 0)
263 psock_close(lsock);
264
265 psock_close(sd);
266
267 return -1;
268 }
269 bn_byte_set(&packet->u.client_initconn.cclass, CLIENT_INITCONN_CLASS_BNET);
270 client_blocksend_packet(sd, packet);
271 packet_del_ref(packet);
272
273 /* reuse this same packet over and over */
274 if (!(rpacket = packet_create(packet_class_bnet)))
275 {
276 std::fprintf(stderr, "%s: could not create packet\n", progname);
277 if (lsock >= 0)
278 psock_close(lsock);
279
280 psock_close(sd);
281
282 return -1;
283 }
284
285 get_defversioninfo(progname, clienttag, &versionid, &gameversion, &exeinfo, &checksum);
286
287 if (std::strcmp(clienttag, CLIENTTAG_DIABLOSHR) == 0 ||
288 std::strcmp(clienttag, CLIENTTAG_DIABLORTL) == 0)
289 {
290 if (!(packet = packet_create(packet_class_bnet)))
291 {
292 std::fprintf(stderr, "%s: could not create packet\n", progname);
293 packet_destroy(rpacket);
294
295 if (lsock >= 0)
296 psock_close(lsock);
297
298 psock_close(sd);
299
300 return -1;
301 }
302 packet_set_size(packet, sizeof(t_client_unknown_1b));
303 packet_set_type(packet, CLIENT_UNKNOWN_1B);
304 bn_short_set(&packet->u.client_unknown_1b.unknown1, CLIENT_UNKNOWN_1B_UNKNOWN3);
305 bn_short_nset(&packet->u.client_unknown_1b.port, lsock_port);
306 bn_int_nset(&packet->u.client_unknown_1b.ip, 0); /* FIXME */
307 bn_int_set(&packet->u.client_unknown_1b.unknown2, CLIENT_UNKNOWN_1B_UNKNOWN3);
308 bn_int_set(&packet->u.client_unknown_1b.unknown3, CLIENT_UNKNOWN_1B_UNKNOWN3);
309 client_blocksend_packet(sd, packet);
310 packet_del_ref(packet);
311 }
312
313 if (!(packet = packet_create(packet_class_bnet)))
314 {
315 std::fprintf(stderr, "%s: could not create packet\n", progname);
316 packet_destroy(rpacket);
317
318 if (lsock >= 0)
319 psock_close(lsock);
320
321 psock_close(sd);
322
323 return -1;
324 }
325 packet_set_size(packet, sizeof(t_client_auth_info));
326 packet_set_type(packet, CLIENT_AUTH_INFO);
327 bn_int_set(&packet->u.client_auth_info.protocol, CLIENT_AUTH_INFO_PROTOCOL);
328 bn_int_tag_set(&packet->u.client_auth_info.archtag, archtag);
329 bn_int_tag_set(&packet->u.client_auth_info.clienttag, clienttag);
330 //AARON
331
332 bn_int_set(&packet->u.client_auth_info.versionid, versionid);
333 bn_int_tag_set(&packet->u.client_auth_info.gamelang, gamelang);
334 bn_int_set(&packet->u.client_auth_info.localip, CLIENT_AUTH_INFO_LOCALIP);
335 {
336 int bias;
337
338 bias = local_tzbias();
339
340 bn_int_set(&packet->u.client_auth_info.bias, (unsigned int)bias); /* rely on 2's complement */
341 dprintf("my tzbias = %d (0x%08hx)\n", bias, (unsigned int)bias);
342 }
343 /* FIXME: determine from locale */
344 bn_int_set(&packet->u.client_auth_info.lcid, CLIENT_AUTH_INFO_LANGID_USENGLISH);
345 bn_int_set(&packet->u.client_auth_info.langid, CLIENT_AUTH_INFO_LANGID_USENGLISH);
346 packet_append_string(packet, CLIENT_AUTH_INFO_LANGSTR_USENGLISH);
347 /* FIXME: determine from locale+timezone... from domain name... nothing really would
348 work. Maybe add some command-line options */
349 packet_append_string(packet, CLIENT_AUTH_INFO_COUNTRYNAME_USA);
350 client_blocksend_packet(sd, packet);
351 packet_del_ref(packet);
352
353 do
354 if (client_blockrecv_packet(sd, rpacket) < 0)
355 {
356 std::fprintf(stderr, "%s: server closed connection\n", progname);
357 packet_destroy(rpacket);
358
359 if (lsock >= 0)
360 psock_close(lsock);
361
362 psock_close(sd);
363
364 return -1;
365 }
366 while (packet_get_type(rpacket) != SERVER_AUTHREQ_109 &&
367 packet_get_type(rpacket) != SERVER_AUTHREPLY_109);
368
369 if (packet_get_type(rpacket) == SERVER_AUTHREQ_109) /* hmm... server wants to check the version number */
370 {
371 dprintf("Got AUTHREQ_109\n");
372 *sessionkey = bn_int_get(rpacket->u.server_authreq_109.sessionkey);
373 *sessionnum = bn_int_get(rpacket->u.server_authreq_109.sessionnum);
374 /* FIXME: also get filename and equation */
375
376 if (!ignoreversion)
377 {
378
379 if (!(packet = packet_create(packet_class_bnet)))
380 {
381 std::fprintf(stderr, "%s: could not create packet\n", progname);
382 packet_destroy(rpacket);
383
384 if (lsock >= 0)
385 psock_close(lsock);
386
387 psock_close(sd);
388
389 return -1;
390 }
391 packet_set_size(packet, sizeof(t_client_authreq_109));
392 packet_set_type(packet, CLIENT_AUTHREQ_109);
393 bn_int_set(&packet->u.client_authreq_109.ticks, std::time(NULL));
394
395 {
396 t_cdkey_info cdkey_info = {};
397
398 bn_int_set(&packet->u.client_authreq_109.gameversion, gameversion);
399
400 bn_int_set(&packet->u.client_authreq_109.cdkey_number, 1); /* only one */
401 /* FIXME: put the input cdkey here */
402 packet_append_data(packet, &cdkey_info, sizeof(cdkey_info));
403 packet_append_string(packet, exeinfo);
404 packet_append_string(packet, cdowner);
405 }
406 bn_int_set(&packet->u.client_authreq_109.spawn, 0x0000);
407 bn_int_set(&packet->u.client_authreq_109.checksum, checksum);
408 client_blocksend_packet(sd, packet);
409 packet_del_ref(packet);
410
411 /* now wait for reply */
412 do
413 if (client_blockrecv_packet(sd, rpacket) < 0)
414 {
415 std::fprintf(stderr, "%s: server closed connection\n", progname);
416 packet_destroy(rpacket);
417
418 if (lsock >= 0)
419 psock_close(lsock);
420
421 psock_close(sd);
422
423 return -1;
424 }
425 while (packet_get_type(rpacket) != SERVER_AUTHREPLY_109);
426 //FIXME: check if AUTHREPLY_109 is success or fail
427 if (bn_int_get(rpacket->u.server_authreply_109.message) != SERVER_AUTHREPLY_109_MESSAGE_OK)
428 {
429 std::fprintf(stderr, "AUTHREPLY_109 failed - closing connection\n");
430 packet_destroy(rpacket);
431
432 if (lsock >= 0)
433 psock_close(lsock);
434
435 psock_close(sd);
436
437 return -1;
438 }
439 }
440 }
441 else
442 std::fprintf(stderr, "We didn't get a sessionkey, don't expect login to work!");
443 dprintf("Got AUTHREPLY_109\n");
444
445 if (!(packet = packet_create(packet_class_bnet)))
446 {
447 std::fprintf(stderr, "%s: could not create packet\n", progname);
448 packet_destroy(rpacket);
449
450 if (lsock >= 0)
451 psock_close(lsock);
452
453 psock_close(sd);
454
455 return -1;
456 }
457 packet_set_size(packet, sizeof(t_client_iconreq));
458 packet_set_type(packet, CLIENT_ICONREQ);
459 client_blocksend_packet(sd, packet);
460 packet_del_ref(packet);
461
462 do
463 if (client_blockrecv_packet(sd, rpacket) < 0)
464 {
465 std::fprintf(stderr, "%s: server closed connection\n", progname);
466 packet_destroy(rpacket);
467
468 if (lsock >= 0)
469 psock_close(lsock);
470
471 psock_close(sd);
472
473 return -1;
474 }
475 while (packet_get_type(rpacket) != SERVER_ICONREPLY);
476 dprintf("Got ICONREPLY\n");
477
478 if (std::strcmp(clienttag, CLIENTTAG_STARCRAFT) == 0 ||
479 std::strcmp(clienttag, CLIENTTAG_BROODWARS) == 0 ||
480 std::strcmp(clienttag, CLIENTTAG_WARCIIBNE) == 0)
481 {
482 if (!(packet = packet_create(packet_class_bnet)))
483 {
484 std::fprintf(stderr, "%s: could not create packet\n", progname);
485 packet_destroy(rpacket);
486
487 if (lsock >= 0)
488 psock_close(lsock);
489
490 psock_close(sd);
491
492 return -1;
493 }
494
495 {
496 struct
497 {
498 bn_int sessionkey;
499 bn_int ticks;
500 bn_int productid;
501 bn_int keyvalue1;
502 bn_int keyvalue2;
503 } temp;
504 t_hash key_hash;
505 unsigned int ticks;
506 unsigned int keyvalue1, keyvalue2, productid;
507
508 if (key_interpret(cdkey, &productid, &keyvalue1, &keyvalue2) < 0)
509 {
510 std::fprintf(stderr, "%s: specified key is not valid, sending junk\n", progname);
511 productid = 0;
512 keyvalue1 = 0;
513 keyvalue2 = 0;
514 }
515
516 ticks = 0; /* FIXME: what to use here? */
517 bn_int_set(&temp.ticks, ticks);
518 bn_int_set(&temp.sessionkey, *sessionkey);
519 bn_int_set(&temp.productid, productid);
520 bn_int_set(&temp.keyvalue1, keyvalue1);
521 bn_int_set(&temp.keyvalue2, keyvalue2);
522 bnet_hash(&key_hash, sizeof(temp), &temp);
523
524 packet_set_size(packet, sizeof(t_client_cdkey2));
525 packet_set_type(packet, CLIENT_CDKEY2);
526 bn_int_set(&packet->u.client_cdkey2.spawn, CLIENT_CDKEY2_SPAWN_FALSE); /* FIXME: add option */
527 bn_int_set(&packet->u.client_cdkey2.keylen, std::strlen(cdkey));
528 bn_int_set(&packet->u.client_cdkey2.productid, productid);
529 bn_int_set(&packet->u.client_cdkey2.keyvalue1, keyvalue1);
530 bn_int_set(&packet->u.client_cdkey2.sessionkey, *sessionkey);
531 bn_int_set(&packet->u.client_cdkey2.ticks, ticks);
532 hash_to_bnhash((t_hash const *)&key_hash, packet->u.client_cdkey2.key_hash); /* avoid warning */
533 packet_append_string(packet, cdowner);
534 client_blocksend_packet(sd, packet);
535 packet_del_ref(packet);
536 }
537
538 do
539 if (client_blockrecv_packet(sd, rpacket) < 0)
540 {
541 std::fprintf(stderr, "%s: server closed connection\n", progname);
542 packet_destroy(rpacket);
543
544 if (lsock >= 0)
545 psock_close(lsock);
546
547 psock_close(sd);
548
549 return -1;
550 }
551 while (packet_get_type(rpacket) != SERVER_CDKEYREPLY2);
552 dprintf("Got CDKEYREPLY2 (%u)\n", bn_int_get(rpacket->u.server_cdkeyreply2.message));
553 }
554
555 packet_destroy(rpacket);
556
557 if (lsock >= 0)
558 psock_close(lsock);
559
560 return sd;
561 }
562
563 }
564
565 }