Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

558 rindas
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. uint8_t *buf = server->input;
  170. uint32_t width, height;
  171. unsigned int size;
  172. /* Manage new connections as this function will be called sometimes
  173. * more often than display */
  174. manage_connections(server);
  175. /* Read data from stdin */
  176. read(0, buf, 16);
  177. while(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  178. {
  179. memmove(buf, buf + 1, 15);
  180. read(0, buf + 15, 1);
  181. }
  182. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  183. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  184. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  185. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  186. size = 16 + width * height * 8;
  187. buf = server->input = realloc(server->input, size);
  188. read(0, buf + 16, size - 16);
  189. /* Free the previous canvas, if any */
  190. if(server->canvas)
  191. cucul_free_canvas(server->canvas);
  192. server->canvas = cucul_import_canvas(buf, size, "caca");
  193. if(!server->canvas)
  194. continue; /* Load error */
  195. /* Free the previous export buffer, if any */
  196. if(server->buffer)
  197. {
  198. cucul_free_buffer(server->buffer);
  199. server->buffer = NULL;
  200. }
  201. /* Get ANSI representation of the image and skip the end-of buffer
  202. * linefeed ("\r\n", 2 bytes) */
  203. server->buffer = cucul_export_canvas(server->canvas, "ansi");
  204. server->bufdata = cucul_get_buffer_data(server->buffer);
  205. server->buflen = cucul_get_buffer_size(server->buffer);
  206. server->buflen -= 2;
  207. for(i = 0; i < server->client_count; i++)
  208. {
  209. if(server->clients[i].fd == -1)
  210. continue;
  211. if(send_data(server, &server->clients[i]))
  212. {
  213. fprintf(stderr, "client %i dropped connection\n",
  214. server->clients[i].fd);
  215. close(server->clients[i].fd);
  216. server->clients[i].fd = -1;
  217. }
  218. }
  219. }
  220. /* Kill all remaining clients */
  221. for(i = 0; i < server->client_count; i++)
  222. {
  223. if(server->clients[i].fd == -1)
  224. continue;
  225. close(server->clients[i].fd);
  226. server->clients[i].fd = -1;
  227. }
  228. if(server->buffer)
  229. cucul_free_buffer(server->buffer);
  230. /* Restore SIGPIPE handler */
  231. signal(SIGPIPE, server->sigpipe_handler);
  232. free(server);
  233. #ifdef HAVE_WINDOWS_H
  234. WSACleanup();
  235. #endif
  236. return 0;
  237. }
  238. /*
  239. * XXX: The following functions are local
  240. */
  241. static void manage_connections(struct server *server)
  242. {
  243. int fd, flags;
  244. struct sockaddr_in remote_addr;
  245. socklen_t len = sizeof(struct sockaddr_in);
  246. fd = accept(server->sockfd, (struct sockaddr *)&remote_addr, &len);
  247. if(fd == -1)
  248. return;
  249. fprintf(stderr, "client %i connected from %s\n",
  250. fd, inet_ntoa(remote_addr.sin_addr));
  251. /* Non blocking socket */
  252. flags = fcntl(fd, F_SETFL, 0);
  253. fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  254. if(server->clients == NULL)
  255. {
  256. server->clients = malloc(sizeof(struct client));
  257. if(server->clients == NULL)
  258. return;
  259. }
  260. else
  261. {
  262. server->clients = realloc(server->clients,
  263. (server->client_count+1) * sizeof(struct client));
  264. }
  265. server->clients[server->client_count].fd = fd;
  266. server->clients[server->client_count].ready = 0;
  267. server->clients[server->client_count].inbytes = 0;
  268. server->clients[server->client_count].start = 0;
  269. server->clients[server->client_count].stop = 0;
  270. /* If we already have data to send, send it to the new client */
  271. if(send_data(server, &server->clients[server->client_count]))
  272. {
  273. fprintf(stderr, "client %i dropped connection\n", fd);
  274. close(fd);
  275. server->clients[server->client_count].fd = -1;
  276. return;
  277. }
  278. server->client_count++;
  279. }
  280. static int send_data(struct server *server, struct client *c)
  281. {
  282. ssize_t ret;
  283. /* Not for us */
  284. if(c->fd < 0)
  285. return -1;
  286. /* Listen to incoming data */
  287. for(;;)
  288. {
  289. ret = read(c->fd, c->inbuf + c->inbytes, 1);
  290. if(ret <= 0)
  291. break;
  292. c->inbytes++;
  293. /* Check for telnet sequences */
  294. if(c->inbuf[0] == 0xff)
  295. {
  296. if(c->inbytes == 1)
  297. {
  298. ;
  299. }
  300. else if(c->inbuf[1] == 0xfd || c->inbuf[1] == 0xfc)
  301. {
  302. if(c->inbytes == 3)
  303. {
  304. fprintf(stderr, "client %i said: %.02x %.02x %.02x (%s %s %s)\n",
  305. c->fd, c->inbuf[0], c->inbuf[1], c->inbuf[2],
  306. COMMAND_NAME(c->inbuf[0]), COMMAND_NAME(c->inbuf[1]), OPTION_NAME(c->inbuf[2]));
  307. /* Just ignore, lol */
  308. c->inbytes = 0;
  309. }
  310. }
  311. else
  312. c->inbytes = 0;
  313. }
  314. else if(c->inbytes == 1)
  315. {
  316. if(c->inbuf[0] == 0x03)
  317. {
  318. fprintf(stderr, "client %i pressed C-c\n", c->fd);
  319. return -1; /* User requested to quit */
  320. }
  321. c->inbytes = 0;
  322. }
  323. }
  324. /* Send the telnet initialisation commands */
  325. if(!c->ready)
  326. {
  327. ret = nonblock_write(c->fd, server->prefix, sizeof(INIT_PREFIX));
  328. if(ret == -1)
  329. return (errno == EAGAIN) ? 0 : -1;
  330. if(ret < (ssize_t)sizeof(INIT_PREFIX))
  331. return 0;
  332. c->ready = 1;
  333. }
  334. /* No error, there's just nothing to send yet */
  335. if(!server->buffer)
  336. return 0;
  337. /* If we have backlog, send the backlog */
  338. if(c->stop)
  339. {
  340. ret = nonblock_write(c->fd, c->outbuf + c->start, c->stop - c->start);
  341. if(ret == -1)
  342. {
  343. if(errno == EAGAIN)
  344. ret = 0;
  345. else
  346. {
  347. fprintf(stderr, "client %i failed (%s)\n",
  348. c->fd, strerror(errno));
  349. return -1;
  350. }
  351. }
  352. if(ret == c->stop - c->start)
  353. {
  354. /* We got rid of the backlog! */
  355. c->start = c->stop = 0;
  356. }
  357. else
  358. {
  359. c->start += ret;
  360. if(c->stop - c->start + strlen(ANSI_PREFIX) + server->buflen
  361. > OUTBUFFER)
  362. {
  363. /* Overflow! Empty buffer and start again */
  364. memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
  365. c->start = 0;
  366. c->stop = strlen(ANSI_RESET);
  367. return 0;
  368. }
  369. /* Need to move? */
  370. if(c->stop + strlen(ANSI_PREFIX) + server->buflen > OUTBUFFER)
  371. {
  372. memmove(c->outbuf, c->outbuf + c->start, c->stop - c->start);
  373. c->stop -= c->start;
  374. c->start = 0;
  375. }
  376. memcpy(c->outbuf + c->stop, ANSI_PREFIX, strlen(ANSI_PREFIX));
  377. c->stop += strlen(ANSI_PREFIX);
  378. memcpy(c->outbuf + c->stop, server->bufdata, server->buflen);
  379. c->stop += server->buflen;
  380. return 0;
  381. }
  382. }
  383. /* We no longer have backlog, send our new data */
  384. /* Send ANSI prefix */
  385. ret = nonblock_write(c->fd, ANSI_PREFIX, strlen(ANSI_PREFIX));
  386. if(ret == -1)
  387. {
  388. if(errno == EAGAIN)
  389. ret = 0;
  390. else
  391. {
  392. fprintf(stderr, "client %i failed (%s)\n", c->fd, strerror(errno));
  393. return -1;
  394. }
  395. }
  396. if(ret < (ssize_t)strlen(ANSI_PREFIX))
  397. {
  398. if(strlen(ANSI_PREFIX) + server->buflen > OUTBUFFER)
  399. {
  400. /* Overflow! Empty buffer and start again */
  401. memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
  402. c->start = 0;
  403. c->stop = strlen(ANSI_RESET);
  404. return 0;
  405. }
  406. memcpy(c->outbuf, ANSI_PREFIX, strlen(ANSI_PREFIX) - ret);
  407. c->stop = strlen(ANSI_PREFIX) - ret;
  408. memcpy(c->outbuf + c->stop, server->bufdata, server->buflen);
  409. c->stop += server->buflen;
  410. return 0;
  411. }
  412. /* Send actual data */
  413. ret = nonblock_write(c->fd, server->bufdata, server->buflen);
  414. if(ret == -1)
  415. {
  416. if(errno == EAGAIN)
  417. ret = 0;
  418. else
  419. {
  420. fprintf(stderr, "client %i failed (%s)\n", c->fd, strerror(errno));
  421. return -1;
  422. }
  423. }
  424. if(ret < (int)server->buflen)
  425. {
  426. if(server->buflen > OUTBUFFER)
  427. {
  428. /* Overflow! Empty buffer and start again */
  429. memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
  430. c->start = 0;
  431. c->stop = strlen(ANSI_RESET);
  432. return 0;
  433. }
  434. memcpy(c->outbuf, server->bufdata, server->buflen - ret);
  435. c->stop = server->buflen - ret;
  436. return 0;
  437. }
  438. return 0;
  439. }
  440. ssize_t nonblock_write(int fd, void *buf, size_t len)
  441. {
  442. size_t total = 0;
  443. ssize_t ret;
  444. while(total < len)
  445. {
  446. do
  447. {
  448. ret = write(fd, buf, len);
  449. }
  450. while(ret < 0 && errno == EINTR);
  451. if(ret < 0)
  452. return ret;
  453. else if(ret == 0)
  454. return total;
  455. total += len;
  456. buf = (void *)((uintptr_t)buf + len);
  457. }
  458. return total;
  459. }