-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
463 lines (391 loc) · 11.9 KB
/
Copy pathserver.c
File metadata and controls
463 lines (391 loc) · 11.9 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (c) 2012-2015 OSUE Team <osue-team@vmars.tuwien.ac.at>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* gcc -std=c99 -Wall -g -pedantic -DENDEBUG \
* -D_BSD_SOURCE -D_XOPEN_SOURCE=500 -o server server.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <arpa/inet.h>
/* === Constants === */
#define MAX_TRIES (35)
#define SLOTS (5)
#define COLORS (8)
#define READ_BYTES (2)
#define WRITE_BYTES (1)
#define BUFFER_BYTES (2)
#define SHIFT_WIDTH (3)
#define PARITY_ERR_BIT (6)
#define GAME_LOST_ERR_BIT (7)
#define EXIT_PARITY_ERROR (2)
#define EXIT_GAME_LOST (3)
#define EXIT_MULTIPLE_ERRORS (4)
#define BACKLOG (5)
/* === Macros === */
#ifdef ENDEBUG
#define DEBUG(...) do { fprintf(stderr, __VA_ARGS__); } while(0)
#else
#define DEBUG(...)
#endif
/* Length of an array */
#define COUNT_OF(x) (sizeof(x)/sizeof(x[0]))
/* === Global Variables === */
/* Name of the program */
static const char *progname = "server"; /* default name */
/* File descriptor for server socket */
static int sockfd = -1;
/* File descriptor for connection socket */
static int connfd = -1;
/* This variable is set upon receipt of a signal */
volatile sig_atomic_t quit = 0;
/* === Type Definitions === */
struct opts {
long int portno;
uint8_t secret[SLOTS];
};
/* === Prototypes === */
/**
* @brief Parse command line options
* @param argc The argument counter
* @param argv The argument vector
* @param options Struct where parsed arguments are stored
*/
static void parse_args(int argc, char **argv, struct opts *options);
/**
* @brief Read message from socket
*
* This code *illustrates* one way to deal with partial reads
*
* @param sockfd_con Socket to read from
* @param buffer Buffer where read data is stored
* @param n Size to read
* @return Pointer to buffer on success, else NULL
*/
static uint8_t *read_from_client(int sockfd_con, uint8_t *buffer, size_t n);
/**
* @brief Compute answer to request
* @param req Client's guess
* @param resp Buffer that will be sent to the client
* @param secret The server's secret
* @return Number of correct matches on success; -1 in case of a parity error
*/
static int compute_answer(uint16_t req, uint8_t *resp, uint8_t *secret);
/**
* @brief terminate program on program error
* @param exitcode exit code
* @param fmt format string
*/
static void bail_out(int exitcode, const char *fmt, ...);
/**
* @brief Signal handler
* @param sig Signal number catched
*/
static void signal_handler(int sig);
/**
* @brief free allocated resources
*/
static void free_resources(void);
/* === Implementations === */
static uint8_t *read_from_client(int fd, uint8_t *buffer, size_t n)
{
/* loop, as packet can arrive in several partial reads */
size_t bytes_recv = 0;
do {
ssize_t r;
r = recv(fd, buffer + bytes_recv, n - bytes_recv, 0);
if (r <= 0) {
return NULL;
}
bytes_recv += r;
} while (bytes_recv < n);
if (bytes_recv < n) {
return NULL;
}
return buffer;
}
static int compute_answer(uint16_t req, uint8_t *resp, uint8_t *secret)
{
int colors_left[COLORS];
int guess[COLORS];
uint8_t parity_calc, parity_recv;
int red, white;
int j;
parity_recv = (req >> 15) & 1;
/* extract the guess and calculate parity */
parity_calc = 0;
for (j = 0; j < SLOTS; ++j) {
int tmp = req & 0x7;
parity_calc ^= tmp ^ (tmp >> 1) ^ (tmp >> 2);
guess[j] = tmp;
req >>= SHIFT_WIDTH;
}
parity_calc &= 0x1;
/* marking red and white */
(void) memset(&colors_left[0], 0, sizeof(colors_left));
red = white = 0;
for (j = 0; j < SLOTS; ++j) {
/* mark red */
if (guess[j] == secret[j]) {
red++;
} else {
colors_left[secret[j]]++;
}
}
for (j = 0; j < SLOTS; ++j) {
/* not marked red */
if (guess[j] != secret[j]) {
if (colors_left[guess[j]] > 0) {
white++;
colors_left[guess[j]]--;
}
}
}
/* build response buffer */
resp[0] = red;
resp[0] |= (white << SHIFT_WIDTH);
if (parity_recv != parity_calc) {
resp[0] |= (1 << PARITY_ERR_BIT);
return -1;
} else {
return red;
}
}
static void bail_out(int exitcode, const char *fmt, ...)
{
va_list ap;
(void) fprintf(stderr, "%s: ", progname);
if (fmt != NULL) {
va_start(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
}
if (errno != 0) {
(void) fprintf(stderr, ": %s", strerror(errno));
}
(void) fprintf(stderr, "\n");
free_resources();
exit(exitcode);
}
static void free_resources(void)
{
/* clean up resources */
DEBUG("Shutting down server\n");
if(connfd >= 0) {
(void) close(connfd);
}
if(sockfd >= 0) {
(void) close(sockfd);
}
}
static void signal_handler(int sig)
{
quit = 1;
}
/**
* @brief Program entry point
* @param argc The argument counter
* @param argv The argument vector
* @return EXIT_SUCCESS on success, EXIT_PARITY_ERROR in case of an parity
* error, EXIT_GAME_LOST in case client needed to many guesses,
* EXIT_MULTIPLE_ERRORS in case multiple errors occured in one round
*/
int main(int argc, char *argv[])
{
struct opts options;
int round;
int ret;
parse_args(argc, argv, &options);
/* setup signal handlers */
const int signals[] = {SIGINT, SIGTERM};
struct sigaction s;
s.sa_handler = signal_handler;
s.sa_flags = 0;
if(sigfillset(&s.sa_mask) < 0) {
bail_out(EXIT_FAILURE, "sigfillset");
}
for(int i = 0; i < COUNT_OF(signals); i++) {
if (sigaction(signals[i], &s, NULL) < 0) {
bail_out(EXIT_FAILURE, "sigaction");
}
}
/* Create a new TCP/IP socket `sockfd`, and set the SO_REUSEADDR
option for this socket. Then bind the socket to localhost:portno,
listen, and wait for new connections, which should be assigned to
`connfd`. Terminate the program in case of an error.
*/
DEBUG("Anfang der connection:\n");
sockfd = socket(AF_INET,SOCK_STREAM,0);
int optval =1;
if( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) !=0) {
bail_out(EXIT_FAILURE, "setsockopt");
}
struct sockaddr_in server;
(void)memset(&server, 0, sizeof server);
server.sin_family = AF_INET;
server.sin_port = htons(options.portno);
server.sin_addr.s_addr = INADDR_ANY;
DEBUG("Binding...\n");
if(bind(sockfd, (struct sockaddr*)&server, sizeof server) < 0) {
bail_out(EXIT_FAILURE, "bind");
}
DEBUG("Listening...\n");
if(listen(sockfd, 5) <0) {
bail_out(EXIT_FAILURE, "listen");
}
DEBUG("server port: %li ....server_addr: %s\n",options.portno, "127.0.0.1");
DEBUG("Accepting...\n");
if((connfd = accept(sockfd, NULL, 0)) <0) {
bail_out(EXIT_FAILURE, "listen");
}
/* accepted the connection */
ret = EXIT_SUCCESS;
for (round = 1; round <= MAX_TRIES && !quit; ++round) {
uint16_t request;
static uint8_t buffer[BUFFER_BYTES];
int correct_guesses;
int error = 0;
/* read from client */
if (read_from_client(connfd, &buffer[0], READ_BYTES) == NULL) {
if (quit) break; /* caught signal */
bail_out(EXIT_FAILURE, "read_from_client");
}
request = (buffer[1] << 8) | buffer[0];
//(void)printf("Bekommen: %d %d = %d \n", buffer[1],buffer[0], request);
DEBUG("Round %d: Received 0x%x\n", round, request);
/* compute answer */
correct_guesses = compute_answer(request, buffer, options.secret);
if (round == MAX_TRIES && correct_guesses != SLOTS) {
buffer[0] |= 1 << GAME_LOST_ERR_BIT;
}
DEBUG("Sending byte 0x%x\n", buffer[0]);
/* send message to client */
//#error "insert your code here"
int sended;
if((sended = send(connfd, &buffer[0], sizeof(uint8_t), 0)) < 0) {
bail_out(EXIT_FAILURE, "send... only %d Bytes sended", sended);
}
/* We sent the answer to the client; now stop the game
if its over, or an error occured */
if (*buffer & (1<<PARITY_ERR_BIT)) {
(void) fprintf(stderr, "Parity error\n");
error = 1;
ret = EXIT_PARITY_ERROR;
}
if (*buffer & (1 << GAME_LOST_ERR_BIT)) {
(void) fprintf(stderr, "Game lost\n");
error = 1;
if (ret == EXIT_PARITY_ERROR) {
ret = EXIT_MULTIPLE_ERRORS;
} else {
ret = EXIT_GAME_LOST;
}
}
if (error) {
break;
} else if (correct_guesses == SLOTS) {
/* won */
(void) printf("Runden: %d\n", round);
break;
}
}
/* we are done */
free_resources();
return ret;
}
static void parse_args(int argc, char **argv, struct opts *options)
{
int i;
char *port_arg;
char *secret_arg;
char *endptr;
enum { beige, darkblue, green, orange, red, black, violet, white };
if(argc > 0) {
progname = argv[0];
}
if (argc != 3) {
bail_out(EXIT_FAILURE,
"Usage: %s <server-port> <secret-sequence>", progname);
}
port_arg = argv[1];
secret_arg = argv[2];
errno = 0;
options->portno = strtol(port_arg, &endptr, 10);
if ((errno == ERANGE &&
(options->portno == LONG_MAX || options->portno == LONG_MIN))
|| (errno != 0 && options->portno == 0)) {
bail_out(EXIT_FAILURE, "strtol");
}
if (endptr == port_arg) {
bail_out(EXIT_FAILURE, "No digits were found");
}
/* If we got here, strtol() successfully parsed a number */
if (*endptr != '\0') { /* In principle not necessarily an error... */
bail_out(EXIT_FAILURE,
"Further characters after <server-port>: %s", endptr);
}
/* check for valid port range */
if (options->portno < 1 || options->portno > 65535)
{
bail_out(EXIT_FAILURE, "Use a valid TCP/IP port range (1-65535)");
}
if (strlen(secret_arg) != SLOTS) {
bail_out(EXIT_FAILURE,
"<secret-sequence> has to be %d chars long", SLOTS);
}
/* read secret */
for (i = 0; i < SLOTS; ++i) {
uint8_t color;
switch (secret_arg[i]) {
case 'b':
color = beige;
break;
case 'd':
color = darkblue;
break;
case 'g':
color = green;
break;
case 'o':
color = orange;
break;
case 'r':
color = red;
break;
case 's':
color = black;
break;
case 'v':
color = violet;
break;
case 'w':
color = white;
break;
default:
bail_out(EXIT_FAILURE,
"Bad Color '%c' in <secret-sequence>", secret_arg[i]);
}
options->secret[i] = color;
}
}