Started implementing protocol version 1.1 and narrowing down
[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_PING               12
138 #define SILC_COMMAND_OPER               13
139 #define SILC_COMMAND_JOIN               14
140 #define SILC_COMMAND_MOTD               15
141 #define SILC_COMMAND_UMODE              16
142 #define SILC_COMMAND_CMODE              17
143 #define SILC_COMMAND_CUMODE             18
144 #define SILC_COMMAND_KICK               19
145 #define SILC_COMMAND_BAN                20
146 #define SILC_COMMAND_SILCOPER           23
147 #define SILC_COMMAND_LEAVE              24
148 #define SILC_COMMAND_USERS              25
149 #define SILC_COMMAND_GETKEY             26
150
151 /* Private range start */
152 #define SILC_COMMAND_PRIV_CONNECT       200
153 #define SILC_COMMAND_PRIV_CLOSE         201
154 #define SILC_COMMAND_PRIV_SHUTDOWN      202
155
156 /* Reserved */
157 #define SILC_COMMAND_RESERVED           255
158 /***/
159
160 /****d* silccore/SilcCommandAPI/SilcCommandStatus
161  *
162  * NAME
163  * 
164  *    typedef SilcUInt16 SilcCommandStatus;
165  *
166  * DESCRIPTION
167  *
168  *    The SilcCommandStatus type definition and the status defines.
169  *    The server returns a status in each Command Payload indicating
170  *    the status of the command.
171  *
172  * SOURCE
173  */
174 typedef SilcUInt16 SilcCommandStatus;
175
176 /* Command Status messages */
177 #define SILC_STATUS_OK                      0
178 #define SILC_STATUS_LIST_START              1
179 #define SILC_STATUS_LIST_ITEM               2
180 #define SILC_STATUS_LIST_END                3
181 #define SILC_STATUS_ERR_NO_SUCH_NICK        10
182 #define SILC_STATUS_ERR_NO_SUCH_CHANNEL     11
183 #define SILC_STATUS_ERR_NO_SUCH_SERVER      12
184 #define SILC_STATUS_ERR_TOO_MANY_TARGETS    13
185 #define SILC_STATUS_ERR_NO_RECIPIENT        14
186 #define SILC_STATUS_ERR_UNKNOWN_COMMAND     15
187 #define SILC_STATUS_ERR_WILDCARDS           16
188 #define SILC_STATUS_ERR_NO_CLIENT_ID        17
189 #define SILC_STATUS_ERR_NO_CHANNEL_ID       18
190 #define SILC_STATUS_ERR_NO_SERVER_ID        19
191 #define SILC_STATUS_ERR_BAD_CLIENT_ID       20
192 #define SILC_STATUS_ERR_BAD_CHANNEL_ID      21
193 #define SILC_STATUS_ERR_NO_SUCH_CLIENT_ID   22
194 #define SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID  23
195 #define SILC_STATUS_ERR_NICKNAME_IN_USE     24
196 #define SILC_STATUS_ERR_NOT_ON_CHANNEL      25
197 #define SILC_STATUS_ERR_USER_NOT_ON_CHANNEL 26
198 #define SILC_STATUS_ERR_USER_ON_CHANNEL     27
199 #define SILC_STATUS_ERR_NOT_REGISTERED      28
200 #define SILC_STATUS_ERR_NOT_ENOUGH_PARAMS   29
201 #define SILC_STATUS_ERR_TOO_MANY_PARAMS     30
202 #define SILC_STATUS_ERR_PERM_DENIED         31
203 #define SILC_STATUS_ERR_BANNED_FROM_SERVER  32
204 #define SILC_STATUS_ERR_BAD_PASSWORD        33
205 #define SILC_STATUS_ERR_CHANNEL_IS_FULL     34
206 #define SILC_STATUS_ERR_NOT_INVITED         35
207 #define SILC_STATUS_ERR_BANNED_FROM_CHANNEL 36
208 #define SILC_STATUS_ERR_UNKNOWN_MODE        37
209 #define SILC_STATUS_ERR_NOT_YOU             38
210 #define SILC_STATUS_ERR_NO_CHANNEL_PRIV     39
211 #define SILC_STATUS_ERR_NO_CHANNEL_FOPRIV   40
212 #define SILC_STATUS_ERR_NO_SERVER_PRIV      41
213 #define SILC_STATUS_ERR_NO_ROUTER_PRIV      42
214 #define SILC_STATUS_ERR_BAD_NICKNAME        43
215 #define SILC_STATUS_ERR_BAD_CHANNEL         44
216 #define SILC_STATUS_ERR_AUTH_FAILED         45
217 #define SILC_STATUS_ERR_UNKNOWN_ALGORITHM   46
218 #define SILC_STATUS_ERR_NO_SUCH_SERVER_ID   47
219 /***/
220
221 /* Prototypes */
222
223 /****f* silccore/SilcCommandAPI/silc_command_payload_parse
224  *
225  * SYNOPSIS
226  *
227  *    SilcCommandPayload 
228  *    silc_command_payload_parse(const unsigned char *payload,
229  *                               SilcUInt32 payload_len);
230  *
231  * DESCRIPTION
232  *
233  *    Parses command payload returning new command payload structure. The
234  *    `buffer' is the raw payload.
235  *
236  ***/
237 SilcCommandPayload silc_command_payload_parse(const unsigned char *payload,
238                                               SilcUInt32 payload_len);
239
240 /****f* silccore/SilcCommandAPI/silc_command_payload_encode
241  *
242  * SYNOPSIS
243  *
244  *    SilcBuffer silc_command_payload_encode(SilcCommand cmd,
245  *                                           SilcUInt32 argc,
246  *                                           unsigned char **argv,
247  *                                           SilcUInt32 *argv_lens,
248  *                                           SilcUInt32 *argv_types,
249  *                                           SilcUInt16 ident);
250  *
251  * DESCRIPTION
252  *
253  *     Encodes Command Payload returning it to SilcBuffer.
254  *
255  ***/
256 SilcBuffer silc_command_payload_encode(SilcCommand cmd,
257                                        SilcUInt32 argc,
258                                        unsigned char **argv,
259                                        SilcUInt32 *argv_lens,
260                                        SilcUInt32 *argv_types,
261                                        SilcUInt16 ident);
262
263 /****f* silccore/SilcCommandAPI/silc_command_payload_encode_payload
264  *
265  * SYNOPSIS
266  *
267  *    SilcBuffer 
268  *    silc_command_payload_encode_payload(SilcCommandPayload payload);
269  *
270  * DESCRIPTION
271  *
272  *    Same as silc_command_payload_encode but encodes the buffer from
273  *    SilcCommandPayload structure instead of raw data.
274  *
275  ***/
276 SilcBuffer silc_command_payload_encode_payload(SilcCommandPayload payload);
277
278 /****f* silccore/SilcCommandAPI/silc_command_payload_encode_va
279  *
280  * SYNOPSIS
281  *
282  *    SilcBuffer silc_command_payload_encode_va(SilcCommand cmd, 
283  *                                              SilcUInt16 ident, 
284  *                                              SilcUInt32 argc, ...);
285  *
286  * DESCRIPTION
287  *
288  *    Encodes Command payload with variable argument list. The arguments
289  *    must be: SilcUInt32, unsigned char *, unsigned int, ... One 
290  *    {SilcUInt32, unsigned char * and unsigned int} forms one argument, 
291  *    thus `argc' in case when sending one {SilcUInt32, unsigned char * 
292  *    and SilcUInt32} equals one (1) and when sending two of those it
293  *    equals two (2), and so on. This has to be preserved or bad things
294  *    will happen. The variable arguments is: {type, data, data_len}.
295  *
296  ***/
297 SilcBuffer silc_command_payload_encode_va(SilcCommand cmd, 
298                                           SilcUInt16 ident, 
299                                           SilcUInt32 argc, ...);
300
301 /****f* silccore/SilcCommandAPI/silc_command_payload_encode_vap
302  *
303  * SYNOPSIS
304  *
305  *    SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, 
306  *                                               SilcUInt16 ident, 
307  *                                               SilcUInt32 argc, va_list ap);
308  *
309  * DESCRIPTION
310  *
311  *    This is equivalent to the silc_command_payload_encode_va except
312  *    takes the va_list as argument.
313  *
314  ***/
315 SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, 
316                                            SilcUInt16 ident, 
317                                            SilcUInt32 argc, va_list ap);
318
319 /****f* silccore/SilcCommandAPI/silc_command_reply_payload_encode_va
320  *
321  * SYNOPSIS
322  *
323  *    SilcBuffer 
324  *    silc_command_reply_payload_encode_va(SilcCommand cmd, 
325  *                                         SilcCommandStatus status,
326  *                                         SilcUInt16 ident,
327  *                                         SilcUInt32 argc, ...);
328  *
329  * DESCRIPTION
330  *
331  *    Same as silc_command_payload_encode_va except that this is used to 
332  *    encode strictly command reply packets. The command status message
333  *    to be returned is sent as extra argument to this function. The `argc'
334  *    must not count `status' as on argument.
335  *
336  ***/
337 SilcBuffer 
338 silc_command_reply_payload_encode_va(SilcCommand cmd, 
339                                      SilcCommandStatus status,
340                                      SilcUInt16 ident,
341                                      SilcUInt32 argc, ...);
342
343 /****f* silccore/SilcCommandAPI/silc_command_reply_payload_encode_vap
344  *
345  * SYNOPSIS
346  *
347  *    SilcBuffer 
348  *    silc_command_reply_payload_encode_vap(SilcCommand cmd, 
349  *                                          SilcCommandStatus status,
350  *                                          SilcUInt16 ident, SilcUInt32 argc,
351  *                                          va_list ap);
352  *
353  * DESCRIPTION
354  *
355  *    This is equivalent to the silc_command_reply_payload_encode_va except
356  *    takes the va_list as argument.
357  *
358  ***/
359 SilcBuffer 
360 silc_command_reply_payload_encode_vap(SilcCommand cmd, 
361                                       SilcCommandStatus status,
362                                       SilcUInt16 ident, SilcUInt32 argc, 
363                                       va_list ap);
364
365 /****f* silccore/SilcCommandAPI/silc_command_free
366  *
367  * SYNOPSIS
368  *
369  *    void silc_command_payload_free(SilcCommandPayload payload);
370  *
371  * DESCRIPTION
372  *
373  *    Frees the Command Payload and all data in it.
374  *
375  ***/
376 void silc_command_payload_free(SilcCommandPayload payload);
377
378 /****f* silccore/SilcCommandAPI/silc_command_get
379  *
380  * SYNOPSIS
381  *
382  *    SilcCommand silc_command_get(SilcCommandPayload payload);
383  *
384  * DESCRIPTION
385  *
386  *    Return the command from the payload.
387  *
388  ***/
389 SilcCommand silc_command_get(SilcCommandPayload payload);
390
391 /****f* silccore/SilcCommandAPI/silc_command_get_args
392  *
393  * SYNOPSIS
394  *
395  *    SilcArgumentPayload silc_command_get_args(SilcCommandPayload payload);
396  *
397  * DESCRIPTION
398  *
399  *    Return the Arguments Payload containing the arguments from the
400  *    Command Payload. The caller must not free it.
401  *
402  ***/
403 SilcArgumentPayload silc_command_get_args(SilcCommandPayload payload);
404
405 /****f* silccore/SilcCommandAPI/silc_command_get_ident
406  *
407  * SYNOPSIS
408  *
409  *    SilcUInt16 silc_command_get_ident(SilcCommandPayload payload);
410  *
411  * DESCRIPTION
412  *
413  *    Return the command identifier from the payload. The identifier can
414  *    be used to identify which command reply belongs to which command.
415  *    The client sets the identifier to the payload and server must return
416  *    the same identifier in the command reply.
417  *
418  ***/
419 SilcUInt16 silc_command_get_ident(SilcCommandPayload payload);
420
421 /****f* silccore/SilcCommandAPI/silc_command_get_status
422  *
423  * SYNOPSIS
424  *
425  *    SilcCommandStatus silc_command_get_status(SilcCommandPayload payload);
426  *
427  * DESCRIPTION
428  *
429  *    Returns the SilcCommandStatus from command reply payload's argument 
430  *    payload.  Status can be returned only from command reply payload.
431  *
432  ***/
433 SilcCommandStatus silc_command_get_status(SilcCommandPayload payload);
434
435 /****f* silccore/SilcCommandAPI/silc_command_set_ident
436  *
437  * SYNOPSIS
438  *
439  *    void silc_command_set_ident(SilcCommandPayload payload, 
440  *                                SilcUInt16 ident);
441  *
442  * DESCRIPTION
443  *
444  *    Function to set identifier to already allocated Command Payload. Command
445  *    payloads are frequentlly resent in SILC and thusly this makes it easy
446  *    to set the identifier without encoding new Command Payload. 
447  *
448  ***/
449 void silc_command_set_ident(SilcCommandPayload payload, SilcUInt16 ident);
450
451 /****f* silccore/SilcCommandAPI/silc_command_set_command
452  *
453  * SYNOPSIS
454  *
455  *    void silc_command_set_command(SilcCommandPayload payload, 
456  *                                  SilcCommand command);
457  *
458  * DESCRIPTION
459  *
460  *    Function to set the command to already allocated Command Payload. This
461  *    makes it easy to change the command in the payload without encoding new
462  *    Command Payload.
463  *
464  ***/
465 void silc_command_set_command(SilcCommandPayload payload, SilcCommand command);
466
467 #endif