1 |
/* |
2 |
* Copyright (C) 2002 Erik Fears |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License |
6 |
* as published by the Free Software Foundation; either version 2 |
7 |
* of the License, or (at your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
* GNU General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to |
16 |
* |
17 |
* The Free Software Foundation, Inc. |
18 |
* 59 Temple Place - Suite 330 |
19 |
* Boston, MA 02111-1307, USA. |
20 |
* |
21 |
* |
22 |
*/ |
23 |
|
24 |
#include "setup.h" |
25 |
|
26 |
#include "malloc.h" |
27 |
#include "config.h" |
28 |
#include "inet.h" |
29 |
#include "opm_error.h" |
30 |
#include "opm_types.h" |
31 |
#include "opm_common.h" |
32 |
#include "list.h" |
33 |
|
34 |
#include <string.h> |
35 |
|
36 |
|
37 |
static OPM_CONFIG_HASH_T HASH[] = { |
38 |
{OPM_CONFIG_FD_LIMIT, OPM_TYPE_INT}, |
39 |
{OPM_CONFIG_BIND_IP , OPM_TYPE_ADDRESS}, |
40 |
{OPM_CONFIG_DNSBL_HOST, OPM_TYPE_STRING}, |
41 |
{OPM_CONFIG_TARGET_STRING, OPM_TYPE_STRINGLIST}, |
42 |
{OPM_CONFIG_SCAN_IP, OPM_TYPE_STRING}, |
43 |
{OPM_CONFIG_SCAN_PORT, OPM_TYPE_INT}, |
44 |
{OPM_CONFIG_MAX_READ, OPM_TYPE_INT}, |
45 |
{OPM_CONFIG_TIMEOUT, OPM_TYPE_INT}, |
46 |
}; |
47 |
|
48 |
|
49 |
/* config_create |
50 |
* |
51 |
* Create an OPM_CONFIG_T struct, set default values and return it |
52 |
* |
53 |
* Parameters: |
54 |
* None; |
55 |
* |
56 |
* Return: |
57 |
* Pointer to allocated OPM_CONFIG_T struct |
58 |
*/ |
59 |
|
60 |
OPM_CONFIG_T *libopm_config_create() |
61 |
{ |
62 |
int num, i; |
63 |
OPM_CONFIG_T *ret; |
64 |
|
65 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
66 |
|
67 |
ret = xcalloc(sizeof(OPM_CONFIG_T)); |
68 |
ret->vars = xcalloc(sizeof(void *) * num); |
69 |
|
70 |
|
71 |
/* Set default config items. This in the future would be much better |
72 |
if it could set realistic defaults for each individual config item. |
73 |
|
74 |
OPM_TYPE_INT = 0 |
75 |
OPM_TYPE_STRING = "" |
76 |
OPM_TYPE_ADDRESS = 0.0.0.0 |
77 |
OPM_TYPE_STRINGLIST = empty list |
78 |
*/ |
79 |
|
80 |
for(i = 0; i < num; i++) |
81 |
{ |
82 |
switch(libopm_config_gettype(i)) |
83 |
{ |
84 |
case OPM_TYPE_INT: |
85 |
ret->vars[i] = xcalloc(sizeof(int)); |
86 |
*(int *) ret->vars[i] = 0; |
87 |
break; |
88 |
|
89 |
case OPM_TYPE_STRING: |
90 |
ret->vars[i] = libopm_xstrdup(""); |
91 |
break; |
92 |
|
93 |
case OPM_TYPE_ADDRESS: |
94 |
ret->vars[i] = xcalloc(sizeof(opm_sockaddr)); |
95 |
memset((opm_sockaddr *) ret->vars[i], 0, sizeof(opm_sockaddr)); |
96 |
break; |
97 |
|
98 |
case OPM_TYPE_STRINGLIST: |
99 |
ret->vars[i] = libopm_list_create(); |
100 |
break; |
101 |
default: |
102 |
ret->vars[i] = NULL; |
103 |
} |
104 |
} |
105 |
return ret; |
106 |
} |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
/* config_free |
112 |
* |
113 |
* Free config structure and clean up |
114 |
* |
115 |
* Parameters: |
116 |
* config: Structure to free/cleanup |
117 |
* |
118 |
* Return: |
119 |
* None |
120 |
*/ |
121 |
|
122 |
void libopm_config_free(OPM_CONFIG_T *config) |
123 |
{ |
124 |
int num, i; |
125 |
OPM_NODE_T *p, *next; |
126 |
OPM_LIST_T *list; |
127 |
|
128 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
129 |
|
130 |
for(i = 0; i < num; i++) |
131 |
{ |
132 |
if(config->vars[i] == NULL) |
133 |
continue; |
134 |
|
135 |
switch(libopm_config_gettype(i)) |
136 |
{ |
137 |
case OPM_TYPE_STRINGLIST: |
138 |
list = (OPM_LIST_T *) config->vars[i]; |
139 |
LIST_FOREACH_SAFE(p, next, list->head) |
140 |
{ |
141 |
MyFree(p->data); |
142 |
MyFree(p); |
143 |
} |
144 |
break; |
145 |
default: |
146 |
MyFree(config->vars[i]); |
147 |
break; |
148 |
} |
149 |
} |
150 |
|
151 |
MyFree(config->vars); |
152 |
MyFree(config); |
153 |
} |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
/* config_set |
159 |
* |
160 |
* Set configuration options on config struct. |
161 |
* |
162 |
* Parameters: |
163 |
* config: Config struct to set parameters on |
164 |
* key: Variable within the struct to set |
165 |
* value: Address of value to set |
166 |
* |
167 |
* Return: |
168 |
* 1: Variable was set |
169 |
* 0: Some error occured |
170 |
*/ |
171 |
|
172 |
OPM_ERR_T libopm_config_set(OPM_CONFIG_T *config, int key, const void *value) |
173 |
{ |
174 |
|
175 |
int num; |
176 |
OPM_NODE_T *node; |
177 |
|
178 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
179 |
|
180 |
if(key < 0 || key >= num) |
181 |
return OPM_ERR_BADKEY; /* Return appropriate error code eventually */ |
182 |
|
183 |
switch(libopm_config_gettype(key)) |
184 |
{ |
185 |
case OPM_TYPE_STRING: |
186 |
if((char *) config->vars[key] != NULL) |
187 |
MyFree(config->vars[key]); |
188 |
config->vars[key] = libopm_xstrdup(value); |
189 |
break; |
190 |
|
191 |
case OPM_TYPE_INT: |
192 |
*(int *) config->vars[key] = *(const int *) value; |
193 |
break; |
194 |
|
195 |
case OPM_TYPE_ADDRESS: |
196 |
if (inet_pton(AF_INET, value, &(((opm_sockaddr *)config->vars[key])->sa4.sin_addr)) <= 0) |
197 |
return OPM_ERR_BADVALUE; /* return appropriate err code */ |
198 |
|
199 |
break; |
200 |
|
201 |
case OPM_TYPE_STRINGLIST: |
202 |
node = libopm_node_create(libopm_xstrdup(value)); |
203 |
libopm_list_add((OPM_LIST_T *) config->vars[key], node); |
204 |
break; |
205 |
|
206 |
default: |
207 |
return OPM_ERR_BADKEY; /* return appropriate err code */ |
208 |
|
209 |
} |
210 |
|
211 |
return OPM_SUCCESS; |
212 |
|
213 |
} |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
/* config_gettype |
219 |
* |
220 |
* Get type of key. |
221 |
* |
222 |
* Parameters: |
223 |
* key: Key to get type of. |
224 |
* |
225 |
* Return: |
226 |
* TYPE_? of key |
227 |
*/ |
228 |
|
229 |
int libopm_config_gettype(int key) |
230 |
{ |
231 |
int num, i; |
232 |
|
233 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
234 |
|
235 |
for(i = 0; i < num; i++) |
236 |
if(HASH[i].key == key) |
237 |
return HASH[i].type; |
238 |
|
239 |
return 0; |
240 |
} |
241 |
|
242 |
/* config |
243 |
* |
244 |
* Retrieve a specific config variable from |
245 |
* an OPM_CONFIG_T struct. This is basically a |
246 |
* wrapper to extracting the variable from the |
247 |
* array. |
248 |
* |
249 |
* Parameters: |
250 |
* config: Config struct to extract from |
251 |
* key: Value to extract |
252 |
* |
253 |
* Return: |
254 |
* -ADDRESS- to extracted value in array. This address |
255 |
* will have to be cast on the return end to be any use. |
256 |
*/ |
257 |
|
258 |
void *libopm_config(OPM_CONFIG_T *config, int key) |
259 |
{ |
260 |
return config->vars[key]; |
261 |
} |