build: Add message to target maintainer-clean.
[paraslash.git] / send.h
1 /*
2  * Copyright (C) 2005-2014 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file send.h Sender-related defines and structures. */
8
9 /** The sender subcommands. */
10 enum sender_subcommand {
11         SENDER_ADD, /**< Add a target (udp only). */
12         SENDER_DELETE, /**< Delete a target (udp only). */
13         SENDER_ALLOW, /**< Allow connections from given IP address(es). */
14         SENDER_DENY, /**< Deny connections from given IP address(es). */
15         SENDER_ON, /**< Activate the sender. */
16         SENDER_OFF, /**< Deactivate the sender. */
17         NUM_SENDER_CMDS /**< Used as array size in struct \ref sender. */
18 };
19
20 /**
21  * Describes one supported sender of para_server.
22  *
23  * \sa http_send.c udp_send.c, dccp_send.c.
24  */
25 struct sender {
26         /** The name of the sender. */
27         const char *name;
28         /**
29          * The init function of this sender.
30          *
31          * It must fill in all function pointers of \a s as well as the \a
32          * client_cmds array, see below. It should also do all necessary
33          * preparations to init this sending facility, for example it could
34          * open a tcp port.
35          */
36         void (*init)(struct sender *s);
37         /**
38          * Return the help text of this sender.
39          *
40          * The result must be dynamically allocated and is freed by the caller.
41          */
42         char* (*help)(void);
43         /**
44          * Return current status info about this sender.
45          *
46          * The result must be dynamically allocated and is freed by the caller.
47          */
48         char* (*status)(void);
49         /**
50          * The send-hook.
51          *
52          * It gets called whenever para_server is playing and the current
53          * audio format handler indicates that another chunk of data should
54          * be sent now. The two parameters \a current_chunk and \a chunks_sent
55          * only differ if the stream was repositioned by the \a ff or \a jmp
56          * command. Of course, \a buf is a pointer to the chunk of data which
57          * should be sent, and \a len is the length of this buffer.
58          */
59         void (*send)(long unsigned current_chunk, long unsigned chunks_sent,
60                 const char *buf, size_t len, const char *header_buf,
61                 size_t header_len);
62         /**
63          * Add file descriptors to fd_sets.
64          *
65          * The pre_select function of each supported sender is called just before
66          * para_server enters its main select loop. Each sender may add its own
67          * file descriptors to the \a rfds or the \a wfds set.
68          *
69          * If a file descriptor was added, \a max_fileno must be increased by
70          * this function, if necessary.
71          *
72          * \sa select(2).
73          */
74         void (*pre_select)(int *max_fileno, fd_set *rfds, fd_set *wfds);
75         /**
76          * Handle the file descriptors which are ready for I/O.
77          *
78          * If the pre_select hook added one ore more file descriptors to the
79          * read or write set, this is the hook to check the result and do any
80          * I/O on those descriptors which are ready for reading/writing.
81          */
82         void (*post_select)(fd_set *rfds, fd_set *wfds);
83         /**
84          * Terminate all connected clients.
85          *
86          * This is called e.g. if the stop command was executed. It should make
87          * the clients aware of the end-of-file condition.
88          */
89         void (*shutdown_clients)(void);
90         /**
91          * Array of function pointers for the sender subcommands.
92          *
93          * Each sender may implement any subset of the sender commands by
94          * filling in the appropriate function pointer in the array. A \p NULL
95          * pointer means this command is not implemented by this sender.
96          */
97         int (*client_cmds[NUM_SENDER_CMDS])(struct sender_command_data*);
98         /**
99          * Resolve target-specific URL string
100          *
101          * This method must be defined if the sender supports the add/delete
102          * subcommands. It interprets a string specifying a target URL in a
103          * sender-specific fashion (e.g. embedded FEC string). It can also
104          * fill in sender-specific defaults if necessary.
105          */
106         int (*resolve_target)(const char *, struct sender_command_data *);
107 };
108
109 /** Describes one client, connected to a paraslash sender. */
110 struct sender_client {
111         /** The file descriptor of the client. */
112         int fd;
113         /** The socket "name" of the client. */
114         char *name;
115         /** The position of this client in the client list. */
116         struct list_head node;
117         /** Non-zero if audio file header has been sent. */
118         int header_sent;
119         /** The list of pending chunks for this client. */
120         struct chunk_queue *cq;
121         /** Data specific to the particular sender. */
122         void *private_data;
123 };
124
125 /**
126  * Each paraslash sender may register arbitrary many clients to the virtual
127  * streaming system, possibly with varying fec parameters. In order to do so,
128  * it must allocate a \a fec_client_parms structure and pass it to \ref
129  * vss_add_fec_client.
130  *
131  * Clients are automatically removed from that list by the vss if an error
132  * occurs, or if the sender requests deletion of a client by calling \ref
133  * vss_del_fec_client().
134  */
135
136 /** FEC parameters requested by FEC clients. */
137 struct fec_client_parms {
138         /** Number of data slices plus redundant slices. */
139         uint8_t slices_per_group;
140         /** Number of slices minus number of redundant slices. */
141         uint8_t data_slices_per_group;
142         /** Whether the header must be sent periodically. */
143         bool need_periodic_header;
144         /**
145          * Transport-layer initialisation for FEC support.
146          *
147          * This optional function serves (a) to make the transport layer
148          * ready to send FEC slices and (b) to determine the Maximum
149          * Packet Size (MPS) supported by the connection. The MPS value
150          * determines the largest payload size. This value is used to
151          * send FEC slices that are not larger than the path MTU, to avoid
152          * fragmentation and to maximize packet utilization. The user can
153          * alternatively specify a slice size of up to this value.
154          */
155         int (*init_fec)(struct sender_client *sc);
156         /** Push out FEC-encoded packets */
157         void (*send_fec)(struct sender_client *sc, char *buf, size_t len);
158 };
159
160 /** Describes the current status of one paraslash sender. */
161 struct sender_status {
162         /** The file descriptor of the socket this sender is listening on. */
163         int listen_fd;
164         /** The TCP/DCCP port used by this sender. */
165         int port;
166         /** The current number of simultaneous connections. */
167         int num_clients;
168         /** The maximal number of simultaneous connections. */
169         int max_clients;
170         /** Whether the access control list is a whitelist. */
171         int default_deny;
172         /** The whitelist/blacklist. */
173         struct list_head acl;
174         /** The list of connected clients. */
175         struct list_head client_list;
176 };
177
178 void shutdown_client(struct sender_client *sc, struct sender_status *ss);
179 void shutdown_clients(struct sender_status *ss);
180 void init_sender_status(struct sender_status *ss, char **access_arg, int num_access_args,
181         int port, int max_clients, int default_deny);
182 char *generic_sender_status(struct sender_status *ss, const char *name);
183
184 void generic_com_allow(struct sender_command_data *scd,
185                 struct sender_status *ss);
186 void generic_com_deny(struct sender_command_data *scd,
187                 struct sender_status *ss);
188 int generic_com_on(struct sender_status *ss, unsigned protocol);
189 void generic_com_off(struct sender_status *ss);
190 char *generic_sender_help(void);
191 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds);
192 int send_queued_chunks(int fd, struct chunk_queue *cq);
193 int parse_fec_url(const char *arg, struct sender_command_data *scd);