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