]> git.proxmox.com Git - mirror_frr.git/blob - lib/snmp.c
Merge pull request #294 from opensourcerouting/modules
[mirror_frr.git] / lib / snmp.c
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
22 #include <zebra.h>
23
24 #include <net-snmp/net-snmp-config.h>
25 #include <net-snmp/net-snmp-includes.h>
26
27 #include "smux.h"
28
29 #define min(A,B) ((A) < (B) ? (A) : (B))
30
31 int
32 oid_compare (const oid *o1, int o1_len, const oid *o2, int o2_len)
33 {
34 int i;
35
36 for (i = 0; i < min (o1_len, o2_len); i++)
37 {
38 if (o1[i] < o2[i])
39 return -1;
40 else if (o1[i] > o2[i])
41 return 1;
42 }
43 if (o1_len < o2_len)
44 return -1;
45 if (o1_len > o2_len)
46 return 1;
47
48 return 0;
49 }
50
51 void *
52 oid_copy (void *dest, const void *src, size_t size)
53 {
54 return memcpy (dest, src, size * sizeof (oid));
55 }
56
57 void
58 oid2in_addr (oid oid[], int len, struct in_addr *addr)
59 {
60 int i;
61 u_char *pnt;
62
63 if (len == 0)
64 return;
65
66 pnt = (u_char *) addr;
67
68 for (i = 0; i < len; i++)
69 *pnt++ = oid[i];
70 }
71
72 void
73 oid_copy_addr (oid oid[], struct in_addr *addr, int len)
74 {
75 int i;
76 u_char *pnt;
77
78 if (len == 0)
79 return;
80
81 pnt = (u_char *) addr;
82
83 for (i = 0; i < len; i++)
84 oid[i] = *pnt++;
85 }
86
87 int
88 smux_header_generic (struct variable *v, oid *name, size_t *length, int exact,
89 size_t *var_len, WriteMethod **write_method)
90 {
91 oid fulloid[MAX_OID_LEN];
92 int ret;
93
94 oid_copy (fulloid, v->name, v->namelen);
95 fulloid[v->namelen] = 0;
96 /* Check against full instance. */
97 ret = oid_compare (name, *length, fulloid, v->namelen + 1);
98
99 /* Check single instance. */
100 if ((exact && (ret != 0)) || (!exact && (ret >= 0)))
101 return MATCH_FAILED;
102
103 /* In case of getnext, fill in full instance. */
104 memcpy (name, fulloid, (v->namelen + 1) * sizeof (oid));
105 *length = v->namelen + 1;
106
107 *write_method = 0;
108 *var_len = sizeof(long); /* default to 'long' results */
109
110 return MATCH_SUCCEEDED;
111 }
112
113 int
114 smux_header_table (struct variable *v, oid *name, size_t *length, int exact,
115 size_t *var_len, WriteMethod **write_method)
116 {
117 /* If the requested OID name is less than OID prefix we
118 handle, adjust it to our prefix. */
119 if ((oid_compare (name, *length, v->name, v->namelen)) < 0)
120 {
121 if (exact)
122 return MATCH_FAILED;
123 oid_copy(name, v->name, v->namelen);
124 *length = v->namelen;
125 }
126
127 *write_method = 0;
128 *var_len = sizeof(long);
129
130 return MATCH_SUCCEEDED;
131 }