summaryrefslogtreecommitdiff
path: root/src/module_struct.c
blob: ab1d2f8d2964249cd7a406e41491194b149c57da (plain)
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
/*
 * active port forwarder - software for secure forwarding
 * Copyright (C) 2003-2007 jeremian <jeremian [at] poczta.fm>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <config.h>

#ifdef HAVE_LIBDL

#include "string_functions.h"
#include "module_struct.h"

#include <stdlib.h>
#include <dlfcn.h>
#include <assert.h>

/*
 * Function name: Module_new
 * Description: Create and initialize new Module structure.
 * Returns: Pointer to newly created Module structure.
 */

Module*
Module_new()
{
  Module* tmp = calloc(1, sizeof(Module));
  assert(tmp != NULL);
  if (tmp == NULL) {
    return NULL;
  }
  return tmp;
}

/*
 * Function name: Module_free
 * Description: Free the memory allocated for Module structure.
 * Arguments: m - pointer to pointer to Module structure
 */

void
Module_free(Module** m)
{
  assert(m != NULL);
  if (m == NULL) {
    return;
  }
  assert((*m) != NULL);
  if ((*m) == NULL) {
    return;
  }
  Module_releaseModule(*m);
  if ((*m)->fileName) {
    free((*m)->fileName);
    (*m)->fileName = NULL;
  }
  free((*m));
  (*m) = NULL;
}

/*
 * Function name: Module_set_fileName
 * Description: Set the filename of the module.
 * Arguments: m - pointer to Module structure
 *            fileName - the filename of the module
 */

void
Module_set_fileName(Module* m, char* fileName)
{
  assert(m != NULL);
  if (m == NULL) {
    return;
  }
  string_cp(&(m->fileName), fileName);
}

/*
 * Function name: Module_get_fileName
 * Description: Get the filename of the module.
 * Arguments: m - pointer to Module structure
 * Returns: The filename of the module.
 */

char*
Module_get_fileName(Module* m)
{
  assert(m != NULL);
  if (m == NULL) {
    return NULL;
  }
  return m->fileName;
}

/*
 * Function name: Module_loadModule
 * Description: Load the module into the memory.
 * Arguments: m - pointer to Module structure
 * Returns: 0 - successful/filename not set
 *          1/2 - some errors occured.
 */

int
Module_loadModule(Module* m)
{
  if (Module_get_fileName(m)) {
    m->handle = dlopen(Module_get_fileName(m), RTLD_NOW);
    if (!m->handle) {
      return 1;
    }
    dlerror();
    *(void**) (&m->info) = dlsym(m->handle, "info");
    *(void**) (&m->allow) = dlsym(m->handle, "allow");
    *(void**) (&m->filter) = dlsym(m->handle, "filter");
    if (dlerror() != NULL) {
      return 2;
    }
    m->loaded = 1;
  }	
  return 0;
}

/*
 * Function name: Module_releaseModule
 * Description: Unload the module from the memory.
 * Arguments: m - pointer to Module structure
 * Returns: 0 - successful/module was not loaded
 *          !0 - some errors occured.
 */

int
Module_releaseModule(Module* m)
{
	if (Module_isModuleLoaded(m)) {
		m->loaded = 0;
		m->info = NULL;
		m->allow = NULL;
		m->filter = NULL;
	return dlclose(m->handle);
	}
	return 0;
}

/*
 * Function name: Module_isModuleLoaded
 * Description: Check if the module is loaded into the memory.
 * Arguments: m - pointer to Module structure
 * Returns: 0 - module is not loaded
 *          1 - module is loaded.
 */

int
Module_isModuleLoaded(Module* m)
{
  assert(m != NULL);
  if (m == NULL) {
    return 0;
  }
	return m->loaded;
}

/*
 * Function name: Module_function_info
 * Description: Exec the info function of the module.
 * Arguments: m - pointer to Module structure
 * Returns: Result of the info function of the module.
 */

char*
Module_function_info(Module* m)
{
  if (!Module_isModuleLoaded(m)) {
    return NULL;
  }
  return m->info();
}

/*
 * Function name: Module_function_allow
 * Description: Exec the allow function of the module.
 * Arguments: m - pointer to Module structure
 * Returns: Result of the allow function of the module.
 */

int
Module_function_allow(Module* m, char* host, char* port)
{
  if (!Module_isModuleLoaded(m)) {
    return 0;
  }
  return m->allow(host, port);
}

/*
 * Function name: Module_function_filter
 * Description: Exec the filter function of the module.
 * Arguments: m - pointer to Module structure
 * Returns: Result of the filter function of the module.
 */

int
Module_function_filter(Module* m, char* host, unsigned char* message, int* messageLength)
{
  if (!Module_isModuleLoaded(m)) {
    return 0;
  }
  return m->filter(host, message, messageLength);
}

#endif