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 <string.h> |
27 |
|
28 |
#include "malloc.h" |
29 |
#include "config.h" |
30 |
#include "inet.h" |
31 |
#include "opm_error.h" |
32 |
#include "opm_types.h" |
33 |
#include "opm_common.h" |
34 |
#include "list.h" |
35 |
|
36 |
|
37 |
static OPM_CONFIG_HASH_T HASH[] = |
38 |
{ |
39 |
{ OPM_CONFIG_FD_LIMIT, OPM_TYPE_INT }, |
40 |
{ OPM_CONFIG_BIND_IP , OPM_TYPE_ADDRESS }, |
41 |
{ OPM_CONFIG_DNSBL_HOST, OPM_TYPE_STRING }, |
42 |
{ OPM_CONFIG_TARGET_STRING, OPM_TYPE_STRINGLIST }, |
43 |
{ OPM_CONFIG_SCAN_IP, OPM_TYPE_STRING }, |
44 |
{ OPM_CONFIG_SCAN_PORT, OPM_TYPE_INT }, |
45 |
{ OPM_CONFIG_MAX_READ, OPM_TYPE_INT }, |
46 |
{ OPM_CONFIG_TIMEOUT, OPM_TYPE_INT }, |
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 |
OPM_CONFIG_T * |
60 |
libopm_config_create(void) |
61 |
{ |
62 |
int num; |
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 |
/* |
72 |
* Set default config items. This in the future would be much better |
73 |
* if it could set realistic defaults for each individual config item. |
74 |
* |
75 |
* OPM_TYPE_INT = 0 |
76 |
* OPM_TYPE_STRING = "" |
77 |
* OPM_TYPE_ADDRESS = 0.0.0.0 |
78 |
* OPM_TYPE_STRINGLIST = empty list |
79 |
*/ |
80 |
for (int 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 |
|
106 |
return ret; |
107 |
} |
108 |
|
109 |
/* config_free |
110 |
* |
111 |
* Free config structure and clean up |
112 |
* |
113 |
* Parameters: |
114 |
* config: Structure to free/cleanup |
115 |
* |
116 |
* Return: |
117 |
* None |
118 |
*/ |
119 |
void |
120 |
libopm_config_free(OPM_CONFIG_T *config) |
121 |
{ |
122 |
int num; |
123 |
OPM_NODE_T *p, *next; |
124 |
OPM_LIST_T *list; |
125 |
|
126 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
127 |
|
128 |
for (int i = 0; i < num; i++) |
129 |
{ |
130 |
if (config->vars[i] == NULL) |
131 |
continue; |
132 |
|
133 |
switch (libopm_config_gettype(i)) |
134 |
{ |
135 |
case OPM_TYPE_STRINGLIST: |
136 |
list = (OPM_LIST_T *)config->vars[i]; |
137 |
|
138 |
LIST_FOREACH_SAFE(p, next, list->head) |
139 |
{ |
140 |
MyFree(p->data); |
141 |
MyFree(p); |
142 |
} |
143 |
|
144 |
break; |
145 |
|
146 |
default: |
147 |
MyFree(config->vars[i]); |
148 |
break; |
149 |
} |
150 |
} |
151 |
|
152 |
MyFree(config->vars); |
153 |
MyFree(config); |
154 |
} |
155 |
|
156 |
/* config_set |
157 |
* |
158 |
* Set configuration options on config struct. |
159 |
* |
160 |
* Parameters: |
161 |
* config: Config struct to set parameters on |
162 |
* key: Variable within the struct to set |
163 |
* value: Address of value to set |
164 |
* |
165 |
* Return: |
166 |
* 1: Variable was set |
167 |
* 0: Some error occured |
168 |
*/ |
169 |
|
170 |
OPM_ERR_T |
171 |
libopm_config_set(OPM_CONFIG_T *config, int key, const void *value) |
172 |
{ |
173 |
int num; |
174 |
OPM_NODE_T *node; |
175 |
|
176 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
177 |
|
178 |
if (key < 0 || key >= num) |
179 |
return OPM_ERR_BADKEY; /* Return appropriate error code eventually */ |
180 |
|
181 |
switch (libopm_config_gettype(key)) |
182 |
{ |
183 |
case OPM_TYPE_STRING: |
184 |
if ((char *) config->vars[key]) |
185 |
MyFree(config->vars[key]); |
186 |
|
187 |
config->vars[key] = libopm_xstrdup(value); |
188 |
break; |
189 |
|
190 |
case OPM_TYPE_INT: |
191 |
*(int *) config->vars[key] = *(const int *) value; |
192 |
break; |
193 |
|
194 |
case OPM_TYPE_ADDRESS: |
195 |
if (inet_pton(AF_INET, value, &(((opm_sockaddr *)config->vars[key])->sa4.sin_addr)) <= 0) |
196 |
return OPM_ERR_BADVALUE; /* Return appropriate err code */ |
197 |
|
198 |
break; |
199 |
|
200 |
case OPM_TYPE_STRINGLIST: |
201 |
node = libopm_node_create(libopm_xstrdup(value)); |
202 |
libopm_list_add((OPM_LIST_T *)config->vars[key], node); |
203 |
break; |
204 |
|
205 |
default: |
206 |
return OPM_ERR_BADKEY; /* return appropriate err code */ |
207 |
} |
208 |
|
209 |
return OPM_SUCCESS; |
210 |
} |
211 |
|
212 |
/* config_gettype |
213 |
* |
214 |
* Get type of key. |
215 |
* |
216 |
* Parameters: |
217 |
* key: Key to get type of. |
218 |
* |
219 |
* Return: |
220 |
* TYPE_? of key |
221 |
*/ |
222 |
int |
223 |
libopm_config_gettype(int key) |
224 |
{ |
225 |
int num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
226 |
|
227 |
for (int i = 0; i < num; i++) |
228 |
if (HASH[i].key == key) |
229 |
return HASH[i].type; |
230 |
|
231 |
return 0; |
232 |
} |
233 |
|
234 |
/* config |
235 |
* |
236 |
* Retrieve a specific config variable from |
237 |
* an OPM_CONFIG_T struct. This is basically a |
238 |
* wrapper to extracting the variable from the |
239 |
* array. |
240 |
* |
241 |
* Parameters: |
242 |
* config: Config struct to extract from |
243 |
* key: Value to extract |
244 |
* |
245 |
* Return: |
246 |
* -ADDRESS- to extracted value in array. This address |
247 |
* will have to be cast on the return end to be any use. |
248 |
*/ |
249 |
void * |
250 |
libopm_config(OPM_CONFIG_T *config, int key) |
251 |
{ |
252 |
return config->vars[key]; |
253 |
} |