You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

561 lines
14 KiB

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