📄 ospf_snmp.c
字号:
*type = *offset; } /* LS ID. */ offset++; offsetlen--; len = offsetlen; if (len <= 0) ls_id_next = 1; else { ls_id_next = 0; if (len > IN_ADDR_SIZE) len = IN_ADDR_SIZE; oid2in_addr (offset, len, ls_id); } /* Router ID. */ offset += IN_ADDR_SIZE; offsetlen -= IN_ADDR_SIZE; len = offsetlen; if (len <= 0) router_id_next = 1; else { router_id_next = 0; if (len > IN_ADDR_SIZE) len = IN_ADDR_SIZE; oid2in_addr (offset, len, router_id); } lsa = lsdb_lookup_next (area, type, type_next, ls_id, ls_id_next, router_id, router_id_next); if (lsa) { /* Fill in length. */ *length = v->namelen + OSPF_LSDB_ENTRY_OFFSET; /* Fill in value. */ offset = name + v->namelen; oid_copy_addr (offset, area_id, IN_ADDR_SIZE); offset += IN_ADDR_SIZE; *offset = lsa->data->type; offset++; oid_copy_addr (offset, &lsa->data->id, IN_ADDR_SIZE); offset += IN_ADDR_SIZE; oid_copy_addr (offset, &lsa->data->adv_router, IN_ADDR_SIZE); return lsa; } } while ((area = ospf_area_lookup_next (ospf, area_id, 0)) != NULL); } return NULL;}static u_char *ospfLsdbEntry (struct variable *v, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ struct ospf_lsa *lsa; struct lsa_header *lsah; struct in_addr area_id; u_char type; struct in_addr ls_id; struct in_addr router_id; struct ospf *ospf; /* INDEX { ospfLsdbAreaId, ospfLsdbType, ospfLsdbLsid, ospfLsdbRouterId } */ memset (&area_id, 0, sizeof (struct in_addr)); type = 0; memset (&ls_id, 0, sizeof (struct in_addr)); memset (&router_id, 0, sizeof (struct in_addr)); /* Check OSPF instance. */ ospf = ospf_lookup (); if (ospf == NULL) return NULL; lsa = ospfLsdbLookup (v, name, length, &area_id, &type, &ls_id, &router_id, exact); if (! lsa) return NULL; lsah = lsa->data; /* Return the current value of the variable */ switch (v->magic) { case OSPFLSDBAREAID: /* 1 */ return SNMP_IPADDRESS (lsa->area->area_id); break; case OSPFLSDBTYPE: /* 2 */ return SNMP_INTEGER (lsah->type); break; case OSPFLSDBLSID: /* 3 */ return SNMP_IPADDRESS (lsah->id); break; case OSPFLSDBROUTERID: /* 4 */ return SNMP_IPADDRESS (lsah->adv_router); break; case OSPFLSDBSEQUENCE: /* 5 */ return SNMP_INTEGER (lsah->ls_seqnum); break; case OSPFLSDBAGE: /* 6 */ return SNMP_INTEGER (lsah->ls_age); break; case OSPFLSDBCHECKSUM: /* 7 */ return SNMP_INTEGER (lsah->checksum); break; case OSPFLSDBADVERTISEMENT: /* 8 */ *var_len = ntohs (lsah->length); return (u_char *) lsah; break; default: return NULL; break; } return NULL;}struct ospf_area_range *ospfAreaRangeLookup (struct variable *v, oid *name, size_t *length, struct in_addr *area_id, struct in_addr *range_net, int exact){ oid *offset; int offsetlen; int len; struct ospf *ospf; struct ospf_area *area; struct ospf_area_range *range; struct prefix_ipv4 p; p.family = AF_INET; p.prefixlen = IPV4_MAX_BITLEN; ospf = ospf_lookup (); if (exact) { /* Area ID + Range Network. */ if (v->namelen + IN_ADDR_SIZE + IN_ADDR_SIZE != *length) return NULL; /* Set OID offset for Area ID. */ offset = name + v->namelen; /* Lookup area first. */ oid2in_addr (offset, IN_ADDR_SIZE, area_id); area = ospf_area_lookup_by_area_id (ospf, *area_id); if (! area) return NULL; offset += IN_ADDR_SIZE; /* Lookup area range. */ oid2in_addr (offset, IN_ADDR_SIZE, range_net); p.prefix = *range_net; return ospf_area_range_lookup (area, &p); } else { /* Set OID offset for Area ID. */ offset = name + v->namelen; offsetlen = *length - v->namelen; len = offsetlen; if (len > IN_ADDR_SIZE) len = IN_ADDR_SIZE; oid2in_addr (offset, len, area_id); /* First we search area. */ if (len == IN_ADDR_SIZE) area = ospf_area_lookup_by_area_id (ospf,*area_id); else area = ospf_area_lookup_next (ospf, area_id, len == 0 ? 1 : 0); if (area == NULL) return NULL; do { offset += IN_ADDR_SIZE; offsetlen -= IN_ADDR_SIZE; len = offsetlen; if (len < 0) len = 0; if (len > IN_ADDR_SIZE) len = IN_ADDR_SIZE; oid2in_addr (offset, len, range_net); range = ospf_area_range_lookup_next (area, range_net, len == 0 ? 1 : 0); if (range) { /* Fill in length. */ *length = v->namelen + IN_ADDR_SIZE + IN_ADDR_SIZE; /* Fill in value. */ offset = name + v->namelen; oid_copy_addr (offset, area_id, IN_ADDR_SIZE); offset += IN_ADDR_SIZE; oid_copy_addr (offset, range_net, IN_ADDR_SIZE); return range; } } while ((area = ospf_area_lookup_next (ospf, area_id, 0)) != NULL); } return NULL;}static u_char *ospfAreaRangeEntry (struct variable *v, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ struct ospf_area_range *range; struct in_addr area_id; struct in_addr range_net; struct in_addr mask; struct ospf *ospf; /* Check OSPF instance. */ ospf = ospf_lookup (); if (ospf == NULL) return NULL; memset (&area_id, 0, IN_ADDR_SIZE); memset (&range_net, 0, IN_ADDR_SIZE); range = ospfAreaRangeLookup (v, name, length, &area_id, &range_net, exact); if (! range) return NULL; /* Convert prefixlen to network mask format. */ masklen2ip (range->subst_masklen, &mask); /* Return the current value of the variable */ switch (v->magic) { case OSPFAREARANGEAREAID: /* 1 */ return SNMP_IPADDRESS (area_id); break; case OSPFAREARANGENET: /* 2 */ return SNMP_IPADDRESS (range_net); break; case OSPFAREARANGEMASK: /* 3 */ return SNMP_IPADDRESS (mask); break; case OSPFAREARANGESTATUS: /* 4 */ return SNMP_INTEGER (SNMP_VALID); break; case OSPFAREARANGEEFFECT: /* 5 */#define OSPF_advertiseMatching 1#define OSPF_doNotAdvertiseMatching 2 return SNMP_INTEGER (OSPF_advertiseMatching); break; default: return NULL; break; } return NULL;}struct ospf_nbr_nbma *ospfHostLookup (struct variable *v, oid *name, size_t *length, struct in_addr *addr, int exact){ int len; struct ospf_nbr_nbma *nbr_nbma; struct ospf *ospf; ospf = ospf_lookup (); if (ospf == NULL) return NULL; if (exact) { /* INDEX { ospfHostIpAddress, ospfHostTOS } */ if (*length != v->namelen + IN_ADDR_SIZE + 1) return NULL; /* Check ospfHostTOS. */ if (name[*length - 1] != 0) return NULL; oid2in_addr (name + v->namelen, IN_ADDR_SIZE, addr); nbr_nbma = ospf_nbr_nbma_lookup (ospf, *addr); return nbr_nbma; } else { len = *length - v->namelen; if (len > 4) len = 4; oid2in_addr (name + v->namelen, len, addr); nbr_nbma = ospf_nbr_nbma_lookup_next (ospf, addr, len == 0 ? 1 : 0); if (nbr_nbma == NULL) return NULL; oid_copy_addr (name + v->namelen, addr, IN_ADDR_SIZE); /* Set TOS 0. */ name[v->namelen + IN_ADDR_SIZE] = 0; *length = v->namelen + IN_ADDR_SIZE + 1; return nbr_nbma; } return NULL;}static u_char *ospfHostEntry (struct variable *v, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ struct ospf_nbr_nbma *nbr_nbma; struct ospf_interface *oi; struct in_addr addr; struct ospf *ospf; /* Check OSPF instance. */ ospf = ospf_lookup (); if (ospf == NULL) return NULL; memset (&addr, 0, sizeof (struct in_addr)); nbr_nbma = ospfHostLookup (v, name, length, &addr, exact); if (nbr_nbma == NULL) return NULL; oi = nbr_nbma->oi; /* Return the current value of the variable */ switch (v->magic) { case OSPFHOSTIPADDRESS: /* 1 */ return SNMP_IPADDRESS (nbr_nbma->addr); break; case OSPFHOSTTOS: /* 2 */ return SNMP_INTEGER (0); break; case OSPFHOSTMETRIC: /* 3 */ if (oi) return SNMP_INTEGER (oi->output_cost); else return SNMP_INTEGER (1); break; case OSPFHOSTSTATUS: /* 4 */ return SNMP_INTEGER (SNMP_VALID); break; case OSPFHOSTAREAID: /* 5 */ if (oi && oi->area) return SNMP_IPADDRESS (oi->area->area_id); else return SNMP_IPADDRESS (ospf_empty_addr); break; default: return NULL; break; } return NULL;}struct list *ospf_snmp_iflist;struct ospf_snmp_if{ struct in_addr addr; unsigned int ifindex; struct interface *ifp;};struct ospf_snmp_if *ospf_snmp_if_new (){ struct ospf_snmp_if *osif; osif = XMALLOC (0, sizeof (struct ospf_snmp_if)); memset (osif, 0, sizeof (struct ospf_snmp_if)); return osif;}voidospf_snmp_if_free (struct ospf_snmp_if *osif){ XFREE (0, osif);}voidospf_snmp_if_delete (struct interface *ifp){ struct listnode *nn; struct ospf_snmp_if *osif; LIST_LOOP (ospf_snmp_iflist, osif, nn) { if (osif->ifp == ifp) { list_delete_node (ospf_snmp_iflist, nn); ospf_snmp_if_free (osif); return; } }}voidospf_snmp_if_update (struct interface *ifp){ struct listnode *nn; struct listnode *pn; struct connected *ifc; struct prefix *p; struct ospf_snmp_if *osif; struct in_addr *addr; unsigned int ifindex; ospf_snmp_if_delete (ifp); p = NULL; addr = NULL; ifindex = 0; /* Lookup first IPv4 address entry. */ LIST_LOOP (ifp->connected, ifc, nn) { if (if_is_pointopoint (ifp)) p = ifc->destination; else p = ifc->address; if (p->family == AF_INET) { addr = &p->u.prefix4; break; } } if (! addr) ifindex = ifp->ifindex; /* Add interface to the list. */ pn = NULL; LIST_LOOP (ospf_snmp_iflist, osif, nn) { if (addr) { if (ntohl (osif->addr.s_addr) > ntohl (addr->s_addr)) break; } else { /* Unnumbered interface. */ if (osif->addr.s_addr != 0 || osif->ifindex > ifindex) break; } pn = nn; } osif = ospf_snmp_if_new (); if (addr) osif->addr = *addr; else osif->ifindex = ifindex; osif->ifp = ifp; listnode_add_after (ospf_snmp_iflist, pn, osif);}struct interface *ospf_snmp_if_lookup (struct in_addr *ifaddr, unsigned int *ifindex){ struct listnode *nn; struct ospf_snmp_if *osif; LIST_LOOP (ospf_snmp_iflist, osif, nn) { if (ifaddr->s_addr) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -