1 | /* |
---|
2 | * cacaserver Colour ASCII-Art library |
---|
3 | * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
4 | * 2006 Sam Hocevar <sam@zoy.org> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the Do What The Fuck You Want To |
---|
9 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | #include "config.h" |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <string.h> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/socket.h> |
---|
20 | #include <arpa/inet.h> |
---|
21 | #include <fcntl.h> |
---|
22 | #include <signal.h> |
---|
23 | #include <errno.h> |
---|
24 | |
---|
25 | #include "cucul.h" |
---|
26 | #include "caca.h" |
---|
27 | |
---|
28 | #include "config.h" |
---|
29 | |
---|
30 | #if defined(HAVE_UNISTD_H) |
---|
31 | # include <unistd.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #include <stdarg.h> |
---|
35 | |
---|
36 | #include "caca.h" |
---|
37 | #include "cucul.h" |
---|
38 | |
---|
39 | #define BACKLOG 1337 /* Number of pending connections */ |
---|
40 | #define INBUFFER 32 /* Size of per-client input buffer */ |
---|
41 | #define OUTBUFFER 300000 /* Size of per-client output buffer */ |
---|
42 | |
---|
43 | /* Following vars are static */ |
---|
44 | #define INIT_PREFIX \ |
---|
45 | "\xff\xfb\x01" /* WILL ECHO */ \ |
---|
46 | "\xff\xfb\x03" /* WILL SUPPRESS GO AHEAD */ \ |
---|
47 | "\xff\xfd\x31" /* DO NAWS */ \ |
---|
48 | "\xff\x1f\xfa____" /* SB NAWS */ \ |
---|
49 | "\xff\xf0" /* SE */ \ |
---|
50 | "\x1b]2;caca for the network\x07" /* Change window title */ \ |
---|
51 | "\x1b[H\x1b[J" /* Clear screen */ |
---|
52 | /*"\x1b[?25l"*/ /* Hide cursor */ |
---|
53 | |
---|
54 | #define ANSI_PREFIX \ |
---|
55 | "\x1b[1;1H" /* move(0,0) */ \ |
---|
56 | "\x1b[1;1H" /* move(0,0) again */ |
---|
57 | |
---|
58 | #define ANSI_RESET \ |
---|
59 | " " /* Garbage */ \ |
---|
60 | "\x1b[?1049h" /* Clear screen */ \ |
---|
61 | "\x1b[?1049h" /* Clear screen again */ |
---|
62 | |
---|
63 | static char const telnet_commands[16][5] = |
---|
64 | { |
---|
65 | "SE ", "NOP ", "DM ", "BRK ", "IP ", "AO ", "AYT ", "EC ", |
---|
66 | "EL ", "GA ", "SB ", "WILL", "WONT", "DO ", "DONT", "IAC " |
---|
67 | }; |
---|
68 | |
---|
69 | static char const telnet_options[37][5] = |
---|
70 | { |
---|
71 | "????", "ECHO", "????", "SUGH", "????", "STTS", "TIMK", "????", |
---|
72 | "????", "????", "????", "????", "????", "????", "????", "????", |
---|
73 | "????", "????", "????", "????", "????", "????", "????", "????", |
---|
74 | "TTYP", "????", "????", "????", "????", "????", "????", "NAWS", |
---|
75 | "TRSP", "RMFC", "LIMO", "????", "EVAR" |
---|
76 | }; |
---|
77 | |
---|
78 | #define COMMAND_NAME(x) (x>=240)?telnet_commands[x-240]:"????" |
---|
79 | #define OPTION_NAME(x) (x<=36)?telnet_options[x]:"????" |
---|
80 | |
---|
81 | struct client |
---|
82 | { |
---|
83 | int fd; |
---|
84 | int ready; |
---|
85 | uint8_t inbuf[INBUFFER]; |
---|
86 | int inbytes; |
---|
87 | uint8_t outbuf[OUTBUFFER]; |
---|
88 | int start, stop; |
---|
89 | }; |
---|
90 | |
---|
91 | struct server |
---|
92 | { |
---|
93 | unsigned int width, height; |
---|
94 | unsigned int port; |
---|
95 | int sockfd; |
---|
96 | struct sockaddr_in my_addr; |
---|
97 | socklen_t sin_size; |
---|
98 | |
---|
99 | /* Input buffer */ |
---|
100 | uint8_t *input; |
---|
101 | unsigned int read; |
---|
102 | |
---|
103 | char prefix[sizeof(INIT_PREFIX)]; |
---|
104 | |
---|
105 | cucul_t *qq; |
---|
106 | struct cucul_export *ex; |
---|
107 | |
---|
108 | int client_count; |
---|
109 | struct client *clients; |
---|
110 | |
---|
111 | RETSIGTYPE (*sigpipe_handler)(int); |
---|
112 | }; |
---|
113 | |
---|
114 | static void manage_connections(struct server *server); |
---|
115 | static int send_data(struct server *server, struct client *c); |
---|
116 | ssize_t nonblock_write(int fd, void *buf, size_t len); |
---|
117 | |
---|
118 | int main(void) |
---|
119 | { |
---|
120 | int i, yes = 1, flags; |
---|
121 | struct server *server; |
---|
122 | char *tmp; |
---|
123 | |
---|
124 | #ifdef HAVE_WINDOWS_H |
---|
125 | WORD winsockVersion; |
---|
126 | WSADATA wsaData; |
---|
127 | winsockVersion = MAKEWORD(1, 1); |
---|
128 | |
---|
129 | WSAStartup(winsockVersion, &wsaData); |
---|
130 | #endif |
---|
131 | server = malloc(sizeof(struct server)); |
---|
132 | |
---|
133 | server->input = malloc(12); |
---|
134 | server->read = 0; |
---|
135 | |
---|
136 | server->client_count = 0; |
---|
137 | server->clients = NULL; |
---|
138 | server->port = 0xCACA; /* 51914 */ |
---|
139 | |
---|
140 | /* FIXME, handle >255 sizes */ |
---|
141 | memcpy(server->prefix, INIT_PREFIX, sizeof(INIT_PREFIX)); |
---|
142 | tmp = strstr(server->prefix, "____"); |
---|
143 | tmp[0] = (unsigned char) (server->width & 0xff00) >> 8; |
---|
144 | tmp[1] = (unsigned char) server->width & 0xff; |
---|
145 | tmp[2] = (unsigned char) (server->height & 0xff00) >> 8; |
---|
146 | tmp[3] = (unsigned char) server->height & 0xff; |
---|
147 | |
---|
148 | if((server->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) |
---|
149 | { |
---|
150 | perror("socket"); |
---|
151 | return -1; |
---|
152 | } |
---|
153 | |
---|
154 | if(setsockopt(server->sockfd, SOL_SOCKET, |
---|
155 | SO_REUSEADDR, &yes, sizeof(int)) == -1) |
---|
156 | { |
---|
157 | perror("setsockopt SO_REUSEADDR"); |
---|
158 | return -1; |
---|
159 | } |
---|
160 | |
---|
161 | server->my_addr.sin_family = AF_INET; |
---|
162 | server-> my_addr.sin_port = htons(server->port); |
---|
163 | server->my_addr.sin_addr.s_addr = INADDR_ANY; |
---|
164 | memset(&(server->my_addr.sin_zero), '\0', 8); |
---|
165 | |
---|
166 | if(bind(server->sockfd, (struct sockaddr *)&server->my_addr, |
---|
167 | sizeof(struct sockaddr)) == -1) |
---|
168 | { |
---|
169 | perror("bind"); |
---|
170 | return -1; |
---|
171 | } |
---|
172 | |
---|
173 | /* Non blocking socket */ |
---|
174 | flags = fcntl(server->sockfd, F_GETFL, 0); |
---|
175 | fcntl(server->sockfd, F_SETFL, flags | O_NONBLOCK); |
---|
176 | |
---|
177 | if(listen(server->sockfd, BACKLOG) == -1) |
---|
178 | { |
---|
179 | perror("listen"); |
---|
180 | return -1; |
---|
181 | } |
---|
182 | |
---|
183 | server->qq = NULL; |
---|
184 | server->ex = NULL; |
---|
185 | |
---|
186 | /* Ignore SIGPIPE */ |
---|
187 | server->sigpipe_handler = signal(SIGPIPE, SIG_IGN); |
---|
188 | |
---|
189 | fprintf(stderr, "initialised network, listening on port %i\n", |
---|
190 | server->port); |
---|
191 | |
---|
192 | /* Main loop */ |
---|
193 | for(;;) |
---|
194 | { |
---|
195 | uint8_t *buf = server->input; |
---|
196 | uint32_t width, height; |
---|
197 | unsigned int size; |
---|
198 | |
---|
199 | /* Manage new connections as this function will be called sometimes |
---|
200 | * more often than display */ |
---|
201 | manage_connections(server); |
---|
202 | |
---|
203 | /* Read data from stdin */ |
---|
204 | read(0, buf, 12); |
---|
205 | |
---|
206 | while(buf[0] != 'C' && buf[1] != 'A' && buf[2] != 'C' && buf[3] != 'A') |
---|
207 | { |
---|
208 | memmove(buf, buf + 1, 11); |
---|
209 | read(0, buf + 11, 1); |
---|
210 | } |
---|
211 | |
---|
212 | width = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[5] << 16) |
---|
213 | | ((uint32_t)buf[6] << 8) | (uint32_t)buf[7]; |
---|
214 | height = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16) |
---|
215 | | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11]; |
---|
216 | |
---|
217 | size = 12 + width * height * 8 + 4; |
---|
218 | buf = server->input = realloc(server->input, size); |
---|
219 | read(0, buf + 12, size - 12); |
---|
220 | |
---|
221 | /* Free the previous canvas, if any */ |
---|
222 | if(server->qq) |
---|
223 | cucul_free(server->qq); |
---|
224 | |
---|
225 | server->qq = cucul_load(buf, size); |
---|
226 | |
---|
227 | if(!server->qq) |
---|
228 | continue; /* Load error */ |
---|
229 | |
---|
230 | /* Free the previous export buffer, if any */ |
---|
231 | if(server->ex) |
---|
232 | { |
---|
233 | cucul_free_export(server->ex); |
---|
234 | server->ex = NULL; |
---|
235 | } |
---|
236 | |
---|
237 | /* Get ANSI representation of the image and skip the end-of buffer |
---|
238 | * linefeed ("\r\n\0", 3 bytes) */ |
---|
239 | server->ex = cucul_create_export(server->qq, "ansi"); |
---|
240 | server->ex->size -= 3; |
---|
241 | |
---|
242 | for(i = 0; i < server->client_count; i++) |
---|
243 | { |
---|
244 | if(server->clients[i].fd == -1) |
---|
245 | continue; |
---|
246 | |
---|
247 | if(send_data(server, &server->clients[i])) |
---|
248 | { |
---|
249 | fprintf(stderr, "client %i dropped connection\n", |
---|
250 | server->clients[i].fd); |
---|
251 | close(server->clients[i].fd); |
---|
252 | server->clients[i].fd = -1; |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | /* Kill all remaining clients */ |
---|
258 | for(i = 0; i < server->client_count; i++) |
---|
259 | { |
---|
260 | if(server->clients[i].fd == -1) |
---|
261 | continue; |
---|
262 | |
---|
263 | close(server->clients[i].fd); |
---|
264 | server->clients[i].fd = -1; |
---|
265 | } |
---|
266 | |
---|
267 | if(server->ex) |
---|
268 | cucul_free_export(server->ex); |
---|
269 | |
---|
270 | /* Restore SIGPIPE handler */ |
---|
271 | signal(SIGPIPE, server->sigpipe_handler); |
---|
272 | |
---|
273 | free(server); |
---|
274 | |
---|
275 | #ifdef HAVE_WINDOWS_H |
---|
276 | WSACleanup(); |
---|
277 | #endif |
---|
278 | return 0; |
---|
279 | } |
---|
280 | |
---|
281 | /* |
---|
282 | * XXX: The following functions are local |
---|
283 | */ |
---|
284 | |
---|
285 | static void manage_connections(struct server *server) |
---|
286 | { |
---|
287 | int fd, flags; |
---|
288 | struct sockaddr_in remote_addr; |
---|
289 | socklen_t len = sizeof(struct sockaddr_in); |
---|
290 | |
---|
291 | fd = accept(server->sockfd, (struct sockaddr *)&remote_addr, &len); |
---|
292 | if(fd == -1) |
---|
293 | return; |
---|
294 | |
---|
295 | fprintf(stderr, "client %i connected from %s\n", |
---|
296 | fd, inet_ntoa(remote_addr.sin_addr)); |
---|
297 | |
---|
298 | /* Non blocking socket */ |
---|
299 | flags = fcntl(fd, F_SETFL, 0); |
---|
300 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
---|
301 | |
---|
302 | if(server->clients == NULL) |
---|
303 | { |
---|
304 | server->clients = malloc(sizeof(struct client)); |
---|
305 | if(server->clients == NULL) |
---|
306 | return; |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | server->clients = realloc(server->clients, |
---|
311 | (server->client_count+1) * sizeof(struct client)); |
---|
312 | } |
---|
313 | |
---|
314 | server->clients[server->client_count].fd = fd; |
---|
315 | server->clients[server->client_count].ready = 0; |
---|
316 | server->clients[server->client_count].inbytes = 0; |
---|
317 | server->clients[server->client_count].start = 0; |
---|
318 | server->clients[server->client_count].stop = 0; |
---|
319 | |
---|
320 | /* If we already have data to send, send it to the new client */ |
---|
321 | if(send_data(server, &server->clients[server->client_count])) |
---|
322 | { |
---|
323 | fprintf(stderr, "client %i dropped connection\n", fd); |
---|
324 | close(fd); |
---|
325 | server->clients[server->client_count].fd = -1; |
---|
326 | return; |
---|
327 | } |
---|
328 | |
---|
329 | server->client_count++; |
---|
330 | } |
---|
331 | |
---|
332 | static int send_data(struct server *server, struct client *c) |
---|
333 | { |
---|
334 | ssize_t ret; |
---|
335 | |
---|
336 | /* Not for us */ |
---|
337 | if(c->fd < 0) |
---|
338 | return -1; |
---|
339 | |
---|
340 | /* Listen to incoming data */ |
---|
341 | for(;;) |
---|
342 | { |
---|
343 | ret = read(c->fd, c->inbuf + c->inbytes, 1); |
---|
344 | if(ret <= 0) |
---|
345 | break; |
---|
346 | |
---|
347 | c->inbytes++; |
---|
348 | |
---|
349 | /* Check for telnet sequences */ |
---|
350 | if(c->inbuf[0] == 0xff) |
---|
351 | { |
---|
352 | if(c->inbytes == 1) |
---|
353 | { |
---|
354 | ; |
---|
355 | } |
---|
356 | else if(c->inbuf[1] == 0xfd || c->inbuf[1] == 0xfc) |
---|
357 | { |
---|
358 | if(c->inbytes == 3) |
---|
359 | { |
---|
360 | fprintf(stderr, "client %i said: %.02x %.02x %.02x (%s %s %s)\n", |
---|
361 | c->fd, c->inbuf[0], c->inbuf[1], c->inbuf[2], |
---|
362 | COMMAND_NAME(c->inbuf[0]), COMMAND_NAME(c->inbuf[1]), OPTION_NAME(c->inbuf[2])); |
---|
363 | /* Just ignore, lol */ |
---|
364 | c->inbytes = 0; |
---|
365 | } |
---|
366 | } |
---|
367 | else |
---|
368 | c->inbytes = 0; |
---|
369 | } |
---|
370 | else if(c->inbytes == 1) |
---|
371 | { |
---|
372 | if(c->inbuf[0] == 0x03) |
---|
373 | { |
---|
374 | fprintf(stderr, "client %i pressed C-c\n", c->fd); |
---|
375 | return -1; /* User requested to quit */ |
---|
376 | } |
---|
377 | |
---|
378 | c->inbytes = 0; |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | /* Send the telnet initialisation commands */ |
---|
383 | if(!c->ready) |
---|
384 | { |
---|
385 | ret = nonblock_write(c->fd, server->prefix, sizeof(INIT_PREFIX)); |
---|
386 | if(ret == -1) |
---|
387 | return (errno == EAGAIN) ? 0 : -1; |
---|
388 | |
---|
389 | if(ret < (ssize_t)sizeof(INIT_PREFIX)) |
---|
390 | return 0; |
---|
391 | |
---|
392 | c->ready = 1; |
---|
393 | } |
---|
394 | |
---|
395 | /* No error, there's just nothing to send yet */ |
---|
396 | if(!server->ex) |
---|
397 | return 0; |
---|
398 | |
---|
399 | /* If we have backlog, send the backlog */ |
---|
400 | if(c->stop) |
---|
401 | { |
---|
402 | ret = nonblock_write(c->fd, c->outbuf + c->start, c->stop - c->start); |
---|
403 | |
---|
404 | if(ret == -1) |
---|
405 | { |
---|
406 | if(errno == EAGAIN) |
---|
407 | ret = 0; |
---|
408 | else |
---|
409 | return -1; |
---|
410 | } |
---|
411 | |
---|
412 | if(ret == c->stop - c->start) |
---|
413 | { |
---|
414 | /* We got rid of the backlog! */ |
---|
415 | c->start = c->stop = 0; |
---|
416 | } |
---|
417 | else |
---|
418 | { |
---|
419 | c->start += ret; |
---|
420 | |
---|
421 | if(c->stop - c->start + strlen(ANSI_PREFIX) + server->ex->size |
---|
422 | > OUTBUFFER) |
---|
423 | { |
---|
424 | /* Overflow! Empty buffer and start again */ |
---|
425 | memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET)); |
---|
426 | c->start = 0; |
---|
427 | c->stop = strlen(ANSI_RESET); |
---|
428 | return 0; |
---|
429 | } |
---|
430 | |
---|
431 | /* Need to move? */ |
---|
432 | if(c->stop + strlen(ANSI_PREFIX) + server->ex->size > OUTBUFFER) |
---|
433 | { |
---|
434 | memmove(c->outbuf, c->outbuf + c->start, c->stop - c->start); |
---|
435 | c->stop -= c->start; |
---|
436 | c->start = 0; |
---|
437 | } |
---|
438 | |
---|
439 | memcpy(c->outbuf + c->stop, ANSI_PREFIX, strlen(ANSI_PREFIX)); |
---|
440 | c->stop += strlen(ANSI_PREFIX); |
---|
441 | memcpy(c->outbuf + c->stop, server->ex->buffer, server->ex->size); |
---|
442 | c->stop += server->ex->size; |
---|
443 | |
---|
444 | return 0; |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | /* We no longer have backlog, send our new data */ |
---|
449 | |
---|
450 | /* Send ANSI prefix */ |
---|
451 | ret = nonblock_write(c->fd, ANSI_PREFIX, strlen(ANSI_PREFIX)); |
---|
452 | if(ret == -1) |
---|
453 | { |
---|
454 | if(errno == EAGAIN) |
---|
455 | ret = 0; |
---|
456 | else |
---|
457 | return -1; |
---|
458 | } |
---|
459 | |
---|
460 | if(ret < (ssize_t)strlen(ANSI_PREFIX)) |
---|
461 | { |
---|
462 | if(strlen(ANSI_PREFIX) + server->ex->size > OUTBUFFER) |
---|
463 | { |
---|
464 | /* Overflow! Empty buffer and start again */ |
---|
465 | memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET)); |
---|
466 | c->start = 0; |
---|
467 | c->stop = strlen(ANSI_RESET); |
---|
468 | return 0; |
---|
469 | } |
---|
470 | |
---|
471 | memcpy(c->outbuf, ANSI_PREFIX, strlen(ANSI_PREFIX) - ret); |
---|
472 | c->stop = strlen(ANSI_PREFIX) - ret; |
---|
473 | memcpy(c->outbuf + c->stop, server->ex->buffer, server->ex->size); |
---|
474 | c->stop += server->ex->size; |
---|
475 | |
---|
476 | return 0; |
---|
477 | } |
---|
478 | |
---|
479 | /* Send actual data */ |
---|
480 | ret = nonblock_write(c->fd, server->ex->buffer, server->ex->size); |
---|
481 | if(ret == -1) |
---|
482 | { |
---|
483 | if(errno == EAGAIN) |
---|
484 | ret = 0; |
---|
485 | else |
---|
486 | return -1; |
---|
487 | } |
---|
488 | |
---|
489 | if(ret < (int)server->ex->size) |
---|
490 | { |
---|
491 | if(server->ex->size > OUTBUFFER) |
---|
492 | { |
---|
493 | /* Overflow! Empty buffer and start again */ |
---|
494 | memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET)); |
---|
495 | c->start = 0; |
---|
496 | c->stop = strlen(ANSI_RESET); |
---|
497 | return 0; |
---|
498 | } |
---|
499 | |
---|
500 | memcpy(c->outbuf, server->ex->buffer, server->ex->size - ret); |
---|
501 | c->stop = server->ex->size - ret; |
---|
502 | |
---|
503 | return 0; |
---|
504 | } |
---|
505 | |
---|
506 | return 0; |
---|
507 | } |
---|
508 | |
---|
509 | ssize_t nonblock_write(int fd, void *buf, size_t len) |
---|
510 | { |
---|
511 | size_t total = 0; |
---|
512 | ssize_t ret; |
---|
513 | |
---|
514 | while(total < len) |
---|
515 | { |
---|
516 | do |
---|
517 | { |
---|
518 | ret = write(fd, buf, len); |
---|
519 | } |
---|
520 | while(ret < 0 && errno == EINTR); |
---|
521 | |
---|
522 | if(ret < 0) |
---|
523 | return ret; |
---|
524 | else if(ret == 0) |
---|
525 | return total; |
---|
526 | |
---|
527 | total += len; |
---|
528 | buf = (void *)((uintptr_t)buf + len); |
---|
529 | } |
---|
530 | |
---|
531 | return total; |
---|
532 | } |
---|
533 | |
---|