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

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