-
Notifications
You must be signed in to change notification settings - Fork 0
/
packethandler.c
248 lines (206 loc) · 7.09 KB
/
packethandler.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
/*
* packetreader.c
*
* Created on: 16 Sep 2017
* Author: Bibl
*/
#include <stdio.h>
#include <winsock2.h>
#include "packetqueue.h"
#include "packethandler.h"
int is_live_connection(struct connection *conn) {
return conn != NULL;
}
void dissolve_connection(struct connection **connptr) {
/* close the socket connection */
struct connection *conn = *connptr;
SOCKET socket = conn->socket;
closesocket(socket);
/* free the actual struct in memory */
GlobalFree(conn);
/* set the pointer to the connection pointer
* to NULL indicating that the connection at
* that index is free. */
memset(connptr, 0, sizeof(struct connection *));
}
struct connection *create_connection(struct connection *connections[], SOCKET *accept_socket) {
/* we are storing pointers to connection
* structs in our array as these structs
* are very large in memory (few KB). most
* of this memory is occupied by the underlying
* read and write buffers. we may change
* them to be pointers to the buffers in
* the future, so that they can be dynamically
* resized as well. */
struct connection *conn = GlobalAlloc(GPTR, sizeof(struct connection));
if(conn != NULL) {
memset(conn, 0, sizeof(struct connection));
conn->socket = *accept_socket;
conn->read_buf.buf = conn->__read_buf;
conn->read_buf.len = DATA_BUF_LEN;
conn->write_buf.buf = conn->__write_buf;
conn->write_buf.len = DATA_BUF_LEN;
/* the connections array is maintained such
* that when a connection is closed, that
* space in the array is zeroed. when a new
* connection is created, that memory is
* set to a pointer to the connection struct. */
for (int i = 0; i < MAX_CLIENTS; i++) {
struct connection *c = connections[i];
/* zeroed memory, no connection in this position */
if (!is_live_connection(c)) {
conn->idx = i;
connections[i] = conn;
return conn;
}
}
}
return NULL;
}
void *handler_start(void *arg) {
struct server_config *config = arg;
/* initialise the server state */
config->is_live = 1;
config->is_error = 0;
SOCKET listen_socket, accept_socket;
/* allocate our client connections (pointers) */
struct connection *connections[MAX_CLIENTS];
memset(connections, 0, sizeof(connections));
FD_SET write_set, read_set;
DWORD num_descs_ready;
DWORD non_blocking = 1;
DWORD io_bytes = 0;
DWORD flags = 0;
/* server socket/winsock initialisation */
if((listen_socket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) {
config->error_msg = "WSASocket failed";
goto error;
}
if(bind(listen_socket, (PSOCKADDR) &config->addr, sizeof(config->addr)) == SOCKET_ERROR) {
config->error_msg = "bind failed";
goto error;
}
if(listen(listen_socket, 5)) {
config->error_msg = "listen failed";
goto error;
}
/* put the socket into non blocking mode so
* that we can use select(). */
if(ioctlsocket(listen_socket, FIONBIO, &non_blocking) == SOCKET_ERROR) {
config->error_msg = "ioctlsocket/non blocking mode on listen socket failed";
goto error;
}
/* main event loop */
while(TRUE) {
/* clear our sets before each select() call */
FD_ZERO(&read_set);
FD_ZERO(&write_set);
/* set the bit for our server listen socket. notifications
* on this socket indicate that a client is trying to
* connect (i.e. we need to accept() it). */
FD_SET(listen_socket, &read_set);
/* for all active connections, set the read bit unconditionally. */
for(int i=0; i < MAX_CLIENTS; i++) {
struct connection *conn = connections[i];
if(is_live_connection(conn)) {
SOCKET client_socket = conn->socket;
FD_SET(client_socket, &read_set);
// TODO: set for writing when packets are pending.
}
}
/* blocking call, returns when one of the indicated
* file descriptors in either the reader or write set
* become available. timeout is set to NULL, i.e. no
* timeout. */
num_descs_ready = select(0, &read_set, &write_set, NULL, NULL);
/* error checking (fatal) */
if(num_descs_ready == SOCKET_ERROR) {
// TODO: maybe we need to disconnect all clients?
// would it work? the failure could be due
// to an error on the write streams.
config->error_msg = "select failed";
goto error;
}
/* set on the listen socket, i.e. accept */
if(FD_ISSET(listen_socket, &read_set)) {
num_descs_ready--;
accept_socket = accept(listen_socket, NULL, NULL);
if(accept_socket == INVALID_SOCKET) {
if(WSAGetLastError() != WSAEWOULDBLOCK) {
config->error_msg = "accept failed";
goto error;
}
} else {
/* set the connected client socket to be
* non blocking as well. */
non_blocking = 1;
if(ioctlsocket(accept_socket, FIONBIO, &non_blocking) == SOCKET_ERROR) {
config->error_msg = "ioctlsocket/non blocking mode on accepted socket failed";
goto error;
}
/* try to create the connection struct. note that
* create_connection also adds the created connection
* to the connections array. */
struct connection *conn;
if(!(conn = create_connection(connections, &accept_socket))) {
printf("could not create connection to %d\n", accept_socket);
} else {
printf("connected on %d @%d\n", conn->socket, conn->idx);
}
}
}
/* go through all the connections, but stop early
* if we have exhausted our indicated descriptors. */
for(int i=0; num_descs_ready > 0; i++) {
struct connection *conn = connections[i];
if(is_live_connection(conn)) {
/* note for the following: WSAEWOULDBLOCK is non fatal. */
/* read on the client socket. */
if(FD_ISSET(conn->socket, &read_set)) {
printf("[R] it's a read on %d.\n", conn->socket);
num_descs_ready--;
flags = io_bytes = 0;
if(WSARecv(conn->socket, &conn->read_buf, 1, &io_bytes, &flags, NULL, NULL) == SOCKET_ERROR) {
if(WSAGetLastError() != WSAEWOULDBLOCK) {
printf("WSARecv failed with %d on %d\n", WSAGetLastError(), conn->socket);
dissolve_connection(&conn);
}
continue;
} else {
// TODO: parse packet
printf(" of %ld bytes.\n", io_bytes);
if(io_bytes == 0) {
// closed
dissolve_connection(&conn);
continue;
}
// else ok
}
}
/* write on the client socket. */
if(FD_ISSET(conn->socket, &write_set)) {
printf("[W] it's a write on %d.\n", conn->socket);
num_descs_ready--;
if(WSASend(conn->socket, &conn->write_buf, 1, &io_bytes, 0 , NULL, NULL) == SOCKET_ERROR) {
if(WSAGetLastError() != WSAEWOULDBLOCK) {
printf("WSASend failed with %d on %d\n", WSAGetLastError(), conn->socket);
dissolve_connection(&conn);
}
continue;
} else {
// TODO: write packet
printf(" of %ld bytes.\n", io_bytes);
}
}
}
}
}
/* the loop should not exit to here */
printf("fatal: loop leaked (wth)\n");
exit(2);
error: {
config->is_live = 0;
config->is_error = 1;
}
return NULL;
}