forked from skywodd/mini_files_transfert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
345 lines (249 loc) · 7.38 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
/* Includes */
#include <stdio.h> /* For printf() */
#include <sys/socket.h> /* For socket() */
#include <netdb.h> /* For struct hostent */
#include <sys/time.h> /* For timeout */
#include <sys/types.h> /* For AF_NET */
#include <unistd.h> /* For write() */
#include <string.h> /* For memcpy() */
#include "common.h" /* For prototypes and error codes */
/**
* Clients table
*/
struct {
int free, ready, sockfd;
unsigned long flength;
FILE* fo;
} clients[NB_MAX_CLIENTS] = { { 1, 0, -1, 0, NULL } };
static int accept_client(const int sockfd) {
/* Local variables */
int i;
/* Debug message */
/*puts("[DEBUG] New client !");*/
/* Check all clients table */
for (i = 0; i < NB_MAX_CLIENTS; ++i) {
/* Search a free entry */
if (clients[i].free) {
/* Debug message */
printf("[DEBUG] Client accepted in slot %d\n", i);
/* Client accepted */
clients[i].free = 0;
/* Waiting for file header */
clients[i].ready = 0;
/* Store socket file descriptor */
clients[i].sockfd = sockfd;
/* Return without error */
return NO_ERROR;
}
}
/* Debug message */
puts("[DEBUG] Client dropped (no more free place) !");
/* No free entry found, drop client */
close(sockfd);
/* Return without error */
return NO_ERROR;
}
static int process_client(const int index) {
/* Local variables */
unsigned char buffer[BUFFER_SIZE]; /* Input buffer */
char filename[FILENAME_SIZE]; /* Temp buffer for filename */
long bytes_received, tmp; /* Number of byte received */
File_header_t *file_header; /* Pointer to File_header_t */
/* Debug message */
/*printf("[DEBUG] New event from client %d\n", index);*/
/* Check if client send header or data */
if (!clients[index].ready) { /* header */
/* Debug message */
puts("[DEBUG] Receiving header ...");
/* Receive data */
if ((bytes_received = read(clients[index].sockfd, buffer,
sizeof(File_header_t))) == -1) {
/* Drop error message */
perror("read()");
/* Return with error */
return SOCKET_ERROR;
}
if (!bytes_received) { /* no more data */
/* Debug message */
/*puts("[DEBUG] Socket closed !");*/
/* Free client table entry */
clients[index].free = 1;
/* Close client socket */
close(clients[index].sockfd);
/* Debug message */
puts("[DEBUG] Slot free !");
/* Return without error */
return NO_ERROR;
}
/* Turn buffer into file header */
file_header = (File_header_t*) buffer;
/* Get file info */
strcpy(filename, file_header->name);
printf("[DEBUG] Filename : %s\n", filename);
/* Get file length */
clients[index].flength = ntohl(file_header->length);
printf("[DEBUG] File length : %ld\n", clients[index].flength);
/* Debug message */
/*puts("[DEBUG] Opening output file ...");*/
/* Open output file */
if ((clients[index].fo = fopen(filename, "wb")) == NULL ) {
/* Drop error message */
perror("fopen()");
/* Return with error */
return FILE_ERROR;
}
/* Debug message */
puts("[DEBUG] Client ready for data !");
/* Client is now ready */
clients[index].ready = 1;
} else { /* data */
/* Debug message */
/*puts("[DEBUG] Receiving data ...");*/
/* Receive data */
tmp = (clients[index].flength > BUFFER_SIZE) ?
BUFFER_SIZE : clients[index].flength;
if ((bytes_received = read(clients[index].sockfd, buffer, tmp)) == -1) {
/* Drop error message */
perror("read()");
/* Return with error */
return SOCKET_ERROR;
}
/* Process data */
if (bytes_received) {
/* Debug message */
/*puts("[DEBUG] Writing data ...");*/
/* Write data to output file */
if (fwrite(buffer, 1, bytes_received, clients[index].fo)
!= bytes_received) {
/* Drop error message */
puts("I/O file error !");
/* Return with error */
return FILE_ERROR;
}
/* Update file length */
clients[index].flength -= bytes_received;
/*printf("[DEBUG] File length remaining : %ld\n",
clients[index].flength);*/
/* Check for end-of-file */
if (clients[index].flength <= 0) {
/* Debug message */
puts("[DEBUG] End-of-file !");
/* Waiting for next file header */
clients[index].ready = 0;
}
} else { /* no more data */
/* Debug message */
/*puts("[DEBUG] Socket closed !");*/
/* Free client table entry */
clients[index].free = 1;
/* Close client socket */
close(clients[index].sockfd);
/* Close output file */
fclose(clients[index].fo);
/* Debug message */
puts("[DEBUG] Slot free !");
}
}
/* Return without error */
return NO_ERROR;
}
int server_mode(const struct hostent *host, const unsigned int port) {
/* Local variables */
socklen_t addr_length; /* Sockaddr length */
struct sockaddr_in sa; /* Socket remote target info structure */
fd_set rfds; /* File descriptor set for select() */
int i, sockfd, conn_sockfd, maxfd, err; /* listening socket fd, client socket fd, max fd number */
/* Debug message */
printf("[DEBUG] Listen on %s : %d\n", host->h_name, port);
/* Compute socket bind structure */
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
memcpy(&sa.sin_addr, host->h_addr_list[0], sizeof(sa.sin_addr));
/* Debug message */
puts("[DEBUG] Opening socket ...");
/* Open socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
/* Drop error message */
perror("socket()");
/* Return with error */
return SOCKET_ERROR;
}
/* Debug message */
puts("[DEBUG] Binding socket ...");
/* Bind socket to local address */
if (bind(sockfd, (struct sockaddr*) &sa, sizeof sa) == -1) {
/* Drop error message */
perror("bind()");
/* Return with error */
return SOCKET_ERROR;
}
/* Debug message */
puts("[DEBUG] Start listening ...");
/* Start listening on socket */
if (listen(sockfd, 5) == -1) {
/* Drop error message */
perror("listen()");
/* Return with error */
return SOCKET_ERROR;
}
/* CTRL+C FTW #meGusta */
for (;;) {
/* Initialize fd_set and maxfd */
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
maxfd = sockfd;
/* Add all available clients into fd_set */
for (i = 0; i < NB_MAX_CLIENTS; ++i) {
/* If client is available */
if (!clients[i].free) {
/* Add it to fd_set */
FD_SET(clients[i].sockfd, &rfds);
/* Update maxfd */
if (clients[i].sockfd > maxfd)
maxfd = clients[i].sockfd;
}
}
/* Debug message */
/*puts("[DEBUG] Clients list reloaded !");*/
switch (select(maxfd + 1, &rfds, NULL, NULL, NULL )) {
case -1: /* If error */
/* Drop error message */
perror("select()");
/* Return with error */
return SELECT_ERROR;
break;
default: /* Socket event */
/* If event on listening socket */
if (FD_ISSET(sockfd, &rfds)) {
/* Accept new incoming socket */
if ((conn_sockfd = accept(sockfd, (struct sockaddr*) &sa,
&addr_length)) == -1) {
/* Drop error message */
perror("accept()");
/* Return with error */
return SOCKET_ERROR;
}
/* Process new incoming socket */
if ((err = accept_client(conn_sockfd)) != NO_ERROR)
return err;
}
/* Process event on other sockets */
for (i = 0; i < NB_MAX_CLIENTS; ++i) {
/* If client is available */
if (!clients[i].free) {
/* If event from client */
if (FD_ISSET(clients[i].sockfd, &rfds)) {
/* Process client */
if ((err = process_client(i)) != NO_ERROR)
return err;
}
}
}
break;
}
}
/* Close socket */
close(sockfd);
/* Return without error */
return NO_ERROR;
}