]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Indented with src/scripts/indent-emp.
[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 !=
89             (hostp =
90              gethostbyaddr((char *)&sin->sin_addr, sizeof(sin->sin_addr),
91                            AF_INET)))
92             strcpy(lp->hostname, hostp->h_name);
93 #endif /* RESOLVE_IPADDRESS */
94         lp->cnum = 255;
95         lp->curid = -1;
96         time(&lp->curup);
97         lp->iop = io_open(s, IO_READ | IO_WRITE | IO_NBLOCK,
98                           IO_BUFSIZE, 0, 0);
99     }
100     return lp;
101 }
102
103 struct player *
104 player_delete(struct player *lp)
105 {
106     struct player *back;
107
108     back = (struct player *)lp->queue.q_back;
109     if (back)
110         emp_remque(&lp->queue);
111     if (lp->iop) {
112         /* it's a real player */
113         io_close(lp->iop);
114         lp->iop = 0;
115     }
116     free((s_char *)lp);
117     /* XXX may need to free bigmap here */
118     return back;
119 }
120
121 struct player *
122 player_next(struct player *lp)
123 {
124     if (lp == 0)
125         lp = (struct player *)Players.q_forw;
126     else
127         lp = (struct player *)lp->queue.q_forw;
128     if (&lp->queue == &Players)
129         return 0;
130     return lp;
131 }
132
133 struct player *
134 player_prev(struct player *lp)
135 {
136     if (lp == 0)
137         lp = (struct player *)Players.q_back;
138     else
139         lp = (struct player *)lp->queue.q_back;
140     if (&lp->queue == &Players)
141         return 0;
142     return lp;
143 }
144
145 struct player *
146 getplayer(natid cnum)
147 {
148     register struct emp_qelem *qp;
149
150     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
151         if (((struct player *)qp)->cnum == cnum)
152             return (struct player *)qp;
153
154     return 0;
155 }
156
157 struct player *
158 player_find_other(struct player *us, register natid cnum)
159 {
160     register struct emp_qelem *qp;
161
162     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
163         if (((struct player *)qp)->cnum == cnum &&
164             ((struct player *)qp != us) &&
165             (((struct player *)qp)->state == PS_PLAYING))
166             return (struct player *)qp;
167
168
169     return 0;
170 }
171
172 void
173 player_wakeup_all(natid cnum)
174 {
175     register struct player *lp;
176
177     if (NULL != (lp = getplayer(cnum)))
178         player_wakeup(lp);
179 }
180
181 void
182 player_wakeup(struct player *pl)
183 {
184     if (pl->waiting)
185         empth_wakeup(pl->proc);
186 }
187
188 /*ARGSUSED*/
189 void
190 player_accept(void *argv)
191 {
192     extern s_char *loginport;
193     extern int errno;
194     struct sockaddr_in sin;
195     struct servent *sp;
196     int s;
197     short port;
198     int val;
199     int maxfd;
200     struct player *np;
201     int len;
202     int ns;
203     int set = 1;
204     int stacksize;
205     char buf[128];
206
207     player_init();
208     sp = getservbyname("empire", "tcp");
209     if (sp == 0)
210         port = htons(atoi(loginport));
211     else
212         port = sp->s_port;
213     sin.sin_addr.s_addr = INADDR_ANY;
214     sin.sin_port = port;
215     sin.sin_family = AF_INET;
216     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
217         logerror("inet socket create");
218         exit(1);
219     }
220     val = 1;
221 #if !(defined(__linux__) && defined(__alpha__))
222     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
223         < 0) {
224         logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
225         exit(1);
226     }
227 #else
228     logerror
229         ("Alpha/Linux?  You don't support SO_REUSEADDR yet, do you?\n");
230 #endif
231     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
232         logerror("inet socket bind");
233         exit(1);
234     }
235 #ifdef LISTENMAXCONN            /* because someone in linux world didn't want to use
236                                  * SOMAXCONN as defined in the header files... */
237     if (listen(s, LISTENMAXCONN) < 0) {
238         logerror("inet socket listen");
239         exit(1);
240     }
241 #else
242     if (listen(s, SOMAXCONN) < 0) {
243         logerror("inet socket listen");
244         exit(1);
245     }
246 #endif
247     maxfd = getfdtablesize() - 1;
248     while (1) {
249         empth_select(s, EMPTH_FD_READ);
250         len = sizeof(sin);
251         ns = accept(s, (struct sockaddr *)&sin, &len);
252         if (ns < 0) {
253             logerror("new socket accept");
254             continue;
255         }
256         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
257                          (char *)&set, sizeof(set));
258         if (ns >= maxfd) {
259             logerror("new fd %d, max %d, no fd's left for new user",
260                      ns, maxfd);
261             close(ns);
262             continue;
263         }
264         np = player_new(ns, &sin);
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 }