]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
faeda1c222408ec3e2c1c1c419fbe70838ac1cbb
[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 servent *sp;
75     int s;
76     short port;
77     int val;
78
79     emp_initque(&Players);
80     init_player_commands();
81
82     sp = getservbyname("empire", "tcp");
83     if (sp == 0)
84         port = htons(atoi(loginport));
85     else
86         port = sp->s_port;
87     sin.sin_addr.s_addr = INADDR_ANY;
88     sin.sin_port = port;
89     sin.sin_family = AF_INET;
90     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
91         logerror("inet socket create");
92         exit(1);
93     }
94     val = 1;
95     /*
96      * WIN32's SO_REUSEADDR operates differently than POSIX's SO_REUSEADDR.
97      * WIN32's SO_REUSEADDR allows the port number to be used immediately
98      * even if the port number is currently being used by another program.
99      * In WIN32, there is no waiting time when a port is closed before it
100      * can be reused so SO_REUSEADDR is not required for WIN32.
101      */
102 #ifndef _WIN32
103     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
104         logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
105         exit(1);
106     }
107 #endif
108     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
109         logerror("inet socket bind");
110         exit(1);
111     }
112 #ifdef LISTENMAXCONN            /* because someone in linux world didn't want to use
113                                  * SOMAXCONN as defined in the header files... */
114     if (listen(s, LISTENMAXCONN) < 0) {
115         logerror("inet socket listen");
116         exit(1);
117     }
118 #else
119     if (listen(s, SOMAXCONN) < 0) {
120         logerror("inet socket listen");
121         exit(1);
122     }
123 #endif
124     player_socket = s;
125 }
126
127 struct player *
128 player_new(int s, struct sockaddr_in *sin)
129 {
130     struct player *lp;
131 #ifdef RESOLVE_IPADDRESS
132     struct hostent *hostp;
133 #endif
134
135     lp = malloc(sizeof(struct player));
136     if (!lp)
137       return NULL;
138     memset(lp, 0, sizeof(struct player));
139     if (sin) {
140         /* real player, not dummy created by update and market update */
141         lp->iop = io_open(s,
142                           IO_READ | IO_WRITE | IO_NBLOCK,
143                           IO_BUFSIZE, 0, 0);
144         if (!lp->iop) {
145             free(lp);
146             return NULL;
147         }
148         emp_insque(&lp->queue, &Players);
149         strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
150 #ifdef RESOLVE_IPADDRESS
151         if (NULL !=
152             (hostp = gethostbyaddr(&sin->sin_addr, sizeof(sin->sin_addr),
153                                    AF_INET)))
154             strcpy(lp->hostname, hostp->h_name);
155 #endif /* RESOLVE_IPADDRESS */
156         lp->cnum = 255;
157         lp->curid = -1;
158         time(&lp->curup);
159     }
160     return lp;
161 }
162
163 struct player *
164 player_delete(struct player *lp)
165 {
166     struct player *back;
167
168     back = (struct player *)lp->queue.q_back;
169     if (back)
170         emp_remque(&lp->queue);
171     if (lp->iop) {
172         /* it's a real player */
173         io_close(lp->iop);
174         lp->iop = 0;
175     }
176     free(lp);
177     /* XXX may need to free bigmap here */
178     return back;
179 }
180
181 struct player *
182 player_next(struct player *lp)
183 {
184     if (lp == 0)
185         lp = (struct player *)Players.q_forw;
186     else
187         lp = (struct player *)lp->queue.q_forw;
188     if (&lp->queue == &Players)
189         return 0;
190     return lp;
191 }
192
193 struct player *
194 player_prev(struct player *lp)
195 {
196     if (lp == 0)
197         lp = (struct player *)Players.q_back;
198     else
199         lp = (struct player *)lp->queue.q_back;
200     if (&lp->queue == &Players)
201         return 0;
202     return lp;
203 }
204
205 struct player *
206 getplayer(natid cnum)
207 {
208     register struct emp_qelem *qp;
209
210     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
211         if (((struct player *)qp)->cnum == cnum)
212             return (struct player *)qp;
213
214     return 0;
215 }
216
217 struct player *
218 player_find_other(struct player *us, register natid cnum)
219 {
220     register struct emp_qelem *qp;
221
222     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
223         if (((struct player *)qp)->cnum == cnum &&
224             ((struct player *)qp != us) &&
225             (((struct player *)qp)->state == PS_PLAYING))
226             return (struct player *)qp;
227
228
229     return 0;
230 }
231
232 void
233 player_wakeup_all(natid cnum)
234 {
235     register struct player *lp;
236
237     if (NULL != (lp = getplayer(cnum)))
238         player_wakeup(lp);
239 }
240
241 void
242 player_wakeup(struct player *pl)
243 {
244     if (pl->waiting)
245         empth_wakeup(pl->proc);
246 }
247
248 /*ARGSUSED*/
249 void
250 player_accept(void *unused)
251 {
252     struct sockaddr_in sin;
253     int s = player_socket;
254     struct player *np;
255     int len;
256     int ns;
257     int set = 1;
258     int stacksize;
259     char buf[128];
260
261     while (1) {
262         empth_select(s, EMPTH_FD_READ);
263         len = sizeof(sin);
264         ns = accept(s, (struct sockaddr *)&sin, &len);
265         if (ns < 0) {
266             logerror("new socket accept");
267             continue;
268         }
269         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
270         np = player_new(ns, &sin);
271         if (!np) {
272             logerror("can't create player for fd %d", ns);
273             close(ns);
274             continue;
275         }
276         /* XXX may not be big enough */
277         stacksize = 100000
278 /* budget */  + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
279 /* power */ MAXNOC * sizeof(struct powstr));
280         sprintf(buf, "Player (fd #%d)", ns);
281         empth_create(PP_PLAYER, player_login, stacksize,
282                      0, buf, "Empire player", np);
283     }
284 }