| 1 |
/* |
| 2 |
* defaultrouter MIB architecture support |
| 3 |
* |
| 4 |
* $Id:$ |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
|
| 9 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 10 |
#include <net-snmp/data_access/defaultrouter.h> |
| 11 |
|
| 12 |
#include "ip-mib/ipDefaultRouterTable/ipDefaultRouterTable.h" |
| 13 |
#include "defaultrouter.h" |
| 14 |
#include "defaultrouter_private.h" |
| 15 |
|
| 16 |
/**---------------------------------------------------------------------*/ |
| 17 |
/* |
| 18 |
* local static prototypes |
| 19 |
*/ |
| 20 |
static int _access_defaultrouter_entry_compare_addr(const void *lhs, |
| 21 |
const void *rhs); |
| 22 |
static void _access_defaultrouter_entry_release(void *entry, void *unused); |
| 23 |
|
| 24 |
/**---------------------------------------------------------------------*/ |
| 25 |
/* |
| 26 |
* container functions |
| 27 |
*/ |
| 28 |
/** |
| 29 |
*/ |
| 30 |
netsnmp_container * |
| 31 |
netsnmp_access_defaultrouter_container_init(u_int flags) |
| 32 |
{ |
| 33 |
netsnmp_container *container1; |
| 34 |
|
| 35 |
DEBUGMSGTL(("access:defaultrouter:container", "init\n")); |
| 36 |
|
| 37 |
/* |
| 38 |
* create the containers. one indexed by ifIndex, the other |
| 39 |
* indexed by ifName. |
| 40 |
*/ |
| 41 |
container1 = netsnmp_container_find("access_defaultrouter:table_container"); |
| 42 |
if (NULL == container1) { |
| 43 |
snmp_log(LOG_ERR, "defaultrouter primary container is not found\n"); |
| 44 |
return NULL; |
| 45 |
} |
| 46 |
container1->container_name = strdup("dr_index"); |
| 47 |
|
| 48 |
if (flags & NETSNMP_ACCESS_DEFAULTROUTER_INIT_ADDL_IDX_BY_ADDR) { |
| 49 |
netsnmp_container *container2 = |
| 50 |
netsnmp_container_find("defaultrouter_addr:access_defaultrouter:table_container"); |
| 51 |
if (NULL == container2) { |
| 52 |
snmp_log(LOG_ERR, "defaultrouter secondary container not found\n"); |
| 53 |
CONTAINER_FREE(container1); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
container2->compare = _access_defaultrouter_entry_compare_addr; |
| 58 |
container2->container_name = strdup("dr_addr"); |
| 59 |
|
| 60 |
netsnmp_container_add_index(container1, container2); |
| 61 |
} |
| 62 |
|
| 63 |
return container1; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @retval NULL error |
| 68 |
* @retval !NULL pointer to container |
| 69 |
*/ |
| 70 |
netsnmp_container* |
| 71 |
netsnmp_access_defaultrouter_container_load(netsnmp_container* container, |
| 72 |
u_int load_flags) |
| 73 |
{ |
| 74 |
int rc; |
| 75 |
u_int container_flags = 0; |
| 76 |
|
| 77 |
DEBUGMSGTL(("access:defaultrouter:container", "load\n")); |
| 78 |
|
| 79 |
if (NULL == container) { |
| 80 |
if (load_flags & NETSNMP_ACCESS_DEFAULTROUTER_LOAD_ADDL_IDX_BY_ADDR) { |
| 81 |
container_flags |= |
| 82 |
NETSNMP_ACCESS_DEFAULTROUTER_INIT_ADDL_IDX_BY_ADDR; |
| 83 |
} |
| 84 |
container = |
| 85 |
netsnmp_access_defaultrouter_container_init(container_flags); |
| 86 |
} |
| 87 |
|
| 88 |
if (NULL == container) { |
| 89 |
snmp_log(LOG_ERR, "no container specified/found for access_defaultrouter\n"); |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
rc = netsnmp_arch_defaultrouter_container_load(container, load_flags); |
| 94 |
if (0 != rc) { |
| 95 |
netsnmp_access_defaultrouter_container_free(container, |
| 96 |
NETSNMP_ACCESS_DEFAULTROUTER_FREE_NOFLAGS); |
| 97 |
container = NULL; |
| 98 |
} |
| 99 |
|
| 100 |
return container; |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
netsnmp_access_defaultrouter_container_free(netsnmp_container *container, |
| 105 |
u_int free_flags) |
| 106 |
{ |
| 107 |
DEBUGMSGTL(("access:defaultrouter:container", "free\n")); |
| 108 |
|
| 109 |
if (NULL == container) { |
| 110 |
snmp_log(LOG_ERR, |
| 111 |
"invalid container for netsnmp_access_defaultrouter_free\n"); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
if(! (free_flags & NETSNMP_ACCESS_DEFAULTROUTER_FREE_DONT_CLEAR)) { |
| 116 |
/* |
| 117 |
* free all items. |
| 118 |
*/ |
| 119 |
CONTAINER_CLEAR(container, _access_defaultrouter_entry_release, NULL); |
| 120 |
} |
| 121 |
|
| 122 |
if(! (free_flags & NETSNMP_ACCESS_DEFAULTROUTER_FREE_KEEP_CONTAINER)) |
| 123 |
CONTAINER_FREE(container); |
| 124 |
} |
| 125 |
|
| 126 |
/**---------------------------------------------------------------------*/ |
| 127 |
/* |
| 128 |
* defaultrouter_entry functions |
| 129 |
*/ |
| 130 |
/** |
| 131 |
*/ |
| 132 |
/** |
| 133 |
*/ |
| 134 |
netsnmp_defaultrouter_entry * |
| 135 |
netsnmp_access_defaultrouter_entry_create(void) |
| 136 |
{ |
| 137 |
int rc = 0; |
| 138 |
netsnmp_defaultrouter_entry *entry = |
| 139 |
SNMP_MALLOC_TYPEDEF(netsnmp_defaultrouter_entry); |
| 140 |
|
| 141 |
DEBUGMSGTL(("access:defaultrouter:entry", "create\n")); |
| 142 |
|
| 143 |
if(NULL == entry) |
| 144 |
return NULL; |
| 145 |
|
| 146 |
entry->oid_index.len = 1; |
| 147 |
entry->oid_index.oids = &entry->ns_dr_index; |
| 148 |
|
| 149 |
/* |
| 150 |
* set up defaults |
| 151 |
*/ |
| 152 |
entry->dr_lifetime = IPDEFAULTROUTERLIFETIME_MAX; |
| 153 |
entry->dr_preference = IPDEFAULTROUTERPREFERENCE_MEDIUM; |
| 154 |
|
| 155 |
rc = netsnmp_arch_defaultrouter_entry_init(entry); |
| 156 |
if (SNMP_ERR_NOERROR != rc) { |
| 157 |
DEBUGMSGT(("access:defaultrouter:create","error %d in arch init\n", rc)); |
| 158 |
netsnmp_access_defaultrouter_entry_free(entry); |
| 159 |
entry = NULL; |
| 160 |
} |
| 161 |
|
| 162 |
return entry; |
| 163 |
} |
| 164 |
|
| 165 |
void |
| 166 |
netsnmp_access_defaultrouter_entry_free(netsnmp_defaultrouter_entry * entry) |
| 167 |
{ |
| 168 |
if (NULL == entry) |
| 169 |
return; |
| 170 |
|
| 171 |
free(entry); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* update an old defaultrouter_entry from a new one |
| 176 |
* |
| 177 |
* @note: only mib related items are compared. Internal objects |
| 178 |
* such as oid_index, ns_dr_index and flags are not compared. |
| 179 |
* |
| 180 |
* @retval -1 : error |
| 181 |
* @retval >=0 : number of fields updated |
| 182 |
*/ |
| 183 |
int |
| 184 |
netsnmp_access_defaultrouter_entry_update(netsnmp_defaultrouter_entry *lhs, |
| 185 |
netsnmp_defaultrouter_entry *rhs) |
| 186 |
{ |
| 187 |
int changed = 0; |
| 188 |
|
| 189 |
if (lhs->dr_addresstype != rhs->dr_addresstype) { |
| 190 |
++changed; |
| 191 |
lhs->dr_addresstype = rhs->dr_addresstype; |
| 192 |
} |
| 193 |
|
| 194 |
if (lhs->dr_address_len != rhs->dr_address_len) { |
| 195 |
changed += 2; |
| 196 |
lhs->dr_address_len = rhs->dr_address_len; |
| 197 |
memcpy(lhs->dr_address, rhs->dr_address, rhs->dr_address_len); |
| 198 |
} |
| 199 |
else if (memcmp(lhs->dr_address, rhs->dr_address, rhs->dr_address_len) != 0) { |
| 200 |
++changed; |
| 201 |
memcpy(lhs->dr_address, rhs->dr_address, rhs->dr_address_len); |
| 202 |
} |
| 203 |
|
| 204 |
if (lhs->dr_if_index != rhs->dr_if_index) { |
| 205 |
++changed; |
| 206 |
lhs->dr_if_index = rhs->dr_if_index; |
| 207 |
} |
| 208 |
|
| 209 |
if (lhs->dr_lifetime != rhs->dr_lifetime) { |
| 210 |
++changed; |
| 211 |
lhs->dr_lifetime = rhs->dr_lifetime; |
| 212 |
} |
| 213 |
|
| 214 |
if (lhs->dr_preference != rhs->dr_preference) { |
| 215 |
++changed; |
| 216 |
lhs->dr_preference = rhs->dr_preference; |
| 217 |
} |
| 218 |
|
| 219 |
return changed; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* copy an defaultrouter_entry |
| 224 |
* |
| 225 |
* @retval -1 : error |
| 226 |
* @retval 0 : no error |
| 227 |
*/ |
| 228 |
int |
| 229 |
netsnmp_access_defaultrouter_entry_copy(netsnmp_defaultrouter_entry *lhs, |
| 230 |
netsnmp_defaultrouter_entry *rhs) |
| 231 |
{ |
| 232 |
lhs->dr_addresstype = rhs->dr_addresstype; |
| 233 |
lhs->dr_address_len = rhs->dr_address_len; |
| 234 |
memcpy(lhs->dr_address, rhs->dr_address, rhs->dr_address_len); |
| 235 |
lhs->dr_if_index = rhs->dr_if_index; |
| 236 |
lhs->dr_lifetime = rhs->dr_lifetime; |
| 237 |
lhs->dr_preference = rhs->dr_preference; |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
/**---------------------------------------------------------------------*/ |
| 243 |
/* |
| 244 |
* Utility routines |
| 245 |
*/ |
| 246 |
|
| 247 |
/** |
| 248 |
*/ |
| 249 |
void |
| 250 |
_access_defaultrouter_entry_release(void *entry, void *context) |
| 251 |
{ |
| 252 |
netsnmp_access_defaultrouter_entry_free(entry); |
| 253 |
} |
| 254 |
|
| 255 |
static int _access_defaultrouter_entry_compare_addr(const void *lhs, |
| 256 |
const void *rhs) |
| 257 |
{ |
| 258 |
const netsnmp_defaultrouter_entry *lh = (const netsnmp_defaultrouter_entry *)lhs; |
| 259 |
const netsnmp_defaultrouter_entry *rh = (const netsnmp_defaultrouter_entry *)rhs; |
| 260 |
|
| 261 |
netsnmp_assert(NULL != lhs); |
| 262 |
netsnmp_assert(NULL != rhs); |
| 263 |
|
| 264 |
/* |
| 265 |
* compare address length |
| 266 |
*/ |
| 267 |
if (lh->dr_address_len < rh->dr_address_len) |
| 268 |
return -1; |
| 269 |
else if (lh->dr_address_len > rh->dr_address_len) |
| 270 |
return 1; |
| 271 |
|
| 272 |
/* |
| 273 |
* length equal, compare address |
| 274 |
*/ |
| 275 |
return memcmp(lh->dr_address, rh->dr_address, lh->dr_address_len); |
| 276 |
} |