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