updates
[silc.git] / lib / silccore / silccommand.h
1 /*
2  
3   silccommand.h
4  
5   Author: Pekka Riikonen <priikone@silcnet.org>
6  
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8  
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 /****h* silccore/SILC Command Interface
22  *
23  * DESCRIPTION
24  *
25  * Implementation of the Command Payload. The Command Payload is used to
26  * send commands and also command replies usually between client and
27  * server.
28  *
29  ***/
30
31 #ifndef SILCCOMMAND_H
32 #define SILCCOMMAND_H
33
34 /****f* silccore/SilcCommandAPI/SilcCommandCb
35  *
36  * SYNOPSIS
37  *
38  *    typedef void (*SilcCommandCb)(void *context, void *context2);
39  *
40  * DESCRIPTION
41  *
42  *    Command function callback. The actual command function pointer.
43  *    This is generic command callback that the application may choose to
44  *    use with its command routines.  However, none of the generic
45  *    routines depend on this callback so application may freely define
46  *    their own command callback if desired.
47  *
48  ***/
49 typedef void (*SilcCommandCb)(void *context, void *context2);
50
51 /****s* silccore/SilcCommandAPI/SilcCommandPayload
52  *
53  * NAME
54  * 
55  *    typedef struct SilcCommandPayloadStruct *SilcCommandPayload;
56  *
57  * DESCRIPTION
58  *
59  *    This context is the actual Command Payload and is allocated
60  *    by silc_command_payload_parse and given as argument usually to
61  *    all silc_command_payload_* functions.  It is freed by the
62  *    silc_command_payload_free function.
63  *
64  ***/
65 typedef struct SilcCommandPayloadStruct *SilcCommandPayload;
66
67 /****d* silccore/SilcCommandAPI/SilcCommandFlags
68  *
69  * NAME
70  * 
71  *    typedef enum { ... } SilcCommandFlags;
72  *
73  * DESCRIPTION
74  *
75  *    Command flags that set how the commands behave on different
76  *    situations. These can be OR'es together to set multiple flags.
77  *    The application is resoponsible of implementing the behaviour
78  *    of these flags. These are here just to define generic flags.
79  *    The server usually makes use of these flags.
80  *
81  * SOURCE
82  */
83 typedef enum {
84   SILC_CF_NONE           = 0,
85
86   /* Command may only be used once per (about) 2 seconds. Bursts up
87      to 5 commands are allowed though. */
88   SILC_CF_LAG            = (1L << 1),
89
90   /* Command may only be used once per (about) 2 seconds. No bursts
91      are allowed at all. */
92   SILC_CF_LAG_STRICT     = (1L << 2),
93
94   /* Command is available for registered connections (connections
95      whose ID has been created. */
96   SILC_CF_REG            = (1L << 3),
97
98   /* Command is available only for server operators */
99   SILC_CF_OPER           = (1L << 4),
100
101   /* Command is available only for SILC (router) operators. If this 
102      is set SILC_CF_OPER is not necessary to be set. */
103   SILC_CF_SILC_OPER      = (1L << 5),
104
105 } SilcCommandFlag;
106 /***/
107
108 /****d* silccore/SilcCommandAPI/SilcCommand
109  *
110  * NAME
111  * 
112  *    typedef unsigned char SilcCommand;
113  *
114  * DESCRIPTION
115  *
116  *    The SilcCommand type definition and the commands. The commands
117  *    listed here are the official SILC Commands and they have client
118  *    and server counterparts.
119  *
120  * SOURCE
121  */
122 typedef unsigned char SilcCommand;
123
124 /* All SILC commands. These are commands that have client and server
125    counterparts. */
126 #define SILC_COMMAND_NONE               0
127 #define SILC_COMMAND_WHOIS              1
128 #define SILC_COMMAND_WHOWAS             2
129 #define SILC_COMMAND_IDENTIFY           3
130 #define SILC_COMMAND_NICK               4
131 #define SILC_COMMAND_LIST               5
132 #define SILC_COMMAND_TOPIC              6
133 #define SILC_COMMAND_INVITE             7
134 #define SILC_COMMAND_QUIT               8
135 #define SILC_COMMAND_KILL               9
136 #define SILC_COMMAND_INFO               10
137 #define SILC_COMMAND_STATS              11
138 #define SILC_COMMAND_PING               12
139 #define SILC_COMMAND_OPER               13
140 #define SILC_COMMAND_JOIN               14
141 #define SILC_COMMAND_MOTD               15
142 #define SILC_COMMAND_UMODE              16
143 #define SILC_COMMAND_CMODE              17
144 #define SILC_COMMAND_CUMODE             18
145 #define SILC_COMMAND_KICK               19
146 #define SILC_COMMAND_BAN                20
147 #define SILC_COMMAND_DETACH             21
148 #define SILC_COMMAND_SILCOPER           23
149 #define SILC_COMMAND_LEAVE              24
150 #define SILC_COMMAND_USERS              25
151 #define SILC_COMMAND_GETKEY             26
152 #define SILC_COMMAND_SERVICE            27
153
154 /* Private range start */
155 #define SILC_COMMAND_PRIV_CONNECT       200
156 #define SILC_COMMAND_PRIV_CLOSE         201
157 #define SILC_COMMAND_PRIV_SHUTDOWN      202
158
159 /* Reserved */
160 #define SILC_COMMAND_RESERVED           255
161 /***/
162
163 /* Prototypes */
164
165 /****f* silccore/SilcCommandAPI/silc_command_payload_parse
166  *
167  * SYNOPSIS
168  *
169  *    SilcCommandPayload 
170  *    silc_command_payload_parse(const unsigned char *payload,
171  *                               SilcUInt32 payload_len);
172  *
173  * DESCRIPTION
174  *
175  *    Parses command payload returning new command payload structure. The
176  *    `buffer' is the raw payload.
177  *
178  ***/
179 SilcCommandPayload silc_command_payload_parse(const unsigned char *payload,
180                                               SilcUInt32 payload_len);
181
182 /****f* silccore/SilcCommandAPI/silc_command_payload_encode
183  *
184  * SYNOPSIS
185  *
186  *    SilcBuffer silc_command_payload_encode(SilcCommand cmd,
187  *                                           SilcUInt32 argc,
188  *                                           unsigned char **argv,
189  *                                           SilcUInt32 *argv_lens,
190  *                                           SilcUInt32 *argv_types,
191  *                                           SilcUInt16 ident);
192  *
193  * DESCRIPTION
194  *
195  *     Encodes Command Payload returning it to SilcBuffer.
196  *
197  ***/
198 SilcBuffer silc_command_payload_encode(SilcCommand cmd,
199                                        SilcUInt32 argc,
200                                        unsigned char **argv,
201                                        SilcUInt32 *argv_lens,
202                                        SilcUInt32 *argv_types,
203                                        SilcUInt16 ident);
204
205 /****f* silccore/SilcCommandAPI/silc_command_payload_encode_payload
206  *
207  * SYNOPSIS
208  *
209  *    SilcBuffer 
210  *    silc_command_payload_encode_payload(SilcCommandPayload payload);
211  *
212  * DESCRIPTION
213  *
214  *    Same as silc_command_payload_encode but encodes the buffer from
215  *    SilcCommandPayload structure instead of raw data.
216  *
217  ***/
218 SilcBuffer silc_command_payload_encode_payload(SilcCommandPayload payload);
219
220 /****f* silccore/SilcCommandAPI/silc_command_payload_encode_va
221  *
222  * SYNOPSIS
223  *
224  *    SilcBuffer silc_command_payload_encode_va(SilcCommand cmd, 
225  *                                              SilcUInt16 ident, 
226  *                                              SilcUInt32 argc, ...);
227  *
228  * DESCRIPTION
229  *
230  *    Encodes Command payload with variable argument list. The arguments
231  *    must be: SilcUInt32, unsigned char *, unsigned int, ... One 
232  *    {SilcUInt32, unsigned char * and unsigned int} forms one argument, 
233  *    thus `argc' in case when sending one {SilcUInt32, unsigned char * 
234  *    and SilcUInt32} equals one (1) and when sending two of those it
235  *    equals two (2), and so on. This has to be preserved or bad things
236  *    will happen. The variable arguments is: {type, data, data_len}.
237  *
238  ***/
239 SilcBuffer silc_command_payload_encode_va(SilcCommand cmd, 
240                                           SilcUInt16 ident, 
241                                           SilcUInt32 argc, ...);
242
243 /****f* silccore/SilcCommandAPI/silc_command_payload_encode_vap
244  *
245  * SYNOPSIS
246  *
247  *    SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, 
248  *                                               SilcUInt16 ident, 
249  *                                               SilcUInt32 argc, va_list ap);
250  *
251  * DESCRIPTION
252  *
253  *    This is equivalent to the silc_command_payload_encode_va except
254  *    takes the va_list as argument.
255  *
256  ***/
257 SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, 
258                                            SilcUInt16 ident, 
259                                            SilcUInt32 argc, va_list ap);
260
261 /****f* silccore/SilcCommandAPI/silc_command_reply_payload_encode_va
262  *
263  * SYNOPSIS
264  *
265  *    SilcBuffer 
266  *    silc_command_reply_payload_encode_va(SilcCommand cmd, 
267  *                                         SilcStatus status,
268  *                                         SilcStatus error,
269  *                                         SilcUInt16 ident,
270  *                                         SilcUInt32 argc, ...);
271  *
272  * DESCRIPTION
273  *
274  *    Same as silc_command_payload_encode_va except that this is used to 
275  *    encode strictly command reply packets.  The `argc' must not count 
276  *    `status' and `error' as arguments.  The `status' includes the
277  *    command reply status.  If single reply will be sent then it includes
278  *    SILC_STATUS_OK if error did not occur.  It includes an error value
279  *    if error did occur.  In this case `error' field is ignored.  If
280  *    there will be multiple successful command replies then the `status'
281  *    includes a list value and `error' is ignored.  If there will
282  *    multiple error replies the `status' includes a list value, and
283  *    the `error' includes an error value.  Thus, the `error' value is
284  *    specified only if there will be list of errors.
285  *
286  * NOTES
287  *
288  *    Protocol defines that it is possible to send both list of successful
289  *    and list of error replies at the same time, as long as the error
290  *    replies are sent after the successful replies.
291  *
292  ***/
293 SilcBuffer 
294 silc_command_reply_payload_encode_va(SilcCommand cmd, 
295                                      SilcStatus status,
296                                      SilcStatus error,
297                                      SilcUInt16 ident,
298                                      SilcUInt32 argc, ...);
299
300 /****f* silccore/SilcCommandAPI/silc_command_reply_payload_encode_vap
301  *
302  * SYNOPSIS
303  *
304  *    SilcBuffer 
305  *    silc_command_reply_payload_encode_vap(SilcCommand cmd, 
306  *                                          SilcStatus status,
307  *                                          SilcStatus error,
308  *                                          SilcUInt16 ident, SilcUInt32 argc,
309  *                                          va_list ap);
310  *
311  * DESCRIPTION
312  *
313  *    This is equivalent to the silc_command_reply_payload_encode_va except
314  *    takes the va_list as argument.
315  *
316  ***/
317 SilcBuffer 
318 silc_command_reply_payload_encode_vap(SilcCommand cmd, 
319                                       SilcStatus status,
320                                       SilcStatus error,
321                                       SilcUInt16 ident, SilcUInt32 argc, 
322                                       va_list ap);
323
324 /****f* silccore/SilcCommandAPI/silc_command_free
325  *
326  * SYNOPSIS
327  *
328  *    void silc_command_payload_free(SilcCommandPayload payload);
329  *
330  * DESCRIPTION
331  *
332  *    Frees the Command Payload and all data in it.
333  *
334  ***/
335 void silc_command_payload_free(SilcCommandPayload payload);
336
337 /****f* silccore/SilcCommandAPI/silc_command_get
338  *
339  * SYNOPSIS
340  *
341  *    SilcCommand silc_command_get(SilcCommandPayload payload);
342  *
343  * DESCRIPTION
344  *
345  *    Return the command from the payload.
346  *
347  ***/
348 SilcCommand silc_command_get(SilcCommandPayload payload);
349
350 /****f* silccore/SilcCommandAPI/silc_command_get_args
351  *
352  * SYNOPSIS
353  *
354  *    SilcArgumentPayload silc_command_get_args(SilcCommandPayload payload);
355  *
356  * DESCRIPTION
357  *
358  *    Return the Arguments Payload containing the arguments from the
359  *    Command Payload. The caller must not free it.
360  *
361  ***/
362 SilcArgumentPayload silc_command_get_args(SilcCommandPayload payload);
363
364 /****f* silccore/SilcCommandAPI/silc_command_get_ident
365  *
366  * SYNOPSIS
367  *
368  *    SilcUInt16 silc_command_get_ident(SilcCommandPayload payload);
369  *
370  * DESCRIPTION
371  *
372  *    Return the command identifier from the payload. The identifier can
373  *    be used to identify which command reply belongs to which command.
374  *    The client sets the identifier to the payload and server must return
375  *    the same identifier in the command reply.
376  *
377  ***/
378 SilcUInt16 silc_command_get_ident(SilcCommandPayload payload);
379
380 /****f* silccore/SilcCommandAPI/silc_command_get_status
381  *
382  * SYNOPSIS
383  *
384  *    bool silc_command_get_status(SilcCommandPayload payload, 
385  *                                 SilcStatus *status,
386  *                                 SilcStatus *error);
387  *
388  * DESCRIPTION
389  *
390  *    This function returns the command reply status into `status' and
391  *    error status, if error occurred into the `error'.  The function
392  *    returns TRUE if command reply status is not error, and FALSE if
393  *    error occurred.  In this case the `error' will include the actual
394  *    error status.  The `status' can be in this case some list value
395  *    which indicates that there will be list of errors.
396  *
397  ***/
398 bool silc_command_get_status(SilcCommandPayload payload, 
399                              SilcStatus *status,
400                              SilcStatus *error);
401
402 /****f* silccore/SilcCommandAPI/silc_command_set_ident
403  *
404  * SYNOPSIS
405  *
406  *    void silc_command_set_ident(SilcCommandPayload payload, 
407  *                                SilcUInt16 ident);
408  *
409  * DESCRIPTION
410  *
411  *    Function to set identifier to already allocated Command Payload. Command
412  *    payloads are frequentlly resent in SILC and thusly this makes it easy
413  *    to set the identifier without encoding new Command Payload. 
414  *
415  ***/
416 void silc_command_set_ident(SilcCommandPayload payload, SilcUInt16 ident);
417
418 /****f* silccore/SilcCommandAPI/silc_command_set_command
419  *
420  * SYNOPSIS
421  *
422  *    void silc_command_set_command(SilcCommandPayload payload, 
423  *                                  SilcCommand command);
424  *
425  * DESCRIPTION
426  *
427  *    Function to set the command to already allocated Command Payload. This
428  *    makes it easy to change the command in the payload without encoding new
429  *    Command Payload.
430  *
431  ***/
432 void silc_command_set_command(SilcCommandPayload payload, SilcCommand command);
433
434 #endif