2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 49 SNMP support */
12 #include "acl/FilledChecklist.h"
13 #include "base/CbcPointer.h"
14 #include "CachePeer.h"
15 #include "client_db.h"
17 #include "comm/Connection.h"
18 #include "comm/Loops.h"
19 #include "comm/UdpOpenDialer.h"
20 #include "ip/Address.h"
22 #include "snmp/Forwarder.h"
23 #include "snmp_agent.h"
24 #include "snmp_core.h"
25 #include "SnmpRequest.h"
26 #include "SquidConfig.h"
29 static void snmpPortOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
31 mib_tree_entry
*mib_tree_head
;
32 mib_tree_entry
*mib_tree_last
;
34 Comm::ConnectionPointer snmpIncomingConn
;
35 Comm::ConnectionPointer snmpOutgoingConn
;
37 static mib_tree_entry
* snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
= atNone
);
38 static mib_tree_entry
*snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
, int children
,...);
39 static oid
*snmpCreateOid(int length
,...);
40 mib_tree_entry
* snmpLookupNodeStr(mib_tree_entry
*entry
, const char *str
);
41 bool snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
);
42 SQUIDCEXTERN
void (*snmplib_debug_hook
) (int, char *);
43 static oid
*static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
44 static oid
*time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
45 static oid
*peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
46 static oid
*client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
47 static void snmpDecodePacket(SnmpRequest
* rq
);
48 static void snmpConstructReponse(SnmpRequest
* rq
);
50 static oid_ParseFn
*snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
);
51 static oid_ParseFn
*snmpTreeGet(oid
* Current
, snint CurrentLen
);
52 static mib_tree_entry
*snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
);
53 static mib_tree_entry
*snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
);
54 extern "C" void snmpSnmplibDebug(int lvl
, char *buf
);
57 * The functions used during startup:
64 * Turns the MIB into a Tree structure. Called during the startup process.
69 debugs(49, 5, "snmpInit: Building SNMP mib tree structure");
71 snmplib_debug_hook
= snmpSnmplibDebug
;
74 * This following bit of evil is to get the final node in the "squid" mib
75 * without having a "search" function. A search function should be written
76 * to make this and the other code much less evil.
78 mib_tree_head
= snmpAddNode(snmpCreateOid(1, 1), 1, NULL
, NULL
, atNone
, 0);
80 assert(mib_tree_head
);
81 debugs(49, 5, "snmpInit: root is " << mib_tree_head
);
82 snmpAddNodeStr("1", 3, NULL
, NULL
);
84 snmpAddNodeStr("1.3", 6, NULL
, NULL
);
86 snmpAddNodeStr("1.3.6", 1, NULL
, NULL
);
87 snmpAddNodeStr("1.3.6.1", 4, NULL
, NULL
);
88 snmpAddNodeStr("1.3.6.1.4", 1, NULL
, NULL
);
89 snmpAddNodeStr("1.3.6.1.4.1", 3495, NULL
, NULL
);
90 mib_tree_entry
*m2
= snmpAddNodeStr("1.3.6.1.4.1.3495", 1, NULL
, NULL
);
92 mib_tree_entry
*n
= snmpLookupNodeStr(NULL
, "1.3.6.1.4.1.3495.1");
95 /* SQ_SYS - 1.3.6.1.4.1.3495.1.1 */
96 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 1, NULL
, NULL
);
97 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSVMSIZ
, snmp_sysFn
, static_Inst
, atSum
);
98 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSSTOR
, snmp_sysFn
, static_Inst
, atSum
);
99 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYS_UPTIME
, snmp_sysFn
, static_Inst
, atMax
);
101 /* SQ_CONF - 1.3.6.1.4.1.3495.1.2 */
102 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 2, NULL
, NULL
);
103 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_ADMIN
, snmp_confFn
, static_Inst
);
104 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION
, snmp_confFn
, static_Inst
);
105 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION_ID
, snmp_confFn
, static_Inst
);
106 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_LOG_FAC
, snmp_confFn
, static_Inst
);
108 /* SQ_CONF + CONF_STORAGE - 1.3.6.1.4.1.3495.1.5 */
109 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_STORAGE
, NULL
, NULL
);
110 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_MMAXSZ
, snmp_confFn
, static_Inst
, atSum
);
111 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWMAXSZ
, snmp_confFn
, static_Inst
, atSum
);
112 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWHIWM
, snmp_confFn
, static_Inst
, atMin
);
113 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWLOWM
, snmp_confFn
, static_Inst
, atMin
);
115 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_UNIQNAME
, snmp_confFn
, static_Inst
);
117 /* SQ_PRF - 1.3.6.1.4.1.3495.1.3 */
118 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 3, NULL
, NULL
); /* SQ_PRF */
120 /* PERF_SYS - 1.3.6.1.4.1.3495.1.3.1 */
121 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_SYS
, NULL
, NULL
);
122 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_PF
, snmp_prfSysFn
, static_Inst
, atSum
);
123 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMR
, snmp_prfSysFn
, static_Inst
, atSum
);
124 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MEMUSAGE
, snmp_prfSysFn
, static_Inst
, atSum
);
125 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUTIME
, snmp_prfSysFn
, static_Inst
, atSum
);
126 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUUSAGE
, snmp_prfSysFn
, static_Inst
, atSum
);
127 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MAXRESSZ
, snmp_prfSysFn
, static_Inst
, atSum
);
128 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMOBJCNT
, snmp_prfSysFn
, static_Inst
, atSum
);
131 The meaning of LRU is "oldest timestamped object in cache, if LRU algorithm is
133 What this SMP support needs to do is aggregate via a special filter equivalent to
134 min() to retain the semantic oldest-object meaning. A special one is needed that
135 works as unsigned and ignores '0' values.
137 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURLRUEXP
, snmp_prfSysFn
, static_Inst
);
138 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNLREQ
, snmp_prfSysFn
, static_Inst
, atSum
);
139 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNUSED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
140 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURRESERVED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
141 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUSED_FD
, snmp_prfSysFn
, static_Inst
, atSum
);
142 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURMAX_FD
, snmp_prfSysFn
, static_Inst
, atMax
);
144 /* PERF_PROTO - 1.3.6.1.4.1.3495.1.3.2 */
145 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_PROTO
, NULL
, NULL
);
146 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_AGGR
, NULL
, NULL
);
147 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_REQ
, snmp_prfProtoFn
, static_Inst
, atSum
);
148 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_HITS
, snmp_prfProtoFn
, static_Inst
, atSum
);
149 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_ERRORS
, snmp_prfProtoFn
, static_Inst
, atSum
);
150 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
, atSum
);
151 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
, atSum
);
152 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_S
, snmp_prfProtoFn
, static_Inst
, atSum
);
153 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_R
, snmp_prfProtoFn
, static_Inst
, atSum
);
154 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_SKB
, snmp_prfProtoFn
, static_Inst
, atSum
);
155 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_RKB
, snmp_prfProtoFn
, static_Inst
, atSum
);
156 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_REQ
, snmp_prfProtoFn
, static_Inst
, atSum
);
157 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ERRORS
, snmp_prfProtoFn
, static_Inst
, atSum
);
158 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
, atSum
);
159 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
, atSum
);
160 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CURSWAP
, snmp_prfProtoFn
, static_Inst
, atSum
);
161 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CLIENTS
, snmp_prfProtoFn
, static_Inst
, atSum
);
163 /* Note this is time-series rather than 'static' */
164 /* cacheMedianSvcTable */
165 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_MEDIAN
, NULL
, NULL
);
167 /* cacheMedianSvcEntry */
168 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2", 1, NULL
, NULL
);
169 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_TIME
, snmp_prfProtoFn
, time_Inst
, atAverage
);
170 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_ALL
, snmp_prfProtoFn
, time_Inst
, atAverage
);
171 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_MISS
, snmp_prfProtoFn
, time_Inst
, atAverage
);
172 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NM
, snmp_prfProtoFn
, time_Inst
, atAverage
);
173 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_HIT
, snmp_prfProtoFn
, time_Inst
, atAverage
);
174 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_QUERY
, snmp_prfProtoFn
, time_Inst
, atAverage
);
175 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_REPLY
, snmp_prfProtoFn
, time_Inst
, atAverage
);
176 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_DNS
, snmp_prfProtoFn
, time_Inst
, atAverage
);
177 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_RHR
, snmp_prfProtoFn
, time_Inst
, atAverage
);
178 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_BHR
, snmp_prfProtoFn
, time_Inst
, atAverage
);
179 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NH
, snmp_prfProtoFn
, time_Inst
, atAverage
);
181 /* SQ_NET - 1.3.6.1.4.1.3495.1.4 */
182 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 4, NULL
, NULL
);
184 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_IP_CACHE
, NULL
, NULL
);
185 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_ENT
, snmp_netIpFn
, static_Inst
, atSum
);
186 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_REQ
, snmp_netIpFn
, static_Inst
, atSum
);
187 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_HITS
, snmp_netIpFn
, static_Inst
, atSum
);
188 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_PENDHIT
, snmp_netIpFn
, static_Inst
, atSum
);
189 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_NEGHIT
, snmp_netIpFn
, static_Inst
, atSum
);
190 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_MISS
, snmp_netIpFn
, static_Inst
, atSum
);
191 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_GHBN
, snmp_netIpFn
, static_Inst
, atSum
);
192 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_LOC
, snmp_netIpFn
, static_Inst
, atSum
);
194 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_FQDN_CACHE
, NULL
, NULL
);
195 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_ENT
, snmp_netFqdnFn
, static_Inst
, atSum
);
196 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_REQ
, snmp_netFqdnFn
, static_Inst
, atSum
);
197 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_HITS
, snmp_netFqdnFn
, static_Inst
, atSum
);
198 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_PENDHIT
, snmp_netFqdnFn
, static_Inst
, atSum
);
199 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_NEGHIT
, snmp_netFqdnFn
, static_Inst
, atSum
);
200 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_MISS
, snmp_netFqdnFn
, static_Inst
, atSum
);
201 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_GHBN
, snmp_netFqdnFn
, static_Inst
, atSum
);
203 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_DNS_CACHE
, NULL
, NULL
);
204 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REQ
, snmp_netDnsFn
, static_Inst
, atSum
);
205 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REP
, snmp_netDnsFn
, static_Inst
, atSum
);
206 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_SERVERS
, snmp_netDnsFn
, static_Inst
, atSum
);
208 /* SQ_MESH - 1.3.6.1.4.1.3495.1.5 */
209 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 5, NULL
, NULL
);
211 /* cachePeerTable - 1.3.6.1.4.1.3495.1.5.1 */
212 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_PTBL
, NULL
, NULL
);
214 /* CachePeerTableEntry (version 3) - 1.3.6.1.4.1.3495.1.5.1.3 */
215 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1", 3, NULL
, NULL
);
216 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_INDEX
, snmp_meshPtblFn
, peer_Inst
);
217 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_NAME
, snmp_meshPtblFn
, peer_Inst
);
218 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR_TYPE
, snmp_meshPtblFn
, peer_Inst
);
219 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR
, snmp_meshPtblFn
, peer_Inst
);
220 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_HTTP
, snmp_meshPtblFn
, peer_Inst
);
221 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ICP
, snmp_meshPtblFn
, peer_Inst
);
222 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_TYPE
, snmp_meshPtblFn
, peer_Inst
);
223 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_STATE
, snmp_meshPtblFn
, peer_Inst
);
224 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_SENT
, snmp_meshPtblFn
, peer_Inst
);
225 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_PACKED
, snmp_meshPtblFn
, peer_Inst
);
226 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_FETCHES
, snmp_meshPtblFn
, peer_Inst
);
227 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_RTT
, snmp_meshPtblFn
, peer_Inst
);
228 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_IGN
, snmp_meshPtblFn
, peer_Inst
);
229 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_S
, snmp_meshPtblFn
, peer_Inst
);
230 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_R
, snmp_meshPtblFn
, peer_Inst
);
232 /* cacheClientTable - 1.3.6.1.4.1.3495.1.5.2 */
233 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_CTBL
, NULL
, NULL
);
235 /* BUG 2811: we NEED to create a reliable index for the client DB and make version 3 of the table. */
236 /* for now we have version 2 table with OID capable of mixed IPv4 / IPv6 clients and upgraded address text format. */
238 /* cacheClientEntry - 1.3.6.1.4.1.3495.1.5.2.2 */
239 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2", 2, NULL
, NULL
);
240 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR_TYPE
, snmp_meshCtblFn
, client_Inst
);
241 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR
, snmp_meshCtblFn
, client_Inst
);
242 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTREQ
, snmp_meshCtblFn
, client_Inst
);
243 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTBYTES
, snmp_meshCtblFn
, client_Inst
);
244 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITS
, snmp_meshCtblFn
, client_Inst
);
245 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITBYTES
, snmp_meshCtblFn
, client_Inst
);
246 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPREQ
, snmp_meshCtblFn
, client_Inst
);
247 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPBYTES
, snmp_meshCtblFn
, client_Inst
);
248 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITS
, snmp_meshCtblFn
, client_Inst
);
249 mib_tree_last
= snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITBYTES
, snmp_meshCtblFn
, client_Inst
);
251 debugs(49, 9, "snmpInit: Completed SNMP mib tree structure");
257 debugs(49, 5, "snmpConnectionOpen: Called");
259 if (Config
.Port
.snmp
<= 0)
262 snmpIncomingConn
= new Comm::Connection
;
263 snmpIncomingConn
->local
= Config
.Addrs
.snmp_incoming
;
264 snmpIncomingConn
->local
.port(Config
.Port
.snmp
);
266 if (!Ip::EnableIpv6
&& !snmpIncomingConn
->local
.setIPv4()) {
267 debugs(49, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << snmpIncomingConn
->local
<< " is not an IPv4 address.");
268 fatal("SNMP port cannot be opened.");
270 /* split-stack for now requires IPv4-only SNMP */
271 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& snmpIncomingConn
->local
.isAnyAddr()) {
272 snmpIncomingConn
->local
.setIPv4();
275 AsyncCall::Pointer call
= asyncCall(49, 2, "snmpIncomingConnectionOpened",
276 Comm::UdpOpenDialer(&snmpPortOpened
));
277 Ipc::StartListening(SOCK_DGRAM
, IPPROTO_UDP
, snmpIncomingConn
, Ipc::fdnInSnmpSocket
, call
);
279 if (!Config
.Addrs
.snmp_outgoing
.isNoAddr()) {
280 snmpOutgoingConn
= new Comm::Connection
;
281 snmpOutgoingConn
->local
= Config
.Addrs
.snmp_outgoing
;
282 snmpOutgoingConn
->local
.port(Config
.Port
.snmp
);
284 if (!Ip::EnableIpv6
&& !snmpOutgoingConn
->local
.setIPv4()) {
285 debugs(49, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << snmpOutgoingConn
->local
<< " is not an IPv4 address.");
286 fatal("SNMP port cannot be opened.");
288 /* split-stack for now requires IPv4-only SNMP */
289 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& snmpOutgoingConn
->local
.isAnyAddr()) {
290 snmpOutgoingConn
->local
.setIPv4();
292 AsyncCall::Pointer c
= asyncCall(49, 2, "snmpOutgoingConnectionOpened",
293 Comm::UdpOpenDialer(&snmpPortOpened
));
294 Ipc::StartListening(SOCK_DGRAM
, IPPROTO_UDP
, snmpOutgoingConn
, Ipc::fdnOutSnmpSocket
, c
);
296 snmpOutgoingConn
= snmpIncomingConn
;
297 debugs(1, DBG_IMPORTANT
, "Sending SNMP messages from " << snmpOutgoingConn
->local
);
302 snmpPortOpened(const Comm::ConnectionPointer
&conn
, int errNo
)
304 if (!Comm::IsConnOpen(conn
))
305 fatalf("Cannot open SNMP %s Port",(conn
->fd
== snmpIncomingConn
->fd
?"receiving":"sending"));
307 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
309 if (conn
->fd
== snmpIncomingConn
->fd
)
310 debugs(1, DBG_IMPORTANT
, "Accepting SNMP messages on " << snmpIncomingConn
->local
);
311 else if (conn
->fd
== snmpOutgoingConn
->fd
)
312 debugs(1, DBG_IMPORTANT
, "Sending SNMP messages from " << snmpOutgoingConn
->local
);
314 fatalf("Lost SNMP port (%d) on FD %d", (int)conn
->local
.port(), conn
->fd
);
320 if (Comm::IsConnOpen(snmpIncomingConn
)) {
321 debugs(49, DBG_IMPORTANT
, "Closing SNMP receiving port " << snmpIncomingConn
->local
);
322 snmpIncomingConn
->close();
324 snmpIncomingConn
= NULL
;
326 if (Comm::IsConnOpen(snmpOutgoingConn
) && snmpIncomingConn
!= snmpOutgoingConn
) {
327 // Perform OUT port closure so as not to step on IN port when sharing a conn.
328 debugs(49, DBG_IMPORTANT
, "Closing SNMP sending port " << snmpOutgoingConn
->local
);
329 snmpOutgoingConn
->close();
331 snmpOutgoingConn
= NULL
;
335 * Functions for handling the requests.
339 * Accept the UDP packet
342 snmpHandleUdp(int sock
, void *not_used
)
344 static char buf
[SNMP_REQUEST_SIZE
];
346 SnmpRequest
*snmp_rq
;
349 debugs(49, 5, "snmpHandleUdp: Called.");
351 Comm::SetSelect(sock
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
353 memset(buf
, '\0', sizeof(buf
));
355 len
= comm_udp_recvfrom(sock
, sizeof(buf
)-1, 0, from
);
358 debugs(49, 3, "snmpHandleUdp: FD " << sock
<< ": received " << len
<< " bytes from " << from
<< ".");
360 snmp_rq
= (SnmpRequest
*)xcalloc(1, sizeof(SnmpRequest
));
361 snmp_rq
->buf
= (u_char
*) buf
;
363 snmp_rq
->sock
= sock
;
364 snmp_rq
->outbuf
= (unsigned char *)xmalloc(snmp_rq
->outlen
= SNMP_REQUEST_SIZE
);
365 snmp_rq
->from
= from
;
366 snmpDecodePacket(snmp_rq
);
367 xfree(snmp_rq
->outbuf
);
370 debugs(49, DBG_IMPORTANT
, "snmpHandleUdp: FD " << sock
<< " recvfrom: " << xstrerror());
375 * Turn SNMP packet into a PDU, check available ACL's
378 snmpDecodePacket(SnmpRequest
* rq
)
380 struct snmp_pdu
*PDU
;
382 u_char
*buf
= rq
->buf
;
384 allow_t allow
= ACCESS_DENIED
;
386 if (!Config
.accessList
.snmp
) {
387 debugs(49, DBG_IMPORTANT
, "WARNING: snmp_access not configured. agent query DENIED from : " << rq
->from
);
391 debugs(49, 5, HERE
<< "Called.");
392 PDU
= snmp_pdu_create(0);
393 /* Allways answer on SNMPv1 */
394 rq
->session
.Version
= SNMP_VERSION_1
;
395 Community
= snmp_parse(&rq
->session
, PDU
, buf
, len
);
397 /* Check if we have explicit permission to access SNMP data.
398 * default (set above) is to deny all */
400 ACLFilledChecklist
checklist(Config
.accessList
.snmp
, NULL
, NULL
);
401 checklist
.src_addr
= rq
->from
;
402 checklist
.snmp_community
= (char *) Community
;
403 allow
= checklist
.fastCheck();
405 if (allow
== ACCESS_ALLOWED
&& (snmp_coexist_V2toV1(PDU
))) {
406 rq
->community
= Community
;
408 debugs(49, 5, "snmpAgentParse: reqid=[" << PDU
->reqid
<< "]");
409 snmpConstructReponse(rq
);
411 debugs(49, DBG_IMPORTANT
, "WARNING: SNMP agent query DENIED from : " << rq
->from
);
416 debugs(49, DBG_IMPORTANT
, "WARNING: Failed SNMP agent query from : " << rq
->from
);
422 * Packet OK, ACL Check OK, Create reponse.
425 snmpConstructReponse(SnmpRequest
* rq
)
428 struct snmp_pdu
*RespPDU
;
430 debugs(49, 5, "snmpConstructReponse: Called.");
432 if (UsingSmp() && IamWorkerProcess()) {
433 AsyncJob::Start(new Snmp::Forwarder(static_cast<Snmp::Pdu
&>(*rq
->PDU
),
434 static_cast<Snmp::Session
&>(rq
->session
), rq
->sock
, rq
->from
));
435 snmp_free_pdu(rq
->PDU
);
439 RespPDU
= snmpAgentResponse(rq
->PDU
);
440 snmp_free_pdu(rq
->PDU
);
442 if (RespPDU
!= NULL
) {
443 snmp_build(&rq
->session
, RespPDU
, rq
->outbuf
, &rq
->outlen
);
444 comm_udp_sendto(rq
->sock
, rq
->from
, rq
->outbuf
, rq
->outlen
);
445 snmp_free_pdu(RespPDU
);
450 * Decide how to respond to the request, construct a response and
451 * return the response to the requester.
455 snmpAgentResponse(struct snmp_pdu
*PDU
) {
457 struct snmp_pdu
*Answer
= NULL
;
459 debugs(49, 5, "snmpAgentResponse: Called.");
461 if ((Answer
= snmp_pdu_create(SNMP_PDU_RESPONSE
))) {
462 Answer
->reqid
= PDU
->reqid
;
463 Answer
->errindex
= 0;
465 if (PDU
->command
== SNMP_PDU_GET
|| PDU
->command
== SNMP_PDU_GETNEXT
) {
467 int get_next
= (PDU
->command
== SNMP_PDU_GETNEXT
);
468 variable_list
*VarPtr_
;
469 variable_list
**RespVars
= &(Answer
->variables
);
470 oid_ParseFn
*ParseFn
;
472 /* Loop through all variables */
474 for (VarPtr_
= PDU
->variables
; VarPtr_
; VarPtr_
= VarPtr_
->next_variable
) {
475 variable_list
*VarPtr
= VarPtr_
;
476 variable_list
*VarNew
= NULL
;
477 oid
*NextOidName
= NULL
;
478 snint NextOidNameLen
= 0;
483 ParseFn
= snmpTreeNext(VarPtr
->name
, VarPtr
->name_length
, &NextOidName
, &NextOidNameLen
);
485 ParseFn
= snmpTreeGet(VarPtr
->name
, VarPtr
->name_length
);
487 if (ParseFn
== NULL
) {
488 Answer
->errstat
= SNMP_ERR_NOSUCHNAME
;
489 debugs(49, 5, "snmpAgentResponse: No such oid. ");
492 VarPtr
= snmp_var_new(NextOidName
, NextOidNameLen
);
496 int * errstatTmp
= &(Answer
->errstat
);
498 VarNew
= (*ParseFn
) (VarPtr
, (snint
*) errstatTmp
);
501 snmp_var_free(VarPtr
);
504 if ((Answer
->errstat
!= SNMP_ERR_NOERROR
) || (VarNew
== NULL
)) {
505 Answer
->errindex
= index
;
506 debugs(49, 5, "snmpAgentResponse: error.");
509 snmp_var_free(VarNew
);
511 while ((VarPtr
= Answer
->variables
) != NULL
) {
512 Answer
->variables
= VarPtr
->next_variable
;
513 snmp_var_free(VarPtr
);
516 /* Steal the original PDU list of variables for the error response */
517 Answer
->variables
= PDU
->variables
;
519 PDU
->variables
= NULL
;
524 /* No error. Insert this var at the end, and move on to the next.
528 RespVars
= &(VarNew
->next_variable
);
537 snmpTreeGet(oid
* Current
, snint CurrentLen
)
539 oid_ParseFn
*Fn
= NULL
;
540 mib_tree_entry
*mibTreeEntry
= NULL
;
543 debugs(49, 5, "snmpTreeGet: Called");
546 debugs(49, 6, "snmpTreeGet: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
) );
548 mibTreeEntry
= mib_tree_head
;
550 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
553 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
554 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
559 if (mibTreeEntry
&& mibTreeEntry
->parsefunction
)
560 Fn
= mibTreeEntry
->parsefunction
;
562 debugs(49, 5, "snmpTreeGet: return");
568 snmpAggrType(oid
* Current
, snint CurrentLen
)
572 mib_tree_entry
* mibTreeEntry
= mib_tree_head
;
573 AggrType type
= atNone
;
576 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
579 while (mibTreeEntry
!= NULL
&& count
< CurrentLen
) {
580 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
581 if (mibTreeEntry
!= NULL
)
582 type
= mibTreeEntry
->aggrType
;
591 snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
)
593 oid_ParseFn
*Fn
= NULL
;
596 debugs(49, 5, "snmpTreeNext: Called");
599 debugs(49, 6, "snmpTreeNext: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
));
601 mib_tree_entry
*mibTreeEntry
= mib_tree_head
;
603 if (mibTreeEntry
&& Current
[count
] == mibTreeEntry
->name
[count
]) {
606 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
607 mib_tree_entry
*nextmibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
609 if (!nextmibTreeEntry
)
612 mibTreeEntry
= nextmibTreeEntry
;
616 debugs(49, 5, "snmpTreeNext: Recursed down to requested object");
621 if (mibTreeEntry
== mib_tree_last
)
624 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
625 *NextLen
= CurrentLen
;
626 *Next
= (*mibTreeEntry
->instancefunction
) (Current
, NextLen
, mibTreeEntry
, &Fn
);
628 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
633 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
635 mib_tree_entry
*nextoid
= snmpTreeSiblingEntry(Current
[count
], count
, mibTreeEntry
->parent
);
637 debugs(49, 5, "snmpTreeNext: Next OID found for sibling" << nextoid
);
638 mibTreeEntry
= nextoid
;
641 debugs(49, 5, "snmpTreeNext: Attempting to recurse up for next object");
646 if (mibTreeEntry
->parent
->parent
) {
647 nextoid
= mibTreeEntry
->parent
;
648 mibTreeEntry
= snmpTreeEntry(Current
[count
] + 1, count
, nextoid
->parent
);
651 mibTreeEntry
= nextoid
;
655 nextoid
= mibTreeEntry
;
661 while ((mibTreeEntry
) && (!mibTreeEntry
->parsefunction
)) {
662 mibTreeEntry
= mibTreeEntry
->leaves
[0];
666 *NextLen
= mibTreeEntry
->len
;
667 *Next
= (*mibTreeEntry
->instancefunction
) (mibTreeEntry
->name
, NextLen
, mibTreeEntry
, &Fn
);
671 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
678 static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
680 oid
*instance
= NULL
;
681 if (*len
<= current
->len
) {
682 instance
= (oid
*)xmalloc(sizeof(*name
) * (*len
+ 1));
683 memcpy(instance
, name
, sizeof(*name
) * (*len
));
687 *Fn
= current
->parsefunction
;
692 time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
694 oid
*instance
= NULL
;
695 int identifier
= 0, loop
= 0;
696 int index
[TIME_INDEX_LEN
] = {TIME_INDEX
};
698 if (*len
<= current
->len
) {
699 instance
= (oid
*)xmalloc(sizeof(*name
) * (*len
+ 1));
700 memcpy(instance
, name
, sizeof(*name
) * (*len
));
701 instance
[*len
] = *index
;
704 identifier
= name
[*len
- 1];
706 while ((loop
< TIME_INDEX_LEN
) && (identifier
!= index
[loop
]))
709 if (loop
< (TIME_INDEX_LEN
- 1)) {
710 instance
= (oid
*)xmalloc(sizeof(*name
) * (*len
));
711 memcpy(instance
, name
, sizeof(*name
) * (*len
));
712 instance
[*len
- 1] = index
[++loop
];
716 *Fn
= current
->parsefunction
;
721 peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
723 oid
*instance
= NULL
;
724 CachePeer
*peers
= Config
.peers
;
727 debugs(49, 6, "snmp peer_Inst: No Peers.");
728 current
= current
->parent
->parent
->parent
->leaves
[1];
729 while ((current
) && (!current
->parsefunction
))
730 current
= current
->leaves
[0];
735 instance
= client_Inst(current
->name
, len
, current
, Fn
);
736 } else if (*len
<= current
->len
) {
737 debugs(49, 6, "snmp peer_Inst: *len <= current->len ???");
738 instance
= (oid
*)xmalloc(sizeof(*name
) * ( *len
+ 1));
739 memcpy(instance
, name
, sizeof(*name
) * (*len
));
743 int no
= name
[current
->len
] ;
745 // Note: This works because the Config.peers keeps its index according to its position.
746 for ( i
=0 ; peers
&& (i
< no
) ; peers
= peers
->next
, ++i
) ;
749 debugs(49, 6, "snmp peer_Inst: Encode peer #" << i
);
750 instance
= (oid
*)xmalloc(sizeof(*name
) * (current
->len
+ 1 ));
751 memcpy(instance
, name
, (sizeof(*name
) * current
->len
));
752 instance
[current
->len
] = no
+ 1 ; // i.e. the next index on cache_peeer table.
754 debugs(49, 6, "snmp peer_Inst: We have " << i
<< " peers. Can't find #" << no
);
758 *Fn
= current
->parsefunction
;
763 client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
765 oid
*instance
= NULL
;
771 if (*len
<= current
->len
) {
772 aux
= client_entry(NULL
);
779 size
= sizeof(in_addr
);
781 size
= sizeof(in6_addr
);
783 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", size=" << size
);
785 instance
= (oid
*)xmalloc(sizeof(*name
) * (*len
+ size
));
786 memcpy(instance
, name
, (sizeof(*name
) * (*len
)));
788 if ( !laddr
.isAnyAddr() ) {
789 addr2oid(laddr
, &instance
[ *len
]); // the addr
793 int shift
= *len
- current
->len
; // i.e 4 or 16
794 oid2addr(&name
[*len
- shift
], laddr
,shift
);
795 aux
= client_entry(&laddr
);
801 if (!laddr
.isAnyAddr()) {
803 newshift
= sizeof(in_addr
);
805 newshift
= sizeof(in6_addr
);
807 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", newshift=" << newshift
);
809 instance
= (oid
*)xmalloc(sizeof(*name
) * (current
->len
+ newshift
));
810 memcpy(instance
, name
, (sizeof(*name
) * (current
->len
)));
811 addr2oid(laddr
, &instance
[current
->len
]); // the addr.
812 *len
= current
->len
+ newshift
;
816 *Fn
= current
->parsefunction
;
825 * Tree utility functions.
829 * Returns a sibling object for the requested child object or NULL
830 * if it does not exit
832 static mib_tree_entry
*
833 snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
)
835 mib_tree_entry
*next
= NULL
;
838 while ((!next
) && (count
< current
->children
)) {
839 if (current
->leaves
[count
]->name
[len
] == entry
) {
840 next
= current
->leaves
[count
];
846 /* Exactly the sibling on right */
847 if (count
< current
->children
) {
848 next
= current
->leaves
[count
];
857 * Returns the requested child object or NULL if it does not exist
859 static mib_tree_entry
*
860 snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
)
862 mib_tree_entry
*next
= NULL
;
865 while ((!next
) && current
&& (count
< current
->children
)) {
866 if (current
->leaves
[count
]->name
[len
] == entry
) {
867 next
= current
->leaves
[count
];
877 snmpAddNodeChild(mib_tree_entry
*entry
, mib_tree_entry
*child
)
879 debugs(49, 5, "snmpAddNodeChild: assigning " << child
<< " to parent " << entry
);
880 entry
->leaves
= (mib_tree_entry
**)xrealloc(entry
->leaves
, sizeof(mib_tree_entry
*) * (entry
->children
+ 1));
881 entry
->leaves
[entry
->children
] = child
;
882 entry
->leaves
[entry
->children
]->parent
= entry
;
887 snmpLookupNodeStr(mib_tree_entry
*root
, const char *str
)
898 if (! snmpCreateOidFromStr(str
, &name
, &namelen
))
901 /* I wish there were some kind of sensible existing tree traversal
902 * routine to use. I'll worry about that later */
905 return e
; /* XXX it should only be this? */
909 while (r
< namelen
) {
911 /* Find the child node which matches this */
912 for (i
= 0; i
< e
->children
&& e
->leaves
[i
]->name
[r
] != name
[r
]; ++i
) ; // seek-loop
914 /* Are we pointing to that node? */
915 if (i
>= e
->children
)
917 assert(e
->leaves
[i
]->name
[r
] == name
[r
]);
919 /* Skip to that node! */
929 snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
)
931 char const *delim
= ".";
937 /* Parse the OID string into oid bits */
938 while (size_t len
= strcspn(s
, delim
)) {
939 *name
= (oid
*)xrealloc(*name
, sizeof(oid
) * ((*nl
) + 1));
940 (*name
)[*nl
] = atoi(s
); // stops at the '.' delimiter
942 // exit with true when the last octet has been parsed
948 // if we aborted before the lst octet was found, return false.
954 * Create an entry. Return a pointer to the newly created node, or NULL
957 static mib_tree_entry
*
958 snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
)
960 mib_tree_entry
*m
, *b
;
966 b
= snmpLookupNodeStr(mib_tree_head
, base_str
);
969 debugs(49, 5, "snmpAddNodeStr: " << base_str
<< ": -> " << b
);
971 /* Create OID string for new entry */
972 snprintf(s
, 1024, "%s.%d", base_str
, o
);
973 if (! snmpCreateOidFromStr(s
, &n
, &nl
))
977 m
= snmpAddNode(n
, nl
, parsefunction
, instancefunction
, aggrType
, 0);
979 /* Link it into the existing tree */
980 snmpAddNodeChild(b
, m
);
982 /* Return the node */
987 * Adds a node to the MIB tree structure and adds the appropriate children
989 static mib_tree_entry
*
990 snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, AggrType aggrType
, int children
,...)
994 mib_tree_entry
*entry
= NULL
;
995 va_start(args
, children
);
998 debugs(49, 6, "snmpAddNode: Children : " << children
<< ", Oid : " << snmpDebugOid(name
, len
, tmp
));
1000 va_start(args
, children
);
1001 entry
= (mib_tree_entry
*)xmalloc(sizeof(mib_tree_entry
));
1004 entry
->parsefunction
= parsefunction
;
1005 entry
->instancefunction
= instancefunction
;
1006 entry
->children
= children
;
1007 entry
->leaves
= NULL
;
1008 entry
->aggrType
= aggrType
;
1011 entry
->leaves
= (mib_tree_entry
**)xmalloc(sizeof(mib_tree_entry
*) * children
);
1013 for (loop
= 0; loop
< children
; ++loop
) {
1014 entry
->leaves
[loop
] = va_arg(args
, mib_tree_entry
*);
1015 entry
->leaves
[loop
]->parent
= entry
;
1022 /* End of tree utility functions */
1025 * Returns the list of parameters in an oid
1028 snmpCreateOid(int length
,...)
1033 va_start(args
, length
);
1035 new_oid
= (oid
*)xmalloc(sizeof(oid
) * length
);
1038 for (loop
= 0; loop
< length
; ++loop
) {
1039 new_oid
[loop
] = va_arg(args
, int);
1048 * Debug calls, prints out the OID for debugging purposes.
1051 snmpDebugOid(oid
* Name
, snint Len
, MemBuf
&outbuf
)
1055 if (outbuf
.isNull())
1056 outbuf
.init(16, MAX_IPSTRLEN
);
1058 for (x
= 0; x
< Len
; ++x
) {
1059 size_t bytes
= snprintf(mbuf
, sizeof(mbuf
), ".%u", (unsigned int) Name
[x
]);
1060 outbuf
.append(mbuf
, bytes
);
1062 return outbuf
.content();
1066 snmpSnmplibDebug(int lvl
, char *buf
)
1068 debugs(49, lvl
, buf
);
1072 IPv4 address: 10.10.0.9 ==>
1074 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01 ==>
1075 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1
1078 addr2oid(Ip::Address
&addr
, oid
* Dest
)
1082 struct in_addr i4addr
;
1083 struct in6_addr i6addr
;
1084 oid code
= addr
.isIPv6()? INETADDRESSTYPE_IPV6
: INETADDRESSTYPE_IPV4
;
1085 u_int size
= (code
== INETADDRESSTYPE_IPV4
) ? sizeof(struct in_addr
):sizeof(struct in6_addr
);
1087 if ( code
== INETADDRESSTYPE_IPV4
) {
1088 addr
.getInAddr(i4addr
);
1089 cp
= (u_char
*) &(i4addr
.s_addr
);
1091 addr
.getInAddr(i6addr
);
1092 cp
= (u_char
*) &i6addr
;
1094 for ( i
=0 ; i
< size
; ++i
) {
1095 // OID's are in network order
1100 debugs(49, 7, "addr2oid: Dest : " << snmpDebugOid(Dest
, size
, tmp
));
1104 oid == 10.10.0.9 ==>
1105 IPv4 address: 10.10.0.9
1106 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1 ==>
1107 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01
1110 oid2addr(oid
* id
, Ip::Address
&addr
, u_int size
)
1112 struct in_addr i4addr
;
1113 struct in6_addr i6addr
;
1116 if ( size
== sizeof(struct in_addr
) )
1117 cp
= (u_char
*) &(i4addr
.s_addr
);
1119 cp
= (u_char
*) &(i6addr
);
1121 debugs(49, 7, "oid2addr: id : " << snmpDebugOid(id
, size
, tmp
) );
1122 for (i
=0 ; i
<size
; ++i
) {
1125 if ( size
== sizeof(struct in_addr
) )
1131 /* SNMP checklists */
1132 #include "acl/Strategised.h"
1133 #include "acl/Strategy.h"
1134 #include "acl/StringData.h"
1136 class ACLSNMPCommunityStrategy
: public ACLStrategy
<char const *>
1140 virtual int match (ACLData
<MatchType
> * &, ACLFilledChecklist
*, ACLFlags
&);
1141 static ACLSNMPCommunityStrategy
*Instance();
1142 /* Not implemented to prevent copies of the instance. */
1143 /* Not private to prevent brain dead g++ warnings about
1144 * private constructors with no friends */
1145 ACLSNMPCommunityStrategy(ACLSNMPCommunityStrategy
const &);
1148 static ACLSNMPCommunityStrategy Instance_
;
1149 ACLSNMPCommunityStrategy() {}
1151 ACLSNMPCommunityStrategy
&operator=(ACLSNMPCommunityStrategy
const &);
1154 class ACLSNMPCommunity
1158 static ACL::Prototype RegistryProtoype
;
1159 static ACLStrategised
<char const *> RegistryEntry_
;
1162 ACL::Prototype
ACLSNMPCommunity::RegistryProtoype(&ACLSNMPCommunity::RegistryEntry_
, "snmp_community");
1163 ACLStrategised
<char const *> ACLSNMPCommunity::RegistryEntry_(new ACLStringData
, ACLSNMPCommunityStrategy::Instance(), "snmp_community");
1166 ACLSNMPCommunityStrategy::match (ACLData
<MatchType
> * &data
, ACLFilledChecklist
*checklist
, ACLFlags
&)
1168 return data
->match (checklist
->snmp_community
);
1171 ACLSNMPCommunityStrategy
*
1172 ACLSNMPCommunityStrategy::Instance()
1177 ACLSNMPCommunityStrategy
ACLSNMPCommunityStrategy::Instance_
;