| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
#include <net-snmp/net-snmp-includes.h> |
| 4 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 5 |
|
| 6 |
#include "sctpTables_common.h" |
| 7 |
#include "sctpAssocTable.h" |
| 8 |
#include "sctpAssocRemAddrTable.h" |
| 9 |
#include "sctpAssocLocalAddrTable.h" |
| 10 |
#include "sctpLookupLocalPortTable.h" |
| 11 |
#include "sctpLookupRemPortTable.h" |
| 12 |
#include "sctpLookupRemHostNameTable.h" |
| 13 |
#include "sctpLookupRemPrimIPAddrTable.h" |
| 14 |
#include "sctpLookupRemIPAddrTable.h" |
| 15 |
|
| 16 |
netsnmp_feature_require(container_lifo); |
| 17 |
|
| 18 |
static void |
| 19 |
sctpAssocTable_collect_invalid(void *what, void *magic) |
| 20 |
{ |
| 21 |
sctpAssocTable_entry *entry = what; |
| 22 |
netsnmp_container *to_delete = magic; |
| 23 |
|
| 24 |
if (entry->valid) |
| 25 |
entry->valid = 0; |
| 26 |
else |
| 27 |
CONTAINER_INSERT(to_delete, entry); |
| 28 |
} |
| 29 |
|
| 30 |
/* |
| 31 |
* Remove all entries from sctpAssocTable, which are not marked as valid. |
| 32 |
* All valid entries are then marked as invalid (to delete them in next cache |
| 33 |
* load, if the entry is not updated). |
| 34 |
*/ |
| 35 |
void |
| 36 |
sctpAssocTable_delete_invalid(netsnmp_container *assocTable) |
| 37 |
{ |
| 38 |
netsnmp_container *to_delete = netsnmp_container_find("lifo"); |
| 39 |
|
| 40 |
CONTAINER_FOR_EACH(assocTable, sctpAssocTable_collect_invalid, |
| 41 |
to_delete); |
| 42 |
|
| 43 |
while (CONTAINER_SIZE(to_delete)) { |
| 44 |
sctpAssocTable_entry *entry = CONTAINER_FIRST(to_delete); |
| 45 |
CONTAINER_REMOVE(assocTable, entry); |
| 46 |
sctpAssocTable_entry_free(entry); |
| 47 |
CONTAINER_REMOVE(to_delete, NULL); |
| 48 |
} |
| 49 |
CONTAINER_FREE(to_delete); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
sctpAssocRemAddrTable_collect_invalid(void *what, void *magic) |
| 54 |
{ |
| 55 |
sctpAssocRemAddrTable_entry *entry = what; |
| 56 |
netsnmp_container *to_delete = magic; |
| 57 |
|
| 58 |
if (entry->valid) |
| 59 |
entry->valid = 0; |
| 60 |
else |
| 61 |
CONTAINER_INSERT(to_delete, entry); |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* Remove all entries from sctpAssocRemAddrTable, which are not marked as valid. |
| 66 |
* All valid entries are then marked as invalid (to delete them in next cache |
| 67 |
* load, if the entry is not updated). |
| 68 |
*/ |
| 69 |
void |
| 70 |
sctpAssocRemAddrTable_delete_invalid(netsnmp_container *remAddrTable) |
| 71 |
{ |
| 72 |
netsnmp_container *to_delete = netsnmp_container_find("lifo"); |
| 73 |
|
| 74 |
CONTAINER_FOR_EACH(remAddrTable, sctpAssocRemAddrTable_collect_invalid, |
| 75 |
to_delete); |
| 76 |
|
| 77 |
while (CONTAINER_SIZE(to_delete)) { |
| 78 |
sctpAssocRemAddrTable_entry *entry = CONTAINER_FIRST(to_delete); |
| 79 |
CONTAINER_REMOVE(remAddrTable, entry); |
| 80 |
sctpAssocRemAddrTable_entry_free(entry); |
| 81 |
CONTAINER_REMOVE(to_delete, NULL); |
| 82 |
} |
| 83 |
CONTAINER_FREE(to_delete); |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
sctpAssocLocalAddrTable_collect_invalid(void *what, void *magic) |
| 88 |
{ |
| 89 |
sctpAssocLocalAddrTable_entry *entry = what; |
| 90 |
netsnmp_container *to_delete = magic; |
| 91 |
|
| 92 |
if (entry->valid) |
| 93 |
entry->valid = 0; |
| 94 |
else |
| 95 |
CONTAINER_INSERT(to_delete, entry); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Remove all entries from sctpAssocLocalAddrTable, which are not marked as valid. |
| 100 |
* All valid entries are then marked as invalid (to delete them in next cache |
| 101 |
* load, if the entry is not updated). |
| 102 |
*/ |
| 103 |
void |
| 104 |
sctpAssocLocalAddrTable_delete_invalid(netsnmp_container *localAddrTable) |
| 105 |
{ |
| 106 |
netsnmp_container *to_delete = netsnmp_container_find("lifo"); |
| 107 |
|
| 108 |
CONTAINER_FOR_EACH(localAddrTable, |
| 109 |
sctpAssocLocalAddrTable_collect_invalid, to_delete); |
| 110 |
|
| 111 |
while (CONTAINER_SIZE(to_delete)) { |
| 112 |
sctpAssocLocalAddrTable_entry *entry = CONTAINER_FIRST(to_delete); |
| 113 |
CONTAINER_REMOVE(localAddrTable, entry); |
| 114 |
sctpAssocLocalAddrTable_entry_free(entry); |
| 115 |
CONTAINER_REMOVE(to_delete, NULL); |
| 116 |
} |
| 117 |
CONTAINER_FREE(to_delete); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
int |
| 122 |
sctpAssocRemAddrTable_add_or_update(netsnmp_container *remAddrTable, |
| 123 |
sctpAssocRemAddrTable_entry * entry) |
| 124 |
{ |
| 125 |
/* |
| 126 |
* we have full sctpAssocLocalAddrTable entry, update or add it in the container |
| 127 |
*/ |
| 128 |
sctpAssocRemAddrTable_entry *old; |
| 129 |
|
| 130 |
entry->valid = 1; |
| 131 |
|
| 132 |
/* |
| 133 |
* try to find it in the container |
| 134 |
*/ |
| 135 |
sctpAssocRemAddrTable_entry_update_index(entry); |
| 136 |
old = CONTAINER_FIND(remAddrTable, entry); |
| 137 |
|
| 138 |
if (old != NULL) { |
| 139 |
/* |
| 140 |
* update existing entry, don't overwrite the timestamp |
| 141 |
*/ |
| 142 |
time_t timestamp = old->sctpAssocRemAddrStartTime; |
| 143 |
if (timestamp == 0 && entry->sctpAssocRemAddrStartTime == 0) |
| 144 |
timestamp = netsnmp_get_agent_uptime(); /* set the timestamp if it was not set before */ |
| 145 |
sctpAssocRemAddrTable_entry_copy(entry, old); |
| 146 |
old->sctpAssocRemAddrStartTime = timestamp; |
| 147 |
sctpAssocRemAddrTable_entry_free(entry); |
| 148 |
|
| 149 |
} else { |
| 150 |
/* |
| 151 |
* the entry is new, add it there |
| 152 |
*/ |
| 153 |
if (entry->sctpAssocRemAddrStartTime == 0) { |
| 154 |
entry->sctpAssocRemAddrStartTime = netsnmp_get_agent_uptime(); |
| 155 |
} |
| 156 |
CONTAINER_INSERT(remAddrTable, entry); |
| 157 |
} |
| 158 |
|
| 159 |
return SNMP_ERR_NOERROR; |
| 160 |
} |
| 161 |
|
| 162 |
int |
| 163 |
sctpAssocLocalAddrTable_add_or_update(netsnmp_container *localAddrTable, |
| 164 |
sctpAssocLocalAddrTable_entry * |
| 165 |
entry) |
| 166 |
{ |
| 167 |
/* |
| 168 |
* we have full sctpAssocLocalAddrTable entry, update or add it in the container |
| 169 |
*/ |
| 170 |
sctpAssocLocalAddrTable_entry *old; |
| 171 |
|
| 172 |
entry->valid = 1; |
| 173 |
|
| 174 |
/* |
| 175 |
* try to find it in the container |
| 176 |
*/ |
| 177 |
sctpAssocLocalAddrTable_entry_update_index(entry); |
| 178 |
old = CONTAINER_FIND(localAddrTable, entry); |
| 179 |
|
| 180 |
if (old != NULL) { |
| 181 |
/* |
| 182 |
* update existing entry, don't overwrite the timestamp |
| 183 |
*/ |
| 184 |
time_t timestamp = old->sctpAssocLocalAddrStartTime; |
| 185 |
if (timestamp == 0 && entry->sctpAssocLocalAddrStartTime == 0) |
| 186 |
timestamp = netsnmp_get_agent_uptime(); /* set the timestamp if it was not set before */ |
| 187 |
sctpAssocLocalAddrTable_entry_copy(entry, old); |
| 188 |
old->sctpAssocLocalAddrStartTime = timestamp; |
| 189 |
sctpAssocLocalAddrTable_entry_free(entry); |
| 190 |
|
| 191 |
} else { |
| 192 |
/* |
| 193 |
* the entry is new, add it there |
| 194 |
*/ |
| 195 |
if (entry->sctpAssocLocalAddrStartTime == 0) { |
| 196 |
entry->sctpAssocLocalAddrStartTime = |
| 197 |
netsnmp_get_agent_uptime(); |
| 198 |
} |
| 199 |
CONTAINER_INSERT(localAddrTable, entry); |
| 200 |
} |
| 201 |
|
| 202 |
return SNMP_ERR_NOERROR; |
| 203 |
} |
| 204 |
|
| 205 |
int |
| 206 |
sctpAssocTable_add_or_update(netsnmp_container *assocTable, |
| 207 |
sctpAssocTable_entry * entry) |
| 208 |
{ |
| 209 |
/* |
| 210 |
* we have full sctpAssocTable entry, update or add it in the container |
| 211 |
*/ |
| 212 |
sctpAssocTable_entry *old; |
| 213 |
|
| 214 |
entry->valid = 1; |
| 215 |
|
| 216 |
/* |
| 217 |
* try to find it in the container |
| 218 |
*/ |
| 219 |
sctpAssocTable_entry_update_index(entry); |
| 220 |
old = CONTAINER_FIND(assocTable, entry); |
| 221 |
|
| 222 |
if (old != NULL) { |
| 223 |
/* |
| 224 |
* update existing entry, don't overwrite the timestamp |
| 225 |
*/ |
| 226 |
time_t timestamp = old->sctpAssocStartTime; |
| 227 |
if (timestamp == 0 && entry->sctpAssocStartTime == 0 |
| 228 |
&& entry->sctpAssocState >= SCTPASSOCSTATE_ESTABLISHED) |
| 229 |
timestamp = netsnmp_get_agent_uptime(); /* set the timestamp if it was not set before and entry reaches the right state */ |
| 230 |
sctpAssocTable_entry_copy(entry, old); |
| 231 |
old->sctpAssocStartTime = timestamp; |
| 232 |
sctpAssocTable_entry_free(entry); |
| 233 |
|
| 234 |
} else { |
| 235 |
/* |
| 236 |
* the entry is new, add it there |
| 237 |
*/ |
| 238 |
if (entry->sctpAssocStartTime == 0 |
| 239 |
&& entry->sctpAssocState >= SCTPASSOCSTATE_ESTABLISHED) { |
| 240 |
entry->sctpAssocStartTime = netsnmp_get_agent_uptime(); |
| 241 |
} |
| 242 |
CONTAINER_INSERT(assocTable, entry); |
| 243 |
} |
| 244 |
|
| 245 |
return SNMP_ERR_NOERROR; |
| 246 |
} |
| 247 |
|
| 248 |
static void |
| 249 |
sctpTables_add_local_port(sctpAssocTable_entry * assoc, |
| 250 |
sctpTables_containers * containers) |
| 251 |
{ |
| 252 |
sctpLookupLocalPortTable_entry *entry; |
| 253 |
|
| 254 |
entry = sctpLookupLocalPortTable_entry_create(); |
| 255 |
if (entry == NULL) { |
| 256 |
DEBUGMSGTL(("sctp:tables:fill_lookup", |
| 257 |
"cannot create sctpLookupLocalPortTable_entry")); |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
entry->sctpAssocId = assoc->sctpAssocId; |
| 262 |
entry->sctpAssocLocalPort = assoc->sctpAssocLocalPort; |
| 263 |
entry->sctpLookupLocalPortStartTime = assoc->sctpAssocStartTime; |
| 264 |
sctpLookupLocalPortTable_entry_update_index(entry); |
| 265 |
CONTAINER_INSERT(containers->sctpLookupLocalPortTable, entry); |
| 266 |
} |
| 267 |
|
| 268 |
static void |
| 269 |
sctpTables_add_rem_port(sctpAssocTable_entry * assoc, |
| 270 |
sctpTables_containers * containers) |
| 271 |
{ |
| 272 |
sctpLookupRemPortTable_entry *entry; |
| 273 |
|
| 274 |
entry = sctpLookupRemPortTable_entry_create(); |
| 275 |
if (entry == NULL) { |
| 276 |
DEBUGMSGTL(("sctp:tables:fill_lookup", |
| 277 |
"cannot create sctpLookupRemPortTable_entry")); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
entry->sctpAssocId = assoc->sctpAssocId; |
| 282 |
entry->sctpAssocRemPort = assoc->sctpAssocRemPort; |
| 283 |
entry->sctpLookupRemPortStartTime = assoc->sctpAssocStartTime; |
| 284 |
sctpLookupRemPortTable_entry_update_index(entry); |
| 285 |
CONTAINER_INSERT(containers->sctpLookupRemPortTable, entry); |
| 286 |
} |
| 287 |
|
| 288 |
static void |
| 289 |
sctpTables_add_rem_hostname(sctpAssocTable_entry * assoc, |
| 290 |
sctpTables_containers * containers) |
| 291 |
{ |
| 292 |
sctpLookupRemHostNameTable_entry *entry; |
| 293 |
|
| 294 |
if (assoc->sctpAssocRemHostName_len == 0) |
| 295 |
return; /* the association does not know its hostname */ |
| 296 |
|
| 297 |
entry = sctpLookupRemHostNameTable_entry_create(); |
| 298 |
if (entry == NULL) { |
| 299 |
DEBUGMSGTL(("sctp:tables:fill_lookup", |
| 300 |
"cannot create sctpLookupRemHostNameTable_entry")); |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
entry->sctpAssocId = assoc->sctpAssocId; |
| 305 |
entry->sctpAssocRemHostName_len = assoc->sctpAssocRemHostName_len; |
| 306 |
memcpy(entry->sctpAssocRemHostName, assoc->sctpAssocRemHostName, |
| 307 |
assoc->sctpAssocRemHostName_len); |
| 308 |
entry->sctpLookupRemHostNameStartTime = assoc->sctpAssocStartTime; |
| 309 |
|
| 310 |
sctpLookupRemHostNameTable_entry_update_index(entry); |
| 311 |
CONTAINER_INSERT(containers->sctpLookupRemHostNameTable, entry); |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
sctpTables_add_rem_prim_ip_addr(sctpAssocTable_entry * assoc, |
| 316 |
sctpTables_containers * containers) |
| 317 |
{ |
| 318 |
sctpLookupRemPrimIPAddrTable_entry *entry; |
| 319 |
|
| 320 |
entry = sctpLookupRemPrimIPAddrTable_entry_create(); |
| 321 |
if (entry == NULL) { |
| 322 |
DEBUGMSGTL(("sctp:tables:fill_lookup", |
| 323 |
"cannot create sctpLookupRemPrimIPAddrTable_entry")); |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
entry->sctpAssocId = assoc->sctpAssocId; |
| 328 |
entry->sctpAssocRemPrimAddrType = assoc->sctpAssocRemPrimAddrType; |
| 329 |
entry->sctpAssocRemPrimAddr_len = assoc->sctpAssocRemPrimAddr_len; |
| 330 |
memcpy(entry->sctpAssocRemPrimAddr, assoc->sctpAssocRemPrimAddr, |
| 331 |
assoc->sctpAssocRemPrimAddr_len); |
| 332 |
entry->sctpLookupRemPrimIPAddrStartTime = assoc->sctpAssocStartTime; |
| 333 |
|
| 334 |
sctpLookupRemPrimIPAddrTable_entry_update_index(entry); |
| 335 |
CONTAINER_INSERT(containers->sctpLookupRemPrimIPAddrTable, entry); |
| 336 |
} |
| 337 |
|
| 338 |
static void |
| 339 |
sctpTables_fill_lookup_assoc(void *what, void *magic) |
| 340 |
{ |
| 341 |
sctpAssocTable_entry *entry = what; |
| 342 |
sctpTables_containers *containers = magic; |
| 343 |
|
| 344 |
sctpTables_add_local_port(entry, containers); |
| 345 |
sctpTables_add_rem_port(entry, containers); |
| 346 |
sctpTables_add_rem_hostname(entry, containers); |
| 347 |
sctpTables_add_rem_prim_ip_addr(entry, containers); |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
sctpTables_add_rem_ip_addr(sctpAssocRemAddrTable_entry * rem_addr, |
| 352 |
sctpTables_containers * containers) |
| 353 |
{ |
| 354 |
sctpLookupRemIPAddrTable_entry *entry; |
| 355 |
|
| 356 |
entry = sctpLookupRemIPAddrTable_entry_create(); |
| 357 |
if (entry == NULL) { |
| 358 |
DEBUGMSGTL(("sctp:tables:fill_lookup", |
| 359 |
"cannot create sctpLookupRemIPAddrTable_entry")); |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
entry->sctpAssocId = rem_addr->sctpAssocId; |
| 364 |
entry->sctpAssocRemAddrType = rem_addr->sctpAssocRemAddrType; |
| 365 |
entry->sctpAssocRemAddr_len = rem_addr->sctpAssocRemAddr_len; |
| 366 |
memcpy(entry->sctpAssocRemAddr, rem_addr->sctpAssocRemAddr, |
| 367 |
rem_addr->sctpAssocRemAddr_len); |
| 368 |
entry->sctpLookupRemIPAddrStartTime = |
| 369 |
rem_addr->sctpAssocRemAddrStartTime; |
| 370 |
|
| 371 |
sctpLookupRemIPAddrTable_entry_update_index(entry); |
| 372 |
CONTAINER_INSERT(containers->sctpLookupRemIPAddrTable, entry); |
| 373 |
} |
| 374 |
|
| 375 |
static void |
| 376 |
sctpTables_fill_lookup_rem_addr(void *what, void *magic) |
| 377 |
{ |
| 378 |
sctpAssocRemAddrTable_entry *entry = what; |
| 379 |
sctpTables_containers *containers = magic; |
| 380 |
sctpTables_add_rem_ip_addr(entry, containers); |
| 381 |
} |
| 382 |
|
| 383 |
int |
| 384 |
sctpTables_fill_lookup(sctpTables_containers * containers) |
| 385 |
{ |
| 386 |
/* |
| 387 |
* clear all the lookup tables |
| 388 |
*/ |
| 389 |
sctpLookupLocalPortTable_container_clear(containers-> |
| 390 |
sctpLookupLocalPortTable); |
| 391 |
sctpLookupRemPortTable_container_clear(containers-> |
| 392 |
sctpLookupRemPortTable); |
| 393 |
sctpLookupRemHostNameTable_container_clear(containers-> |
| 394 |
sctpLookupRemHostNameTable); |
| 395 |
sctpLookupRemPrimIPAddrTable_container_clear(containers-> |
| 396 |
sctpLookupRemPrimIPAddrTable); |
| 397 |
sctpLookupRemIPAddrTable_container_clear(containers-> |
| 398 |
sctpLookupRemIPAddrTable); |
| 399 |
|
| 400 |
/* |
| 401 |
* fill the lookup tables |
| 402 |
*/ |
| 403 |
CONTAINER_FOR_EACH(containers->sctpAssocTable, |
| 404 |
sctpTables_fill_lookup_assoc, containers); |
| 405 |
CONTAINER_FOR_EACH(containers->sctpAssocRemAddrTable, |
| 406 |
sctpTables_fill_lookup_rem_addr, containers); |
| 407 |
|
| 408 |
return SNMP_ERR_NOERROR; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
int |
| 414 |
sctpTables_load(void) |
| 415 |
{ |
| 416 |
sctpTables_containers containers; |
| 417 |
int ret; |
| 418 |
u_long flags = 0; |
| 419 |
|
| 420 |
containers.sctpAssocTable = sctpAssocTable_get_container(); |
| 421 |
containers.sctpAssocRemAddrTable = |
| 422 |
sctpAssocRemAddrTable_get_container(); |
| 423 |
containers.sctpAssocLocalAddrTable = |
| 424 |
sctpAssocLocalAddrTable_get_container(); |
| 425 |
containers.sctpLookupLocalPortTable = |
| 426 |
sctpLookupLocalPortTable_get_container(); |
| 427 |
containers.sctpLookupRemPortTable = |
| 428 |
sctpLookupRemPortTable_get_container(); |
| 429 |
containers.sctpLookupRemHostNameTable = |
| 430 |
sctpLookupRemHostNameTable_get_container(); |
| 431 |
containers.sctpLookupRemPrimIPAddrTable = |
| 432 |
sctpLookupRemPrimIPAddrTable_get_container(); |
| 433 |
containers.sctpLookupRemIPAddrTable = |
| 434 |
sctpLookupRemIPAddrTable_get_container(); |
| 435 |
|
| 436 |
ret = sctpTables_arch_load(&containers, &flags); |
| 437 |
|
| 438 |
if (flags & SCTP_TABLES_LOAD_FLAG_DELETE_INVALID) { |
| 439 |
sctpAssocTable_delete_invalid(containers.sctpAssocTable); |
| 440 |
sctpAssocRemAddrTable_delete_invalid(containers. |
| 441 |
sctpAssocRemAddrTable); |
| 442 |
sctpAssocLocalAddrTable_delete_invalid(containers. |
| 443 |
sctpAssocLocalAddrTable); |
| 444 |
} |
| 445 |
|
| 446 |
if (flags & SCTP_TABLES_LOAD_FLAG_AUTO_LOOKUP) { |
| 447 |
ret = sctpTables_fill_lookup(&containers); |
| 448 |
} |
| 449 |
|
| 450 |
return ret; |
| 451 |
} |