2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * accept.c: Keep track of people logged in
29 * Known contributors to this file:
31 * Markus Armbruster, 2005-2012
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
46 #include "empthread.h"
54 #include "prototypes.h"
56 static struct emp_qelem Players;
57 static int player_socket;
58 static size_t player_addrlen;
63 emp_initque(&Players);
64 init_player_commands();
66 player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
67 loginport, &player_addrlen);
75 lp = malloc(sizeof(struct player));
78 memset(lp, 0, sizeof(struct player));
80 /* real player, not dummy created by update and market update */
81 lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE);
86 emp_insque(&lp->queue, &Players);
88 lp->may_sleep = PLAYER_SLEEP_FREELY;
96 player_delete(struct player *lp)
98 struct timeval timeout;
102 /* it's a real player */
103 io_timeout(&timeout, player->curup + minutes(max_idle));
104 io_close(lp->iop, &timeout);
107 back = (struct player *)lp->queue.q_back;
109 emp_remque(&lp->queue);
111 /* XXX may need to free bigmap here */
116 player_next(struct player *lp)
119 lp = (struct player *)Players.q_forw;
121 lp = (struct player *)lp->queue.q_forw;
122 if (&lp->queue == &Players)
128 player_prev(struct player *lp)
131 lp = (struct player *)Players.q_back;
133 lp = (struct player *)lp->queue.q_back;
134 if (&lp->queue == &Players)
140 * Return player in state PS_PLAYING for CNUM.
143 getplayer(natid cnum)
145 struct emp_qelem *qp;
148 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
149 pl = (struct player *)qp;
150 if (pl->cnum == cnum && pl->state == PS_PLAYING)
159 player_accept(void *unused)
161 struct sockaddr *sap;
163 int s = player_socket;
170 #ifdef RESOLVE_IPADDRESS
171 struct hostent *hostp;
174 /* auto sockaddr_storage would be simpler, but less portable */
175 sap = malloc(player_addrlen);
178 empth_select(s, EMPTH_FD_READ, NULL);
179 len = player_addrlen;
180 ns = accept(s, sap, &len);
181 /* FIXME accept() can block on some systems (RST after select() reports ready) */
183 logerror("new socket accept");
186 /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
187 (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
190 logerror("can't create player for fd %d", ns);
194 #ifdef HAVE_GETADDRINFO
195 inaddr = sap->sa_family == AF_INET
196 ? (void *)&((struct sockaddr_in *)sap)->sin_addr
197 : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
198 /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
199 if (!inet_ntop(sap->sa_family, inaddr,
200 np->hostaddr, sizeof(np->hostaddr))) {
201 logerror("inet_ntop() failed: %s", strerror(errno));
206 inaddr = &((struct sockaddr_in *)sap)->sin_addr;
207 strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
209 #ifdef RESOLVE_IPADDRESS
210 hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
212 strcpy(np->hostname, hostp->h_name);
213 #endif /* RESOLVE_IPADDRESS */
214 /* FIXME ancient black magic; figure out true stack need */
216 /* budget */ + MAX(WORLD_SZ() * sizeof(int) * 7,
217 /* power */ MAXNOC * sizeof(struct powstr));
218 sprintf(buf, "Conn%d", ns);
219 empth_create(player_login, stacksize, 0, buf, np);