/[LeafOK_CVS]/pvpgn-1.7.4/src/client/bnbot.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/client/bnbot.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
Error occurred while calculating annotation data.
no message

1 /*
2 * Copyright (C) 1999 Ross Combs (rocombs@cs.nmsu.edu)
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 <stdio.h>
20 #ifdef HAVE_STDDEF_H
21 # include <stddef.h>
22 #else
23 # ifndef NULL
24 # define NULL ((void *)0)
25 # endif
26 #endif
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 #endif
30 #include "compat/exitstatus.h"
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #else
34 # ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 # endif
37 #endif
38 #ifdef HAVE_MEMORY_H
39 # include <memory.h>
40 #endif
41 #include "compat/memset.h"
42 #include "compat/memcpy.h"
43 #include <ctype.h>
44 #include <errno.h>
45 #include "compat/strerror.h"
46 #ifdef TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
52 # else
53 # include <time.h>
54 # endif
55 #endif
56 #ifdef HAVE_UNISTD_H
57 # include <unistd.h>
58 #endif
59 #ifdef HAVE_FCNTL_H
60 # include <fcntl.h>
61 #else
62 # ifdef HAVE_SYS_FILE_H
63 # include <sys/file.h>
64 # endif
65 #endif
66 #ifdef HAVE_TERMIOS_H
67 # include <termios.h>
68 #endif
69 #include "compat/termios.h"
70 #ifdef HAVE_SYS_TYPES_H
71 # include <sys/types.h>
72 #endif
73 #ifdef HAVE_SYS_SELECT_H
74 # include <sys/select.h>
75 #endif
76 #ifdef HAVE_SYS_SOCKET_H
77 # include <sys/socket.h>
78 #endif
79 #include "compat/socket.h"
80 #include "compat/send.h"
81 #ifdef HAVE_SYS_PARAM_H
82 # include <sys/param.h>
83 #endif
84 #ifdef HAVE_NETINET_IN_H
85 # include <netinet/in.h>
86 #endif
87 #include "compat/netinet_in.h"
88 #ifdef HAVE_ARPA_INET_H
89 # include <arpa/inet.h>
90 #endif
91 #include "compat/inet_ntoa.h"
92 #ifdef HAVE_NETDB_H
93 # include <netdb.h>
94 #endif
95 #include "compat/psock.h"
96 #include "common/packet.h"
97 #include "common/init_protocol.h"
98 #include "common/bot_protocol.h"
99 #include "common/bn_type.h"
100 #include "common/field_sizes.h"
101 #include "common/network.h"
102 #include "common/version.h"
103 #include "common/util.h"
104 #ifdef CLIENTDEBUG
105 # include "common/eventlog.h"
106 #endif
107 #include "client.h"
108 #include "common/setup_after.h"
109
110
111 static void usage(char const * progname);
112
113
114 static void usage(char const * progname)
115 {
116 fprintf(stderr,"usage: %s [<options>] [<servername> [<TCP portnumber>]]\n"
117 " -h, --help, --usage show this information and exit\n"
118 " -v, --version print version number and exit\n",
119 progname);
120 exit(STATUS_FAILURE);
121 }
122
123
124 extern int main(int argc, char * argv[])
125 {
126 int a;
127 int sd;
128 struct sockaddr_in saddr;
129 t_packet * packet;
130 t_packet * rpacket;
131 char const * servname=NULL;
132 unsigned short servport=0;
133 char text[MAX_MESSAGE_LEN];
134 struct hostent * host;
135 unsigned int commpos;
136 struct termios in_attr_old;
137 struct termios in_attr_new;
138 int changed_in;
139 unsigned int currsize;
140 int fd_stdin;
141 unsigned int screen_width,screen_height;
142
143 if (argc<1 || !argv || !argv[0])
144 {
145 fprintf(stderr,"bad arguments\n");
146 return STATUS_FAILURE;
147 }
148
149 for (a=1; a<argc; a++)
150 if (servname && isdigit((int)argv[a][0]) && a+1>=argc)
151 {
152 if (str_to_ushort(argv[a],&servport)<0)
153 {
154 fprintf(stderr,"%s: \"%s\" should be a positive integer\n",argv[0],argv[a]);
155 usage(argv[0]);
156 }
157 }
158 else if (!servname && argv[a][0]!='-' && a+2>=argc)
159 servname = argv[a];
160 else if (strcmp(argv[a],"-v")==0 || strcmp(argv[a],"--version")==0)
161 {
162 printf("version "PVPGN_VERSION"\n");
163 return STATUS_SUCCESS;
164 }
165 else if (strcmp(argv[a],"-h")==0 || strcmp(argv[a],"--help")==0 || strcmp(argv[a],"--usage")==0)
166 usage(argv[0]);
167 else
168 {
169 fprintf(stderr,"%s: unknown option \"%s\"\n",argv[0],argv[a]);
170 usage(argv[0]);
171 }
172
173 if (servport==0)
174 servport = BNETD_SERV_PORT;
175 if (!servname)
176 servname = BNETD_DEFAULT_HOST;
177
178 if (psock_init()<0)
179 {
180 fprintf(stderr,"%s: could not inialialize socket functions\n",argv[0]);
181 return STATUS_FAILURE;
182 }
183
184 if (!(host = gethostbyname(servname)))
185 {
186 fprintf(stderr,"%s: unknown host \"%s\"\n",argv[0],servname);
187 return STATUS_FAILURE;
188 }
189
190 fd_stdin = fileno(stdin);
191 if (tcgetattr(fd_stdin,&in_attr_old)>=0)
192 {
193 in_attr_new = in_attr_old;
194 in_attr_new.c_lflag &= ~(ECHO | ICANON); /* turn off ECHO and ICANON */
195 in_attr_new.c_cc[VMIN] = 0; /* don't require reads to return data */
196 in_attr_new.c_cc[VTIME] = 1; /* timeout = .1 second */
197 tcsetattr(fd_stdin,TCSANOW,&in_attr_new);
198 changed_in = 1;
199 }
200 else
201 {
202 fprintf(stderr,"%s: could not set terminal attributes for stdin\n",argv[0]);
203 changed_in = 0;
204 }
205
206 if (client_get_termsize(fd_stdin,&screen_width,&screen_height)<0)
207 {
208 fprintf(stderr,"%s: could not determine screen size\n",argv[0]);
209 if (changed_in)
210 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
211 return STATUS_FAILURE;
212 }
213
214 if ((sd = psock_socket(PSOCK_PF_INET,PSOCK_SOCK_STREAM,PSOCK_IPPROTO_TCP))<0)
215 {
216 fprintf(stderr,"%s: could not create socket (psock_socket: %s)\n",argv[0],pstrerror(psock_errno()));
217 if (changed_in)
218 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
219 return STATUS_FAILURE;
220 }
221
222 memset(&saddr,0,sizeof(saddr));
223 saddr.sin_family = PSOCK_AF_INET;
224 saddr.sin_port = htons(servport);
225 memcpy(&saddr.sin_addr.s_addr,host->h_addr_list[0],host->h_length);
226 if (psock_connect(sd,(struct sockaddr *)&saddr,sizeof(saddr))<0)
227 {
228 fprintf(stderr,"%s: could not connect to server \"%s\" port %hu (psock_connect: %s)\n",argv[0],servname,servport,pstrerror(psock_errno()));
229 if (changed_in)
230 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
231 return STATUS_FAILURE;
232 }
233
234 if (psock_ctl(sd,PSOCK_NONBLOCK)<0)
235 {
236 fprintf(stderr,"%s: could not set TCP socket to non-blocking mode (psock_ctl: %s)\n",argv[0],pstrerror(psock_errno()));
237 psock_close(sd);
238 if (changed_in)
239 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
240 return STATUS_FAILURE;
241 }
242
243 printf("Connected to %s:%hu.\n",inet_ntoa(saddr.sin_addr),servport);
244 #ifdef CLIENTDEBUG
245 eventlog_set(stderr);
246 #endif
247
248 if (!(packet = packet_create(packet_class_init)))
249 {
250 fprintf(stderr,"%s: could not create packet\n",argv[0]);
251 if (changed_in)
252 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
253 return STATUS_FAILURE;
254 }
255 bn_byte_set(&packet->u.client_initconn.class,CLIENT_INITCONN_CLASS_BOT);
256 client_blocksend_packet(sd,packet);
257 packet_del_ref(packet);
258
259 if (!(rpacket = packet_create(packet_class_raw)))
260 {
261 fprintf(stderr,"%s: could not create packet\n",argv[0]);
262 if (changed_in)
263 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
264 return STATUS_FAILURE;
265 }
266
267 if (!(packet = packet_create(packet_class_raw)))
268 {
269 fprintf(stderr,"%s: could not create packet\n",argv[0]);
270 if (changed_in)
271 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
272 return STATUS_FAILURE;
273 }
274 packet_append_ntstring(packet,"\004");
275 client_blocksend_packet(sd,packet);
276 packet_del_ref(packet);
277
278 {
279 int highest_fd;
280 t_psock_fd_set rfds;
281
282 PSOCK_FD_ZERO(&rfds);
283 highest_fd = fd_stdin;
284 if (sd>highest_fd)
285 highest_fd = sd;
286
287 commpos = 0;
288 text[0] = '\0';
289
290 for (;;)
291 {
292 PSOCK_FD_SET(fd_stdin,&rfds);
293 PSOCK_FD_SET(sd,&rfds);
294
295 if (psock_select(highest_fd+1,&rfds,NULL,NULL,NULL)<0)
296 {
297 if (errno!=PSOCK_EINTR)
298 fprintf(stderr,"%s: select failed (select: %s)\n",argv[0],pstrerror(errno));
299 continue;
300 }
301
302 if (PSOCK_FD_ISSET(sd,&rfds)) /* got network data */
303 {
304 packet_set_size(rpacket,MAX_PACKET_SIZE-1);
305 currsize = 0;
306 if (net_recv_packet(sd,rpacket,&currsize)<0)
307 {
308 psock_close(sd);
309 sd = -1;
310 }
311
312 if (currsize>0)
313 {
314 char * str=packet_get_raw_data(rpacket,0);
315
316 str[currsize] = '\0';
317 printf("%s",str);
318 fflush(stdout);
319 }
320
321 if (sd==-1) /* if connection was closed */
322 {
323 printf("Connection closed by server.\n");
324 if (changed_in)
325 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
326 packet_del_ref(rpacket);
327 return STATUS_SUCCESS;
328 }
329 }
330
331 if (PSOCK_FD_ISSET(fd_stdin,&rfds)) /* got keyboard data */
332 {
333 switch (client_get_comm("",text,sizeof(text),&commpos,-1,0,screen_width))
334 {
335 case -1: /* cancel */
336 if (changed_in)
337 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
338 return STATUS_FAILURE;
339
340 case 0: /* timeout */
341 break;
342
343 case 1:
344 if (!(packet = packet_create(packet_class_raw)))
345 {
346 fprintf(stderr,"%s: could not create packet\n",argv[0]);
347 if (changed_in)
348 tcsetattr(fd_stdin,TCSAFLUSH,&in_attr_old);
349 packet_del_ref(rpacket);
350 return STATUS_FAILURE;
351 }
352 packet_append_ntstring(packet,text);
353 packet_append_ntstring(packet,"\r\n");
354 client_blocksend_packet(sd,packet);
355 packet_del_ref(packet);
356 commpos = 0;
357 text[0] = '\0';
358 }
359 }
360 }
361 }
362
363 /* not reached */
364 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1