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