updates.
[silc.git] / lib / silccore / silcprotocol.h
1 /*
2
3   silcprotocol.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 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 #ifndef SILCPROTOCOL_H
22 #define SILCPROTOCOL_H
23
24 /* Protocol type definition. */
25 typedef unsigned char SilcProtocolType;
26
27 /* Protocol state definition. */
28 typedef unsigned char SilcProtocolState;
29
30 /* Protocol states. Do NOT change the values of these states, especially
31    the START state or you break every protocol. */
32 #define SILC_PROTOCOL_STATE_UNKNOWN 0
33 #define SILC_PROTOCOL_STATE_START 1
34 #define SILC_PROTOCOL_STATE_END 252
35 #define SILC_PROTOCOL_STATE_FAILURE 253  /* Received failure from remote */
36 #define SILC_PROTOCOL_STATE_ERROR 254    /* Local error at our end */
37
38 /* Type definition for above auth methods */
39 typedef unsigned char SilcProtocolAuthMeth;
40
41 /* 
42    SILC Protocol Object.
43
44    Short description of the field following:
45    
46    SilcProtocolType type
47
48        Protocol type. This is enumeration.
49   
50    SilcProtocolCallback callback;
51
52        Callback function for the protocol. This is SilcTaskCallback function
53        pointer as the protocols in SILC are executed as timeout tasks.
54
55    The object expands to another structure as well. Short description of 
56    these fields following:
57
58    SilcProtocolObject *protocol
59
60        This is the pointer to the protocol object defined above.
61
62    SilcProtocolState state
63
64        Protocol state. This is enumeration. The state of the protocol can
65        be changed in the callback function.
66
67    void *context
68
69        Context to be sent for the callback function. This is usually 
70        object for either SILC client or server. However, this abstraction 
71        makes it possible that this pointer could be some other object as well. 
72
73    SilcProtocolExecute execute;
74
75        Executes the protocol and its states. The correct state must be set
76        before calling this function. The state is usually set in the protocol
77        specific routines.
78
79    SilcProtocolExecute execute_final;
80
81        Executes the final callback function of the protocol. Read on.
82
83    SilcProtocolFinalCallback final_callback;
84
85        This is a callback function that is called with timeout _after_ the
86        protocol has finished or error occurs. If this is NULL, naturally 
87        nothing will be executed. Protocol should call this function only at 
88        SILC_PROTOCOL_STATE_END and SILC_PROTOCOL_STATE_ERROR states.
89
90 */
91 typedef SilcTaskCallback SilcProtocolCallback;
92
93 typedef struct SilcProtocolObjectStruct {
94   SilcProtocolType type;
95   SilcProtocolCallback callback;
96
97   struct SilcProtocolObjectStruct *next;
98 } SilcProtocolObject;
99
100 typedef SilcTaskCallback SilcProtocolFinalCallback;
101 typedef SilcTaskCallback SilcProtocolExecute;
102
103 typedef struct SilcProtocolStruct {
104   SilcProtocolObject *protocol;
105   SilcProtocolState state;
106   void *context;
107
108   //  SilcProtocolExecute execute;
109   void (*execute)(void *, int, void *, int, long, long);
110   SilcProtocolExecute execute_final;
111   SilcProtocolFinalCallback final_callback;
112 } *SilcProtocol;
113
114 /* Prototypes */
115 void silc_protocol_register(SilcProtocolType type,
116                             SilcProtocolCallback callback);
117 void silc_protocol_unregister(SilcProtocolType type,
118                               SilcProtocolCallback callback);
119 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
120                          void *context, SilcProtocolFinalCallback callback);
121 void silc_protocol_free(SilcProtocol protocol);
122 void silc_protocol_execute(void *qptr, int type,
123                            void *context, int fd,
124                            long secs, long usecs);
125 void silc_protocol_execute_final(void *qptr, int type, 
126                                  void *context, int fd);
127 void silc_protocol_cancel(void *qptr, void *context);
128
129 #endif