]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
errno may not be a variable; users must include errno.h instead of
[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     struct sockaddr_in sin;
194     struct servent *sp;
195     int s;
196     short port;
197     int val;
198     int maxfd;
199     struct player *np;
200     int len;
201     int ns;
202     int set = 1;
203     int stacksize;
204     char buf[128];
205
206     player_init();
207     sp = getservbyname("empire", "tcp");
208     if (sp == 0)
209         port = htons(atoi(loginport));
210     else
211         port = sp->s_port;
212     sin.sin_addr.s_addr = INADDR_ANY;
213     sin.sin_port = port;
214     sin.sin_family = AF_INET;
215     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
216         logerror("inet socket create");
217         exit(1);
218     }
219     val = 1;
220 #if !(defined(__linux__) && defined(__alpha__))
221     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val))
222         < 0) {
223         logerror("inet socket setsockopt SO_REUSEADDR (%d)", errno);
224         exit(1);
225     }
226 #else
227     logerror
228         ("Alpha/Linux?  You don't support SO_REUSEADDR yet, do you?\n");
229 #endif
230     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
231         logerror("inet socket bind");
232         exit(1);
233     }
234 #ifdef LISTENMAXCONN            /* because someone in linux world didn't want to use
235                                  * SOMAXCONN as defined in the header files... */
236     if (listen(s, LISTENMAXCONN) < 0) {
237         logerror("inet socket listen");
238         exit(1);
239     }
240 #else
241     if (listen(s, SOMAXCONN) < 0) {
242         logerror("inet socket listen");
243         exit(1);
244     }
245 #endif
246     maxfd = getfdtablesize() - 1;
247     while (1) {
248         empth_select(s, EMPTH_FD_READ);
249         len = sizeof(sin);
250         ns = accept(s, (struct sockaddr *)&sin, &len);
251         if (ns < 0) {
252             logerror("new socket accept");
253             continue;
254         }
255         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
256                          (char *)&set, sizeof(set));
257         if (ns >= maxfd) {
258             logerror("new fd %d, max %d, no fd's left for new user",
259                      ns, maxfd);
260             close(ns);
261             continue;
262         }
263         np = player_new(ns, &sin);
264         /* XXX may not be big enough */
265         stacksize = 100000
266 /* budget */  + max(WORLD_X * WORLD_Y / 2 * sizeof(int) * 7,
267 /* power */ MAXNOC * sizeof(struct powstr));
268         sprintf(buf, "Player (fd #%d)", ns);
269         empth_create(PP_PLAYER, player_login, stacksize,
270                      0, buf, "Empire player", np);
271     }
272 }