Statistics
| Branch: | Tag: | Revision:

root / src / charon / plugins / attr / attr_provider.c @ b5a2055fb1b88ea4abb97334d89e311c9ceaa7d4

History | View | Annotate | Download (3.8 KB)

1
/*
2
 * Copyright (C) 2009 Martin Willi
3
 * Hochschule fuer Technik Rapperswil
4
 *
5
 * This program is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License as published by the
7
 * Free Software Foundation; either version 2 of the License, or (at your
8
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
9
 *
10
 * This program is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
 * for more details.
14
 */
15
16
#include "attr_provider.h"
17
18
#include <time.h>
19
20
#include <daemon.h>
21
22
#define SERVER_MAX        2
23
24
typedef struct private_attr_provider_t private_attr_provider_t;
25
typedef struct attribute_entry_t attribute_entry_t;
26
27
/**
28
 * private data of attr_provider
29
 */
30
struct private_attr_provider_t {
31
32
    /**
33
     * public functions
34
     */
35
    attr_provider_t public;
36
37
    /**
38
     * List of attributes, attribute_entry_t
39
     */
40
    linked_list_t *attributes;
41
};
42
43
struct attribute_entry_t {
44
    /** type of attribute */
45
    configuration_attribute_type_t type;
46
    /** attribute value */
47
    chunk_t value;
48
};
49
50
/**
51
 * convert enumerator value from attribute_entry
52
 */
53
static bool attr_enum_filter(void *null, attribute_entry_t **in,
54
            configuration_attribute_type_t *type, void* none, chunk_t *value)
55
{
56
    *type = (*in)->type;
57
    *value = (*in)->value;
58
    return TRUE;
59
}
60
61
/**
62
 * Implementation of attribute_provider_t.create_attribute_enumerator
63
 */
64
static enumerator_t* create_attribute_enumerator(private_attr_provider_t *this,
65
                                            identification_t *id, host_t *vip)
66
{
67
    if (vip)
68
    {
69
        return enumerator_create_filter(
70
                        this->attributes->create_enumerator(this->attributes),
71
                        (void*)attr_enum_filter, NULL, NULL);
72
    }
73
    return enumerator_create_empty();
74
}
75
76
/**
77
 * Implementation of attr_provider_t.destroy
78
 */
79
static void destroy(private_attr_provider_t *this)
80
{
81
    attribute_entry_t *entry;
82
83
    while (this->attributes->remove_last(this->attributes,
84
                                         (void**)&entry) == SUCCESS)
85
    {
86
        free(entry->value.ptr);
87
        free(entry);
88
    }
89
    this->attributes->destroy(this->attributes);
90
    free(this);
91
}
92
93
/**
94
 * Add an attribute entry to the list
95
 */
96
static void add_entry(private_attr_provider_t *this, char *key, int nr,
97
                      configuration_attribute_type_t type)
98
{
99
    attribute_entry_t *entry;
100
    host_t *host;
101
    char *str;
102
103
    str = lib->settings->get_str(lib->settings, "charon.%s%d", NULL, key, nr);
104
    if (str)
105
    {
106
        host = host_create_from_string(str, 0);
107
        if (host)
108
        {
109
            entry = malloc_thing(attribute_entry_t);
110
111
            if (host->get_family(host) == AF_INET6)
112
            {
113
                switch (type)
114
                {
115
                    case INTERNAL_IP4_DNS:
116
                        type = INTERNAL_IP6_DNS;
117
                        break;
118
                    case INTERNAL_IP4_NBNS:
119
                        type = INTERNAL_IP6_NBNS;
120
                        break;
121
                    default:
122
                        break;
123
                }
124
            }
125
            entry->type = type;
126
            entry->value = chunk_clone(host->get_address(host));
127
            host->destroy(host);
128
            this->attributes->insert_last(this->attributes, entry);
129
        }
130
    }
131
}
132
133
/*
134
 * see header file
135
 */
136
attr_provider_t *attr_provider_create(database_t *db)
137
{
138
    private_attr_provider_t *this;
139
    int i;
140
141
    this = malloc_thing(private_attr_provider_t);
142
143
    this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *, host_t *))return_null;
144
    this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *, identification_t*))return_false;
145
    this->public.provider.create_attribute_enumerator = (enumerator_t*(*)(attribute_provider_t*, identification_t *id, host_t *vip))create_attribute_enumerator;
146
    this->public.destroy = (void(*)(attr_provider_t*))destroy;
147
148
    this->attributes = linked_list_create();
149
150
    for (i = 1; i <= SERVER_MAX; i++)
151
    {
152
        add_entry(this, "dns", i, INTERNAL_IP4_DNS);
153
        add_entry(this, "nbns", i, INTERNAL_IP4_NBNS);
154
    }
155
156
    return &this->public;
157
}
158