]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
c6fff50b7ec84b311f2a3ef712bc1879168eb886
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "common.h"
43 #include "gen.h"
44 #include "optlist.h"
45
46 #if !defined(_WIN32)
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 <winsock.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     struct hostent *hostp;
77
78     lp = (struct player *)malloc(sizeof(struct player));
79     if (!lp)
80       return NULL;
81     memset(lp, 0, sizeof(struct player));
82     if (sin) {
83         /* real player, not dummy created by update and market update */
84         lp->iop = io_open(s,
85                           IO_READ | IO_WRITE | IO_NBLOCK,
86                           IO_BUFSIZE, 0, 0);
87         if (!lp->iop) {
88             free(lp);
89             return NULL;
90         }
91         emp_insque(&lp->queue, &Players);
92         strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
93 #ifdef RESOLVE_IPADDRESS
94         if (NULL !=
95             (hostp =
96              gethostbyaddr((char *)&sin->sin_addr, sizeof(sin->sin_addr),
97                            AF_INET)))
98             strcpy(lp->hostname, hostp->h_name);
99 #endif /* RESOLVE_IPADDRESS */
100         lp->cnum = 255;
101         lp->curid = -1;
102         time(&lp->curup);
103     }
104     return lp;
105 }
106
107 struct player *
108 player_delete(struct player *lp)
109 {
110     struct player *back;
111
112     back = (struct player *)lp->queue.q_back;
113     if (back)
114         emp_remque(&lp->queue);
115     if (lp->iop) {
116         /* it's a real player */
117         io_close(lp->iop);
118         lp->iop = 0;
119     }
120     free((s_char *)lp);
121     /* XXX may need to free bigmap here */
122     return back;
123 }
124
125 struct player *
126 player_next(struct player *lp)
127 {
128     if (lp == 0)
129         lp = (struct player *)Players.q_forw;
130     else
131         lp = (struct player *)lp->queue.q_forw;
132     if (&lp->queue == &Players)
133         return 0;
134     return lp;
135 }
136
137 struct player *
138 player_prev(struct player *lp)
139 {
140     if (lp == 0)
141         lp = (struct player *)Players.q_back;
142     else
143         lp = (struct player *)lp->queue.q_back;
144     if (&lp->queue == &Players)
145         return 0;
146     return lp;
147 }
148
149 struct player *
150 getplayer(natid cnum)
151 {
152     register struct emp_qelem *qp;
153
154     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
155         if (((struct player *)qp)->cnum == cnum)
156             return (struct player *)qp;
157
158     return 0;
159 }
160
161 struct player *
162 player_find_other(struct player *us, register natid cnum)
163 {
164     register struct emp_qelem *qp;
165
166     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
167         if (((struct player *)qp)->cnum == cnum &&
168             ((struct player *)qp != us) &&
169             (((struct player *)qp)->state == PS_PLAYING))
170             return (struct player *)qp;
171
172
173     return 0;
174 }
175
176 void
177 player_wakeup_all(natid cnum)
178 {
179     register struct player *lp;
180
181     if (NULL != (lp = getplayer(cnum)))
182         player_wakeup(lp);
183 }
184
185 void
186 player_wakeup(struct player *pl)
187 {
188     if (pl->waiting)
189         empth_wakeup(pl->proc);
190 }
191
192 /*ARGSUSED*/
193 void
194 player_accept(void *argv)
195 {
196     extern s_char *loginport;
197     struct sockaddr_in sin;
198     struct servent *sp;
199     int s;
200     short port;
201     int val;
202     struct player *np;
203     socklen_t len;
204     int ns;
205     int set = 1;
206     int stacksize;
207     char buf[128];
208
209     player_init();
210     sp = getservbyname("empire", "tcp");
211     if (sp == 0)
212         port = htons(atoi(loginport));
213     else
214         port = sp->s_port;
215     sin.sin_addr.s_addr = INADDR_ANY;
216     sin.sin_port = port;
217     sin.sin_family = AF_INET;
218     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
219         logerror("inet socket create");
220         exit(1);
221     }
222     val = 1;
223 #if !(defined(__linux__) && defined(__alpha__))
224     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
225         < 0) {
226         logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
227         exit(1);
228     }
229 #else
230     logerror
231         ("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 }