]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
(getplayer): There may be multiple players for the same country in the
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
33
34 #if defined(_WIN32)
35 #define WIN32
36 #include <winsock2.h>
37 #undef NS_ALL
38 #endif
39
40 #include "prototypes.h"
41 #include "misc.h"
42 #include "proto.h"
43 #include "empthread.h"
44 #include "player.h"
45 #include "file.h"
46 #include "empio.h"
47 #include "power.h"
48 #include "gen.h"
49 #include "optlist.h"
50
51 #if !defined(_WIN32)
52 #include <arpa/inet.h>
53 #include <sys/socket.h>
54 #include <sys/wait.h>
55 #include <sys/time.h>
56 #include <netdb.h>
57 #include <netinet/in.h>
58 #include <sys/ioctl.h>
59 #include <unistd.h>
60 #endif
61 #include <signal.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66
67 static struct emp_qelem Players;
68 static int player_socket;
69
70 void
71 player_init(void)
72 {
73     struct sockaddr_in sin;
74     struct hostent *hp;
75     struct servent *sp;
76     int s;
77     int val;
78
79     emp_initque(&Players);
80     init_player_commands();
81
82     sin.sin_family = AF_INET;
83     if (!*listen_addr)
84         sin.sin_addr.s_addr = INADDR_ANY;
85     else if (isdigit(*listen_addr))
86         sin.sin_addr.s_addr = inet_addr(listen_addr);
87     else {
88         hp = gethostbyname(listen_addr);
89         if (!hp) {
90             logerror("Can't resolve listen address %s", listen_addr);
91             exit(1);
92         }
93         memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
94     }
95     if (isdigit(*loginport))
96         sin.sin_port = htons(atoi(loginport));
97     else {
98         sp = getservbyname(loginport, "tcp");
99         if (!sp) {
100             logerror("Can't resolve service %s", loginport);
101             exit(1);
102         }
103         sin.sin_port = sp->s_port;
104     }
105     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
106         logerror("inet socket create");
107         exit(1);
108     }
109     val = 1;
110     /*
111      * WIN32's SO_REUSEADDR operates differently than POSIX's SO_REUSEADDR.
112      * WIN32's SO_REUSEADDR allows the port number to be used immediately
113      * even if the port number is currently being used by another program.
114      * In WIN32, there is no waiting time when a port is closed before it
115      * can be reused so SO_REUSEADDR is not required for WIN32.
116      */
117 #ifndef _WIN32
118     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
119         logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
120         exit(1);
121     }
122 #endif
123     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
124         logerror("inet socket bind");
125         exit(1);
126     }
127     if (listen(s, SOMAXCONN) < 0) {
128         logerror("inet socket listen");
129         exit(1);
130     }
131     player_socket = s;
132 }
133
134 struct player *
135 player_new(int s, struct sockaddr_in *sin)
136 {
137     struct player *lp;
138 #ifdef RESOLVE_IPADDRESS
139     struct hostent *hostp;
140 #endif
141
142     lp = malloc(sizeof(struct player));
143     if (!lp)
144       return NULL;
145     memset(lp, 0, sizeof(struct player));
146     if (sin) {
147         /* real player, not dummy created by update and market update */
148         lp->iop = io_open(s,
149                           IO_READ | IO_WRITE | IO_NBLOCK,
150                           IO_BUFSIZE, 0, 0);
151         if (!lp->iop) {
152             free(lp);
153             return NULL;
154         }
155         emp_insque(&lp->queue, &Players);
156         strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
157 #ifdef RESOLVE_IPADDRESS
158         if (NULL !=
159             (hostp = gethostbyaddr(&sin->sin_addr, sizeof(sin->sin_addr),
160                                    AF_INET)))
161             strcpy(lp->hostname, hostp->h_name);
162 #endif /* RESOLVE_IPADDRESS */
163         lp->cnum = 255;
164         lp->curid = -1;
165         time(&lp->curup);
166     }
167     return lp;
168 }
169
170 struct player *
171 player_delete(struct player *lp)
172 {
173     struct player *back;
174
175     back = (struct player *)lp->queue.q_back;
176     if (back)
177         emp_remque(&lp->queue);
178     if (lp->iop) {
179         /* it's a real player */
180         io_close(lp->iop);
181         lp->iop = 0;
182     }
183     free(lp);
184     /* XXX may need to free bigmap here */
185     return back;
186 }
187
188 struct player *
189 player_next(struct player *lp)
190 {
191     if (lp == 0)
192         lp = (struct player *)Players.q_forw;
193     else
194         lp = (struct player *)lp->queue.q_forw;
195     if (&lp->queue == &Players)
196         return 0;
197     return lp;
198 }
199
200 struct player *
201 player_prev(struct player *lp)
202 {
203     if (lp == 0)
204         lp = (struct player *)Players.q_back;
205     else
206         lp = (struct player *)lp->queue.q_back;
207     if (&lp->queue == &Players)
208         return 0;
209     return lp;
210 }
211
212 /*
213  * Return player in state PS_PLAYING for CNUM.
214  */
215 struct player *
216 getplayer(natid cnum)
217 {
218     struct emp_qelem *qp;
219     struct player *pl;
220
221     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
222         pl = (struct player *)qp;
223         if (pl->cnum == cnum && pl->state == PS_PLAYING)
224             return pl;
225     }
226
227     return NULL;
228 }
229
230 struct player *
231 player_find_other(struct player *us, register natid cnum)
232 {
233     register struct emp_qelem *qp;
234
235     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
236         if (((struct player *)qp)->cnum == cnum &&
237             ((struct player *)qp != us) &&
238             (((struct player *)qp)->state == PS_PLAYING))
239             return (struct player *)qp;
240
241
242     return 0;
243 }
244
245 void
246 player_wakeup_all(natid cnum)
247 {
248     register struct player *lp;
249
250     if (NULL != (lp = getplayer(cnum)))
251         player_wakeup(lp);
252 }
253
254 void
255 player_wakeup(struct player *pl)
256 {
257     if (pl->waiting)
258         empth_wakeup(pl->proc);
259 }
260
261 /*ARGSUSED*/
262 void
263 player_accept(void *unused)
264 {
265     struct sockaddr_in sin;
266     int s = player_socket;
267     struct player *np;
268     int len;
269     int ns;
270     int set = 1;
271     int stacksize;
272     char buf[128];
273
274     while (1) {
275         empth_select(s, EMPTH_FD_READ);
276         len = sizeof(sin);
277         ns = accept(s, (struct sockaddr *)&sin, &len);
278         if (ns < 0) {
279             logerror("new socket accept");
280             continue;
281         }
282         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
283         np = player_new(ns, &sin);
284         if (!np) {
285             logerror("can't create player for fd %d", ns);
286             close(ns);
287             continue;
288         }
289         /* XXX may not be big enough */
290         stacksize = 100000
291 /* budget */  + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
292 /* power */ MAXNOC * sizeof(struct powstr));
293         sprintf(buf, "Player (fd #%d)", ns);
294         empth_create(PP_PLAYER, player_login, stacksize,
295                      0, buf, "Empire player", np);
296     }
297 }