Initial revision
[silc.git] / lib / silcsim / silcsim.c
1 /*
2
3   silcsim.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 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   These routines implement the SILC Module (SIM) support. SIM's are
22   dynamically run-time loaded shared objects that can implement some
23   routines SILC can use to extend its features. Currently all
24   SILC Crypto modules are implemented as SIM's. They all implement
25   the SILC Crypto API and can be loaded run-time into the SILC and
26   used when needed. 
27
28   Basically any SILC API can be implemented as SIM, however, currently
29   only SILC Crypto modules (ciphers and hash functions (PKCS are not
30   supported)) are supported.
31
32   This implementation expects that no SILC specific symbols needs to
33   be exported into the SIM's. This means that SIM's cannot directly
34   use SILC specific symbols or definitions. This feature can be
35   supported with SIM's but currently (with Crypto modules) this is
36   not needed. 
37
38   NOTE: These routines maybe highly system dependant thus I can expect 
39   some heavy #ifdef's here. However, I'm more happy to see some macro 
40   SIM API and restrict the #ifdef's to silcsim.h file.
41
42 */
43
44 #include "silcincludes.h"
45
46 #ifdef SILC_SIM                 /* SIM upport enabled */
47
48 /* Allocates new SIM context. This is later send to all SIM 
49    routines. */
50
51 SilcSimContext *silc_sim_alloc()
52 {
53   SilcSimContext *new;
54
55   SILC_LOG_DEBUG(("Initializing new SIM context"));
56
57   new = silc_calloc(1, sizeof(*new));
58   if (!new) {
59     SILC_LOG_ERROR(("Could not allocate new SIM context"));
60     return NULL;
61   }
62
63   new->handle = NULL;
64   new->type = SILC_SIM_NONE;
65   new->libname = NULL;
66   new->flags = SILC_SIM_FLAGS;
67
68   return new;
69 }
70
71 /* Free's SIM context. SIM must be closed with silc_sim_close before
72    calling this. */
73
74 void silc_sim_free(SilcSimContext *sim)
75 {
76   assert(sim->handle == NULL);
77
78   if (sim)
79     silc_free(sim);
80 }
81
82 /* Loads SIM into the SILC system. */
83
84 int silc_sim_load(SilcSimContext *sim)
85 {
86   assert(sim != NULL);
87
88   SILC_LOG_DEBUG(("Loading SIM '%s'", sim->libname));
89
90   /* Load the library */
91   sim->handle = dlopen(sim->libname, sim->flags);
92   if (!sim->handle) {
93     SILC_LOG_ERROR(("Error loading SIM: %s", silc_sim_error()));
94     return FALSE;
95   }
96
97   return TRUE;
98 }
99
100 /* Closes SIM. This is called when execution of program is ending or
101    one explicitly wants to remove this SIM from SILC. */
102
103 int silc_sim_close(SilcSimContext *sim)
104 {
105   assert(sim != NULL);
106
107   SILC_LOG_DEBUG(("Closing SIM '%s'", sim->libname));
108
109   /* Close the library */
110   dlclose(sim->handle);
111
112   return TRUE;
113 }
114
115 /* Returns error string if error has occured while processing SIM's. */
116
117 char *silc_sim_error()
118 {
119   return dlerror();
120 }
121
122 /* Returns opaque pointer for a symbol in SIM. Caller must know the
123    symbols they want to get from SIM and use the returned pointer to
124    what ever it is intended. */
125
126 void *silc_sim_getsym(SilcSimContext *sim, const char *symbol)
127 {
128   assert(sim != NULL);
129
130   SILC_LOG_DEBUG(("Getting symbol '%s' from SIM", symbol));
131
132   return dlsym(sim->handle, symbol);
133 }
134
135 #endif /* SILC_SIM */