Path: blob/main/contrib/bsnmp/snmp_mibII/mibII_nettomedia.c
39478 views
/*1* Copyright (c) 2001-20032* Fraunhofer Institute for Open Communication Systems (FhG Fokus).3* All rights reserved.4*5* Author: Harti Brandt <[email protected]>6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*28* $Begemot: bsnmp/snmp_mibII/mibII_nettomedia.c,v 1.8 2004/08/06 08:47:03 brandt Exp $29*30* Read-only implementation of the Arp table (ipNetToMediaTable)31*32* The problem with the table is, that we don't receive routing message33* when a) an arp table entry is resolved and b) when an arp table entry is34* deleted automatically. Therefor we need to poll the table from time to35* time.36*/37#include "mibII.h"38#include "mibII_oid.h"3940#define ARPREFRESH 304142struct mibarp *43mib_find_arp(const struct mibif *ifp, struct in_addr in)44{45struct mibarp *at;46uint32_t a = ntohl(in.s_addr);4748if (get_ticks() >= mibarpticks + ARPREFRESH)49mib_arp_update();5051TAILQ_FOREACH(at, &mibarp_list, link)52if (at->index.subs[0] == ifp->index &&53(at->index.subs[1] == ((a >> 24) & 0xff)) &&54(at->index.subs[2] == ((a >> 16) & 0xff)) &&55(at->index.subs[3] == ((a >> 8) & 0xff)) &&56(at->index.subs[4] == ((a >> 0) & 0xff)))57return (at);58return (NULL);59}6061struct mibarp *62mib_arp_create(const struct mibif *ifp, struct in_addr in, const u_char *phys,63size_t physlen)64{65struct mibarp *at;66uint32_t a = ntohl(in.s_addr);6768if ((at = malloc(sizeof(*at))) == NULL)69return (NULL);70at->flags = 0;7172at->index.len = 5;73at->index.subs[0] = ifp->index;74at->index.subs[1] = (a >> 24) & 0xff;75at->index.subs[2] = (a >> 16) & 0xff;76at->index.subs[3] = (a >> 8) & 0xff;77at->index.subs[4] = (a >> 0) & 0xff;78if ((at->physlen = physlen) > sizeof(at->phys))79at->physlen = sizeof(at->phys);80memcpy(at->phys, phys, at->physlen);8182INSERT_OBJECT_OID(at, &mibarp_list);8384return (at);85}8687void88mib_arp_delete(struct mibarp *at)89{90TAILQ_REMOVE(&mibarp_list, at, link);91free(at);92}9394int95op_nettomedia(struct snmp_context *ctx __unused, struct snmp_value *value,96u_int sub, u_int iidx __unused, enum snmp_op op)97{98struct mibarp *at;99100at = NULL; /* gcc */101102if (get_ticks() >= mibarpticks + ARPREFRESH)103mib_arp_update();104105switch (op) {106107case SNMP_OP_GETNEXT:108if ((at = NEXT_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)109return (SNMP_ERR_NOSUCHNAME);110index_append(&value->var, sub, &at->index);111break;112113case SNMP_OP_GET:114if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)115return (SNMP_ERR_NOSUCHNAME);116break;117118case SNMP_OP_SET:119if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)120return (SNMP_ERR_NO_CREATION);121return (SNMP_ERR_NOT_WRITEABLE);122123case SNMP_OP_ROLLBACK:124case SNMP_OP_COMMIT:125abort();126}127128switch (value->var.subs[sub - 1]) {129130case LEAF_ipNetToMediaIfIndex:131value->v.integer = at->index.subs[0];132break;133134case LEAF_ipNetToMediaPhysAddress:135return (string_get(value, at->phys, at->physlen));136137case LEAF_ipNetToMediaNetAddress:138value->v.ipaddress[0] = at->index.subs[1];139value->v.ipaddress[1] = at->index.subs[2];140value->v.ipaddress[2] = at->index.subs[3];141value->v.ipaddress[3] = at->index.subs[4];142break;143144case LEAF_ipNetToMediaType:145value->v.integer = (at->flags & MIBARP_PERM) ? 4 : 3;146break;147}148return (SNMP_ERR_NOERROR);149}150151152