]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Import of Empire 4.2.12
[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 "bit.h"
37 #include "proto.h"
38 #include "empthread.h"
39 #include "player.h"
40 #include "file.h"
41 #include "io_mask.h"
42 #include "empio.h"
43 #include "power.h"
44 #include "common.h"
45 #include "gen.h"
46 #include "optlist.h"
47
48 #if !defined(_WIN32)
49 #include <sys/socket.h>
50 #include <sys/wait.h>
51 #include <sys/time.h>
52 #include <netdb.h>
53 #include <netinet/in.h>
54 #include <sys/ioctl.h>
55 #include <unistd.h>
56 #else
57 #include <winsock.h>
58 #endif
59 #include <signal.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <stdlib.h>
63 #include <stdio.h>
64
65 struct  emp_qelem Players;
66
67 void
68 player_init(void)
69 {
70         emp_initque(&Players);
71         init_player_commands();
72 }
73
74 struct player *
75 player_new(int s, struct sockaddr_in *sin)
76 {
77         struct  player *lp;
78         struct hostent *hostp;
79
80         lp = (struct player *) calloc(1, sizeof(struct player));
81         bzero((s_char *)lp, sizeof(struct player));
82         if (sin) {
83                 /* update uses dummy player */
84                 /* so does the market updater */
85                 emp_insque(&lp->queue, &Players);
86                 strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
87 #ifdef RESOLVE_IPADDRESS
88                 if (NULL != (hostp = gethostbyaddr((char *) &sin->sin_addr, sizeof(sin->sin_addr), AF_INET)))
89                         strcpy(lp->hostname, hostp->h_name);
90 #endif /* RESOLVE_IPADDRESS */
91                 lp->cnum = 255;
92               lp->curid = -1;
93                 time(&lp->curup);
94                 lp->iop = io_open(s, IO_READ|IO_WRITE|IO_NBLOCK,
95                         IO_BUFSIZE, 0, 0);
96         }
97         return lp;
98 }
99
100 struct player *
101 player_delete(struct player *lp)
102 {
103         struct  player *back;
104
105         back = (struct player *) lp->queue.q_back;
106         if (back)
107                 emp_remque(&lp->queue);
108         if (lp->iop) {
109                 /* it's a real player */
110                 io_close(lp->iop);
111                 lp->iop = 0;
112         }
113         free((s_char *)lp);
114         /* XXX may need to free bigmap here */
115         return back;
116 }
117
118 struct player *
119 player_next(struct player *lp)
120 {
121         if (lp == 0)
122                 lp = (struct player *)Players.q_forw;
123         else
124                 lp = (struct player *)lp->queue.q_forw;
125         if (&lp->queue == &Players)
126                 return 0;
127         return lp;
128 }
129
130 struct player *
131 player_prev(struct player *lp)
132 {
133         if (lp == 0)
134                 lp = (struct player *)Players.q_back;
135         else
136                 lp = (struct player *)lp->queue.q_back;
137         if (&lp->queue == &Players)
138                 return 0;
139         return lp;
140 }
141
142 struct player *
143 getplayer(natid cnum)
144 {
145         register struct emp_qelem *qp;
146
147         for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
148                 if (((struct player *)qp)->cnum == cnum)
149                         return (struct player *)qp;
150
151         return 0;
152 }
153
154 struct player *
155 player_find_other(struct player *us, register natid cnum)
156 {
157         register struct emp_qelem *qp;
158
159         for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
160                 if (((struct player *)qp)->cnum == cnum &&
161                     ((struct player *)qp != us) &&
162                     (((struct player *)qp)->state == PS_PLAYING))
163                         return (struct player *)qp;
164
165         
166         return 0;
167 }
168
169 void
170 player_wakeup_all(natid cnum)
171 {
172         register struct player *lp;
173
174         if (NULL != (lp = getplayer(cnum)))
175                 player_wakeup(lp);
176 }
177
178 void
179 player_wakeup(struct player *pl)
180 {
181         if (pl->waiting)
182                 empth_wakeup(pl->proc);
183 }
184
185 /*ARGSUSED*/
186 void
187 player_accept(void *argv)
188 {
189         extern  s_char *loginport;
190         extern  int errno;
191         struct  sockaddr_in sin;
192         struct  servent *sp;
193         int     s;
194         short   port;
195         int     val;
196         int     maxfd;
197         struct  player *np;
198         int     len;
199         int     ns;
200         int     set = 1;
201         int     stacksize;
202         char    buf[128];
203
204         player_init();
205         sp = getservbyname("empire", "tcp");
206         if (sp == 0)
207                 port = htons(atoi(loginport));
208         else
209                 port = sp->s_port;
210         sin.sin_addr.s_addr = INADDR_ANY;
211         sin.sin_port = port;
212         sin.sin_family = AF_INET;
213         if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
214                 logerror("inet socket create");
215                 exit(1);
216         }
217         val = 1;
218 #if !(defined(__linux__) && defined(__alpha__))
219         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)) < 0) {
220                 logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
221                 exit(1);
222         }
223 #else
224     logerror("Alpha/Linux?  You don't support SO_REUSEADDR yet, do you?\n");
225 #endif
226         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
227                 logerror("inet socket bind");
228                 exit(1);
229         }
230 #ifdef LISTENMAXCONN /* because someone in linux world didn't want to use
231                       * SOMAXCONN as defined in the header files... */
232         if (listen(s, LISTENMAXCONN) < 0) {
233                 logerror("inet socket listen");
234                 exit(1);
235         }
236 #else
237         if (listen(s, SOMAXCONN) < 0) {
238                 logerror("inet socket listen");
239                 exit(1);
240         }
241 #endif
242         maxfd = getfdtablesize() - 1;
243         while (1) {
244                 empth_select(s, EMPTH_FD_READ);
245                 len = sizeof(sin);
246                 ns = accept(s, (struct sockaddr *) &sin, &len);
247                 if (ns < 0) {
248                         logerror("new socket accept");
249                         continue;
250                 }
251                 (void) setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
252                         (char *) &set, sizeof(set));
253                 if (ns >= maxfd) {
254                         logerror("new fd %d, max %d, no fd's left for new user",
255                                 ns, maxfd);
256                         close(ns);
257                         continue;
258                 }
259                 np = player_new(ns, &sin);
260                 /* XXX may not be big enough */
261                 stacksize = 100000
262 /* budget */              + max(WORLD_X*WORLD_Y/2 * sizeof(int) * 7,
263 /* power */                     MAXNOC * sizeof(struct powstr));
264                 sprintf(buf, "Player (fd #%d)", ns);
265                 empth_create(PP_PLAYER, player_login, stacksize,
266                              0, buf, "Empire player", np);
267         }
268 }