]>
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 | ||
4f13e83d PR |
67 | void oid2int(oid oid[], int *dest) |
68 | { | |
69 | uint8_t i; | |
70 | uint8_t *pnt; | |
71 | int network_dest; | |
72 | ||
73 | pnt = (uint8_t *)&network_dest; | |
74 | ||
75 | for (i = 0; i < sizeof(int); i++) | |
76 | *pnt++ = oid[i]; | |
77 | *dest = ntohl(network_dest); | |
78 | } | |
79 | ||
4ce217af | 80 | void oid_copy_addr(oid oid[], const struct in_addr *addr, int len) |
3a4c9688 | 81 | { |
d62a17ae | 82 | int i; |
4ce217af | 83 | const uint8_t *pnt; |
d62a17ae | 84 | |
85 | if (len == 0) | |
86 | return; | |
3a4c9688 | 87 | |
d7c0a89a | 88 | pnt = (uint8_t *)addr; |
3a4c9688 | 89 | |
d62a17ae | 90 | for (i = 0; i < len; i++) |
91 | oid[i] = *pnt++; | |
3a4c9688 VB |
92 | } |
93 | ||
4f13e83d PR |
94 | void oid_copy_int(oid oid[], int *val) |
95 | { | |
96 | uint8_t i; | |
97 | const uint8_t *pnt; | |
98 | int network_val; | |
99 | ||
100 | network_val = htonl(*val); | |
101 | pnt = (uint8_t *)&network_val; | |
102 | ||
103 | for (i = 0; i < sizeof(int); i++) | |
104 | oid[i] = *pnt++; | |
105 | } | |
106 | ||
0d020cd6 PR |
107 | void oid2string(oid oid[], int len, char *string) |
108 | { | |
109 | int i; | |
110 | uint8_t *pnt; | |
111 | ||
112 | if (len == 0) | |
113 | return; | |
114 | ||
115 | pnt = (uint8_t *)string; | |
116 | ||
117 | for (i = 0; i < len; i++) | |
4f13e83d | 118 | *pnt++ = (uint8_t)oid[i]; |
0d020cd6 PR |
119 | } |
120 | ||
121 | void oid_copy_str(oid oid[], const char *string, int len) | |
122 | { | |
123 | int i; | |
124 | const uint8_t *pnt; | |
125 | ||
126 | if (len == 0) | |
127 | return; | |
128 | ||
129 | pnt = (uint8_t *)string; | |
130 | ||
131 | for (i = 0; i < len; i++) | |
132 | oid[i] = *pnt++; | |
133 | } | |
134 | ||
d62a17ae | 135 | int smux_header_generic(struct variable *v, oid *name, size_t *length, |
136 | int exact, size_t *var_len, WriteMethod **write_method) | |
3a4c9688 | 137 | { |
d62a17ae | 138 | oid fulloid[MAX_OID_LEN]; |
139 | int ret; | |
3a4c9688 | 140 | |
d62a17ae | 141 | oid_copy(fulloid, v->name, v->namelen); |
142 | fulloid[v->namelen] = 0; | |
143 | /* Check against full instance. */ | |
144 | ret = oid_compare(name, *length, fulloid, v->namelen + 1); | |
3a4c9688 | 145 | |
d62a17ae | 146 | /* Check single instance. */ |
147 | if ((exact && (ret != 0)) || (!exact && (ret >= 0))) | |
148 | return MATCH_FAILED; | |
3a4c9688 | 149 | |
d62a17ae | 150 | /* In case of getnext, fill in full instance. */ |
151 | memcpy(name, fulloid, (v->namelen + 1) * sizeof(oid)); | |
152 | *length = v->namelen + 1; | |
3a4c9688 | 153 | |
d62a17ae | 154 | *write_method = 0; |
155 | *var_len = sizeof(long); /* default to 'long' results */ | |
3a4c9688 | 156 | |
d62a17ae | 157 | return MATCH_SUCCEEDED; |
3a4c9688 | 158 | } |
8046ba6e | 159 | |
d62a17ae | 160 | int smux_header_table(struct variable *v, oid *name, size_t *length, int exact, |
161 | size_t *var_len, WriteMethod **write_method) | |
8046ba6e | 162 | { |
d62a17ae | 163 | /* If the requested OID name is less than OID prefix we |
164 | handle, adjust it to our prefix. */ | |
165 | if ((oid_compare(name, *length, v->name, v->namelen)) < 0) { | |
166 | if (exact) | |
167 | return MATCH_FAILED; | |
168 | oid_copy(name, v->name, v->namelen); | |
169 | *length = v->namelen; | |
170 | } | |
171 | ||
172 | *write_method = 0; | |
173 | *var_len = sizeof(long); | |
174 | ||
175 | return MATCH_SUCCEEDED; | |
8046ba6e | 176 | } |