2 * DEBUG: section 49 SNMP support
3 * AUTHOR: Glenn Chisholm
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 #include "acl/FilledChecklist.h"
34 #include "base/CbcPointer.h"
35 #include "CachePeer.h"
36 #include "client_db.h"
38 #include "comm/Connection.h"
39 #include "comm/Loops.h"
40 #include "comm/UdpOpenDialer.h"
41 #include "ip/Address.h"
43 #include "snmp_agent.h"
44 #include "snmp_core.h"
45 #include "snmp/Forwarder.h"
46 #include "SquidConfig.h"
49 static void snmpPortOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
51 mib_tree_entry
*mib_tree_head
;
52 mib_tree_entry
*mib_tree_last
;
54 Comm::ConnectionPointer snmpIncomingConn
;
55 Comm::ConnectionPointer snmpOutgoingConn
;
57 static mib_tree_entry
* snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
= atNone
);
58 static mib_tree_entry
*snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
, int children
,...);
59 static oid
*snmpCreateOid(int length
,...);
60 mib_tree_entry
* snmpLookupNodeStr(mib_tree_entry
*entry
, const char *str
);
61 int snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
);
62 SQUIDCEXTERN
void (*snmplib_debug_hook
) (int, char *);
63 static oid
*static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
64 static oid
*time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
65 static oid
*peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
66 static oid
*client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
67 static void snmpDecodePacket(snmp_request_t
* rq
);
68 static void snmpConstructReponse(snmp_request_t
* rq
);
70 static oid_ParseFn
*snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
);
71 static oid_ParseFn
*snmpTreeGet(oid
* Current
, snint CurrentLen
);
72 static mib_tree_entry
*snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
);
73 static mib_tree_entry
*snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
);
74 extern "C" void snmpSnmplibDebug(int lvl
, char *buf
);
77 * The functions used during startup:
84 * Turns the MIB into a Tree structure. Called during the startup process.
89 debugs(49, 5, "snmpInit: Building SNMP mib tree structure");
91 snmplib_debug_hook
= snmpSnmplibDebug
;
94 * This following bit of evil is to get the final node in the "squid" mib
95 * without having a "search" function. A search function should be written
96 * to make this and the other code much less evil.
98 mib_tree_head
= snmpAddNode(snmpCreateOid(1, 1), 1, NULL
, NULL
, atNone
, 0);
100 assert(mib_tree_head
);
101 debugs(49, 5, "snmpInit: root is " << mib_tree_head
);
102 snmpAddNodeStr("1", 3, NULL
, NULL
);
104 snmpAddNodeStr("1.3", 6, NULL
, NULL
);
106 snmpAddNodeStr("1.3.6", 1, NULL
, NULL
);
107 snmpAddNodeStr("1.3.6.1", 4, NULL
, NULL
);
108 snmpAddNodeStr("1.3.6.1.4", 1, NULL
, NULL
);
109 snmpAddNodeStr("1.3.6.1.4.1", 3495, NULL
, NULL
);
110 mib_tree_entry
*m2
= snmpAddNodeStr("1.3.6.1.4.1.3495", 1, NULL
, NULL
);
112 mib_tree_entry
*n
= snmpLookupNodeStr(NULL
, "1.3.6.1.4.1.3495.1");
115 /* SQ_SYS - 1.3.6.1.4.1.3495.1.1 */
116 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 1, NULL
, NULL
);
117 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSVMSIZ
, snmp_sysFn
, static_Inst
, atSum
);
118 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSSTOR
, snmp_sysFn
, static_Inst
, atSum
);
119 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYS_UPTIME
, snmp_sysFn
, static_Inst
, atMax
);
121 /* SQ_CONF - 1.3.6.1.4.1.3495.1.2 */
122 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 2, NULL
, NULL
);
123 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_ADMIN
, snmp_confFn
, static_Inst
);
124 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION
, snmp_confFn
, static_Inst
);
125 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION_ID
, snmp_confFn
, static_Inst
);
126 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_LOG_FAC
, snmp_confFn
, static_Inst
);
128 /* SQ_CONF + CONF_STORAGE - 1.3.6.1.4.1.3495.1.5 */
129 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_STORAGE
, NULL
, NULL
);
130 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_MMAXSZ
, snmp_confFn
, static_Inst
, atSum
);
131 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWMAXSZ
, snmp_confFn
, static_Inst
, atSum
);
132 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWHIWM
, snmp_confFn
, static_Inst
, atMin
);
133 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWLOWM
, snmp_confFn
, static_Inst
, atMin
);
135 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_UNIQNAME
, snmp_confFn
, static_Inst
);
137 /* SQ_PRF - 1.3.6.1.4.1.3495.1.3 */
138 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 3, NULL
, NULL
); /* SQ_PRF */
140 /* PERF_SYS - 1.3.6.1.4.1.3495.1.3.1 */
141 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_SYS
, NULL
, NULL
);
142 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_PF
, snmp_prfSysFn
, static_Inst
, atSum
);
143 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMR
, snmp_prfSysFn
, static_Inst
, atSum
);
144 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MEMUSAGE
, snmp_prfSysFn
, static_Inst
, atSum
);
145 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUTIME
, snmp_prfSysFn
, static_Inst
, atSum
);
146 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUUSAGE
, snmp_prfSysFn
, static_Inst
, atSum
);
147 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MAXRESSZ
, snmp_prfSysFn
, static_Inst
, atSum
);
148 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMOBJCNT
, snmp_prfSysFn
, static_Inst
, atSum
);
151 The meaning of LRU is "oldest timestamped object in cache, if LRU algorithm is
153 What this SMP support needs to do is aggregate via a special filter equivalent to
154 min() to retain the semantic oldest-object meaning. A special one is needed that
155 works as unsigned and ignores '0' values.
157 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURLRUEXP
, snmp_prfSysFn
, static_Inst
);
158 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNLREQ
, snmp_prfSysFn
, static_Inst
, atSum
);
159 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNUSED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
160 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURRESERVED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
161 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUSED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
162 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURMAX_FD
, snmp_prfSysFn
, static_Inst
, atMax
);
164 /* PERF_PROTO - 1.3.6.1.4.1.3495.1.3.2 */
165 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_PROTO
, NULL
, NULL
);
166 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_AGGR
, NULL
, NULL
);
167 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_REQ
, snmp_prfProtoFn
, static_Inst
, atSum
);
168 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_HITS
, snmp_prfProtoFn
, static_Inst
, atSum
);
169 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_ERRORS
, snmp_prfProtoFn
, static_Inst
, atSum
);
170 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
, atSum
);
171 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
, atSum
);
172 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_S
, snmp_prfProtoFn
, static_Inst
, atSum
);
173 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_R
, snmp_prfProtoFn
, static_Inst
, atSum
);
174 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_SKB
, snmp_prfProtoFn
, static_Inst
, atSum
);
175 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_RKB
, snmp_prfProtoFn
, static_Inst
, atSum
);
176 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_REQ
, snmp_prfProtoFn
, static_Inst
, atSum
);
177 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ERRORS
, snmp_prfProtoFn
, static_Inst
, atSum
);
178 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
, atSum
);
179 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
, atSum
);
180 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CURSWAP
, snmp_prfProtoFn
, static_Inst
, atSum
);
181 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CLIENTS
, snmp_prfProtoFn
, static_Inst
, atSum
);
183 /* Note this is time-series rather than 'static' */
184 /* cacheMedianSvcTable */
185 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_MEDIAN
, NULL
, NULL
);
187 /* cacheMedianSvcEntry */
188 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2", 1, NULL
, NULL
);
189 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_TIME
, snmp_prfProtoFn
, time_Inst
, atAverage
);
190 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_ALL
, snmp_prfProtoFn
, time_Inst
, atAverage
);
191 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_MISS
, snmp_prfProtoFn
, time_Inst
, atAverage
);
192 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NM
, snmp_prfProtoFn
, time_Inst
, atAverage
);
193 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_HIT
, snmp_prfProtoFn
, time_Inst
, atAverage
);
194 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_QUERY
, snmp_prfProtoFn
, time_Inst
, atAverage
);
195 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_REPLY
, snmp_prfProtoFn
, time_Inst
, atAverage
);
196 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_DNS
, snmp_prfProtoFn
, time_Inst
, atAverage
);
197 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_RHR
, snmp_prfProtoFn
, time_Inst
, atAverage
);
198 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_BHR
, snmp_prfProtoFn
, time_Inst
, atAverage
);
199 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NH
, snmp_prfProtoFn
, time_Inst
, atAverage
);
201 /* SQ_NET - 1.3.6.1.4.1.3495.1.4 */
202 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 4, NULL
, NULL
);
204 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_IP_CACHE
, NULL
, NULL
);
205 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_ENT
, snmp_netIpFn
, static_Inst
, atSum
);
206 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_REQ
, snmp_netIpFn
, static_Inst
, atSum
);
207 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_HITS
, snmp_netIpFn
, static_Inst
, atSum
);
208 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_PENDHIT
, snmp_netIpFn
, static_Inst
, atSum
);
209 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_NEGHIT
, snmp_netIpFn
, static_Inst
, atSum
);
210 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_MISS
, snmp_netIpFn
, static_Inst
, atSum
);
211 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_GHBN
, snmp_netIpFn
, static_Inst
, atSum
);
212 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_LOC
, snmp_netIpFn
, static_Inst
, atSum
);
214 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_FQDN_CACHE
, NULL
, NULL
);
215 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_ENT
, snmp_netFqdnFn
, static_Inst
, atSum
);
216 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_REQ
, snmp_netFqdnFn
, static_Inst
, atSum
);
217 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_HITS
, snmp_netFqdnFn
, static_Inst
, atSum
);
218 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_PENDHIT
, snmp_netFqdnFn
, static_Inst
, atSum
);
219 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_NEGHIT
, snmp_netFqdnFn
, static_Inst
, atSum
);
220 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_MISS
, snmp_netFqdnFn
, static_Inst
, atSum
);
221 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_GHBN
, snmp_netFqdnFn
, static_Inst
, atSum
);
223 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_DNS_CACHE
, NULL
, NULL
);
224 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REQ
, snmp_netDnsFn
, static_Inst
, atSum
);
225 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REP
, snmp_netDnsFn
, static_Inst
, atSum
);
226 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_SERVERS
, snmp_netDnsFn
, static_Inst
, atSum
);
228 /* SQ_MESH - 1.3.6.1.4.1.3495.1.5 */
229 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 5, NULL
, NULL
);
231 /* cachePeerTable - 1.3.6.1.4.1.3495.1.5.1 */
232 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_PTBL
, NULL
, NULL
);
234 /* CachePeerTableEntry (version 3) - 1.3.6.1.4.1.3495.1.5.1.3 */
235 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1", 3, NULL
, NULL
);
236 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_INDEX
, snmp_meshPtblFn
, peer_Inst
);
237 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_NAME
, snmp_meshPtblFn
, peer_Inst
);
238 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR_TYPE
, snmp_meshPtblFn
, peer_Inst
);
239 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR
, snmp_meshPtblFn
, peer_Inst
);
240 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_HTTP
, snmp_meshPtblFn
, peer_Inst
);
241 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ICP
, snmp_meshPtblFn
, peer_Inst
);
242 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_TYPE
, snmp_meshPtblFn
, peer_Inst
);
243 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_STATE
, snmp_meshPtblFn
, peer_Inst
);
244 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_SENT
, snmp_meshPtblFn
, peer_Inst
);
245 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_PACKED
, snmp_meshPtblFn
, peer_Inst
);
246 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_FETCHES
, snmp_meshPtblFn
, peer_Inst
);
247 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_RTT
, snmp_meshPtblFn
, peer_Inst
);
248 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_IGN
, snmp_meshPtblFn
, peer_Inst
);
249 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_S
, snmp_meshPtblFn
, peer_Inst
);
250 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_R
, snmp_meshPtblFn
, peer_Inst
);
252 /* cacheClientTable - 1.3.6.1.4.1.3495.1.5.2 */
253 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_CTBL
, NULL
, NULL
);
255 /* BUG 2811: we NEED to create a reliable index for the client DB and make version 3 of the table. */
256 /* for now we have version 2 table with OID capable of mixed IPv4 / IPv6 clients and upgraded address text format. */
258 /* cacheClientEntry - 1.3.6.1.4.1.3495.1.5.2.2 */
259 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2", 2, NULL
, NULL
);
260 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR_TYPE
, snmp_meshCtblFn
, client_Inst
);
261 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR
, snmp_meshCtblFn
, client_Inst
);
262 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTREQ
, snmp_meshCtblFn
, client_Inst
);
263 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTBYTES
, snmp_meshCtblFn
, client_Inst
);
264 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITS
, snmp_meshCtblFn
, client_Inst
);
265 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITBYTES
, snmp_meshCtblFn
, client_Inst
);
266 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPREQ
, snmp_meshCtblFn
, client_Inst
);
267 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPBYTES
, snmp_meshCtblFn
, client_Inst
);
268 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITS
, snmp_meshCtblFn
, client_Inst
);
269 mib_tree_last
= snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITBYTES
, snmp_meshCtblFn
, client_Inst
);
271 debugs(49, 9, "snmpInit: Completed SNMP mib tree structure");
277 debugs(49, 5, "snmpConnectionOpen: Called");
279 if (Config
.Port
.snmp
<= 0)
282 snmpIncomingConn
= new Comm::Connection
;
283 snmpIncomingConn
->local
= Config
.Addrs
.snmp_incoming
;
284 snmpIncomingConn
->local
.SetPort(Config
.Port
.snmp
);
286 if (!Ip::EnableIpv6
&& !snmpIncomingConn
->local
.SetIPv4()) {
287 debugs(49, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << snmpIncomingConn
->local
<< " is not an IPv4 address.");
288 fatal("SNMP port cannot be opened.");
290 /* split-stack for now requires IPv4-only SNMP */
291 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& snmpIncomingConn
->local
.IsAnyAddr()) {
292 snmpIncomingConn
->local
.SetIPv4();
295 AsyncCall::Pointer call
= asyncCall(49, 2, "snmpIncomingConnectionOpened",
296 Comm::UdpOpenDialer(&snmpPortOpened
));
297 Ipc::StartListening(SOCK_DGRAM
, IPPROTO_UDP
, snmpIncomingConn
, Ipc::fdnInSnmpSocket
, call
);
299 if (!Config
.Addrs
.snmp_outgoing
.IsNoAddr()) {
300 snmpOutgoingConn
= new Comm::Connection
;
301 snmpOutgoingConn
->local
= Config
.Addrs
.snmp_outgoing
;
302 snmpOutgoingConn
->local
.SetPort(Config
.Port
.snmp
);
304 if (!Ip::EnableIpv6
&& !snmpOutgoingConn
->local
.SetIPv4()) {
305 debugs(49, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << snmpOutgoingConn
->local
<< " is not an IPv4 address.");
306 fatal("SNMP port cannot be opened.");
308 /* split-stack for now requires IPv4-only SNMP */
309 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& snmpOutgoingConn
->local
.IsAnyAddr()) {
310 snmpOutgoingConn
->local
.SetIPv4();
312 AsyncCall::Pointer call
= asyncCall(49, 2, "snmpOutgoingConnectionOpened",
313 Comm::UdpOpenDialer(&snmpPortOpened
));
314 Ipc::StartListening(SOCK_DGRAM
, IPPROTO_UDP
, snmpOutgoingConn
, Ipc::fdnOutSnmpSocket
, call
);
316 snmpOutgoingConn
= snmpIncomingConn
;
317 debugs(1, DBG_IMPORTANT
, "Sending SNMP messages from " << snmpOutgoingConn
->local
);
322 snmpPortOpened(const Comm::ConnectionPointer
&conn
, int errNo
)
324 if (!Comm::IsConnOpen(conn
))
325 fatalf("Cannot open SNMP %s Port",(conn
->fd
== snmpIncomingConn
->fd
?"receiving":"sending"));
327 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
329 if (conn
->fd
== snmpIncomingConn
->fd
)
330 debugs(1, DBG_IMPORTANT
, "Accepting SNMP messages on " << snmpIncomingConn
->local
);
331 else if (conn
->fd
== snmpOutgoingConn
->fd
)
332 debugs(1, DBG_IMPORTANT
, "Sending SNMP messages from " << snmpOutgoingConn
->local
);
334 fatalf("Lost SNMP port (%d) on FD %d", (int)conn
->local
.GetPort(), conn
->fd
);
340 if (Comm::IsConnOpen(snmpIncomingConn
)) {
341 debugs(49, DBG_IMPORTANT
, "Closing SNMP receiving port " << snmpIncomingConn
->local
);
342 snmpIncomingConn
->close();
344 snmpIncomingConn
= NULL
;
346 if (Comm::IsConnOpen(snmpOutgoingConn
) && snmpIncomingConn
!= snmpOutgoingConn
) {
347 // Perform OUT port closure so as not to step on IN port when sharing a conn.
348 debugs(49, DBG_IMPORTANT
, "Closing SNMP sending port " << snmpOutgoingConn
->local
);
349 snmpOutgoingConn
->close();
351 snmpOutgoingConn
= NULL
;
355 * Functions for handling the requests.
359 * Accept the UDP packet
362 snmpHandleUdp(int sock
, void *not_used
)
364 LOCAL_ARRAY(char, buf
, SNMP_REQUEST_SIZE
);
366 snmp_request_t
*snmp_rq
;
369 debugs(49, 5, "snmpHandleUdp: Called.");
371 Comm::SetSelect(sock
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
373 memset(buf
, '\0', SNMP_REQUEST_SIZE
);
375 len
= comm_udp_recvfrom(sock
,
383 debugs(49, 3, "snmpHandleUdp: FD " << sock
<< ": received " << len
<< " bytes from " << from
<< ".");
385 snmp_rq
= (snmp_request_t
*)xcalloc(1, sizeof(snmp_request_t
));
386 snmp_rq
->buf
= (u_char
*) buf
;
388 snmp_rq
->sock
= sock
;
389 snmp_rq
->outbuf
= (unsigned char *)xmalloc(snmp_rq
->outlen
= SNMP_REQUEST_SIZE
);
390 snmp_rq
->from
= from
;
391 snmpDecodePacket(snmp_rq
);
392 xfree(snmp_rq
->outbuf
);
395 debugs(49, DBG_IMPORTANT
, "snmpHandleUdp: FD " << sock
<< " recvfrom: " << xstrerror());
400 * Turn SNMP packet into a PDU, check available ACL's
403 snmpDecodePacket(snmp_request_t
* rq
)
405 struct snmp_pdu
*PDU
;
407 u_char
*buf
= rq
->buf
;
409 allow_t allow
= ACCESS_DENIED
;
411 if (!Config
.accessList
.snmp
) {
412 debugs(49, DBG_IMPORTANT
, "WARNING: snmp_access not configured. agent query DENIED from : " << rq
->from
);
416 debugs(49, 5, HERE
<< "Called.");
417 PDU
= snmp_pdu_create(0);
418 /* Allways answer on SNMPv1 */
419 rq
->session
.Version
= SNMP_VERSION_1
;
420 Community
= snmp_parse(&rq
->session
, PDU
, buf
, len
);
422 /* Check if we have explicit permission to access SNMP data.
423 * default (set above) is to deny all */
425 ACLFilledChecklist
checklist(Config
.accessList
.snmp
, NULL
, NULL
);
426 checklist
.src_addr
= rq
->from
;
427 checklist
.snmp_community
= (char *) Community
;
428 allow
= checklist
.fastCheck();
430 if (allow
== ACCESS_ALLOWED
&& (snmp_coexist_V2toV1(PDU
))) {
431 rq
->community
= Community
;
433 debugs(49, 5, "snmpAgentParse: reqid=[" << PDU
->reqid
<< "]");
434 snmpConstructReponse(rq
);
436 debugs(49, DBG_IMPORTANT
, "WARNING: SNMP agent query DENIED from : " << rq
->from
);
441 debugs(49, DBG_IMPORTANT
, "WARNING: Failed SNMP agent query from : " << rq
->from
);
447 * Packet OK, ACL Check OK, Create reponse.
450 snmpConstructReponse(snmp_request_t
* rq
)
453 struct snmp_pdu
*RespPDU
;
455 debugs(49, 5, "snmpConstructReponse: Called.");
457 if (UsingSmp() && IamWorkerProcess()) {
458 AsyncJob::Start(new Snmp::Forwarder(static_cast<Snmp::Pdu
&>(*rq
->PDU
),
459 static_cast<Snmp::Session
&>(rq
->session
), rq
->sock
, rq
->from
));
460 snmp_free_pdu(rq
->PDU
);
464 RespPDU
= snmpAgentResponse(rq
->PDU
);
465 snmp_free_pdu(rq
->PDU
);
467 if (RespPDU
!= NULL
) {
468 snmp_build(&rq
->session
, RespPDU
, rq
->outbuf
, &rq
->outlen
);
469 comm_udp_sendto(rq
->sock
, rq
->from
, rq
->outbuf
, rq
->outlen
);
470 snmp_free_pdu(RespPDU
);
475 * Decide how to respond to the request, construct a response and
476 * return the response to the requester.
480 snmpAgentResponse(struct snmp_pdu
*PDU
) {
482 struct snmp_pdu
*Answer
= NULL
;
484 debugs(49, 5, "snmpAgentResponse: Called.");
486 if ((Answer
= snmp_pdu_create(SNMP_PDU_RESPONSE
))) {
487 Answer
->reqid
= PDU
->reqid
;
488 Answer
->errindex
= 0;
490 if (PDU
->command
== SNMP_PDU_GET
|| PDU
->command
== SNMP_PDU_GETNEXT
) {
492 int get_next
= (PDU
->command
== SNMP_PDU_GETNEXT
);
493 variable_list
*VarPtr_
;
494 variable_list
**RespVars
= &(Answer
->variables
);
495 oid_ParseFn
*ParseFn
;
497 /* Loop through all variables */
499 for (VarPtr_
= PDU
->variables
; VarPtr_
; VarPtr_
= VarPtr_
->next_variable
) {
500 variable_list
*VarPtr
= VarPtr_
;
501 variable_list
*VarNew
= NULL
;
502 oid
*NextOidName
= NULL
;
503 snint NextOidNameLen
= 0;
508 ParseFn
= snmpTreeNext(VarPtr
->name
, VarPtr
->name_length
, &NextOidName
, &NextOidNameLen
);
510 ParseFn
= snmpTreeGet(VarPtr
->name
, VarPtr
->name_length
);
512 if (ParseFn
== NULL
) {
513 Answer
->errstat
= SNMP_ERR_NOSUCHNAME
;
514 debugs(49, 5, "snmpAgentResponse: No such oid. ");
517 VarPtr
= snmp_var_new(NextOidName
, NextOidNameLen
);
521 int * errstatTmp
= &(Answer
->errstat
);
523 VarNew
= (*ParseFn
) (VarPtr
, (snint
*) errstatTmp
);
526 snmp_var_free(VarPtr
);
529 if ((Answer
->errstat
!= SNMP_ERR_NOERROR
) || (VarNew
== NULL
)) {
530 Answer
->errindex
= index
;
531 debugs(49, 5, "snmpAgentResponse: error.");
534 snmp_var_free(VarNew
);
536 while ((VarPtr
= Answer
->variables
) != NULL
) {
537 Answer
->variables
= VarPtr
->next_variable
;
538 snmp_var_free(VarPtr
);
541 /* Steal the original PDU list of variables for the error response */
542 Answer
->variables
= PDU
->variables
;
544 PDU
->variables
= NULL
;
549 /* No error. Insert this var at the end, and move on to the next.
553 RespVars
= &(VarNew
->next_variable
);
562 snmpTreeGet(oid
* Current
, snint CurrentLen
)
564 oid_ParseFn
*Fn
= NULL
;
565 mib_tree_entry
*mibTreeEntry
= NULL
;
568 debugs(49, 5, "snmpTreeGet: Called");
571 debugs(49, 6, "snmpTreeGet: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
) );
573 mibTreeEntry
= mib_tree_head
;
575 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
578 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
579 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
584 if (mibTreeEntry
&& mibTreeEntry
->parsefunction
)
585 Fn
= mibTreeEntry
->parsefunction
;
587 debugs(49, 5, "snmpTreeGet: return");
593 snmpAggrType(oid
* Current
, snint CurrentLen
)
597 mib_tree_entry
* mibTreeEntry
= mib_tree_head
;
598 AggrType type
= atNone
;
601 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
604 while (mibTreeEntry
!= NULL
&& count
< CurrentLen
) {
605 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
606 if (mibTreeEntry
!= NULL
)
607 type
= mibTreeEntry
->aggrType
;
616 snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
)
618 oid_ParseFn
*Fn
= NULL
;
619 mib_tree_entry
*mibTreeEntry
= NULL
, *nextoid
= NULL
;
622 debugs(49, 5, "snmpTreeNext: Called");
625 debugs(49, 6, "snmpTreeNext: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
));
627 mibTreeEntry
= mib_tree_head
;
629 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
632 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
633 mib_tree_entry
*nextmibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
635 if (!nextmibTreeEntry
)
638 mibTreeEntry
= nextmibTreeEntry
;
642 debugs(49, 5, "snmpTreeNext: Recursed down to requested object");
647 if (mibTreeEntry
== mib_tree_last
)
650 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
651 *NextLen
= CurrentLen
;
652 *Next
= (*mibTreeEntry
->instancefunction
) (Current
, NextLen
, mibTreeEntry
, &Fn
);
655 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
660 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
662 nextoid
= snmpTreeSiblingEntry(Current
[count
], count
, mibTreeEntry
->parent
);
664 debugs(49, 5, "snmpTreeNext: Next OID found for sibling" << nextoid
);
665 mibTreeEntry
= nextoid
;
668 debugs(49, 5, "snmpTreeNext: Attempting to recurse up for next object");
673 if (mibTreeEntry
->parent
->parent
) {
674 nextoid
= mibTreeEntry
->parent
;
675 mibTreeEntry
= snmpTreeEntry(Current
[count
] + 1, count
, nextoid
->parent
);
678 mibTreeEntry
= nextoid
;
682 nextoid
= mibTreeEntry
;
688 while ((mibTreeEntry
) && (!mibTreeEntry
->parsefunction
)) {
689 mibTreeEntry
= mibTreeEntry
->leaves
[0];
693 *NextLen
= mibTreeEntry
->len
;
694 *Next
= (*mibTreeEntry
->instancefunction
) (mibTreeEntry
->name
, NextLen
, mibTreeEntry
, &Fn
);
699 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
706 static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
708 oid
*instance
= NULL
;
709 if (*len
<= current
->len
) {
710 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ 1));
711 memcpy(instance
, name
, (sizeof(name
) * *len
));
715 *Fn
= current
->parsefunction
;
720 time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
722 oid
*instance
= NULL
;
723 int identifier
= 0, loop
= 0;
724 int index
[TIME_INDEX_LEN
] = {TIME_INDEX
};
726 if (*len
<= current
->len
) {
727 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ 1));
728 memcpy(instance
, name
, (sizeof(name
) * *len
));
729 instance
[*len
] = *index
;
732 identifier
= name
[*len
- 1];
734 while ((loop
< TIME_INDEX_LEN
) && (identifier
!= index
[loop
]))
737 if (loop
< (TIME_INDEX_LEN
- 1)) {
738 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
));
739 memcpy(instance
, name
, (sizeof(name
) * *len
));
740 instance
[*len
- 1] = index
[++loop
];
744 *Fn
= current
->parsefunction
;
749 peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
751 oid
*instance
= NULL
;
752 CachePeer
*peers
= Config
.peers
;
755 debugs(49, 6, "snmp peer_Inst: No Peers.");
756 current
= current
->parent
->parent
->parent
->leaves
[1];
757 while ((current
) && (!current
->parsefunction
))
758 current
= current
->leaves
[0];
760 instance
= client_Inst(current
->name
, len
, current
, Fn
);
761 } else if (*len
<= current
->len
) {
762 debugs(49, 6, "snmp peer_Inst: *len <= current->len ???");
763 instance
= (oid
*)xmalloc(sizeof(name
) * ( *len
+ 1));
764 memcpy(instance
, name
, (sizeof(name
) * *len
));
768 int no
= name
[current
->len
] ;
770 // Note: This works because the Config.peers keeps its index according to its position.
771 for ( i
=0 ; peers
&& (i
< no
) ; peers
= peers
->next
, ++i
) ;
774 debugs(49, 6, "snmp peer_Inst: Encode peer #" << i
);
775 instance
= (oid
*)xmalloc(sizeof(name
) * (current
->len
+ 1 ));
776 memcpy(instance
, name
, (sizeof(name
) * current
->len
));
777 instance
[current
->len
] = no
+ 1 ; // i.e. the next index on cache_peeer table.
779 debugs(49, 6, "snmp peer_Inst: We have " << i
<< " peers. Can't find #" << no
);
783 *Fn
= current
->parsefunction
;
788 client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
790 oid
*instance
= NULL
;
796 if (*len
<= current
->len
) {
797 aux
= client_entry(NULL
);
804 size
= sizeof(in_addr
);
806 size
= sizeof(in6_addr
);
808 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", size=" << size
);
810 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ size
));
811 memcpy(instance
, name
, (sizeof(name
) * (*len
)));
813 if ( !laddr
.IsAnyAddr() ) {
814 addr2oid(laddr
, &instance
[ *len
]); // the addr
818 int shift
= *len
- current
->len
; // i.e 4 or 16
819 oid2addr(&name
[*len
- shift
], laddr
,shift
);
820 aux
= client_entry(&laddr
);
826 if (!laddr
.IsAnyAddr()) {
828 newshift
= sizeof(in_addr
);
830 newshift
= sizeof(in6_addr
);
832 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", newshift=" << newshift
);
834 instance
= (oid
*)xmalloc(sizeof(name
) * (current
->len
+ newshift
));
835 memcpy(instance
, name
, (sizeof(name
) * (current
->len
)));
836 addr2oid(laddr
, &instance
[current
->len
]); // the addr.
837 *len
= current
->len
+ newshift
;
841 *Fn
= current
->parsefunction
;
850 * Tree utility functions.
854 * Returns a sibling object for the requested child object or NULL
855 * if it does not exit
857 static mib_tree_entry
*
858 snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
)
860 mib_tree_entry
*next
= NULL
;
863 while ((!next
) && (count
< current
->children
)) {
864 if (current
->leaves
[count
]->name
[len
] == entry
) {
865 next
= current
->leaves
[count
];
871 /* Exactly the sibling on right */
872 if (count
< current
->children
) {
873 next
= current
->leaves
[count
];
882 * Returns the requested child object or NULL if it does not exist
884 static mib_tree_entry
*
885 snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
)
887 mib_tree_entry
*next
= NULL
;
890 while ((!next
) && current
&& (count
< current
->children
)) {
891 if (current
->leaves
[count
]->name
[len
] == entry
) {
892 next
= current
->leaves
[count
];
902 snmpAddNodeChild(mib_tree_entry
*entry
, mib_tree_entry
*child
)
904 debugs(49, 5, "snmpAddNodeChild: assigning " << child
<< " to parent " << entry
);
905 entry
->leaves
= (mib_tree_entry
**)xrealloc(entry
->leaves
, sizeof(mib_tree_entry
*) * (entry
->children
+ 1));
906 entry
->leaves
[entry
->children
] = child
;
907 entry
->leaves
[entry
->children
]->parent
= entry
;
912 snmpLookupNodeStr(mib_tree_entry
*root
, const char *str
)
923 if (! snmpCreateOidFromStr(str
, &name
, &namelen
))
926 /* I wish there were some kind of sensible existing tree traversal
927 * routine to use. I'll worry about that later */
930 return e
; /* XXX it should only be this? */
934 while (r
< namelen
) {
936 /* Find the child node which matches this */
937 for (i
= 0; i
< e
->children
&& e
->leaves
[i
]->name
[r
] != name
[r
]; ++i
) ; // seek-loop
939 /* Are we pointing to that node? */
940 if (i
>= e
->children
)
942 assert(e
->leaves
[i
]->name
[r
] == name
[r
]);
944 /* Skip to that node! */
954 snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
)
956 char const *delim
= ".";
961 char *s
= xstrdup(str
);
964 /* Parse the OID string into oid bits */
965 while ( (p
= strsep(&s_
, delim
)) != NULL
) {
966 *name
= (oid
*)xrealloc(*name
, sizeof(oid
) * ((*nl
) + 1));
967 (*name
)[*nl
] = atoi(p
);
976 * Create an entry. Return a pointer to the newly created node, or NULL
979 static mib_tree_entry
*
980 snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
)
982 mib_tree_entry
*m
, *b
;
988 b
= snmpLookupNodeStr(mib_tree_head
, base_str
);
991 debugs(49, 5, "snmpAddNodeStr: " << base_str
<< ": -> " << b
);
993 /* Create OID string for new entry */
994 snprintf(s
, 1024, "%s.%d", base_str
, o
);
995 if (! snmpCreateOidFromStr(s
, &n
, &nl
))
999 m
= snmpAddNode(n
, nl
, parsefunction
, instancefunction
, aggrType
, 0);
1001 /* Link it into the existing tree */
1002 snmpAddNodeChild(b
, m
);
1004 /* Return the node */
1009 * Adds a node to the MIB tree structure and adds the appropriate children
1011 static mib_tree_entry
*
1012 snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
, int children
,...)
1016 mib_tree_entry
*entry
= NULL
;
1017 va_start(args
, children
);
1020 debugs(49, 6, "snmpAddNode: Children : " << children
<< ", Oid : " << snmpDebugOid(name
, len
, tmp
));
1022 va_start(args
, children
);
1023 entry
= (mib_tree_entry
*)xmalloc(sizeof(mib_tree_entry
));
1026 entry
->parsefunction
= parsefunction
;
1027 entry
->instancefunction
= instancefunction
;
1028 entry
->children
= children
;
1029 entry
->leaves
= NULL
;
1030 entry
->aggrType
= aggrType
;
1033 entry
->leaves
= (mib_tree_entry
**)xmalloc(sizeof(mib_tree_entry
*) * children
);
1035 for (loop
= 0; loop
< children
; ++loop
) {
1036 entry
->leaves
[loop
] = va_arg(args
, mib_tree_entry
*);
1037 entry
->leaves
[loop
]->parent
= entry
;
1043 /* End of tree utility functions */
1046 * Returns the list of parameters in an oid
1049 snmpCreateOid(int length
,...)
1054 va_start(args
, length
);
1056 new_oid
= (oid
*)xmalloc(sizeof(oid
) * length
);
1059 for (loop
= 0; loop
< length
; ++loop
) {
1060 new_oid
[loop
] = va_arg(args
, int);
1068 * Debug calls, prints out the OID for debugging purposes.
1071 snmpDebugOid(oid
* Name
, snint Len
, MemBuf
&outbuf
)
1075 if (outbuf
.isNull())
1076 outbuf
.init(16, MAX_IPSTRLEN
);
1078 for (x
= 0; x
< Len
; ++x
) {
1079 size_t bytes
= snprintf(mbuf
, sizeof(mbuf
), ".%u", (unsigned int) Name
[x
]);
1080 outbuf
.append(mbuf
, bytes
);
1082 return outbuf
.content();
1086 snmpSnmplibDebug(int lvl
, char *buf
)
1088 debugs(49, lvl
, buf
);
1092 IPv4 address: 10.10.0.9 ==>
1094 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01 ==>
1095 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1
1098 addr2oid(Ip::Address
&addr
, oid
* Dest
)
1102 struct in_addr i4addr
;
1103 struct in6_addr i6addr
;
1104 oid code
= addr
.IsIPv6()? INETADDRESSTYPE_IPV6
: INETADDRESSTYPE_IPV4
;
1105 u_int size
= (code
== INETADDRESSTYPE_IPV4
) ? sizeof(struct in_addr
):sizeof(struct in6_addr
);
1107 if ( code
== INETADDRESSTYPE_IPV4
) {
1108 addr
.GetInAddr(i4addr
);
1109 cp
= (u_char
*) &(i4addr
.s_addr
);
1111 addr
.GetInAddr(i6addr
);
1112 cp
= (u_char
*) &i6addr
;
1114 for ( i
=0 ; i
< size
; ++i
) {
1115 // OID's are in network order
1120 debugs(49, 7, "addr2oid: Dest : " << snmpDebugOid(Dest
, size
, tmp
));
1124 oid == 10.10.0.9 ==>
1125 IPv4 address: 10.10.0.9
1126 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1 ==>
1127 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01
1130 oid2addr(oid
* id
, Ip::Address
&addr
, u_int size
)
1132 struct in_addr i4addr
;
1133 struct in6_addr i6addr
;
1136 if ( size
== sizeof(struct in_addr
) )
1137 cp
= (u_char
*) &(i4addr
.s_addr
);
1139 cp
= (u_char
*) &(i6addr
);
1141 debugs(49, 7, "oid2addr: id : " << snmpDebugOid(id
, size
, tmp
) );
1142 for (i
=0 ; i
<size
; ++i
) {
1145 if ( size
== sizeof(struct in_addr
) )
1151 /* SNMP checklists */
1152 #include "acl/Strategy.h"
1153 #include "acl/Strategised.h"
1154 #include "acl/StringData.h"
1156 class ACLSNMPCommunityStrategy
: public ACLStrategy
<char const *>
1160 virtual int match (ACLData
<MatchType
> * &, ACLFilledChecklist
*);
1161 static ACLSNMPCommunityStrategy
*Instance();
1162 /* Not implemented to prevent copies of the instance. */
1163 /* Not private to prevent brain dead g++ warnings about
1164 * private constructors with no friends */
1165 ACLSNMPCommunityStrategy(ACLSNMPCommunityStrategy
const &);
1168 static ACLSNMPCommunityStrategy Instance_
;
1169 ACLSNMPCommunityStrategy() {}
1171 ACLSNMPCommunityStrategy
&operator=(ACLSNMPCommunityStrategy
const &);
1174 class ACLSNMPCommunity
1178 static ACL::Prototype RegistryProtoype
;
1179 static ACLStrategised
<char const *> RegistryEntry_
;
1182 ACL::Prototype
ACLSNMPCommunity::RegistryProtoype(&ACLSNMPCommunity::RegistryEntry_
, "snmp_community");
1183 ACLStrategised
<char const *> ACLSNMPCommunity::RegistryEntry_(new ACLStringData
, ACLSNMPCommunityStrategy::Instance(), "snmp_community");
1186 ACLSNMPCommunityStrategy::match (ACLData
<MatchType
> * &data
, ACLFilledChecklist
*checklist
)
1188 return data
->match (checklist
->snmp_community
);
1191 ACLSNMPCommunityStrategy
*
1192 ACLSNMPCommunityStrategy::Instance()
1197 ACLSNMPCommunityStrategy
ACLSNMPCommunityStrategy::Instance_
;