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"
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cctype>
22 #include <cstring>
23 #include <cerrno>
24
25 #ifdef HAVE_TERMIOS_H
26 # include <termios.h>
27 #endif
28
29 #include "compat/termios.h"
30 #include "compat/psock.h"
31 #include "common/packet.h"
32 #include "common/field_sizes.h"
33 #include "common/util.h"
34 #include "common/init_protocol.h"
35 #include "common/bn_type.h"
36 #include "common/network.h"
37 #ifdef CLIENTDEBUG
38 # include "common/eventlog.h"
39 #endif
40 #include "client.h"
41
42 #ifdef HAVE_ARPA_INET_H
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"
50
51 using namespace pvpgn;
52 using namespace pvpgn::client;
53
54 namespace
55 {
56
57 void usage(char const * progname)
58 {
59 std::fprintf(stderr, "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 std::exit(EXIT_FAILURE);
64 }
65
66 }
67
68 extern int main(int argc, char * argv[])
69 {
70 int a;
71 int sd;
72 struct sockaddr_in saddr;
73 t_packet * packet;
74 t_packet * rpacket;
75 char const * servname = NULL;
76 unsigned short servport = 0;
77 char text[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 std::fprintf(stderr, "bad arguments\n");
90 return EXIT_FAILURE;
91 }
92
93 for (a = 1; a < argc; a++)
94 if (servname && std::isdigit((int)argv[a][0]) && a + 1 >= argc)
95 {
96 if (str_to_ushort(argv[a], &servport) < 0)
97 {
98 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]);
99 usage(argv[0]);
100 }
101 }
102 else if (!servname && argv[a][0] != '-' && a + 2 >= argc)
103 servname = argv[a];
104 else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0)
105 {
106 std::printf("version " PVPGN_VERSION "\n");
107 return EXIT_SUCCESS;
108 }
109 else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage") == 0)
110 usage(argv[0]);
111 else
112 {
113 std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
114 usage(argv[0]);
115 }
116
117 if (servport == 0)
118 servport = BNETD_SERV_PORT;
119 if (!servname)
120 servname = BNETD_DEFAULT_HOST;
121
122 if (psock_init() < 0)
123 {
124 std::fprintf(stderr, "%s: could not inialialize socket functions\n", argv[0]);
125 return EXIT_FAILURE;
126 }
127
128 if (!(host = gethostbyname(servname)))
129 {
130 std::fprintf(stderr, "%s: unknown host \"%s\"\n", argv[0], servname);
131 return EXIT_FAILURE;
132 }
133
134 fd_stdin = fileno(stdin);
135 if (tcgetattr(fd_stdin, &in_attr_old) >= 0)
136 {
137 in_attr_new = in_attr_old;
138 in_attr_new.c_lflag &= ~(ECHO | ICANON); /* turn off ECHO and ICANON */
139 in_attr_new.c_cc[VMIN] = 0; /* don't require reads to return data */
140 in_attr_new.c_cc[VTIME] = 1; /* timeout = .1 second */
141 tcsetattr(fd_stdin, TCSANOW, &in_attr_new);
142 changed_in = 1;
143 }
144 else
145 {
146 std::fprintf(stderr, "%s: could not set terminal attributes for stdin\n", argv[0]);
147 changed_in = 0;
148 }
149
150 if (client_get_termsize(fd_stdin, &screen_width, &screen_height) < 0)
151 {
152 std::fprintf(stderr, "%s: could not determine screen size\n", argv[0]);
153 if (changed_in)
154 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
155 return EXIT_FAILURE;
156 }
157
158 if ((sd = psock_socket(PSOCK_PF_INET, PSOCK_SOCK_STREAM, PSOCK_IPPROTO_TCP)) < 0)
159 {
160 std::fprintf(stderr, "%s: could not create socket (psock_socket: %s)\n", argv[0], std::strerror(psock_errno()));
161 if (changed_in)
162 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
163 return EXIT_FAILURE;
164 }
165
166 std::memset(&saddr, 0, sizeof(saddr));
167 saddr.sin_family = PSOCK_AF_INET;
168 saddr.sin_port = htons(servport);
169 std::memcpy(&saddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
170 if (psock_connect(sd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
171 {
172 std::fprintf(stderr, "%s: could not connect to server \"%s\" port %hu (psock_connect: %s)\n", argv[0], servname, servport, std::strerror(psock_errno()));
173 if (changed_in)
174 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
175 return EXIT_FAILURE;
176 }
177
178 if (psock_ctl(sd, PSOCK_NONBLOCK) < 0)
179 {
180 std::fprintf(stderr, "%s: could not set TCP socket to non-blocking mode (psock_ctl: %s)\n", argv[0], std::strerror(psock_errno()));
181 psock_close(sd);
182 if (changed_in)
183 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
184 return EXIT_FAILURE;
185 }
186
187 char addrstr[INET_ADDRSTRLEN] = { 0 };
188 inet_ntop(AF_INET, &(saddr.sin_addr), addrstr, sizeof(addrstr));
189 std::printf("Connected to %s:%hu.\n", addrstr, servport);
190 #ifdef CLIENTDEBUG
191 eventlog_set(stderr);
192 #endif
193
194 if (!(packet = packet_create(packet_class_init)))
195 {
196 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
197 if (changed_in)
198 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
199 return EXIT_FAILURE;
200 }
201 bn_byte_set(&packet->u.client_initconn.cclass, CLIENT_INITCONN_CLASS_BOT);
202 client_blocksend_packet(sd, packet);
203 packet_del_ref(packet);
204
205 if (!(rpacket = packet_create(packet_class_raw)))
206 {
207 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
208 if (changed_in)
209 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
210 return EXIT_FAILURE;
211 }
212
213 if (!(packet = packet_create(packet_class_raw)))
214 {
215 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
216 if (changed_in)
217 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
218 return EXIT_FAILURE;
219 }
220 packet_append_ntstring(packet, "\004");
221 client_blocksend_packet(sd, packet);
222 packet_del_ref(packet);
223
224 {
225 int highest_fd;
226 t_psock_fd_set rfds;
227
228 PSOCK_FD_ZERO(&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_SET(fd_stdin, &rfds);
239 PSOCK_FD_SET(sd, &rfds);
240
241 if (psock_select(highest_fd + 1, &rfds, NULL, NULL, NULL) < 0)
242 {
243 if (errno != PSOCK_EINTR)
244 std::fprintf(stderr, "%s: select failed (select: %s)\n", argv[0], std::strerror(errno));
245 continue;
246 }
247
248 if (PSOCK_FD_ISSET(sd, &rfds)) /* got network data */
249 {
250 packet_set_size(rpacket, MAX_PACKET_SIZE - 1);
251 currsize = 0;
252 if (net_recv_packet(sd, rpacket, &currsize)<0)
253 {
254 psock_close(sd);
255 sd = -1;
256 }
257
258 if (currsize>0)
259 {
260 char * str = (char*)packet_get_raw_data(rpacket, 0);
261
262 str[currsize] = '\0';
263 std::printf("%s", str);
264 std::fflush(stdout);
265 }
266
267 if (sd == -1) /* if connection was closed */
268 {
269 std::printf("Connection closed by server.\n");
270 if (changed_in)
271 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
272 packet_del_ref(rpacket);
273 return EXIT_SUCCESS;
274 }
275 }
276
277 if (PSOCK_FD_ISSET(fd_stdin, &rfds)) /* got keyboard data */
278 {
279 switch (client_get_comm("", text, sizeof(text), &commpos, -1, 0, screen_width))
280 {
281 case -1: /* cancel */
282 if (changed_in)
283 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
284 return EXIT_FAILURE;
285
286 case 0: /* timeout */
287 break;
288
289 case 1:
290 if (!(packet = packet_create(packet_class_raw)))
291 {
292 std::fprintf(stderr, "%s: could not create packet\n", argv[0]);
293 if (changed_in)
294 tcsetattr(fd_stdin, TCSAFLUSH, &in_attr_old);
295 packet_del_ref(rpacket);
296 return EXIT_FAILURE;
297 }
298 packet_append_ntstring(packet, text);
299 packet_append_ntstring(packet, "\r\n");
300 client_blocksend_packet(sd, packet);
301 packet_del_ref(packet);
302 commpos = 0;
303 text[0] = '\0';
304 }
305 }
306 }
307 }
308
309 /* not reached */
310 }