Debian Codegraph @

src/client/bnbot.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999 Ross Combs ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cctype>
22 #include <cstring>
23 #include <cerrno>
24
25 #ifdef HAVE_TERMIOS_Hcxx . . $ `build/config.h:15:9`!
26 # include <termios.h>
27 #endif
28
29 #include "compat/termios.h"cxx . . $ `<file>/src/compat/termios.h`/
30 #include "compat/psock.h"cxx . . $ `<file>/src/compat/psock.h`/
31 #include "common/packet.h"cxx . . $ `<file>/src/common/packet.h`/
32 #include "common/field_sizes.h"cxx . . $ `<file>/src/common/field_sizes.h`/
33 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
34 #include "common/init_protocol.h"cxx . . $ `<file>/src/common/init_protocol.h`/
35 #include "common/bn_type.h"cxx . . $ `<file>/src/common/bn_type.h`/
36 #include "common/network.h"cxx . . $ `<file>/src/common/network.h`/
37 #ifdef CLIENTDEBUGcxx . . $ `src/common/setup_before.h:49:9`!
38 # include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
39 #endif
40 #include "client.h"cxx . . $ `<file>/src/client/client.h`/
41
42 #ifdef HAVE_ARPA_INET_Hcxx . . $ `build/config.h:13:9`!
43 # include <arpa/inet.h>
44 #endif
45 #ifdef HAVE_WS2TCPIP_H
46 # include <Ws2tcpip.h>
47 #endif
48
49 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
50
51 using namespace pvpgn;
52 using namespace pvpgncxx . . $ pvpgn/::client;
53
54 namespacecxx . . $ `$anonymous_namespace_src/client/bnbot.cpp`/
55 {
56
57 void usagecxx . . $ `$anonymous_namespace_src/client/bnbot.cpp`/usage(85c52e162fed56f9).(char const * progname)
58 {
59 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "usage: %s [<options>] [<servername> [<TCP portnumber>]]\n"
60 " -h, --help, --usage show this information and exit\n"
61 " -v, --version print version number and exit\n",
62 progname);
63 stdcxx . . $ std/::exit(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
64 }
65
66 }
67
68 extern int maincxx . . $ main(afcd3168fc1188a4).(int argc, char * argv[])
69 {
70 int a;
71 int sd;
72 struct sockaddr_in saddr;
73 t_packetcxx . . $ pvpgn/t_packet# * packet;
74 t_packetcxx . . $ pvpgn/t_packet# * rpacket;
75 char const * servname = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
76 unsigned short servport = 0;
77 char text[MAX_MESSAGE_LENcxx . . $ MAX_MESSAGE_LEN.];
78 struct hostent * host;
79 unsigned int commpos;
80 struct termios in_attr_old;
81 struct termios in_attr_new;
82 int changed_in;
83 unsigned int currsize;
84 int fd_stdin;
85 unsigned int screen_width, screen_height;
86
87 if (argc < 1 || !argv || !argv[0])
88 {
89 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "bad arguments\n");
90 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
91 }
92
93 for (a = 1; a < argc; a++)
94 if (servname && stdcxx . . $ std/::isdigitcxx . . $ std/isdigit(a1851578aea3f541).((int)argv[a][0]) && a + 1 >= argc)
95 {
96 if (str_to_ushortcxx . . $ pvpgn/str_to_ushort(c9bce2f4bcd31d4).(argv[a], &servport) < 0)
97 {
98 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]);
99 usagecxx . . $ `$anonymous_namespace_src/client/bnbot.cpp`/usage(85c52e162fed56f9).(argv[0]);
100 }
101 }
102 else if (!servname && argv[a][0] != '-' && a + 2 >= argc)
103 servname = argv[a];
104 else if (stdcxx . . $ std/::strcmp(argv[a], "-v") == 0 || stdcxx . . $ std/::strcmp(argv[a], "--version") == 0)
105 {
106 stdcxx . . $ std/::printfcxx . . $ std/printf(581fddd72a8db8fc).("version " PVPGN_VERSIONcxx . . $ `src/common/version.h:23:9`! "\n");
107 return EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!;
108 }
109 else if (stdcxx . . $ std/::strcmp(argv[a], "-h") == 0 || stdcxx . . $ std/::strcmp(argv[a], "--help") == 0 || stdcxx . . $ std/::strcmp(argv[a], "--usage") == 0)
110 usagecxx . . $ `$anonymous_namespace_src/client/bnbot.cpp`/usage(85c52e162fed56f9).(argv[0]);
111 else
112 {
113 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
114 usagecxx . . $ `$anonymous_namespace_src/client/bnbot.cpp`/usage(85c52e162fed56f9).(argv[0]);
115 }
116
117 if (servport == 0)
118 servport = BNETD_SERV_PORTcxx . . $ BNETD_SERV_PORT.;
119 if (!servname)
120 servname = BNETD_DEFAULT_HOSTcxx . . $ BNETD_DEFAULT_HOST.;
121
122 if (psock_initcxx . . $ `src/compat/psock.h:284:9`!() < 0)
123 {
124 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not inialialize socket functions\n", argv[0]);
125 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
126 }
127
128 if (!(host = gethostbyname(servname)))
129 {
130 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: unknown host \"%s\"\n", argv[0], servname);
131 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
132 }
133
134 fd_stdin = fileno(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!);
135 if (tcgetattr(fd_stdin, &in_attr_old) >= 0)
136 {
137 in_attr_new = in_attr_old;
138 in_attr_new.c_lflag &= ~(ECHOcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:29:9`! | ICANONcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:25:9`!); /* turn off ECHO and ICANON */
139 in_attr_new.c_cc[VMINcxx . . $ `<external>/5505922754558120548/termios-c_cc.h:30:9`!] = 0; /* don't require reads to return data */
140 in_attr_new.c_cc[VTIMEcxx . . $ `<external>/5505922754558120548/termios-c_cc.h:29:9`!] = 1; /* timeout = .1 second */
141 tcsetattr(fd_stdin, TCSANOWcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:24:9`!, &in_attr_new);
142 changed_in = 1;
143 }
144 else
145 {
146 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not set terminal attributes for stdin\n", argv[0]);
147 changed_in = 0;
148 }
149
150 if (client_get_termsizecxx . . $ pvpgn/client/client_get_termsize(1ea1e3350d45fd0e).(fd_stdin, &screen_width, &screen_height) < 0)
151 {
152 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not determine screen size\n", argv[0]);
153 if (changed_in)
154 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
155 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
156 }
157
158 if ((sd = psock_socketcxx . . $ `src/compat/psock.h:287:9`!(PSOCK_PF_INETcxx . . $ `src/compat/psock.h:186:9`!, PSOCK_SOCK_STREAMcxx . . $ `src/compat/psock.h:192:9`!, PSOCK_IPPROTO_TCPcxx . . $ `src/compat/psock.h:197:10`!)) < 0)
159 {
160 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create socket (psock_socket: %s)\n", argv[0], stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!()));
161 if (changed_in)
162 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
163 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
164 }
165
166 stdcxx . . $ std/::memset(&saddr, 0, sizeof(saddr));
167 saddr.sin_family = PSOCK_AF_INETcxx . . $ `src/compat/psock.h:189:9`!;
168 saddr.sin_port = htons(servport);
169 stdcxx . . $ std/::memcpy(&saddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
170 if (psock_connectcxx . . $ `src/compat/psock.h:294:9`!(sd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
171 {
172 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not connect to server \"%s\" port %hu (psock_connect: %s)\n", argv[0], servname, servport, stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!()));
173 if (changed_in)
174 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
175 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
176 }
177
178 if (psock_ctlcxx . . $ psock_ctl(bba9d6e9fb34fad7).(sd, PSOCK_NONBLOCKcxx . . $ `src/compat/psock.h:214:9`!) < 0)
179 {
180 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not set TCP socket to non-blocking mode (psock_ctl: %s)\n", argv[0], stdcxx . . $ std/::strerror(psock_errnocxx . . $ `src/compat/psock.h:286:9`!()));
181 psock_closecxx . . $ `src/compat/psock.h:300:9`!(sd);
182 if (changed_in)
183 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
184 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
185 }
186
187 char addrstr[INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!] = { 0 };
188 inet_ntop(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, &(saddr.sin_addr), addrstr, sizeof(addrstr));
189 stdcxx . . $ std/::printfcxx . . $ std/printf(581fddd72a8db8fc).("Connected to %s:%hu.\n", addrstr, servport);
190 #ifdef CLIENTDEBUGcxx . . $ `src/common/setup_before.h:49:9`!
191 eventlog_setcxx . . $ pvpgn/eventlog_set(8d4262d45fc644ab).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
192 #endif
193
194 if (!(packet = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_initcxx . . $ pvpgn/packet_class_init.)))
195 {
196 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create packet\n", argv[0]);
197 if (changed_in)
198 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
199 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
200 }
201 bn_byte_set(&packet->ucxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#u..client_initconncxx . . $ pvpgn/$anonymous_type_a1640d8ff791e3e5_2#$anonymous_type_3#client_initconn..cclasscxx . . $ pvpgn/$anonymous_type_7cca3803bac3bad7_0#cclass., CLIENT_INITCONN_CLASS_BOTcxx . . $ `src/common/init_protocol.h:61:9`!);
202 client_blocksend_packetcxx . . $ pvpgn/client/client_blocksend_packet(906e81e3c6d5802b).(sd, packet);
203 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(packet);
204
205 if (!(rpacket = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_rawcxx . . $ pvpgn/packet_class_raw.)))
206 {
207 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create packet\n", argv[0]);
208 if (changed_in)
209 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
210 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
211 }
212
213 if (!(packet = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_rawcxx . . $ pvpgn/packet_class_raw.)))
214 {
215 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create packet\n", argv[0]);
216 if (changed_in)
217 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
218 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
219 }
220 packet_append_ntstringcxx . . $ pvpgn/packet_append_ntstring(41801851180b7cbb).(packet, "\004");
221 client_blocksend_packetcxx . . $ pvpgn/client/client_blocksend_packet(906e81e3c6d5802b).(sd, packet);
222 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(packet);
223
224 {
225 int highest_fd;
226 t_psock_fd_setcxx . . $ `src/compat/psock.h:277:9`! rfds;
227
228 PSOCK_FD_ZEROcxx . . $ `src/compat/psock.h:278:9`!(&rfds);
229 highest_fd = fd_stdin;
230 if (sd > highest_fd)
231 highest_fd = sd;
232
233 commpos = 0;
234 text[0] = '\0';
235
236 for (;;)
237 {
238 PSOCK_FD_SETcxx . . $ `src/compat/psock.h:280:9`!(fd_stdin, &rfds);
239 PSOCK_FD_SETcxx . . $ `src/compat/psock.h:280:9`!(sd, &rfds);
240
241 if (psock_selectcxx . . $ `src/compat/psock.h:301:9`!(highest_fd + 1, &rfds, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) < 0)
242 {
243 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != PSOCK_EINTRcxx . . $ `src/compat/psock.h:224:10`!)
244 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: select failed (select: %s)\n", argv[0], stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
245 continue;
246 }
247
248 if (PSOCK_FD_ISSETcxx . . $ `src/compat/psock.h:281:9`!(sd, &rfds)) /* got network data */
249 {
250 packet_set_sizecxx . . $ pvpgn/packet_set_size(a12d8724e4c46208).(rpacket, MAX_PACKET_SIZEcxx . . $ MAX_PACKET_SIZE. - 1);
251 currsize = 0;
252 if (net_recv_packetcxx . . $ pvpgn/net_recv_packet(364823e0b5ea3b9f).(sd, rpacket, &currsize)<0)
253 {
254 psock_closecxx . . $ `src/compat/psock.h:300:9`!(sd);
255 sd = -1;
256 }
257
258 if (currsize>0)
259 {
260 char * str = (char*)packet_get_raw_datacxx . . $ pvpgn/packet_get_raw_data(ed324b086ed1b636).(rpacket, 0);
261
262 str[currsize] = '\0';
263 stdcxx . . $ std/::printfcxx . . $ std/printf(581fddd72a8db8fc).("%s", str);
264 stdcxx . . $ std/::fflushcxx . . $ std/fflush(89bc1efcdac0ca22).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
265 }
266
267 if (sd == -1) /* if connection was closed */
268 {
269 stdcxx . . $ std/::printfcxx . . $ std/printf(581fddd72a8db8fc).("Connection closed by server.\n");
270 if (changed_in)
271 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
272 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(rpacket);
273 return EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!;
274 }
275 }
276
277 if (PSOCK_FD_ISSETcxx . . $ `src/compat/psock.h:281:9`!(fd_stdin, &rfds)) /* got keyboard data */
278 {
279 switch (client_get_commcxx . . $ pvpgn/client/client_get_comm(e9a6a98b384956c7).("", text, sizeof(text), &commpos, -1, 0, screen_width))
280 {
281 case -1: /* cancel */
282 if (changed_in)
283 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
284 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
285
286 case 0: /* timeout */
287 break;
288
289 case 1:
290 if (!(packet = packet_createcxx . . $ pvpgn/packet_create(f721c565085e24c0).(packet_class_rawcxx . . $ pvpgn/packet_class_raw.)))
291 {
292 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: could not create packet\n", argv[0]);
293 if (changed_in)
294 tcsetattr(fd_stdin, TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &in_attr_old);
295 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(rpacket);
296 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
297 }
298 packet_append_ntstringcxx . . $ pvpgn/packet_append_ntstring(41801851180b7cbb).(packet, text);
299 packet_append_ntstringcxx . . $ pvpgn/packet_append_ntstring(41801851180b7cbb).(packet, "\r\n");
300 client_blocksend_packetcxx . . $ pvpgn/client/client_blocksend_packet(906e81e3c6d5802b).(sd, packet);
301 packet_del_refcxx . . $ pvpgn/packet_del_ref(d23439d9c1f63082).(packet);
302 commpos = 0;
303 text[0] = '\0';
304 }
305 }
306 }
307 }
308
309 /* not reached */
310 }