]> 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-2012, 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-2012
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 void
59 player_init(void)
60 {
61     emp_initque(&Players);
62     init_player_commands();
63
64     player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
65                                loginport, &player_addrlen);
66 }
67
68 struct player *
69 player_new(int s)
70 {
71     struct player *lp;
72
73     lp = malloc(sizeof(struct player));
74     if (!lp)
75         return NULL;
76     memset(lp, 0, sizeof(struct player));
77     if (s >= 0) {
78         /* real player, not dummy created by update and market update */
79         lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE);
80         if (!lp->iop) {
81             free(lp);
82             return NULL;
83         }
84         emp_insque(&lp->queue, &Players);
85         lp->cnum = NATID_BAD;
86         lp->may_sleep = PLAYER_SLEEP_FREELY;
87         lp->curid = -1;
88         time(&lp->curup);
89     }
90     return lp;
91 }
92
93 struct player *
94 player_delete(struct player *lp)
95 {
96     struct player *back;
97
98     if (lp->iop) {
99         /* it's a real player */
100         io_close(lp->iop, player->curup + login_grace_time);
101         lp->iop = NULL;
102     }
103     back = (struct player *)lp->queue.q_back;
104     if (back)
105         emp_remque(&lp->queue);
106     free(lp);
107     /* XXX may need to free bigmap here */
108     return back;
109 }
110
111 struct player *
112 player_next(struct player *lp)
113 {
114     if (!lp)
115         lp = (struct player *)Players.q_forw;
116     else
117         lp = (struct player *)lp->queue.q_forw;
118     if (&lp->queue == &Players)
119         return NULL;
120     return lp;
121 }
122
123 struct player *
124 player_prev(struct player *lp)
125 {
126     if (!lp)
127         lp = (struct player *)Players.q_back;
128     else
129         lp = (struct player *)lp->queue.q_back;
130     if (&lp->queue == &Players)
131         return NULL;
132     return lp;
133 }
134
135 /*
136  * Return player in state PS_PLAYING for CNUM.
137  */
138 struct player *
139 getplayer(natid cnum)
140 {
141     struct emp_qelem *qp;
142     struct player *pl;
143
144     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
145         pl = (struct player *)qp;
146         if (pl->cnum == cnum && pl->state == PS_PLAYING)
147             return pl;
148     }
149
150     return NULL;
151 }
152
153 time_t
154 player_io_deadline(struct player *pl, int write)
155 {
156     if (pl->may_sleep < (write ? PLAYER_SLEEP_FREELY : PLAYER_SLEEP_ON_INPUT))
157         return 0;
158     if (pl->state != PS_PLAYING)
159         return pl->curup + login_grace_time;
160     return pl->curup
161         + minutes(pl->nstat & NONVIS ? max_idle: max_idle_visitor);
162 }
163
164 /*ARGSUSED*/
165 void
166 player_accept(void *unused)
167 {
168     struct sockaddr *sap;
169     void *inaddr;
170     int s = player_socket;
171     struct player *np;
172     socklen_t len;
173     int ns;
174     int set = 1;
175     int stacksize;
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
184     while (1) {
185         empth_select(s, EMPTH_FD_READ, NULL);
186         len = player_addrlen;
187         ns = accept(s, sap, &len);
188         /* FIXME accept() can block on some systems (RST after select() reports ready) */
189         if (ns < 0) {
190             logerror("new socket accept");
191             continue;
192         }
193         /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
194         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
195         np = player_new(ns);
196         if (!np) {
197             logerror("can't create player for fd %d", ns);
198             close(ns);
199             continue;
200         }
201 #ifdef HAVE_GETADDRINFO
202         inaddr = sap->sa_family == AF_INET
203             ? (void *)&((struct sockaddr_in *)sap)->sin_addr
204             : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
205         /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
206         if (!inet_ntop(sap->sa_family, inaddr,
207                        np->hostaddr, sizeof(np->hostaddr))) {
208             logerror("inet_ntop() failed: %s", strerror(errno));
209             player_delete(np);
210             continue;
211         }
212 #else
213         inaddr = &((struct sockaddr_in *)sap)->sin_addr;
214         strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
215 #endif
216 #ifdef RESOLVE_IPADDRESS
217         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
218         if (NULL != hostp)
219             strcpy(np->hostname, hostp->h_name);
220 #endif /* RESOLVE_IPADDRESS */
221         /* FIXME ancient black magic; figure out true stack need */
222         stacksize = 100000
223 /* budget */  + MAX(WORLD_SZ() * sizeof(int) * 7,
224 /* power */ MAXNOC * sizeof(struct powstr));
225         sprintf(buf, "Conn%d", ns);
226         empth_create(player_login, stacksize, 0, buf, np);
227     }
228 }