5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
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.
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.
21 * Created: Tue Nov 25 19:25:33 GMT+0200 1997
25 #include "silcincludes.h"
26 #include "silcprotocol.h"
28 /* Dynamically registered protocols */
29 SilcProtocolObject *silc_protocol_list = NULL;
31 /* Dynamically registers new protocol. The protocol is added into protocol
32 list and can be unregistered with silc_protocol_unregister. */
34 void silc_protocol_register(SilcProtocolType type,
35 SilcProtocolCallback callback)
37 SilcProtocolObject *new;
39 new = silc_calloc(1, sizeof(*new));
41 new->callback = callback;
43 if (!silc_protocol_list)
44 silc_protocol_list = new;
46 new->next = silc_protocol_list;
47 silc_protocol_list = new;
51 /* Unregisters protocol. The unregistering is done by both protocol type
52 and the protocol callback. */
54 void silc_protocol_unregister(SilcProtocolType type,
55 SilcProtocolCallback callback)
57 SilcProtocolObject *protocol, *prev;
59 protocol = silc_protocol_list;
61 while (protocol && (protocol->type != type &&
62 protocol->callback != callback)) {
64 protocol = protocol->next;
69 prev->next = protocol->next;
71 silc_protocol_list = protocol->next;
77 /* Allocates a new protocol object. The new allocated and initialized
78 protocol is returned to the new_protocol argument. The argument context
79 is the context to be sent as argument for the protocol. The callback
80 argument is the function to be called _after_ the protocol has finished. */
82 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
83 void *context, SilcProtocolFinalCallback callback)
85 SilcProtocolObject *protocol;
87 SILC_LOG_DEBUG(("Allocating new protocol type %d", type));
89 protocol = silc_protocol_list;
90 while (protocol && protocol->type != type)
91 protocol = protocol->next;
94 SILC_LOG_ERROR(("Requested protocol does not exists"));
99 *new_protocol = silc_calloc(1, sizeof(**new_protocol));
100 (*new_protocol)->protocol = protocol;
101 (*new_protocol)->state = SILC_PROTOCOL_STATE_UNKNOWN;
102 (*new_protocol)->context = context;
103 (*new_protocol)->execute = silc_protocol_execute;
104 (*new_protocol)->execute_final = silc_protocol_execute_final;
105 (*new_protocol)->final_callback = callback;
108 /* Free's a protocol object. */
110 void silc_protocol_free(SilcProtocol protocol)
116 /* Executes next state of the protocol. The state must be set before
117 calling this function. */
119 void silc_protocol_execute(void *qptr, int type,
120 void *context, int fd,
121 long secs, long usecs)
123 SilcProtocol protocol = (SilcProtocol)context;
125 SILC_LOG_DEBUG(("Start"));
128 silc_task_register(qptr, fd, protocol->protocol->callback, context,
131 SILC_TASK_PRI_NORMAL);
133 protocol->protocol->callback(qptr, 0, context, fd);
136 /* Executes the final callback of the protocol. */
138 void silc_protocol_execute_final(void *qptr, int type,
139 void *context, int fd)
141 SilcProtocol protocol = (SilcProtocol)context;
143 SILC_LOG_DEBUG(("Start, state=%d", protocol->state));
145 protocol->final_callback(qptr, 0, context, fd);