Convert filters to lopsub.
[paraslash.git] / command.h
1 /** \file command.h The structure of server and afs commands. */
2
3 /** Per connection data available to command handlers. */
4 struct command_context {
5         /** Network address of the peer. */
6         const char *peer;
7         /** The paraslash user that executes this command. */
8         struct user *u;
9         /** File descriptor and crypto keys. */
10         struct stream_cipher_context scc;
11 };
12
13 /** Prototype of a server command handler. */
14 typedef int (*server_cmd_handler_t)(struct command_context *,
15                 struct lls_parse_result *);
16
17 /**
18  * The lopsub user data structure for server commands.
19  *
20  * One such structure exists for each server command. Lopsub maintains
21  * references to the user data structures and provides lls_user_data() for
22  * applications to fetch the user data pointer of a given command. This
23  * mechanism is used by the dispatcher of command.c to run the command handler.
24  */
25 struct server_cmd_user_data {
26         /** Pointer to the command handler. */
27         server_cmd_handler_t handler;
28 };
29
30 /** Define the user data structure for one command. */
31 #define EXPORT_SERVER_CMD_HANDLER(_cmd) \
32         const struct server_cmd_user_data lsg_server_cmd_com_ ## _cmd ## _user_data = { \
33                 .handler = com_ ## _cmd \
34         };
35
36 /** Get the lopsub command pointer by command name. */
37 #define SERVER_CMD_CMD_PTR(_cmd) \
38         (lls_cmd(LSG_SERVER_CMD_CMD_ ## _cmd, server_cmd_suite))
39
40 /** Get the lopsub parse result of an option. */
41 #define SERVER_CMD_OPT_RESULT(_cmd, _opt, _lpr) \
42                 (lls_opt_result(LSG_SERVER_CMD_ ## _cmd ## _OPT_ ## _opt, _lpr))
43
44 /** How many times an option was given. */
45 #define SERVER_CMD_OPT_GIVEN(_cmd, _opt, _lpr) \
46         (lls_opt_given(SERVER_CMD_OPT_RESULT(_cmd, _opt, _lpr)))
47
48 /** Fetch the (first) argument given to an option of type uint32. */
49 #define SERVER_CMD_UINT32_VAL(_cmd, _opt, _lpr) \
50         (lls_uint32_val(0, SERVER_CMD_OPT_RESULT(_cmd, _opt, _lpr)))
51
52 int send_sb(struct stream_cipher_context *scc, void *buf, size_t numbytes,
53                 int band, bool dont_free);
54 __printf_3_4 int send_sb_va(struct stream_cipher_context *scc, int band,
55                 const char *fmt, ...);
56 int send_strerror(struct command_context *cc, int err);
57 int send_errctx(struct command_context *cc, char *errctx);
58 int recv_sb(struct stream_cipher_context *scc,
59                 enum sb_designator expected_band,
60                 size_t max_size, struct iovec *result);