/contrib/bsnmp/snmp_mibII/mibII_nettomedia.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 150 lines · 90 code · 23 blank · 37 comment · 19 complexity · 3a428bbe08143369398b23da80edd4c5 MD5 · raw file

  1. /*
  2. * Copyright (c) 2001-2003
  3. * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
  4. * All rights reserved.
  5. *
  6. * Author: Harti Brandt <harti@freebsd.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * $Begemot: bsnmp/snmp_mibII/mibII_nettomedia.c,v 1.8 2004/08/06 08:47:03 brandt Exp $
  30. *
  31. * Read-only implementation of the Arp table (ipNetToMediaTable)
  32. *
  33. * The problem with the table is, that we don't receive routing message
  34. * when a) an arp table entry is resolved and b) when an arp table entry is
  35. * deleted automatically. Therefor we need to poll the table from time to
  36. * time.
  37. */
  38. #include "mibII.h"
  39. #include "mibII_oid.h"
  40. #define ARPREFRESH 30
  41. struct mibarp *
  42. mib_find_arp(const struct mibif *ifp, struct in_addr in)
  43. {
  44. struct mibarp *at;
  45. uint32_t a = ntohl(in.s_addr);
  46. if (get_ticks() >= mibarpticks + ARPREFRESH)
  47. mib_arp_update();
  48. TAILQ_FOREACH(at, &mibarp_list, link)
  49. if (at->index.subs[0] == ifp->index &&
  50. (at->index.subs[1] == ((a >> 24) & 0xff)) &&
  51. (at->index.subs[2] == ((a >> 16) & 0xff)) &&
  52. (at->index.subs[3] == ((a >> 8) & 0xff)) &&
  53. (at->index.subs[4] == ((a >> 0) & 0xff)))
  54. return (at);
  55. return (NULL);
  56. }
  57. struct mibarp *
  58. mib_arp_create(const struct mibif *ifp, struct in_addr in, const u_char *phys,
  59. size_t physlen)
  60. {
  61. struct mibarp *at;
  62. uint32_t a = ntohl(in.s_addr);
  63. if ((at = malloc(sizeof(*at))) == NULL)
  64. return (NULL);
  65. at->flags = 0;
  66. at->index.len = 5;
  67. at->index.subs[0] = ifp->index;
  68. at->index.subs[1] = (a >> 24) & 0xff;
  69. at->index.subs[2] = (a >> 16) & 0xff;
  70. at->index.subs[3] = (a >> 8) & 0xff;
  71. at->index.subs[4] = (a >> 0) & 0xff;
  72. if ((at->physlen = physlen) > sizeof(at->phys))
  73. at->physlen = sizeof(at->phys);
  74. memcpy(at->phys, phys, at->physlen);
  75. INSERT_OBJECT_OID(at, &mibarp_list);
  76. return (at);
  77. }
  78. void
  79. mib_arp_delete(struct mibarp *at)
  80. {
  81. TAILQ_REMOVE(&mibarp_list, at, link);
  82. free(at);
  83. }
  84. int
  85. op_nettomedia(struct snmp_context *ctx __unused, struct snmp_value *value,
  86. u_int sub, u_int iidx __unused, enum snmp_op op)
  87. {
  88. struct mibarp *at;
  89. at = NULL; /* gcc */
  90. if (get_ticks() >= mibarpticks + ARPREFRESH)
  91. mib_arp_update();
  92. switch (op) {
  93. case SNMP_OP_GETNEXT:
  94. if ((at = NEXT_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
  95. return (SNMP_ERR_NOSUCHNAME);
  96. index_append(&value->var, sub, &at->index);
  97. break;
  98. case SNMP_OP_GET:
  99. if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
  100. return (SNMP_ERR_NOSUCHNAME);
  101. break;
  102. case SNMP_OP_SET:
  103. if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
  104. return (SNMP_ERR_NO_CREATION);
  105. return (SNMP_ERR_NOT_WRITEABLE);
  106. case SNMP_OP_ROLLBACK:
  107. case SNMP_OP_COMMIT:
  108. abort();
  109. }
  110. switch (value->var.subs[sub - 1]) {
  111. case LEAF_ipNetToMediaIfIndex:
  112. value->v.integer = at->index.subs[0];
  113. break;
  114. case LEAF_ipNetToMediaPhysAddress:
  115. return (string_get(value, at->phys, at->physlen));
  116. case LEAF_ipNetToMediaNetAddress:
  117. value->v.ipaddress[0] = at->index.subs[1];
  118. value->v.ipaddress[1] = at->index.subs[2];
  119. value->v.ipaddress[2] = at->index.subs[3];
  120. value->v.ipaddress[3] = at->index.subs[4];
  121. break;
  122. case LEAF_ipNetToMediaType:
  123. value->v.integer = (at->flags & MIBARP_PERM) ? 4 : 3;
  124. break;
  125. }
  126. return (SNMP_ERR_NOERROR);
  127. }