-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
321 lines (304 loc) · 8.5 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
#include "server.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
#include "config.h"
#include "util.h"
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
/* Private function declaration */
int server_readFromClient(int fd, int maxRead);
char* server_fdToIp(int fd);
void server_writeToAll_start(int efd);
void server_writeToAll(int efd, char* msg);
void server_writeToAll_end(int efd);
void server_writeToClient_start(int fd);
void server_writeToClient(int fd, char* msg);
void server_writeToClient_end(int fd);
void server_writeNoticeAll(int efd, char* notice, char* data);
void server_disconnectClient(int fd);
/* Variable declaration */
int socketfd;
int maxClientRead = MAX_READ;
fd_set active_fd_set;
fd_set read_fd_set;
void server_init(int port, int maxRead){
struct sockaddr_in name;
maxClientRead = maxRead;
/* Create the socket */
socketfd = socket(PF_INET, SOCK_STREAM, 0);
if(socketfd < 0){
socketfd = -1;
perror("socket");
exit(EXIT_FAILURE);
}
/* Give the socket a name */
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
/* Bind the socket */
if(bind(socketfd, (struct sockaddr*)&name, sizeof(name)) < 0){
socketfd = -1;
perror("bind");
exit(EXIT_FAILURE);
}
/* Check whether we initialized correctly */
if(listen(socketfd, 1) < 0){
socketfd = -1;
perror("listen");
exit(EXIT_FAILURE);
}
/* Initialise set of active sockets */
FD_ZERO(&active_fd_set);
FD_SET(socketfd, &active_fd_set);
}
void server_service(){
/* Ensure the socket file descriptor is valid */
if(socketfd < 0){
socketfd = -1;
perror("loop");
exit(EXIT_FAILURE);
}
/* Block until input arrives on one or more active sockets */
read_fd_set = active_fd_set;
if(select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0){
socketfd = -1;
perror("select");
exit(EXIT_FAILURE);
}
/* Service all the sockets with input pending */
struct sockaddr_in clientName;
socklen_t size = sizeof(clientName);
for(int i = 0; i < FD_SETSIZE; ++i){
if(FD_ISSET(i, &read_fd_set)){
if(i == socketfd){
/* Connection request on original socket */
int new = accept(socketfd, (struct sockaddr*)&clientName, &size);
if(new < 0){
socketfd = -1;
perror("accept");
exit(EXIT_FAILURE);
}
struct timeval timeout;
timeout.tv_sec = TIMEOUT_S;
setsockopt(new, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(new, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
FD_SET(new, &active_fd_set);
server_writeToClient(new, MSG_BANNER);
/* Send to all connected clients */
server_writeNoticeAll(new, "Connected", server_fdToIp(new));
}else{
/* Data arriving on an already-connected socket */
if(server_readFromClient(i, maxClientRead) < 0){
/* Finally disconnected client */
server_disconnectClient(i);
}
}
}
}
}
/**
* server_readFromClient()
*
* Read data from the client.
*
* @param fd The socket to be read from.
* @param maxRead The maximum data to be read.
* @return -1 on error, otherwise 0.
**/
int server_readFromClient(int fd, int maxRead){
char buff[maxRead + 1];
int n = read(fd, buff, maxRead);
if(n < 0){
/* Read error */
perror("read");
}else if(n == 0){
/* End-of-file */
return -1;
}else{
/* Check for commands */
if(buff[0] == '/'){
switch(buff[1]){
case 'b' :
server_writeToClient(fd, MSG_BANNER);
break;
case 'h' :
case '?' :
server_writeToClient(fd, MSG_HELP);
break;
case 'l' :
server_writeToClient_start(fd);
server_writeToClient(fd, MSG_PRE);
server_writeToClient(fd, "IP list:\n");
for(int i = 0; i < FD_SETSIZE; ++i){
if(FD_ISSET(i, &active_fd_set)){
if(i >= 0 && i != socketfd){
server_writeToClient(fd, MSG_PRE);
server_writeToClient(fd, "- ");
server_writeToClient(fd, server_fdToIp(i));
server_writeToClient(fd, "\n");
}
}
}
server_writeToClient_end(fd);
break;
case 'q' :
server_writeToClient(fd, MSG_EXIT);
server_disconnectClient(fd);
break;
default :
/* Do nothing */
break;
}
/* If not a command, relay message to other users */
}else if(buff[0] >= ' ' && buff[0] <= '~'){
/* Sanitize data to ASCII and remove line endings */
char r = '\0';
for(int i = n - 1; i >= 0; i--){
char c = buff[i];
if(c < ' ' || c > '~'){
buff[i] = r;
}else{
r = ' ';
}
}
buff[n] = '\0';
/* Prepare shared values */
char* ip = server_fdToIp(fd);
fprintf(stderr, "%s%s: %s\n", MSG_PRE, ip, buff);
/* Send to all connected clients */
server_writeToAll_start(fd);
server_writeToAll(fd, MSG_PRE);
server_writeToAll(fd, ip);
server_writeToAll(fd, ": ");
server_writeToAll(fd, buff);
server_writeToAll(fd, "\n");
server_writeToAll_end(fd);
}
return 0;
}
}
char* server_fdToIp(int fd){
struct sockaddr_in clientName;
socklen_t size = sizeof(clientName);
if(getpeername(fd, (struct sockaddr*)&clientName, &size) < 0){
perror("socket_info");
return "0.0.0.0";
}
return inet_ntoa(clientName.sin_addr);
}
/**
* server_writeToAll_start()
*
* Begin a corked write to all active sockets.
*
* @param efd An excluded socket file descriptor to not write to. Set to -1 if
* you want the message to be sent to all sockets.
**/
void server_writeToAll_start(int efd){
for(int i = 0; i < FD_SETSIZE; ++i){
if(FD_ISSET(i, &active_fd_set)){
if(i >= 0 && i != socketfd && i != efd){
server_writeToClient_start(i);
}
}
}
}
/**
* server_writeToAll()
*
* Begin writing a packet to the client. NOTE: Messages are buffered in kernel
* space.
*
* @param efd An excluded socket file descriptor to not write to. Set to -1 if
* you want the message to be sent to all sockets.
**/
void server_writeToAll(int efd, char* msg){
for(int i = 0; i < FD_SETSIZE; ++i){
if(FD_ISSET(i, &active_fd_set)){
if(i >= 0 && i != socketfd && i != efd){
server_writeToClient(i, msg);
}
}
}
}
/**
* server_writeToAll_end()
*
* Close a corked write to all active sockets, causing a flush write.
*
* @param efd An excluded socket file descriptor to not write to. Set to -1 if
* you want the message to be sent to all sockets.
**/
void server_writeToAll_end(int efd){
for(int i = 0; i < FD_SETSIZE; ++i){
if(FD_ISSET(i, &active_fd_set)){
if(i >= 0 && i != socketfd && i != efd){
server_writeToClient_end(i);
}
}
}
}
/* TODO */
void server_writeToClient_start(int fd){
int opt = 1;
/* Set TCP to cork mode and prevent it from sending a packet */
setsockopt(fd, SOL_TCP, TCP_CORK, &opt, sizeof(opt));
}
/**
* server_writeToClient()
*
* Write a message to the client. NOTE: See server_writeToClient_start() and
* server_writeToClient_end() if sending multiple messages to a socket.
*
* @param fd The socket to be written to.
* @param msg The message to be sent.
**/
void server_writeToClient(int fd, char* msg){
/* Send the data */
if(send(fd, msg, util_strLen(msg), 0) < 0){
perror("send");
}
}
/* TODO */
void server_writeToClient_end(int fd){
int opt = 0;
/* Set TCP to cork mode and prevent it from sending a packet */
setsockopt(fd, SOL_TCP, TCP_CORK, &opt, sizeof(opt));
}
/* TODO */
void server_writeNoticeAll(int efd, char* notice, char* data){
fprintf(stderr, "%s%s -> %s\n", MSG_PRE, notice, data);
server_writeToAll_start(efd);
server_writeToAll(efd, MSG_PRE);
if(notice != NULL){
server_writeToAll(efd, notice);
}
if(data != NULL){
server_writeToAll(efd, " -> ");
server_writeToAll(efd, data);
}
server_writeToAll(efd, "\n");
server_writeToAll_end(efd);
}
/**
* server_disconnectClient()
*
* Disconnect a given client socket.
*
* @param fd The socket to be closed.
**/
void server_disconnectClient(int fd){
/* Send to all connected clients */
server_writeNoticeAll(fd, "Disconnected", server_fdToIp(fd));
/* Finally, flush and disconnect */
shutdown(fd, SHUT_WR);
FD_CLR(fd, &active_fd_set);
}