summaryrefslogtreecommitdiffstats
path: root/hunt/hunt/server.c
blob: c2e9021d9a78d23e25fb94fc621aa30716222cda (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*	$NetBSD: server.c,v 1.8 2014/03/30 04:57:37 dholland Exp $	*/
/*
 * Copyright (c) 1983-2003, Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * + Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * + Neither the name of the University of California, San Francisco nor
 *   the names of its contributors may be used to endorse or promote
 *   products derived from this software without specific prior written
 *   permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: server.c,v 1.8 2014/03/30 04:57:37 dholland Exp $");

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <ifaddrs.h>

#include "hunt_common.h"
#include "pathnames.h"
#include "hunt_private.h"

#ifdef INTERNET

/*
 * Code for finding and talking to hunt daemons.
 */

static struct sockaddr_in *daemons;
static unsigned int numdaemons, maxdaemons;

static struct sockaddr_in *broadcastaddrs;
static int numbroadcastaddrs;

static bool initial = true;
static struct in_addr local_address;
static const char *explicit_host;
static uint16_t port;

void
serverlist_setup(const char *explicit_host_arg, uint16_t port_arg)
{
	char local_name[MAXHOSTNAMELEN + 1];
	struct hostent *hp;

	if (gethostname(local_name, sizeof(local_name)) < 0) {
		leavex(1, "Sorry, I have no hostname.");
	}
	local_name[sizeof(local_name) - 1] = '\0';
	if ((hp = gethostbyname(local_name)) == NULL) {
		leavex(1, "Can't find myself.");
	}
	memcpy(&local_address, hp->h_addr, sizeof(local_address));

	numdaemons = 0;
	maxdaemons = 20;
	daemons = malloc(maxdaemons * sizeof(daemons[0]));
	if (daemons == NULL) {
		leavex(1, "Out of memory.");
	}

	if (explicit_host_arg) {
		explicit_host = explicit_host_arg;
	}
	port = port_arg;
}

static void
add_daemon_addr(const struct sockaddr_storage *addr, socklen_t addrlen,
		uint16_t port_num)
{
	const struct sockaddr_in *sin;

	if (addr->ss_family != AF_INET) {
		return;
	}
	assert(addrlen == sizeof(struct sockaddr_in));
	sin = (const struct sockaddr_in *)addr;

	assert(numdaemons <= maxdaemons);
	if (numdaemons == maxdaemons) {
		maxdaemons += 20;
		daemons = realloc(daemons, maxdaemons * sizeof(daemons[0]));
		if (daemons == NULL) {
			leave(1, "realloc");
		}
	}

	/*
	 * Note that we do *not* convert from network to host
	 * order since the port number we were sent *should*
	 * already be in network order.
	 *
	 * The result may be either the port number for the hunt
	 * socket or the port number for the stats socket... or the
	 * number of players connected and not a port number at all,
	 * depending on the packet type.
	 *
	 * For now at least it is ok to stuff it in here, because the
	 * usage of the various different packet types and the
	 * persistence of the results vs. the program exiting does not
	 * cause us to get confused. If the game is made more
	 * self-contained in the future we'll need to be more careful
	 * about this, especially if we make the caching of results
	 * less scattershot.
	 */
	daemons[numdaemons] = *sin;
	daemons[numdaemons].sin_port = port_num;
	numdaemons++;
}

static bool
have_daemon_addr(const struct sockaddr_storage *addr, socklen_t addrlen)
{
	unsigned j;
	const struct sockaddr_in *sin;

	if (addr->ss_family != AF_INET) {
		return false;
	}
	assert(addrlen == sizeof(struct sockaddr_in));
	sin = (const struct sockaddr_in *)addr;

	for (j = 0; j < numdaemons; j++) {
		if (sin->sin_addr.s_addr == daemons[j].sin_addr.s_addr) {
			return true;
		}
	}
	return false;
}

static void
getbroadcastaddrs(void)
{
	unsigned num, i;
	struct ifaddrs *ifp, *ip;

	broadcastaddrs = NULL;
	numbroadcastaddrs = 0;

	if (getifaddrs(&ifp) < 0) {
		return;
	}

	num = 0;
	for (ip = ifp; ip; ip = ip->ifa_next) {
		if ((ip->ifa_addr->sa_family == AF_INET) &&
		    (ip->ifa_flags & IFF_BROADCAST)) {
			num++;
		}
	}

	broadcastaddrs = malloc(num * sizeof(broadcastaddrs[0]));
	if (broadcastaddrs == NULL) {
		leavex(1, "Out of memory");
	}

	i = 0;
	for (ip = ifp; ip; ip = ip->ifa_next) {
		if ((ip->ifa_addr->sa_family == AF_INET) &&
		    (ip->ifa_flags & IFF_BROADCAST)) {
			memcpy(&broadcastaddrs[i], ip->ifa_broadaddr,
			       sizeof(broadcastaddrs[i]));
			i++;
		}
	}
	assert(i == num);
	numbroadcastaddrs = num;

	freeifaddrs(ifp);
}

static void
send_messages(int contactsock, unsigned short msg)
{
	struct sockaddr_in contactaddr;
	struct hostent *hp;
	uint16_t wiremsg;
	int option;
	int i;

	memset(&contactaddr, 0, sizeof(contactaddr));
	contactaddr.sin_family = SOCK_FAMILY;
	contactaddr.sin_port = htons(port);

	if (explicit_host != NULL) {	/* explicit host given */
		hp = gethostbyname(explicit_host);
		if (hp == NULL) {
			leavex(1, "%s: Unknown host", explicit_host);
		}
		memcpy(&contactaddr.sin_addr, hp->h_addr,
		       sizeof(contactaddr.sin_addr));
		wiremsg = htons(msg);
		(void) sendto(contactsock, &wiremsg, sizeof(wiremsg), 0,
			      (struct sockaddr *)&contactaddr,
			      sizeof(contactaddr));
		return;
	}

	if (!initial) {
		/* favor host of previous session by contacting it first */
		contactaddr.sin_addr = Daemon.sin_addr;

		/* Must be playing! */
		assert(msg == C_PLAYER);
		wiremsg = htons(msg);

		(void) sendto(contactsock, &wiremsg, sizeof(wiremsg), 0,
		    (struct sockaddr *)&contactaddr, sizeof(contactaddr));
	}

	if (initial) {
		getbroadcastaddrs();
	}

#ifdef SO_BROADCAST
	/* Sun's will broadcast even though this option can't be set */
	option = 1;
	if (setsockopt(contactsock, SOL_SOCKET, SO_BROADCAST,
	    &option, sizeof option) < 0) {
		leave(1, "setsockopt broadcast");
		/* NOTREACHED */
	}
#endif

	/* send broadcast packets on all interfaces */
	wiremsg = htons(msg);
	for (i = 0; i < numbroadcastaddrs; i++) {
		contactaddr.sin_addr = broadcastaddrs[i].sin_addr;
		if (sendto(contactsock, &wiremsg, sizeof(wiremsg), 0,
		    (struct sockaddr *)&contactaddr,
		    sizeof(contactaddr)) < 0) {
			leave(1, "sendto");
		}
	}
	contactaddr.sin_addr = local_address;
	if (sendto(contactsock, &wiremsg, sizeof(wiremsg), 0,
	    (struct sockaddr *)&contactaddr, sizeof(contactaddr)) < 0) {
		leave(1, "sendto");
	}
}

static void
get_responses(int contactsock)
{
	struct pollfd set[1];
	struct sockaddr_storage addr;
	socklen_t addrlen;
	int r;
	uint16_t port_num;
	ssize_t portlen;

	/* forget all old responses */
	numdaemons = 0;

	errno = 0;
	set[0].fd = contactsock;
	set[0].events = POLLIN;
	for (;;) {
		r = poll(set, 1, 1000);
		if (r < 0) {
			if (errno == EINTR) {
				continue;
			}
			leave(1, "poll");
		}
		if (r == 0) {
			break;
		}

		addrlen = sizeof(addr);
		portlen = recvfrom(contactsock, &port_num, sizeof(port_num), 0,
				   (struct sockaddr *)&addr, &addrlen);
		if (portlen < 0) {
			if (errno == EINTR) {
				continue;
			}
			leave(1, "recvfrom");
		}
		if (portlen == 0) {
			leavex(1, "recvfrom: Unexpected EOF");
		}
		if ((size_t)portlen != sizeof(port_num)) {
			/* trash, ignore it */
			continue;
		}
		if (have_daemon_addr(&addr, addrlen)) {
			/* this shouldn't happen */
			continue;
		}

		add_daemon_addr(&addr, addrlen, port_num);
	}

	initial = false;
}

void
serverlist_query(unsigned short msg)
{
	int contactsock;

	if (!initial && explicit_host != NULL) {
		/* already did the work, no point doing it again */
		return;
	}

	contactsock = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
	if (contactsock < 0) {
		leave(1, "socket system call failed");
	}

	send_messages(contactsock, msg);
	get_responses(contactsock);

	(void) close(contactsock);
}

unsigned
serverlist_num(void)
{
	return numdaemons;
}

const struct sockaddr_storage *
serverlist_gethost(unsigned i, socklen_t *len_ret)
{
	struct sockaddr_in *ret;

	assert(i < numdaemons);
	ret = &daemons[i];
	*len_ret = sizeof(*ret);
	return (struct sockaddr_storage *)ret;
}

unsigned short
serverlist_getresponse(unsigned i)
{
	assert(i < numdaemons);
	return daemons[i].sin_port;
}

#endif /* INTERNET */