Rework score formula.
[paraslash.git] / send.h
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file send.h Sender-related defines and structures. */
4
5 /**
6  * A little preprocessor fu helps to create the sender_subcommand enumeration
7  * below and the list of sender name strings without duplicating the commands.
8  */
9 #define SENDER_SUBCOMMANDS \
10         SENDER_SUBCOMMAND(add) /**< Add a target (udp only). */ \
11         SENDER_SUBCOMMAND(delete) /**< Delete a target (udp only). */ \
12         SENDER_SUBCOMMAND(allow) /**< Allow connections from given IP address(es). */ \
13         SENDER_SUBCOMMAND(deny) /**< Deny connections from given IP address(es). */ \
14         SENDER_SUBCOMMAND(on) /**< Activate the sender. */ \
15         SENDER_SUBCOMMAND(off) /**< Deactivate the sender. */ \
16
17 /** Concatenate "SENDER_" and the given arg and append a comma. */
18 #define SENDER_SUBCOMMAND(_name) SENDER_ ## _name,
19
20 /**
21  * Each sender subcommand gets an SENDER_xxx identifier. The identifier is
22  * passed from the sender command handler to the server process via shared
23  * memory.
24  */
25 enum sender_subcommand {
26         SENDER_SUBCOMMANDS /**< List of SENDER_xxx identifiers. */
27         NUM_SENDER_CMDS /**< Used as array size in struct \ref sender. */
28 };
29
30 #undef SENDER_SUBCOMMAND
31
32 /**
33  * Redefine it to expand to the stringified name of the sender so that
34  * SENDER_SUBCOMMANDS above now expands to the comma-separated list of sender
35  * name strings. This is used in command.c to define and initialize an array of
36  * char pointers.
37  */
38 #define SENDER_SUBCOMMAND(_name) #_name,
39
40 /**
41  * Describes one supported sender of para_server.
42  *
43  * \sa \ref http_send.c \ref udp_send.c, \ref dccp_send.c.
44  */
45 struct sender {
46         /** The name of the sender. */
47         const char *name;
48         /**
49          * Parse the command line options and initialize this sender (e.g.,
50          * initialize target or access control lists, listen on a network
51          * socket, etc.).
52          */
53         void (*init)(void);
54         /**
55          * Return the help text of this sender.
56          *
57          * The result must be dynamically allocated and is freed by the caller.
58          */
59         char* (*help)(void);
60         /**
61          * Return current status info about this sender.
62          *
63          * The result must be dynamically allocated and is freed by the caller.
64          */
65         char* (*status)(void);
66         /**
67          * The send-hook.
68          *
69          * It gets called whenever para_server is playing and the current
70          * audio format handler indicates that another chunk of data should
71          * be sent now. The two parameters \a current_chunk and \a chunks_sent
72          * only differ if the stream was repositioned by the \a ff or \a jmp
73          * command. Of course, \a buf is a pointer to the chunk of data which
74          * should be sent, and \a len is the length of this buffer.
75          */
76         void (*send)(long unsigned current_chunk, long unsigned chunks_sent,
77                 const char *buf, size_t len, const char *header_buf,
78                 size_t header_len);
79         /**
80          * Add file descriptors to fd_sets.
81          *
82          * The pre_select function of each supported sender is called just before
83          * para_server enters its main select loop. Each sender may add its own
84          * file descriptors to the \a rfds or the \a wfds set.
85          *
86          * If a file descriptor was added, \a max_fileno must be increased by
87          * this function, if necessary.
88          *
89          * \sa select(2).
90          */
91         void (*pre_select)(int *max_fileno, fd_set *rfds, fd_set *wfds);
92         /**
93          * Handle the file descriptors which are ready for I/O.
94          *
95          * If the pre_select hook added one ore more file descriptors to the
96          * read or write set, this is the hook to check the result and do any
97          * I/O on those descriptors which are ready for reading/writing.
98          */
99         void (*post_select)(fd_set *rfds, fd_set *wfds);
100         /**
101          * Terminate all connected clients.
102          *
103          * This is called e.g. if the stop command was executed. It should make
104          * the clients aware of the end-of-file condition.
105          */
106         void (*shutdown_clients)(void);
107         /** Dellocate all resources. Only called on exit. */
108         void (*shutdown)(void);
109         /**
110          * Array of function pointers for the sender subcommands.
111          *
112          * Each sender may implement any subset of the sender commands by
113          * filling in the appropriate function pointer in the array. A \p NULL
114          * pointer means this command is not implemented by this sender.
115          */
116         int (*client_cmds[NUM_SENDER_CMDS])(struct sender_command_data*);
117         /**
118          * Resolve target-specific URL string
119          *
120          * This method must be defined if the sender supports the add/delete
121          * subcommands. It interprets a string specifying a target URL in a
122          * sender-specific fashion (e.g. embedded FEC string). It can also
123          * fill in sender-specific defaults if necessary.
124          */
125         int (*resolve_target)(const char *, struct sender_command_data *);
126 };
127
128 /** NULL-terminated list, defined in \ref vss.c. */
129 extern const struct sender * const senders[];
130 /** Iterate over all senders. */
131 #define FOR_EACH_SENDER(_i) for ((_i) = 0; senders[(_i)]; (_i)++)
132
133 /** Describes one client, connected to a paraslash sender. */
134 struct sender_client {
135         /** The file descriptor of the client. */
136         int fd;
137         /** The socket "name" of the client. */
138         char *name;
139         /** The position of this client in the client list. */
140         struct list_head node;
141         /** Non-zero if audio file header has been sent. */
142         int header_sent;
143         /** The list of pending chunks for this client. */
144         struct chunk_queue *cq;
145         /** Data specific to the particular sender. */
146         void *private_data;
147 };
148
149 /**
150  * Each paraslash sender may register arbitrary many clients to the virtual
151  * streaming system, possibly with varying fec parameters. In order to do so,
152  * it must allocate a \a fec_client_parms structure and pass it to \ref
153  * vss_add_fec_client.
154  *
155  * Clients are automatically removed from that list by the vss if an error
156  * occurs, or if the sender requests deletion of a client by calling \ref
157  * vss_del_fec_client().
158  */
159
160 /** FEC parameters requested by FEC clients. */
161 struct fec_client_parms {
162         /** Number of data slices plus redundant slices. */
163         uint8_t slices_per_group;
164         /** Number of slices minus number of redundant slices. */
165         uint8_t data_slices_per_group;
166         /** Whether the header must be sent periodically. */
167         bool need_periodic_header;
168         /**
169          * Transport-layer initialisation for FEC support.
170          *
171          * This optional function serves (a) to make the transport layer
172          * ready to send FEC slices and (b) to determine the Maximum
173          * Packet Size (MPS) supported by the connection. The MPS value
174          * determines the largest payload size. This value is used to
175          * send FEC slices that are not larger than the path MTU, to avoid
176          * fragmentation and to maximize packet utilization. The user can
177          * alternatively specify a slice size of up to this value.
178          */
179         int (*init_fec)(struct sender_client *sc);
180         /** Push out FEC-encoded packets */
181         void (*send_fec)(struct sender_client *sc, char *buf, size_t len);
182 };
183
184 /** Describes the current status of one paraslash sender. */
185 struct sender_status {
186         /** Number of sockets to listen on, size of the two arrays below. */
187         unsigned num_listen_fds;
188         /** Derived from --http-listen-address and --dccp-listen-address. */
189         char **listen_addresses;
190         /** Default TCP/DCCP port number for addresses w/o port. */
191         int default_port;
192         /** The socket fd(s) this sender is listening on. */
193         int *listen_fds;
194         /** The current number of simultaneous connections. */
195         int num_clients;
196         /** The maximal number of simultaneous connections. */
197         int max_clients;
198         /** Whether the access control list is a whitelist. */
199         int default_deny;
200         /** The whitelist/blacklist. */
201         struct list_head acl;
202         /** The list of connected clients. */
203         struct list_head client_list;
204 };
205
206 /** Iterate over all listening addresses of the http/dccp sender. */
207 #define FOR_EACH_LISTEN_FD(_n, _ss) for (_n = 0; _n < (_ss)->num_listen_fds; _n++)
208
209 void shutdown_client(struct sender_client *sc, struct sender_status *ss);
210 void shutdown_clients(struct sender_status *ss);
211 void init_sender_status(struct sender_status *ss,
212                 const struct lls_opt_result *acl_opt_result,
213                 const struct lls_opt_result *listen_address_opt_result,
214                 int default_port, int max_clients, int default_deny);
215 void free_sender_status(const struct sender_status *ss);
216 char *generic_sender_status(struct sender_status *ss, const char *name);
217 void generic_com_allow(struct sender_command_data *scd,
218                 struct sender_status *ss);
219 void generic_com_deny(struct sender_command_data *scd,
220                 struct sender_status *ss);
221 void generic_com_on(struct sender_status *ss, unsigned protocol);
222 void generic_acl_deplete(struct list_head *acl);
223 void generic_com_off(struct sender_status *ss);
224 char *generic_sender_help(void);
225 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds);
226 int send_queued_chunks(int fd, struct chunk_queue *cq);
227 int parse_fec_url(const char *arg, struct sender_command_data *scd);