]> git.proxmox.com Git - mirror_frr.git/blob - lib/snmp.c
Merge pull request #9846 from idryzhov/lib-zebra-netns
[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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include <net-snmp/net-snmp-config.h>
24 #include <net-snmp/net-snmp-includes.h>
25
26 #include "smux.h"
27
28 #define min(A,B) ((A) < (B) ? (A) : (B))
29
30 int oid_compare(const oid *o1, int o1_len, const oid *o2, int o2_len)
31 {
32 int i;
33
34 for (i = 0; i < min(o1_len, o2_len); i++) {
35 if (o1[i] < o2[i])
36 return -1;
37 else if (o1[i] > o2[i])
38 return 1;
39 }
40 if (o1_len < o2_len)
41 return -1;
42 if (o1_len > o2_len)
43 return 1;
44
45 return 0;
46 }
47
48 void *oid_copy(void *dest, const void *src, size_t size)
49 {
50 return memcpy(dest, src, size * sizeof(oid));
51 }
52
53 void oid2in_addr(oid oid[], int len, struct in_addr *addr)
54 {
55 int i;
56 uint8_t *pnt;
57
58 if (len == 0)
59 return;
60
61 pnt = (uint8_t *)addr;
62
63 for (i = 0; i < len; i++)
64 *pnt++ = oid[i];
65 }
66
67 void oid2in6_addr(oid oid[], struct in6_addr *addr)
68 {
69 unsigned int i;
70 uint8_t *pnt;
71
72 pnt = (uint8_t *)addr;
73
74 for (i = 0; i < sizeof(struct in6_addr); i++)
75 *pnt++ = oid[i];
76 }
77
78 void oid2int(oid oid[], int *dest)
79 {
80 uint8_t i;
81 uint8_t *pnt;
82 int network_dest;
83
84 pnt = (uint8_t *)&network_dest;
85
86 for (i = 0; i < sizeof(int); i++)
87 *pnt++ = oid[i];
88 *dest = ntohl(network_dest);
89 }
90
91 void oid_copy_in_addr(oid oid[], const struct in_addr *addr)
92 {
93 int i;
94 const uint8_t *pnt;
95 int len = sizeof(struct in_addr);
96
97 pnt = (uint8_t *)addr;
98
99 for (i = 0; i < len; i++)
100 oid[i] = *pnt++;
101 }
102
103
104 void oid_copy_in6_addr(oid oid[], const struct in6_addr *addr)
105 {
106 int i;
107 const uint8_t *pnt;
108 int len = sizeof(struct in6_addr);
109
110 pnt = (uint8_t *)addr;
111
112 for (i = 0; i < len; i++)
113 oid[i] = *pnt++;
114 }
115
116 void oid_copy_int(oid oid[], int *val)
117 {
118 uint8_t i;
119 const uint8_t *pnt;
120 int network_val;
121
122 network_val = htonl(*val);
123 pnt = (uint8_t *)&network_val;
124
125 for (i = 0; i < sizeof(int); i++)
126 oid[i] = *pnt++;
127 }
128
129 void oid2string(oid oid[], int len, char *string)
130 {
131 int i;
132 uint8_t *pnt;
133
134 if (len == 0)
135 return;
136
137 pnt = (uint8_t *)string;
138
139 for (i = 0; i < len; i++)
140 *pnt++ = (uint8_t)oid[i];
141 }
142
143 void oid_copy_str(oid oid[], const char *string, int len)
144 {
145 int i;
146 const uint8_t *pnt;
147
148 if (len == 0)
149 return;
150
151 pnt = (uint8_t *)string;
152
153 for (i = 0; i < len; i++)
154 oid[i] = *pnt++;
155 }
156
157 int smux_header_generic(struct variable *v, oid *name, size_t *length,
158 int exact, size_t *var_len, WriteMethod **write_method)
159 {
160 oid fulloid[MAX_OID_LEN];
161 int ret;
162
163 oid_copy(fulloid, v->name, v->namelen);
164 fulloid[v->namelen] = 0;
165 /* Check against full instance. */
166 ret = oid_compare(name, *length, fulloid, v->namelen + 1);
167
168 /* Check single instance. */
169 if ((exact && (ret != 0)) || (!exact && (ret >= 0)))
170 return MATCH_FAILED;
171
172 /* In case of getnext, fill in full instance. */
173 memcpy(name, fulloid, (v->namelen + 1) * sizeof(oid));
174 *length = v->namelen + 1;
175
176 *write_method = 0;
177 *var_len = sizeof(long); /* default to 'long' results */
178
179 return MATCH_SUCCEEDED;
180 }
181
182 int smux_header_table(struct variable *v, oid *name, size_t *length, int exact,
183 size_t *var_len, WriteMethod **write_method)
184 {
185 /* If the requested OID name is less than OID prefix we
186 handle, adjust it to our prefix. */
187 if ((oid_compare(name, *length, v->name, v->namelen)) < 0) {
188 if (exact)
189 return MATCH_FAILED;
190 oid_copy(name, v->name, v->namelen);
191 *length = v->namelen;
192 }
193
194 *write_method = 0;
195 *var_len = sizeof(long);
196
197 return MATCH_SUCCEEDED;
198 }