/[svn]/hopm/branches/1.1.x/src/libopm/src/config.c
ViewVC logotype

Contents of /hopm/branches/1.1.x/src/libopm/src/config.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9996 - (show annotations)
Thu Jun 17 18:43:18 2021 UTC (5 weeks, 2 days ago) by michael
File MIME type: text/x-chdr
File size: 6151 byte(s)
- config.c, libopm.c: remove redundant 'p->addr.ss_family = res->ai_family' assignments

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 <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netdb.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33
34 #include "memory.h"
35 #include "config.h"
36 #include "opm_error.h"
37 #include "opm_types.h"
38 #include "opm_common.h"
39 #include "list.h"
40
41
42 static const OPM_CONFIG_HASH_T HASH[] =
43 {
44 { OPM_CONFIG_FD_LIMIT, OPM_TYPE_INT },
45 { OPM_CONFIG_BIND_IP , OPM_TYPE_ADDRESS },
46 { OPM_CONFIG_DNSBL_HOST, OPM_TYPE_STRING },
47 { OPM_CONFIG_TARGET_STRING, OPM_TYPE_STRINGLIST },
48 { OPM_CONFIG_SCAN_IP, OPM_TYPE_STRING },
49 { OPM_CONFIG_SCAN_PORT, OPM_TYPE_INT },
50 { OPM_CONFIG_MAX_READ, OPM_TYPE_INT },
51 { OPM_CONFIG_TIMEOUT, OPM_TYPE_INT }
52 };
53
54 /* config_create
55 *
56 * Create an OPM_CONFIG_T struct, set default values and return it
57 *
58 * Parameters:
59 * None;
60 *
61 * Return:
62 * Pointer to allocated OPM_CONFIG_T struct
63 */
64 OPM_CONFIG_T *
65 libopm_config_create(void)
66 {
67 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
68 OPM_CONFIG_T *ret;
69
70 ret = libopm_calloc(sizeof(OPM_CONFIG_T));
71 ret->vars = libopm_calloc(sizeof(void *) * num);
72
73
74 /*
75 * Set default config items. This in the future would be much better
76 * if it could set realistic defaults for each individual config item.
77 *
78 * OPM_TYPE_INT = 0
79 * OPM_TYPE_STRING = ""
80 * OPM_TYPE_ADDRESS = 0.0.0.0
81 * OPM_TYPE_STRINGLIST = empty list
82 */
83 for (unsigned int i = 0; i < num; ++i)
84 {
85 switch (libopm_config_gettype(i))
86 {
87 case OPM_TYPE_INT:
88 ret->vars[i] = libopm_calloc(sizeof(int));
89 break;
90
91 case OPM_TYPE_STRING:
92 ret->vars[i] = libopm_strdup("");
93 break;
94
95 case OPM_TYPE_ADDRESS:
96 ret->vars[i] = libopm_calloc(sizeof(struct sockaddr_storage));
97 break;
98
99 case OPM_TYPE_STRINGLIST:
100 ret->vars[i] = libopm_calloc(sizeof(OPM_LIST_T));
101 break;
102
103 default:
104 ret->vars[i] = NULL;
105 }
106 }
107
108 return ret;
109 }
110
111 /* config_free
112 *
113 * Free config structure and clean up
114 *
115 * Parameters:
116 * config: Structure to free/cleanup
117 *
118 * Return:
119 * None
120 */
121 void
122 libopm_config_free(OPM_CONFIG_T *config)
123 {
124 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
125 OPM_NODE_T *p, *next;
126 OPM_LIST_T *list;
127
128 for (unsigned 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 = config->vars[i];
137
138 LIST_FOREACH_SAFE(p, next, list->head)
139 {
140 libopm_free(p->data);
141 libopm_free(p);
142 }
143
144 break;
145
146 default:
147 libopm_free(config->vars[i]);
148 break;
149 }
150 }
151
152 libopm_free(config->vars);
153 libopm_free(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, unsigned int key, const void *value)
172 {
173 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
174 OPM_NODE_T *node;
175
176 if (key >= num)
177 return OPM_ERR_BADKEY; /* Return appropriate error code eventually */
178
179 switch (libopm_config_gettype(key))
180 {
181 case OPM_TYPE_STRING:
182 if (config->vars[key])
183 libopm_free(config->vars[key]);
184
185 config->vars[key] = libopm_strdup(value);
186 break;
187
188 case OPM_TYPE_INT:
189 *(int *)config->vars[key] = *(const int *)value;
190 break;
191
192 case OPM_TYPE_ADDRESS:
193 {
194 struct addrinfo hints, *res;
195
196 memset(&hints, 0, sizeof(hints));
197 hints.ai_family = AF_UNSPEC;
198 hints.ai_socktype = SOCK_STREAM;
199 hints.ai_flags = AI_NUMERICHOST;
200
201 if (getaddrinfo(value, NULL, &hints, &res) || res->ai_family != AF_INET) /* XXX: v4 only for now */
202 {
203 if (res)
204 freeaddrinfo(res);
205
206 return OPM_ERR_BADVALUE; /* Return appropriate err code */
207 }
208
209 struct sockaddr_storage *const addr = config->vars[key];
210 memcpy(addr, res->ai_addr, res->ai_addrlen);
211
212 freeaddrinfo(res);
213 break;
214 }
215
216 case OPM_TYPE_STRINGLIST:
217 node = libopm_node_create(libopm_strdup(value));
218 libopm_list_add(config->vars[key], node);
219 break;
220
221 default:
222 return OPM_ERR_BADKEY; /* return appropriate err code */
223 }
224
225 return OPM_SUCCESS;
226 }
227
228 /* config_gettype
229 *
230 * Get type of key.
231 *
232 * Parameters:
233 * key: Key to get type of.
234 *
235 * Return:
236 * TYPE_? of key
237 */
238 int
239 libopm_config_gettype(int key)
240 {
241 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
242
243 for (unsigned int i = 0; i < num; ++i)
244 if (HASH[i].key == key)
245 return HASH[i].type;
246
247 return 0;
248 }
249
250 /* config
251 *
252 * Retrieve a specific config variable from
253 * an OPM_CONFIG_T struct. This is basically a
254 * wrapper to extracting the variable from the
255 * array.
256 *
257 * Parameters:
258 * config: Config struct to extract from
259 * key: Value to extract
260 *
261 * Return:
262 * -ADDRESS- to extracted value in array. This address
263 * will have to be cast on the return end to be any use.
264 */
265 void *
266 libopm_config(OPM_CONFIG_T *config, unsigned int key)
267 {
268 return config->vars[key];
269 }

Properties

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

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