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: 6070
Committed: Mon Jun 8 20:18:29 2015 UTC (8 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 5586 byte(s)
Log Message:
- libopm/src/config.c: constification; remove extraneous comma from the 'HASH' table

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

Properties

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