aboutsummaryrefslogtreecommitdiffstats
path: root/src/libopm/src/config.c
blob: f2d8279ef1d75cd12764cf1d3b3277d3b2d59e80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (C) 2002  Erik Fears
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to
 *
 *       The Free Software Foundation, Inc.
 *       59 Temple Place - Suite 330
 *       Boston, MA  02111-1307, USA.
 *
 *
 */

#include "setup.h"

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "memory.h"
#include "config.h"
#include "opm_error.h"
#include "opm_types.h"
#include "opm_common.h"
#include "list.h"


static const OPM_CONFIG_HASH_T HASH[] =
{
  { OPM_CONFIG_FD_LIMIT,      OPM_TYPE_INT        },
  { OPM_CONFIG_BIND_IP ,      OPM_TYPE_ADDRESS    },
  { OPM_CONFIG_DNSBL_HOST,    OPM_TYPE_STRING     },
  { OPM_CONFIG_TARGET_STRING, OPM_TYPE_STRINGLIST },
  { OPM_CONFIG_SCAN_IP,       OPM_TYPE_STRING     },
  { OPM_CONFIG_SCAN_PORT,     OPM_TYPE_INT        },
  { OPM_CONFIG_MAX_READ,      OPM_TYPE_INT        },
  { OPM_CONFIG_TIMEOUT,       OPM_TYPE_INT        }
};

/* config_create
 *
 *    Create an OPM_CONFIG_T struct, set default values and return it
 *
 * Parameters:
 *    None;
 *
 * Return:
 *    Pointer to allocated OPM_CONFIG_T struct
 */
OPM_CONFIG_T *
libopm_config_create(void)
{
  const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
  OPM_CONFIG_T *ret;

  ret = libopm_calloc(sizeof(OPM_CONFIG_T));
  ret->vars = libopm_calloc(sizeof(void *) * num);


  /*
   * Set default config items. This in the future would be much better
   * if it could set realistic defaults for each individual config item.
   *
   * OPM_TYPE_INT     = 0
   * OPM_TYPE_STRING  = ""
   * OPM_TYPE_ADDRESS = 0.0.0.0
   * OPM_TYPE_STRINGLIST = empty list
   */
  for (unsigned int i = 0; i < num; ++i)
  {
    switch (libopm_config_gettype(i))
    {
      case OPM_TYPE_INT:
        ret->vars[i] = libopm_calloc(sizeof(int));
        break;

      case OPM_TYPE_STRING:
        ret->vars[i] = libopm_strdup("");
        break;

      case OPM_TYPE_ADDRESS:
        ret->vars[i] = libopm_calloc(sizeof(struct sockaddr_storage));
        break;

      case OPM_TYPE_STRINGLIST:
        ret->vars[i] = libopm_calloc(sizeof(OPM_LIST_T));
        break;

      default:
        ret->vars[i] = NULL;
    }
  }

  return ret;
}

/* config_free
 *
 *    Free config structure and clean up
 *
 * Parameters:
 *    config: Structure to free/cleanup
 *
 * Return:
 *    None
 */
void
libopm_config_free(OPM_CONFIG_T *config)
{
  const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
  OPM_NODE_T *p, *next;
  OPM_LIST_T *list;

  for (unsigned int i = 0; i < num; ++i)
  {
    if (config->vars[i] == NULL)
      continue;

    switch (libopm_config_gettype(i))
    {
      case OPM_TYPE_STRINGLIST:
        list = config->vars[i];

        LIST_FOREACH_SAFE(p, next, list->head)
        {
          libopm_free(p->data);
          libopm_free(p);
        }

        break;

      default:
        libopm_free(config->vars[i]);
        break;
    }
  }

  libopm_free(config->vars);
  libopm_free(config);
}

/* config_set
 *
 *    Set configuration options on config struct.
 *
 * Parameters:
 *    config: Config struct to set parameters on
 *    key:    Variable within the struct to set
 *    value:  Address of value to set
 *
 * Return:
 *    1: Variable was set
 *    0: Some error occured
 */

OPM_ERR_T
libopm_config_set(OPM_CONFIG_T *config, unsigned int key, const void *value)
{
  const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);
  OPM_NODE_T *node;

  if (key >= num)
    return OPM_ERR_BADKEY;  /* Return appropriate error code eventually */

  switch (libopm_config_gettype(key))
  {
    case OPM_TYPE_STRING:
      if (config->vars[key])
        libopm_free(config->vars[key]);

      config->vars[key] = libopm_strdup(value);
      break;

    case OPM_TYPE_INT:
      *(int *)config->vars[key] = *(const int *)value;
      break;

    case OPM_TYPE_ADDRESS:
    {
      struct addrinfo hints, *res;

      memset(&hints, 0, sizeof(hints));
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_flags = AI_NUMERICHOST;

      if (getaddrinfo(value, NULL, &hints, &res) || res->ai_family != AF_INET) /* XXX: v4 only for now */
      {
        if (res)
          freeaddrinfo(res);

        return OPM_ERR_BADVALUE;  /* Return appropriate err code */
      }

      struct sockaddr_storage *const addr = config->vars[key];
      memcpy(addr, res->ai_addr, res->ai_addrlen);
      addr->ss_family = res->ai_family;

      freeaddrinfo(res);
      break;
    }

    case OPM_TYPE_STRINGLIST:
      node = libopm_node_create(libopm_strdup(value));
      libopm_list_add(config->vars[key], node);
      break;

    default:
      return OPM_ERR_BADKEY; /* return appropriate err code */
  }

  return OPM_SUCCESS;
}

/* config_gettype
 *
 *    Get type of key.
 *
 * Parameters:
 *    key: Key to get type of.
 *
 * Return:
 *    TYPE_? of key
 */
int
libopm_config_gettype(int key)
{
  const unsigned int num = sizeof(HASH) / sizeof(HASH[0]);

  for (unsigned int i = 0; i < num; ++i)
    if (HASH[i].key == key)
      return HASH[i].type;

  return 0;
}

/* config
 *
 *    Retrieve a specific config variable from
 *    an OPM_CONFIG_T struct. This is basically a
 *    wrapper to extracting the variable from the
 *    array.
 *
 * Parameters:
 *    config: Config struct to extract from
 *    key:    Value to extract
 *
 * Return:
 *    -ADDRESS- to extracted value in array. This address
 *    will have to be cast on the return end to be any use.
 */
void *
libopm_config(OPM_CONFIG_T *config, unsigned int key)
{
  return config->vars[key];
}