620ca85b7027ce4fe253f259e7a72e2350f19434
[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 253
35 #define SILC_PROTOCOL_STATE_ERROR 254
36
37 /* Connection Authentication protocols' authentication methods */
38 #define SILC_PROTOCOL_CONN_AUTH_NONE 0
39 #define SILC_PROTOCOL_CONN_AUTH_PASSWORD 1
40 #define SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY 2
41
42 /* Type definition for above auth methods */
43 typedef unsigned char SilcProtocolAuthMeth;
44
45 /* 
46    SILC Protocol Object.
47
48    Short description of the field following:
49    
50    SilcProtocolType type
51
52        Protocol type. This is enumeration.
53   
54    SilcProtocolCallback callback;
55
56        Callback function for the protocol. This is SilcTaskCallback function
57        pointer as the protocols in SILC are executed as timeout tasks.
58
59    The object expands to another structure as well. Short description of 
60    these fields following:
61
62    SilcProtocolObject *protocol
63
64        This is the pointer to the protocol object defined above.
65
66    SilcProtocolState state
67
68        Protocol state. This is enumeration. The state of the protocol can
69        be changed in the callback function.
70
71    void *context
72
73        Context to be sent for the callback function. This is usually 
74        object for either SILC client or server. However, this abstraction 
75        makes it possible that this pointer could be some other object as well. 
76
77    SilcProtocolExecute execute;
78
79        Executes the protocol and its states. The correct state must be set
80        before calling this function. The state is usually set in the protocol
81        specific routines.
82
83    SilcProtocolExecute execute_final;
84
85        Executes the final callback function of the protocol. Read on.
86
87    SilcProtocolFinalCallback final_callback;
88
89        This is a callback function that is called with timeout _after_ the
90        protocol has finished or error occurs. If this is NULL, naturally 
91        nothing will be executed. Protocol should call this function only at 
92        SILC_PROTOCOL_STATE_END and SILC_PROTOCOL_STATE_ERROR states.
93
94 */
95 typedef SilcTaskCallback SilcProtocolCallback;
96
97 typedef struct SilcProtocolObjectStruct {
98   SilcProtocolType type;
99   SilcProtocolCallback callback;
100
101   struct SilcProtocolObjectStruct *next;
102 } SilcProtocolObject;
103
104 typedef SilcTaskCallback SilcProtocolFinalCallback;
105 typedef SilcTaskCallback SilcProtocolExecute;
106
107 typedef struct SilcProtocolStruct {
108   SilcProtocolObject *protocol;
109   SilcProtocolState state;
110   void *context;
111
112   //  SilcProtocolExecute execute;
113   void (*execute)(void *, int, void *, int, long, long);
114   SilcProtocolExecute execute_final;
115   SilcProtocolFinalCallback final_callback;
116 } *SilcProtocol;
117
118 /* Prototypes */
119 void silc_protocol_register(SilcProtocolType type,
120                             SilcProtocolCallback callback);
121 void silc_protocol_unregister(SilcProtocolType type,
122                               SilcProtocolCallback callback);
123 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
124                          void *context, SilcProtocolFinalCallback callback);
125 void silc_protocol_free(SilcProtocol protocol);
126 void silc_protocol_execute(void *qptr, int type,
127                            void *context, int fd,
128                            long secs, long usecs);
129 void silc_protocol_execute_final(void *qptr, int type, 
130                                  void *context, int fd);
131
132 #endif