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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9996 - (hide annotations)
Thu Jun 17 18:43:18 2021 UTC (3 months, 1 week 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 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 8202 #include <stdlib.h>
27 michael 9530 #include <string.h>
28 michael 8202 #include <sys/types.h>
29     #include <sys/socket.h>
30 michael 9530 #include <netdb.h>
31 michael 8202 #include <netinet/in.h>
32     #include <arpa/inet.h>
33 michael 5325
34 michael 5947 #include "memory.h"
35 michael 5052 #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 michael 6071 static const OPM_CONFIG_HASH_T HASH[] =
43 michael 5325 {
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 michael 6071 { OPM_CONFIG_TIMEOUT, OPM_TYPE_INT }
52 michael 5052 };
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 michael 5325 OPM_CONFIG_T *
65     libopm_config_create(void)
66 michael 5052 {
67 michael 9534 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
68 michael 5325 OPM_CONFIG_T *ret;
69 michael 5052
70 michael 8206 ret = libopm_calloc(sizeof(OPM_CONFIG_T));
71     ret->vars = libopm_calloc(sizeof(void *) * num);
72 michael 5052
73    
74 michael 5325 /*
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 michael 5052 */
83 michael 5707 for (unsigned int i = 0; i < num; ++i)
84 michael 5325 {
85     switch (libopm_config_gettype(i))
86     {
87     case OPM_TYPE_INT:
88 michael 8206 ret->vars[i] = libopm_calloc(sizeof(int));
89 michael 5325 break;
90 michael 5052
91 michael 5325 case OPM_TYPE_STRING:
92 michael 8206 ret->vars[i] = libopm_strdup("");
93 michael 5325 break;
94 michael 5052
95 michael 5325 case OPM_TYPE_ADDRESS:
96 michael 9530 ret->vars[i] = libopm_calloc(sizeof(struct sockaddr_storage));
97 michael 5325 break;
98 michael 5052
99 michael 5325 case OPM_TYPE_STRINGLIST:
100 michael 8580 ret->vars[i] = libopm_calloc(sizeof(OPM_LIST_T));
101 michael 5325 break;
102 michael 5913
103 michael 5325 default:
104     ret->vars[i] = NULL;
105     }
106     }
107 michael 5052
108 michael 5325 return ret;
109 michael 5052 }
110    
111     /* config_free
112     *
113     * Free config structure and clean up
114     *
115     * Parameters:
116     * config: Structure to free/cleanup
117 michael 5325 *
118 michael 5052 * Return:
119     * None
120     */
121 michael 5325 void
122     libopm_config_free(OPM_CONFIG_T *config)
123 michael 5052 {
124 michael 9534 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
125 michael 5325 OPM_NODE_T *p, *next;
126     OPM_LIST_T *list;
127 michael 5052
128 michael 6068 for (unsigned int i = 0; i < num; ++i)
129 michael 5325 {
130     if (config->vars[i] == NULL)
131     continue;
132 michael 5052
133 michael 5325 switch (libopm_config_gettype(i))
134     {
135     case OPM_TYPE_STRINGLIST:
136 michael 5912 list = config->vars[i];
137 michael 5052
138 michael 5325 LIST_FOREACH_SAFE(p, next, list->head)
139     {
140 michael 8206 libopm_free(p->data);
141     libopm_free(p);
142 michael 5325 }
143 michael 5052
144 michael 5325 break;
145 michael 5052
146 michael 5325 default:
147 michael 8206 libopm_free(config->vars[i]);
148 michael 5325 break;
149     }
150     }
151 michael 5052
152 michael 8206 libopm_free(config->vars);
153     libopm_free(config);
154 michael 5325 }
155 michael 5052
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 michael 5325 * value: Address of value to set
164 michael 5052 *
165     * Return:
166     * 1: Variable was set
167     * 0: Some error occured
168     */
169    
170 michael 5325 OPM_ERR_T
171 michael 5923 libopm_config_set(OPM_CONFIG_T *config, unsigned int key, const void *value)
172 michael 5052 {
173 michael 9534 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
174 michael 5325 OPM_NODE_T *node;
175 michael 5052
176 michael 5923 if (key >= num)
177 michael 5325 return OPM_ERR_BADKEY; /* Return appropriate error code eventually */
178 michael 5052
179 michael 5325 switch (libopm_config_gettype(key))
180     {
181     case OPM_TYPE_STRING:
182 michael 5912 if (config->vars[key])
183 michael 8206 libopm_free(config->vars[key]);
184 michael 5052
185 michael 8206 config->vars[key] = libopm_strdup(value);
186 michael 5325 break;
187 michael 5052
188 michael 5325 case OPM_TYPE_INT:
189 michael 5931 *(int *)config->vars[key] = *(const int *)value;
190 michael 5325 break;
191 michael 5164
192 michael 5325 case OPM_TYPE_ADDRESS:
193 michael 9530 {
194     struct addrinfo hints, *res;
195    
196 michael 9533 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 michael 9530 if (getaddrinfo(value, NULL, &hints, &res) || res->ai_family != AF_INET) /* XXX: v4 only for now */
202     {
203 michael 9541 if (res)
204     freeaddrinfo(res);
205    
206 michael 5325 return OPM_ERR_BADVALUE; /* Return appropriate err code */
207 michael 9530 }
208 michael 5164
209 michael 9541 struct sockaddr_storage *const addr = config->vars[key];
210     memcpy(addr, res->ai_addr, res->ai_addrlen);
211 michael 9530
212     freeaddrinfo(res);
213 michael 5325 break;
214 michael 9530 }
215 michael 5052
216 michael 5325 case OPM_TYPE_STRINGLIST:
217 michael 8206 node = libopm_node_create(libopm_strdup(value));
218 michael 5912 libopm_list_add(config->vars[key], node);
219 michael 5325 break;
220 michael 5052
221 michael 5325 default:
222     return OPM_ERR_BADKEY; /* return appropriate err code */
223     }
224 michael 5052
225 michael 5325 return OPM_SUCCESS;
226 michael 5052 }
227    
228     /* config_gettype
229     *
230     * Get type of key.
231 michael 5325 *
232 michael 5052 * Parameters:
233     * key: Key to get type of.
234 michael 5325 *
235 michael 5052 * Return:
236     * TYPE_? of key
237     */
238 michael 5325 int
239     libopm_config_gettype(int key)
240 michael 5052 {
241 michael 9534 const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
242 michael 5052
243 michael 5707 for (unsigned int i = 0; i < num; ++i)
244 michael 5325 if (HASH[i].key == key)
245     return HASH[i].type;
246 michael 5052
247 michael 5325 return 0;
248 michael 5052 }
249    
250     /* config
251     *
252     * Retrieve a specific config variable from
253 michael 5325 * an OPM_CONFIG_T struct. This is basically a
254 michael 5052 * 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 michael 5325 void *
266 michael 5923 libopm_config(OPM_CONFIG_T *config, unsigned int key)
267 michael 5052 {
268 michael 5325 return config->vars[key];
269 michael 5052 }

Properties

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

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