]> git.pond.sub.org Git - empserver/blob - src/lib/player/accept.c
Trim system includes.
[empserver] / src / lib / player / accept.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  *     Markus Armbruster, 2005
33  */
34
35 #include <config.h>
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #if !defined(_WIN32)
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #endif
46 #include <unistd.h>
47
48 #include "empio.h"
49 #include "empthread.h"
50 #include "file.h"
51 #include "misc.h"
52 #include "optlist.h"
53 #include "player.h"
54 #include "power.h"
55 #include "proto.h"
56 #include "prototypes.h"
57
58 static struct emp_qelem Players;
59 static int player_socket;
60 static size_t player_addrlen;
61
62 void
63 player_init(void)
64 {
65     emp_initque(&Players);
66     init_player_commands();
67
68     player_socket = tcp_listen(*listen_addr ? listen_addr : NULL,
69                                loginport, &player_addrlen);
70 }
71
72 struct player *
73 player_new(int s)
74 {
75     struct player *lp;
76
77     lp = malloc(sizeof(struct player));
78     if (!lp)
79       return NULL;
80     memset(lp, 0, sizeof(struct player));
81     if (s >= 0) {
82         /* real player, not dummy created by update and market update */
83         lp->iop = io_open(s, IO_READ | IO_WRITE | IO_NBLOCK, IO_BUFSIZE);
84         if (!lp->iop) {
85             free(lp);
86             return NULL;
87         }
88         emp_insque(&lp->queue, &Players);
89         lp->cnum = 255;
90         lp->curid = -1;
91         time(&lp->curup);
92     }
93     return lp;
94 }
95
96 struct player *
97 player_delete(struct player *lp)
98 {
99     struct player *back;
100
101     back = (struct player *)lp->queue.q_back;
102     if (back)
103         emp_remque(&lp->queue);
104     if (lp->iop) {
105         /* it's a real player */
106         io_close(lp->iop);
107         lp->iop = 0;
108     }
109     free(lp);
110     /* XXX may need to free bigmap here */
111     return back;
112 }
113
114 struct player *
115 player_next(struct player *lp)
116 {
117     if (lp == 0)
118         lp = (struct player *)Players.q_forw;
119     else
120         lp = (struct player *)lp->queue.q_forw;
121     if (&lp->queue == &Players)
122         return 0;
123     return lp;
124 }
125
126 struct player *
127 player_prev(struct player *lp)
128 {
129     if (lp == 0)
130         lp = (struct player *)Players.q_back;
131     else
132         lp = (struct player *)lp->queue.q_back;
133     if (&lp->queue == &Players)
134         return 0;
135     return lp;
136 }
137
138 /*
139  * Return player in state PS_PLAYING for CNUM.
140  */
141 struct player *
142 getplayer(natid cnum)
143 {
144     struct emp_qelem *qp;
145     struct player *pl;
146
147     for (qp = Players.q_forw; qp != &Players; qp = qp->q_forw) {
148         pl = (struct player *)qp;
149         if (pl->cnum == cnum && pl->state == PS_PLAYING)
150             return pl;
151     }
152
153     return NULL;
154 }
155
156 /*ARGSUSED*/
157 void
158 player_accept(void *unused)
159 {
160     struct sockaddr *sap;
161     void *inaddr;
162     int s = player_socket;
163     struct player *np;
164     socklen_t len;
165     int ns;
166     int set = 1;
167     int stacksize;
168     char buf[128];
169 #ifdef RESOLVE_IPADDRESS
170     struct hostent *hostp;
171 #endif
172
173     /* auto sockaddr_storage would be simpler, but less portable */
174     sap = malloc(player_addrlen);
175
176     while (1) {
177         empth_select(s, EMPTH_FD_READ);
178         len = player_addrlen;
179         ns = accept(s, sap, &len);
180         /* FIXME accept() can block on some systems (RST after select() reports ready) */
181         if (ns < 0) {
182             logerror("new socket accept");
183             continue;
184         }
185         /* FIXME SO_KEEPALIVE is useless, player_kill_idle() strikes long before */
186         (void)setsockopt(ns, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
187         np = player_new(ns);
188         if (!np) {
189             logerror("can't create player for fd %d", ns);
190             close(ns);
191             continue;
192         }
193 #ifdef HAVE_GETADDRINFO
194         inaddr = sap->sa_family == AF_INET
195             ? (void *)&((struct sockaddr_in *)sap)->sin_addr
196             : (void *)&((struct sockaddr_in6 *)sap)->sin6_addr;
197         /* Assumes that if you got getaddrinfo(), you got inet_ntop() too */
198         if (!inet_ntop(sap->sa_family, inaddr,
199                        np->hostaddr, sizeof(np->hostaddr))) {
200             logerror("inet_ntop() failed: %s", strerror(errno));
201             close(ns);
202             continue;
203         }
204 #else
205         inaddr = &((struct sockaddr_in *)sap)->sin_addr;
206         strcpy(np->hostaddr, inet_ntoa(*(struct in_addr *)inaddr));
207 #endif
208 #ifdef RESOLVE_IPADDRESS
209         hostp = gethostbyaddr(inaddr, player_addrlen, sap->sa_family);
210         if (NULL != hostp)
211             strcpy(np->hostname, hostp->h_name);
212 #endif /* RESOLVE_IPADDRESS */
213         /* FIXME ancient black magic; figure out true stack need */
214         stacksize = 100000
215 /* budget */  + MAX(WORLD_SZ() * sizeof(int) * 7,
216 /* power */ MAXNOC * sizeof(struct powstr));
217         sprintf(buf, "Player (fd #%d)", ns);
218         empth_create(player_login, stacksize, 0, buf, np);
219     }
220 }