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

Last change on this file since 619 was 619, checked in by Jean-Yves Lamoureux, 14 years ago
  • Added telnet commands and options strings for debug
  • Property svn:keywords set to Id
File size: 14.2 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 619 2006-03-15 15:03:43Z jylam $
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\x1f\xfa____" /* SB NAWS */ \
53    "\xff\xf0"         /* SE */  \
54    "\x1b]2;caca for the network\x07" /* Change window title */ \
55    "\x1b[H\x1b[J" /* Clear screen */
56    /*"\x1b[?25l"*/ /* Hide cursor */
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
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
83struct client
84{
85    int fd;
86    int ready;
87    uint8_t inbuf[INBUFFER];
88    int inbytes;
89    uint8_t outbuf[OUTBUFFER];
90    int start, stop;
91};
92
93struct driver_private
94{
95    unsigned int width, height;
96    unsigned int port;
97    int sockfd;
98    struct sockaddr_in my_addr;
99    socklen_t sin_size;
100
101    char prefix[sizeof(INIT_PREFIX)];
102
103    struct cucul_buffer *ex;
104
105    int client_count;
106    struct client *clients;
107
108    RETSIGTYPE (*sigpipe_handler)(int);
109};
110
111static void manage_connections(caca_t *kk);
112static int send_data(caca_t *kk, struct client *c);
113ssize_t nonblock_write(int fd, void *buf, size_t len);
114
115static int network_init_graphics(caca_t *kk)
116{
117    int yes = 1, flags;
118    int port = 0xCACA; /* 51914 */
119    unsigned int width = 0, height = 0;
120    char *tmp;
121
122    kk->drv.p = malloc(sizeof(struct driver_private));
123    if(kk->drv.p == NULL)
124        return -1;
125
126#if defined(HAVE_GETENV)
127    tmp = getenv("CACA_PORT");
128    if(tmp && *tmp)
129    {
130        int new_port = atoi(tmp);
131        if(new_port)
132            port = new_port;
133    }
134
135    tmp = getenv("CACA_GEOMETRY");
136    if(tmp && *tmp)
137        sscanf(tmp, "%ux%u", &width, &height);
138#endif
139
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
151    kk->drv.p->client_count = 0;
152    kk->drv.p->clients = NULL;
153    kk->drv.p->port = port;
154
155    _cucul_set_size(kk->qq, kk->drv.p->width, kk->drv.p->height);
156
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;
164
165    if ((kk->drv.p->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
166    {
167        perror("socket");
168        return -1;
169    }
170
171    if (setsockopt(kk->drv.p->sockfd, SOL_SOCKET,
172                   SO_REUSEADDR, &yes, sizeof(int)) == -1)
173    {
174        perror("setsockopt SO_REUSEADDR");
175        return -1;
176    }
177
178    kk->drv.p->my_addr.sin_family = AF_INET;
179    kk->drv.p-> my_addr.sin_port = htons(kk->drv.p->port);
180    kk->drv.p->my_addr.sin_addr.s_addr = INADDR_ANY;
181    memset(&(kk->drv.p->my_addr.sin_zero), '\0', 8);
182
183    if (bind(kk->drv.p->sockfd, (struct sockaddr *)&kk->drv.p->my_addr,
184             sizeof(struct sockaddr)) == -1)
185    {
186        perror("bind");
187        return -1;
188    }
189
190    /* Non blocking socket */
191    flags = fcntl(kk->drv.p->sockfd, F_GETFL, 0);
192    fcntl(kk->drv.p->sockfd, F_SETFL, flags | O_NONBLOCK);
193
194    if (listen(kk->drv.p->sockfd, BACKLOG) == -1)
195    {
196        perror("listen");
197        return -1;
198    }
199
200    kk->drv.p->ex = NULL;
201
202    /* Ignore SIGPIPE */
203    kk->drv.p->sigpipe_handler = signal(SIGPIPE, SIG_IGN);
204
205    fprintf(stderr, "initialised network, listening on port %i\n",
206                    kk->drv.p->port);
207
208    return 0;
209}
210
211static int network_end_graphics(caca_t *kk)
212{
213    int i;
214
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;
219    }
220
221    if(kk->drv.p->ex)
222        cucul_free(kk->drv.p->ex);
223
224    /* Restore SIGPIPE handler */
225    signal(SIGPIPE, kk->drv.p->sigpipe_handler);
226
227    free(kk->drv.p);
228
229    return 0;
230}
231
232static int network_set_window_title(caca_t *kk, char const *title)
233{
234    /* Not handled (yet) */
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{
250    int i;
251
252    /* Free the previous export buffer, if any */
253    if(kk->drv.p->ex)
254    {
255        cucul_free(kk->drv.p->ex);
256        kk->drv.p->ex = NULL;
257    }
258
259    /* Get ANSI representation of the image and skip the end-of buffer
260     * linefeed ("\r\n\0", 3 bytes) */
261    kk->drv.p->ex = cucul_export(kk->qq, CUCUL_FORMAT_ANSI);
262    kk->drv.p->ex->size -= 3;
263
264    for(i = 0; i < kk->drv.p->client_count; i++)
265    {
266        if(kk->drv.p->clients[i].fd == -1)
267            continue;
268
269        if(send_data(kk, &kk->drv.p->clients[i]))
270        {
271            fprintf(stderr, "client %i dropped connection\n",
272                            kk->drv.p->clients[i].fd);
273            close(kk->drv.p->clients[i].fd);
274            kk->drv.p->clients[i].fd = -1;
275        }
276    }
277
278    manage_connections(kk);
279}
280
281static void network_handle_resize(caca_t *kk)
282{
283    /* Not handled */
284}
285
286static unsigned int network_get_event(caca_t *kk)
287{
288    /* Manage new connections as this function will be called sometimes
289     * more often than display */
290    manage_connections(kk);
291
292    /* Event not handled */
293    return 0;
294}
295
296/*
297 * XXX: The following functions are local
298 */
299
300static void manage_connections(caca_t *kk)
301{
302    int fd, flags;
303    struct sockaddr_in remote_addr;
304    socklen_t len = sizeof(struct sockaddr_in);
305
306    fd = accept(kk->drv.p->sockfd, (struct sockaddr *)&remote_addr, &len);
307    if(fd == -1)
308        return;
309
310    fprintf(stderr, "client %i connected from %s\n",
311                    fd, inet_ntoa(remote_addr.sin_addr));
312
313    /* Non blocking socket */
314    flags = fcntl(fd, F_SETFL, 0);
315    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
316
317    if(kk->drv.p->clients == NULL)
318    {
319        kk->drv.p->clients = malloc(sizeof(struct client));
320        if(kk->drv.p->clients == NULL)
321            return;
322    }
323    else
324    {
325        kk->drv.p->clients = realloc(kk->drv.p->clients,
326                                     (kk->drv.p->client_count+1) * sizeof(struct client));
327    }
328
329    kk->drv.p->clients[kk->drv.p->client_count].fd = fd;
330    kk->drv.p->clients[kk->drv.p->client_count].ready = 0;
331    kk->drv.p->clients[kk->drv.p->client_count].inbytes = 0;
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 */
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    }
343
344    kk->drv.p->client_count++;
345}
346
347static int send_data(caca_t *kk, struct client *c)
348{
349    ssize_t ret;
350
351    /* Not for us */
352    if(c->fd < 0)
353        return -1;
354
355    /* Listen to incoming data */
356    for(;;)
357    {
358        ret = read(c->fd, c->inbuf + c->inbytes, 1);
359        if(ret <= 0)
360            break;
361
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                {
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],
377                            COMMAND_NAME(c->inbuf[0]), COMMAND_NAME(c->inbuf[1]), OPTION_NAME(c->inbuf[2]));
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        }
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
410    /* No error, there's just nothing to send yet */
411    if(!kk->drv.p->ex)
412        return 0;
413
414    /* If we have backlog, send the backlog */
415    if(c->stop)
416    {
417        ret = nonblock_write(c->fd, c->outbuf + c->start, c->stop - c->start);
418
419        if(ret == -1)
420        {
421            if(errno == EAGAIN)
422                ret = 0;
423            else
424                return -1;
425        }
426
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;
435
436            if(c->stop - c->start + strlen(ANSI_PREFIX) + kk->drv.p->ex->size
437                > OUTBUFFER)
438            {
439                /* Overflow! Empty buffer and start again */
440                memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
441                c->start = 0;
442                c->stop = strlen(ANSI_RESET);
443                return 0;
444            }
445
446            /* Need to move? */
447            if(c->stop + strlen(ANSI_PREFIX) + kk->drv.p->ex->size > OUTBUFFER)
448            {
449                memmove(c->outbuf, c->outbuf + c->start, c->stop - c->start);
450                c->stop -= c->start;
451                c->start = 0;
452            }
453
454            memcpy(c->outbuf + c->stop, ANSI_PREFIX, strlen(ANSI_PREFIX));
455            c->stop += strlen(ANSI_PREFIX);
456            memcpy(c->outbuf + c->stop, kk->drv.p->ex->buffer, kk->drv.p->ex->size);
457            c->stop += kk->drv.p->ex->size;
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));
467    if(ret == -1)
468    {
469        if(errno == EAGAIN)
470            ret = 0;
471        else
472            return -1;
473    }
474
475    if(ret < (ssize_t)strlen(ANSI_PREFIX))
476    {
477        if(strlen(ANSI_PREFIX) + kk->drv.p->ex->size > OUTBUFFER)
478        {
479            /* Overflow! Empty buffer and start again */
480            memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
481            c->start = 0;
482            c->stop = strlen(ANSI_RESET);
483            return 0;
484        }
485
486        memcpy(c->outbuf, ANSI_PREFIX, strlen(ANSI_PREFIX) - ret);
487        c->stop = strlen(ANSI_PREFIX) - ret;
488        memcpy(c->outbuf + c->stop, kk->drv.p->ex->buffer, kk->drv.p->ex->size);
489        c->stop += kk->drv.p->ex->size;
490
491        return 0;
492    }
493
494    /* Send actual data */
495    ret = nonblock_write(c->fd, kk->drv.p->ex->buffer, kk->drv.p->ex->size);
496    if(ret == -1)
497    {
498        if(errno == EAGAIN)
499            ret = 0;
500        else
501            return -1;
502    }
503
504    if(ret < (int)kk->drv.p->ex->size)
505    {
506        if(kk->drv.p->ex->size > OUTBUFFER)
507        {
508            /* Overflow! Empty buffer and start again */
509            memcpy(c->outbuf, ANSI_RESET, strlen(ANSI_RESET));
510            c->start = 0;
511            c->stop = strlen(ANSI_RESET);
512            return 0;
513        }
514
515        memcpy(c->outbuf, kk->drv.p->ex->buffer, kk->drv.p->ex->size - ret);
516        c->stop = kk->drv.p->ex->size - ret;
517
518        return 0;
519    }
520
521    return 0;
522}
523
524ssize_t nonblock_write(int fd, void *buf, size_t len)
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        }
535        while(ret < 0 && errno == EINTR);
536
537        if(ret < 0)
538            return ret;
539        else if(ret == 0)
540            return total;
541
542        total += len;
543        buf = (void *)((uintptr_t)buf + len);
544    }
545
546    return total;
547}
548
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
567
568#endif /* USE_NETWORK */
569
Note: See TracBrowser for help on using the repository browser.