eth_client.c
author Lluís Batlle <viric@viric.name>
Thu, 20 Mar 2014 16:33:27 +0100
changeset 97 eea77d5a624c
parent 87 be4ee314545c
permissions -rw-r--r--
Merging the savefile branch. I hope it works; I even don't remember it.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53
07500c5c53cb Adding license and web html.
viric@llimona
parents: 43
diff changeset
     1
/*
07500c5c53cb Adding license and web html.
viric@llimona
parents: 43
diff changeset
     2
    stdin mix - a mixer/multiplexer for stdin to processes
07500c5c53cb Adding license and web html.
viric@llimona
parents: 43
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
07500c5c53cb Adding license and web html.
viric@llimona
parents: 43
diff changeset
     4
07500c5c53cb Adding license and web html.
viric@llimona
parents: 43
diff changeset
     5
    Please find the license in the provided COPYING file.
07500c5c53cb Adding license and web html.
viric@llimona
parents: 43
diff changeset
     6
*/
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
     7
#include <unistd.h>
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
     8
#include <sys/types.h>
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
     9
#include <sys/select.h>
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    10
#include "main.h"
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    11
#include "handlers.h"
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    12
#include "filter.h"
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    13
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    14
static int myfd = -1;
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    15
70
51e9b56b487b New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents: 66
diff changeset
    16
extern struct FilterRules *client_recv_fr;
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    17
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    18
void c_eth_init()
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    19
{
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    20
    eth_proto_init();
87
be4ee314545c Making client and server use different ethernet protocols
viric@llimona
parents: 70
diff changeset
    21
    myfd = eth_proto_open(ETH_CLIENT);
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    22
    if (myfd < 0)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    23
        error("Cannot eth_proto_open");
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    24
}
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    25
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    26
void c_eth_shutdown()
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    27
{
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    28
    if (myfd >= 0)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    29
        close(myfd);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    30
}
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    31
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    32
void c_eth_prepare_read_fdset(fd_set *read_set, int *maxfd)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    33
{
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    34
    FD_SET(myfd, read_set);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    35
    *maxfd = max(*maxfd, myfd);
66
b2469563a1dc Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents: 60
diff changeset
    36
    if (!eth_proto_allow_sending())
b2469563a1dc Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents: 60
diff changeset
    37
        avoid_sending(read_set);
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    38
}
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    39
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    40
/* Send -1 on eof */
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    41
int c_eth_process_read_fdset(fd_set *read_set)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    42
{
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    43
    if (FD_ISSET(myfd, read_set))
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    44
    {
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    45
        int res;
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    46
        int olen;
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    47
        res = eth_proto_recv(stream_buffer, stream_buffer_size);
66
b2469563a1dc Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents: 60
diff changeset
    48
        if (res < 0)
b2469563a1dc Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents: 60
diff changeset
    49
            return 1; /* ignore it */
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    50
        if (res == 0) /* EOF */
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    51
        {
70
51e9b56b487b New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents: 66
diff changeset
    52
            filter_flush(client_recv_fr, ostream_buffer, &olen);
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    53
            if (olen > 0)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    54
                send_to_client_stdout(ostream_buffer, olen);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    55
            return -1;
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    56
        }
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    57
        hex_dump("recv_unix_client",stream_buffer, res);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    58
70
51e9b56b487b New client filter in raw mode (-t), where ^] closes the client.
viric@mandarina
parents: 66
diff changeset
    59
        filter_stream(client_recv_fr, ostream_buffer, &olen, stream_buffer,
43
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    60
                res);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    61
        if (olen > 0)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    62
            send_to_client_stdout(ostream_buffer, olen);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    63
    }
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    64
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    65
    return 0;
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    66
}
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    67
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    68
void c_eth_send_to_connected(const char *buffer, size_t size)
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    69
{
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    70
    eth_proto_send(buffer, size);
625794738afc Added first attempt for an ethernet protocol. Even not tried.
viric@llimona
parents:
diff changeset
    71
}