]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
(player_new): Proper error checking. Oops, now it may return NULL.
[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 "proto.h"
37 #include "empthread.h"
38 #include "player.h"
39 #include "file.h"
40 #include "io_mask.h"
41 #include "empio.h"
42 #include "power.h"
43 #include "common.h"
44 #include "gen.h"
45 #include "optlist.h"
46
47 #if !defined(_WIN32)
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     struct hostent *hostp;
78
79     lp = (struct player *)malloc(sizeof(struct player));
80     if (!lp)
81       return NULL;
82     memset(lp, 0, sizeof(struct player));
83     if (sin) {
84         /* real player, not dummy created by update and market update */
85         lp->iop = io_open(s,
86                           IO_READ | IO_WRITE | IO_NBLOCK,
87                           IO_BUFSIZE, 0, 0);
88         if (!lp->iop) {
89             free(lp);
90             return NULL;
91         }
92         emp_insque(&lp->queue, &Players);
93         strcpy(lp->hostaddr, inet_ntoa(sin->sin_addr));
94 #ifdef RESOLVE_IPADDRESS
95         if (NULL !=
96             (hostp =
97              gethostbyaddr((char *)&sin->sin_addr, sizeof(sin->sin_addr),
98                            AF_INET)))
99             strcpy(lp->hostname, hostp->h_name);
100 #endif /* RESOLVE_IPADDRESS */
101         lp->cnum = 255;
102         lp->curid = -1;
103         time(&lp->curup);
104     }
105     return lp;
106 }
107
108 struct player *
109 player_delete(struct player *lp)
110 {
111     struct player *back;
112
113     back = (struct player *)lp->queue.q_back;
114     if (back)
115         emp_remque(&lp->queue);
116     if (lp->iop) {
117         /* it's a real player */
118         io_close(lp->iop);
119         lp->iop = 0;
120     }
121     free((s_char *)lp);
122     /* XXX may need to free bigmap here */
123     return back;
124 }
125
126 struct player *
127 player_next(struct player *lp)
128 {
129     if (lp == 0)
130         lp = (struct player *)Players.q_forw;
131     else
132         lp = (struct player *)lp->queue.q_forw;
133     if (&lp->queue == &Players)
134         return 0;
135     return lp;
136 }
137
138 struct player *
139 player_prev(struct player *lp)
140 {
141     if (lp == 0)
142         lp = (struct player *)Players.q_back;
143     else
144         lp = (struct player *)lp->queue.q_back;
145     if (&lp->queue == &Players)
146         return 0;
147     return lp;
148 }
149
150 struct player *
151 getplayer(natid cnum)
152 {
153     register struct emp_qelem *qp;
154
155     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
156         if (((struct player *)qp)->cnum == cnum)
157             return (struct player *)qp;
158
159     return 0;
160 }
161
162 struct player *
163 player_find_other(struct player *us, register natid cnum)
164 {
165     register struct emp_qelem *qp;
166
167     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw)
168         if (((struct player *)qp)->cnum == cnum &&
169             ((struct player *)qp != us) &&
170             (((struct player *)qp)->state == PS_PLAYING))
171             return (struct player *)qp;
172
173
174     return 0;
175 }
176
177 void
178 player_wakeup_all(natid cnum)
179 {
180     register struct player *lp;
181
182     if (NULL != (lp = getplayer(cnum)))
183         player_wakeup(lp);
184 }
185
186 void
187 player_wakeup(struct player *pl)
188 {
189     if (pl->waiting)
190         empth_wakeup(pl->proc);
191 }
192
193 /*ARGSUSED*/
194 void
195 player_accept(void *argv)
196 {
197     extern s_char *loginport;
198     struct sockaddr_in sin;
199     struct servent *sp;
200     int s;
201     short port;
202     int val;
203     int maxfd;
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
233         ("Alpha/Linux?  You don't support SO_REUSEADDR yet, do you?\n");
234 #endif
235     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
236         logerror("inet socket bind");
237         exit(1);
238     }
239 #ifdef LISTENMAXCONN            /* because someone in linux world didn't want to use
240                                  * SOMAXCONN as defined in the header files... */
241     if (listen(s, LISTENMAXCONN) < 0) {
242         logerror("inet socket listen");
243         exit(1);
244     }
245 #else
246     if (listen(s, SOMAXCONN) < 0) {
247         logerror("inet socket listen");
248         exit(1);
249     }
250 #endif
251     maxfd = getfdtablesize() - 1;
252     while (1) {
253         empth_select(s, EMPTH_FD_READ);
254         len = sizeof(sin);
255         ns = accept(s, (struct sockaddr *)&sin, &len);
256         if (ns < 0) {
257             logerror("new socket accept");
258             continue;
259         }
260         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE,
261                          (char *)&set, sizeof(set));
262         if (ns >= maxfd) {
263             logerror("new fd %d, max %d, no fd's left for new user",
264                      ns, maxfd);
265             close(ns);
266             continue;
267         }
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 }