Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

554 строки
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\xfe\x1f" /* DON'T NAWS */ \
  46. "\xff\x1f\xfa____" /* Set size, replaced in display */ \
  47. "\xff\xf0" \
  48. "\x1b]2;caca for the network\x07" /* Change window title */ \
  49. "\x1b[H\x1b[J" /* Clear screen */ \
  50. /*"\x1b[?25l"*/ /* Hide cursor */ \
  51. #define ANSI_PREFIX \
  52. "\x1b[1;1H" /* move(0,0) */ \
  53. "\x1b[1;1H" /* move(0,0) again */
  54. #define ANSI_RESET \
  55. " " /* Garbage */ \
  56. "\x1b[?1049h" /* Clear screen */ \
  57. "\x1b[?1049h" /* Clear screen again */
  58. struct client
  59. {
  60. int fd;
  61. int ready;
  62. uint8_t inbuf[INBUFFER];
  63. int inbytes;
  64. uint8_t outbuf[OUTBUFFER];
  65. int start, stop;
  66. };
  67. struct driver_private
  68. {
  69. unsigned int width, height;
  70. unsigned int port;
  71. int sockfd;
  72. struct sockaddr_in my_addr;
  73. socklen_t sin_size;
  74. char prefix[sizeof(INIT_PREFIX)];
  75. struct cucul_buffer *ex;
  76. int client_count;
  77. struct client *clients;
  78. RETSIGTYPE (*sigpipe_handler)(int);
  79. };
  80. static void manage_connections(caca_t *kk);
  81. static int send_data(caca_t *kk, struct client *c);
  82. ssize_t nonblock_write(int fd, void *buf, size_t len);
  83. static int network_init_graphics(caca_t *kk)
  84. {
  85. int yes = 1, flags;
  86. int port = 0xCACA; /* 51914 */
  87. unsigned int width = 0, height = 0;
  88. char *tmp;
  89. kk->drv.p = malloc(sizeof(struct driver_private));
  90. if(kk->drv.p == NULL)
  91. return -1;
  92. #if defined(HAVE_GETENV)
  93. tmp = getenv("CACA_PORT");
  94. if(tmp && *tmp)
  95. {
  96. int new_port = atoi(tmp);
  97. if(new_port)
  98. port = new_port;
  99. }
  100. tmp = getenv("CACA_GEOMETRY");
  101. if(tmp && *tmp)
  102. sscanf(tmp, "%ux%u", &width, &height);
  103. #endif
  104. if(width && height)
  105. {
  106. kk->drv.p->width = width;
  107. kk->drv.p->height = height;
  108. }
  109. else
  110. {
  111. kk->drv.p->width = 80;
  112. kk->drv.p->height = 24;
  113. }
  114. kk->drv.p->client_count = 0;
  115. kk->drv.p->clients = NULL;
  116. kk->drv.p->port = port;
  117. _cucul_set_size(kk->qq, kk->drv.p->width, kk->drv.p->height);
  118. /* FIXME, handle >255 sizes */
  119. memcpy(kk->drv.p->prefix, INIT_PREFIX, sizeof(INIT_PREFIX));
  120. tmp = strstr(kk->drv.p->prefix, "____");
  121. tmp[0] = (unsigned char) (kk->drv.p->width & 0xff00) >> 8;
  122. tmp[1] = (unsigned char) kk->drv.p->width & 0xff;
  123. tmp[2] = (unsigned char) (kk->drv.p->height & 0xff00) >> 8;
  124. tmp[3] = (unsigned char) kk->drv.p->height & 0xff;
  125. if ((kk->drv.p->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
  126. {
  127. perror("socket");
  128. return -1;
  129. }
  130. if (setsockopt(kk->drv.p->sockfd, SOL_SOCKET,
  131. SO_REUSEADDR, &yes, sizeof(int)) == -1)
  132. {
  133. perror("setsockopt SO_REUSEADDR");
  134. return -1;
  135. }
  136. kk->drv.p->my_addr.sin_family = AF_INET;
  137. kk->drv.p-> my_addr.sin_port = htons(kk->drv.p->port);
  138. kk->drv.p->my_addr.sin_addr.s_addr = INADDR_ANY;
  139. memset(&(kk->drv.p->my_addr.sin_zero), '\0', 8);
  140. if (bind(kk->drv.p->sockfd, (struct sockaddr *)&kk->drv.p->my_addr,
  141. sizeof(struct sockaddr)) == -1)
  142. {
  143. perror("bind");
  144. return -1;
  145. }
  146. /* Non blocking socket */
  147. flags = fcntl(kk->drv.p->sockfd, F_GETFL, 0);
  148. fcntl(kk->drv.p->sockfd, F_SETFL, flags | O_NONBLOCK);
  149. if (listen(kk->drv.p->sockfd, BACKLOG) == -1)
  150. {
  151. perror("listen");
  152. return -1;
  153. }
  154. kk->drv.p->ex = NULL;
  155. /* Ignore SIGPIPE */
  156. kk->drv.p->sigpipe_handler = signal(SIGPIPE, SIG_IGN);
  157. fprintf(stderr, "initialised network, listening on port %i\n",
  158. kk->drv.p->port);
  159. return 0;
  160. }
  161. static int network_end_graphics(caca_t *kk)
  162. {
  163. int i;
  164. for(i = 0; i < kk->drv.p->client_count; i++)
  165. {
  166. close(kk->drv.p->clients[i].fd);
  167. kk->drv.p->clients[i].fd = -1;
  168. }
  169. if(kk->drv.p->ex)
  170. cucul_free(kk->drv.p->ex);
  171. /* Restore SIGPIPE handler */
  172. signal(SIGPIPE, kk->drv.p->sigpipe_handler);
  173. free(kk->drv.p);
  174. return 0;
  175. }
  176. static int network_set_window_title(caca_t *kk, char const *title)
  177. {
  178. /* Not handled (yet) */
  179. return 0;
  180. }
  181. static unsigned int network_get_window_width(caca_t *kk)
  182. {
  183. return kk->drv.p->width * 6;
  184. }
  185. static unsigned int network_get_window_height(caca_t *kk)
  186. {
  187. return kk->drv.p->height * 10;
  188. }
  189. static void network_display(caca_t *kk)
  190. {
  191. int i;
  192. /* Free the previous export buffer, if any */
  193. if(kk->drv.p->ex)
  194. {
  195. cucul_free(kk->drv.p->ex);
  196. kk->drv.p->ex = NULL;
  197. }
  198. /* Get ANSI representation of the image and skip the end-of buffer
  199. * linefeed ("\r\n\0", 3 bytes) */
  200. kk->drv.p->ex = cucul_export(kk->qq, CUCUL_FORMAT_ANSI);
  201. kk->drv.p->ex->size -= 3;
  202. for(i = 0; i < kk->drv.p->client_count; i++)
  203. {
  204. if(kk->drv.p->clients[i].fd == -1)
  205. continue;
  206. if(send_data(kk, &kk->drv.p->clients[i]))
  207. {
  208. fprintf(stderr, "client %i dropped connection\n",
  209. kk->drv.p->clients[i].fd);
  210. close(kk->drv.p->clients[i].fd);
  211. kk->drv.p->clients[i].fd = -1;
  212. }
  213. }
  214. manage_connections(kk);
  215. }
  216. static void network_handle_resize(caca_t *kk)
  217. {
  218. /* Not handled */
  219. }
  220. static unsigned int network_get_event(caca_t *kk)
  221. {
  222. /* Manage new connections as this function will be called sometimes
  223. * more often than display */
  224. manage_connections(kk);
  225. /* Event not handled */
  226. return 0;
  227. }
  228. /*
  229. * XXX: The following functions are local
  230. */
  231. static void manage_connections(caca_t *kk)
  232. {
  233. int fd, flags;
  234. struct sockaddr_in remote_addr;
  235. socklen_t len = sizeof(struct sockaddr_in);
  236. fd = accept(kk->drv.p->sockfd, (struct sockaddr *)&remote_addr, &len);
  237. if(fd == -1)
  238. return;
  239. fprintf(stderr, "client %i connected from %s\n",
  240. fd, inet_ntoa(remote_addr.sin_addr));
  241. /* Non blocking socket */
  242. flags = fcntl(fd, F_SETFL, 0);
  243. fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  244. if(kk->drv.p->clients == NULL)
  245. {
  246. kk->drv.p->clients = malloc(sizeof(struct client));
  247. if(kk->drv.p->clients == NULL)
  248. return;
  249. }
  250. else
  251. {
  252. kk->drv.p->clients = realloc(kk->drv.p->clients,
  253. (kk->drv.p->client_count+1) * sizeof(struct client));
  254. }
  255. kk->drv.p->clients[kk->drv.p->client_count].fd = fd;
  256. kk->drv.p->clients[kk->drv.p->client_count].ready = 0;
  257. kk->drv.p->clients[kk->drv.p->client_count].inbytes = 0;
  258. kk->drv.p->clients[kk->drv.p->client_count].start = 0;
  259. kk->drv.p->clients[kk->drv.p->client_count].stop = 0;
  260. /* If we already have data to send, send it to the new client */
  261. if(send_data(kk, &kk->drv.p->clients[kk->drv.p->client_count]))
  262. {
  263. fprintf(stderr, "client %i dropped connection\n", fd);
  264. close(fd);
  265. kk->drv.p->clients[kk->drv.p->client_count].fd = -1;
  266. return;
  267. }
  268. kk->drv.p->client_count++;
  269. }
  270. static int send_data(caca_t *kk, struct client *c)
  271. {
  272. ssize_t ret;
  273. /* Not for us */
  274. if(c->fd < 0)
  275. return -1;
  276. /* Listen to incoming data */
  277. for(;;)
  278. {
  279. ret = read(c->fd, c->inbuf + c->inbytes, 1);
  280. if(ret <= 0)
  281. break;
  282. c->inbytes++;
  283. /* Check for telnet sequences */
  284. if(c->inbuf[0] == 0xff)
  285. {
  286. if(c->inbytes == 1)
  287. {
  288. ;
  289. }
  290. else if(c->inbuf[1] == 0xfd || c->inbuf[1] == 0xfc)
  291. {
  292. if(c->inbytes == 3)
  293. {
  294. fprintf(stderr, "client %i said: %.02x %.02x %.02x\n",
  295. c->fd, c->inbuf[0], c->inbuf[1], c->inbuf[2]);
  296. /* Just ignore, lol */
  297. c->inbytes = 0;
  298. }
  299. }
  300. else
  301. c->inbytes = 0;
  302. }
  303. else if(c->inbytes == 1)
  304. {
  305. if(c->inbuf[0] == 0x03)
  306. {
  307. fprintf(stderr, "client %i pressed C-c\n", c->fd);
  308. return -1; /* User requested to quit */
  309. }
  310. c->inbytes = 0;
  311. }
  312. }
  313. /* Send the telnet initialisation commands */
  314. if(!c->ready)
  315. {
  316. ret = nonblock_write(c->fd, kk->drv.p->prefix, sizeof(INIT_PREFIX));
  317. if(ret == -1)
  318. return (errno == EAGAIN) ? 0 : -1;
  319. if(ret < (ssize_t)sizeof(INIT_PREFIX))
  320. return 0;
  321. c->ready = 1;
  322. }
  323. /* No error, there's just nothing to send yet */
  324. if(!kk->drv.p->ex)
  325. return 0;
  326. /* If we have backlog, send the backlog */
  327. if(c->stop)
  328. {
  329. ret = nonblock_write(c->fd, c->outbuf + c->start, c->stop - c->start);
  330. if(ret == -1)
  331. {
  332. if(errno == EAGAIN)
  333. ret = 0;
  334. else
  335. return -1;
  336. }
  337. if(ret == c->stop - c->start)
  338. {
  339. /* We got rid of the backlog! */
  340. c->start = c->stop = 0;
  341. }
  342. else
  343. {
  344. c->start += ret;
  345. if(c->stop - c->start + strlen(ANSI_PREFIX) + kk->drv.p->ex->size
  346. > OUTBUFFER)
  347. {
  348. /* Overflow! Empty buffer and start again */
  349. memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
  350. c->start = 0;
  351. c->stop = strlen(ANSI_RESET);
  352. return 0;
  353. }
  354. /* Need to move? */
  355. if(c->stop + strlen(ANSI_PREFIX) + kk->drv.p->ex->size > OUTBUFFER)
  356. {
  357. memmove(c->outbuf, c->outbuf + c->start, c->stop - c->start);
  358. c->stop -= c->start;
  359. c->start = 0;
  360. }
  361. memcpy(c->outbuf + c->stop, ANSI_PREFIX, strlen(ANSI_PREFIX));
  362. c->stop += strlen(ANSI_PREFIX);
  363. memcpy(c->outbuf + c->stop, kk->drv.p->ex->buffer, kk->drv.p->ex->size);
  364. c->stop += kk->drv.p->ex->size;
  365. return 0;
  366. }
  367. }
  368. /* We no longer have backlog, send our new data */
  369. /* Send ANSI prefix */
  370. ret = nonblock_write(c->fd, ANSI_PREFIX, strlen(ANSI_PREFIX));
  371. if(ret == -1)
  372. {
  373. if(errno == EAGAIN)
  374. ret = 0;
  375. else
  376. return -1;
  377. }
  378. if(ret < (ssize_t)strlen(ANSI_PREFIX))
  379. {
  380. if(strlen(ANSI_PREFIX) + kk->drv.p->ex->size > OUTBUFFER)
  381. {
  382. /* Overflow! Empty buffer and start again */
  383. memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
  384. c->start = 0;
  385. c->stop = strlen(ANSI_RESET);
  386. return 0;
  387. }
  388. memcpy(c->outbuf, ANSI_PREFIX, strlen(ANSI_PREFIX) - ret);
  389. c->stop = strlen(ANSI_PREFIX) - ret;
  390. memcpy(c->outbuf + c->stop, kk->drv.p->ex->buffer, kk->drv.p->ex->size);
  391. c->stop += kk->drv.p->ex->size;
  392. return 0;
  393. }
  394. /* Send actual data */
  395. ret = nonblock_write(c->fd, kk->drv.p->ex->buffer, kk->drv.p->ex->size);
  396. if(ret == -1)
  397. {
  398. if(errno == EAGAIN)
  399. ret = 0;
  400. else
  401. return -1;
  402. }
  403. if(ret < (int)kk->drv.p->ex->size)
  404. {
  405. if(kk->drv.p->ex->size > OUTBUFFER)
  406. {
  407. /* Overflow! Empty buffer and start again */
  408. memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
  409. c->start = 0;
  410. c->stop = strlen(ANSI_RESET);
  411. return 0;
  412. }
  413. memcpy(c->outbuf, kk->drv.p->ex->buffer, kk->drv.p->ex->size - ret);
  414. c->stop = kk->drv.p->ex->size - ret;
  415. return 0;
  416. }
  417. return 0;
  418. }
  419. ssize_t nonblock_write(int fd, void *buf, size_t len)
  420. {
  421. size_t total = 0;
  422. ssize_t ret;
  423. while(total < len)
  424. {
  425. do
  426. {
  427. ret = write(fd, buf, len);
  428. }
  429. while(ret < 0 && errno == EINTR);
  430. if(ret < 0)
  431. return ret;
  432. else if(ret == 0)
  433. return total;
  434. total += len;
  435. buf = (void *)((uintptr_t)buf + len);
  436. }
  437. return total;
  438. }
  439. /*
  440. * Driver initialisation
  441. */
  442. void network_init_driver(caca_t *kk)
  443. {
  444. kk->drv.driver = CACA_DRIVER_NETWORK;
  445. kk->drv.init_graphics = network_init_graphics;
  446. kk->drv.end_graphics = network_end_graphics;
  447. kk->drv.set_window_title = network_set_window_title;
  448. kk->drv.get_window_width = network_get_window_width;
  449. kk->drv.get_window_height = network_get_window_height;
  450. kk->drv.display = network_display;
  451. kk->drv.handle_resize = network_handle_resize;
  452. kk->drv.get_event = network_get_event;
  453. }
  454. #endif /* USE_NETWORK */