/
bang-module.c
230 lines (193 loc) · 5.37 KB
/
bang-module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* \file bang-module.c
* \author Nikhil Bysani
* \date December 21, 2008
*
* \brief A interface to load modules into the program.
*/
#include"bang-module.h"
#include"bang-module-api.h"
#include"bang-signals.h"
#include"bang-types.h"
#include"bang-types.h"
#include<dlfcn.h>
#include<openssl/sha.h>
#include<openssl/evp.h>
#include<stdlib.h>
#include<string.h>
/**
* \page BANG Modules
*
* A bang module should be a C share object file. Currently it must have
* the following symbol names defined:
* -BANG_module_name: the name of the module
* -BANG_module_version: version of the module
* -BANG_module_init: initialize the module
* -BANG_module_run: runs the module
*
* How should the GUI interact with the module? Using their owns symbols in the module
* such as "GUI_init".
*/
#define BDEBUG_1
/**
* \param path Valid path to a file.
*
* \brief Finds the sha1 hash of the file
*/
static unsigned char* module_hash(char *path) {
FILE *fd = fopen(path,"r");
if (fd == NULL) return NULL;
char buf[UPDATE_SIZE];
int read = UPDATE_SIZE;
unsigned char *md = (unsigned char*) calloc(SHA_DIGEST_LENGTH,sizeof(unsigned char));
static EVP_MD_CTX *ctx = NULL;
ctx = (ctx == NULL) ? EVP_MD_CTX_create() : ctx;
EVP_MD_CTX_init(ctx);
///TODO: Error checking!
EVP_DigestInit_ex(ctx,EVP_sha1(),NULL);
/*
* We don't really need to do this.
* It'd be pretty interesting if we found a shared library bigger than memory.)
*/
while (read < UPDATE_SIZE) {
read = fread(buf,sizeof(char),UPDATE_SIZE,fd);
EVP_DigestUpdate(ctx,buf,read);
}
EVP_DigestFinal_ex(ctx,md,NULL);
/* EVP_MD_CTX_destroy(ctx); */
return md;
}
/**
* \return An api.
*/
static BANG_api get_BANG_api() {
static int completed = 0;
static BANG_api api;
if (!completed) {
/*If we forget to do something in this function, this will force
* the program to segfault when a module calls a function
* we were susposed to give them. */
memset(&api,0,sizeof(BANG_api));
api.BANG_debug_on_all_peers = &BANG_debug_on_all_peers;
api.BANG_get_me_peers = &BANG_get_me_peers;
api.BANG_number_of_active_peers = &BANG_number_of_active_peers;
api.BANG_get_my_id = &BANG_get_my_id;
api.BANG_assert_authority = &BANG_assert_authority;
api.BANG_request_job = &BANG_request_job;
api.BANG_finished_request = &BANG_finished_request;
api.BANG_send_job = &BANG_send_job;
}
completed = 1;
return api;
}
BANG_module* BANG_load_module(char *path) {
/* Get rid of any lingering errors. */
/* while (dlerror() != NULL) {}
* Never mind, this apprently causes segfaults
*/
void *handle = dlopen(path,RTLD_NOW);
BANG_sigargs args;
args.args = dlerror();
/* Make sure the dlsym worked. */
if (args.args != NULL) {
args.length = strlen(args.args);
BANG_send_signal(BANG_MODULE_ERROR,&args,1);
#ifdef BDEBUG_1
fprintf(stderr,"Could not find the module.\n");
fprintf(stderr,args.args);
fprintf(stderr,"\n");
#endif
free(args.args);
return NULL;
}
BANG_module *module = (BANG_module*) calloc(1,sizeof(BANG_module));
module->module_name = dlsym(handle,"BANG_module_name");
/* Make sure the dlsym worked. */
if ((args.args = dlerror()) != NULL) {
dlclose(handle);
free(module);
args.length = strlen(args.args);
BANG_send_signal(BANG_MODULE_ERROR,&args,1);
free(args.args);
#ifdef BDEBUG_1
fprintf(stderr,"Could not find the module name.\n");
#endif
return NULL;
}
module->module_version = dlsym(handle,"BANG_module_version");
/* Make sure the dlsym worked. */
if ((args.args = dlerror()) != NULL) {
dlclose(handle);
free(module);
args.length = strlen(args.args);
BANG_send_signal(BANG_MODULE_ERROR,&args,1);
free(args.args);
#ifdef BDEBUG_1
fprintf(stderr,"Could not find the module version.\n");
#endif
return NULL;
}
module->module_init = dlsym(handle,"BANG_module_init");
/* Make sure the dlsym worked. */
if ((args.args = dlerror()) != NULL) {
dlclose(handle);
free(module);
args.length = strlen(args.args);
BANG_send_signal(BANG_MODULE_ERROR,&args,1);
free(args.args);
#ifdef BDEBUG_1
fprintf(stderr,"Could not find the module init function.\n");
#endif
return NULL;
}
module->module_run = dlsym(handle,"BANG_module_run");
/* Make sure the dlsym worked. */
if ((args.args = dlerror()) != NULL) {
dlclose(handle);
free(module);
args.length = strlen(args.args);
BANG_send_signal(BANG_MODULE_ERROR,&args,1);
free(args.args);
#ifdef BDEBUG_1
fprintf(stderr,"Could not find the module run function.\n");
#endif
return NULL;
}
module->md = module_hash(path);
/*
* The real question is, should I do a hash of the module handle?
* Then we need to know how long the module handle is.
*/
module->handle = handle;
module->path = path;
module->module_init(get_BANG_api());
return module;
}
void BANG_unload_module(BANG_module *module) {
if (module != NULL) {
dlclose(module->handle);
free(module->md);
free(module);
}
}
void BANG_run_module(BANG_module *module) {
if (module != NULL) {
BANG_sigargs args;
args.args = module;
args.length = sizeof(BANG_module);
BANG_send_signal(BANG_RUNNING_MODULE,&args,1);
module->module_run();
}
}
void* BANG_get_symbol(BANG_module *module, char *symbol) {
if (module != NULL) {
/* Get rid of any lingering errors. */
while (dlerror() != NULL) {}
void* sym_to_get = dlsym(module->handle,symbol);
if (dlerror() == NULL)
return sym_to_get;
else
return NULL;
} else
return NULL;
}