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