]>
Commit | Line | Data |
---|---|---|
3a4c9688 VB |
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 | * | |
896014f4 DL |
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 | |
3a4c9688 VB |
19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
3a4c9688 VB |
23 | #include <net-snmp/net-snmp-config.h> |
24 | #include <net-snmp/net-snmp-includes.h> | |
25 | ||
26 | #include "smux.h" | |
27 | ||
d62a17ae | 28 | int oid_compare(const oid *o1, int o1_len, const oid *o2, int o2_len) |
3a4c9688 | 29 | { |
d62a17ae | 30 | int i; |
31 | ||
7a8ce9d5 | 32 | for (i = 0; i < MIN(o1_len, o2_len); i++) { |
d62a17ae | 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 | ||
43 | return 0; | |
3a4c9688 VB |
44 | } |
45 | ||
d62a17ae | 46 | void *oid_copy(void *dest, const void *src, size_t size) |
3a4c9688 | 47 | { |
d62a17ae | 48 | return memcpy(dest, src, size * sizeof(oid)); |
3a4c9688 VB |
49 | } |
50 | ||
d62a17ae | 51 | void oid2in_addr(oid oid[], int len, struct in_addr *addr) |
3a4c9688 | 52 | { |
d62a17ae | 53 | int i; |
d7c0a89a | 54 | uint8_t *pnt; |
3a4c9688 | 55 | |
d62a17ae | 56 | if (len == 0) |
57 | return; | |
3a4c9688 | 58 | |
d7c0a89a | 59 | pnt = (uint8_t *)addr; |
d62a17ae | 60 | |
61 | for (i = 0; i < len; i++) | |
62 | *pnt++ = oid[i]; | |
3a4c9688 VB |
63 | } |
64 | ||
5a224c19 PR |
65 | void oid2in6_addr(oid oid[], struct in6_addr *addr) |
66 | { | |
67 | unsigned int i; | |
68 | uint8_t *pnt; | |
69 | ||
70 | pnt = (uint8_t *)addr; | |
71 | ||
72 | for (i = 0; i < sizeof(struct in6_addr); i++) | |
73 | *pnt++ = oid[i]; | |
74 | } | |
75 | ||
4f13e83d PR |
76 | void oid2int(oid oid[], int *dest) |
77 | { | |
78 | uint8_t i; | |
79 | uint8_t *pnt; | |
80 | int network_dest; | |
81 | ||
82 | pnt = (uint8_t *)&network_dest; | |
83 | ||
84 | for (i = 0; i < sizeof(int); i++) | |
85 | *pnt++ = oid[i]; | |
86 | *dest = ntohl(network_dest); | |
87 | } | |
88 | ||
9e263221 | 89 | void oid_copy_in_addr(oid oid[], const struct in_addr *addr) |
3a4c9688 | 90 | { |
d62a17ae | 91 | int i; |
4ce217af | 92 | const uint8_t *pnt; |
9e263221 | 93 | int len = sizeof(struct in_addr); |
d62a17ae | 94 | |
9e263221 PR |
95 | pnt = (uint8_t *)addr; |
96 | ||
97 | for (i = 0; i < len; i++) | |
98 | oid[i] = *pnt++; | |
99 | } | |
100 | ||
101 | ||
102 | void oid_copy_in6_addr(oid oid[], const struct in6_addr *addr) | |
103 | { | |
104 | int i; | |
105 | const uint8_t *pnt; | |
106 | int len = sizeof(struct in6_addr); | |
3a4c9688 | 107 | |
d7c0a89a | 108 | pnt = (uint8_t *)addr; |
3a4c9688 | 109 | |
d62a17ae | 110 | for (i = 0; i < len; i++) |
111 | oid[i] = *pnt++; | |
3a4c9688 VB |
112 | } |
113 | ||
4f13e83d PR |
114 | void oid_copy_int(oid oid[], int *val) |
115 | { | |
116 | uint8_t i; | |
117 | const uint8_t *pnt; | |
118 | int network_val; | |
119 | ||
120 | network_val = htonl(*val); | |
121 | pnt = (uint8_t *)&network_val; | |
122 | ||
123 | for (i = 0; i < sizeof(int); i++) | |
124 | oid[i] = *pnt++; | |
125 | } | |
126 | ||
0d020cd6 PR |
127 | void oid2string(oid oid[], int len, char *string) |
128 | { | |
129 | int i; | |
130 | uint8_t *pnt; | |
131 | ||
132 | if (len == 0) | |
133 | return; | |
134 | ||
135 | pnt = (uint8_t *)string; | |
136 | ||
137 | for (i = 0; i < len; i++) | |
4f13e83d | 138 | *pnt++ = (uint8_t)oid[i]; |
0d020cd6 PR |
139 | } |
140 | ||
141 | void oid_copy_str(oid oid[], const char *string, int len) | |
142 | { | |
143 | int i; | |
144 | const uint8_t *pnt; | |
145 | ||
146 | if (len == 0) | |
147 | return; | |
148 | ||
149 | pnt = (uint8_t *)string; | |
150 | ||
151 | for (i = 0; i < len; i++) | |
152 | oid[i] = *pnt++; | |
153 | } | |
154 | ||
d62a17ae | 155 | int smux_header_generic(struct variable *v, oid *name, size_t *length, |
156 | int exact, size_t *var_len, WriteMethod **write_method) | |
3a4c9688 | 157 | { |
d62a17ae | 158 | oid fulloid[MAX_OID_LEN]; |
159 | int ret; | |
3a4c9688 | 160 | |
d62a17ae | 161 | oid_copy(fulloid, v->name, v->namelen); |
162 | fulloid[v->namelen] = 0; | |
163 | /* Check against full instance. */ | |
164 | ret = oid_compare(name, *length, fulloid, v->namelen + 1); | |
3a4c9688 | 165 | |
d62a17ae | 166 | /* Check single instance. */ |
167 | if ((exact && (ret != 0)) || (!exact && (ret >= 0))) | |
168 | return MATCH_FAILED; | |
3a4c9688 | 169 | |
d62a17ae | 170 | /* In case of getnext, fill in full instance. */ |
171 | memcpy(name, fulloid, (v->namelen + 1) * sizeof(oid)); | |
172 | *length = v->namelen + 1; | |
3a4c9688 | 173 | |
d62a17ae | 174 | *write_method = 0; |
175 | *var_len = sizeof(long); /* default to 'long' results */ | |
3a4c9688 | 176 | |
d62a17ae | 177 | return MATCH_SUCCEEDED; |
3a4c9688 | 178 | } |
8046ba6e | 179 | |
d62a17ae | 180 | int smux_header_table(struct variable *v, oid *name, size_t *length, int exact, |
181 | size_t *var_len, WriteMethod **write_method) | |
8046ba6e | 182 | { |
d62a17ae | 183 | /* If the requested OID name is less than OID prefix we |
184 | handle, adjust it to our prefix. */ | |
185 | if ((oid_compare(name, *length, v->name, v->namelen)) < 0) { | |
186 | if (exact) | |
187 | return MATCH_FAILED; | |
188 | oid_copy(name, v->name, v->namelen); | |
189 | *length = v->namelen; | |
190 | } | |
191 | ||
192 | *write_method = 0; | |
193 | *var_len = sizeof(long); | |
194 | ||
195 | return MATCH_SUCCEEDED; | |
8046ba6e | 196 | } |