summaryrefslogtreecommitdiff
path: root/agent/mibgroup/ip-mib/data_access/defaultrouter_common.c
blob: ae85bfaa8f3a5f983ed0da2751f85c01dc6c4d17 (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *  defaultrouter MIB architecture support
 *
 * $Id:$
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/data_access/defaultrouter.h>

#include "ip-mib/ipDefaultRouterTable/ipDefaultRouterTable.h"

/**---------------------------------------------------------------------*/
/*
 * local static prototypes
 */
static int _access_defaultrouter_entry_compare_addr(const void *lhs,
                                                    const void *rhs);
static void _access_defaultrouter_entry_release(netsnmp_defaultrouter_entry * entry,
                                                void *unused);

/**---------------------------------------------------------------------*/
/*
 * external per-architecture functions prototypes
 *
 * These shouldn't be called by the general public, so they aren't in
 * the header file.
 */
extern int
netsnmp_arch_defaultrouter_entry_init(netsnmp_defaultrouter_entry *entry);

extern int
netsnmp_arch_defaultrouter_container_load(netsnmp_container* container,
                                          u_int load_flags);

/**---------------------------------------------------------------------*/
/*
 * container functions
 */
/**
 */
netsnmp_container *
netsnmp_access_defaultrouter_container_init(u_int flags)
{
    netsnmp_container *container1;

    DEBUGMSGTL(("access:defaultrouter:container", "init\n"));

    /*
     * create the containers. one indexed by ifIndex, the other
     * indexed by ifName.
     */
    container1 = netsnmp_container_find("access_defaultrouter:table_container");
    if (NULL == container1) {
        snmp_log(LOG_ERR, "defaultrouter primary container is not found\n");
        return NULL;
    }
    container1->container_name = strdup("dr_index");

    if (flags & NETSNMP_ACCESS_DEFAULTROUTER_INIT_ADDL_IDX_BY_ADDR) {
        netsnmp_container *container2 =
            netsnmp_container_find("defaultrouter_addr:access_defaultrouter:table_container");
        if (NULL == container2) {
            snmp_log(LOG_ERR, "defaultrouter secondary container not found\n");
            CONTAINER_FREE(container1);
            return NULL;
        }

        container2->compare = _access_defaultrouter_entry_compare_addr;
        container2->container_name = strdup("dr_addr");

        netsnmp_container_add_index(container1, container2);
    }

    return container1;
}

/**
 * @retval NULL  error
 * @retval !NULL pointer to container
 */
netsnmp_container*
netsnmp_access_defaultrouter_container_load(netsnmp_container* container,
                                            u_int load_flags)
{
    int rc;
     u_int container_flags = 0;

    DEBUGMSGTL(("access:defaultrouter:container", "load\n"));

    if (NULL == container) {
        if (load_flags & NETSNMP_ACCESS_DEFAULTROUTER_LOAD_ADDL_IDX_BY_ADDR) {
            container_flags |=
                NETSNMP_ACCESS_DEFAULTROUTER_INIT_ADDL_IDX_BY_ADDR;
        }
        container =
            netsnmp_access_defaultrouter_container_init(container_flags);
    }

    if (NULL == container) {
        snmp_log(LOG_ERR, "no container specified/found for access_defaultrouter\n");
        return NULL;
    }

    rc =  netsnmp_arch_defaultrouter_container_load(container, load_flags);
    if (0 != rc) {
        netsnmp_access_defaultrouter_container_free(container,
                                    NETSNMP_ACCESS_DEFAULTROUTER_FREE_NOFLAGS);
        container = NULL;
    }

    return container;
}

void
netsnmp_access_defaultrouter_container_free(netsnmp_container *container,
                                            u_int free_flags)
{
    DEBUGMSGTL(("access:defaultrouter:container", "free\n"));

    if (NULL == container) {
        snmp_log(LOG_ERR,
                 "invalid container for netsnmp_access_defaultrouter_free\n");
        return;
    }

    if(! (free_flags & NETSNMP_ACCESS_DEFAULTROUTER_FREE_DONT_CLEAR)) {
        /*
         * free all items.
         */
        CONTAINER_CLEAR(container,
                        (netsnmp_container_obj_func*)_access_defaultrouter_entry_release,
                        NULL);
    }

    if(! (free_flags & NETSNMP_ACCESS_DEFAULTROUTER_FREE_KEEP_CONTAINER))
        CONTAINER_FREE(container);
}

/**---------------------------------------------------------------------*/
/*
 * defaultrouter_entry functions
 */
/**
 */
/**
 */
netsnmp_defaultrouter_entry *
netsnmp_access_defaultrouter_entry_create(void)
{
    int rc = 0;
    netsnmp_defaultrouter_entry *entry =
        SNMP_MALLOC_TYPEDEF(netsnmp_defaultrouter_entry);

    DEBUGMSGTL(("access:defaultrouter:entry", "create\n"));

    if(NULL == entry)
        return NULL;

    entry->oid_index.len = 1;
    entry->oid_index.oids = &entry->ns_dr_index;

    /*
     * set up defaults
     */
    entry->dr_lifetime   = IPDEFAULTROUTERLIFETIME_MAX;
    entry->dr_preference = IPDEFAULTROUTERPREFERENCE_MEDIUM;

    rc = netsnmp_arch_defaultrouter_entry_init(entry);
    if (SNMP_ERR_NOERROR != rc) {
        DEBUGMSGT(("access:defaultrouter:create","error %d in arch init\n", rc));
        netsnmp_access_defaultrouter_entry_free(entry);
        entry = NULL;
    }

    return entry;
}

void
netsnmp_access_defaultrouter_entry_free(netsnmp_defaultrouter_entry * entry)
{
    if (NULL == entry)
        return;

    free(entry);
}

/**
 * update an old defaultrouter_entry from a new one
 *
 * @note: only mib related items are compared. Internal objects
 * such as oid_index, ns_dr_index and flags are not compared.
 *
 * @retval -1  : error
 * @retval >=0 : number of fields updated
 */
int
netsnmp_access_defaultrouter_entry_update(netsnmp_defaultrouter_entry *lhs,
                                      netsnmp_defaultrouter_entry *rhs)
{
    int changed = 0;

    if (lhs->dr_addresstype != rhs->dr_addresstype) {
        ++changed;
        lhs->dr_addresstype = rhs->dr_addresstype;
    }

    if (lhs->dr_address_len != rhs->dr_address_len) {
        changed += 2;
        lhs->dr_address_len = rhs->dr_address_len;
        memcpy(lhs->dr_address, rhs->dr_address, rhs->dr_address_len);
    }
    else if (memcmp(lhs->dr_address, rhs->dr_address, rhs->dr_address_len) != 0) {
        ++changed;
        memcpy(lhs->dr_address, rhs->dr_address, rhs->dr_address_len);
    }

    if (lhs->dr_if_index != rhs->dr_if_index) {
        ++changed;
        lhs->dr_if_index = rhs->dr_if_index;
    }

    if (lhs->dr_lifetime != rhs->dr_lifetime) {
        ++changed;
        lhs->dr_lifetime = rhs->dr_lifetime;
    }

    if (lhs->dr_preference != rhs->dr_preference) {
        ++changed;
        lhs->dr_preference = rhs->dr_preference;
    }

    return changed;
}

/**
 * copy an  defaultrouter_entry
 *
 * @retval -1  : error
 * @retval 0   : no error
 */
int
netsnmp_access_defaultrouter_entry_copy(netsnmp_defaultrouter_entry *lhs,
                                    netsnmp_defaultrouter_entry *rhs)
{
    lhs->dr_addresstype = rhs->dr_addresstype;
    lhs->dr_address_len = rhs->dr_address_len;
    memcpy(lhs->dr_address, rhs->dr_address, rhs->dr_address_len);
    lhs->dr_if_index    = rhs->dr_if_index;
    lhs->dr_lifetime    = rhs->dr_lifetime;
    lhs->dr_preference  = rhs->dr_preference;

    return 0;
}

/**---------------------------------------------------------------------*/
/*
 * Utility routines
 */

/**
 */
void
_access_defaultrouter_entry_release(netsnmp_defaultrouter_entry * entry, void *context)
{
    netsnmp_access_defaultrouter_entry_free(entry);
}

static int _access_defaultrouter_entry_compare_addr(const void *lhs,
                                                const void *rhs)
{
    const netsnmp_defaultrouter_entry *lh = (const netsnmp_defaultrouter_entry *)lhs;
    const netsnmp_defaultrouter_entry *rh = (const netsnmp_defaultrouter_entry *)rhs;

    netsnmp_assert(NULL != lhs);
    netsnmp_assert(NULL != rhs);

    /*
     * compare address length
     */
    if (lh->dr_address_len < rh->dr_address_len)
        return -1;
    else if (lh->dr_address_len > rh->dr_address_len)
        return 1;

    /*
     * length equal, compare address
     */
    return memcmp(lh->dr_address, rh->dr_address, lh->dr_address_len);
}