]>
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 | ||
28 | #define min(A,B) ((A) < (B) ? (A) : (B)) | |
29 | ||
d62a17ae | 30 | int oid_compare(const oid *o1, int o1_len, const oid *o2, int o2_len) |
3a4c9688 | 31 | { |
d62a17ae | 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; | |
3a4c9688 VB |
46 | } |
47 | ||
d62a17ae | 48 | void *oid_copy(void *dest, const void *src, size_t size) |
3a4c9688 | 49 | { |
d62a17ae | 50 | return memcpy(dest, src, size * sizeof(oid)); |
3a4c9688 VB |
51 | } |
52 | ||
d62a17ae | 53 | void oid2in_addr(oid oid[], int len, struct in_addr *addr) |
3a4c9688 | 54 | { |
d62a17ae | 55 | int i; |
d7c0a89a | 56 | uint8_t *pnt; |
3a4c9688 | 57 | |
d62a17ae | 58 | if (len == 0) |
59 | return; | |
3a4c9688 | 60 | |
d7c0a89a | 61 | pnt = (uint8_t *)addr; |
d62a17ae | 62 | |
63 | for (i = 0; i < len; i++) | |
64 | *pnt++ = oid[i]; | |
3a4c9688 VB |
65 | } |
66 | ||
5a224c19 PR |
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 | ||
4f13e83d PR |
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 | ||
4ce217af | 91 | void oid_copy_addr(oid oid[], const struct in_addr *addr, int len) |
3a4c9688 | 92 | { |
d62a17ae | 93 | int i; |
4ce217af | 94 | const uint8_t *pnt; |
d62a17ae | 95 | |
96 | if (len == 0) | |
97 | return; | |
3a4c9688 | 98 | |
d7c0a89a | 99 | pnt = (uint8_t *)addr; |
3a4c9688 | 100 | |
d62a17ae | 101 | for (i = 0; i < len; i++) |
102 | oid[i] = *pnt++; | |
3a4c9688 VB |
103 | } |
104 | ||
4f13e83d PR |
105 | void oid_copy_int(oid oid[], int *val) |
106 | { | |
107 | uint8_t i; | |
108 | const uint8_t *pnt; | |
109 | int network_val; | |
110 | ||
111 | network_val = htonl(*val); | |
112 | pnt = (uint8_t *)&network_val; | |
113 | ||
114 | for (i = 0; i < sizeof(int); i++) | |
115 | oid[i] = *pnt++; | |
116 | } | |
117 | ||
0d020cd6 PR |
118 | void oid2string(oid oid[], int len, char *string) |
119 | { | |
120 | int i; | |
121 | uint8_t *pnt; | |
122 | ||
123 | if (len == 0) | |
124 | return; | |
125 | ||
126 | pnt = (uint8_t *)string; | |
127 | ||
128 | for (i = 0; i < len; i++) | |
4f13e83d | 129 | *pnt++ = (uint8_t)oid[i]; |
0d020cd6 PR |
130 | } |
131 | ||
132 | void oid_copy_str(oid oid[], const char *string, int len) | |
133 | { | |
134 | int i; | |
135 | const uint8_t *pnt; | |
136 | ||
137 | if (len == 0) | |
138 | return; | |
139 | ||
140 | pnt = (uint8_t *)string; | |
141 | ||
142 | for (i = 0; i < len; i++) | |
143 | oid[i] = *pnt++; | |
144 | } | |
145 | ||
d62a17ae | 146 | int smux_header_generic(struct variable *v, oid *name, size_t *length, |
147 | int exact, size_t *var_len, WriteMethod **write_method) | |
3a4c9688 | 148 | { |
d62a17ae | 149 | oid fulloid[MAX_OID_LEN]; |
150 | int ret; | |
3a4c9688 | 151 | |
d62a17ae | 152 | oid_copy(fulloid, v->name, v->namelen); |
153 | fulloid[v->namelen] = 0; | |
154 | /* Check against full instance. */ | |
155 | ret = oid_compare(name, *length, fulloid, v->namelen + 1); | |
3a4c9688 | 156 | |
d62a17ae | 157 | /* Check single instance. */ |
158 | if ((exact && (ret != 0)) || (!exact && (ret >= 0))) | |
159 | return MATCH_FAILED; | |
3a4c9688 | 160 | |
d62a17ae | 161 | /* In case of getnext, fill in full instance. */ |
162 | memcpy(name, fulloid, (v->namelen + 1) * sizeof(oid)); | |
163 | *length = v->namelen + 1; | |
3a4c9688 | 164 | |
d62a17ae | 165 | *write_method = 0; |
166 | *var_len = sizeof(long); /* default to 'long' results */ | |
3a4c9688 | 167 | |
d62a17ae | 168 | return MATCH_SUCCEEDED; |
3a4c9688 | 169 | } |
8046ba6e | 170 | |
d62a17ae | 171 | int smux_header_table(struct variable *v, oid *name, size_t *length, int exact, |
172 | size_t *var_len, WriteMethod **write_method) | |
8046ba6e | 173 | { |
d62a17ae | 174 | /* If the requested OID name is less than OID prefix we |
175 | handle, adjust it to our prefix. */ | |
176 | if ((oid_compare(name, *length, v->name, v->namelen)) < 0) { | |
177 | if (exact) | |
178 | return MATCH_FAILED; | |
179 | oid_copy(name, v->name, v->namelen); | |
180 | *length = v->namelen; | |
181 | } | |
182 | ||
183 | *write_method = 0; | |
184 | *var_len = sizeof(long); | |
185 | ||
186 | return MATCH_SUCCEEDED; | |
8046ba6e | 187 | } |