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