-
Notifications
You must be signed in to change notification settings - Fork 0
/
qotd.c
396 lines (321 loc) · 8.11 KB
/
qotd.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
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
/*
* qotd.c - IPv6 UPD QOTD server conforming to RFC865
*
* usage: qotd [filename]
* if filename is not given, quotes.txt will be tried
*
* Copyright 2016-2018 job <job@function1.nl>
*
* This code is released into the P U B L I C D O M A I N!
*
*/
/* IDEAS:
* comments
* improve random distribution
*/
/* Set to 1 if you want to watch the quote file for changes, so
* that when it changes the quote list is automatically updated.
* This uses linux's inotify API, so it may not work in some
* environments.
* adding -D COMPILE_AUTOUPDATE to your compiler flags will typically
* work as well */
#ifndef COMPILE_AUTOUPDATE
#define COMPILE_AUTOUPDATE 0
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#if COMPILE_AUTOUPDATE
#include <sys/inotify.h>
#include <poll.h>
#include <errno.h>
#endif
#define BUF_SIZE 512
struct Quote
{
char * msg;
unsigned short len;
};
/* Converts a sockaddr to a human readable one */
char * get_ip_str(const struct sockaddr * sa, char * s, size_t maxlen)
{
switch (sa->sa_family)
{
case AF_INET: /* IPv4 */
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
s, maxlen);
break;
case AF_INET6: /* IPv6 */
inet_ntop(AF_INET6,
&(((struct sockaddr_in6 *)sa)->sin6_addr),
s, maxlen);
break;
default: /* Unknown */
strncpy(s, "Unkown AF", maxlen);
return NULL;
}
return s;
}
/* prints msg, with error details, and exits */
void ferr(const char * msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
/* Parses a quote list from a file. Returns NULL if there are no
* quotes in file */
struct Quote * parse_quote_list(const char * filename, size_t * len)
{
FILE * input = fopen(filename, "r");
if (input == NULL)
{
/* I know that this should only be fatal for the first time,
* not when the quote file has been updated(fall back to old),
* but that seems to be a rare case and I am a little lazy. */
ferr("Couldn't read quote list file");
}
/* initial list length, will double every time there is too little
* space */
size_t list_len = 512;
struct Quote * list = malloc(list_len * sizeof(struct Quote));
struct Quote q; /* current quote */
char * p = NULL; /* current quote message */
int c; /* current unsigned char, cast to int for EOF */
char prevEsc = 0; /* previous char was escape char */
size_t i, j;
for (i = 0; ; i++)
{
p = malloc(BUF_SIZE);
/* -1 for terminating null byte */
for (j = 0; j < BUF_SIZE - 1;)
{
c = fgetc(input);
if (c == EOF)
{
goto endoffile;
}
if (c == '\\' && !prevEsc)
{
prevEsc = 1;
continue;
}
p[j] = (char)c;
j++;
if (c == '\n' && !prevEsc)
{
/* if this is hit, high chance msg < BUF_SIZE, so
* resize */
p = realloc(p, j);
break;
}
prevEsc = 0;
}
q.msg = p;
q.len = j;
if (i >= list_len)
{
list_len *= 2;
list = realloc(list, list_len * sizeof(struct Quote));
}
list[i] = q;
}
endoffile:
fclose(input);
free(p);
if (i == 0)
{
/* No quotes in list */
free(list);
return NULL;
}
/* if last quote has no \n quote won't be added */
list = realloc(list, i * sizeof(struct Quote));
*len = i;
return list;
}
#if COMPILE_AUTOUPDATE
/* frees a quote list */
/* only used with autoupdate */
void free_quote_list(struct Quote * l, size_t len)
{
for (size_t i = 0; i < len; i++)
{
free(l[i].msg);
}
free(l);
}
#endif
int main(int argc, char **argv)
{
size_t n_quotes, ran;
const struct in6_addr ia = IN6ADDR_ANY_INIT;
int sock, r;
struct sockaddr_in6 serv_addr;
struct sockaddr_storage cli_addr;
socklen_t cli_len = (socklen_t)sizeof(cli_addr);
char buf[BUF_SIZE];
char s[INET6_ADDRSTRLEN];
#if COMPILE_AUTOUPDATE
int watchfd; /* inotify watch file descriptor */
int wd; /* quotefile watch descriptor */
nfds_t fdlen = 2; /* number of poll() file descriptors(fds) */
struct pollfd fdlist[2]; /* 2 fds: sock & wd */
#endif
char * quotepath = argc > 1 ? argv[1] : "quotes.txt";
struct Quote * quotes = parse_quote_list(
quotepath, &n_quotes
);
if (quotes == NULL)
{
fprintf(stderr, "No quotes in list!\n");
exit(EXIT_FAILURE);
}
#if COMPILE_AUTOUPDATE
/* create file descriptor for the inotify api,
* which watches the quote file */
watchfd = inotify_init1(IN_NONBLOCK);
if (watchfd < 0)
{
ferr("inotify_init1");
}
/* add watch descriptor for quote file */
wd = inotify_add_watch(watchfd, quotepath, IN_CLOSE);
if (wd < 0)
{
fprintf(stderr, "Cannot watch %s:", quotepath);
ferr("inotify_add_watch");
}
#endif
srand(time(NULL)); /* seed the RNG with the current time */
sock = socket(AF_INET6, SOCK_DGRAM, 0); /* IPv6 UDP socket */
if (sock < 0)
ferr("Error opening socket");
const socklen_t yes = 1;
/* make the socket reusable */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(void*)&yes, sizeof(yes)) < 0)
ferr("Error on setsockopt");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin6_family = AF_INET6; /* IPv6 */
serv_addr.sin6_port = htons(17); /* spec specifies port 17*/
serv_addr.sin6_addr = ia; /* use local ip */
if (bind(sock, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
ferr("Error on binding socket");
#if COMPILE_AUTOUPDATE
/* socket input */
fdlist[0].fd = sock;
fdlist[0].events = POLLIN;
/* inotify quotefile */
fdlist[1].fd = watchfd;
fdlist[1].events = POLLIN;
#endif
while (1)
{
#if COMPILE_AUTOUPDATE
r = poll(fdlist, fdlen, -1);
if (r < 0)
{
/* EINTR is when there was an intteruption and the syscall
* fails. So do not fatal error, but try again! */
if (errno == EINTR)
{
continue;
}
ferr("poll");
}
if (r == 0) continue;
/* r > 0 from here */
if (fdlist[0].revents & POLLIN)
{
/* socket data available */
#endif
memset(buf, 0, BUF_SIZE);
r = recvfrom(sock, buf, BUF_SIZE - 1, 0,
(struct sockaddr *)&cli_addr, &cli_len);
printf("Got message from %s\t%s", get_ip_str(
(struct sockaddr *)&cli_addr, s, sizeof(s)), buf);
ran = rand() % n_quotes; /* modulo bias XD */
r = sendto(sock, quotes[ran].msg, quotes[ran].len, 0,
(struct sockaddr *)&cli_addr, cli_len);
if (r < 0)
perror("Error on sendto");
#if COMPILE_AUTOUPDATE
continue;
}
if (fdlist[1].revents & POLLIN)
{
/* inotify event: quote list might be changed */
/* Some systems cannot read integer variables if they
* are not properly aligned. On other systems, incorrect
* alignment may decrease performance. Hence, the buffer
* used for reading from the inotify file descriptor
* should have the same alignment as
* struct inotify_event. (Taken from man inotify) */
char ibuf[1024]
__attribute__ ((aligned(
__alignof__(struct inotify_event))));
ssize_t ilen;
struct inotify_event * ievent;
char * p;
/* browse through events */
while (1)
{
ilen = read(watchfd, ibuf, sizeof(ibuf));
if (ilen < 0 &&
!(errno == EAGAIN || errno == EWOULDBLOCK))
{
/* EAGAIN/EWOULDBLOCK is when read() would
* block in order to read data(See `man read`)*/
ferr("read");
}
if (ilen <= 0)
{
break;
}
for (p = ibuf; p < ibuf + ilen;
p += sizeof(struct inotify_event))
{
ievent = (struct inotify_event *)p;
if (ievent->mask & IN_CLOSE_WRITE)
{
/* might be changed! reparse quote list */
printf("Quote list has been changed, installing new one...\n");
size_t nq_len;
struct Quote * nq = parse_quote_list(
quotepath, &nq_len);
if (nq == NULL)
{
printf("New quote list has no quotes! Abort.\n");
break;
}
/* all good, update! */
free_quote_list(quotes, n_quotes);
quotes = nq;
n_quotes = nq_len;
/* break..? */
break;
}
if (ievent->len)
{
/*printf("%s\n", ievent->name);*/
p += ievent->len;
}
}
}
continue;
}
#endif
}
close(sock);
#if COMPILE_AUTOUPDATE
close(watchfd);
#endif
return 0;
}