summaryrefslogtreecommitdiff
path: root/agent/mibgroup/sctp-mib/sctpTables_common.c
blob: 21ced9050ed0f29d4ae71e602f27bc1e09b57e26 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include "sctpTables_common.h"
#include "sctpAssocTable.h"
#include "sctpAssocRemAddrTable.h"
#include "sctpAssocLocalAddrTable.h"
#include "sctpLookupLocalPortTable.h"
#include "sctpLookupRemPortTable.h"
#include "sctpLookupRemHostNameTable.h"
#include "sctpLookupRemPrimIPAddrTable.h"
#include "sctpLookupRemIPAddrTable.h"

static void
sctpAssocTable_collect_invalid(void *what, void *magic)
{
    sctpAssocTable_entry *entry = what;
    netsnmp_container *to_delete = magic;

    if (entry->valid)
        entry->valid = 0;
    else
        CONTAINER_INSERT(to_delete, entry);
}

/*
 * Remove all entries from sctpAssocTable, which are not marked as valid.
 * All valid entries are then marked as invalid (to delete them in next cache
 * load, if the entry is not updated). 
 */
void
sctpAssocTable_delete_invalid(netsnmp_container *assocTable)
{
    netsnmp_container *to_delete = netsnmp_container_find("lifo");

    CONTAINER_FOR_EACH(assocTable, sctpAssocTable_collect_invalid,
                       to_delete);

    while (CONTAINER_SIZE(to_delete)) {
        sctpAssocTable_entry *entry = CONTAINER_FIRST(to_delete);
        CONTAINER_REMOVE(assocTable, entry);
        sctpAssocTable_entry_free(entry);
        CONTAINER_REMOVE(to_delete, NULL);
    }
    CONTAINER_FREE(to_delete);
}

static void
sctpAssocRemAddrTable_collect_invalid(void *what, void *magic)
{
    sctpAssocRemAddrTable_entry *entry = what;
    netsnmp_container *to_delete = magic;

    if (entry->valid)
        entry->valid = 0;
    else
        CONTAINER_INSERT(to_delete, entry);
}

/*
 * Remove all entries from sctpAssocRemAddrTable, which are not marked as valid.
 * All valid entries are then marked as invalid (to delete them in next cache
 * load, if the entry is not updated). 
 */
void
sctpAssocRemAddrTable_delete_invalid(netsnmp_container *remAddrTable)
{
    netsnmp_container *to_delete = netsnmp_container_find("lifo");

    CONTAINER_FOR_EACH(remAddrTable, sctpAssocRemAddrTable_collect_invalid,
                       to_delete);

    while (CONTAINER_SIZE(to_delete)) {
        sctpAssocRemAddrTable_entry *entry = CONTAINER_FIRST(to_delete);
        CONTAINER_REMOVE(remAddrTable, entry);
        sctpAssocRemAddrTable_entry_free(entry);
        CONTAINER_REMOVE(to_delete, NULL);
    }
    CONTAINER_FREE(to_delete);
}

static void
sctpAssocLocalAddrTable_collect_invalid(void *what, void *magic)
{
    sctpAssocLocalAddrTable_entry *entry = what;
    netsnmp_container *to_delete = magic;

    if (entry->valid)
        entry->valid = 0;
    else
        CONTAINER_INSERT(to_delete, entry);
}

/*
 * Remove all entries from sctpAssocLocalAddrTable, which are not marked as valid.
 * All valid entries are then marked as invalid (to delete them in next cache
 * load, if the entry is not updated). 
 */
void
sctpAssocLocalAddrTable_delete_invalid(netsnmp_container *localAddrTable)
{
    netsnmp_container *to_delete = netsnmp_container_find("lifo");

    CONTAINER_FOR_EACH(localAddrTable,
                       sctpAssocLocalAddrTable_collect_invalid, to_delete);

    while (CONTAINER_SIZE(to_delete)) {
        sctpAssocLocalAddrTable_entry *entry = CONTAINER_FIRST(to_delete);
        CONTAINER_REMOVE(localAddrTable, entry);
        sctpAssocLocalAddrTable_entry_free(entry);
        CONTAINER_REMOVE(to_delete, NULL);
    }
    CONTAINER_FREE(to_delete);
}


int
sctpAssocRemAddrTable_add_or_update(netsnmp_container *remAddrTable,
                                    sctpAssocRemAddrTable_entry * entry)
{
    /*
     * we have full sctpAssocLocalAddrTable entry, update or add it in the container 
     */
    sctpAssocRemAddrTable_entry *old;

    entry->valid = 1;

    /*
     * try to find it in the container 
     */
    sctpAssocRemAddrTable_entry_update_index(entry);
    old = CONTAINER_FIND(remAddrTable, entry);

    if (old != NULL) {
        /*
         * update existing entry, don't overwrite the timestamp 
         */
        time_t          timestamp = old->sctpAssocRemAddrStartTime;
        if (timestamp == 0 && entry->sctpAssocRemAddrStartTime == 0)
            timestamp = netsnmp_get_agent_uptime();     /* set the timestamp if it was not set before */
        sctpAssocRemAddrTable_entry_copy(entry, old);
        old->sctpAssocRemAddrStartTime = timestamp;
        sctpAssocRemAddrTable_entry_free(entry);

    } else {
        /*
         * the entry is new, add it there 
         */
        if (entry->sctpAssocRemAddrStartTime == 0) {
            entry->sctpAssocRemAddrStartTime = netsnmp_get_agent_uptime();
        }
        CONTAINER_INSERT(remAddrTable, entry);
    }

    return SNMP_ERR_NOERROR;
}

int
sctpAssocLocalAddrTable_add_or_update(netsnmp_container *localAddrTable,
                                      sctpAssocLocalAddrTable_entry *
                                      entry)
{
    /*
     * we have full sctpAssocLocalAddrTable entry, update or add it in the container 
     */
    sctpAssocLocalAddrTable_entry *old;

    entry->valid = 1;

    /*
     * try to find it in the container 
     */
    sctpAssocLocalAddrTable_entry_update_index(entry);
    old = CONTAINER_FIND(localAddrTable, entry);

    if (old != NULL) {
        /*
         * update existing entry, don't overwrite the timestamp
         */
        time_t          timestamp = old->sctpAssocLocalAddrStartTime;
        if (timestamp == 0 && entry->sctpAssocLocalAddrStartTime == 0)
            timestamp = netsnmp_get_agent_uptime();     /* set the timestamp if it was not set before */
        sctpAssocLocalAddrTable_entry_copy(entry, old);
        old->sctpAssocLocalAddrStartTime = timestamp;
        sctpAssocLocalAddrTable_entry_free(entry);

    } else {
        /*
         * the entry is new, add it there 
         */
        if (entry->sctpAssocLocalAddrStartTime == 0) {
            entry->sctpAssocLocalAddrStartTime =
                netsnmp_get_agent_uptime();
        }
        CONTAINER_INSERT(localAddrTable, entry);
    }

    return SNMP_ERR_NOERROR;
}

int
sctpAssocTable_add_or_update(netsnmp_container *assocTable,
                             sctpAssocTable_entry * entry)
{
    /*
     * we have full sctpAssocTable entry, update or add it in the container 
     */
    sctpAssocTable_entry *old;

    entry->valid = 1;

    /*
     * try to find it in the container 
     */
    sctpAssocTable_entry_update_index(entry);
    old = CONTAINER_FIND(assocTable, entry);

    if (old != NULL) {
        /*
         * update existing entry, don't overwrite the timestamp
         */
        time_t          timestamp = old->sctpAssocStartTime;
        if (timestamp == 0 && entry->sctpAssocStartTime == 0
            && entry->sctpAssocState >= SCTPASSOCSTATE_ESTABLISHED)
            timestamp = netsnmp_get_agent_uptime();     /* set the timestamp if it was not set before and entry reaches the right state */
        sctpAssocTable_entry_copy(entry, old);
        old->sctpAssocStartTime = timestamp;
        sctpAssocTable_entry_free(entry);

    } else {
        /*
         * the entry is new, add it there 
         */
        if (entry->sctpAssocStartTime == 0
            && entry->sctpAssocState >= SCTPASSOCSTATE_ESTABLISHED) {
            entry->sctpAssocStartTime = netsnmp_get_agent_uptime();
        }
        CONTAINER_INSERT(assocTable, entry);
    }

    return SNMP_ERR_NOERROR;
}

static void
sctpTables_add_local_port(sctpAssocTable_entry * assoc,
                          sctpTables_containers * containers)
{
    sctpLookupLocalPortTable_entry *entry;

    entry = sctpLookupLocalPortTable_entry_create();
    if (entry == NULL) {
        DEBUGMSGTL(("sctp:tables:fill_lookup",
                    "cannot create sctpLookupLocalPortTable_entry"));
        return;
    }

    entry->sctpAssocId = assoc->sctpAssocId;
    entry->sctpAssocLocalPort = assoc->sctpAssocLocalPort;
    entry->sctpLookupLocalPortStartTime = assoc->sctpAssocStartTime;
    sctpLookupLocalPortTable_entry_update_index(entry);
    CONTAINER_INSERT(containers->sctpLookupLocalPortTable, entry);
}

static void
sctpTables_add_rem_port(sctpAssocTable_entry * assoc,
                        sctpTables_containers * containers)
{
    sctpLookupRemPortTable_entry *entry;

    entry = sctpLookupRemPortTable_entry_create();
    if (entry == NULL) {
        DEBUGMSGTL(("sctp:tables:fill_lookup",
                    "cannot create sctpLookupRemPortTable_entry"));
        return;
    }

    entry->sctpAssocId = assoc->sctpAssocId;
    entry->sctpAssocRemPort = assoc->sctpAssocRemPort;
    entry->sctpLookupRemPortStartTime = assoc->sctpAssocStartTime;
    sctpLookupRemPortTable_entry_update_index(entry);
    CONTAINER_INSERT(containers->sctpLookupRemPortTable, entry);
}

static void
sctpTables_add_rem_hostname(sctpAssocTable_entry * assoc,
                            sctpTables_containers * containers)
{
    sctpLookupRemHostNameTable_entry *entry;

    if (assoc->sctpAssocRemHostName_len == 0)
        return;                 /* the association does not know its hostname */

    entry = sctpLookupRemHostNameTable_entry_create();
    if (entry == NULL) {
        DEBUGMSGTL(("sctp:tables:fill_lookup",
                    "cannot create sctpLookupRemHostNameTable_entry"));
        return;
    }

    entry->sctpAssocId = assoc->sctpAssocId;
    entry->sctpAssocRemHostName_len = assoc->sctpAssocRemHostName_len;
    memcpy(entry->sctpAssocRemHostName, assoc->sctpAssocRemHostName,
           assoc->sctpAssocRemHostName_len);
    entry->sctpLookupRemHostNameStartTime = assoc->sctpAssocStartTime;

    sctpLookupRemHostNameTable_entry_update_index(entry);
    CONTAINER_INSERT(containers->sctpLookupRemHostNameTable, entry);
}

static void
sctpTables_add_rem_prim_ip_addr(sctpAssocTable_entry * assoc,
                                sctpTables_containers * containers)
{
    sctpLookupRemPrimIPAddrTable_entry *entry;

    entry = sctpLookupRemPrimIPAddrTable_entry_create();
    if (entry == NULL) {
        DEBUGMSGTL(("sctp:tables:fill_lookup",
                    "cannot create sctpLookupRemPrimIPAddrTable_entry"));
        return;
    }

    entry->sctpAssocId = assoc->sctpAssocId;
    entry->sctpAssocRemPrimAddrType = assoc->sctpAssocRemPrimAddrType;
    entry->sctpAssocRemPrimAddr_len = assoc->sctpAssocRemPrimAddr_len;
    memcpy(entry->sctpAssocRemPrimAddr, assoc->sctpAssocRemPrimAddr,
           assoc->sctpAssocRemPrimAddr_len);
    entry->sctpLookupRemPrimIPAddrStartTime = assoc->sctpAssocStartTime;

    sctpLookupRemPrimIPAddrTable_entry_update_index(entry);
    CONTAINER_INSERT(containers->sctpLookupRemPrimIPAddrTable, entry);
}

static void
sctpTables_fill_lookup_assoc(void *what, void *magic)
{
    sctpAssocTable_entry *entry = what;
    sctpTables_containers *containers = magic;

    sctpTables_add_local_port(entry, containers);
    sctpTables_add_rem_port(entry, containers);
    sctpTables_add_rem_hostname(entry, containers);
    sctpTables_add_rem_prim_ip_addr(entry, containers);
}

static void
sctpTables_add_rem_ip_addr(sctpAssocRemAddrTable_entry * rem_addr,
                           sctpTables_containers * containers)
{
    sctpLookupRemIPAddrTable_entry *entry;

    entry = sctpLookupRemIPAddrTable_entry_create();
    if (entry == NULL) {
        DEBUGMSGTL(("sctp:tables:fill_lookup",
                    "cannot create sctpLookupRemIPAddrTable_entry"));
        return;
    }

    entry->sctpAssocId = rem_addr->sctpAssocId;
    entry->sctpAssocRemAddrType = rem_addr->sctpAssocRemAddrType;
    entry->sctpAssocRemAddr_len = rem_addr->sctpAssocRemAddr_len;
    memcpy(entry->sctpAssocRemAddr, rem_addr->sctpAssocRemAddr,
           rem_addr->sctpAssocRemAddr_len);
    entry->sctpLookupRemIPAddrStartTime =
        rem_addr->sctpAssocRemAddrStartTime;

    sctpLookupRemIPAddrTable_entry_update_index(entry);
    CONTAINER_INSERT(containers->sctpLookupRemIPAddrTable, entry);
}

static void
sctpTables_fill_lookup_rem_addr(void *what, void *magic)
{
    sctpAssocRemAddrTable_entry *entry = what;
    sctpTables_containers *containers = magic;
    sctpTables_add_rem_ip_addr(entry, containers);
}

int
sctpTables_fill_lookup(sctpTables_containers * containers)
{
    /*
     * clear all the lookup tables 
     */
    sctpLookupLocalPortTable_container_clear(containers->
                                             sctpLookupLocalPortTable);
    sctpLookupRemPortTable_container_clear(containers->
                                           sctpLookupRemPortTable);
    sctpLookupRemHostNameTable_container_clear(containers->
                                               sctpLookupRemHostNameTable);
    sctpLookupRemPrimIPAddrTable_container_clear(containers->
                                                 sctpLookupRemPrimIPAddrTable);
    sctpLookupRemIPAddrTable_container_clear(containers->
                                             sctpLookupRemIPAddrTable);

    /*
     * fill the lookup tables 
     */
    CONTAINER_FOR_EACH(containers->sctpAssocTable,
                       sctpTables_fill_lookup_assoc, containers);
    CONTAINER_FOR_EACH(containers->sctpAssocRemAddrTable,
                       sctpTables_fill_lookup_rem_addr, containers);

    return SNMP_ERR_NOERROR;
}



int
sctpTables_load(void)
{
    sctpTables_containers containers;
    int             ret;
    u_long          flags = 0;

    containers.sctpAssocTable = sctpAssocTable_get_container();
    containers.sctpAssocRemAddrTable =
        sctpAssocRemAddrTable_get_container();
    containers.sctpAssocLocalAddrTable =
        sctpAssocLocalAddrTable_get_container();
    containers.sctpLookupLocalPortTable =
        sctpLookupLocalPortTable_get_container();
    containers.sctpLookupRemPortTable =
        sctpLookupRemPortTable_get_container();
    containers.sctpLookupRemHostNameTable =
        sctpLookupRemHostNameTable_get_container();
    containers.sctpLookupRemPrimIPAddrTable =
        sctpLookupRemPrimIPAddrTable_get_container();
    containers.sctpLookupRemIPAddrTable =
        sctpLookupRemIPAddrTable_get_container();

    ret = sctpTables_arch_load(&containers, &flags);

    if (flags | SCTP_TABLES_LOAD_FLAG_DELETE_INVALID) {
        sctpAssocTable_delete_invalid(containers.sctpAssocTable);
        sctpAssocRemAddrTable_delete_invalid(containers.
                                             sctpAssocRemAddrTable);
        sctpAssocLocalAddrTable_delete_invalid(containers.
                                               sctpAssocLocalAddrTable);
    }

    if (flags | SCTP_TABLES_LOAD_FLAG_AUTO_LOOKUP) {
        ret = sctpTables_fill_lookup(&containers);
    }

    return ret;
}