2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * accept.c: Keep track of people logged in
30 * Known contributors to this file:
42 #include "prototypes.h"
45 #include "empthread.h"
54 #include <arpa/inet.h>
55 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <sys/ioctl.h>
68 static struct emp_qelem Players;
69 static int player_socket;
70 static int player_addrlen;
75 emp_initque(&Players);
76 init_player_commands();
78 player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
79 loginport, &player_addrlen);
87 lp = malloc(sizeof(struct player));
90 memset(lp, 0, sizeof(struct player));
92 /* real player, not dummy created by update and market update */
94 IO_READ | IO_WRITE | IO_NBLOCK,
100 emp_insque(&lp->queue, &Players);
109 player_delete(struct player *lp)
113 back = (struct player *)lp->queue.q_back;
115 emp_remque(&lp->queue);
117 /* it's a real player */
122 /* XXX may need to free bigmap here */
127 player_next(struct player *lp)
130 lp = (struct player *)Players.q_forw;
132 lp = (struct player *)lp->queue.q_forw;
133 if (&lp->queue == &Players)
139 player_prev(struct player *lp)
142 lp = (struct player *)Players.q_back;
144 lp = (struct player *)lp->queue.q_back;
145 if (&lp->queue == &Players)
151 * Return player in state PS_PLAYING for CNUM.
154 getplayer(natid cnum)
156 struct emp_qelem *qp;
159 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
160 pl = (struct player *)qp;
161 if (pl->cnum == cnum && pl->state == PS_PLAYING)
169 player_wakeup_all(natid cnum)
171 register struct player *lp;
173 if (NULL != (lp = getplayer(cnum)))
178 player_wakeup(struct player *pl)
181 empth_wakeup(pl->proc);
186 player_accept(void *unused)
188 struct sockaddr *sap;
190 int s = player_socket;
197 #ifdef RESOLVE_IPADDRESS
198 struct hostent *hostp;
201 /* auto sockaddr_storage would be simpler, but less portable */
202 sap = malloc(player_addrlen);
205 empth_select(s, EMPTH_FD_READ);
206 len = player_addrlen;
207 ns = accept(s, sap, &len);
208 /* FIXME accept() can block on some systems (RST after select() reported s ready) */
210 logerror("new socket accept");
213 /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
214 (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
217 logerror("can't create player for fd %d", ns);
221 #ifdef HAVE_GETADDRINFO
222 inaddr = sap->sa_family == AF_INET
223 ? (void *)&((struct sockaddr_in *)sap)->sin_addr
224 : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
225 /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
226 if (!inet_ntop(sap->sa_family, inaddr,
227 np->hostaddr, sizeof(np->hostaddr))) {
228 logerror("inet_ntop() failed: %s", strerror(errno));
233 inaddr = &((struct sockaddr_in *)sap)->sin_addr;
234 strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
236 #ifdef RESOLVE_IPADDRESS
237 hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
239 strcpy(np->hostname, hostp->h_name);
240 #endif /* RESOLVE_IPADDRESS */
241 /* XXX may not be big enough */
243 /* budget */ + MAX(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
244 /* power */ MAXNOC * sizeof(struct powstr));
245 sprintf(buf, "Player (fd #%d)", ns);
246 empth_create(PP_PLAYER, player_login, stacksize,
247 0, buf, "Empire player", np);