forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.c
271 lines (240 loc) · 7.12 KB
/
connection.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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include "common.h"
#include "log.h"
#include "connection.h"
#include "server.h"
#include "sarray.h"
#include "ptrlist.h"
#include "id.h"
#include "cli.h"
#include "universe.h"
#include "sector.h"
#include "star.h"
#include "base.h"
#include "planet.h"
#include "player.h"
extern struct universe *univ;
struct conndata* conn_create()
{
struct conndata *data;
int r = 1;
data = malloc(sizeof(*data));
if (data != NULL) {
memset(data, 0, sizeof(*data));
data->id = gen_id();
data->rbufs = CONN_BUFSIZE;
if ((data->rbuf = malloc(data->rbufs)) == NULL) {
log_printfn("connection", "failed allocating receive buffer for connection %zx", data->id);
conndata_free(data);
return NULL;
}
data->sbufs = CONN_MAXBUFSIZE;
if ((data->sbuf = malloc(data->sbufs)) == NULL) {
log_printfn("connection", "failed allocating send buffer for connection %zx", data->id);
conndata_free(data);
return NULL;
}
if (pipe(data->threadfds) != 0) {
log_printfn("connection", "failed opening pipe to thread for connection %zx", data->id);
conndata_free(data);
return NULL;
}
}
return data;
}
static void conn_signalserver(struct conndata *data, struct signal *msg, char *msgdata)
{
if (write(data->serverfd, msg, sizeof(*msg)) < 1)
bug("server signalling fd seems closed when sending signal: error %d (%s)", errno, strerror(errno));
if (msg->cnt > 0) {
if (write(data->serverfd, msgdata, msg->cnt) < 1)
bug("server signalling fd seems closed when seems closed when sending data: error %d (%s)", errno, strerror(errno));
}
}
/*
* This function needs to be very safe as it can be called on a
* half-initialized conndata structure if something went wrong.
*/
void conndata_free(void *ptr)
{
struct conndata *data = ptr;
if (data != NULL) {
if (data->peerfd)
close(data->peerfd);
if (data->threadfds[0])
close(data->threadfds[0]);
if (data->threadfds[1])
close(data->threadfds[1]);
if (data->peer)
free(data->peer);
if (data->rbuf)
free(data->rbuf);
if (data->sbuf)
free(data->sbuf);
if (data->pl)
player_free(data->pl);
}
}
void conn_cleanexit(struct conndata *data)
{
log_printfn("connection", "connection %zx is terminating", data->id);
struct signal msg = {
.cnt = sizeof(data->id),
.type = MSG_RM
};
conn_signalserver(data, &msg, (char*)&data->id);
pthread_exit(0);
}
void conn_send(struct conndata *data, char *format, ...)
{
va_list ap;
size_t len, sb = 0;
va_start(ap, format);
vsnprintf(data->sbuf, data->sbufs, format, ap);
va_end(ap);
len = strlen(data->sbuf);
if (len == data->sbufs)
log_printfn("connection", "warning: sending maximum amount allowable (%d bytes) on connection %zx, this probably indicates overflow", data->sbufs, data->id);
do {
sb += send(data->peerfd, data->sbuf + sb, len - sb, MSG_NOSIGNAL);
if (sb < 1) {
log_printfn("connection", "send error (connection id %zx), terminating connection", data->id);
conn_cleanexit(data);
}
} while (sb < len);
}
void conn_error(struct conndata *data, char *format, ...)
{
va_list ap;
va_start(ap, format);
conn_send(data, "Oops! An internal error occured: %s.\nYour current state is NOT saved and you are being forcibly disconnected.\nSorry!", format, ap);
va_end(ap);
conn_cleanexit(data);
}
static void conn_act(struct conndata *data)
{
struct sector *s;
if (data->rbuf[0] != '\0' && cli_run_cmd(data->pl->cli, data->rbuf) < 0)
conn_send(data, "Unknown command or syntax error: \"%s\"\n", data->rbuf);
}
static void conn_handlesignal(struct conndata *data, struct signal *msg, char *msgdata)
{
switch (msg->type) {
case MSG_TERM:
conn_send(data, "\nServer is shutting down, you are being disconnected.\n");
conn_cleanexit(data);
break;
case MSG_PAUSE:
conn_send(data, "\nYou have been paused by God. This might mean the whole universe is currently on hold\n"
"or just you. Anything you enter at the prompt will queue up until you are resumed.\n");
data->paused = 1;
break;
case MSG_CONT:
conn_send(data, "\nYou have been resumed, feel free to play away!\n");
data->paused = 0;
break;
case MSG_WALL:
conn_send(data, "\nMessage to all connected users:\n"
"%s"
"\nEnd of message.\n", msgdata);
break;
default:
log_printfn("connection", "received unknown signal: %d", msg->type);
}
}
static void conn_receivemsg(struct conndata *data, int fd)
{
struct signal msg;
char *msgdata;
read(fd, &msg, sizeof(msg));
if (msg.cnt > 0) {
msgdata = alloca(msg.cnt);
read(fd, msgdata, msg.cnt);
} else {
msgdata = NULL;
}
conn_handlesignal(data, &msg, msgdata);
}
static void conn_loop(struct conndata *data)
{
int rb, i;
char* ptr;
while (1) {
rb = 0;
do {
conn_send(data, "yastg> ");
/* FIXME: Doesn't handle CTRL-D for some reason. */
FD_ZERO(&data->rfds);
FD_SET(data->peerfd, &data->rfds);
FD_SET(data->threadfds[0], &data->rfds);
i = select(MAX(data->peerfd, data->threadfds[0]) + 1, &data->rfds, NULL, NULL, NULL);
if (i == -1) {
log_printfn("connection", "select() reported error, terminating connection %zx", data->id);
conn_cleanexit(data);
}
if (FD_ISSET(data->threadfds[0], &data->rfds)) {
/* We have received a message on the signalling fd */
do {
conn_receivemsg(data, data->threadfds[0]);
} while (data->paused);
}
if (FD_ISSET(data->peerfd, &data->rfds)) {
/* We have received something from the peer */
rb += recv(data->peerfd, data->rbuf + rb, data->rbufs - rb, 0);
if (rb < 1) {
log_printfn("connection", "peer %s disconnected, terminating connection %zx", data->peer, data->id);
conn_cleanexit(data);
}
if ((rb == data->rbufs) && (data->rbuf[rb - 1] != '\n')) {
if (data->rbufs == CONN_MAXBUFSIZE) {
log_printfn("connection", "peer sent more data than allowed (%u), connection %zx terminated", CONN_MAXBUFSIZE, data->id);
conn_cleanexit(data);
}
data->rbufs <<= 1;
if (data->rbufs > CONN_MAXBUFSIZE)
data->rbufs = CONN_MAXBUFSIZE;
if ((ptr = realloc(data->rbuf, data->rbufs)) == NULL) {
data->rbufs >>= 1;
log_printfn("connection", "unable to increase receive buffer size, connection %zx terminated", data->id);
conn_cleanexit(data);
} else {
data->rbuf = ptr;
}
}
}
} while ((rb == 0) || (data->rbuf[rb - 1] != '\n'));
data->rbuf[rb - 1] = '\0';
chomp(data->rbuf);
mprintf("debug: received \"%s\" on socket\n", data->rbuf);
conn_act(data);
}
}
void* conn_main(void *dataptr)
{
struct conndata *data = dataptr;
/* temporary solution */
/* Create player */
data->pl = malloc(sizeof(struct player));
data->pl->name = strdup("Alfred");
data->pl->conn = data;
log_printfn("connection", "peer %s successfully logged in as %s", data->peer, data->pl->name);
player_init(data->pl);
conn_loop(data);
log_printfn("connection", "peer %s disconnected, cleaning up", data->peer);
conn_cleanexit(data);
return NULL;
}