source: libcaca/trunk/caca/driver_network.c @ 609

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