1 |
michael |
5052 |
/* |
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 |
michael |
5325 |
#include <string.h> |
27 |
|
|
|
28 |
michael |
5052 |
#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 |
michael |
5325 |
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 |
michael |
5052 |
}; |
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 |
michael |
5325 |
OPM_CONFIG_T * |
60 |
|
|
libopm_config_create(void) |
61 |
michael |
5052 |
{ |
62 |
michael |
5707 |
unsigned int num; |
63 |
michael |
5325 |
OPM_CONFIG_T *ret; |
64 |
michael |
5052 |
|
65 |
michael |
5325 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
66 |
michael |
5052 |
|
67 |
michael |
5325 |
ret = xcalloc(sizeof(OPM_CONFIG_T)); |
68 |
|
|
ret->vars = xcalloc(sizeof(void *) * num); |
69 |
michael |
5052 |
|
70 |
|
|
|
71 |
michael |
5325 |
/* |
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 |
michael |
5052 |
*/ |
80 |
michael |
5707 |
for (unsigned int i = 0; i < num; ++i) |
81 |
michael |
5325 |
{ |
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 |
michael |
5052 |
|
89 |
michael |
5325 |
case OPM_TYPE_STRING: |
90 |
|
|
ret->vars[i] = libopm_xstrdup(""); |
91 |
|
|
break; |
92 |
michael |
5052 |
|
93 |
michael |
5325 |
case OPM_TYPE_ADDRESS: |
94 |
|
|
ret->vars[i] = xcalloc(sizeof(opm_sockaddr)); |
95 |
|
|
break; |
96 |
michael |
5052 |
|
97 |
michael |
5325 |
case OPM_TYPE_STRINGLIST: |
98 |
|
|
ret->vars[i] = libopm_list_create(); |
99 |
|
|
break; |
100 |
|
|
default: |
101 |
|
|
ret->vars[i] = NULL; |
102 |
|
|
} |
103 |
|
|
} |
104 |
michael |
5052 |
|
105 |
michael |
5325 |
return ret; |
106 |
michael |
5052 |
} |
107 |
|
|
|
108 |
|
|
/* config_free |
109 |
|
|
* |
110 |
|
|
* Free config structure and clean up |
111 |
|
|
* |
112 |
|
|
* Parameters: |
113 |
|
|
* config: Structure to free/cleanup |
114 |
michael |
5325 |
* |
115 |
michael |
5052 |
* Return: |
116 |
|
|
* None |
117 |
|
|
*/ |
118 |
michael |
5325 |
void |
119 |
|
|
libopm_config_free(OPM_CONFIG_T *config) |
120 |
michael |
5052 |
{ |
121 |
michael |
5707 |
unsigned int num; |
122 |
michael |
5325 |
OPM_NODE_T *p, *next; |
123 |
|
|
OPM_LIST_T *list; |
124 |
michael |
5052 |
|
125 |
michael |
5325 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
126 |
michael |
5052 |
|
127 |
michael |
5707 |
for (unsigned int i = 0; i < num; i++) |
128 |
michael |
5325 |
{ |
129 |
|
|
if (config->vars[i] == NULL) |
130 |
|
|
continue; |
131 |
michael |
5052 |
|
132 |
michael |
5325 |
switch (libopm_config_gettype(i)) |
133 |
|
|
{ |
134 |
|
|
case OPM_TYPE_STRINGLIST: |
135 |
|
|
list = (OPM_LIST_T *)config->vars[i]; |
136 |
michael |
5052 |
|
137 |
michael |
5325 |
LIST_FOREACH_SAFE(p, next, list->head) |
138 |
|
|
{ |
139 |
|
|
MyFree(p->data); |
140 |
|
|
MyFree(p); |
141 |
|
|
} |
142 |
michael |
5052 |
|
143 |
michael |
5325 |
break; |
144 |
michael |
5052 |
|
145 |
michael |
5325 |
default: |
146 |
|
|
MyFree(config->vars[i]); |
147 |
|
|
break; |
148 |
|
|
} |
149 |
|
|
} |
150 |
michael |
5052 |
|
151 |
michael |
5325 |
MyFree(config->vars); |
152 |
|
|
MyFree(config); |
153 |
|
|
} |
154 |
michael |
5052 |
|
155 |
|
|
/* config_set |
156 |
|
|
* |
157 |
|
|
* Set configuration options on config struct. |
158 |
|
|
* |
159 |
|
|
* Parameters: |
160 |
|
|
* config: Config struct to set parameters on |
161 |
|
|
* key: Variable within the struct to set |
162 |
michael |
5325 |
* value: Address of value to set |
163 |
michael |
5052 |
* |
164 |
|
|
* Return: |
165 |
|
|
* 1: Variable was set |
166 |
|
|
* 0: Some error occured |
167 |
|
|
*/ |
168 |
|
|
|
169 |
michael |
5325 |
OPM_ERR_T |
170 |
|
|
libopm_config_set(OPM_CONFIG_T *config, int key, const void *value) |
171 |
michael |
5052 |
{ |
172 |
michael |
5325 |
int num; |
173 |
|
|
OPM_NODE_T *node; |
174 |
michael |
5052 |
|
175 |
michael |
5325 |
num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
176 |
michael |
5052 |
|
177 |
michael |
5325 |
if (key < 0 || key >= num) |
178 |
|
|
return OPM_ERR_BADKEY; /* Return appropriate error code eventually */ |
179 |
michael |
5052 |
|
180 |
michael |
5325 |
switch (libopm_config_gettype(key)) |
181 |
|
|
{ |
182 |
|
|
case OPM_TYPE_STRING: |
183 |
|
|
if ((char *) config->vars[key]) |
184 |
|
|
MyFree(config->vars[key]); |
185 |
michael |
5052 |
|
186 |
michael |
5325 |
config->vars[key] = libopm_xstrdup(value); |
187 |
|
|
break; |
188 |
michael |
5052 |
|
189 |
michael |
5325 |
case OPM_TYPE_INT: |
190 |
|
|
*(int *) config->vars[key] = *(const int *) value; |
191 |
|
|
break; |
192 |
michael |
5164 |
|
193 |
michael |
5325 |
case OPM_TYPE_ADDRESS: |
194 |
|
|
if (inet_pton(AF_INET, value, &(((opm_sockaddr *)config->vars[key])->sa4.sin_addr)) <= 0) |
195 |
|
|
return OPM_ERR_BADVALUE; /* Return appropriate err code */ |
196 |
michael |
5164 |
|
197 |
michael |
5325 |
break; |
198 |
michael |
5052 |
|
199 |
michael |
5325 |
case OPM_TYPE_STRINGLIST: |
200 |
|
|
node = libopm_node_create(libopm_xstrdup(value)); |
201 |
|
|
libopm_list_add((OPM_LIST_T *)config->vars[key], node); |
202 |
|
|
break; |
203 |
michael |
5052 |
|
204 |
michael |
5325 |
default: |
205 |
|
|
return OPM_ERR_BADKEY; /* return appropriate err code */ |
206 |
|
|
} |
207 |
michael |
5052 |
|
208 |
michael |
5325 |
return OPM_SUCCESS; |
209 |
michael |
5052 |
} |
210 |
|
|
|
211 |
|
|
/* config_gettype |
212 |
|
|
* |
213 |
|
|
* Get type of key. |
214 |
michael |
5325 |
* |
215 |
michael |
5052 |
* Parameters: |
216 |
|
|
* key: Key to get type of. |
217 |
michael |
5325 |
* |
218 |
michael |
5052 |
* Return: |
219 |
|
|
* TYPE_? of key |
220 |
|
|
*/ |
221 |
michael |
5325 |
int |
222 |
|
|
libopm_config_gettype(int key) |
223 |
michael |
5052 |
{ |
224 |
michael |
5707 |
unsigned int num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T); |
225 |
michael |
5052 |
|
226 |
michael |
5707 |
for (unsigned int i = 0; i < num; ++i) |
227 |
michael |
5325 |
if (HASH[i].key == key) |
228 |
|
|
return HASH[i].type; |
229 |
michael |
5052 |
|
230 |
michael |
5325 |
return 0; |
231 |
michael |
5052 |
} |
232 |
|
|
|
233 |
|
|
/* config |
234 |
|
|
* |
235 |
|
|
* Retrieve a specific config variable from |
236 |
michael |
5325 |
* an OPM_CONFIG_T struct. This is basically a |
237 |
michael |
5052 |
* wrapper to extracting the variable from the |
238 |
|
|
* array. |
239 |
|
|
* |
240 |
|
|
* Parameters: |
241 |
|
|
* config: Config struct to extract from |
242 |
|
|
* key: Value to extract |
243 |
|
|
* |
244 |
|
|
* Return: |
245 |
|
|
* -ADDRESS- to extracted value in array. This address |
246 |
|
|
* will have to be cast on the return end to be any use. |
247 |
|
|
*/ |
248 |
michael |
5325 |
void * |
249 |
|
|
libopm_config(OPM_CONFIG_T *config, int key) |
250 |
michael |
5052 |
{ |
251 |
michael |
5325 |
return config->vars[key]; |
252 |
michael |
5052 |
} |