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