]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Break inclusion cycles. To simplify the change, move a few
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 #include "prototypes.h"
35 #include "misc.h"
36 #include "proto.h"
37 #include "empthread.h"
38 #include "player.h"
39 #include "file.h"
40 #include "empio.h"
41 #include "power.h"
42 #include "gen.h"
43 #include "optlist.h"
44
45 #if !defined(_WIN32)
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
48 #include <sys/wait.h>
49 #include <sys/time.h>
50 #include <netdb.h>
51 #include <netinet/in.h>
52 #include <sys/ioctl.h>
53 #include <unistd.h>
54 #else
55 #include <winsock2.h>
56 #endif
57 #include <signal.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62
63 struct emp_qelem Players;
64
65 void
66 player_init(void)
67 {
68     emp_initque(&Players);
69     init_player_commands();
70 }
71
72 struct player *
73 player_new(int s, struct sockaddr_in *sin)
74 {
75     struct player *lp;
76 #ifdef RESOLVE_IPADDRESS
77     struct hostent *hostp;
78 #endif
79
80     lp = (struct player *)malloc(sizeof(struct player));
81     if (!lp)
82       return NULL;
83     memset(lp, 0, sizeof(struct player));
84     if (sin) {
85         /* real player, not dummy created by update and market update */
86         lp->iop = io_open(s,
87                           IO_READ | IO_WRITE | IO_NBLOCK,
88                           IO_BUFSIZE, 0, 0);
89         if (!lp->iop) {
90             free(lp);
91             return NULL;
92         }
93         emp_insque(&lp->queue, &Players);
94         strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
95 #ifdef RESOLVE_IPADDRESS
96         if (NULL !=
97             (hostp =
98              gethostbyaddr((char *)&sin->sin_addr, sizeof(sin->sin_addr),
99                            AF_INET)))
100             strcpy(lp->hostname, hostp->h_name);
101 #endif /* RESOLVE_IPADDRESS */
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((s_char *)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 struct player *
152 getplayer(natid cnum)
153 {
154     register struct emp_qelem *qp;
155
156     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
157         if (((struct player *)qp)->cnum == cnum)
158             return (struct player *)qp;
159
160     return 0;
161 }
162
163 struct player *
164 player_find_other(struct player *us, register natid cnum)
165 {
166     register struct emp_qelem *qp;
167
168     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
169         if (((struct player *)qp)->cnum == cnum &&
170             ((struct player *)qp != us) &&
171             (((struct player *)qp)->state == PS_PLAYING))
172             return (struct player *)qp;
173
174
175     return 0;
176 }
177
178 void
179 player_wakeup_all(natid cnum)
180 {
181     register struct player *lp;
182
183     if (NULL != (lp = getplayer(cnum)))
184         player_wakeup(lp);
185 }
186
187 void
188 player_wakeup(struct player *pl)
189 {
190     if (pl->waiting)
191         empth_wakeup(pl->proc);
192 }
193
194 /*ARGSUSED*/
195 void
196 player_accept(void *unused)
197 {
198     struct sockaddr_in sin;
199     struct servent *sp;
200     int s;
201     short port;
202     int val;
203     struct player *np;
204     int len;
205     int ns;
206     int set = 1;
207     int stacksize;
208     char buf[128];
209
210     player_init();
211     sp = getservbyname("empire", "tcp");
212     if (sp == 0)
213         port = htons(atoi(loginport));
214     else
215         port = sp->s_port;
216     sin.sin_addr.s_addr = INADDR_ANY;
217     sin.sin_port = port;
218     sin.sin_family = AF_INET;
219     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
220         logerror("inet socket create");
221         exit(1);
222     }
223     val = 1;
224 #if !(defined(__linux__) && defined(__alpha__))
225     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
226         < 0) {
227         logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
228         exit(1);
229     }
230 #else
231     logerror("Alpha/Linux?  You don't support SO_REUSEADDR yet, do you?\n");
232 #endif
233     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
234         logerror("inet socket bind");
235         exit(1);
236     }
237 #ifdef LISTENMAXCONN            /* because someone in linux world didn't want to use
238                                  * SOMAXCONN as defined in the header files... */
239     if (listen(s, LISTENMAXCONN) < 0) {
240         logerror("inet socket listen");
241         exit(1);
242     }
243 #else
244     if (listen(s, SOMAXCONN) < 0) {
245         logerror("inet socket listen");
246         exit(1);
247     }
248 #endif
249     while (1) {
250         empth_select(s, EMPTH_FD_READ);
251         len = sizeof(sin);
252         ns = accept(s, (struct sockaddr *)&sin, &len);
253         if (ns < 0) {
254             logerror("new socket accept");
255             continue;
256         }
257         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
258                          (char *)&set, sizeof(set));
259         np = player_new(ns, &sin);
260         if (!np) {
261             logerror("can't create player for fd %d", ns);
262             close(ns);
263             continue;
264         }
265         /* XXX may not be big enough */
266         stacksize = 100000
267 /* budget */  + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
268 /* power */ MAXNOC * sizeof(struct powstr));
269         sprintf(buf, "Player (fd #%d)", ns);
270         empth_create(PP_PLAYER, player_login, stacksize,
271                      0, buf, "Empire player", np);
272     }
273 }