]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Update known contributors comment.
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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
33  */
34
35 #include <config.h>
36
37 #if defined(_WIN32)
38 #define WIN32
39 #include <winsock2.h>
40 #undef NS_ALL
41 #endif
42
43 #include "prototypes.h"
44 #include "misc.h"
45 #include "proto.h"
46 #include "empthread.h"
47 #include "player.h"
48 #include "file.h"
49 #include "empio.h"
50 #include "power.h"
51 #include "gen.h"
52 #include "optlist.h"
53
54 #if !defined(_WIN32)
55 #include <arpa/inet.h>
56 #include <sys/socket.h>
57 #include <sys/time.h>
58 #include <netdb.h>
59 #include <netinet/in.h>
60 #include <sys/ioctl.h>
61 #include <unistd.h>
62 #endif
63 #include <signal.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <stdlib.h>
67 #include <stdio.h>
68
69 static struct emp_qelem Players;
70 static int player_socket;
71 static int player_addrlen;
72
73 void
74 player_init(void)
75 {
76     emp_initque(&Players);
77     init_player_commands();
78
79     player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
80                                loginport, &player_addrlen);
81 }
82
83 struct player *
84 player_new(int s)
85 {
86     struct player *lp;
87
88     lp = malloc(sizeof(struct player));
89     if (!lp)
90       return NULL;
91     memset(lp, 0, sizeof(struct player));
92     if (s >= 0) {
93         /* real player, not dummy created by update and market update */
94         lp->iop = io_open(s,
95                           IO_READ | IO_WRITE | IO_NBLOCK,
96                           IO_BUFSIZE, 0, 0);
97         if (!lp->iop) {
98             free(lp);
99             return NULL;
100         }
101         emp_insque(&lp->queue, &Players);
102         lp->cnum = 255;
103         lp->curid = -1;
104         time(&lp->curup);
105     }
106     return lp;
107 }
108
109 struct player *
110 player_delete(struct player *lp)
111 {
112     struct player *back;
113
114     back = (struct player *)lp->queue.q_back;
115     if (back)
116         emp_remque(&lp->queue);
117     if (lp->iop) {
118         /* it's a real player */
119         io_close(lp->iop);
120         lp->iop = 0;
121     }
122     free(lp);
123     /* XXX may need to free bigmap here */
124     return back;
125 }
126
127 struct player *
128 player_next(struct player *lp)
129 {
130     if (lp == 0)
131         lp = (struct player *)Players.q_forw;
132     else
133         lp = (struct player *)lp->queue.q_forw;
134     if (&lp->queue == &Players)
135         return 0;
136     return lp;
137 }
138
139 struct player *
140 player_prev(struct player *lp)
141 {
142     if (lp == 0)
143         lp = (struct player *)Players.q_back;
144     else
145         lp = (struct player *)lp->queue.q_back;
146     if (&lp->queue == &Players)
147         return 0;
148     return lp;
149 }
150
151 /*
152  * Return player in state PS_PLAYING for CNUM.
153  */
154 struct player *
155 getplayer(natid cnum)
156 {
157     struct emp_qelem *qp;
158     struct player *pl;
159
160     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
161         pl = (struct player *)qp;
162         if (pl->cnum == cnum && pl->state == PS_PLAYING)
163             return pl;
164     }
165
166     return NULL;
167 }
168
169 void
170 player_wakeup_all(natid cnum)
171 {
172     register struct player *lp;
173
174     if (NULL != (lp = getplayer(cnum)))
175         player_wakeup(lp);
176 }
177
178 void
179 player_wakeup(struct player *pl)
180 {
181     if (pl->waiting)
182         empth_wakeup(pl->proc);
183 }
184
185 /*ARGSUSED*/
186 void
187 player_accept(void *unused)
188 {
189     struct sockaddr *sap;
190     void *inaddr;
191     int s = player_socket;
192     struct player *np;
193     int len;
194     int ns;
195     int set = 1;
196     int stacksize;
197     char buf[128];
198 #ifdef RESOLVE_IPADDRESS
199     struct hostent *hostp;
200 #endif
201
202     /* auto sockaddr_storage would be simpler, but less portable */
203     sap = malloc(player_addrlen);
204
205     while (1) {
206         empth_select(s, EMPTH_FD_READ);
207         len = player_addrlen;
208         ns = accept(s, sap, &len);
209         /* FIXME accept() can block on some systems (RST after select() reported s ready) */
210         if (ns < 0) {
211             logerror("new socket accept");
212             continue;
213         }
214         /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
215         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
216         np = player_new(ns);
217         if (!np) {
218             logerror("can't create player for fd %d", ns);
219             close(ns);
220             continue;
221         }
222 #ifdef HAVE_GETADDRINFO
223         inaddr = sap->sa_family == AF_INET
224             ? (void *)&((struct sockaddr_in *)sap)->sin_addr
225             : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
226         /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
227         if (!inet_ntop(sap->sa_family, inaddr,
228                        np->hostaddr, sizeof(np->hostaddr))) {
229             logerror("inet_ntop() failed: %s", strerror(errno));
230             close(ns);
231             continue;
232         }
233 #else
234         inaddr = &((struct sockaddr_in *)sap)->sin_addr;
235         strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
236 #endif
237 #ifdef RESOLVE_IPADDRESS
238         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
239         if (NULL != hostp)
240             strcpy(np->hostname, hostp->h_name);
241 #endif /* RESOLVE_IPADDRESS */
242         /* XXX may not be big enough */
243         stacksize = 100000
244 /* budget */  + MAX(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
245 /* power */ MAXNOC * sizeof(struct powstr));
246         sprintf(buf, "Player (fd #%d)", ns);
247         empth_create(PP_PLAYER, player_login, stacksize,
248                      0, buf, "Empire player", np);
249     }
250 }