]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
310684946c73d31add64e1e8187c43cc04ebf22d
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  accept.c: Keep track of people logged in
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Markus Armbruster, 2005-2008
33  */
34
35 #include <config.h>
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <netdb.h>
44 #include <unistd.h>
45
46 #include "empio.h"
47 #include "empthread.h"
48 #include "file.h"
49 #include "misc.h"
50 #include "nat.h"
51 #include "optlist.h"
52 #include "player.h"
53 #include "power.h"
54 #include "proto.h"
55 #include "prototypes.h"
56
57 static struct emp_qelem Players;
58 static int player_socket;
59 static size_t player_addrlen;
60
61 void
62 player_init(void)
63 {
64     emp_initque(&Players);
65     init_player_commands();
66
67     player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
68                                loginport, &player_addrlen);
69 }
70
71 struct player *
72 player_new(int s)
73 {
74     struct player *lp;
75     struct timeval idle_timeout;
76
77     lp = malloc(sizeof(struct player));
78     if (!lp)
79       return NULL;
80     memset(lp, 0, sizeof(struct player));
81     idle_timeout.tv_sec = max_idle * 60;
82     idle_timeout.tv_usec = 0 ;
83     if (s >= 0) {
84         /* real player, not dummy created by update and market update */
85         lp->iop = io_open(s, IO_READ | IO_WRITE | IO_NBLOCK, IO_BUFSIZE,
86                           idle_timeout);
87         if (!lp->iop) {
88             free(lp);
89             return NULL;
90         }
91         emp_insque(&lp->queue, &Players);
92         lp->cnum = NATID_BAD;
93         lp->curid = -1;
94         time(&lp->curup);
95     }
96     return lp;
97 }
98
99 struct player *
100 player_delete(struct player *lp)
101 {
102     struct player *back;
103
104     back = (struct player *)lp->queue.q_back;
105     if (back)
106         emp_remque(&lp->queue);
107     if (lp->iop) {
108         /* it's a real player */
109         io_close(lp->iop);
110         lp->iop = 0;
111     }
112     free(lp);
113     /* XXX may need to free bigmap here */
114     return back;
115 }
116
117 struct player *
118 player_next(struct player *lp)
119 {
120     if (lp == 0)
121         lp = (struct player *)Players.q_forw;
122     else
123         lp = (struct player *)lp->queue.q_forw;
124     if (&lp->queue == &Players)
125         return 0;
126     return lp;
127 }
128
129 struct player *
130 player_prev(struct player *lp)
131 {
132     if (lp == 0)
133         lp = (struct player *)Players.q_back;
134     else
135         lp = (struct player *)lp->queue.q_back;
136     if (&lp->queue == &Players)
137         return 0;
138     return lp;
139 }
140
141 /*
142  * Return player in state PS_PLAYING for CNUM.
143  */
144 struct player *
145 getplayer(natid cnum)
146 {
147     struct emp_qelem *qp;
148     struct player *pl;
149
150     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
151         pl = (struct player *)qp;
152         if (pl->cnum == cnum && pl->state == PS_PLAYING)
153             return pl;
154     }
155
156     return NULL;
157 }
158
159 /*ARGSUSED*/
160 void
161 player_accept(void *unused)
162 {
163     struct sockaddr *sap;
164     void *inaddr;
165     int s = player_socket;
166     struct player *np;
167     socklen_t len;
168     int ns;
169     int set = 1;
170     int stacksize;
171     char buf[128];
172 #ifdef RESOLVE_IPADDRESS
173     struct hostent *hostp;
174 #endif
175
176     /* auto sockaddr_storage would be simpler, but less portable */
177     sap = malloc(player_addrlen);
178
179     while (1) {
180         empth_select(s, EMPTH_FD_READ, NULL);
181         len = player_addrlen;
182         ns = accept(s, sap, &len);
183         /* FIXME accept() can block on some systems (RST after select() reports ready) */
184         if (ns < 0) {
185             logerror("new socket accept");
186             continue;
187         }
188         /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
189         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
190         np = player_new(ns);
191         if (!np) {
192             logerror("can't create player for fd %d", ns);
193             close(ns);
194             continue;
195         }
196 #ifdef HAVE_GETADDRINFO
197         inaddr = sap->sa_family == AF_INET
198             ? (void *)&((struct sockaddr_in *)sap)->sin_addr
199             : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
200         /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
201         if (!inet_ntop(sap->sa_family, inaddr,
202                        np->hostaddr, sizeof(np->hostaddr))) {
203             logerror("inet_ntop() failed: %s", strerror(errno));
204             close(ns);
205             continue;
206         }
207 #else
208         inaddr = &((struct sockaddr_in *)sap)->sin_addr;
209         strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
210 #endif
211 #ifdef RESOLVE_IPADDRESS
212         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
213         if (NULL != hostp)
214             strcpy(np->hostname, hostp->h_name);
215 #endif /* RESOLVE_IPADDRESS */
216         /* FIXME ancient black magic; figure out true stack need */
217         stacksize = 100000
218 /* budget */  + MAX(WORLD_SZ() * sizeof(int) * 7,
219 /* power */ MAXNOC * sizeof(struct powstr));
220         sprintf(buf, "Conn%d", ns);
221         empth_create(player_login, stacksize, 0, buf, np);
222     }
223 }