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