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
26 * Revision 1.2 2000/07/05 06:06:35 priikone
27 * Global cosmetic change.
29 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
30 * Imported from internal CVS/Added Log headers.
35 #include "silcincludes.h"
36 #include "silcprotocol.h"
38 /* Allocates a new protocol object. The new allocated and initialized
39 protocol is returned to the new_protocol argument. The argument context
40 is the context to be sent as argument for the protocol. The callback
41 argument is the function to be called _after_ the protocol has finished. */
43 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
44 void *context, SilcProtocolFinalCallback callback)
48 SILC_LOG_DEBUG(("Allocating new protocol type %d", type));
50 for (i = 0; silc_protocol_list[i].callback; i++)
51 if (silc_protocol_list[i].type == type)
54 if (!silc_protocol_list[i].callback) {
55 SILC_LOG_ERROR(("Requested protocol does not exists"));
59 *new_protocol = silc_calloc(1, sizeof(**new_protocol));
60 (*new_protocol)->protocol = (SilcProtocolObject *)&silc_protocol_list[i];
61 (*new_protocol)->state = SILC_PROTOCOL_STATE_UNKNOWN;
62 (*new_protocol)->context = context;
63 (*new_protocol)->execute = silc_protocol_execute;
64 (*new_protocol)->execute_final = silc_protocol_execute_final;
65 (*new_protocol)->final_callback = callback;
68 /* Free's a protocol object. */
70 void silc_protocol_free(SilcProtocol protocol)
76 /* Executes next state of the protocol. The state must be set before
77 calling this function. */
79 void silc_protocol_execute(void *qptr, int type,
80 void *context, int fd,
81 long secs, long usecs)
83 SilcProtocol protocol = (SilcProtocol)context;
85 SILC_LOG_DEBUG(("Start"));
88 silc_task_register(qptr, fd, protocol->protocol->callback, context,
91 SILC_TASK_PRI_NORMAL);
93 protocol->protocol->callback(qptr, 0, context, fd);
96 /* Executes the final callback of the protocol. */
98 void silc_protocol_execute_final(void *qptr, int type,
99 void *context, int fd)
101 SilcProtocol protocol = (SilcProtocol)context;
103 SILC_LOG_DEBUG(("Start, state=%d", protocol->state));
105 protocol->final_callback(qptr, 0, context, fd);