-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
358 lines (294 loc) · 7.45 KB
/
server.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "server.h"
#include "cclient.h"
#define MAXBUF 4000
#define MAXHNDL 100
#define MAXCONN 100
/**
* By Ryan McKinney
* TCP Chat Server
*/
int server_socket;
HandleNode *head = NULL;
HandleNode *tail = NULL;
int numHandles = 0;
fd_set allSockets;
void serve() {
int client_socket;
int last_socket = server_socket + 1;
fd_set waiting;
FD_SET(server_socket, &allSockets);
if (listen(server_socket, MAXCONN) < 0) {
perror("listen call");
exit(-1);
}
while (1) {
waiting = allSockets;
if (select(last_socket, &waiting, NULL, NULL, NULL) < 0) {
perror("select call");
exit(-1);
}
// Server socket has new waiting client
if (FD_ISSET(server_socket, &waiting)) {
if ((client_socket = accept(server_socket, NULL, NULL)) < 0) {
perror("accept call");
exit(-1);
}
if (client_socket >= last_socket) {
last_socket = client_socket + 1;
}
FD_SET(client_socket, &allSockets);
serveClient(client_socket);
}
// Check client sockets for incoming packets
int cur_socket;
for (cur_socket=0; cur_socket < last_socket; cur_socket++) {
if (cur_socket != server_socket && FD_ISSET(cur_socket, &waiting)) {
serveClient(cur_socket);
}
}
}
return;
}
void serveClient(int client_socket) {
int length;
char *buffer = malloc(MAXBUF);
if ((length = recv(client_socket, buffer, MAXBUF, 0)) < 0) {
perror("recv call");
exit(-1);
}
struct c_header* header = (c_header*)buffer;
// Check incoming message flag
switch (header->flag) {
case F_INIT:
serveInit(buffer, client_socket);
break;
case F_BCAST:
serveBCast(buffer, client_socket);
break;
case F_MSG:
serveMsg(buffer, client_socket);
break;
case F_LIST:
serveList(client_socket);
break;
case F_EXIT:
serveExit(client_socket);
break;
default:
// Client disconnect
removeClient(client_socket);
FD_CLR(client_socket, &allSockets);
numHandles--;
break;
}
}
void serveInit(char* packet, int inc_socket) {
char *handle = packet + sizeof(struct c_header);
c_header *inc_header = (c_header*)packet;
if (isHandleOpen(handle)) {
// Add handle to list
HandleNode *newNode = malloc(sizeof(HandleNode));
newNode->handle = handle;
newNode->handleLength = inc_header->length - sizeof(c_header);
newNode->socket = inc_socket;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = tail->next;
}
numHandles++;
// Send good flag
c_header *header = malloc(MAXBUF);
header->flag = F_INIT_GOOD;
header->length = sizeof(c_header);
// Set time until timeout
if (send(inc_socket, header, MAXBUF, 0) < 0) {
perror("send error");
exit(-1);
}
}
else {
// Send bad flag
c_header *header = malloc(MAXBUF);
header->flag = F_INIT_BAD;
header->length = sizeof(c_header);
send(inc_socket, header, MAXBUF, 0);
}
}
void serveMsg(char* packet, int inc_socket) {
char* current = packet + sizeof(c_header);
char curHandle[MAXBUF];
// Skip client handle info
u_char cLength = (*current);
current += sizeof(u_char) + cLength;
// Get num handles
u_char numHandles = (*current);
current += sizeof(u_char);
int i;
for (i=0; i < numHandles; i++) {
// Get destination handle info
u_char dLength = (*current);
current += sizeof(u_char);
memcpy(curHandle, current, dLength);
current += dLength;
int dest_socket = getSocket(curHandle);
if (dest_socket == -1) {
sendErrorPacket(inc_socket, curHandle, dLength);
}
else {
send(dest_socket, packet, MAXBUF, 0);
}
}
free(packet);
}
void serveBCast(char* packet, int inc_socket) {
// Forward packet to every socket but the incoming
HandleNode *curNode = head;
while (curNode != NULL) {
if (curNode->socket != inc_socket) {
send(curNode->socket, packet, MAXBUF, 0);
}
curNode = curNode->next;
}
}
void serveList(int inc_socket) {
char *packet = malloc(MAXBUF);
// Send INC flag packet first
c_header *header = (c_header*) packet;
header->flag = F_INC;
header->length = sizeof(c_header) + sizeof(int);
memcpy(packet + sizeof(c_header), &numHandles, sizeof(int));
send(inc_socket, packet, MAXBUF, 0);
// Send one Handle packet per handle
HandleNode *curNode = head;
while (curNode != NULL) {
header->flag = F_HNDL;
header->length = sizeof(c_header) + sizeof(u_char) + curNode->handleLength;
memcpy(packet + sizeof(c_header), &(curNode->handleLength), sizeof(u_char));
memcpy(packet + sizeof(c_header) + sizeof(u_char), curNode->handle,
curNode->handleLength);
send(inc_socket, packet, MAXBUF, 0);
curNode = curNode->next;
}
free(packet);
}
void serveExit(int inc_socket) {
numHandles--;
removeClient(inc_socket);
FD_CLR(inc_socket, &allSockets);
// Send ACK flag to exiting client
char *packet = malloc(MAXBUF);
c_header *header = (c_header*)packet;
header->flag = F_ACK;
header->length = sizeof(c_header);
send(inc_socket, packet, MAXBUF, 0);
free(packet);
}
int isHandleOpen(char* handle) {
HandleNode *curNode = head;
while (curNode != NULL) {
if (memcmp(handle, curNode->handle, curNode->handleLength) == 0) {
return 0;
}
curNode = curNode->next;
}
return 1;
}
int getSocket(char* handle) {
HandleNode *curNode = head;
while (curNode != NULL) {
if (memcmp(handle, curNode->handle, curNode->handleLength) == 0) {
return curNode->socket;
}
curNode = curNode->next;
}
return -1;
}
void removeClient(int client_socket) {
HandleNode *curNode = head;
HandleNode *prevNode = NULL;
while (curNode != NULL) {
if (curNode->socket == client_socket) {
if (prevNode == NULL) {
head = curNode->next;
}
else {
prevNode->next = curNode->next;
}
free(curNode);
}
prevNode = curNode;
curNode = curNode->next;
}
}
void sendErrorPacket(int socket_num, char *handle, u_char handleLength) {
char *packet = malloc(MAXBUF);
c_header *header = (c_header*)packet;
header->flag = F_ERROR;
header->length = sizeof(c_header) + sizeof(u_char) + handleLength;
char *current = packet + sizeof(c_header);
memcpy(current, &handleLength, sizeof(u_char));
current += sizeof(u_char);
memcpy(current, handle, handleLength);
send(socket_num, packet, MAXBUF, 0);
}
int setupServer(int port) {
int server_socket= 0;
struct sockaddr_in local;
socklen_t len= sizeof(local);
// create the socket
server_socket= socket(AF_INET, SOCK_STREAM, 0);
if(server_socket < 0) {
perror("socket call");
exit(1);
}
local.sin_family= AF_INET;
local.sin_addr.s_addr= INADDR_ANY;
local.sin_port= htons(port);
// bind the name (address) to a port
if (bind(server_socket, (struct sockaddr *) &local, sizeof(local)) < 0) {
perror("bind call");
exit(-1);
}
//get the port name and print it out
if (getsockname(server_socket, (struct sockaddr*)&local, &len) < 0) {
perror("getsockname call");
exit(-1);
}
if (listen(server_socket, BACKLOG) < 0) {
perror("listen call");
exit(-1);
}
printf("server has port %d \n", ntohs(local.sin_port));
return server_socket;
}
int main(int argc, char *argv[]) {
int server_port = 0;
if (argc > 2) {
perror("usage: server <port-number>\n");
exit(-1);
}
if (argc == 2) {
server_port = strtol(argv[1], NULL, 10);
}
server_socket = setupServer(server_port);
serve();
return 0;
}