]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Clean up superfluous includes
[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 "prototypes.h"
52
53 static struct emp_qelem Players;
54 static int player_socket;
55 static size_t player_addrlen;
56
57 static const char *sockaddr_ntop(struct sockaddr *, char *, size_t);
58
59 void
60 player_init(void)
61 {
62     emp_initque(&Players);
63     init_player_commands();
64
65     player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
66                                loginport, &player_addrlen);
67 }
68
69 struct player *
70 player_new(int s)
71 {
72     struct player *lp;
73
74     lp = malloc(sizeof(struct player));
75     if (!lp)
76         return NULL;
77     memset(lp, 0, sizeof(struct player));
78     if (s >= 0) {
79         /* real player, not dummy created by update and market update */
80         lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE);
81         if (!lp->iop) {
82             free(lp);
83             return NULL;
84         }
85         emp_insque(&lp->queue, &Players);
86         lp->cnum = NATID_BAD;
87         lp->may_sleep = PLAYER_SLEEP_FREELY;
88         lp->curid = -1;
89         time(&lp->curup);
90     }
91     return lp;
92 }
93
94 struct player *
95 player_delete(struct player *lp)
96 {
97     struct player *back;
98
99     if (lp->iop) {
100         /* it's a real player */
101         io_close(lp->iop, player->curup + login_grace_time);
102         lp->iop = NULL;
103     }
104     back = (struct player *)lp->queue.q_back;
105     if (back)
106         emp_remque(&lp->queue);
107     free(lp);
108     /* XXX may need to free bigmap here */
109     return back;
110 }
111
112 struct player *
113 player_next(struct player *lp)
114 {
115     if (!lp)
116         lp = (struct player *)Players.q_forw;
117     else
118         lp = (struct player *)lp->queue.q_forw;
119     if (&lp->queue == &Players)
120         return NULL;
121     return lp;
122 }
123
124 struct player *
125 player_prev(struct player *lp)
126 {
127     if (!lp)
128         lp = (struct player *)Players.q_back;
129     else
130         lp = (struct player *)lp->queue.q_back;
131     if (&lp->queue == &Players)
132         return NULL;
133     return lp;
134 }
135
136 /*
137  * Return player in state PS_PLAYING for CNUM.
138  */
139 struct player *
140 getplayer(natid cnum)
141 {
142     struct emp_qelem *qp;
143     struct player *pl;
144
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)
148             return pl;
149     }
150
151     return NULL;
152 }
153
154 time_t
155 player_io_deadline(struct player *pl, int write)
156 {
157     if (pl->may_sleep < (write ? PLAYER_SLEEP_FREELY : PLAYER_SLEEP_ON_INPUT))
158         return 0;
159     if (pl->state != PS_PLAYING)
160         return pl->curup + login_grace_time;
161     return pl->curup
162         + minutes(pl->nstat & NONVIS ? max_idle: max_idle_visitor);
163 }
164
165 /*ARGSUSED*/
166 void
167 player_accept(void *unused)
168 {
169     static int conn_cnt;
170     struct sockaddr *sap;
171     struct player *np;
172     socklen_t len;
173     const char *p;
174     int ns;
175     int set = 1;
176     char buf[128];
177 #ifdef RESOLVE_IPADDRESS
178     struct hostent *hostp;
179 #endif
180
181     /* auto sockaddr_storage would be simpler, but less portable */
182     sap = malloc(player_addrlen);
183     len = player_addrlen;
184     if (getsockname(player_socket, sap, &len)) {
185         logerror("getsockname() failed: %s", strerror(errno));
186         p = NULL;
187     } else {
188         p = sockaddr_ntop(sap, buf, sizeof(buf));
189         CANT_HAPPEN(!p);
190     }
191     logerror("Listening on %s", p ? buf : "unknown address");
192
193     while (1) {
194         empth_select(player_socket, EMPTH_FD_READ, NULL);
195         len = player_addrlen;
196         ns = accept(player_socket, sap, &len);
197         /* FIXME accept() can block on some systems (RST after select() reports ready) */
198         if (ns < 0) {
199             logerror("new socket accept");
200             continue;
201         }
202         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
203         np = player_new(ns);
204         if (!np) {
205             logerror("can't create player for fd %d", ns);
206             close(ns);
207             continue;
208         }
209         if (!sockaddr_ntop(sap, np->hostaddr, sizeof(np->hostaddr))) {
210             CANT_REACH();
211             player_delete(np);
212             continue;
213         }
214         logerror("Connect from %s", np->hostaddr);
215 #ifdef RESOLVE_IPADDRESS
216         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
217         if (NULL != hostp)
218             strcpy(np->hostname, hostp->h_name);
219 #endif /* RESOLVE_IPADDRESS */
220         sprintf(buf, "Conn%d", conn_cnt++);
221         empth_create(player_login, 1024 * 1024, 0, buf, np);
222     }
223 }
224
225 static const char *
226 sockaddr_ntop(struct sockaddr *sap, char *buf, size_t bufsz)
227 {
228 #ifdef HAVE_GETADDRINFO
229     /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
230     sa_family_t af = sap->sa_family;
231     void *addr;
232     struct sockaddr_in6 *sap6;
233
234     if (af == AF_INET)
235         addr = &((struct sockaddr_in *)sap)->sin_addr;
236     else {
237         sap6 = (struct sockaddr_in6 *)sap;
238         addr = &sap6->sin6_addr;
239         if (IN6_IS_ADDR_V4MAPPED(&sap6->sin6_addr)) {
240             af = AF_INET;
241             addr = sap6->sin6_addr.s6_addr + 12;
242         }
243     }
244     return inet_ntop(af, addr, buf, bufsz);
245 #else
246     const char *p;
247
248     p = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
249     if (strlen(p) >= bufsz) {
250         errno = ENOSPC;
251         return NULL;
252     }
253     return strcpy(buf, p);
254 #endif
255 }