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 "SnmpRequest.h"
47 #include "SquidConfig.h"
50 static void snmpPortOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
52 mib_tree_entry
*mib_tree_head
;
53 mib_tree_entry
*mib_tree_last
;
55 Comm::ConnectionPointer snmpIncomingConn
;
56 Comm::ConnectionPointer snmpOutgoingConn
;
58 static mib_tree_entry
* snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
= atNone
);
59 static mib_tree_entry
*snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
, int children
,...);
60 static oid
*snmpCreateOid(int length
,...);
61 mib_tree_entry
* snmpLookupNodeStr(mib_tree_entry
*entry
, const char *str
);
62 int snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
);
63 SQUIDCEXTERN
void (*snmplib_debug_hook
) (int, char *);
64 static oid
*static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
65 static oid
*time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
66 static oid
*peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
67 static oid
*client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
68 static void snmpDecodePacket(SnmpRequest
* rq
);
69 static void snmpConstructReponse(SnmpRequest
* rq
);
71 static oid_ParseFn
*snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
);
72 static oid_ParseFn
*snmpTreeGet(oid
* Current
, snint CurrentLen
);
73 static mib_tree_entry
*snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
);
74 static mib_tree_entry
*snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
);
75 extern "C" void snmpSnmplibDebug(int lvl
, char *buf
);
78 * The functions used during startup:
85 * Turns the MIB into a Tree structure. Called during the startup process.
90 debugs(49, 5, "snmpInit: Building SNMP mib tree structure");
92 snmplib_debug_hook
= snmpSnmplibDebug
;
95 * This following bit of evil is to get the final node in the "squid" mib
96 * without having a "search" function. A search function should be written
97 * to make this and the other code much less evil.
99 mib_tree_head
= snmpAddNode(snmpCreateOid(1, 1), 1, NULL
, NULL
, atNone
, 0);
101 assert(mib_tree_head
);
102 debugs(49, 5, "snmpInit: root is " << mib_tree_head
);
103 snmpAddNodeStr("1", 3, NULL
, NULL
);
105 snmpAddNodeStr("1.3", 6, NULL
, NULL
);
107 snmpAddNodeStr("1.3.6", 1, NULL
, NULL
);
108 snmpAddNodeStr("1.3.6.1", 4, NULL
, NULL
);
109 snmpAddNodeStr("1.3.6.1.4", 1, NULL
, NULL
);
110 snmpAddNodeStr("1.3.6.1.4.1", 3495, NULL
, NULL
);
111 mib_tree_entry
*m2
= snmpAddNodeStr("1.3.6.1.4.1.3495", 1, NULL
, NULL
);
113 mib_tree_entry
*n
= snmpLookupNodeStr(NULL
, "1.3.6.1.4.1.3495.1");
116 /* SQ_SYS - 1.3.6.1.4.1.3495.1.1 */
117 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 1, NULL
, NULL
);
118 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSVMSIZ
, snmp_sysFn
, static_Inst
, atSum
);
119 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSSTOR
, snmp_sysFn
, static_Inst
, atSum
);
120 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYS_UPTIME
, snmp_sysFn
, static_Inst
, atMax
);
122 /* SQ_CONF - 1.3.6.1.4.1.3495.1.2 */
123 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 2, NULL
, NULL
);
124 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_ADMIN
, snmp_confFn
, static_Inst
);
125 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION
, snmp_confFn
, static_Inst
);
126 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION_ID
, snmp_confFn
, static_Inst
);
127 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_LOG_FAC
, snmp_confFn
, static_Inst
);
129 /* SQ_CONF + CONF_STORAGE - 1.3.6.1.4.1.3495.1.5 */
130 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_STORAGE
, NULL
, NULL
);
131 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_MMAXSZ
, snmp_confFn
, static_Inst
, atSum
);
132 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWMAXSZ
, snmp_confFn
, static_Inst
, atSum
);
133 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWHIWM
, snmp_confFn
, static_Inst
, atMin
);
134 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWLOWM
, snmp_confFn
, static_Inst
, atMin
);
136 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_UNIQNAME
, snmp_confFn
, static_Inst
);
138 /* SQ_PRF - 1.3.6.1.4.1.3495.1.3 */
139 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 3, NULL
, NULL
); /* SQ_PRF */
141 /* PERF_SYS - 1.3.6.1.4.1.3495.1.3.1 */
142 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_SYS
, NULL
, NULL
);
143 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_PF
, snmp_prfSysFn
, static_Inst
, atSum
);
144 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMR
, snmp_prfSysFn
, static_Inst
, atSum
);
145 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MEMUSAGE
, snmp_prfSysFn
, static_Inst
, atSum
);
146 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUTIME
, snmp_prfSysFn
, static_Inst
, atSum
);
147 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUUSAGE
, snmp_prfSysFn
, static_Inst
, atSum
);
148 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MAXRESSZ
, snmp_prfSysFn
, static_Inst
, atSum
);
149 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMOBJCNT
, snmp_prfSysFn
, static_Inst
, atSum
);
152 The meaning of LRU is "oldest timestamped object in cache, if LRU algorithm is
154 What this SMP support needs to do is aggregate via a special filter equivalent to
155 min() to retain the semantic oldest-object meaning. A special one is needed that
156 works as unsigned and ignores '0' values.
158 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURLRUEXP
, snmp_prfSysFn
, static_Inst
);
159 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNLREQ
, snmp_prfSysFn
, static_Inst
, atSum
);
160 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNUSED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
161 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURRESERVED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
162 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUSED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
163 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURMAX_FD
, snmp_prfSysFn
, static_Inst
, atMax
);
165 /* PERF_PROTO - 1.3.6.1.4.1.3495.1.3.2 */
166 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_PROTO
, NULL
, NULL
);
167 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_AGGR
, NULL
, NULL
);
168 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_REQ
, snmp_prfProtoFn
, static_Inst
, atSum
);
169 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_HITS
, snmp_prfProtoFn
, static_Inst
, atSum
);
170 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_ERRORS
, snmp_prfProtoFn
, static_Inst
, atSum
);
171 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
, atSum
);
172 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
, atSum
);
173 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_S
, snmp_prfProtoFn
, static_Inst
, atSum
);
174 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_R
, snmp_prfProtoFn
, static_Inst
, atSum
);
175 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_SKB
, snmp_prfProtoFn
, static_Inst
, atSum
);
176 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_RKB
, snmp_prfProtoFn
, static_Inst
, atSum
);
177 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_REQ
, snmp_prfProtoFn
, static_Inst
, atSum
);
178 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ERRORS
, snmp_prfProtoFn
, static_Inst
, atSum
);
179 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
, atSum
);
180 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
, atSum
);
181 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CURSWAP
, snmp_prfProtoFn
, static_Inst
, atSum
);
182 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CLIENTS
, snmp_prfProtoFn
, static_Inst
, atSum
);
184 /* Note this is time-series rather than 'static' */
185 /* cacheMedianSvcTable */
186 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_MEDIAN
, NULL
, NULL
);
188 /* cacheMedianSvcEntry */
189 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2", 1, NULL
, NULL
);
190 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_TIME
, snmp_prfProtoFn
, time_Inst
, atAverage
);
191 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_ALL
, snmp_prfProtoFn
, time_Inst
, atAverage
);
192 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_MISS
, snmp_prfProtoFn
, time_Inst
, atAverage
);
193 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NM
, snmp_prfProtoFn
, time_Inst
, atAverage
);
194 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_HIT
, snmp_prfProtoFn
, time_Inst
, atAverage
);
195 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_QUERY
, snmp_prfProtoFn
, time_Inst
, atAverage
);
196 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_REPLY
, snmp_prfProtoFn
, time_Inst
, atAverage
);
197 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_DNS
, snmp_prfProtoFn
, time_Inst
, atAverage
);
198 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_RHR
, snmp_prfProtoFn
, time_Inst
, atAverage
);
199 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_BHR
, snmp_prfProtoFn
, time_Inst
, atAverage
);
200 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NH
, snmp_prfProtoFn
, time_Inst
, atAverage
);
202 /* SQ_NET - 1.3.6.1.4.1.3495.1.4 */
203 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 4, NULL
, NULL
);
205 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_IP_CACHE
, NULL
, NULL
);
206 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_ENT
, snmp_netIpFn
, static_Inst
, atSum
);
207 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_REQ
, snmp_netIpFn
, static_Inst
, atSum
);
208 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_HITS
, snmp_netIpFn
, static_Inst
, atSum
);
209 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_PENDHIT
, snmp_netIpFn
, static_Inst
, atSum
);
210 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_NEGHIT
, snmp_netIpFn
, static_Inst
, atSum
);
211 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_MISS
, snmp_netIpFn
, static_Inst
, atSum
);
212 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_GHBN
, snmp_netIpFn
, static_Inst
, atSum
);
213 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_LOC
, snmp_netIpFn
, static_Inst
, atSum
);
215 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_FQDN_CACHE
, NULL
, NULL
);
216 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_ENT
, snmp_netFqdnFn
, static_Inst
, atSum
);
217 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_REQ
, snmp_netFqdnFn
, static_Inst
, atSum
);
218 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_HITS
, snmp_netFqdnFn
, static_Inst
, atSum
);
219 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_PENDHIT
, snmp_netFqdnFn
, static_Inst
, atSum
);
220 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_NEGHIT
, snmp_netFqdnFn
, static_Inst
, atSum
);
221 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_MISS
, snmp_netFqdnFn
, static_Inst
, atSum
);
222 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_GHBN
, snmp_netFqdnFn
, static_Inst
, atSum
);
224 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_DNS_CACHE
, NULL
, NULL
);
225 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REQ
, snmp_netDnsFn
, static_Inst
, atSum
);
226 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REP
, snmp_netDnsFn
, static_Inst
, atSum
);
227 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_SERVERS
, snmp_netDnsFn
, static_Inst
, atSum
);
229 /* SQ_MESH - 1.3.6.1.4.1.3495.1.5 */
230 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 5, NULL
, NULL
);
232 /* cachePeerTable - 1.3.6.1.4.1.3495.1.5.1 */
233 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_PTBL
, NULL
, NULL
);
235 /* CachePeerTableEntry (version 3) - 1.3.6.1.4.1.3495.1.5.1.3 */
236 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1", 3, NULL
, NULL
);
237 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_INDEX
, snmp_meshPtblFn
, peer_Inst
);
238 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_NAME
, snmp_meshPtblFn
, peer_Inst
);
239 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR_TYPE
, snmp_meshPtblFn
, peer_Inst
);
240 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR
, snmp_meshPtblFn
, peer_Inst
);
241 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_HTTP
, snmp_meshPtblFn
, peer_Inst
);
242 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ICP
, snmp_meshPtblFn
, peer_Inst
);
243 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_TYPE
, snmp_meshPtblFn
, peer_Inst
);
244 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_STATE
, snmp_meshPtblFn
, peer_Inst
);
245 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_SENT
, snmp_meshPtblFn
, peer_Inst
);
246 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_PACKED
, snmp_meshPtblFn
, peer_Inst
);
247 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_FETCHES
, snmp_meshPtblFn
, peer_Inst
);
248 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_RTT
, snmp_meshPtblFn
, peer_Inst
);
249 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_IGN
, snmp_meshPtblFn
, peer_Inst
);
250 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_S
, snmp_meshPtblFn
, peer_Inst
);
251 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_R
, snmp_meshPtblFn
, peer_Inst
);
253 /* cacheClientTable - 1.3.6.1.4.1.3495.1.5.2 */
254 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_CTBL
, NULL
, NULL
);
256 /* BUG 2811: we NEED to create a reliable index for the client DB and make version 3 of the table. */
257 /* for now we have version 2 table with OID capable of mixed IPv4 / IPv6 clients and upgraded address text format. */
259 /* cacheClientEntry - 1.3.6.1.4.1.3495.1.5.2.2 */
260 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2", 2, NULL
, NULL
);
261 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR_TYPE
, snmp_meshCtblFn
, client_Inst
);
262 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR
, snmp_meshCtblFn
, client_Inst
);
263 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTREQ
, snmp_meshCtblFn
, client_Inst
);
264 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTBYTES
, snmp_meshCtblFn
, client_Inst
);
265 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITS
, snmp_meshCtblFn
, client_Inst
);
266 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITBYTES
, snmp_meshCtblFn
, client_Inst
);
267 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPREQ
, snmp_meshCtblFn
, client_Inst
);
268 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPBYTES
, snmp_meshCtblFn
, client_Inst
);
269 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITS
, snmp_meshCtblFn
, client_Inst
);
270 mib_tree_last
= snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITBYTES
, snmp_meshCtblFn
, client_Inst
);
272 debugs(49, 9, "snmpInit: Completed SNMP mib tree structure");
278 debugs(49, 5, "snmpConnectionOpen: Called");
280 if (Config
.Port
.snmp
<= 0)
283 snmpIncomingConn
= new Comm::Connection
;
284 snmpIncomingConn
->local
= Config
.Addrs
.snmp_incoming
;
285 snmpIncomingConn
->local
.SetPort(Config
.Port
.snmp
);
287 if (!Ip::EnableIpv6
&& !snmpIncomingConn
->local
.SetIPv4()) {
288 debugs(49, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << snmpIncomingConn
->local
<< " is not an IPv4 address.");
289 fatal("SNMP port cannot be opened.");
291 /* split-stack for now requires IPv4-only SNMP */
292 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& snmpIncomingConn
->local
.IsAnyAddr()) {
293 snmpIncomingConn
->local
.SetIPv4();
296 AsyncCall::Pointer call
= asyncCall(49, 2, "snmpIncomingConnectionOpened",
297 Comm::UdpOpenDialer(&snmpPortOpened
));
298 Ipc::StartListening(SOCK_DGRAM
, IPPROTO_UDP
, snmpIncomingConn
, Ipc::fdnInSnmpSocket
, call
);
300 if (!Config
.Addrs
.snmp_outgoing
.IsNoAddr()) {
301 snmpOutgoingConn
= new Comm::Connection
;
302 snmpOutgoingConn
->local
= Config
.Addrs
.snmp_outgoing
;
303 snmpOutgoingConn
->local
.SetPort(Config
.Port
.snmp
);
305 if (!Ip::EnableIpv6
&& !snmpOutgoingConn
->local
.SetIPv4()) {
306 debugs(49, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << snmpOutgoingConn
->local
<< " is not an IPv4 address.");
307 fatal("SNMP port cannot be opened.");
309 /* split-stack for now requires IPv4-only SNMP */
310 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& snmpOutgoingConn
->local
.IsAnyAddr()) {
311 snmpOutgoingConn
->local
.SetIPv4();
313 AsyncCall::Pointer call
= asyncCall(49, 2, "snmpOutgoingConnectionOpened",
314 Comm::UdpOpenDialer(&snmpPortOpened
));
315 Ipc::StartListening(SOCK_DGRAM
, IPPROTO_UDP
, snmpOutgoingConn
, Ipc::fdnOutSnmpSocket
, call
);
317 snmpOutgoingConn
= snmpIncomingConn
;
318 debugs(1, DBG_IMPORTANT
, "Sending SNMP messages from " << snmpOutgoingConn
->local
);
323 snmpPortOpened(const Comm::ConnectionPointer
&conn
, int errNo
)
325 if (!Comm::IsConnOpen(conn
))
326 fatalf("Cannot open SNMP %s Port",(conn
->fd
== snmpIncomingConn
->fd
?"receiving":"sending"));
328 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
330 if (conn
->fd
== snmpIncomingConn
->fd
)
331 debugs(1, DBG_IMPORTANT
, "Accepting SNMP messages on " << snmpIncomingConn
->local
);
332 else if (conn
->fd
== snmpOutgoingConn
->fd
)
333 debugs(1, DBG_IMPORTANT
, "Sending SNMP messages from " << snmpOutgoingConn
->local
);
335 fatalf("Lost SNMP port (%d) on FD %d", (int)conn
->local
.GetPort(), conn
->fd
);
341 if (Comm::IsConnOpen(snmpIncomingConn
)) {
342 debugs(49, DBG_IMPORTANT
, "Closing SNMP receiving port " << snmpIncomingConn
->local
);
343 snmpIncomingConn
->close();
345 snmpIncomingConn
= NULL
;
347 if (Comm::IsConnOpen(snmpOutgoingConn
) && snmpIncomingConn
!= snmpOutgoingConn
) {
348 // Perform OUT port closure so as not to step on IN port when sharing a conn.
349 debugs(49, DBG_IMPORTANT
, "Closing SNMP sending port " << snmpOutgoingConn
->local
);
350 snmpOutgoingConn
->close();
352 snmpOutgoingConn
= NULL
;
356 * Functions for handling the requests.
360 * Accept the UDP packet
363 snmpHandleUdp(int sock
, void *not_used
)
365 LOCAL_ARRAY(char, buf
, SNMP_REQUEST_SIZE
);
367 SnmpRequest
*snmp_rq
;
370 debugs(49, 5, "snmpHandleUdp: Called.");
372 Comm::SetSelect(sock
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
374 memset(buf
, '\0', SNMP_REQUEST_SIZE
);
376 len
= comm_udp_recvfrom(sock
,
384 debugs(49, 3, "snmpHandleUdp: FD " << sock
<< ": received " << len
<< " bytes from " << from
<< ".");
386 snmp_rq
= (SnmpRequest
*)xcalloc(1, sizeof(SnmpRequest
));
387 snmp_rq
->buf
= (u_char
*) buf
;
389 snmp_rq
->sock
= sock
;
390 snmp_rq
->outbuf
= (unsigned char *)xmalloc(snmp_rq
->outlen
= SNMP_REQUEST_SIZE
);
391 snmp_rq
->from
= from
;
392 snmpDecodePacket(snmp_rq
);
393 xfree(snmp_rq
->outbuf
);
396 debugs(49, DBG_IMPORTANT
, "snmpHandleUdp: FD " << sock
<< " recvfrom: " << xstrerror());
401 * Turn SNMP packet into a PDU, check available ACL's
404 snmpDecodePacket(SnmpRequest
* rq
)
406 struct snmp_pdu
*PDU
;
408 u_char
*buf
= rq
->buf
;
410 allow_t allow
= ACCESS_DENIED
;
412 if (!Config
.accessList
.snmp
) {
413 debugs(49, DBG_IMPORTANT
, "WARNING: snmp_access not configured. agent query DENIED from : " << rq
->from
);
417 debugs(49, 5, HERE
<< "Called.");
418 PDU
= snmp_pdu_create(0);
419 /* Allways answer on SNMPv1 */
420 rq
->session
.Version
= SNMP_VERSION_1
;
421 Community
= snmp_parse(&rq
->session
, PDU
, buf
, len
);
423 /* Check if we have explicit permission to access SNMP data.
424 * default (set above) is to deny all */
426 ACLFilledChecklist
checklist(Config
.accessList
.snmp
, NULL
, NULL
);
427 checklist
.src_addr
= rq
->from
;
428 checklist
.snmp_community
= (char *) Community
;
429 allow
= checklist
.fastCheck();
431 if (allow
== ACCESS_ALLOWED
&& (snmp_coexist_V2toV1(PDU
))) {
432 rq
->community
= Community
;
434 debugs(49, 5, "snmpAgentParse: reqid=[" << PDU
->reqid
<< "]");
435 snmpConstructReponse(rq
);
437 debugs(49, DBG_IMPORTANT
, "WARNING: SNMP agent query DENIED from : " << rq
->from
);
442 debugs(49, DBG_IMPORTANT
, "WARNING: Failed SNMP agent query from : " << rq
->from
);
448 * Packet OK, ACL Check OK, Create reponse.
451 snmpConstructReponse(SnmpRequest
* rq
)
454 struct snmp_pdu
*RespPDU
;
456 debugs(49, 5, "snmpConstructReponse: Called.");
458 if (UsingSmp() && IamWorkerProcess()) {
459 AsyncJob::Start(new Snmp::Forwarder(static_cast<Snmp::Pdu
&>(*rq
->PDU
),
460 static_cast<Snmp::Session
&>(rq
->session
), rq
->sock
, rq
->from
));
461 snmp_free_pdu(rq
->PDU
);
465 RespPDU
= snmpAgentResponse(rq
->PDU
);
466 snmp_free_pdu(rq
->PDU
);
468 if (RespPDU
!= NULL
) {
469 snmp_build(&rq
->session
, RespPDU
, rq
->outbuf
, &rq
->outlen
);
470 comm_udp_sendto(rq
->sock
, rq
->from
, rq
->outbuf
, rq
->outlen
);
471 snmp_free_pdu(RespPDU
);
476 * Decide how to respond to the request, construct a response and
477 * return the response to the requester.
481 snmpAgentResponse(struct snmp_pdu
*PDU
) {
483 struct snmp_pdu
*Answer
= NULL
;
485 debugs(49, 5, "snmpAgentResponse: Called.");
487 if ((Answer
= snmp_pdu_create(SNMP_PDU_RESPONSE
))) {
488 Answer
->reqid
= PDU
->reqid
;
489 Answer
->errindex
= 0;
491 if (PDU
->command
== SNMP_PDU_GET
|| PDU
->command
== SNMP_PDU_GETNEXT
) {
493 int get_next
= (PDU
->command
== SNMP_PDU_GETNEXT
);
494 variable_list
*VarPtr_
;
495 variable_list
**RespVars
= &(Answer
->variables
);
496 oid_ParseFn
*ParseFn
;
498 /* Loop through all variables */
500 for (VarPtr_
= PDU
->variables
; VarPtr_
; VarPtr_
= VarPtr_
->next_variable
) {
501 variable_list
*VarPtr
= VarPtr_
;
502 variable_list
*VarNew
= NULL
;
503 oid
*NextOidName
= NULL
;
504 snint NextOidNameLen
= 0;
509 ParseFn
= snmpTreeNext(VarPtr
->name
, VarPtr
->name_length
, &NextOidName
, &NextOidNameLen
);
511 ParseFn
= snmpTreeGet(VarPtr
->name
, VarPtr
->name_length
);
513 if (ParseFn
== NULL
) {
514 Answer
->errstat
= SNMP_ERR_NOSUCHNAME
;
515 debugs(49, 5, "snmpAgentResponse: No such oid. ");
518 VarPtr
= snmp_var_new(NextOidName
, NextOidNameLen
);
522 int * errstatTmp
= &(Answer
->errstat
);
524 VarNew
= (*ParseFn
) (VarPtr
, (snint
*) errstatTmp
);
527 snmp_var_free(VarPtr
);
530 if ((Answer
->errstat
!= SNMP_ERR_NOERROR
) || (VarNew
== NULL
)) {
531 Answer
->errindex
= index
;
532 debugs(49, 5, "snmpAgentResponse: error.");
535 snmp_var_free(VarNew
);
537 while ((VarPtr
= Answer
->variables
) != NULL
) {
538 Answer
->variables
= VarPtr
->next_variable
;
539 snmp_var_free(VarPtr
);
542 /* Steal the original PDU list of variables for the error response */
543 Answer
->variables
= PDU
->variables
;
545 PDU
->variables
= NULL
;
550 /* No error. Insert this var at the end, and move on to the next.
554 RespVars
= &(VarNew
->next_variable
);
563 snmpTreeGet(oid
* Current
, snint CurrentLen
)
565 oid_ParseFn
*Fn
= NULL
;
566 mib_tree_entry
*mibTreeEntry
= NULL
;
569 debugs(49, 5, "snmpTreeGet: Called");
572 debugs(49, 6, "snmpTreeGet: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
) );
574 mibTreeEntry
= mib_tree_head
;
576 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
579 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
580 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
585 if (mibTreeEntry
&& mibTreeEntry
->parsefunction
)
586 Fn
= mibTreeEntry
->parsefunction
;
588 debugs(49, 5, "snmpTreeGet: return");
594 snmpAggrType(oid
* Current
, snint CurrentLen
)
598 mib_tree_entry
* mibTreeEntry
= mib_tree_head
;
599 AggrType type
= atNone
;
602 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
605 while (mibTreeEntry
!= NULL
&& count
< CurrentLen
) {
606 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
607 if (mibTreeEntry
!= NULL
)
608 type
= mibTreeEntry
->aggrType
;
617 snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
)
619 oid_ParseFn
*Fn
= NULL
;
620 mib_tree_entry
*mibTreeEntry
= NULL
, *nextoid
= NULL
;
623 debugs(49, 5, "snmpTreeNext: Called");
626 debugs(49, 6, "snmpTreeNext: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
));
628 mibTreeEntry
= mib_tree_head
;
630 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
633 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
634 mib_tree_entry
*nextmibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
636 if (!nextmibTreeEntry
)
639 mibTreeEntry
= nextmibTreeEntry
;
643 debugs(49, 5, "snmpTreeNext: Recursed down to requested object");
648 if (mibTreeEntry
== mib_tree_last
)
651 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
652 *NextLen
= CurrentLen
;
653 *Next
= (*mibTreeEntry
->instancefunction
) (Current
, NextLen
, mibTreeEntry
, &Fn
);
656 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
661 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
663 nextoid
= snmpTreeSiblingEntry(Current
[count
], count
, mibTreeEntry
->parent
);
665 debugs(49, 5, "snmpTreeNext: Next OID found for sibling" << nextoid
);
666 mibTreeEntry
= nextoid
;
669 debugs(49, 5, "snmpTreeNext: Attempting to recurse up for next object");
674 if (mibTreeEntry
->parent
->parent
) {
675 nextoid
= mibTreeEntry
->parent
;
676 mibTreeEntry
= snmpTreeEntry(Current
[count
] + 1, count
, nextoid
->parent
);
679 mibTreeEntry
= nextoid
;
683 nextoid
= mibTreeEntry
;
689 while ((mibTreeEntry
) && (!mibTreeEntry
->parsefunction
)) {
690 mibTreeEntry
= mibTreeEntry
->leaves
[0];
694 *NextLen
= mibTreeEntry
->len
;
695 *Next
= (*mibTreeEntry
->instancefunction
) (mibTreeEntry
->name
, NextLen
, mibTreeEntry
, &Fn
);
700 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
707 static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
709 oid
*instance
= NULL
;
710 if (*len
<= current
->len
) {
711 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ 1));
712 memcpy(instance
, name
, (sizeof(name
) * *len
));
716 *Fn
= current
->parsefunction
;
721 time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
723 oid
*instance
= NULL
;
724 int identifier
= 0, loop
= 0;
725 int index
[TIME_INDEX_LEN
] = {TIME_INDEX
};
727 if (*len
<= current
->len
) {
728 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ 1));
729 memcpy(instance
, name
, (sizeof(name
) * *len
));
730 instance
[*len
] = *index
;
733 identifier
= name
[*len
- 1];
735 while ((loop
< TIME_INDEX_LEN
) && (identifier
!= index
[loop
]))
738 if (loop
< (TIME_INDEX_LEN
- 1)) {
739 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
));
740 memcpy(instance
, name
, (sizeof(name
) * *len
));
741 instance
[*len
- 1] = index
[++loop
];
745 *Fn
= current
->parsefunction
;
750 peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
752 oid
*instance
= NULL
;
753 CachePeer
*peers
= Config
.peers
;
756 debugs(49, 6, "snmp peer_Inst: No Peers.");
757 current
= current
->parent
->parent
->parent
->leaves
[1];
758 while ((current
) && (!current
->parsefunction
))
759 current
= current
->leaves
[0];
761 instance
= client_Inst(current
->name
, len
, current
, Fn
);
762 } else if (*len
<= current
->len
) {
763 debugs(49, 6, "snmp peer_Inst: *len <= current->len ???");
764 instance
= (oid
*)xmalloc(sizeof(name
) * ( *len
+ 1));
765 memcpy(instance
, name
, (sizeof(name
) * *len
));
769 int no
= name
[current
->len
] ;
771 // Note: This works because the Config.peers keeps its index according to its position.
772 for ( i
=0 ; peers
&& (i
< no
) ; peers
= peers
->next
, ++i
) ;
775 debugs(49, 6, "snmp peer_Inst: Encode peer #" << i
);
776 instance
= (oid
*)xmalloc(sizeof(name
) * (current
->len
+ 1 ));
777 memcpy(instance
, name
, (sizeof(name
) * current
->len
));
778 instance
[current
->len
] = no
+ 1 ; // i.e. the next index on cache_peeer table.
780 debugs(49, 6, "snmp peer_Inst: We have " << i
<< " peers. Can't find #" << no
);
784 *Fn
= current
->parsefunction
;
789 client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
791 oid
*instance
= NULL
;
797 if (*len
<= current
->len
) {
798 aux
= client_entry(NULL
);
805 size
= sizeof(in_addr
);
807 size
= sizeof(in6_addr
);
809 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", size=" << size
);
811 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ size
));
812 memcpy(instance
, name
, (sizeof(name
) * (*len
)));
814 if ( !laddr
.IsAnyAddr() ) {
815 addr2oid(laddr
, &instance
[ *len
]); // the addr
819 int shift
= *len
- current
->len
; // i.e 4 or 16
820 oid2addr(&name
[*len
- shift
], laddr
,shift
);
821 aux
= client_entry(&laddr
);
827 if (!laddr
.IsAnyAddr()) {
829 newshift
= sizeof(in_addr
);
831 newshift
= sizeof(in6_addr
);
833 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", newshift=" << newshift
);
835 instance
= (oid
*)xmalloc(sizeof(name
) * (current
->len
+ newshift
));
836 memcpy(instance
, name
, (sizeof(name
) * (current
->len
)));
837 addr2oid(laddr
, &instance
[current
->len
]); // the addr.
838 *len
= current
->len
+ newshift
;
842 *Fn
= current
->parsefunction
;
851 * Tree utility functions.
855 * Returns a sibling object for the requested child object or NULL
856 * if it does not exit
858 static mib_tree_entry
*
859 snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
)
861 mib_tree_entry
*next
= NULL
;
864 while ((!next
) && (count
< current
->children
)) {
865 if (current
->leaves
[count
]->name
[len
] == entry
) {
866 next
= current
->leaves
[count
];
872 /* Exactly the sibling on right */
873 if (count
< current
->children
) {
874 next
= current
->leaves
[count
];
883 * Returns the requested child object or NULL if it does not exist
885 static mib_tree_entry
*
886 snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
)
888 mib_tree_entry
*next
= NULL
;
891 while ((!next
) && current
&& (count
< current
->children
)) {
892 if (current
->leaves
[count
]->name
[len
] == entry
) {
893 next
= current
->leaves
[count
];
903 snmpAddNodeChild(mib_tree_entry
*entry
, mib_tree_entry
*child
)
905 debugs(49, 5, "snmpAddNodeChild: assigning " << child
<< " to parent " << entry
);
906 entry
->leaves
= (mib_tree_entry
**)xrealloc(entry
->leaves
, sizeof(mib_tree_entry
*) * (entry
->children
+ 1));
907 entry
->leaves
[entry
->children
] = child
;
908 entry
->leaves
[entry
->children
]->parent
= entry
;
913 snmpLookupNodeStr(mib_tree_entry
*root
, const char *str
)
924 if (! snmpCreateOidFromStr(str
, &name
, &namelen
))
927 /* I wish there were some kind of sensible existing tree traversal
928 * routine to use. I'll worry about that later */
931 return e
; /* XXX it should only be this? */
935 while (r
< namelen
) {
937 /* Find the child node which matches this */
938 for (i
= 0; i
< e
->children
&& e
->leaves
[i
]->name
[r
] != name
[r
]; ++i
) ; // seek-loop
940 /* Are we pointing to that node? */
941 if (i
>= e
->children
)
943 assert(e
->leaves
[i
]->name
[r
] == name
[r
]);
945 /* Skip to that node! */
955 snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
)
957 char const *delim
= ".";
962 char *s
= xstrdup(str
);
965 /* Parse the OID string into oid bits */
966 while ( (p
= strsep(&s_
, delim
)) != NULL
) {
967 *name
= (oid
*)xrealloc(*name
, sizeof(oid
) * ((*nl
) + 1));
968 (*name
)[*nl
] = atoi(p
);
977 * Create an entry. Return a pointer to the newly created node, or NULL
980 static mib_tree_entry
*
981 snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
)
983 mib_tree_entry
*m
, *b
;
989 b
= snmpLookupNodeStr(mib_tree_head
, base_str
);
992 debugs(49, 5, "snmpAddNodeStr: " << base_str
<< ": -> " << b
);
994 /* Create OID string for new entry */
995 snprintf(s
, 1024, "%s.%d", base_str
, o
);
996 if (! snmpCreateOidFromStr(s
, &n
, &nl
))
1000 m
= snmpAddNode(n
, nl
, parsefunction
, instancefunction
, aggrType
, 0);
1002 /* Link it into the existing tree */
1003 snmpAddNodeChild(b
, m
);
1005 /* Return the node */
1010 * Adds a node to the MIB tree structure and adds the appropriate children
1012 static mib_tree_entry
*
1013 snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
, int children
,...)
1017 mib_tree_entry
*entry
= NULL
;
1018 va_start(args
, children
);
1021 debugs(49, 6, "snmpAddNode: Children : " << children
<< ", Oid : " << snmpDebugOid(name
, len
, tmp
));
1023 va_start(args
, children
);
1024 entry
= (mib_tree_entry
*)xmalloc(sizeof(mib_tree_entry
));
1027 entry
->parsefunction
= parsefunction
;
1028 entry
->instancefunction
= instancefunction
;
1029 entry
->children
= children
;
1030 entry
->leaves
= NULL
;
1031 entry
->aggrType
= aggrType
;
1034 entry
->leaves
= (mib_tree_entry
**)xmalloc(sizeof(mib_tree_entry
*) * children
);
1036 for (loop
= 0; loop
< children
; ++loop
) {
1037 entry
->leaves
[loop
] = va_arg(args
, mib_tree_entry
*);
1038 entry
->leaves
[loop
]->parent
= entry
;
1045 /* End of tree utility functions */
1048 * Returns the list of parameters in an oid
1051 snmpCreateOid(int length
,...)
1056 va_start(args
, length
);
1058 new_oid
= (oid
*)xmalloc(sizeof(oid
) * length
);
1061 for (loop
= 0; loop
< length
; ++loop
) {
1062 new_oid
[loop
] = va_arg(args
, int);
1071 * Debug calls, prints out the OID for debugging purposes.
1074 snmpDebugOid(oid
* Name
, snint Len
, MemBuf
&outbuf
)
1078 if (outbuf
.isNull())
1079 outbuf
.init(16, MAX_IPSTRLEN
);
1081 for (x
= 0; x
< Len
; ++x
) {
1082 size_t bytes
= snprintf(mbuf
, sizeof(mbuf
), ".%u", (unsigned int) Name
[x
]);
1083 outbuf
.append(mbuf
, bytes
);
1085 return outbuf
.content();
1089 snmpSnmplibDebug(int lvl
, char *buf
)
1091 debugs(49, lvl
, buf
);
1095 IPv4 address: 10.10.0.9 ==>
1097 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01 ==>
1098 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1
1101 addr2oid(Ip::Address
&addr
, oid
* Dest
)
1105 struct in_addr i4addr
;
1106 struct in6_addr i6addr
;
1107 oid code
= addr
.IsIPv6()? INETADDRESSTYPE_IPV6
: INETADDRESSTYPE_IPV4
;
1108 u_int size
= (code
== INETADDRESSTYPE_IPV4
) ? sizeof(struct in_addr
):sizeof(struct in6_addr
);
1110 if ( code
== INETADDRESSTYPE_IPV4
) {
1111 addr
.GetInAddr(i4addr
);
1112 cp
= (u_char
*) &(i4addr
.s_addr
);
1114 addr
.GetInAddr(i6addr
);
1115 cp
= (u_char
*) &i6addr
;
1117 for ( i
=0 ; i
< size
; ++i
) {
1118 // OID's are in network order
1123 debugs(49, 7, "addr2oid: Dest : " << snmpDebugOid(Dest
, size
, tmp
));
1127 oid == 10.10.0.9 ==>
1128 IPv4 address: 10.10.0.9
1129 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1 ==>
1130 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01
1133 oid2addr(oid
* id
, Ip::Address
&addr
, u_int size
)
1135 struct in_addr i4addr
;
1136 struct in6_addr i6addr
;
1139 if ( size
== sizeof(struct in_addr
) )
1140 cp
= (u_char
*) &(i4addr
.s_addr
);
1142 cp
= (u_char
*) &(i6addr
);
1144 debugs(49, 7, "oid2addr: id : " << snmpDebugOid(id
, size
, tmp
) );
1145 for (i
=0 ; i
<size
; ++i
) {
1148 if ( size
== sizeof(struct in_addr
) )
1154 /* SNMP checklists */
1155 #include "acl/Strategy.h"
1156 #include "acl/Strategised.h"
1157 #include "acl/StringData.h"
1159 class ACLSNMPCommunityStrategy
: public ACLStrategy
<char const *>
1163 virtual int match (ACLData
<MatchType
> * &, ACLFilledChecklist
*);
1164 static ACLSNMPCommunityStrategy
*Instance();
1165 /* Not implemented to prevent copies of the instance. */
1166 /* Not private to prevent brain dead g++ warnings about
1167 * private constructors with no friends */
1168 ACLSNMPCommunityStrategy(ACLSNMPCommunityStrategy
const &);
1171 static ACLSNMPCommunityStrategy Instance_
;
1172 ACLSNMPCommunityStrategy() {}
1174 ACLSNMPCommunityStrategy
&operator=(ACLSNMPCommunityStrategy
const &);
1177 class ACLSNMPCommunity
1181 static ACL::Prototype RegistryProtoype
;
1182 static ACLStrategised
<char const *> RegistryEntry_
;
1185 ACL::Prototype
ACLSNMPCommunity::RegistryProtoype(&ACLSNMPCommunity::RegistryEntry_
, "snmp_community");
1186 ACLStrategised
<char const *> ACLSNMPCommunity::RegistryEntry_(new ACLStringData
, ACLSNMPCommunityStrategy::Instance(), "snmp_community");
1189 ACLSNMPCommunityStrategy::match (ACLData
<MatchType
> * &data
, ACLFilledChecklist
*checklist
)
1191 return data
->match (checklist
->snmp_community
);
1194 ACLSNMPCommunityStrategy
*
1195 ACLSNMPCommunityStrategy::Instance()
1200 ACLSNMPCommunityStrategy
ACLSNMPCommunityStrategy::Instance_
;