]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / player / accept.c
1 /*
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
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-2009
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 "file.h"
48 #include "misc.h"
49 #include "nat.h"
50 #include "optlist.h"
51 #include "player.h"
52 #include "power.h"
53 #include "proto.h"
54 #include "prototypes.h"
55
56 static struct emp_qelem Players;
57 static int player_socket;
58 static size_t player_addrlen;
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     struct timeval idle_timeout;
75
76     lp = malloc(sizeof(struct player));
77     if (!lp)
78         return NULL;
79     memset(lp, 0, sizeof(struct player));
80     idle_timeout.tv_sec = max_idle * 60;
81     idle_timeout.tv_usec = 0;
82     if (s >= 0) {
83         /* real player, not dummy created by update and market update */
84         lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE, idle_timeout);
85         if (!lp->iop) {
86             free(lp);
87             return NULL;
88         }
89         emp_insque(&lp->queue, &Players);
90         lp->cnum = NATID_BAD;
91         lp->may_sleep = PLAYER_SLEEP_FREELY;
92         lp->curid = -1;
93         time(&lp->curup);
94     }
95     return lp;
96 }
97
98 struct player *
99 player_delete(struct player *lp)
100 {
101     struct player *back;
102
103     back = (struct player *)lp->queue.q_back;
104     if (back)
105         emp_remque(&lp->queue);
106     if (lp->iop) {
107         /* it's a real player */
108         io_close(lp->iop);
109         lp->iop = NULL;
110     }
111     free(lp);
112     /* XXX may need to free bigmap here */
113     return back;
114 }
115
116 struct player *
117 player_next(struct player *lp)
118 {
119     if (!lp)
120         lp = (struct player *)Players.q_forw;
121     else
122         lp = (struct player *)lp->queue.q_forw;
123     if (&lp->queue == &Players)
124         return NULL;
125     return lp;
126 }
127
128 struct player *
129 player_prev(struct player *lp)
130 {
131     if (!lp)
132         lp = (struct player *)Players.q_back;
133     else
134         lp = (struct player *)lp->queue.q_back;
135     if (&lp->queue == &Players)
136         return NULL;
137     return lp;
138 }
139
140 /*
141  * Return player in state PS_PLAYING for CNUM.
142  */
143 struct player *
144 getplayer(natid cnum)
145 {
146     struct emp_qelem *qp;
147     struct player *pl;
148
149     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
150         pl = (struct player *)qp;
151         if (pl->cnum == cnum && pl->state == PS_PLAYING)
152             return pl;
153     }
154
155     return NULL;
156 }
157
158 /*ARGSUSED*/
159 void
160 player_accept(void *unused)
161 {
162     struct sockaddr *sap;
163     void *inaddr;
164     int s = player_socket;
165     struct player *np;
166     socklen_t len;
167     int ns;
168     int set = 1;
169     int stacksize;
170     char buf[128];
171 #ifdef RESOLVE_IPADDRESS
172     struct hostent *hostp;
173 #endif
174
175     /* auto sockaddr_storage would be simpler, but less portable */
176     sap = malloc(player_addrlen);
177
178     while (1) {
179         empth_select(s, EMPTH_FD_READ, NULL);
180         len = player_addrlen;
181         ns = accept(s, sap, &len);
182         /* FIXME accept() can block on some systems (RST after select() reports ready) */
183         if (ns < 0) {
184             logerror("new socket accept");
185             continue;
186         }
187         /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
188         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
189         np = player_new(ns);
190         if (!np) {
191             logerror("can't create player for fd %d", ns);
192             close(ns);
193             continue;
194         }
195 #ifdef HAVE_GETADDRINFO
196         inaddr = sap->sa_family == AF_INET
197             ? (void *)&((struct sockaddr_in *)sap)->sin_addr
198             : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
199         /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
200         if (!inet_ntop(sap->sa_family, inaddr,
201                        np->hostaddr, sizeof(np->hostaddr))) {
202             logerror("inet_ntop() failed: %s", strerror(errno));
203             player_delete(np);
204             continue;
205         }
206 #else
207         inaddr = &((struct sockaddr_in *)sap)->sin_addr;
208         strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
209 #endif
210 #ifdef RESOLVE_IPADDRESS
211         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
212         if (NULL != hostp)
213             strcpy(np->hostname, hostp->h_name);
214 #endif /* RESOLVE_IPADDRESS */
215         /* FIXME ancient black magic; figure out true stack need */
216         stacksize = 100000
217 /* budget */  + MAX(WORLD_SZ() * sizeof(int) * 7,
218 /* power */ MAXNOC * sizeof(struct powstr));
219         sprintf(buf, "Conn%d", ns);
220         empth_create(player_login, stacksize, 0, buf, np);
221     }
222 }