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.
 
 
 
 
 
 

570 lines
14 KiB

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