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