2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, 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-2020
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
46 #include "empthread.h"
51 #include "prototypes.h"
53 static struct emp_qelem Players;
54 static int player_socket;
55 static size_t player_addrlen;
57 static const char *sockaddr_ntop(struct sockaddr *, char *, size_t);
62 emp_initque(&Players);
63 init_player_commands();
65 player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
66 loginport, &player_addrlen);
74 lp = malloc(sizeof(struct player));
77 memset(lp, 0, sizeof(struct player));
79 /* real player, not dummy created by update and market update */
80 lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE);
85 emp_insque(&lp->queue, &Players);
87 lp->may_sleep = PLAYER_SLEEP_FREELY;
95 player_delete(struct player *lp)
100 /* it's a real player */
101 io_close(lp->iop, player->curup + login_grace_time);
104 back = (struct player *)lp->queue.q_back;
106 emp_remque(&lp->queue);
108 /* XXX may need to free bigmap here */
113 player_next(struct player *lp)
116 lp = (struct player *)Players.q_forw;
118 lp = (struct player *)lp->queue.q_forw;
119 if (&lp->queue == &Players)
125 player_prev(struct player *lp)
128 lp = (struct player *)Players.q_back;
130 lp = (struct player *)lp->queue.q_back;
131 if (&lp->queue == &Players)
137 * Return player in state PS_PLAYING for @cnum.
140 getplayer(natid cnum)
142 struct emp_qelem *qp;
145 for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
146 pl = (struct player *)qp;
147 if (pl->cnum == cnum && pl->state == PS_PLAYING)
155 player_io_deadline(struct player *pl, int write)
157 if (pl->may_sleep < (write ? PLAYER_SLEEP_FREELY : PLAYER_SLEEP_ON_INPUT))
159 if (pl->state != PS_PLAYING)
160 return pl->curup + login_grace_time;
162 + minutes(pl->nstat & NONVIS ? max_idle: max_idle_visitor);
167 player_accept(void *unused)
170 struct sockaddr *sap;
178 /* auto sockaddr_storage would be simpler, but less portable */
179 sap = malloc(player_addrlen);
180 len = player_addrlen;
181 if (getsockname(player_socket, sap, &len)) {
182 logerror("getsockname() failed: %s", strerror(errno));
185 p = sockaddr_ntop(sap, buf, sizeof(buf));
188 logerror("Listening on %s", p ? buf : "unknown address");
191 empth_select(player_socket, EMPTH_FD_READ, NULL);
192 len = player_addrlen;
193 ns = accept(player_socket, sap, &len);
194 /* FIXME accept() can block on some systems (RST after select() reports ready) */
196 logerror("new socket accept");
199 (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
202 logerror("can't create player for fd %d", ns);
206 if (!sockaddr_ntop(sap, np->hostaddr, sizeof(np->hostaddr))) {
211 logerror("Connect from %s", np->hostaddr);
212 sprintf(buf, "Conn%d", conn_cnt++);
213 empth_create(player_login, 1024 * 1024, 0, buf, np);
218 sockaddr_ntop(struct sockaddr *sap, char *buf, size_t bufsz)
220 #ifdef HAVE_GETADDRINFO
221 /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
222 sa_family_t af = sap->sa_family;
224 struct sockaddr_in6 *sap6;
227 addr = &((struct sockaddr_in *)sap)->sin_addr;
229 sap6 = (struct sockaddr_in6 *)sap;
230 addr = &sap6->sin6_addr;
231 #ifdef HAVE_WORKING_IN6_IS_ADDR_V4MAPPED
232 if (IN6_IS_ADDR_V4MAPPED(&sap6->sin6_addr)) {
234 addr = sap6->sin6_addr.s6_addr + 12;
238 return inet_ntop(af, addr, buf, bufsz);
242 p = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
243 if (strlen(p) >= bufsz) {
247 return strcpy(buf, p);