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

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