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