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