/[svn]/hopm/trunk/src/libopm/src/config.c
ViewVC logotype

Contents of /hopm/trunk/src/libopm/src/config.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5135 - (show annotations)
Thu Dec 25 18:51:51 2014 UTC (4 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 5934 byte(s)
- propset svn:eol-style native

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.26