-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
371 lines (343 loc) · 9.82 KB
/
player.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* The server's conception of a player.
*
* Copyright (C) 2015 "Frostsnow" (Wade T. Cline).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "player.h"
void player_free(struct player* player, struct flub* status) {
struct flub* flub;
int ret;
// Join player thread.
ret = pthread_join(player->thread, (void**)&flub);
if (ret) {
g_log_warn("Unable to join player thread: '%s'",
g_serr(ret));
}
if (flub) {
g_log_warn("Player error: '%s'", flub->message);
}
// Close to-server pipe.
if (close(player->pipe_server_to[0]) == -1) {
g_log_warn("Unable to close to-server read end: '%s'",
g_serr(errno));
}
// Close from-server pipe.
if (close(player->pipe_server_from[0]) == -1) {
g_log_warn("Unable to close from-server read end: '%s'",
g_serr(errno));
}
if (!player->killed) { // Server "kills" player by closing write end.
if (close(player->pipe_server_from[1]) == -1) {
g_log_warn("Unable to close from-server write end: "
"'%s'", g_serr(errno));
}
}
// Close socket.
if (close(player->sockfd)) {
g_log_warn("Closing player socket: '%s'", g_serr(errno));
}
// Clear data.
memset((void*)player, 0, sizeof(struct player));
}
struct flub* player_init(struct player* player, int fd) {
int flags;
struct flub* flub;
int ret;
long size;
const struct timeval timeval = {60, 0};
// Clear any previous data.
memset((void*)player, 0, sizeof(struct player));
// Set socket.
player->sockfd = fd;
player->connected = 1;
if (setsockopt(player->sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeval,
sizeof(timeval)) == -1) {
flub = g_flub_toss("Unable to set socket recieve timeout: "
"'%s'", g_serr(errno));
goto out;
}
// Create from-server pipe.
if (pipe(player->pipe_server_from) == -1) {
flub = g_flub_toss("Unable to create from-server pipe: "
"'%s'", g_serr(errno));
goto out;
}
ret = fcntl(player->pipe_server_from[1], F_GETPIPE_SZ);
if (ret != G_PLAYER_PIPE_WRITE_SIZE) {
// Resize pipe.
if (fcntl(player->pipe_server_from[1], F_SETPIPE_SZ,
G_PLAYER_PIPE_WRITE_SIZE) == -1) {
flub = g_flub_toss("Unable to set from-server pipe "
"size to '%u': '%s'", G_PLAYER_PIPE_WRITE_SIZE,
g_serr(errno));
goto out2;
}
}
errno = 0;
size = fpathconf(player->pipe_server_from[1], _PC_PIPE_BUF);
if (size == -1 && errno) {
flub = g_flub_toss("Unable to check from-server atomicity: "
"'%s'", g_serr(errno));
goto out2;
} else if (size != -1 && size < GLS_PIPE_BUF) {
flub = g_flub_toss("From-server pipe atomicity too small, "
"was '%li', >='%u' expected", size, GLS_PIPE_BUF);
goto out2;
}
flags = fcntl(player->pipe_server_from[1], F_GETFL);
if (flags == -1) {
flub = g_flub_toss("Unable to get from-server flags: '%s'",
g_serr(errno));
goto out2;
}
flags |= O_NONBLOCK;
if(fcntl(player->pipe_server_from[1], F_SETFL, flags) == -1) {
flub = g_flub_toss("Unable to set non-blocking on from-server "
"flags: '%s'", g_serr(errno));
goto out2;
}
// Create to-server pipe.
if (pipe(player->pipe_server_to) == -1) {
flub = g_flub_toss("Unable to create to-server pipe: "
"'%s'", g_serr(errno));
goto out2;
}
errno = 0;
size = fpathconf(player->pipe_server_to[1], _PC_PIPE_BUF);
if (size == -1 && errno) {
flub = g_flub_toss("Unable to check to-server atomicity: "
"'%s'", g_serr(errno));
goto out3;
} else if (size != -1 && size < GLS_PIPE_BUF) {
flub = g_flub_toss("To-server pipe atomicity too small, "
"was '%li', >='%u' expected", size, GLS_PIPE_BUF);
goto out3;
}
flags = fcntl(player->pipe_server_to[0], F_GETFL);
if (flags == -1) {
flub = g_flub_toss("Unable to get to-server flags: '%s'",
g_serr(errno));
goto out3;
}
flags |= O_NONBLOCK;
if (fcntl(player->pipe_server_to[0], F_SETFL, flags) == -1) {
flub = g_flub_toss("Unable to set non-blocking on to-server "
"flags: '%s'", g_serr(errno));
goto out3;
}
// Create player thread.
ret = pthread_create(&player->thread, NULL, player_thread,
(void*)player);
if (ret) {
flub = g_flub_toss("Unable to create pthread: '%s'",
g_serr(ret));
goto out3;
}
return NULL;
out3:
// Close to-server pipe.
if (close(player->pipe_server_to[0]) == -1) {
flub_append(flub, "unable to close to-server read end (%s)",
g_serr(errno));
}
if (close(player->pipe_server_to[1]) == -1) {
flub_append(flub, "unable to close to-server write end (%s)",
g_serr(errno));
}
out2:
// Close from-server pipe.
if (close(player->pipe_server_from[0]) == -1) {
flub_append(flub, "unable to close from-server read end (%s)",
g_serr(errno));
}
if (close(player->pipe_server_from[1]) == -1) {
flub_append(flub, "unable to close from-server write end(%s)",
g_serr(errno));
}
out:
// Close socket.
if (close(player->sockfd) == -1) {
flub_append(flub, "unable to close player socket (%s)",
g_serr(errno));
}
player->connected = 0;
return flub;
}
struct flub* player_kill(struct player* player) {
int flags;
// Close from-server pipe write end.
if (close(player->pipe_server_from[1]) == -1) {
g_log_warn("Unable to close from-server write end: '%s'",
g_serr(errno));
}
// Set socket to non-blocking.
flags = fcntl(player->sockfd, F_GETFD);
if (flags == -1) {
g_log_warn("Unable to get socket flags: '%s'", g_serr(errno));
} else {
flags |= O_NONBLOCK;
if (fcntl(player->sockfd, F_SETFD, flags) == -1) {
g_log_warn("Unable to set socket flags: '%s'",
g_serr(errno));
}
}
// Set killed flag.
player->killed = 1;
return NULL;
}
char* player_name(struct player* player) {
if (!player->connected) {
// Not connected.
strlcpy(player->name, "(nobody)", GLS_NICK_LENGTH);
} else if (!player->protoverokay || !player->authenticated) {
// Socket metadata.
socklen_t addrlen;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addrlen = sizeof(struct sockaddr_in);
if (getpeername(player->sockfd, &addr, &addrlen) == -1) {
strlcpy(player->name, "(error)", GLS_NICK_LENGTH);
}
snprintf(player->name, GLS_NICK_LENGTH, "%s port %u",
inet_ntoa(addr.sin_addr),
(unsigned)ntohs(addr.sin_port));
player->name[GLS_NICK_LENGTH - 1] = '\0';
} else {
// Player nickname.
strlcpy(player->name, player->nick, GLS_NICK_LENGTH);
}
return player->name;
}
void* player_thread(void* v_player) {
const int FD_PIPE = 0;
const int FD_SOCKET = 1;
struct flub* flub;
struct gls_packet packet;
struct player* player = (struct player*)v_player;
struct pollfd pollfds[2];
int ret;
// Initialize thread-specific data.
ret = g_serr_init();
if (ret) {
g_log_error("Unable to setup system error buffer");
goto out;
}
ret = g_flub_init();
if (ret) {
g_log_error("Unable to initialize flub: '%s'", g_serr(ret));
goto out;
}
flub = gls_init();
if (flub) {
flub = g_flub_toss("Unable to initialize gls buffer: '%s'",
flub->message);
goto out;
}
// Run the event loop.
flub = NULL;
while (1) {
// Check for io events.
memset(pollfds, 0, sizeof(pollfds));
pollfds[FD_PIPE].fd = player->pipe_server_from[0];
pollfds[FD_PIPE].events = POLLIN | POLLRDHUP;
pollfds[FD_SOCKET].fd = player->sockfd;
pollfds[FD_SOCKET].events = POLLIN | POLLRDHUP;
if (poll(pollfds, 2, -1) == -1) {
flub = g_flub_toss("Unable to poll fds: '%s'",
g_serr(errno));
break;
}
// Data from socket.
if (pollfds[FD_SOCKET].revents & POLLIN) {
// Check for disconnect.
if (pollfds[FD_SOCKET].revents &
(POLLHUP | POLLRDHUP)) {
// Finish reading from socket.
if (ioctl(player->sockfd, FIONREAD, &ret)
== -1) {
flub = g_flub_toss("Unable to check "
"for bytes after hangup: '%s'",
g_serr(errno));
break;
} if (!ret) {
// End of data.
break;
}
}
// Move data from socket to server.
flub = gls_packet_read(&packet, player->sockfd, 1);
if (flub) {
break;
}
flub = gls_packet_write(&packet,
player->pipe_server_to[1]);
if (flub) {
break;
}
} else if (pollfds[FD_SOCKET].revents & (POLLERR | POLLNVAL)) {
// Socket polling error.
flub = g_flub_toss("Socket polling error: '%s'",
g_serr(errno));
break;
}
// Data from server.
if (pollfds[FD_PIPE].revents & (POLLIN | POLLHUP)) {
// Check for server kill.
if (pollfds[FD_SOCKET].revents & POLLHUP) {
// Finish reading from server pipe.
if (ioctl(player->pipe_server_from[0], FIONREAD,
&ret)
== -1) {
flub = g_flub_toss("Unable to check "
"for bytes after kill: '%s'",
g_serr(errno));
break;
} if (!ret) {
// End of data.
break;
}
}
// Move data from server to socket.
flub = gls_packet_read(&packet,
player->pipe_server_from[0], 0);
if (flub) {
break;
}
flub = gls_packet_write(&packet, player->sockfd);
if (flub) {
break;
}
} else if (pollfds[FD_PIPE].revents & (POLLERR | POLLNVAL)) {
// Pipe polling error.
flub = g_flub_toss("Pipe polling error: '%s'",
g_serr(errno));
break;
}
}
// Close to-server pipe write end.
out:
if (close(player->pipe_server_to[1]) == -1) {
g_log_warn("Unable to close write-end of to-server pipe");
}
// Return success/failure.
if (flub) {
// The thread-specific data for the flub may be cleaned when this
// function exits, so move it somewhere more permanent.
memcpy(&player->flub, flub, sizeof(struct flub));
return &player->flub;
}
return NULL;
}