Debian Codegraph @

src/bnetd/connection.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998 Mark Baysinger ([email protected])
3 * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected])
4 * Copyright (C) 2000,2001 Marco Ziech ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 #define CONNECTION_INTERNAL_ACCESS
21 #include "common/setup_before.h"
22 #include "connection.h"
23
24 #include <cerrno>
25 #include <cstring>
26 #include <cassert>
27
28 #ifdef WIN32_GUI
29 #include <win32/winmain.h>
30 #endif
31 #include "compat/strcasecmp.h"
32 #include "compat/strncasecmp.h"
33 #include "compat/socket.h"
34 #include "compat/psock.h"
35 #include "common/eventlog.h"
36 #include "common/addr.h"
37 #include "common/queue.h"
38 #include "common/packet.h"
39 #include "common/tag.h"
40 #include "common/bn_type.h"
41 #include "common/version.h"
42 #include "common/util.h"
43 #include "common/list.h"
44 #include "common/bnet_protocol.h"
45 #include "common/field_sizes.h"
46 #include "common/rcm.h"
47 #include "common/fdwatch.h"
48 #include "common/elist.h"
49 #include "common/xalloc.h"
50
51 #include "account.h"
52 #include "account_wrap.h"
53 #include "realm.h"
54 #include "channel.h"
55 #include "game.h"
56 #include "tick.h"
57 #include "message.h"
58 #include "prefs.h"
59 #include "watch.h"
60 #include "timer.h"
61 #include "irc.h"
62 #include "ipban.h"
63 #include "game_conv.h"
64 #include "udptest_send.h"
65 #include "character.h"
66 #include "versioncheck.h"
67 #include "anongame.h"
68 #include "clan.h"
69 #include "topic.h"
70 #include "server.h"
71 #include "handle_d2cs.h"
72 #include "command_groups.h"
73 #include "attrlayer.h"
74 #include "anongame_wol.h"
75 #include "icons.h"
76 #include "i18n.h"
77 #include "common/setup_after.h"
78 #include "prefs.h"
79
80 #ifdef WITH_LUA
81 #include "luainterface.h"
82 #endif
83
84 namespace pvpgn
85 {
86
87 namespace bnetd
88 {
89
90 /* types and data structures used for the connlist array */
91 typedef struct {
92 t_connection *c;
93 t_elist freelist;
94 } t_conn_entry;
95
96 t_conn_entry *connarray = NULL;
97 t_elist arrayflist;
98
99 static int totalcount = 0;
100 static t_list * conn_head = NULL;
101 static t_list * conn_dead = NULL;
102
103 static void conn_send_welcome(t_connection * c);
104 static void conn_send_issue(t_connection * c);
105
106 static int connarray_create(void);
107 static void connarray_destroy(void);
108 static t_connection *connarray_get_conn(unsigned index);
109 static unsigned connarray_add_conn(t_connection *c);
110 static void connarray_del_conn(unsigned index);
111
112 static void conn_send_welcome(t_connection * c)
113 {
114 char const * filename;
115 std::FILE * fp;
116
117 if (!c)
118 {
119 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
120 return;
121 }
122
123 if (c->protocol.cflags & conn_flags_welcomed)
124 return;
125
126 if ((conn_get_class(c) == conn_class_irc) || (conn_get_class(c) == conn_class_wol))
127 {
128 c->protocol.cflags |= conn_flags_welcomed;
129 return;
130 }
131
132 if (filename = prefs_get_motdfile())
133 {
134 std::string lang_filename = i18n_filename(filename, conn_get_gamelang_localized(c));
135 if (fp = std::fopen(lang_filename.c_str(), "r"))
136 {
137 message_send_file(c, fp);
138 if (std::fclose(fp) < 0)
139 {
140 eventlog(eventlog_level_error, __FUNCTION__, "could not close MOTD file \"{}\" after reading (std::fopen: {})", lang_filename, std::strerror(errno));
141 }
142 }
143 else
144 {
145 eventlog(eventlog_level_error, __FUNCTION__, "could not open MOTD file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno));
146 }
147 }
148 c->protocol.cflags |= conn_flags_welcomed;
149 }
150
151
152 static void conn_send_issue(t_connection * c)
153 {
154 char const * filename;
155 std::FILE * fp;
156
157 if (!c)
158 {
159 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
160 return;
161 }
162
163 if ((filename = prefs_get_issuefile()))
164 if ((fp = std::fopen(filename, "r")))
165 {
166 message_send_file(c, fp);
167 if (std::fclose(fp) < 0)
168 eventlog(eventlog_level_error, __FUNCTION__, "could not close issue file \"{}\" after reading (std::fopen: {})", filename, std::strerror(errno));
169 }
170 else
171 eventlog(eventlog_level_error, __FUNCTION__, "could not open issue file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno));
172 else
173 eventlog(eventlog_level_debug, __FUNCTION__, "no issue file");
174 }
175
176 // [zap-zero] 20020629
177 extern void conn_shutdown(t_connection * c, std::time_t now, t_timer_data foo)
178 {
179 if (!c)
180 {
181 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
182 return;
183 }
184
185 if (now == (std::time_t)0) /* zero means user logged out before expiration */
186 {
187 eventlog(eventlog_level_trace, __FUNCTION__, "[{}] connection already closed", conn_get_socket(c));
188 return;
189 }
190
191 eventlog(eventlog_level_trace, __FUNCTION__, "[{}] closing connection", conn_get_socket(c));
192
193 conn_set_state(c, conn_state_destroy);
194 }
195
196 extern void conn_test_latency(t_connection * c, std::time_t now, t_timer_data delta)
197 {
198 t_packet * packet;
199
200 if (!c)
201 {
202 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
203 return;
204 }
205
206
207 if (now == (std::time_t)0) /* zero means user logged out before expiration */
208 return;
209
210 if (conn_get_state(c) == conn_state_destroy) // [zap-zero] 20020910
211 return; // state_destroy: do nothing
212
213
214 if ((conn_get_class(c) == conn_class_irc) || (conn_get_class(c) == conn_class_wol)) {
215 /* We should start pinging the client after we received the first line ... */
216 /* NOTE: RFC2812 only suggests that PINGs are being sent
217 * if no other activity is detected. However it explecitly
218 * allows PINGs to be sent if there is activity on this
219 * connection. In other words we just don't care :)
220 */
221 if (conn_get_ircping(c) != 0) {
222 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] ping timeout (closing connection)", conn_get_socket(c));
223 conn_set_latency(c, 0);
224 conn_set_state(c, conn_state_destroy);
225 }
226 irc_send_ping(c);
227 }
228 else if (conn_get_class(c) == conn_class_w3route) {
229 if (!(packet = packet_create(packet_class_w3route))) {
230 eventlog(eventlog_level_error, __FUNCTION__, "[{}] packet_create failed", conn_get_socket(c));
231 }
232 else {
233 packet_set_size(packet, sizeof(t_server_w3route_echoreq));
234 packet_set_type(packet, SERVER_W3ROUTE_ECHOREQ);
235 bn_int_set(&packet->u.server_w3route_echoreq.ticks, get_ticks());
236 conn_push_outqueue(c, packet);
237 packet_del_ref(packet);
238 }
239 }
240 else {
241
242 /* FIXME: I think real Battle.net sends these even before login */
243 if (!conn_get_game(c))
244 {
245 if ((packet = packet_create(packet_class_bnet)))
246 {
247 packet_set_size(packet, sizeof(t_server_echoreq));
248 packet_set_type(packet, SERVER_ECHOREQ);
249 bn_int_set(&packet->u.server_echoreq.ticks, get_ticks());
250 conn_push_outqueue(c, packet);
251 packet_del_ref(packet);
252 }
253 else
254 {
255 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
256 }
257 }
258 }
259
260 if (timerlist_add_timer(c, now + (std::time_t)delta.n, conn_test_latency, delta) < 0)
261 eventlog(eventlog_level_error, __FUNCTION__, "could not add timer");
262 }
263
264
265 static void conn_send_nullmsg(t_connection * c, std::time_t now, t_timer_data delta)
266 {
267 if (!c)
268 {
269 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
270 return;
271 }
272
273 if (now == (std::time_t)0) /* zero means user logged out before expiration */
274 return;
275
276 message_send_text(c, message_type_null, c, NULL);
277
278 if (timerlist_add_timer(c, now + (std::time_t)delta.n, conn_send_nullmsg, delta) < 0)
279 eventlog(eventlog_level_error, __FUNCTION__, "could not add timer");
280 }
281
282
283 extern char const * conn_class_get_str(t_conn_class cclass)
284 {
285 switch (cclass)
286 {
287 case conn_class_init:
288 return "init";
289 case conn_class_bnet:
290 return "bnet";
291 case conn_class_file:
292 return "file";
293 case conn_class_bot:
294 return "bot";
295 case conn_class_d2cs_bnetd:
296 return "d2cs_bnetd";
297 case conn_class_telnet:
298 return "telnet";
299 case conn_class_irc:
300 return "irc";
301 case conn_class_wol:
302 return "wol";
303 case conn_class_wserv:
304 return "wserv";
305 case conn_class_apireg:
306 return "apireg";
307 case conn_class_wgameres:
308 return "wgameres";
309 case conn_class_wladder:
310 return "wladder";
311 case conn_class_none:
312 return "none";
313 case conn_class_w3route:
314 return "w3route";
315 default:
316 return "UNKNOWN";
317 }
318 }
319
320
321 extern char const * conn_state_get_str(t_conn_state state)
322 {
323 switch (state)
324 {
325 case conn_state_empty:
326 return "empty";
327 case conn_state_initial:
328 return "initial";
329 case conn_state_connected:
330 return "connected";
331 case conn_state_bot_username:
332 return "bot_username";
333 case conn_state_bot_password:
334 return "bot_password";
335 case conn_state_loggedin:
336 return "loggedin";
337 case conn_state_destroy:
338 return "destroy";
339 case conn_state_untrusted:
340 return "untrusted";
341 case conn_state_pending_raw:
342 return "pending_raw";
343 default:
344 return "UNKNOWN";
345 }
346 }
347
348 extern int conn_set_realm_cb(void *data, void *newref);
349
350 extern t_connection * conn_create(int tsock, int usock, unsigned int real_local_addr, unsigned short real_local_port, unsigned int local_addr, unsigned short local_port, unsigned int addr, unsigned short port)
351 {
352 t_connection * temp;
353
354
355 if (tsock < 0)
356 {
357 eventlog(eventlog_level_error, __FUNCTION__, "got bad TCP socket {}", tsock);
358 return NULL;
359 }
360 if (usock < -1) /* -1 is allowed for some connection classes like bot, irc, and telnet */
361 {
362 eventlog(eventlog_level_error, __FUNCTION__, "got bad UDP socket {}", usock);
363 return NULL;
364 }
365
366 temp = (t_connection*)xmalloc(sizeof(t_connection));
367 temp->socket.tcp_sock = tsock;
368 temp->socket.tcp_addr = addr;
369 temp->socket.tcp_port = port;
370 temp->socket.udp_sock = usock;
371 temp->socket.udp_addr = addr; /* same for now but client can request it to be different */
372 temp->socket.local_addr = local_addr;
373 temp->socket.local_port = local_port;
374 temp->socket.real_local_addr = real_local_addr;
375 temp->socket.real_local_port = real_local_port;
376 temp->socket.udp_port = port;
377 temp->socket.fdw_idx = -1;
378 temp->protocol.cclass = conn_class_init;
379 temp->protocol.state = conn_state_initial;
380 temp->protocol.sessionkey = ((unsigned int)std::rand()) ^ ((unsigned int)now + (unsigned int)real_local_port);
381 temp->protocol.sessionnum = connarray_add_conn(temp);
382 temp->protocol.secret = ((unsigned int)std::rand()) ^ (totalcount + ((unsigned int)now));
383 temp->protocol.flags = MF_PLUG;
384 temp->protocol.latency = 0;
385 temp->protocol.chat.dnd = NULL;
386 temp->protocol.chat.away = NULL;
387 temp->protocol.chat.ignore_list = NULL;
388 temp->protocol.chat.ignore_count = 0;
389 temp->protocol.chat.quota.totcount = 0;
390 temp->protocol.chat.quota.list = list_create();
391 temp->protocol.client.versionid = 0;
392 temp->protocol.client.gameversion = 0;
393 temp->protocol.client.checksum = 0;
394 temp->protocol.client.archtag = 0;
395 temp->protocol.client.clienttag = 0;
396 temp->protocol.client.clientver = NULL;
397 temp->protocol.client.gamelang = 0;
398 temp->protocol.client.country = NULL;
399 temp->protocol.client.tzbias = 0;
400 temp->protocol.client.host = NULL;
401 temp->protocol.client.user = NULL;
402 temp->protocol.client.clientexe = NULL;
403 temp->protocol.client.owner = NULL;
404 temp->protocol.client.cdkey = NULL;
405 temp->protocol.client.versioncheck = nullptr;
406 temp->protocol.account = NULL;
407 temp->protocol.chat.channel = NULL;
408 temp->protocol.chat.last_message = now;
409 temp->protocol.chat.lastsender = NULL;
410 temp->protocol.chat.irc.ircline = NULL;
411 temp->protocol.chat.irc.ircping = 0;
412 temp->protocol.chat.irc.ircpass = NULL;
413 temp->protocol.chat.tmpOP_channel = NULL;
414 temp->protocol.chat.tmpVOICE_channel = NULL;
415 temp->protocol.game = NULL;
416 temp->protocol.queues.outqueue = NULL;
417 temp->protocol.queues.outsize = 0;
418 temp->protocol.queues.outsizep = 0;
419 temp->protocol.queues.inqueue = NULL;
420 temp->protocol.queues.insize = 0;
421 temp->protocol.loggeduser = NULL;
422 temp->protocol.d2.realm = NULL;
423 rcm_regref_init(&temp->protocol.d2.realm_regref, &conn_set_realm_cb, temp);
424 temp->protocol.d2.character = NULL;
425 temp->protocol.d2.realminfo = NULL;
426 temp->protocol.d2.charname = NULL;
427 temp->protocol.w3.w3_playerinfo = NULL;
428 temp->protocol.w3.routeconn = NULL;
429 temp->protocol.w3.anongame = NULL;
430 temp->protocol.w3.anongame_search_starttime = 0;
431 temp->protocol.w3.client_proof = NULL;
432 temp->protocol.w3.server_proof = NULL;
433 temp->protocol.bound = NULL;
434 elist_init(&temp->protocol.timers);
435
436 temp->protocol.wol.ingame = 0;
437
438 temp->protocol.wol.codepage = 0;
439 temp->protocol.wol.pageme = true;
440 temp->protocol.wol.findme = true;
441
442 temp->protocol.wol.apgar = NULL;
443 temp->protocol.wol.anongame_player = NULL;
444
445
446 temp->protocol.cr_time = now;
447 temp->protocol.passfail_count = 0;
448
449
450 temp->protocol.cflags = 0;
451
452 list_prepend_data(conn_head, temp);
453
454 eventlog(eventlog_level_debug, __FUNCTION__, "[{}][{}] sessionkey=0x{:08} sessionnum=0x{:08}", temp->socket.tcp_sock, temp->socket.udp_sock, temp->protocol.sessionkey, temp->protocol.sessionnum);
455
456 return temp;
457 }
458
459
460 extern t_anongame * conn_create_anongame(t_connection *c)
461 {
462 t_anongame * temp;
463 int i;
464
465 if (c->protocol.w3.anongame) {
466 eventlog(eventlog_level_error, __FUNCTION__, "anongame already allocated");
467 return c->protocol.w3.anongame;
468 }
469
470 temp = (t_anongame*)xmalloc(sizeof(t_anongame));
471 temp->count = 0;
472 temp->id = 0;
473 temp->tid = 0;
474
475 for (i = 0; i < ANONGAME_MAX_GAMECOUNT / 2; i++)
476 temp->tc[i] = NULL;
477
478 temp->race = 0;
479 temp->playernum = 0;
480 temp->handle = 0;
481 temp->addr = 0;
482 temp->loaded = 0;
483 temp->joined = 0;
484 temp->map_prefs = 0xffffffff;
485 temp->type = 0;
486 temp->gametype = 0;
487 temp->queue = 0;
488 temp->info = NULL;
489
490 c->protocol.w3.anongame = temp;
491
492 return temp;
493 }
494
495 extern t_anongame * conn_get_anongame(t_connection *c)
496 {
497 if (!c) {
498 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
499 return NULL;
500 }
501 return c->protocol.w3.anongame;
502 }
503
504 extern void conn_destroy_anongame(t_connection *c)
505 {
506 t_anongame * a;
507
508 if (!c)
509 {
510 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
511 return;
512 }
513
514 if (!(a = c->protocol.w3.anongame))
515 {
516 eventlog(eventlog_level_error, __FUNCTION__, "NULL anongame");
517 return;
518 }
519
520 // delete reference to this connection
521 if (a->info) {
522 a->info->player[a->playernum - 1] = NULL;
523 if (--(a->info->currentplayers) == 0)
524 anongameinfo_destroy(a->info);
525 }
526
527 // [quetzal] 20020824
528 // unqueue from anongame search list,
529 // if we got AT game, unqueue entire team.
530 if (anongame_arranged(a->queue)) {
531 anongame_unqueue(a->tc[0], a->queue);
532 }
533 else {
534 anongame_unqueue(c, a->queue);
535 }
536 xfree(c->protocol.w3.anongame);
537 c->protocol.w3.anongame = NULL;
538 }
539
540 extern void conn_destroy(t_connection * c, t_elem ** elem, int conn_or_dead_list)
541 {
542 char const * classstr;
543 t_elem * curr;
544
545
546 if (c == NULL) {
547 eventlog(eventlog_level_error, "conn_destroy", "got NULL connection");
548 return;
549 }
550
551 classstr = conn_class_get_str(c->protocol.cclass);
552
553 if (list_remove_data(conn_head, c, (conn_or_dead_list) ? &curr : elem) < 0)
554 {
555 eventlog(eventlog_level_error, __FUNCTION__, "could not remove item from list");
556 return;
557 }
558
559 if (c->protocol.cclass == conn_class_d2cs_bnetd)
560 {
561 t_realm * realm;
562
563 realm = conn_get_realm(c);
564 if (realm)
565 realm_deactive(realm);
566 else
567 {
568 eventlog(eventlog_level_error, __FUNCTION__, "could not find realm for d2cs connection");
569 }
570 }
571 else if (c->protocol.cclass == conn_class_w3route && c->protocol.w3.routeconn && c->protocol.w3.routeconn->protocol.w3.anongame)
572 {
573 anongame_stats(c);
574 conn_destroy_anongame(c->protocol.w3.routeconn); // [zap-zero] destroy anongame too when game connection is invalid
575 }
576
577 if (c->protocol.d2.realm) {
578 realm_add_player_number(c->protocol.d2.realm, -1);
579 realm_put(c->protocol.d2.realm, &c->protocol.d2.realm_regref);
580 }
581
582
583 /* free the memory with user quota */
584 {
585 t_qline * qline;
586
587 LIST_TRAVERSE(c->protocol.chat.quota.list, curr)
588 {
589 qline = (t_qline*)elem_get_data(curr);
590 xfree(qline);
591 list_remove_elem(c->protocol.chat.quota.list, &curr);
592 }
593 list_destroy(c->protocol.chat.quota.list);
594 }
595
596 /* if this user in a channel, notify everyone that the user has left */
597 if (c->protocol.chat.channel)
598 channel_del_connection(c->protocol.chat.channel, c, message_type_quit, NULL);
599
600 if ((c->protocol.game) && (c->protocol.account))
601 {
602 if (game_get_status(c->protocol.game) == game_status_started)
603 {
604 game_set_self_report(c->protocol.game, c->protocol.account, game_result_disconnect);
605 game_set_report(c->protocol.game, c->protocol.account, "disconnect", "disconnect");
606 }
607 }
608
609 conn_set_game(c, NULL, NULL, NULL, game_type_none, 0);
610 c->protocol.state = conn_state_empty;
611
612 watchlist->del(c);
613 timerlist_del_all_timers(c);
614
615 clanmember_set_offline(c);
616
617 if (c->protocol.account)
618 watchlist->dispatch(c->protocol.account, NULL, c->protocol.client.clienttag, Watch::ET_logout);
619
620 if (c->protocol.chat.lastsender)
621 xfree((void *)c->protocol.chat.lastsender); /* avoid warning */
622
623 if (c->protocol.chat.away)
624 xfree((void *)c->protocol.chat.away); /* avoid warning */
625 if (c->protocol.chat.dnd)
626 xfree((void *)c->protocol.chat.dnd); /* avoid warning */
627 if (c->protocol.chat.tmpOP_channel)
628 xfree((void *)c->protocol.chat.tmpOP_channel); /* avoid warning */
629 if (c->protocol.chat.tmpVOICE_channel)
630 xfree((void *)c->protocol.chat.tmpVOICE_channel); /* avoid warning */
631
632 if (c->protocol.client.clientver)
633 xfree((void *)c->protocol.client.clientver); /* avoid warning */
634 if (c->protocol.client.country)
635 xfree((void *)c->protocol.client.country); /* avoid warning */
636 if (c->protocol.client.host)
637 xfree((void *)c->protocol.client.host); /* avoid warning */
638 if (c->protocol.client.user)
639 xfree((void *)c->protocol.client.user); /* avoid warning */
640 if (c->protocol.client.clientexe)
641 xfree((void *)c->protocol.client.clientexe); /* avoid warning */
642 if (c->protocol.client.owner)
643 xfree((void *)c->protocol.client.owner); /* avoid warning */
644 if (c->protocol.client.cdkey)
645 xfree((void *)c->protocol.client.cdkey); /* avoid warning */
646 if (c->protocol.d2.realminfo)
647 xfree((void *)c->protocol.d2.realminfo); /* avoid warning */
648 if (c->protocol.d2.charname)
649 xfree((void *)c->protocol.d2.charname); /* avoid warning */
650 if (c->protocol.chat.irc.ircline)
651 xfree((void *)c->protocol.chat.irc.ircline); /* avoid warning */
652 if (c->protocol.chat.irc.ircpass)
653 xfree((void *)c->protocol.chat.irc.ircpass); /* avoid warning */
654
655 if (c->protocol.wol.apgar)
656 xfree((void *)c->protocol.wol.apgar); /* avoid warning */
657
658 if (c->protocol.wol.anongame_player)
659 anongame_wol_destroy(c);
660
661 /* ADDED BY UNDYING SOULZZ 4/8/02 */
662 if (c->protocol.w3.w3_playerinfo)
663 xfree((void *)c->protocol.w3.w3_playerinfo); /* avoid warning */
664
665 if (c->protocol.w3.client_proof)
666 xfree((void *)c->protocol.w3.client_proof); /* avoid warning */
667
668 if (c->protocol.w3.server_proof)
669 xfree((void *)c->protocol.w3.server_proof); /* avoid warning */
670
671 if (c->protocol.bound)
672 c->protocol.bound->protocol.bound = NULL;
673
674 if (c->protocol.chat.ignore_count > 0)
675 {
676 if (!c->protocol.chat.ignore_list)
677 {
678 eventlog(eventlog_level_error, __FUNCTION__, "found NULL ignore_list with ignore_count={}", c->protocol.chat.ignore_count);
679 }
680 else
681 {
682 xfree(c->protocol.chat.ignore_list);
683 }
684 }
685
686 if (c->protocol.account)
687 {
688 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" logged out", c->socket.tcp_sock, conn_get_loggeduser(c));
689 //amadeo
690 #ifdef WIN32_GUI
691 guiOnUpdateUserList();
692 #endif
693 if (prefs_get_sync_on_logoff()) {
694 if (account_save(conn_get_account(c), FS_FORCE) < 0)
695 eventlog(eventlog_level_error, __FUNCTION__, "cannot sync account (sync_on_logoff)");
696 }
697
698 #ifdef WITH_LUA
699 lua_handle_user(c, NULL, NULL, luaevent_user_disconnect);
700 #endif
701
702 if (account_get_conn(c->protocol.account) == c) /* make sure you don't set this when already on new conn (relogin with same account) */
703 account_set_conn(c->protocol.account, NULL);
704 c->protocol.account = NULL; /* the account code will free the memory later */
705 }
706
707 /* logged user is no longer only for logged in users */
708 if (c->protocol.loggeduser) xfree((void*)c->protocol.loggeduser);
709
710 /* make sure the connection is closed */
711 if (c->socket.tcp_sock != -1) { /* -1 means that the socket was already closed by conn_close() */
712 fdwatch_del_fd(c->socket.fdw_idx);
713 psock_shutdown(c->socket.tcp_sock, PSOCK_SHUT_RDWR);
714 psock_close(c->socket.tcp_sock);
715 }
716 /* clear out the packet queues */
717 if (c->protocol.queues.inqueue) packet_del_ref(c->protocol.queues.inqueue);
718 queue_clear(&c->protocol.queues.outqueue);
719
720 // [zap-zero] 20020601
721 if (c->protocol.w3.routeconn) {
722 c->protocol.w3.routeconn->protocol.w3.routeconn = NULL;
723 if (c->protocol.w3.routeconn->protocol.cclass == conn_class_w3route)
724 conn_set_state(c->protocol.w3.routeconn, conn_state_destroy);
725 }
726
727 if (c->protocol.w3.anongame)
728 conn_destroy_anongame(c);
729
730 /* delete the conn from the dead list if its there, we dont check for error
731 * because connections may be destroyed without first setting state to destroy */
732 if (conn_dead) list_remove_data(conn_dead, c, (conn_or_dead_list) ? elem : &curr);
733 connarray_del_conn(c->protocol.sessionnum);
734
735 eventlog(eventlog_level_info, __FUNCTION__, "[{}] closed {} connection", c->socket.tcp_sock, classstr);
736
737 xfree(c);
738 }
739
740
741 extern int conn_match(t_connection const * c, char const * username)
742 {
743 if (!c)
744 {
745 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
746 return -1;
747 }
748 if (!username)
749 {
750 eventlog(eventlog_level_error, __FUNCTION__, "got NULL username");
751 return -1;
752 }
753
754 if (!c->protocol.account)
755 return 0;
756
757 return account_match(c->protocol.account, username);
758 }
759
760
761 extern t_conn_class conn_get_class(t_connection const * c)
762 {
763 if (!c)
764 {
765 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
766 return conn_class_none;
767 }
768
769 return c->protocol.cclass;
770 }
771
772
773 extern void conn_set_class(t_connection * c, t_conn_class cclass)
774 {
775 t_timer_data data;
776 unsigned long delta;
777 t_conn_class oldclass;
778
779 if (!c)
780 {
781 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
782 return;
783 }
784
785 if (c->protocol.cclass == cclass)
786 return;
787
788 oldclass = c->protocol.cclass;
789 c->protocol.cclass = cclass;
790
791 switch (cclass) {
792 case conn_class_bnet:
793 if (prefs_get_udptest_port() != 0)
794 conn_set_game_port(c, (unsigned short)prefs_get_udptest_port());
795 udptest_send(c);
796
797 /* remove any init timers */
798 if (oldclass == conn_class_init) timerlist_del_all_timers(c);
799 delta = prefs_get_latency();
800 data.n = delta;
801 if (timerlist_add_timer(c, now + (std::time_t)delta, conn_test_latency, data) < 0)
802 eventlog(eventlog_level_error, __FUNCTION__, "could not add timer");
803
804 eventlog(eventlog_level_debug, __FUNCTION__, "added latency check timer");
805 break;
806
807 case conn_class_w3route:
808 delta = prefs_get_latency();
809 data.n = delta;
810 if (timerlist_add_timer(c, now + (std::time_t)delta, conn_test_latency, data)<0)
811 eventlog(eventlog_level_error, __FUNCTION__, "could not add timer");
812 break;
813
814 case conn_class_bot:
815 case conn_class_telnet:
816 {
817 t_packet * rpacket;
818 if (cclass == conn_class_bot) {
819 if ((delta = prefs_get_nullmsg())>0) {
820 data.n = delta;
821 if (timerlist_add_timer(c, now + (std::time_t)delta, conn_send_nullmsg, data) < 0)
822 eventlog(eventlog_level_error, __FUNCTION__, "could not add timer");
823 }
824 }
825
826 /* remove any init timers */
827 if (oldclass == conn_class_init) timerlist_del_all_timers(c);
828 conn_send_issue(c);
829
830 if (!(rpacket = packet_create(packet_class_raw)))
831 eventlog(eventlog_level_error, __FUNCTION__, "could not create rpacket");
832 else {
833 packet_append_ntstring(rpacket, "Username: ");
834 conn_push_outqueue(c, rpacket);
835 packet_del_ref(rpacket);
836 }
837
838 break;
839 }
840
841 default:
842 /* remove any init timers */
843 if (oldclass == conn_class_init)
844 timerlist_del_all_timers(c);
845 break;
846 }
847
848 }
849
850
851 extern t_conn_state conn_get_state(t_connection const * c)
852 {
853 if (!c)
854 {
855 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
856 return conn_state_empty;
857 }
858
859 return c->protocol.state;
860 }
861
862
863 extern void conn_set_state(t_connection * c, t_conn_state state)
864 {
865 t_elem * elem;
866
867 if (!c)
868 {
869 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
870 return;
871 }
872
873 /* special case for destroying connections, add them to conn_dead list */
874 if (state == conn_state_destroy && c->protocol.state != conn_state_destroy) {
875 if (!conn_dead)
876 conn_dead = list_create();
877 list_append_data(conn_dead, c);
878 }
879 else if (state != conn_state_destroy && c->protocol.state == conn_state_destroy)
880 if (list_remove_data(conn_dead, c, &elem)) {
881 eventlog(eventlog_level_error, __FUNCTION__, "could not remove dead connection");
882 return;
883 }
884
885 c->protocol.state = state;
886 }
887
888 extern unsigned int conn_get_sessionkey(t_connection const * c)
889 {
890 if (!c)
891 {
892 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
893 return 0;
894 }
895
896 return c->protocol.sessionkey;
897 }
898
899
900 extern unsigned int conn_get_sessionnum(t_connection const * c)
901 {
902 if (!c)
903 {
904 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
905 return 0;
906 }
907
908 return c->protocol.sessionnum;
909 }
910
911
912 extern unsigned int conn_get_secret(t_connection const * c)
913 {
914 if (!c)
915 {
916 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
917 return 0;
918 }
919
920 return c->protocol.secret;
921 }
922
923
924 extern unsigned int conn_get_addr(t_connection const * c)
925 {
926 if (!c)
927 {
928 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
929 return 0;
930 }
931
932 return c->socket.tcp_addr;
933 }
934
935
936 extern unsigned short conn_get_port(t_connection const * c)
937 {
938 if (!c)
939 {
940 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
941 return 0;
942 }
943
944 return c->socket.tcp_port;
945 }
946
947
948 extern unsigned int conn_get_local_addr(t_connection const * c)
949 {
950 if (!c)
951 {
952 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
953 return 0;
954 }
955
956 return c->socket.local_addr;
957 }
958
959
960 extern unsigned short conn_get_local_port(t_connection const * c)
961 {
962 if (!c)
963 {
964 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
965 return 0;
966 }
967
968 return c->socket.local_port;
969 }
970
971
972 extern unsigned int conn_get_real_local_addr(t_connection const * c)
973 {
974 if (!c)
975 {
976 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
977 return 0;
978 }
979
980 return c->socket.real_local_addr;
981 }
982
983
984 extern unsigned short conn_get_real_local_port(t_connection const * c)
985 {
986 if (!c)
987 {
988 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
989 return 0;
990 }
991
992 return c->socket.real_local_port;
993 }
994
995
996 extern unsigned int conn_get_game_addr(t_connection const * c)
997 {
998 if (!c)
999 {
1000 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1001 return 0;
1002 }
1003
1004 return c->socket.udp_addr;
1005 }
1006
1007
1008 extern int conn_set_game_addr(t_connection * c, unsigned int game_addr)
1009 {
1010 if (!c)
1011 {
1012 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1013 return -1;
1014 }
1015
1016 c->socket.udp_addr = game_addr;
1017 return 0;
1018 }
1019
1020
1021 extern unsigned short conn_get_game_port(t_connection const * c)
1022 {
1023 if (!c)
1024 {
1025 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1026 return 0;
1027 }
1028
1029 return c->socket.udp_port;
1030 }
1031
1032
1033 extern int conn_set_game_port(t_connection * c, unsigned short game_port)
1034 {
1035 if (!c)
1036 {
1037 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1038 return -1;
1039 }
1040
1041 c->socket.udp_port = game_port;
1042 return 0;
1043 }
1044
1045
1046 extern void conn_set_host(t_connection * c, char const * host)
1047 {
1048 if (!c)
1049 {
1050 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1051 return;
1052 }
1053 if (!host)
1054 {
1055 eventlog(eventlog_level_error, __FUNCTION__, "got NULL host");
1056 return;
1057 }
1058
1059 if (c->protocol.client.host)
1060 xfree((void *)c->protocol.client.host); /* avoid warning */
1061 c->protocol.client.host = xstrdup(host);
1062 }
1063
1064
1065 extern void conn_set_user(t_connection * c, char const * user)
1066 {
1067 if (!c)
1068 {
1069 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1070 return;
1071 }
1072 if (!user)
1073 {
1074 eventlog(eventlog_level_error, __FUNCTION__, "got NULL user");
1075 return;
1076 }
1077
1078 if (c->protocol.client.user)
1079 xfree((void *)c->protocol.client.user); /* avoid warning */
1080 c->protocol.client.user = xstrdup(user);
1081 }
1082
1083
1084 extern void conn_set_owner(t_connection * c, char const * owner)
1085 {
1086 if (!c)
1087 {
1088 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1089 return;
1090 }
1091 if (!owner)
1092 {
1093 eventlog(eventlog_level_error, __FUNCTION__, "got NULL owner");
1094 return;
1095 }
1096
1097 if (c->protocol.client.owner)
1098 xfree((void *)c->protocol.client.owner); /* avoid warning */
1099 c->protocol.client.owner = xstrdup(owner);
1100 }
1101
1102 extern const char * conn_get_user(t_connection const * c)
1103 {
1104 if (!c) {
1105 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1106 return NULL;
1107 }
1108 return c->protocol.client.user;
1109 }
1110
1111 extern const char * conn_get_owner(t_connection const * c)
1112 {
1113 if (!c) {
1114 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1115 return NULL;
1116 }
1117 return c->protocol.client.owner;
1118 }
1119
1120 extern void conn_set_cdkey(t_connection * c, char const * cdkey)
1121 {
1122 if (!c)
1123 {
1124 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1125 return;
1126 }
1127 if (!cdkey)
1128 {
1129 eventlog(eventlog_level_error, __FUNCTION__, "got NULL cdkey");
1130 return;
1131 }
1132
1133 if (c->protocol.client.cdkey)
1134 xfree((void *)c->protocol.client.cdkey); /* avoid warning */
1135 c->protocol.client.cdkey = xstrdup(cdkey);
1136 }
1137
1138
1139 extern char const * conn_get_clientexe(t_connection const * c)
1140 {
1141 if (!c)
1142 {
1143 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1144 return NULL;
1145 }
1146
1147 if (!c->protocol.client.clientexe)
1148 return "";
1149 return c->protocol.client.clientexe;
1150 }
1151
1152
1153 extern void conn_set_clientexe(t_connection * c, char const * clientexe)
1154 {
1155 char const * temp;
1156
1157 if (!c)
1158 {
1159 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1160 return;
1161 }
1162 if (!clientexe)
1163 {
1164 eventlog(eventlog_level_error, __FUNCTION__, "got NULL clientexe");
1165 return;
1166 }
1167
1168 temp = xstrdup(clientexe);
1169 if (c->protocol.client.clientexe)
1170 xfree((void *)c->protocol.client.clientexe); /* avoid warning */
1171 c->protocol.client.clientexe = temp;
1172 }
1173
1174
1175 extern char const * conn_get_clientver(t_connection const * c)
1176 {
1177 if (!c)
1178 {
1179 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1180 return NULL;
1181 }
1182
1183 if (!c->protocol.client.clientver)
1184 return "";
1185 return c->protocol.client.clientver;
1186 }
1187
1188
1189 extern void conn_set_clientver(t_connection * c, char const * clientver)
1190 {
1191 char const * temp;
1192
1193 if (!c)
1194 {
1195 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1196 return;
1197 }
1198 if (!clientver)
1199 {
1200 eventlog(eventlog_level_error, __FUNCTION__, "got NULL clientver");
1201 return;
1202 }
1203
1204 temp = xstrdup(clientver);
1205 if (c->protocol.client.clientver)
1206 xfree((void *)c->protocol.client.clientver); /* avoid warning */
1207 c->protocol.client.clientver = temp;
1208 }
1209
1210
1211 extern t_tag conn_get_archtag(t_connection const * c)
1212 {
1213 if (!c) {
1214 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1215 return 0; /* unknown */
1216 }
1217
1218 return c->protocol.client.archtag;
1219 }
1220
1221
1222 extern void conn_set_archtag(t_connection * c, t_tag archtag)
1223 {
1224 if (!c) {
1225 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1226 return;
1227 }
1228 if (!tag_check_arch(archtag)) {
1229 eventlog(eventlog_level_error, __FUNCTION__, "got UNKNOWN archtag");
1230 return;
1231 }
1232 if (c->protocol.client.archtag != archtag)
1233 c->protocol.client.archtag = archtag;
1234 }
1235
1236
1237 extern t_tag conn_get_gamelang(t_connection const * c)
1238 {
1239 if (!c)
1240 {
1241 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1242 return 0;
1243 }
1244
1245 return c->protocol.client.gamelang;
1246 }
1247
1248
1249 extern void conn_set_gamelang(t_connection * c, t_tag gamelang)
1250 {
1251 char gamelang_str[5];
1252
1253 if (!c) {
1254 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1255 return;
1256 }
1257 if (!gamelang)
1258 return; /* only war3 & w3xp have gamelang */
1259
1260 bool found;
1261 language_find_by_tag(gamelang, found);
1262 if (!found)
1263 {
1264 eventlog(eventlog_level_warn, __FUNCTION__, "got UNKNOWN gamelang");
1265 return;
1266 }
1267 if (c->protocol.client.gamelang != gamelang)
1268 eventlog(eventlog_level_info, __FUNCTION__, "[{}] setting client gamelang to \"{}\"", conn_get_socket(c), tag_uint_to_str(gamelang_str, gamelang));
1269
1270 c->protocol.client.gamelang = gamelang;
1271 }
1272
1273
1274 extern t_clienttag conn_get_clienttag(t_connection const * c)
1275 {
1276 if (!c)
1277 {
1278 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1279 return CLIENTTAG_UNKNOWN_UINT;
1280 }
1281
1282 if (!c->protocol.client.clienttag)
1283 return CLIENTTAG_UNKNOWN_UINT;
1284 return c->protocol.client.clienttag;
1285 }
1286
1287
1288 extern t_clienttag conn_get_fake_clienttag(t_connection const * c)
1289 {
1290 char const * clienttag;
1291 t_account * account;
1292
1293 if (!c)
1294 {
1295 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1296 return 0; /* unknown */
1297 }
1298
1299 account = conn_get_account(c);
1300 if (account) /* BITS remote connections don't need to have an account */
1301 if ((clienttag = account_get_strattr(account, "BNET\\fakeclienttag")))
1302 return tag_str_to_uint(clienttag);
1303 return c->protocol.client.clienttag;
1304 }
1305
1306
1307 extern void conn_set_clienttag(t_connection * c, t_clienttag clienttag)
1308 {
1309 char clienttag_str[5];
1310
1311 if (!c) {
1312 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1313 return;
1314 }
1315 if (!tag_check_client(clienttag)) {
1316 eventlog(eventlog_level_error, __FUNCTION__, "got UNKNOWN clienttag \"{}\"", tag_uint_to_str(clienttag_str, clienttag));
1317 return;
1318 }
1319 if (c->protocol.client.clienttag != clienttag)
1320 {
1321 c->protocol.client.clienttag = clienttag;
1322 if (c->protocol.chat.channel)
1323 channel_update_userflags(c);
1324 }
1325
1326 }
1327
1328
1329 extern unsigned long conn_get_gameversion(t_connection const * c)
1330 {
1331 if (!c)
1332 {
1333 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1334 return 0;
1335 }
1336
1337 return c->protocol.client.gameversion;
1338 }
1339
1340
1341 extern int conn_set_gameversion(t_connection * c, unsigned long gameversion)
1342 {
1343 if (!c)
1344 {
1345 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1346 return -1;
1347 }
1348
1349 c->protocol.client.gameversion = gameversion;
1350 return 0;
1351 }
1352
1353
1354 extern unsigned long conn_get_checksum(t_connection const * c)
1355 {
1356 if (!c)
1357 {
1358 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1359 return 0;
1360 }
1361
1362 return c->protocol.client.checksum;
1363 }
1364
1365
1366 extern int conn_set_checksum(t_connection * c, unsigned long checksum)
1367 {
1368 if (!c)
1369 {
1370 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1371 return -1;
1372 }
1373
1374 c->protocol.client.checksum = checksum;
1375 return 0;
1376 }
1377
1378
1379 extern unsigned long conn_get_versionid(t_connection const * c)
1380 {
1381 if (!c)
1382 {
1383 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1384 return 0;
1385 }
1386
1387 return c->protocol.client.versionid;
1388 }
1389
1390
1391 extern int conn_set_versionid(t_connection * c, unsigned long versionid)
1392 {
1393 if (!c)
1394 {
1395 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1396 return -1;
1397 }
1398
1399 c->protocol.client.versionid = versionid;
1400 return 0;
1401 }
1402
1403
1404 extern int conn_get_tzbias(t_connection const * c)
1405 {
1406 if (!c)
1407 {
1408 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1409 return 0;
1410 }
1411
1412 return c->protocol.client.tzbias;
1413 }
1414
1415
1416 extern void conn_set_tzbias(t_connection * c, int tzbias)
1417 {
1418 if (!c)
1419 {
1420 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1421 return;
1422 }
1423
1424 c->protocol.client.tzbias = tzbias;
1425 }
1426
1427
1428 static void conn_set_account(t_connection * c, t_account * account)
1429 {
1430 t_connection * other;
1431 char const * tname;
1432
1433 if (!c)
1434 {
1435 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1436 return;
1437 }
1438 if (!account)
1439 {
1440 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1441 return;
1442 }
1443
1444 if ((other = connlist_find_connection_by_accountname((tname = account_get_name(account)))))
1445 {
1446 eventlog(eventlog_level_info, __FUNCTION__, "[{}] forcing logout of previous login for \"{}\"", conn_get_socket(c), tname);
1447 conn_set_state(other, conn_state_destroy);
1448 }
1449
1450 c->protocol.account = account;
1451 c->protocol.state = conn_state_loggedin;
1452 account_set_conn(account, c);
1453 {
1454 char const * flagstr;
1455
1456 if ((flagstr = account_get_strattr(account, "BNET\\flags\\initial")))
1457 conn_add_flags(c, std::strtoul(flagstr, NULL, 0));
1458 }
1459
1460 account_set_ll_time(c->protocol.account, (unsigned int)now);
1461 account_set_ll_owner(c->protocol.account, c->protocol.client.owner);
1462 account_set_ll_clienttag(c->protocol.account, c->protocol.client.clienttag);
1463 account_set_ll_ip(c->protocol.account, addr_num_to_ip_str(c->socket.tcp_addr));
1464
1465 if (c->protocol.client.host)
1466 {
1467 xfree((void *)c->protocol.client.host); /* avoid warning */
1468 c->protocol.client.host = NULL;
1469 }
1470 if (c->protocol.client.user)
1471 {
1472 xfree((void *)c->protocol.client.user); /* avoid warning */
1473 c->protocol.client.user = NULL;
1474 }
1475 if (c->protocol.client.clientexe)
1476 {
1477 xfree((void *)c->protocol.client.clientexe); /* avoid warning */
1478 c->protocol.client.clientexe = NULL;
1479 }
1480 if (c->protocol.client.owner)
1481 {
1482 xfree((void *)c->protocol.client.owner); /* avoid warning */
1483 c->protocol.client.owner = NULL;
1484 }
1485 if (c->protocol.client.cdkey)
1486 {
1487 xfree((void *)c->protocol.client.cdkey); /* avoid warning */
1488 c->protocol.client.cdkey = NULL;
1489 }
1490
1491 clanmember_set_online(c);
1492
1493 totalcount++;
1494
1495 watchlist->dispatch(c->protocol.account, NULL, c->protocol.client.clienttag, Watch::ET_login);
1496
1497 return;
1498 }
1499
1500
1501 extern void conn_login(t_connection *c, t_account *a, const char *loggeduser)
1502 {
1503 assert(c != NULL);
1504 assert(a != NULL);
1505 assert(loggeduser != NULL);
1506
1507 conn_set_account(c, a);
1508 if (std::strcmp(conn_get_loggeduser(c), loggeduser))
1509 conn_set_loggeduser(c, loggeduser);
1510 }
1511
1512
1513 extern t_account * conn_get_account(t_connection const * c)
1514 {
1515 if (!c)
1516 {
1517 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1518 return NULL;
1519 }
1520
1521 return c->protocol.account;
1522 }
1523
1524
1525 extern int conn_set_loggeduser(t_connection * c, char const * username)
1526 {
1527 const char * temp;
1528
1529 assert(c != NULL);
1530 assert(username != NULL);
1531
1532 if (username[0] != '#')
1533 temp = xstrdup(username);
1534 else {
1535 unsigned int userid = 0;
1536 str_to_uint(&username[1], &userid);
1537 if (userid != 0)
1538 {
1539 if (prefs_get_account_force_username())
1540 {
1541 t_account* account = accountlist_find_account_by_uid(userid);
1542 temp = xstrdup(account_get_name(account));
1543 }
1544 else
1545 temp = xstrdup(std::string("#" + userid).c_str());
1546 }
1547 else
1548 { //theoretically this should never happen...
1549 eventlog(eventlog_level_error, __FUNCTION__, "got invalid numeric uid \"{}\"", username);
1550 // set value that would have been set prior to this bugfix...
1551 temp = xstrdup(username);
1552 }
1553 }
1554 if (c->protocol.loggeduser) xfree((void*)c->protocol.loggeduser);
1555
1556 c->protocol.loggeduser = temp;
1557
1558 return 0;
1559 }
1560
1561
1562 extern char const * conn_get_loggeduser(t_connection const * c)
1563 {
1564 assert(c != NULL);
1565
1566 if (!c->protocol.loggeduser && c->protocol.account)
1567 return account_get_name(c->protocol.account);
1568 return c->protocol.loggeduser;
1569 }
1570
1571
1572 extern unsigned int conn_get_flags(t_connection const * c)
1573 {
1574 if (!c)
1575 {
1576 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1577 return 0;
1578 }
1579
1580 return c->protocol.flags;
1581 }
1582
1583
1584 extern int conn_set_flags(t_connection * c, unsigned int flags)
1585 {
1586 if (!c) {
1587 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1588 return -1;
1589 }
1590
1591 if (flags != c->protocol.flags) {
1592 c->protocol.flags = flags;
1593 if (c->protocol.chat.channel) channel_update_userflags(c);
1594 }
1595
1596 return 0;
1597 }
1598
1599
1600 extern void conn_add_flags(t_connection * c, unsigned int flags)
1601 {
1602 unsigned int oldflags;
1603
1604 if (!c)
1605 {
1606 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1607 return;
1608 }
1609 oldflags = c->protocol.flags;
1610 c->protocol.flags |= flags;
1611
1612 if (oldflags != c->protocol.flags && c->protocol.chat.channel)
1613 channel_update_userflags(c);
1614 }
1615
1616
1617 extern void conn_del_flags(t_connection * c, unsigned int flags)
1618 {
1619 unsigned int oldflags;
1620
1621 if (!c)
1622 {
1623 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1624 return;
1625 }
1626 oldflags = c->protocol.flags;
1627 c->protocol.flags &= ~flags;
1628
1629 if (oldflags != c->protocol.flags && c->protocol.chat.channel)
1630 channel_update_userflags(c);
1631 }
1632
1633
1634 extern unsigned int conn_get_latency(t_connection const * c)
1635 {
1636 if (!c)
1637 {
1638 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1639 return 0;
1640 }
1641
1642 return c->protocol.latency;
1643 }
1644
1645
1646 extern void conn_set_latency(t_connection * c, unsigned int ms)
1647 {
1648 if (!c)
1649 {
1650 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1651 return;
1652 }
1653
1654
1655 if (c->protocol.latency != ms)
1656 {
1657 c->protocol.latency = ms;
1658
1659 if (c->protocol.chat.channel)
1660 channel_update_latency(c);
1661 }
1662 }
1663
1664
1665 extern char const * conn_get_awaystr(t_connection const * c)
1666 {
1667 if (!c)
1668 {
1669 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1670 return NULL;
1671 }
1672
1673 return c->protocol.chat.away;
1674 }
1675
1676
1677 extern int conn_set_awaystr(t_connection * c, char const * away)
1678 {
1679 if (!c)
1680 {
1681 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1682 return -1;
1683 }
1684
1685 if (c->protocol.chat.away)
1686 xfree((void *)c->protocol.chat.away); /* avoid warning */
1687 if (!away)
1688 c->protocol.chat.away = NULL;
1689 else
1690 c->protocol.chat.away = xstrdup(away);
1691
1692 return 0;
1693 }
1694
1695
1696 extern char const * conn_get_dndstr(t_connection const * c)
1697 {
1698 if (!c)
1699 {
1700 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1701 return NULL;
1702 }
1703
1704 return c->protocol.chat.dnd;
1705 }
1706
1707
1708 extern int conn_set_dndstr(t_connection * c, char const * dnd)
1709 {
1710 if (!c)
1711 {
1712 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1713 return -1;
1714 }
1715
1716 if (c->protocol.chat.dnd)
1717 xfree((void *)c->protocol.chat.dnd); /* avoid warning */
1718 if (!dnd)
1719 c->protocol.chat.dnd = NULL;
1720 else
1721 c->protocol.chat.dnd = xstrdup(dnd);
1722
1723 return 0;
1724 }
1725
1726
1727 extern int conn_add_ignore(t_connection * c, t_account * account)
1728 {
1729 t_account * * newlist;
1730 t_connection *dest_c;
1731
1732 if (!c) {
1733 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1734 return -1;
1735 }
1736
1737 if (!account) {
1738 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1739 return -1;
1740 }
1741
1742 newlist = (t_account**)xrealloc(c->protocol.chat.ignore_list, sizeof(t_account const *)*(c->protocol.chat.ignore_count + 1));
1743 newlist[c->protocol.chat.ignore_count++] = account;
1744 c->protocol.chat.ignore_list = newlist;
1745
1746 dest_c = account_get_conn(account);
1747 if (dest_c) {
1748 t_message *message;
1749
1750 message = message_create(message_type_userflags, dest_c, NULL);
1751 if (!message) return 0;
1752 message_send(message, c);
1753 message_destroy(message);
1754 }
1755
1756 return 0;
1757 }
1758
1759
1760 extern int conn_del_ignore(t_connection * c, t_account const * account)
1761 {
1762 t_account * * newlist;
1763 t_account * temp;
1764 unsigned int i;
1765
1766 if (!c)
1767 {
1768 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1769 return -1;
1770 }
1771 if (!account)
1772 {
1773 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1774 return -1;
1775 }
1776
1777 for (i = 0; i < c->protocol.chat.ignore_count; i++)
1778 if (c->protocol.chat.ignore_list[i] == account)
1779 break;
1780 if (i == c->protocol.chat.ignore_count)
1781 return -1; /* not in list */
1782
1783 /* swap entry to be deleted with last entry */
1784 temp = c->protocol.chat.ignore_list[c->protocol.chat.ignore_count - 1];
1785 c->protocol.chat.ignore_list[c->protocol.chat.ignore_count - 1] = c->protocol.chat.ignore_list[i];
1786 c->protocol.chat.ignore_list[i] = temp;
1787
1788 if (c->protocol.chat.ignore_count == 1) /* some realloc()s are buggy */
1789 {
1790 xfree(c->protocol.chat.ignore_list);
1791 newlist = NULL;
1792 }
1793 else
1794 newlist = (t_account**)xrealloc(c->protocol.chat.ignore_list, sizeof(t_account const *)*(c->protocol.chat.ignore_count - 1));
1795
1796 c->protocol.chat.ignore_count--;
1797 c->protocol.chat.ignore_list = newlist;
1798
1799 return 0;
1800 }
1801
1802
1803 extern int conn_add_watch(t_connection * c, t_account * account, t_clienttag clienttag)
1804 {
1805 if (!c)
1806 {
1807 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1808 return -1;
1809 }
1810
1811 watchlist->add(c, account, clienttag, Watch::ET_login | Watch::ET_logout | Watch::ET_joingame | Watch::ET_leavegame);
1812 return 0;
1813 }
1814
1815
1816 extern int conn_del_watch(t_connection * c, t_account * account, t_clienttag clienttag)
1817 {
1818 if (!c)
1819 {
1820 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1821 return -1;
1822 }
1823
1824 if (watchlist->del(c, account, clienttag, Watch::ET_login | Watch::ET_logout | Watch::ET_joingame | Watch::ET_leavegame) < 0)
1825 return -1;
1826 return 0;
1827 }
1828
1829
1830 extern int conn_check_ignoring(t_connection const * c, char const * me)
1831 {
1832 unsigned int i;
1833 t_account * temp;
1834
1835 if (!c)
1836 {
1837 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1838 return -1;
1839 }
1840
1841 if (!me || !(temp = accountlist_find_account(me)))
1842 return -1;
1843
1844 if (c->protocol.chat.ignore_list)
1845 for (i = 0; i < c->protocol.chat.ignore_count; i++)
1846 if (c->protocol.chat.ignore_list[i] == temp)
1847 return 1;
1848
1849 return 0;
1850 }
1851
1852
1853 extern t_channel * conn_get_channel(t_connection const * c)
1854 {
1855 if (!c)
1856 {
1857 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1858 return NULL;
1859 }
1860
1861 return c->protocol.chat.channel;
1862 }
1863
1864
1865 extern int conn_set_channel_var(t_connection * c, t_channel * channel)
1866 {
1867 if (!c)
1868 {
1869 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1870 return -1;
1871 }
1872 c->protocol.chat.channel = channel;
1873 return 0;
1874 }
1875
1876
1877 extern int conn_set_channel(t_connection * c, char const * channelname)
1878 {
1879 t_channel * channel;
1880 t_channel * oldchannel;
1881 t_account * acc;
1882 t_elem * curr;
1883 int clantag = 0;
1884 t_clan * clan = NULL;
1885 t_clanmember * member = NULL;
1886 unsigned int created;
1887
1888 if (!c)
1889 {
1890 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1891 return -1;
1892 }
1893
1894 acc = c->protocol.account;
1895
1896 if (!acc)
1897 {
1898 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
1899 return -1;
1900 }
1901
1902 if (!channelname)
1903 {
1904 eventlog(eventlog_level_error, __FUNCTION__, "got NULL channelname");
1905 return -1;
1906 }
1907
1908 oldchannel = c->protocol.chat.channel;
1909
1910 channel = channellist_find_channel_by_name(channelname, conn_get_country(c), realm_get_name(conn_get_realm(c)));
1911
1912 if (channel && (channel == oldchannel))
1913 return 0;
1914
1915 if ((strncasecmp(channelname, "clan ", 5) == 0) && (std::strlen(channelname) < 10))
1916 clantag = str_to_clantag(&channelname[5]);
1917
1918 if ((clantag) && !((account_get_auth_admin(acc, channelname) == 1) || (account_get_auth_admin(acc, NULL) == 1))) {
1919 /* PELISH: Admins should be able to join any channel */
1920 clan = account_get_clan(acc);
1921 if ((!clan) || (clan_get_clantag(clan) != clantag)) {
1922 if (!channel)
1923 {
1924 message_send_text(c, message_type_error, c, std::string("Unable to join channel " + std::string(channelname) + ", there is no member of that clan in the channel!").c_str());
1925
1926 if (conn_get_game(c) || c->protocol.chat.channel == NULL)
1927 {
1928 // FIXME: This is not tested to be according to battle.net!!
1929 // This is fix for empty clan channels with preventing to join CHANNEL_NAME_BANNED when is used _handle_join_command
1930 message_send_text(c, message_type_error, c, std::string("You have been redirected to " + std::string(CHANNEL_NAME_BANNED)).c_str());
1931 channel = channellist_find_channel_by_name(CHANNEL_NAME_BANNED, conn_get_country(c), realm_get_name(conn_get_realm(c)));
1932 }
1933 else
1934 return 0;
1935 }
1936 else
1937 {
1938 t_clan * ch_clan;
1939 if ((ch_clan = clanlist_find_clan_by_clantag(clantag)) && (clan_get_channel_type(ch_clan) == 1))
1940 {
1941 message_send_text(c, message_type_error, c, localize(c, "This is a private clan channel, unable to join!"));
1942 return 0;
1943 }
1944 }
1945 }
1946 else {
1947 if ((clan) && (clan_get_clantag(clan) == clantag) && (member = account_get_clanmember(acc))) {
1948 if (clanmember_get_status(member) >= CLAN_SHAMAN)
1949 /* PELISH: Giving tmpOP to SHAMAN and CHIEFTAIN on clanchannel */
1950 conn_set_tmpOP_channel(c, channelname);
1951 }
1952 }
1953 }
1954
1955 if (c->protocol.chat.channel)
1956 conn_part_channel(c);
1957
1958 if (channel)
1959 {
1960 if (channel_check_banning(channel, c))
1961 {
1962 message_send_text(c, message_type_error, c, localize(c, "You are banned from that channel."));
1963 return -1;
1964 }
1965
1966 if ((account_get_auth_admin(acc, NULL) != 1) && (account_get_auth_admin(acc, channelname) != 1) &&
1967 (account_get_auth_operator(acc, NULL) != 1) && (account_get_auth_operator(acc, channelname) != 1) &&
1968 (channel_get_max(channel) == 0))
1969 {
1970 message_send_text(c, message_type_error, c, localize(c, "That channel is for Admins/Operators only."));
1971 return -1;
1972 }
1973
1974 if ((account_get_auth_admin(acc, NULL) != 1) && (account_get_auth_admin(acc, channelname) != 1) &&
1975 (account_get_auth_operator(acc, NULL) != 1) && (account_get_auth_operator(acc, channelname) != 1) &&
1976 (channel_get_max(channel) != -1) && (channel_get_curr(channel) >= channel_get_max(channel)))
1977 {
1978 message_send_text(c, message_type_error, c, localize(c, "The channel is currently full."));
1979 return -1;
1980 }
1981 }
1982
1983 if (conn_set_joingamewhisper_ack(c, 0) < 0)
1984 eventlog(eventlog_level_error, __FUNCTION__, "Unable to reset conn_set_joingamewhisper_ack flag");
1985
1986 if (conn_set_leavegamewhisper_ack(c, 0) < 0)
1987 eventlog(eventlog_level_error, __FUNCTION__, "Unable to reset conn_set_leavegamewhisper_ack flag");
1988
1989 /* if you're entering a channel, make sure they didn't exit a game without telling us */
1990 if (c->protocol.game)
1991 {
1992 game_del_player(conn_get_game(c), c);
1993 c->protocol.game = NULL;
1994 }
1995
1996 created = 0;
1997
1998 if (!channel)
1999 {
2000 if (clantag)
2001 channel = channel_create(channelname, channelname, 0, 0, 1, 1, prefs_get_chanlog(), NULL, NULL, (prefs_get_maxusers_per_channel() > 0) ? prefs_get_maxusers_per_channel() : -1, 0, 1, 0);
2002 else
2003 channel = channel_create(channelname, channelname, 0, 0, 1, 1, prefs_get_chanlog(), NULL, NULL, (prefs_get_maxusers_per_channel() > 0) ? prefs_get_maxusers_per_channel() : -1, 0, 0, 0);
2004 if (!channel)
2005 {
2006 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create channel on join \"{}\"", conn_get_socket(c), channelname);
2007 return -1;
2008 }
2009 created = 1;
2010 }
2011
2012 c->protocol.chat.channel = channel;
2013
2014 if (channel_add_connection(channel, c) < 0)
2015 {
2016 if (created)
2017 channel_destroy(channel, &curr);
2018 c->protocol.chat.channel = NULL;
2019 return -1;
2020 }
2021
2022 eventlog(eventlog_level_info, __FUNCTION__, "[{}] joined channel \"{}\"", conn_get_socket(c), channel_get_name(c->protocol.chat.channel));
2023 conn_send_welcome(c);
2024
2025 if (c->protocol.chat.channel && (channel_get_flags(c->protocol.chat.channel) & channel_flags_thevoid))
2026 message_send_text(c, message_type_info, c, localize(c, "This channel does not have chat privileges."));
2027 if (clantag && clan && (clan_get_clantag(clan) == clantag))
2028 {
2029 char msgtemp[MAX_MESSAGE_LEN];
2030 std::sprintf(msgtemp, "%s", clan_get_motd(clan));
2031 message_send_text(c, message_type_info, c, msgtemp);
2032 }
2033
2034
2035 if (conn_is_irc_variant(c) == 0)
2036 {
2037 class_topic Topic;
2038 Topic.display(c, std::string(channel_get_name(c->protocol.chat.channel)));
2039 }
2040
2041 if (c->protocol.chat.channel && (channel_get_flags(c->protocol.chat.channel) & channel_flags_moderated))
2042 message_send_text(c, message_type_error, c, localize(c, "This channel is moderated."));
2043
2044 if (c->protocol.chat.channel != oldchannel)
2045 clanmember_on_change_status_by_connection(c);
2046
2047 return 0;
2048 }
2049
2050 extern int conn_part_channel(t_connection * c)
2051 {
2052 if (!c) {
2053 ERROR0("got NULL connection");
2054 return -1;
2055 }
2056
2057 if (!c->protocol.chat.channel) {
2058 ERROR0("client want to PART channel but is not in channel! c->protocol.chat.channel == NULL");
2059 return -1;
2060 }
2061
2062 channel_del_connection(c->protocol.chat.channel, c, message_type_part, NULL);
2063 c->protocol.chat.channel = NULL;
2064
2065 return 0;
2066 }
2067
2068 extern int conn_kick_channel(t_connection * c, char const * text)
2069 {
2070 /* According to RFC2812 in *text is reason, wol sends by text op nickname */
2071
2072 if (!c) {
2073 ERROR0("got NULL connection");
2074 return -1;
2075 }
2076
2077 if (!c->protocol.chat.channel) {
2078 ERROR0("client want to KICK channel but is not in channel! c->protocol.chat.channel == NULL");
2079 return -1;
2080 }
2081
2082 channel_del_connection(c->protocol.chat.channel, c, message_type_kick, text);
2083 c->protocol.chat.channel = NULL;
2084
2085 return 0;
2086 }
2087
2088 extern int conn_quit_channel(t_connection * c, char const * text)
2089 {
2090 /* According to RFC2812 in *text is user difinable QUIT message i.e.: Gone to have lunch */
2091
2092 if (!c) {
2093 ERROR0("got NULL connection");
2094 return -1;
2095 }
2096
2097 if (!c->protocol.chat.channel) {
2098 ERROR0("client want to QUIT channel but is not in channel! c->protocol.chat.channel == NULL");
2099 return -1;
2100 }
2101
2102 channel_del_connection(c->protocol.chat.channel, c, message_type_quit, text);
2103 c->protocol.chat.channel = NULL;
2104
2105 return 0;
2106 }
2107
2108 extern t_game * conn_get_game(t_connection const * c)
2109 {
2110 if (!c)
2111 {
2112 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2113 return NULL;
2114 }
2115
2116 return c->protocol.game;
2117 }
2118
2119
2120 extern int conn_set_game(t_connection * c, char const * gamename, char const * gamepass, char const * gameinfo, t_game_type type, int version)
2121 /*
2122 * If game not exists (create) version != 0 (called in handle_bnet.c, function _client_startgameX())
2123 * If game exists (join) version == 0 always (called in handle_bnet.c, function _client_joingame())
2124 * If game exists (join) gameinfo == "" (called in handle_bnet.c, function _client_joingame())
2125 * [KWS]
2126 */
2127 {
2128 if (!c) {
2129 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2130 return 0;
2131 }
2132
2133 if (c->protocol.game) {
2134 if (gamename) {
2135 if (strcasecmp(gamename, game_get_name(c->protocol.game)))
2136 eventlog(eventlog_level_error, __FUNCTION__, "[{}] tried to join a new game \"{}\" while already in a game \"{}\"!", conn_get_socket(c), gamename, game_get_name(c->protocol.game));
2137 else return 0;
2138 }
2139 game_del_player(conn_get_game(c), c);
2140 c->protocol.game = NULL;
2141 }
2142
2143 if (gamename) {
2144 if (!(c->protocol.game = gamelist_find_game_available(gamename, c->protocol.client.clienttag, type))
2145 && !gamelist_find_game_available(gamename, c->protocol.client.clienttag, game_type_all)) {
2146 /* do not allow creation of games with same name of same clienttag when game is not started or done */
2147 // create game with initial values
2148 c->protocol.game = game_create(gamename, gamepass, gameinfo, type, version, c->protocol.client.clienttag, conn_get_gameversion(c));
2149
2150 if (c->protocol.game && conn_get_realm(c) && conn_get_charname(c)) {
2151 game_set_realmname(c->protocol.game, realm_get_name(conn_get_realm(c)));
2152 realm_add_game_number(conn_get_realm(c), 1);
2153 send_d2cs_gameinforeq(c);
2154 }
2155 }
2156
2157 if (c->protocol.game) {
2158 // add new player to the game
2159 if (game_add_player(conn_get_game(c), gamepass, version, c) < 0) {
2160 c->protocol.game = NULL; // bad password or version #
2161 return -1;
2162 }
2163
2164 #ifdef WITH_LUA
2165 // handle game create when it's owner joins the game
2166 if (c == game_get_owner(c->protocol.game))
2167 lua_handle_game(c->protocol.game, NULL, luaevent_game_create);
2168 #endif
2169
2170 if (game_is_ladder(c->protocol.game)) {
2171 if (c == game_get_owner(c->protocol.game))
2172 message_send_text(c, message_type_info, c, localize(c, "Created ladder game"));
2173 else
2174 message_send_text(c, message_type_info, c, localize(c, "Joined ladder game"));
2175 }
2176 }
2177 }
2178 else c->protocol.game = NULL;
2179
2180 return 0;
2181 }
2182
2183 extern unsigned int conn_get_tcpaddr(t_connection * c)
2184 {
2185 if (!c)
2186 {
2187 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2188 return 0;
2189 }
2190
2191 return c->socket.tcp_addr;
2192 }
2193
2194
2195 extern t_packet * conn_get_in_queue(t_connection * c)
2196 {
2197 assert(c);
2198
2199 return c->protocol.queues.inqueue;
2200 }
2201
2202
2203 extern void conn_put_in_queue(t_connection * c, t_packet * packet)
2204 {
2205 assert(c);
2206
2207 c->protocol.queues.inqueue = packet;
2208 }
2209
2210
2211 extern unsigned int conn_get_in_size(t_connection const * c)
2212 {
2213 if (!c)
2214 {
2215 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2216 return 0;
2217 }
2218
2219 return c->protocol.queues.insize;
2220 }
2221
2222
2223 extern void conn_set_in_size(t_connection * c, unsigned int size)
2224 {
2225 if (!c)
2226 {
2227 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2228 return;
2229 }
2230
2231 c->protocol.queues.insize = size;
2232 }
2233
2234
2235 extern unsigned int conn_get_out_size(t_connection const * c)
2236 {
2237 if (!c)
2238 {
2239 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2240 return 0;
2241 }
2242 return c->protocol.queues.outsize;
2243 }
2244
2245
2246 extern void conn_set_out_size(t_connection * c, unsigned int size)
2247 {
2248 if (!c)
2249 {
2250 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2251 return;
2252 }
2253
2254 c->protocol.queues.outsize = size;
2255 }
2256
2257 extern int conn_push_outqueue(t_connection * c, t_packet * packet)
2258 {
2259 if (!c)
2260 {
2261 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2262 return -1;
2263 }
2264
2265 if (!packet)
2266 {
2267 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
2268 return -1;
2269 }
2270
2271 // Protection from hack attempt
2272 // Limit out queue packets due to it may cause memory leak with not enough memory program crash on a server machine
2273 t_queue ** q = &c->protocol.queues.outqueue;
2274 if (prefs_get_packet_limit() && queue_get_length((t_queue const * const *)q) > prefs_get_packet_limit())
2275 {
2276 queue_clear(q);
2277 conn_set_state(c, conn_state_destroy);
2278 eventlog(eventlog_level_error, __FUNCTION__, "outqueue reached limit of {} packets (hack attempt?)", prefs_get_packet_limit());
2279 return 0;
2280 }
2281
2282 queue_push_packet((t_queue * *)&c->protocol.queues.outqueue, packet);
2283 if (!c->protocol.queues.outsizep++) fdwatch_update_fd(c->socket.fdw_idx, fdwatch_type_read | fdwatch_type_write);
2284
2285 return 0;
2286 }
2287
2288 extern int conn_clear_outqueue(t_connection * c)
2289 {
2290 if (!c)
2291 {
2292 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2293 return -1;
2294 }
2295
2296 queue_clear(&c->protocol.queues.outqueue);
2297 return 0;
2298 }
2299
2300 extern t_packet * conn_peek_outqueue(t_connection * c)
2301 {
2302 if (!c)
2303 {
2304 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2305 return NULL;
2306 }
2307
2308 if (c->protocol.queues.outqueue)
2309 return queue_peek_packet((t_queue const * const *)&c->protocol.queues.outqueue);
2310 else return 0;
2311 }
2312
2313 extern t_packet * conn_pull_outqueue(t_connection * c)
2314 {
2315 if (!c)
2316 {
2317 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2318 return NULL;
2319 }
2320
2321 if (c->protocol.queues.outsizep) {
2322 if (!(--c->protocol.queues.outsizep)) fdwatch_update_fd(c->socket.fdw_idx, fdwatch_type_read);
2323 return queue_pull_packet((t_queue * *)&c->protocol.queues.outqueue);
2324 }
2325
2326 return NULL;
2327 }
2328
2329 extern char const * conn_get_username_real(t_connection const * c, char const * fn, unsigned int ln)
2330 {
2331 char const * result;
2332
2333 if (!c)
2334 {
2335 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection (from {}:{})", fn, ln);
2336 return NULL;
2337 }
2338
2339 if (!c->protocol.account)
2340 {
2341 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account (from {}:{})", fn, ln);
2342 return NULL;
2343 }
2344 result = account_get_name(c->protocol.account);
2345 if (result == NULL)
2346 eventlog(eventlog_level_error, __FUNCTION__, "returned previous error after being called by {}:{}", fn, ln);
2347
2348 return result;
2349 }
2350
2351
2352 extern char const * conn_get_chatname(t_connection const * c)
2353 {
2354 if (!c)
2355 {
2356 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2357 return NULL;
2358 }
2359
2360 if ((c->protocol.cclass == conn_class_bnet) && c->protocol.bound)
2361 {
2362 if (c->protocol.d2.character)
2363 return character_get_name(c->protocol.d2.character);
2364 if (c->protocol.bound->protocol.d2.character)
2365 return character_get_name(c->protocol.bound->protocol.d2.character);
2366 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got connection class {} bound to class {} without a character", conn_get_socket(c), conn_class_get_str(c->protocol.cclass), c->protocol.bound->protocol.cclass);
2367 }
2368 if (!c->protocol.account)
2369 return NULL; /* no name yet */
2370 return conn_get_loggeduser(c);
2371 }
2372
2373
2374 extern int conn_unget_chatname(t_connection const * c, char const * name)
2375 {
2376 if (!c)
2377 {
2378 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2379 return -1;
2380 }
2381
2382 if ((c->protocol.cclass == conn_class_bnet) && c->protocol.bound)
2383 return 0;
2384 return 0;
2385 }
2386
2387
2388 extern char const * conn_get_chatcharname(t_connection const * c, t_connection const * dst)
2389 {
2390 char const * accname;
2391 char * chatcharname;
2392
2393 if (!c)
2394 {
2395 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2396 return NULL;
2397 }
2398
2399 if (!c->protocol.account)
2400 return NULL; /* no name yet */
2401
2402 /* for D2 Users */
2403 accname = conn_get_loggeduser(c);
2404 if (!accname)
2405 return NULL;
2406
2407 if (dst && dst->protocol.d2.charname)
2408 {
2409 const char *mychar;
2410
2411 if (c->protocol.d2.charname) mychar = c->protocol.d2.charname;
2412 else mychar = "";
2413 chatcharname = (char*)xmalloc(std::strlen(accname) + 2 + std::strlen(mychar));
2414 std::sprintf(chatcharname, "%s*%s", mychar, accname);
2415 }
2416 else chatcharname = xstrdup(accname);
2417
2418 return chatcharname;
2419 }
2420
2421
2422 extern int conn_unget_chatcharname(t_connection const * c, char const * name)
2423 {
2424 if (!c)
2425 {
2426 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2427 return -1;
2428 }
2429 if (!name)
2430 {
2431 eventlog(eventlog_level_error, __FUNCTION__, "got NULL name");
2432 return -1;
2433 }
2434
2435 xfree((void *)name); /* avoid warning */
2436 return 0;
2437 }
2438
2439
2440 extern t_message_class conn_get_message_class(t_connection const * c, t_connection const * dst)
2441 {
2442 if (dst && dst->protocol.d2.charname) /* message to D2 user must be char*account */
2443 return message_class_charjoin;
2444
2445 return message_class_normal;
2446 }
2447
2448
2449 extern unsigned int conn_get_userid(t_connection const * c)
2450 {
2451 if (!c)
2452 {
2453 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2454 return 0;
2455 }
2456
2457 if (!c->protocol.account)
2458 {
2459 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
2460 return 0;
2461 }
2462
2463 return account_get_uid(c->protocol.account);
2464 }
2465
2466
2467 extern int conn_get_socket(t_connection const * c)
2468 {
2469 if (!c)
2470 {
2471 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2472 return -1;
2473 }
2474
2475 return c->socket.tcp_sock;
2476 }
2477
2478
2479 extern int conn_get_game_socket(t_connection const * c)
2480 {
2481 if (!c)
2482 {
2483 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2484 return -1;
2485 }
2486
2487 return c->socket.udp_sock;
2488 }
2489
2490
2491 extern int conn_set_game_socket(t_connection * c, int usock)
2492 {
2493 if (!c)
2494 {
2495 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2496 return -1;
2497 }
2498
2499 c->socket.udp_sock = usock;
2500 return 0;
2501 }
2502
2503 /* Player icon that displayed in a channel in all games (except Warcraft 3) */
2504 extern char const * conn_get_playerinfo(t_connection const * c)
2505 {
2506 t_account * account;
2507 static char playerinfo[MAX_PLAYERINFO_STR];
2508 t_clienttag clienttag;
2509 char revtag[5];
2510 char const * usericon;
2511
2512 if (!c)
2513 {
2514 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2515 return NULL;
2516 }
2517 if (!(account = conn_get_account(c)))
2518 {
2519 eventlog(eventlog_level_error, __FUNCTION__, "connection has no account");
2520 return NULL;
2521 }
2522
2523 if (!(clienttag = conn_get_fake_clienttag(c)))
2524 {
2525 eventlog(eventlog_level_error, __FUNCTION__, "connection has NULL fakeclienttag");
2526 return NULL;
2527 }
2528 tag_uint_to_revstr(revtag, clienttag);
2529
2530 // allow set icon to a user directly from the database (override default tag always if not null)
2531 if (usericon = account_get_user_icon(account, clienttag))
2532 std::sprintf(revtag, "%s", usericon);
2533 else if (clienttag == CLIENTTAG_BNCHATBOT_UINT)
2534 {
2535 std::strcpy(playerinfo, revtag); /* FIXME: what to return here? */
2536 }
2537 else if ((clienttag == CLIENTTAG_STARCRAFT_UINT) || (clienttag == CLIENTTAG_BROODWARS_UINT))
2538 {
2539 if (conn_get_versionid(c) <= 0x000000c7)
2540 {
2541 std::sprintf(playerinfo, "%s %u %d %u %u %u",
2542 revtag,
2543 account_get_ladder_rating(account, clienttag, ladder_id_normal),
2544 account_get_ladder_rank(account, clienttag, ladder_id_normal),
2545 account_get_normal_wins(account, clienttag),
2546 0, 0);
2547 }
2548 else
2549 {
2550 std::sprintf(playerinfo, "%s %u %d %u %u %u %u %u %u %s",
2551 revtag,
2552 account_get_ladder_rating(account, clienttag, ladder_id_normal),
2553 account_get_ladder_rank(account, clienttag, ladder_id_normal),
2554 account_get_normal_wins(account, clienttag),
2555 0, 0,
2556 account_get_ladder_high_rating(account, clienttag, ladder_id_normal),
2557 0, 0,
2558 revtag);
2559 }
2560 }
2561 else if (clienttag == CLIENTTAG_SHAREWARE_UINT)
2562 {
2563 std::sprintf(playerinfo, "%s %u %d %u %u %u",
2564 revtag,
2565 account_get_ladder_rating(account, clienttag, ladder_id_normal),
2566 account_get_ladder_rank(account, clienttag, ladder_id_normal),
2567 account_get_normal_wins(account, clienttag),
2568 0, 0);
2569 }
2570 else if (clienttag == CLIENTTAG_DIABLORTL_UINT)
2571 {
2572 std::sprintf(playerinfo, "%s %u %u %u %u %u %u %u %u %u",
2573 revtag,
2574 account_get_normal_level(account, clienttag),
2575 account_get_normal_class(account, clienttag),
2576 account_get_normal_diablo_kills(account, clienttag),
2577 account_get_normal_strength(account, clienttag),
2578 account_get_normal_magic(account, clienttag),
2579 account_get_normal_dexterity(account, clienttag),
2580 account_get_normal_vitality(account, clienttag),
2581 account_get_normal_gold(account, clienttag),
2582 0);
2583 }
2584 else if (clienttag == CLIENTTAG_DIABLOSHR_UINT)
2585 {
2586 std::sprintf(playerinfo, "%s %u %u %u %u %u %u %u %u %u",
2587 revtag,
2588 account_get_normal_level(account, clienttag),
2589 account_get_normal_class(account, clienttag),
2590 account_get_normal_diablo_kills(account, clienttag),
2591 account_get_normal_strength(account, clienttag),
2592 account_get_normal_magic(account, clienttag),
2593 account_get_normal_dexterity(account, clienttag),
2594 account_get_normal_vitality(account, clienttag),
2595 account_get_normal_gold(account, clienttag),
2596 0);
2597 }
2598 else if (clienttag == CLIENTTAG_WARCIIBNE_UINT)
2599 {
2600 unsigned int a, b;
2601
2602 a = account_get_ladder_rating(account, clienttag, ladder_id_normal);
2603 b = account_get_ladder_rating(account, clienttag, ladder_id_ironman);
2604
2605 std::sprintf(playerinfo, "%s %u %d %u %u %u %u %u %d",
2606 revtag,
2607 a,
2608 account_get_ladder_rank(account, clienttag, ladder_id_normal),
2609 account_get_normal_wins(account, clienttag),
2610 0,
2611 0,
2612 (a > b) ? a : b,
2613 b,
2614 account_get_ladder_rank(account, clienttag, ladder_id_ironman));
2615 }
2616 else if (clienttag == CLIENTTAG_DIABLO2DV_UINT || clienttag == CLIENTTAG_DIABLO2XP_UINT)
2617 {
2618 /* This sets portrait of character */
2619 if (!conn_get_realm(c) || !conn_get_realminfo(c))
2620 {
2621 std::strcpy(playerinfo, revtag);
2622 }
2623 else
2624 {
2625 std::strcpy(playerinfo, conn_get_realminfo(c));
2626 }
2627 }
2628 else
2629 std::strcpy(playerinfo, revtag); /* open char */
2630
2631 // if custom_icons is enabled then set a custom client tag by player rating
2632 // do not override user selected icon if it's not null
2633 if (!usericon && prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag))
2634 {
2635 if (t_icon_info * icon = customicons_get_icon_by_account(account, clienttag))
2636 std::snprintf(playerinfo, sizeof playerinfo, "%s", icon->icon_code);
2637 }
2638
2639 #ifdef WITH_LUA
2640 // change icon info from Lua
2641 if (const char * iconinfo = lua_handle_user_icon((t_connection*)c, playerinfo))
2642 return iconinfo;
2643 #endif
2644
2645 return playerinfo;
2646 }
2647
2648
2649 extern int conn_set_playerinfo(t_connection const * c, char const * playerinfo)
2650 {
2651 t_clienttag clienttag;
2652 char clienttag_str[5];
2653
2654 if (!c)
2655 {
2656 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2657 return -1;
2658 }
2659 if (!playerinfo)
2660 {
2661 eventlog(eventlog_level_error, __FUNCTION__, "got NULL playerinfo");
2662 return -1;
2663 }
2664 clienttag = c->protocol.client.clienttag;
2665
2666 if (clienttag == CLIENTTAG_DIABLORTL_UINT)
2667 {
2668 unsigned int level;
2669 unsigned int chclass;
2670 unsigned int diablo_kills;
2671 unsigned int strength;
2672 unsigned int magic;
2673 unsigned int dexterity;
2674 unsigned int vitality;
2675 unsigned int gold;
2676
2677 if (std::sscanf(playerinfo, "LTRD %u %u %u %u %u %u %u %u %*u",
2678 &level,
2679 &chclass,
2680 &diablo_kills,
2681 &strength,
2682 &magic,
2683 &dexterity,
2684 &vitality,
2685 &gold) != 8)
2686 {
2687 eventlog(eventlog_level_error, __FUNCTION__, "got bad playerinfo");
2688 return -1;
2689 }
2690
2691 account_set_normal_level(conn_get_account(c), clienttag, level);
2692 account_set_normal_class(conn_get_account(c), clienttag, chclass);
2693 account_set_normal_diablo_kills(conn_get_account(c), clienttag, diablo_kills);
2694 account_set_normal_strength(conn_get_account(c), clienttag, strength);
2695 account_set_normal_magic(conn_get_account(c), clienttag, magic);
2696 account_set_normal_dexterity(conn_get_account(c), clienttag, dexterity);
2697 account_set_normal_vitality(conn_get_account(c), clienttag, vitality);
2698 account_set_normal_gold(conn_get_account(c), clienttag, gold);
2699 }
2700 else if (clienttag == CLIENTTAG_DIABLOSHR_UINT)
2701 {
2702 unsigned int level;
2703 unsigned int chclass;
2704 unsigned int diablo_kills;
2705 unsigned int strength;
2706 unsigned int magic;
2707 unsigned int dexterity;
2708 unsigned int vitality;
2709 unsigned int gold;
2710
2711 if (std::sscanf(playerinfo, "RHSD %u %u %u %u %u %u %u %u %*u",
2712 &level,
2713 &chclass,
2714 &diablo_kills,
2715 &strength,
2716 &magic,
2717 &dexterity,
2718 &vitality,
2719 &gold) != 8)
2720 {
2721 eventlog(eventlog_level_error, __FUNCTION__, "got bad playerinfo");
2722 return -1;
2723 }
2724
2725 account_set_normal_level(conn_get_account(c), clienttag, level);
2726 account_set_normal_class(conn_get_account(c), clienttag, chclass);
2727 account_set_normal_diablo_kills(conn_get_account(c), clienttag, diablo_kills);
2728 account_set_normal_strength(conn_get_account(c), clienttag, strength);
2729 account_set_normal_magic(conn_get_account(c), clienttag, magic);
2730 account_set_normal_dexterity(conn_get_account(c), clienttag, dexterity);
2731 account_set_normal_vitality(conn_get_account(c), clienttag, vitality);
2732 account_set_normal_gold(conn_get_account(c), clienttag, gold);
2733 }
2734 else if (clienttag == CLIENTTAG_DIABLO2DV_UINT)
2735 {
2736 /* not much to do */ /* FIXME: get char name here? */
2737 eventlog(eventlog_level_trace, __FUNCTION__, "[{}] playerinfo request for client \"{}\" playerinfo=\"{}\"", conn_get_socket(c), tag_uint_to_str(clienttag_str, clienttag), playerinfo);
2738 }
2739 else if (clienttag == CLIENTTAG_DIABLO2XP_UINT)
2740 {
2741 /* in playerinfo we get strings of the form "Realmname,charname" */
2742 eventlog(eventlog_level_trace, __FUNCTION__, "[{}] playerinfo request for client \"{}\" playerinfo=\"{}\"", conn_get_socket(c), tag_uint_to_str(clienttag_str, clienttag), playerinfo);
2743 }
2744 else
2745 {
2746 eventlog(eventlog_level_warn, __FUNCTION__, "setting playerinfo for client \"{}\" not supported (playerinfo=\"{}\")", tag_uint_to_str(clienttag_str, clienttag), playerinfo);
2747 return -1;
2748 }
2749
2750 return 0;
2751 }
2752
2753
2754 extern char const * conn_get_realminfo(t_connection const * c)
2755 {
2756 if (!c)
2757 {
2758 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2759 return NULL;
2760 }
2761 return c->protocol.d2.realminfo;
2762 }
2763
2764
2765 extern int conn_set_realminfo(t_connection * c, char const * realminfo)
2766 {
2767 char const * temp;
2768
2769 if (!c)
2770 {
2771 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2772 return -1;
2773 }
2774
2775 if (realminfo)
2776 temp = xstrdup(realminfo);
2777 else
2778 temp = NULL;
2779
2780 if (c->protocol.d2.realminfo) /* if it was set before, free it now */
2781 xfree((void *)c->protocol.d2.realminfo); /* avoid warning */
2782 c->protocol.d2.realminfo = temp;
2783 return 0;
2784 }
2785
2786
2787 extern char const * conn_get_charname(t_connection const * c)
2788 {
2789 if (!c)
2790 {
2791 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2792 return NULL;
2793 }
2794 return c->protocol.d2.charname;
2795 }
2796
2797
2798 extern int conn_set_charname(t_connection * c, char const * charname)
2799 {
2800 char const * temp;
2801
2802 if (!c)
2803 {
2804 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2805 return -1;
2806 }
2807
2808 if (charname)
2809 temp = xstrdup(charname);
2810 else
2811 temp = charname;
2812
2813 if (c->protocol.d2.charname) /* free it, if it was previously set */
2814 xfree((void *)c->protocol.d2.charname); /* avoid warning */
2815 c->protocol.d2.charname = temp;
2816 return 0;
2817 }
2818
2819
2820 extern int conn_set_idletime(t_connection * c)
2821 {
2822 if (!c)
2823 {
2824 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2825 return -1;
2826 }
2827
2828 c->protocol.chat.last_message = now;
2829 return 0;
2830 }
2831
2832
2833 extern unsigned int conn_get_idletime(t_connection const * c)
2834 {
2835 if (!c)
2836 {
2837 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2838 return 0;
2839 }
2840
2841 return (unsigned int)std::difftime(now, c->protocol.chat.last_message);
2842 }
2843
2844
2845 extern t_realm * conn_get_realm(t_connection const * c)
2846 {
2847 if (!c)
2848 {
2849 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2850 return NULL;
2851 }
2852
2853 return c->protocol.d2.realm;
2854 }
2855
2856
2857 extern int conn_set_realm(t_connection * c, t_realm * realm)
2858 {
2859 if (!c)
2860 {
2861 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2862 return -1;
2863 }
2864
2865 if (c->protocol.d2.realm)
2866 realm_put(c->protocol.d2.realm, &c->protocol.d2.realm_regref);
2867
2868 if (!realm)
2869 c->protocol.d2.realm = NULL;
2870 else
2871 {
2872 c->protocol.d2.realm = realm_get(realm, &c->protocol.d2.realm_regref);
2873 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] set to \"{}\"", conn_get_socket(c), realm_get_name(realm));
2874 }
2875
2876 return 0;
2877 }
2878
2879 extern int conn_set_realm_cb(void *data, void *newref)
2880 {
2881 t_connection *c = (t_connection*)data;
2882 t_realm *newrealm = (t_realm*)newref;
2883
2884 assert(c->protocol.d2.realm); /* this should never be NULL here */
2885
2886 /* we are removing a reference */
2887 realm_put(c->protocol.d2.realm, &c->protocol.d2.realm_regref);
2888
2889 if (newrealm)
2890 c->protocol.d2.realm = realm_get(newrealm, &c->protocol.d2.realm_regref);
2891 else {
2892 /* close the connection for players on unconfigured realms */
2893 conn_set_state(c, conn_state_destroy);
2894 c->protocol.d2.realm = NULL;
2895 }
2896
2897 return 0;
2898 }
2899
2900
2901 extern int conn_set_character(t_connection * c, t_character * character)
2902 {
2903 if (!c)
2904 {
2905 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2906 return -1;
2907 }
2908 if (!character)
2909 {
2910 eventlog(eventlog_level_error, __FUNCTION__, "got NULL character");
2911 return -1;
2912 }
2913
2914 c->protocol.d2.character = character;
2915
2916 return 0;
2917 }
2918
2919
2920 extern void conn_set_country(t_connection * c, char const * country)
2921 {
2922 if (!c)
2923 {
2924 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2925 return;
2926 }
2927 if (!country)
2928 {
2929 eventlog(eventlog_level_error, __FUNCTION__, "got NULL country");
2930 return;
2931 }
2932
2933 if (c->protocol.client.country)
2934 xfree((void *)c->protocol.client.country); /* avoid warning */
2935 c->protocol.client.country = xstrdup(country);
2936 }
2937
2938
2939 extern char const * conn_get_country(t_connection const * c)
2940 {
2941 if (!c)
2942 {
2943 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2944 return NULL;
2945 }
2946
2947 return c->protocol.client.country;
2948 }
2949
2950
2951 extern int conn_bind(t_connection * c1, t_connection * c2)
2952 {
2953 if (!c1)
2954 {
2955 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2956 return -1;
2957 }
2958 if (!c2)
2959 {
2960 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2961 return -1;
2962 }
2963
2964 c1->protocol.bound = c2;
2965 c2->protocol.bound = c1;
2966
2967 return 0;
2968 }
2969
2970
2971 extern int conn_set_ircline(t_connection * c, char const * line)
2972 {
2973 if (!c) {
2974 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2975 return -1;
2976 }
2977 if (!line) {
2978 eventlog(eventlog_level_error, __FUNCTION__, "got NULL line");
2979 return -1;
2980 }
2981 if (c->protocol.chat.irc.ircline)
2982 xfree((void *)c->protocol.chat.irc.ircline); /* avoid warning */
2983 c->protocol.chat.irc.ircline = xstrdup(line);
2984 return 0;
2985 }
2986
2987
2988 extern char const * conn_get_ircline(t_connection const * c)
2989 {
2990 if (!c) {
2991 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
2992 return NULL;
2993 }
2994 return c->protocol.chat.irc.ircline;
2995 }
2996
2997
2998 extern int conn_set_ircpass(t_connection * c, char const * pass)
2999 {
3000 if (!c) {
3001 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3002 return -1;
3003 }
3004 if (c->protocol.chat.irc.ircpass)
3005 xfree((void *)c->protocol.chat.irc.ircpass); /* avoid warning */
3006 if (!pass)
3007 c->protocol.chat.irc.ircpass = NULL;
3008 else
3009 c->protocol.chat.irc.ircpass = xstrdup(pass);
3010
3011 return 0;
3012 }
3013
3014
3015 extern char const * conn_get_ircpass(t_connection const * c)
3016 {
3017 if (!c) {
3018 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3019 return NULL;
3020 }
3021 return c->protocol.chat.irc.ircpass;
3022 }
3023
3024
3025 extern int conn_set_ircping(t_connection * c, unsigned int ping)
3026 {
3027 if (!c) {
3028 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3029 return -1;
3030 }
3031 c->protocol.chat.irc.ircping = ping;
3032 return 0;
3033 }
3034
3035
3036 extern unsigned int conn_get_ircping(t_connection const * c)
3037 {
3038 if (!c) {
3039 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3040 return 0;
3041 }
3042 return c->protocol.chat.irc.ircping;
3043 }
3044
3045 // NonReal
3046 extern int conn_get_welcomed(t_connection const * c)
3047 {
3048 if (!c)
3049 {
3050 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3051 return 0;
3052 }
3053
3054 return (c->protocol.cflags & conn_flags_welcomed);
3055 }
3056
3057 // NonReal
3058 extern void conn_set_welcomed(t_connection * c, int welcomed)
3059 {
3060
3061 if (!c)
3062 {
3063 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3064 return;
3065 }
3066 c->protocol.cflags |= conn_flags_welcomed;
3067 }
3068
3069 /* ADDED BY UNDYING SOULZZ 4/7/02 */
3070 extern int conn_set_w3_playerinfo(t_connection * c, const char * w3_playerinfo)
3071 {
3072 const char * temp;
3073
3074 if (!c)
3075 {
3076 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3077 return -1;
3078 }
3079
3080 temp = xstrdup(w3_playerinfo);
3081
3082 if (c->protocol.w3.w3_playerinfo)
3083 xfree((void *)c->protocol.w3.w3_playerinfo);
3084
3085 c->protocol.w3.w3_playerinfo = temp;
3086
3087 return 1;
3088 }
3089
3090 extern const char * conn_get_w3_playerinfo(t_connection * c)
3091 {
3092 if (!c)
3093 {
3094 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3095 return NULL;
3096 }
3097 return c->protocol.w3.w3_playerinfo;
3098 }
3099
3100
3101 extern int conn_quota_exceeded(t_connection * con, char const * text)
3102 {
3103 t_qline * qline;
3104 t_elem * curr;
3105
3106 if (!prefs_get_quota() ||
3107 !conn_get_account(con)
3108 // FIXME: (HarpyWar) do not allow flood for admins due to possible abuse with quick command sending that high load a server processor
3109 // If we really need to ignore flood protection, it can be allowed in Lua config for special users
3110 /* || (account_get_command_groups(conn_get_account(con)) & command_get_group("/admin-con"))*/
3111 ) return 0;
3112
3113 if (std::strlen(text) > prefs_get_quota_maxline())
3114 {
3115 message_send_text(con, message_type_error, con, localize(con, "Your line length quota has been exceeded!"));
3116 return 1;
3117 }
3118
3119 LIST_TRAVERSE(con->protocol.chat.quota.list, curr)
3120 {
3121 qline = (t_qline*)elem_get_data(curr);
3122 if (now >= qline->inf + (std::time_t)prefs_get_quota_time())
3123 {
3124 /* these lines are at least quota_time old */
3125 list_remove_elem(con->protocol.chat.quota.list, &curr);
3126 if (qline->count > con->protocol.chat.quota.totcount)
3127 eventlog(eventlog_level_error, __FUNCTION__, "qline->count={} but con->protocol.chat.quota.totcount={}", qline->count, con->protocol.chat.quota.totcount);
3128 con->protocol.chat.quota.totcount -= qline->count;
3129 xfree(qline);
3130 }
3131 else
3132 break; /* old items are first, so we know nothing else will match */
3133 }
3134
3135 qline = (t_qline*)xmalloc(sizeof(t_qline));
3136 qline->inf = now; /* set the moment */
3137 if (std::strlen(text) > prefs_get_quota_wrapline()) /* round up on the divide */
3138 qline->count = (std::strlen(text) + prefs_get_quota_wrapline() - 1) / prefs_get_quota_wrapline();
3139 else
3140 qline->count = 1;
3141
3142 list_append_data(con->protocol.chat.quota.list, qline);
3143
3144 con->protocol.chat.quota.totcount += qline->count;
3145
3146 if (con->protocol.chat.quota.totcount >= prefs_get_quota_lines())
3147 {
3148 message_send_text(con, message_type_error, con, localize(con, "Your message quota has been exceeded!"));
3149 if (con->protocol.chat.quota.totcount >= prefs_get_quota_dobae())
3150 {
3151 /* kick out the dobae user for violation of the quota rule */
3152 conn_set_state(con, conn_state_destroy);
3153 if (con->protocol.chat.channel)
3154 channel_message_log(con->protocol.chat.channel, con, 0, "DISCONNECTED FOR DOBAE ABUSE");
3155 return 2;
3156 }
3157 return 1;
3158 }
3159
3160 return 0;
3161 }
3162
3163
3164 extern int conn_set_lastsender(t_connection * c, char const * sender)
3165 {
3166 if (!c)
3167 {
3168 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conection");
3169 return -1;
3170 }
3171 if (c->protocol.chat.lastsender)
3172 xfree((void *)c->protocol.chat.lastsender); /* avoid warning */
3173 if (!sender)
3174 {
3175 c->protocol.chat.lastsender = NULL;
3176 return 0;
3177 }
3178 c->protocol.chat.lastsender = xstrdup(sender);
3179
3180 return 0;
3181 }
3182
3183
3184 extern char const * conn_get_lastsender(t_connection const * c)
3185 {
3186 if (!c)
3187 {
3188 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3189 return NULL;
3190 }
3191 return c->protocol.chat.lastsender;
3192 }
3193
3194
3195 const VersionCheck *conn_get_versioncheck(t_connection *c)
3196 {
3197 if (!c)
3198 {
3199 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3200 return nullptr;
3201 }
3202
3203 return c->protocol.client.versioncheck;
3204 }
3205
3206
3207 bool conn_set_versioncheck(t_connection *c, const VersionCheck* versioncheck)
3208 {
3209 if (!c)
3210 {
3211 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3212 return false;
3213 }
3214
3215 c->protocol.client.versioncheck = versioncheck;
3216
3217 return true;
3218 }
3219
3220 extern int conn_get_echoback(t_connection * c)
3221 {
3222 if (!c)
3223 {
3224 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3225 return 0;
3226 }
3227
3228 return (c->protocol.cflags & conn_flags_echoback);
3229 }
3230
3231 extern void conn_set_echoback(t_connection * c, int echoback)
3232 {
3233 if (!c)
3234 {
3235 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3236 return;
3237 }
3238 if (echoback)
3239 c->protocol.cflags |= conn_flags_echoback;
3240 else
3241 c->protocol.cflags &= ~conn_flags_echoback;
3242 }
3243
3244 extern int conn_set_udpok(t_connection * c)
3245 {
3246 if (!c)
3247 {
3248 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3249 return -1;
3250 }
3251
3252 if (!(c->protocol.cflags & conn_flags_udpok))
3253 {
3254 c->protocol.cflags |= conn_flags_udpok;
3255 c->protocol.flags &= ~MF_PLUG;
3256 }
3257
3258 return 0;
3259 }
3260
3261
3262 extern t_connection * conn_get_routeconn(t_connection const * c)
3263 {
3264 if (!c)
3265 {
3266 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3267 return NULL;
3268 }
3269
3270 return c->protocol.w3.routeconn;
3271 }
3272
3273
3274 extern int conn_set_routeconn(t_connection * c, t_connection * rc)
3275 {
3276 if (!c) {
3277 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conection");
3278 return -1;
3279 }
3280 c->protocol.w3.routeconn = rc;
3281
3282 return 0;
3283 }
3284
3285 extern int conn_get_crtime(t_connection *c)
3286 {
3287 if (!c)
3288 {
3289 eventlog(eventlog_level_error, "conn_get_crtime", "got NULL connection");
3290 return -1;
3291 }
3292 return c->protocol.cr_time;
3293 }
3294
3295 extern int conn_set_joingamewhisper_ack(t_connection * c, unsigned int value)
3296 {
3297 if (!c)
3298 {
3299 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3300 return -1;
3301 }
3302 if (value)
3303 c->protocol.cflags |= conn_flags_joingamewhisper;
3304 else
3305 c->protocol.cflags &= ~conn_flags_joingamewhisper;
3306 return 0;
3307 }
3308 extern int conn_get_joingamewhisper_ack(t_connection * c)
3309 {
3310 if (!c)
3311 {
3312 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3313 return -1;
3314 }
3315 return (c->protocol.cflags & conn_flags_joingamewhisper);
3316 }
3317
3318 extern int conn_set_leavegamewhisper_ack(t_connection * c, unsigned int value)
3319 {
3320 if (!c)
3321 {
3322 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3323 return -1;
3324 }
3325 if (value)
3326 c->protocol.cflags |= conn_flags_leavegamewhisper;
3327 else
3328 c->protocol.cflags &= ~conn_flags_leavegamewhisper;
3329 return 0;
3330 }
3331 extern int conn_get_leavegamewhisper_ack(t_connection * c)
3332 {
3333 if (!c)
3334 {
3335 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3336 return -1;
3337 }
3338 return (c->protocol.cflags & conn_flags_leavegamewhisper);
3339 }
3340
3341 extern int conn_set_anongame_search_starttime(t_connection * c, std::time_t t)
3342 {
3343 if (c == NULL) {
3344 eventlog(eventlog_level_error, "conn_set_anongame_search_starttime", "got NULL connection");
3345 return -1;
3346 }
3347 c->protocol.w3.anongame_search_starttime = t;
3348 return 0;
3349 }
3350
3351 extern std::time_t conn_get_anongame_search_starttime(t_connection * c)
3352 {
3353 if (c == NULL) {
3354 eventlog(eventlog_level_error, "conn_set_anongame_search_starttime", "got NULL connection");
3355 return ((std::time_t) 0);
3356 }
3357 return c->protocol.w3.anongame_search_starttime;
3358 }
3359
3360
3361 extern t_elist * conn_get_timer(t_connection *c)
3362 {
3363 if (!c) {
3364 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3365 return NULL;
3366 }
3367
3368 return &c->protocol.timers;
3369 }
3370
3371
3372 extern int conn_add_fdwatch(t_connection *c, fdwatch_handler handle)
3373 {
3374 assert(c);
3375 c->socket.fdw_idx = fdwatch_add_fd(c->socket.tcp_sock, fdwatch_type_read, handle, c);
3376 return c->socket.fdw_idx;
3377 }
3378
3379
3380 extern void conn_close_read(t_connection *c)
3381 {
3382 assert(c);
3383 conn_set_state(c, conn_state_destroy);
3384
3385 /* only if we still got output packets remove the read availability
3386 * from fdwatch, we are NOT allowed to remove all availability or
3387 * remove it completely from fdwatch while handling read, also
3388 * if the connection has no output packets is ok to leave it
3389 * in read availability check cause it will be closed immediately
3390 * in connlist_reap() anyway
3391 */
3392 if (conn_peek_outqueue(c))
3393 fdwatch_update_fd(c->socket.fdw_idx, fdwatch_type_write);
3394 }
3395
3396
3397 extern int conn_get_user_count_by_clienttag(t_clienttag ct)
3398 {
3399 t_connection * conn;
3400 t_elem const * curr;
3401 int clienttagusers = 0;
3402
3403 /* Get Number of Users for client tag specific */
3404 LIST_TRAVERSE_CONST(connlist(), curr)
3405 {
3406 conn = (t_connection*)elem_get_data(curr);
3407 if ((ct == conn->protocol.client.clienttag)
3408 && (conn->protocol.state == conn_state_loggedin)) clienttagusers++;
3409 }
3410
3411 return clienttagusers;
3412 }
3413
3414 extern int connlist_create(void)
3415 {
3416 conn_head = list_create();
3417 connarray_create();
3418 return 0;
3419 }
3420
3421 extern int connlist_destroy(void)
3422 {
3423 if (conn_dead) list_destroy(conn_dead);
3424 conn_dead = NULL;
3425 connarray_destroy();
3426 /* FIXME: if called with active connection, connection are not freed */
3427 if (list_destroy(conn_head) < 0)
3428 return -1;
3429 conn_head = NULL;
3430 return 0;
3431 }
3432
3433 extern void connlist_reap(void)
3434 {
3435 t_elem *curr;
3436 t_connection *c;
3437
3438 if (!conn_dead || !conn_head) return;
3439
3440 LIST_TRAVERSE(conn_dead, curr)
3441 {
3442 c = (t_connection *)elem_get_data(curr);
3443
3444 if (!c)
3445 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in conn_dead list");
3446 else if (!conn_peek_outqueue(c)) {
3447 conn_destroy(c, &curr, DESTROY_FROM_DEADLIST); /* also removes from conn_dead list and fdwatch */
3448 }
3449 }
3450 }
3451
3452 extern t_list * connlist(void)
3453 {
3454 return conn_head;
3455 }
3456
3457
3458 extern t_connection * connlist_find_connection_by_accountname(char const * accountname)
3459 {
3460 t_account * temp;
3461
3462 if (!accountname)
3463 {
3464 eventlog(eventlog_level_error, __FUNCTION__, "got NULL accountname");
3465 return NULL;
3466 }
3467
3468 if (!(temp = accountlist_find_account(accountname)))
3469 return NULL;
3470
3471 return account_get_conn(temp);
3472 }
3473
3474 extern t_connection * connlist_find_connection_by_account(t_account * account)
3475 {
3476 if (!account) {
3477 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
3478 return NULL;
3479 }
3480 return account_get_conn(account);
3481 }
3482
3483
3484 extern t_connection * connlist_find_connection_by_sessionkey(unsigned int sessionkey)
3485 {
3486 t_connection * c;
3487 t_elem const * curr;
3488
3489 LIST_TRAVERSE_CONST(conn_head, curr)
3490 {
3491 c = (t_connection*)elem_get_data(curr);
3492 if (c->protocol.sessionkey == sessionkey)
3493 return c;
3494 }
3495
3496 return NULL;
3497 }
3498
3499
3500 extern t_connection * connlist_find_connection_by_sessionnum(unsigned int sessionnum)
3501 {
3502 return connarray_get_conn(sessionnum);
3503 }
3504
3505
3506 extern t_connection * connlist_find_connection_by_socket(int socket)
3507 {
3508 t_connection * c;
3509 t_elem const * curr;
3510
3511 LIST_TRAVERSE_CONST(conn_head, curr)
3512 {
3513 c = (t_connection*)elem_get_data(curr);
3514 if (c->socket.tcp_sock == socket)
3515 return c;
3516 }
3517
3518 return NULL;
3519 }
3520
3521
3522 extern t_connection * connlist_find_connection_by_name(char const * name, t_realm * realm)
3523 {
3524 char charname[MAX_CHARNAME_LEN];
3525 char const * temp;
3526
3527 if (!name)
3528 {
3529 eventlog(eventlog_level_error, __FUNCTION__, "got NULL name");
3530 return NULL;
3531 }
3532 if (name[0] == '\0')
3533 {
3534 eventlog(eventlog_level_error, __FUNCTION__, "got empty name");
3535 return NULL;
3536 }
3537
3538 /* format: *username */
3539 if (name[0] == '*')
3540 {
3541 name++;
3542 return connlist_find_connection_by_accountname(name);
3543 }
3544
3545 /* If is charname@otherrealm or ch@rname@realm */
3546 if ((temp = std::strrchr(name, '@'))) /* search from the right */
3547 {
3548 unsigned int n;
3549
3550 n = temp - name;
3551 if (n >= MAX_CHARNAME_LEN)
3552 {
3553 eventlog(eventlog_level_info, __FUNCTION__, "character name too long in \"{}\" (charname@otherrealm format)", name);
3554 return NULL;
3555 }
3556 std::strncpy(charname, name, n);
3557 charname[n] = '\0';
3558 return connlist_find_connection_by_charname(name, temp + 1);
3559 }
3560
3561 /* format: charname*username */
3562 if ((temp = std::strchr(name, '*')))
3563 {
3564 unsigned int n;
3565
3566 n = temp - name;
3567 if (n >= MAX_CHARNAME_LEN)
3568 {
3569 eventlog(eventlog_level_info, __FUNCTION__, "character name too long in \"{}\" (charname*username format)", name);
3570 return NULL;
3571 }
3572 name = temp + 1;
3573 return connlist_find_connection_by_accountname(name);
3574 }
3575
3576 /* format: charname (realm must be not NULL) */
3577 if (realm)
3578 return connlist_find_connection_by_charname(name, realm_get_name(realm));
3579
3580 /* format: Simple username, clients with no realm, like starcraft or d2 open,
3581 * the format is the same of charname but is matched if realmname is NULL */
3582 return connlist_find_connection_by_accountname(name);
3583 }
3584
3585
3586 extern t_connection * connlist_find_connection_by_charname(char const * charname, char const * realmname)
3587 {
3588 t_connection * c;
3589 t_elem const * curr;
3590
3591 if (!realmname) {
3592 eventlog(eventlog_level_error, __FUNCTION__, "got NULL realmname");
3593 return NULL;
3594 }
3595 LIST_TRAVERSE_CONST(conn_head, curr)
3596 {
3597 c = (t_connection*)elem_get_data(curr);
3598 if (!c)
3599 continue;
3600 if (!c->protocol.d2.charname)
3601 continue;
3602 if (!c->protocol.d2.realm)
3603 continue;
3604 if ((strcasecmp(c->protocol.d2.charname, charname) == 0) && (strcasecmp(realm_get_name(c->protocol.d2.realm), realmname) == 0))
3605 return c;
3606 }
3607 return NULL;
3608 }
3609
3610
3611 extern t_connection * connlist_find_connection_by_uid(unsigned int uid)
3612 {
3613 t_account * temp;
3614
3615 if (!(temp = accountlist_find_account_by_uid(uid)))
3616 {
3617 return NULL;
3618 }
3619 return account_get_conn(temp);
3620 }
3621
3622 extern int connlist_get_length(void)
3623 {
3624 return list_get_length(conn_head);
3625 }
3626
3627
3628 extern unsigned int connlist_login_get_length(void)
3629 {
3630 t_connection const * c;
3631 unsigned int count;
3632 t_elem const * curr;
3633
3634 count = 0;
3635 LIST_TRAVERSE_CONST(conn_head, curr)
3636 {
3637 c = (const t_connection*)elem_get_data(curr);
3638 if ((c->protocol.state == conn_state_loggedin) &&
3639 ((c->protocol.cclass == conn_class_bnet) || (c->protocol.cclass == conn_class_bot) || (c->protocol.cclass == conn_class_telnet)
3640 || (c->protocol.cclass == conn_class_irc) || (c->protocol.cclass == conn_class_wol)))
3641 count++;
3642 }
3643
3644 return count;
3645 }
3646
3647
3648 extern int connlist_total_logins(void)
3649 {
3650 return totalcount;
3651 }
3652
3653
3654 extern unsigned int connlist_count_connections(unsigned int addr)
3655 {
3656 t_connection * c;
3657 t_elem const * curr;
3658 unsigned int count;
3659
3660 count = 0;
3661
3662 LIST_TRAVERSE_CONST(conn_head, curr)
3663 {
3664 c = (t_connection *)elem_get_data(curr);
3665 if (c->socket.tcp_addr == addr)
3666 count++;
3667 }
3668
3669 return count;
3670 }
3671
3672 /* Warcraft 3 icon that displayed in a channel */
3673 extern int conn_update_w3_playerinfo(t_connection * c)
3674 {
3675 t_account * account;
3676 t_clienttag clienttag;
3677 t_clan * user_clan;
3678 int clantag = 0;
3679 unsigned int acctlevel;
3680 char tempplayerinfo[40];
3681 char raceicon; /* appeared in 1.03 */
3682 unsigned int raceiconnumber;
3683 unsigned int wins;
3684 char clantag_str_tmp[5];
3685 const char * clantag_str = NULL;
3686 char revtag[5];
3687 char clienttag_str[5];
3688
3689 if (c == NULL) {
3690 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
3691 return -1;
3692 }
3693
3694 account = conn_get_account(c);
3695
3696 if (account == NULL) {
3697 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account");
3698 return -1;
3699 }
3700
3701 clienttag = c->protocol.client.clienttag;
3702
3703 if (!((clienttag == CLIENTTAG_WARCRAFT3_UINT) ||
3704 (clienttag == CLIENTTAG_WAR3XP_UINT))){
3705 return 0;
3706 }
3707
3708 std::strncpy(revtag, tag_uint_to_str(clienttag_str, conn_get_fake_clienttag(c)), 5); revtag[4] = '\0';
3709 strreverse(revtag);
3710
3711 acctlevel = account_get_highestladderlevel(account, clienttag);
3712 account_get_raceicon(account, &raceicon, &raceiconnumber, &wins, clienttag);
3713
3714 if ((user_clan = account_get_clan(account)) != NULL)
3715 clantag = clan_get_clantag(user_clan);
3716
3717 if (clantag) {
3718 std::sprintf(clantag_str_tmp, "%c%c%c%c", clantag & 0xff, (clantag >> 8) & 0xff, (clantag >> 16) & 0xff, clantag >> 24);
3719 clantag_str = clantag_str_tmp;
3720 while ((*clantag_str) == 0) clantag_str++;
3721 }
3722
3723 const char* usericon = account_get_user_icon(account, clienttag);
3724 // allow set icon to a user directly from the database (override default icon always if not null)
3725 if (usericon)
3726 {
3727 if (clantag)
3728 std::sprintf(tempplayerinfo, "%s %s %u %s", revtag, usericon, acctlevel, clantag_str);
3729 else
3730 std::sprintf(tempplayerinfo, "%s %s %u", revtag, usericon, acctlevel);
3731 eventlog(eventlog_level_info, __FUNCTION__, "[{}] {} using user-selected icon [{}]", conn_get_socket(c), revtag, usericon);
3732 }
3733 // default icon "WAR3" or "W3XP"
3734 else if (acctlevel == 0)
3735 {
3736 if (clantag)
3737 std::sprintf(tempplayerinfo, "%s %s 0 %s", revtag, revtag, clantag_str);
3738 else
3739 std::strcpy(tempplayerinfo, revtag);
3740 eventlog(eventlog_level_info, __FUNCTION__, "[{}] {}", conn_get_socket(c), revtag);
3741 }
3742 // display race icon with a level number
3743 else
3744 {
3745 if (clantag)
3746 std::sprintf(tempplayerinfo, "%s %1u%c3W %u %s", revtag, raceiconnumber, raceicon, acctlevel, clantag_str);
3747 else
3748 std::sprintf(tempplayerinfo, "%s %1u%c3W %u", revtag, raceiconnumber, raceicon, acctlevel);
3749 eventlog(eventlog_level_info, __FUNCTION__, "[{}] {} using generated icon [{}{}3W]", conn_get_socket(c), revtag, raceiconnumber, raceicon);
3750 }
3751
3752 // if custom stats is enabled then set a custom client icon by player rating
3753 // do not override user selected icon if any
3754 if (!usericon && prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag))
3755 {
3756 if (t_icon_info* icon = customicons_get_icon_by_account(account, clienttag))
3757 {
3758 if (clantag)
3759 std::sprintf(tempplayerinfo, "%s %s %u %s", revtag, icon->icon_code, 0, clantag_str);
3760 else
3761 std::sprintf(tempplayerinfo, "%s %s %u", revtag, icon->icon_code, 0);
3762 }
3763 }
3764
3765 #ifdef WITH_LUA
3766 // change icon info from Lua
3767 if (const char * iconinfo = lua_handle_user_icon(c, tempplayerinfo))
3768 {
3769 conn_set_w3_playerinfo(c, iconinfo);
3770 return 0;
3771 }
3772 #endif
3773 conn_set_w3_playerinfo(c, tempplayerinfo);
3774 return 0;
3775 }
3776
3777
3778 extern int conn_get_passfail_count(t_connection * c)
3779 {
3780 if (!c)
3781 {
3782 eventlog(eventlog_level_error, "conn_get_passfail_count", "got NULL connection");
3783 return -1;
3784 }
3785 return c->protocol.passfail_count;
3786 }
3787
3788
3789 extern int conn_set_passfail_count(t_connection * c, unsigned int n)
3790 {
3791 if (c == NULL)
3792 {
3793 eventlog(eventlog_level_error, "conn_set_passfail_count", "got NULL connection");
3794 return -1;
3795 }
3796 c->protocol.passfail_count = n;
3797 return 0;
3798 }
3799
3800
3801 extern int conn_increment_passfail_count(t_connection * c)
3802 {
3803 unsigned int count;
3804
3805 if (prefs_get_passfail_count() > 0)
3806 {
3807 count = conn_get_passfail_count(c) + 1;
3808 if (count == prefs_get_passfail_count())
3809 {
3810 ipbanlist_add(NULL, addr_num_to_ip_str(conn_get_addr(c)), now + (std::time_t)prefs_get_passfail_bantime());
3811 eventlog(eventlog_level_info, __FUNCTION__, "[{}] failed password tries: {} (banned ip)", conn_get_socket(c), count);
3812 conn_set_state(c, conn_state_destroy);
3813 return -1;
3814 }
3815 else conn_set_passfail_count(c, count);
3816 }
3817 return 0;
3818 }
3819
3820
3821 extern char const * conn_get_client_proof(t_connection * c)
3822 {
3823 if (!c)
3824 {
3825 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3826 return NULL;
3827 }
3828
3829 return c->protocol.w3.client_proof;
3830 }
3831
3832 extern int conn_set_client_proof(t_connection * c, char const * client_proof)
3833 {
3834 if (!c)
3835 {
3836 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3837 return -1;
3838 }
3839
3840 if (c->protocol.w3.client_proof){
3841 xfree((void*)c->protocol.w3.client_proof);
3842 c->protocol.w3.client_proof = NULL;
3843 }
3844
3845 if (client_proof != NULL) {
3846 char * proof = (char *)xmalloc(20);
3847 std::memcpy(proof, client_proof, 20);
3848 c->protocol.w3.client_proof = proof;
3849 }
3850 return 0;
3851 }
3852
3853
3854 extern char const * conn_get_server_proof(t_connection * c)
3855 {
3856 if (!c)
3857 {
3858 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3859 return NULL;
3860 }
3861
3862 return c->protocol.w3.server_proof;
3863 }
3864
3865 extern int conn_set_server_proof(t_connection * c, char const * server_proof)
3866 {
3867 if (!c)
3868 {
3869 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3870 return -1;
3871 }
3872
3873 if (c->protocol.w3.server_proof){
3874 xfree((void*)c->protocol.w3.server_proof);
3875 c->protocol.w3.server_proof = NULL;
3876 }
3877
3878 if (server_proof != NULL) {
3879 char * proof = (char *)xmalloc(20);
3880 std::memcpy(proof, server_proof, 20);
3881 c->protocol.w3.server_proof = proof;
3882 }
3883 return 0;
3884 }
3885
3886
3887 extern int conn_set_tmpOP_channel(t_connection * c, char const * tmpOP_channel)
3888 {
3889 if (!c)
3890 {
3891 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3892 return -1;
3893 }
3894
3895 if (c->protocol.chat.tmpOP_channel)
3896 {
3897 xfree((void *)c->protocol.chat.tmpOP_channel);
3898 c->protocol.chat.tmpOP_channel = NULL;
3899 }
3900
3901 if (tmpOP_channel)
3902 c->protocol.chat.tmpOP_channel = xstrdup(tmpOP_channel);
3903
3904 return 0;
3905 }
3906
3907 extern char const * conn_get_tmpOP_channel(t_connection * c)
3908 {
3909 if (!c)
3910 {
3911 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3912 return NULL;
3913 }
3914
3915 return c->protocol.chat.tmpOP_channel;
3916 }
3917
3918 extern int conn_set_tmpVOICE_channel(t_connection * c, char const * tmpVOICE_channel)
3919 {
3920 if (!c)
3921 {
3922 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3923 return -1;
3924 }
3925
3926 if (c->protocol.chat.tmpVOICE_channel)
3927 {
3928 xfree((void *)c->protocol.chat.tmpVOICE_channel);
3929 c->protocol.chat.tmpVOICE_channel = NULL;
3930 }
3931
3932 if (tmpVOICE_channel)
3933 c->protocol.chat.tmpVOICE_channel = xstrdup(tmpVOICE_channel);
3934
3935 return 0;
3936 }
3937
3938 extern char const * conn_get_tmpVOICE_channel(t_connection * c)
3939 {
3940 if (!c)
3941 {
3942 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
3943 return NULL;
3944 }
3945
3946 return c->protocol.chat.tmpVOICE_channel;
3947 }
3948
3949 static int connarray_create(void)
3950 {
3951 unsigned i;
3952 t_conn_entry *curr;
3953
3954 if (connarray) connarray_destroy();
3955 connarray = (t_conn_entry*)xmalloc(sizeof(t_conn_entry)* fdw_maxcons);
3956
3957 elist_init(&arrayflist);
3958 /* put all elements as free */
3959 for (i = 0, curr = connarray; i < fdw_maxcons; i++, curr++) {
3960 elist_add_tail(&arrayflist, &curr->freelist);
3961 curr->c = NULL;
3962 }
3963
3964 return 0;
3965 }
3966
3967
3968 static void connarray_destroy(void)
3969 {
3970 if (connarray) xfree((void*)connarray);
3971 connarray = NULL;
3972 }
3973
3974 static t_connection *connarray_get_conn(unsigned index)
3975 {
3976 if (index >= fdw_maxcons) return NULL;
3977 return connarray[index].c;
3978 }
3979
3980 static unsigned connarray_add_conn(t_connection *c)
3981 {
3982 t_conn_entry *curr;
3983
3984 assert(c);
3985 assert(!elist_empty(&arrayflist));
3986
3987 curr = elist_entry(elist_next(&arrayflist), t_conn_entry, freelist);
3988 assert(curr->c == NULL); /* it should never be free and != NULL */
3989 curr->c = c;
3990 elist_del(&curr->freelist);
3991 return (curr - connarray); /* return the array index */
3992 }
3993
3994 static void connarray_del_conn(unsigned index)
3995 {
3996 t_conn_entry *curr;
3997
3998 if (index >= fdw_maxcons) return;
3999 curr = connarray + index;
4000 curr->c = NULL;
4001 elist_add_tail(&arrayflist, &curr->freelist);
4002 }
4003
4004 extern int conn_is_irc_variant(t_connection * c)
4005 {
4006 if (!c)
4007 {
4008 eventlog(eventlog_level_error, __FUNCTION__, "get NULL conn");
4009 return -1;
4010 }
4011
4012 if ((c->protocol.cclass == conn_class_irc) ||
4013 (c->protocol.cclass == conn_class_wol) ||
4014 (c->protocol.cclass == conn_class_wserv) ||
4015 (c->protocol.cclass == conn_class_wladder)
4016 )
4017 return 1;
4018
4019 return 0;
4020 }
4021
4022 /**
4023 * Westwood Online Extensions
4024 */
4025 extern int conn_get_wol(t_connection * c)
4026 {
4027 if (!c)
4028 {
4029 eventlog(eventlog_level_error, __FUNCTION__, "get NULL conn");
4030 return -1;
4031 }
4032
4033 if (c->protocol.cclass == conn_class_wol)
4034 return 1;
4035
4036 return 0;
4037 }
4038
4039 extern void conn_wol_set_apgar(t_connection * c, char const * apgar)
4040 {
4041 if (!c)
4042 {
4043 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
4044 return;
4045 }
4046 if (!apgar)
4047 {
4048 eventlog(eventlog_level_error, __FUNCTION__, "got NULL WOL apgar");
4049 return;
4050 }
4051
4052 if (c->protocol.wol.apgar)
4053 xfree((void *)c->protocol.wol.apgar); /* avoid warning */
4054 c->protocol.wol.apgar = xstrdup(apgar);
4055 }
4056
4057 extern char const * conn_wol_get_apgar(t_connection * c)
4058 {
4059 if (!c)
4060 {
4061 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4062 return NULL;
4063 }
4064
4065 return c->protocol.wol.apgar;
4066 }
4067
4068 extern void conn_wol_set_codepage(t_connection * c, int codepage)
4069 {
4070 if (!c)
4071 {
4072 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4073 return;
4074 }
4075
4076 c->protocol.wol.codepage = codepage;
4077 }
4078
4079 extern int conn_wol_get_codepage(t_connection * c)
4080 {
4081 if (!c)
4082 {
4083 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4084 return -1;
4085 }
4086
4087 return c->protocol.wol.codepage;
4088 }
4089
4090 extern void conn_wol_set_findme(t_connection * c, bool findme)
4091 {
4092 if (!c)
4093 {
4094 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4095 return;
4096 }
4097
4098 c->protocol.wol.findme = findme;
4099 }
4100
4101 extern bool conn_wol_get_findme(t_connection * c)
4102 {
4103 if (!c)
4104 {
4105 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4106 return false;
4107 }
4108
4109 bool bRet;
4110 if (c->protocol.wol.findme == 0)
4111 bRet = false;
4112 else
4113 bRet = true;
4114
4115 return bRet;
4116 }
4117
4118 extern void conn_wol_set_pageme(t_connection * c, bool pageme)
4119 {
4120 if (!c)
4121 {
4122 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4123 return;
4124 }
4125
4126 c->protocol.wol.pageme = pageme;
4127 }
4128
4129 extern bool conn_wol_get_pageme(t_connection * c)
4130 {
4131 if (!c)
4132 {
4133 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4134 return false;
4135 }
4136
4137 bool bRet;
4138 if (c->protocol.wol.pageme == 0)
4139 bRet = false;
4140 else
4141 bRet = true;
4142
4143 return bRet;
4144 }
4145
4146 extern void conn_wol_set_anongame_player(t_connection * c, t_anongame_wol_player * anongame_player)
4147 {
4148 if (!c)
4149 {
4150 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4151 return;
4152 }
4153
4154 if (!anongame_player)
4155 {
4156 eventlog(eventlog_level_error, __FUNCTION__, "got NULL anongame_player");
4157 return;
4158 }
4159
4160 c->protocol.wol.anongame_player = anongame_player;
4161 }
4162
4163 extern t_anongame_wol_player * conn_wol_get_anongame_player(t_connection * c)
4164 {
4165 if (!c)
4166 {
4167 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4168 return NULL;
4169 }
4170
4171 return c->protocol.wol.anongame_player;
4172 }
4173
4174
4175
4176 extern int conn_client_readmemory(t_connection * c, unsigned int request_id, unsigned int offset, unsigned int length)
4177 {
4178 t_packet * rpacket;
4179 t_clienttag clienttag;
4180
4181 if (!c)
4182 {
4183 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4184 return -1;
4185 }
4186 clienttag = conn_get_clienttag(c);
4187
4188 // disallow clients that doesn't support SID_READMEMORY
4189 if (clienttag != CLIENTTAG_STARCRAFT_UINT && clienttag != CLIENTTAG_BROODWARS_UINT && clienttag != CLIENTTAG_STARJAPAN_UINT && clienttag != CLIENTTAG_SHAREWARE_UINT &&
4190 clienttag != CLIENTTAG_DIABLORTL_UINT && clienttag != CLIENTTAG_DIABLOSHR_UINT && clienttag != CLIENTTAG_WARCIIBNE_UINT)
4191 {
4192 return -1;
4193 }
4194 if (!(rpacket = packet_create(packet_class_bnet)))
4195 return -1;
4196
4197 packet_set_size(rpacket, sizeof(t_server_readmemory));
4198 packet_set_type(rpacket, SERVER_READMEMORY);
4199
4200 bn_int_set(&rpacket->u.server_readmemory.request_id, request_id);
4201 bn_int_set(&rpacket->u.server_readmemory.address, offset);
4202 bn_int_set(&rpacket->u.server_readmemory.length, length);
4203
4204 conn_push_outqueue(c, rpacket);
4205 packet_del_ref(rpacket);
4206
4207 return 0;
4208 }
4209
4210 extern int conn_client_requiredwork(t_connection * c, const char * filename)
4211 {
4212 t_packet * rpacket;
4213
4214 if (!c)
4215 {
4216 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
4217 return -1;
4218 }
4219 if (!(rpacket = packet_create(packet_class_bnet)))
4220 return -1;
4221
4222 packet_set_size(rpacket, sizeof(t_server_requiredwork));
4223 packet_set_type(rpacket, SERVER_REQUIREDWORK);
4224 packet_append_string(rpacket, filename); // filename should be "IX86ExtraWork.mpq"
4225
4226 conn_push_outqueue(c, rpacket);
4227 packet_del_ref(rpacket);
4228
4229 return 0;
4230 }
4231 }
4232
4233 }