snmp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SNMP support
  2. * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #ifdef HAVE_SNMP
  23. #include <net-snmp/net-snmp-config.h>
  24. #include <net-snmp/net-snmp-includes.h>
  25. #include "smux.h"
  26. #define min(A,B) ((A) < (B) ? (A) : (B))
  27. int
  28. oid_compare (const oid *o1, int o1_len, const oid *o2, int o2_len)
  29. {
  30. int i;
  31. for (i = 0; i < min (o1_len, o2_len); i++)
  32. {
  33. if (o1[i] < o2[i])
  34. return -1;
  35. else if (o1[i] > o2[i])
  36. return 1;
  37. }
  38. if (o1_len < o2_len)
  39. return -1;
  40. if (o1_len > o2_len)
  41. return 1;
  42. return 0;
  43. }
  44. void *
  45. oid_copy (void *dest, const void *src, size_t size)
  46. {
  47. return memcpy (dest, src, size * sizeof (oid));
  48. }
  49. void
  50. oid2in_addr (oid oid[], int len, struct in_addr *addr)
  51. {
  52. int i;
  53. u_char *pnt;
  54. if (len == 0)
  55. return;
  56. pnt = (u_char *) addr;
  57. for (i = 0; i < len; i++)
  58. *pnt++ = oid[i];
  59. }
  60. void
  61. oid_copy_addr (oid oid[], struct in_addr *addr, int len)
  62. {
  63. int i;
  64. u_char *pnt;
  65. if (len == 0)
  66. return;
  67. pnt = (u_char *) addr;
  68. for (i = 0; i < len; i++)
  69. oid[i] = *pnt++;
  70. }
  71. int
  72. smux_header_generic (struct variable *v, oid *name, size_t *length, int exact,
  73. size_t *var_len, WriteMethod **write_method)
  74. {
  75. oid fulloid[MAX_OID_LEN];
  76. int ret;
  77. oid_copy (fulloid, v->name, v->namelen);
  78. fulloid[v->namelen] = 0;
  79. /* Check against full instance. */
  80. ret = oid_compare (name, *length, fulloid, v->namelen + 1);
  81. /* Check single instance. */
  82. if ((exact && (ret != 0)) || (!exact && (ret >= 0)))
  83. return MATCH_FAILED;
  84. /* In case of getnext, fill in full instance. */
  85. memcpy (name, fulloid, (v->namelen + 1) * sizeof (oid));
  86. *length = v->namelen + 1;
  87. *write_method = 0;
  88. *var_len = sizeof(long); /* default to 'long' results */
  89. return MATCH_SUCCEEDED;
  90. }
  91. int
  92. smux_header_table (struct variable *v, oid *name, size_t *length, int exact,
  93. size_t *var_len, WriteMethod **write_method)
  94. {
  95. /* If the requested OID name is less than OID prefix we
  96. handle, adjust it to our prefix. */
  97. if ((oid_compare (name, *length, v->name, v->namelen)) < 0)
  98. {
  99. if (exact)
  100. return MATCH_FAILED;
  101. oid_copy(name, v->name, v->namelen);
  102. *length = v->namelen;
  103. }
  104. *write_method = 0;
  105. *var_len = sizeof(long);
  106. return MATCH_SUCCEEDED;
  107. }
  108. #endif /* HAVE_SNMP */