]> git.tuebingen.mpg.de Git - paraslash.git/blob - send.h
send: Avoid select-specific arguments in {pre,post}_select().
[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 watch its own
84          * file descriptors for reading or writing.
85          *
86          * \sa select(2).
87          */
88         void (*pre_select)(struct sched *s);
89         /**
90          * Handle the file descriptors which are ready for I/O.
91          *
92          * If the pre_select hook asked for one or more file descriptors to be
93          * watched, this is the hook to check the result and perform I/O on the
94          * descriptors which are ready for reading/writing.
95          */
96         void (*post_select)(struct sched *s);
97         /**
98          * Terminate all connected clients.
99          *
100          * This is called e.g. if the stop command was executed. It should make
101          * the clients aware of the end-of-file condition.
102          */
103         void (*shutdown_clients)(void);
104         /** Dellocate all resources. Only called on exit. */
105         void (*shutdown)(void);
106         /**
107          * Array of function pointers for the sender subcommands.
108          *
109          * Each sender may implement any subset of the sender commands by
110          * filling in the appropriate function pointer in the array. A \p NULL
111          * pointer means this command is not implemented by this sender.
112          */
113         int (*client_cmds[NUM_SENDER_CMDS])(struct sender_command_data*);
114         /**
115          * Resolve target-specific URL string
116          *
117          * This method must be defined if the sender supports the add/delete
118          * subcommands. It interprets a string specifying a target URL in a
119          * sender-specific fashion (e.g. embedded FEC string). It can also
120          * fill in sender-specific defaults if necessary.
121          */
122         int (*resolve_target)(const char *, struct sender_command_data *);
123 };
124
125 /** NULL-terminated list, defined in \ref vss.c. */
126 extern const struct sender * const senders[];
127 /** Iterate over all senders. */
128 #define FOR_EACH_SENDER(_i) for ((_i) = 0; senders[(_i)]; (_i)++)
129
130 /** Describes one client, connected to a paraslash sender. */
131 struct sender_client {
132         /** The file descriptor of the client. */
133         int fd;
134         /** The socket "name" of the client. */
135         char *name;
136         /** The position of this client in the client list. */
137         struct list_head node;
138         /** Non-zero if audio file header has been sent. */
139         int header_sent;
140         /** The list of pending chunks for this client. */
141         struct chunk_queue *cq;
142         /** Data specific to the particular sender. */
143         void *private_data;
144 };
145
146 /**
147  * Each paraslash sender may register arbitrary many clients to the virtual
148  * streaming system, possibly with varying fec parameters. In order to do so,
149  * it must allocate a \a fec_client_parms structure and pass it to \ref
150  * vss_add_fec_client.
151  *
152  * Clients are automatically removed from that list by the vss if an error
153  * occurs, or if the sender requests deletion of a client by calling \ref
154  * vss_del_fec_client().
155  */
156
157 /** FEC parameters requested by FEC clients. */
158 struct fec_client_parms {
159         /** Number of data slices plus redundant slices. */
160         uint8_t slices_per_group;
161         /** Number of slices minus number of redundant slices. */
162         uint8_t data_slices_per_group;
163         /** Whether the header must be sent periodically. */
164         bool need_periodic_header;
165         /**
166          * Transport-layer initialisation for FEC support.
167          *
168          * This optional function serves (a) to make the transport layer
169          * ready to send FEC slices and (b) to determine the Maximum
170          * Packet Size (MPS) supported by the connection. The MPS value
171          * determines the largest payload size. This value is used to
172          * send FEC slices that are not larger than the path MTU, to avoid
173          * fragmentation and to maximize packet utilization. The user can
174          * alternatively specify a slice size of up to this value.
175          */
176         int (*init_fec)(struct sender_client *sc);
177         /** Push out FEC-encoded packets */
178         void (*send_fec)(struct sender_client *sc, char *buf, size_t len);
179 };
180
181 /** Describes the current status of one paraslash sender. */
182 struct sender_status {
183         /** Number of sockets to listen on, size of the two arrays below. */
184         unsigned num_listen_fds;
185         /** Derived from --http-listen-address and --dccp-listen-address. */
186         char **listen_addresses;
187         /** Default TCP/DCCP port number for addresses w/o port. */
188         int default_port;
189         /** The socket fd(s) this sender is listening on. */
190         int *listen_fds;
191         /** The current number of simultaneous connections. */
192         int num_clients;
193         /** The maximal number of simultaneous connections. */
194         int max_clients;
195         /** Whether the access control list is a whitelist. */
196         int default_deny;
197         /** The whitelist/blacklist. */
198         struct list_head acl;
199         /** The list of connected clients. */
200         struct list_head client_list;
201 };
202
203 /** Iterate over all listening addresses of the http/dccp sender. */
204 #define FOR_EACH_LISTEN_FD(_n, _ss) for (_n = 0; _n < (_ss)->num_listen_fds; _n++)
205
206 void shutdown_client(struct sender_client *sc, struct sender_status *ss);
207 void shutdown_clients(struct sender_status *ss);
208 void init_sender_status(struct sender_status *ss,
209                 const struct lls_opt_result *acl_opt_result,
210                 const struct lls_opt_result *listen_address_opt_result,
211                 int default_port, int max_clients, int default_deny);
212 void free_sender_status(const struct sender_status *ss);
213 char *generic_sender_status(struct sender_status *ss, const char *name);
214 void generic_com_allow(struct sender_command_data *scd,
215                 struct sender_status *ss);
216 void generic_com_deny(struct sender_command_data *scd,
217                 struct sender_status *ss);
218 void generic_com_on(struct sender_status *ss, unsigned protocol);
219 void generic_acl_deplete(struct list_head *acl);
220 void generic_com_off(struct sender_status *ss);
221 char *generic_sender_help(void);
222 struct sender_client *accept_sender_client(struct sender_status *ss);
223 int send_queued_chunks(int fd, struct chunk_queue *cq);
224 int parse_fec_url(const char *arg, struct sender_command_data *scd);