-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_server.c
211 lines (169 loc) · 4.53 KB
/
test_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifndef WIN32
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#else
#include <winsock.h>
#endif
#include "net.h"
#include "data.h"
#include "cfg.h"
#include "getopt.h"
#include "error.h"
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
static int port=DEFAULT_PORT;
static struct sockaddr_in server;
static char *name=NULL;
static int ttime=1;
static int verbose=0; /* whether to print a verbose message */
static int toplist=0; /* whether to print the list of top players */
int fd;
/* print info based on the recieved packet */
static void print_info (char * packet)
{
int n_pl; /* number of players */
n_pl=get_int(packet+1);
if (verbose) {
if (n_pl)
printf ((n_pl>1)? "There are %d players on the server.\n"
: "There is %d player on the server.\n", n_pl);
else
printf ("This server is not being used.\n");
} else
printf("%d\n",n_pl);
if (toplist && n_pl) {
unsigned int n_tp=*(packet+5); /* number of players on top list */
if (!n_tp)
printf("The top list is empty.\n");
else {
unsigned int i; /* iterator */
/* pointer to the next set of player-specific info */
char * p = packet+6;
packet[256-1]='\0';
printf ("Players on top list (%d):\n", n_tp);
printf ("rank\tfrags\tdeaths\tcolor\tname\n");
for (i=0; i<n_tp; i++) {
printf ("#%d\t%d\t%d\t%d\t%s\n",
i+1, get_int(p), get_int(p+4),
*(p+8), p+9);
p+=10+strlen(p+9);
}
}
}
}
/* find server address from name and put it into server structure */
static void find_server(void)
{
struct hostent *h;
h=gethostbyname(name);
if (!h){fprintf(stderr,"Error: Can't resolve server address.\n");exit(1);}
server.sin_family=AF_INET;
server.sin_port=htons(port);
server.sin_addr=*((struct in_addr*)(h->h_addr_list[0]));
}
/* find a socket and and initialize it */
static void init_socket(void)
{
fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(fd<0){fprintf(stderr,"Can't get socket.\n");exit(1);}
}
/* contact server, send information request and wait for response */
static int contact_server(void)
{
static char packet[256];
fd_set fds;
struct timeval tv;
tv.tv_sec=ttime;
tv.tv_usec=0;
FD_ZERO(&fds);
FD_SET(fd,&fds);
packet[0]=P_INFO;
send_packet(packet,1,(struct sockaddr*)(&server),0,0);
if (!select(fd+1,&fds,NULL,NULL,&tv)){fprintf(stderr,"No reply within %i second%s.\n",ttime,ttime==1?"":"s");return 1;}
if (recv_packet(packet,256,0,0,1,0,0)<0)
{
if (errno==EINTR){fprintf(stderr,"Server hung up.\n");return 1;}
else {fprintf(stderr,"Connection refused.\n");return 1;}
}
if ((*packet)!=P_INFO){fprintf(stderr,"Server error.\n");return 1;}
print_info(packet);
return 0;
}
/* write help to stdout */
static void print_help(void)
{
printf( "0verkill server testing program.\n"
"(c)2000 Brainsoft\n"
"Usage: test_server [-hvT] [-t <timeout>] [-p <port number>] -a <address>\n"
" -v produce verbose output\n"
" -T print the list of top players (implies -v)\n");
}
static int parse_number (const char * arg)
{
char *c; /* used by strtoul */
int n;
n=strtoul(arg,&c,10);
if (*c){fprintf(stderr,"Error: Not a number.\n");exit(1);}
if (errno==ERANGE)
{
if (!port){fprintf(stderr,"Error: Number underflow.\n");exit(1);}
else {fprintf(stderr,"Error: Number overflow.\n");exit(1);}
}
return n;
}
static void parse_command_line(int argc,char **argv)
{
int a;
while((a=getopt(argc,argv,"hvTp:a:t:")) != -1)
{
switch(a)
{
case 'T':
toplist = 1;
case 'v':
verbose = 1;
break;
case 'a':
name=optarg;
break;
case 'p':
port=parse_number(optarg);
break;
case 't':
ttime=parse_number(optarg);
if(ttime<1){fprintf(stderr,"Error: Timeout too low.\n");exit(1);}
break;
case '?':
case ':':
case 'h':
print_help();
exit(0);
}
}
}
int main(int argc, char **argv)
{
int ret;
#ifdef WIN32
WSADATA wd;
WSAStartup(0x101, &wd);
#endif
parse_command_line(argc,argv);
if (!name){print_help();exit(1);}
init_socket();
find_server();
ret=contact_server();
free_packet_buffer();
if(fd) close(fd);
return ret;
}