ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.0.x/src/libopm/src/config.c
Revision: 5911
Committed: Tue May 5 18:32:36 2015 UTC (8 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 5600 byte(s)
Log Message:
- config.c, proxy.c: removed useless casts

File Contents

# Content
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 unsigned 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 (unsigned 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 break;
96
97 case OPM_TYPE_STRINGLIST:
98 ret->vars[i] = libopm_list_create();
99 break;
100 default:
101 ret->vars[i] = NULL;
102 }
103 }
104
105 return ret;
106 }
107
108 /* config_free
109 *
110 * Free config structure and clean up
111 *
112 * Parameters:
113 * config: Structure to free/cleanup
114 *
115 * Return:
116 * None
117 */
118 void
119 libopm_config_free(OPM_CONFIG_T *config)
120 {
121 unsigned int num;
122 OPM_NODE_T *p, *next;
123 OPM_LIST_T *list;
124
125 num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T);
126
127 for (unsigned int i = 0; i < num; i++)
128 {
129 if (config->vars[i] == NULL)
130 continue;
131
132 switch (libopm_config_gettype(i))
133 {
134 case OPM_TYPE_STRINGLIST:
135 list = config->vars[i];
136
137 LIST_FOREACH_SAFE(p, next, list->head)
138 {
139 MyFree(p->data);
140 MyFree(p);
141 }
142
143 break;
144
145 default:
146 MyFree(config->vars[i]);
147 break;
148 }
149 }
150
151 MyFree(config->vars);
152 MyFree(config);
153 }
154
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 * value: Address of value to set
163 *
164 * Return:
165 * 1: Variable was set
166 * 0: Some error occured
167 */
168
169 OPM_ERR_T
170 libopm_config_set(OPM_CONFIG_T *config, int key, const void *value)
171 {
172 int num;
173 OPM_NODE_T *node;
174
175 num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T);
176
177 if (key < 0 || key >= num)
178 return OPM_ERR_BADKEY; /* Return appropriate error code eventually */
179
180 switch (libopm_config_gettype(key))
181 {
182 case OPM_TYPE_STRING:
183 if (config->vars[key])
184 MyFree(config->vars[key]);
185
186 config->vars[key] = libopm_xstrdup(value);
187 break;
188
189 case OPM_TYPE_INT:
190 *(int *) config->vars[key] = *(const int *) value;
191 break;
192
193 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
197 break;
198
199 case OPM_TYPE_STRINGLIST:
200 node = libopm_node_create(libopm_xstrdup(value));
201 libopm_list_add(config->vars[key], node);
202 break;
203
204 default:
205 return OPM_ERR_BADKEY; /* return appropriate err code */
206 }
207
208 return OPM_SUCCESS;
209 }
210
211 /* config_gettype
212 *
213 * Get type of key.
214 *
215 * Parameters:
216 * key: Key to get type of.
217 *
218 * Return:
219 * TYPE_? of key
220 */
221 int
222 libopm_config_gettype(int key)
223 {
224 unsigned int num = sizeof(HASH) / sizeof(OPM_CONFIG_HASH_T);
225
226 for (unsigned int i = 0; i < num; ++i)
227 if (HASH[i].key == key)
228 return HASH[i].type;
229
230 return 0;
231 }
232
233 /* config
234 *
235 * Retrieve a specific config variable from
236 * an OPM_CONFIG_T struct. This is basically a
237 * 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 void *
249 libopm_config(OPM_CONFIG_T *config, int key)
250 {
251 return config->vars[key];
252 }

Properties

Name Value
svn:eol-style native
svn:keywords Id