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