]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Update copyright notice
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  accept.c: Keep track of people logged in
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Markus Armbruster, 2005-2013
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <netdb.h>
43 #include <unistd.h>
44
45 #include "empio.h"
46 #include "empthread.h"
47 #include "misc.h"
48 #include "nat.h"
49 #include "optlist.h"
50 #include "player.h"
51 #include "power.h"
52 #include "prototypes.h"
53
54 static struct emp_qelem Players;
55 static int player_socket;
56 static size_t player_addrlen;
57
58 static const char *sockaddr_ntop(struct sockaddr *, char *, size_t);
59
60 void
61 player_init(void)
62 {
63     emp_initque(&Players);
64     init_player_commands();
65
66     player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
67                                loginport, &player_addrlen);
68 }
69
70 struct player *
71 player_new(int s)
72 {
73     struct player *lp;
74
75     lp = malloc(sizeof(struct player));
76     if (!lp)
77         return NULL;
78     memset(lp, 0, sizeof(struct player));
79     if (s >= 0) {
80         /* real player, not dummy created by update and market update */
81         lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE);
82         if (!lp->iop) {
83             free(lp);
84             return NULL;
85         }
86         emp_insque(&lp->queue, &Players);
87         lp->cnum = NATID_BAD;
88         lp->may_sleep = PLAYER_SLEEP_FREELY;
89         lp->curid = -1;
90         time(&lp->curup);
91     }
92     return lp;
93 }
94
95 struct player *
96 player_delete(struct player *lp)
97 {
98     struct player *back;
99
100     if (lp->iop) {
101         /* it's a real player */
102         io_close(lp->iop, player->curup + login_grace_time);
103         lp->iop = NULL;
104     }
105     back = (struct player *)lp->queue.q_back;
106     if (back)
107         emp_remque(&lp->queue);
108     free(lp);
109     /* XXX may need to free bigmap here */
110     return back;
111 }
112
113 struct player *
114 player_next(struct player *lp)
115 {
116     if (!lp)
117         lp = (struct player *)Players.q_forw;
118     else
119         lp = (struct player *)lp->queue.q_forw;
120     if (&lp->queue == &Players)
121         return NULL;
122     return lp;
123 }
124
125 struct player *
126 player_prev(struct player *lp)
127 {
128     if (!lp)
129         lp = (struct player *)Players.q_back;
130     else
131         lp = (struct player *)lp->queue.q_back;
132     if (&lp->queue == &Players)
133         return NULL;
134     return lp;
135 }
136
137 /*
138  * Return player in state PS_PLAYING for CNUM.
139  */
140 struct player *
141 getplayer(natid cnum)
142 {
143     struct emp_qelem *qp;
144     struct player *pl;
145
146     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
147         pl = (struct player *)qp;
148         if (pl->cnum == cnum && pl->state == PS_PLAYING)
149             return pl;
150     }
151
152     return NULL;
153 }
154
155 time_t
156 player_io_deadline(struct player *pl, int write)
157 {
158     if (pl->may_sleep < (write ? PLAYER_SLEEP_FREELY : PLAYER_SLEEP_ON_INPUT))
159         return 0;
160     if (pl->state != PS_PLAYING)
161         return pl->curup + login_grace_time;
162     return pl->curup
163         + minutes(pl->nstat & NONVIS ? max_idle: max_idle_visitor);
164 }
165
166 /*ARGSUSED*/
167 void
168 player_accept(void *unused)
169 {
170     static int conn_cnt;
171     struct sockaddr *sap;
172     struct player *np;
173     socklen_t len;
174     const char *p;
175     int ns;
176     int set = 1;
177     int stacksize;
178     char buf[128];
179 #ifdef RESOLVE_IPADDRESS
180     struct hostent *hostp;
181 #endif
182
183     /* auto sockaddr_storage would be simpler, but less portable */
184     sap = malloc(player_addrlen);
185     len = player_addrlen;
186     if (getsockname(player_socket, sap, &len)) {
187         logerror("getsockname() failed: %s", strerror(errno));
188         p = NULL;
189     } else {
190         p = sockaddr_ntop(sap, buf, sizeof(buf));
191         CANT_HAPPEN(!p);
192     }
193     logerror("Listening on %s", p ? buf : "unknown address");
194
195     while (1) {
196         empth_select(player_socket, EMPTH_FD_READ, NULL);
197         len = player_addrlen;
198         ns = accept(player_socket, sap, &len);
199         /* FIXME accept() can block on some systems (RST after select() reports ready) */
200         if (ns < 0) {
201             logerror("new socket accept");
202             continue;
203         }
204         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
205         np = player_new(ns);
206         if (!np) {
207             logerror("can't create player for fd %d", ns);
208             close(ns);
209             continue;
210         }
211         if (!sockaddr_ntop(sap, np->hostaddr, sizeof(np->hostaddr))) {
212             CANT_REACH();
213             player_delete(np);
214             continue;
215         }
216         logerror("Connect from %s", np->hostaddr);
217 #ifdef RESOLVE_IPADDRESS
218         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
219         if (NULL != hostp)
220             strcpy(np->hostname, hostp->h_name);
221 #endif /* RESOLVE_IPADDRESS */
222         /* FIXME ancient black magic; figure out true stack need */
223         stacksize = 100000
224 /* budget */  + MAX(WORLD_SZ() * sizeof(int) * 7,
225 /* power */ MAXNOC * sizeof(struct powstr));
226         sprintf(buf, "Conn%d", conn_cnt++);
227         empth_create(player_login, stacksize, 0, buf, np);
228     }
229 }
230
231 static const char *
232 sockaddr_ntop(struct sockaddr *sap, char *buf, size_t bufsz)
233 {
234 #ifdef HAVE_GETADDRINFO
235     /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
236     sa_family_t af = sap->sa_family;
237     void *addr;
238     struct sockaddr_in6 *sap6;
239
240     if (af == AF_INET)
241         addr = &((struct sockaddr_in *)sap)->sin_addr;
242     else {
243         sap6 = (struct sockaddr_in6 *)sap;
244         addr = &sap6->sin6_addr;
245         if (IN6_IS_ADDR_V4MAPPED(&sap6->sin6_addr)) {
246             af = AF_INET;
247             addr = sap6->sin6_addr.s6_addr + 12;
248         }
249     }
250     return inet_ntop(af, addr, buf, bufsz);
251 #else
252     const char *p;
253
254     p = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
255     if (strlen(p) >= bufsz) {
256         errno = ENOSPC;
257         return NULL;
258     }
259     return strcpy(buf, p);
260 #endif
261 }