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 "cache_snmp.h"
36 #include "ipc/StartListening.h"
37 #include "compat/strsep.h"
38 #include "ip/Address.h"
40 #define SNMP_REQUEST_SIZE 4096
41 #define MAX_PROTOSTAT 5
43 /// dials snmpConnectionOpened call
44 class SnmpListeningStartedDialer
: public CallDialer
,
45 public Ipc::StartListeningCb
48 typedef void (*Handler
)(int fd
, int errNo
);
49 SnmpListeningStartedDialer(Handler aHandler
): handler(aHandler
) {}
51 virtual void print(std::ostream
&os
) const { startPrint(os
) << ')'; }
53 virtual bool canDial(AsyncCall
&) const { return true; }
54 virtual void dial(AsyncCall
&) { (handler
)(fd
, errNo
); }
61 Ip::Address theOutSNMPAddr
;
63 typedef struct _mib_tree_entry mib_tree_entry
;
64 typedef oid
*(instance_Fn
) (oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
66 struct _mib_tree_entry
{
69 oid_ParseFn
*parsefunction
;
70 instance_Fn
*instancefunction
;
73 struct _mib_tree_entry
**leaves
;
75 struct _mib_tree_entry
*parent
;
78 mib_tree_entry
*mib_tree_head
;
79 mib_tree_entry
*mib_tree_last
;
81 static void snmpIncomingConnectionOpened(int fd
, int errNo
);
82 static void snmpOutgoingConnectionOpened(int fd
, int errNo
);
84 static mib_tree_entry
* snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
);
85 static mib_tree_entry
*snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, int children
,...);
86 static oid
*snmpCreateOid(int length
,...);
87 mib_tree_entry
* snmpLookupNodeStr(mib_tree_entry
*entry
, const char *str
);
88 int snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
);
89 SQUIDCEXTERN
void (*snmplib_debug_hook
) (int, char *);
90 static oid
*static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
91 static oid
*time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
92 static oid
*peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
93 static oid
*client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
);
94 static void snmpDecodePacket(snmp_request_t
* rq
);
95 static void snmpConstructReponse(snmp_request_t
* rq
);
97 static struct snmp_pdu
*snmpAgentResponse(struct snmp_pdu
*PDU
);
98 static oid_ParseFn
*snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
);
99 static oid_ParseFn
*snmpTreeGet(oid
* Current
, snint CurrentLen
);
100 static mib_tree_entry
*snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
);
101 static mib_tree_entry
*snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
);
102 static void snmpSnmplibDebug(int lvl
, char *buf
);
105 * The functions used during startup:
108 * snmpConnectionShutdown
109 * snmpConnectionClose
113 * Turns the MIB into a Tree structure. Called during the startup process.
118 debugs(49, 5, "snmpInit: Building SNMP mib tree structure");
120 snmplib_debug_hook
= snmpSnmplibDebug
;
123 * This following bit of evil is to get the final node in the "squid" mib
124 * without having a "search" function. A search function should be written
125 * to make this and the other code much less evil.
127 mib_tree_head
= snmpAddNode(snmpCreateOid(1, 1), 1, NULL
, NULL
, 0);
129 assert(mib_tree_head
);
130 debugs(49, 5, "snmpInit: root is " << mib_tree_head
);
131 snmpAddNodeStr("1", 3, NULL
, NULL
);
133 snmpAddNodeStr("1.3", 6, NULL
, NULL
);
135 snmpAddNodeStr("1.3.6", 1, NULL
, NULL
);
136 snmpAddNodeStr("1.3.6.1", 4, NULL
, NULL
);
137 snmpAddNodeStr("1.3.6.1.4", 1, NULL
, NULL
);
138 snmpAddNodeStr("1.3.6.1.4.1", 3495, NULL
, NULL
);
139 mib_tree_entry
*m2
= snmpAddNodeStr("1.3.6.1.4.1.3495", 1, NULL
, NULL
);
141 mib_tree_entry
*n
= snmpLookupNodeStr(NULL
, "1.3.6.1.4.1.3495.1");
144 /* SQ_SYS - 1.3.6.1.4.1.3495.1.1 */
145 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 1, NULL
, NULL
);
146 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSVMSIZ
, snmp_sysFn
, static_Inst
);
147 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYSSTOR
, snmp_sysFn
, static_Inst
);
148 snmpAddNodeStr("1.3.6.1.4.1.3495.1.1", SYS_UPTIME
, snmp_sysFn
, static_Inst
);
150 /* SQ_CONF - 1.3.6.1.4.1.3495.1.2 */
151 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 2, NULL
, NULL
);
152 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_ADMIN
, snmp_confFn
, static_Inst
);
153 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION
, snmp_confFn
, static_Inst
);
154 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_VERSION_ID
, snmp_confFn
, static_Inst
);
155 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_LOG_FAC
, snmp_confFn
, static_Inst
);
157 /* SQ_CONF + CONF_STORAGE - 1.3.6.1.4.1.3495.1.5 */
158 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_STORAGE
, NULL
, NULL
);
159 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_MMAXSZ
, snmp_confFn
, static_Inst
);
160 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWMAXSZ
, snmp_confFn
, static_Inst
);
161 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWHIWM
, snmp_confFn
, static_Inst
);
162 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2.5", CONF_ST_SWLOWM
, snmp_confFn
, static_Inst
);
164 snmpAddNodeStr("1.3.6.1.4.1.3495.1.2", CONF_UNIQNAME
, snmp_confFn
, static_Inst
);
166 /* SQ_PRF - 1.3.6.1.4.1.3495.1.3 */
167 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 3, NULL
, NULL
); /* SQ_PRF */
169 /* PERF_SYS - 1.3.6.1.4.1.3495.1.3.1 */
170 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_SYS
, NULL
, NULL
);
171 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_PF
, snmp_prfSysFn
, static_Inst
);
172 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMR
, snmp_prfSysFn
, static_Inst
);
173 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MEMUSAGE
, snmp_prfSysFn
, static_Inst
);
174 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUTIME
, snmp_prfSysFn
, static_Inst
);
175 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CPUUSAGE
, snmp_prfSysFn
, static_Inst
);
176 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_MAXRESSZ
, snmp_prfSysFn
, static_Inst
);
177 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_NUMOBJCNT
, snmp_prfSysFn
, static_Inst
);
178 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURLRUEXP
, snmp_prfSysFn
, static_Inst
);
179 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNLREQ
, snmp_prfSysFn
, static_Inst
);
180 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUNUSED_FD
, snmp_prfSysFn
, static_Inst
);
181 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURRESERVED_FD
, snmp_prfSysFn
, static_Inst
);
182 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURUSED_FD
, snmp_prfSysFn
, static_Inst
);
183 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.1", PERF_SYS_CURMAX_FD
, snmp_prfSysFn
, static_Inst
);
185 /* PERF_PROTO - 1.3.6.1.4.1.3495.1.3.2 */
186 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3", PERF_PROTO
, NULL
, NULL
);
187 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_AGGR
, NULL
, NULL
);
188 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_REQ
, snmp_prfProtoFn
, static_Inst
);
189 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_HITS
, snmp_prfProtoFn
, static_Inst
);
190 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_ERRORS
, snmp_prfProtoFn
, static_Inst
);
191 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
);
192 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_HTTP_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
);
193 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_S
, snmp_prfProtoFn
, static_Inst
);
194 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_R
, snmp_prfProtoFn
, static_Inst
);
195 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_SKB
, snmp_prfProtoFn
, static_Inst
);
196 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ICP_RKB
, snmp_prfProtoFn
, static_Inst
);
197 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_REQ
, snmp_prfProtoFn
, static_Inst
);
198 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_ERRORS
, snmp_prfProtoFn
, static_Inst
);
199 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_IN
, snmp_prfProtoFn
, static_Inst
);
200 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_KBYTES_OUT
, snmp_prfProtoFn
, static_Inst
);
201 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CURSWAP
, snmp_prfProtoFn
, static_Inst
);
202 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.1", PERF_PROTOSTAT_AGGR_CLIENTS
, snmp_prfProtoFn
, static_Inst
);
204 /* Note this is time-series rather than 'static' */
205 /* cacheMedianSvcTable */
206 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2", PERF_PROTOSTAT_MEDIAN
, NULL
, NULL
);
208 /* cacheMedianSvcEntry */
209 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2", 1, NULL
, NULL
);
210 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_TIME
, snmp_prfProtoFn
, time_Inst
);
211 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_ALL
, snmp_prfProtoFn
, time_Inst
);
212 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_MISS
, snmp_prfProtoFn
, time_Inst
);
213 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NM
, snmp_prfProtoFn
, time_Inst
);
214 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_HIT
, snmp_prfProtoFn
, time_Inst
);
215 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_QUERY
, snmp_prfProtoFn
, time_Inst
);
216 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_ICP_REPLY
, snmp_prfProtoFn
, time_Inst
);
217 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_DNS
, snmp_prfProtoFn
, time_Inst
);
218 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_RHR
, snmp_prfProtoFn
, time_Inst
);
219 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_BHR
, snmp_prfProtoFn
, time_Inst
);
220 snmpAddNodeStr("1.3.6.1.4.1.3495.1.3.2.2.1", PERF_MEDIAN_HTTP_NH
, snmp_prfProtoFn
, time_Inst
);
222 /* SQ_NET - 1.3.6.1.4.1.3495.1.4 */
223 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 4, NULL
, NULL
);
225 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_IP_CACHE
, NULL
, NULL
);
226 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_ENT
, snmp_netIpFn
, static_Inst
);
227 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_REQ
, snmp_netIpFn
, static_Inst
);
228 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_HITS
, snmp_netIpFn
, static_Inst
);
229 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_PENDHIT
, snmp_netIpFn
, static_Inst
);
230 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_NEGHIT
, snmp_netIpFn
, static_Inst
);
231 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_MISS
, snmp_netIpFn
, static_Inst
);
232 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_GHBN
, snmp_netIpFn
, static_Inst
);
233 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.1", IP_LOC
, snmp_netIpFn
, static_Inst
);
235 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_FQDN_CACHE
, NULL
, NULL
);
236 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_ENT
, snmp_netFqdnFn
, static_Inst
);
237 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_REQ
, snmp_netFqdnFn
, static_Inst
);
238 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_HITS
, snmp_netFqdnFn
, static_Inst
);
239 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_PENDHIT
, snmp_netFqdnFn
, static_Inst
);
240 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_NEGHIT
, snmp_netFqdnFn
, static_Inst
);
241 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_MISS
, snmp_netFqdnFn
, static_Inst
);
242 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.2", FQDN_GHBN
, snmp_netFqdnFn
, static_Inst
);
244 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4", NET_DNS_CACHE
, NULL
, NULL
);
246 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REQ
, snmp_netDnsFn
, static_Inst
);
247 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REP
, snmp_netDnsFn
, static_Inst
);
248 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_SERVERS
, snmp_netDnsFn
, static_Inst
);
250 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REQ
, snmp_netIdnsFn
, static_Inst
);
251 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_REP
, snmp_netIdnsFn
, static_Inst
);
252 snmpAddNodeStr("1.3.6.1.4.1.3495.1.4.3", DNS_SERVERS
, snmp_netIdnsFn
, static_Inst
);
255 /* SQ_MESH - 1.3.6.1.4.1.3495.1.5 */
256 snmpAddNodeStr("1.3.6.1.4.1.3495.1", 5, NULL
, NULL
);
258 /* cachePeerTable - 1.3.6.1.4.1.3495.1.5.1 */
259 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_PTBL
, NULL
, NULL
);
261 /* CachePeerTableEntry (version 3) - 1.3.6.1.4.1.3495.1.5.1.3 */
262 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1", 3, NULL
, NULL
);
263 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_INDEX
, snmp_meshPtblFn
, peer_Inst
);
264 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_NAME
, snmp_meshPtblFn
, peer_Inst
);
265 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR_TYPE
, snmp_meshPtblFn
, peer_Inst
);
266 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ADDR
, snmp_meshPtblFn
, peer_Inst
);
267 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_HTTP
, snmp_meshPtblFn
, peer_Inst
);
268 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_ICP
, snmp_meshPtblFn
, peer_Inst
);
269 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_TYPE
, snmp_meshPtblFn
, peer_Inst
);
270 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_STATE
, snmp_meshPtblFn
, peer_Inst
);
271 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_SENT
, snmp_meshPtblFn
, peer_Inst
);
272 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_PACKED
, snmp_meshPtblFn
, peer_Inst
);
273 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_FETCHES
, snmp_meshPtblFn
, peer_Inst
);
274 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_RTT
, snmp_meshPtblFn
, peer_Inst
);
275 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_IGN
, snmp_meshPtblFn
, peer_Inst
);
276 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_S
, snmp_meshPtblFn
, peer_Inst
);
277 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.1.3", MESH_PTBL_KEEPAL_R
, snmp_meshPtblFn
, peer_Inst
);
279 /* cacheClientTable - 1.3.6.1.4.1.3495.1.5.2 */
280 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5", MESH_CTBL
, NULL
, NULL
);
282 /* BUG 2811: we NEED to create a reliable index for the client DB and make version 3 of the table. */
283 /* for now we have version 2 table with OID capable of mixed IPv4 / IPv6 clients and upgraded address text format. */
285 /* cacheClientEntry - 1.3.6.1.4.1.3495.1.5.2.2 */
286 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2", 2, NULL
, NULL
);
287 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR_TYPE
, snmp_meshCtblFn
, client_Inst
);
288 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ADDR
, snmp_meshCtblFn
, client_Inst
);
289 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTREQ
, snmp_meshCtblFn
, client_Inst
);
290 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTBYTES
, snmp_meshCtblFn
, client_Inst
);
291 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITS
, snmp_meshCtblFn
, client_Inst
);
292 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_HTHITBYTES
, snmp_meshCtblFn
, client_Inst
);
293 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPREQ
, snmp_meshCtblFn
, client_Inst
);
294 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPBYTES
, snmp_meshCtblFn
, client_Inst
);
295 snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITS
, snmp_meshCtblFn
, client_Inst
);
296 mib_tree_last
= snmpAddNodeStr("1.3.6.1.4.1.3495.1.5.2.2", MESH_CTBL_ICPHITBYTES
, snmp_meshCtblFn
, client_Inst
);
298 debugs(49, 9, "snmpInit: Completed SNMP mib tree structure");
302 snmpConnectionOpen(void)
304 debugs(49, 5, "snmpConnectionOpen: Called");
306 if (Config
.Port
.snmp
> 0) {
307 Config
.Addrs
.snmp_incoming
.SetPort(Config
.Port
.snmp
);
309 AsyncCall::Pointer call
= asyncCall(49, 2,
310 "snmpIncomingConnectionOpened",
311 SnmpListeningStartedDialer(&snmpIncomingConnectionOpened
));
313 Ipc::StartListening(SOCK_DGRAM
,
315 Config
.Addrs
.snmp_incoming
,
317 Ipc::fdnInSnmpSocket
, call
);
319 if (!Config
.Addrs
.snmp_outgoing
.IsNoAddr()) {
320 Config
.Addrs
.snmp_outgoing
.SetPort(Config
.Port
.snmp
);
322 AsyncCall::Pointer call
= asyncCall(49, 2,
323 "snmpOutgoingConnectionOpened",
324 SnmpListeningStartedDialer(&snmpOutgoingConnectionOpened
));
326 Ipc::StartListening(SOCK_DGRAM
,
328 Config
.Addrs
.snmp_outgoing
,
330 Ipc::fdnOutSnmpSocket
, call
);
336 snmpIncomingConnectionOpened(int fd
, int errNo
)
338 theInSnmpConnection
= fd
;
339 if (theInSnmpConnection
< 0)
340 fatal("Cannot open Incoming SNMP Port");
342 commSetSelect(theInSnmpConnection
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
,
345 debugs(1, 1, "Accepting SNMP messages on " << Config
.Addrs
.snmp_incoming
<<
346 ", FD " << theInSnmpConnection
<< ".");
348 if (Config
.Addrs
.snmp_outgoing
.IsNoAddr())
349 theOutSnmpConnection
= theInSnmpConnection
;
353 snmpOutgoingConnectionOpened(int fd
, int errNo
)
355 theOutSnmpConnection
= fd
;
356 if (theOutSnmpConnection
< 0)
357 fatal("Cannot open Outgoing SNMP Port");
359 commSetSelect(theOutSnmpConnection
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
,
362 debugs(1, 1, "Outgoing SNMP messages on " << Config
.Addrs
.snmp_outgoing
<<
363 ", FD " << theOutSnmpConnection
<< ".");
366 struct addrinfo
*xaddr
= NULL
;
370 theOutSNMPAddr
.SetEmpty();
372 theOutSNMPAddr
.InitAddrInfo(xaddr
);
374 x
= getsockname(theOutSnmpConnection
, xaddr
->ai_addr
, &xaddr
->ai_addrlen
);
377 debugs(51, 1, "theOutSnmpConnection FD " << theOutSnmpConnection
<< ": getsockname: " << xstrerror());
379 theOutSNMPAddr
= *xaddr
;
381 theOutSNMPAddr
.FreeAddrInfo(xaddr
);
386 snmpConnectionShutdown(void)
388 if (theInSnmpConnection
< 0)
391 if (theInSnmpConnection
!= theOutSnmpConnection
) {
392 debugs(49, 1, "FD " << theInSnmpConnection
<< " Closing SNMP socket");
393 comm_close(theInSnmpConnection
);
397 * Here we set 'theInSnmpConnection' to -1 even though the SNMP 'in'
398 * and 'out' sockets might be just one FD. This prevents this
399 * function from executing repeatedly. When we are really ready to
400 * exit or restart, main will comm_close the 'out' descriptor.
402 theInSnmpConnection
= -1;
405 * Normally we only write to the outgoing SNMP socket, but we
406 * also have a read handler there to catch messages sent to that
407 * specific interface. During shutdown, we must disable reading
408 * on the outgoing socket.
410 assert(theOutSnmpConnection
> -1);
412 commSetSelect(theOutSnmpConnection
, COMM_SELECT_READ
, NULL
, NULL
, 0);
416 snmpConnectionClose(void)
418 snmpConnectionShutdown();
420 if (theOutSnmpConnection
> -1) {
421 debugs(49, 1, "FD " << theOutSnmpConnection
<< " Closing SNMP socket");
422 comm_close(theOutSnmpConnection
);
423 /* make sure the SNMP out connection is unset */
424 theOutSnmpConnection
= -1;
429 * Functions for handling the requests.
433 * Accept the UDP packet
436 snmpHandleUdp(int sock
, void *not_used
)
438 LOCAL_ARRAY(char, buf
, SNMP_REQUEST_SIZE
);
440 snmp_request_t
*snmp_rq
;
443 debugs(49, 5, "snmpHandleUdp: Called.");
445 commSetSelect(sock
, COMM_SELECT_READ
, snmpHandleUdp
, NULL
, 0);
447 memset(buf
, '\0', SNMP_REQUEST_SIZE
);
449 len
= comm_udp_recvfrom(sock
,
457 debugs(49, 3, "snmpHandleUdp: FD " << sock
<< ": received " << len
<< " bytes from " << from
<< ".");
459 snmp_rq
= (snmp_request_t
*)xcalloc(1, sizeof(snmp_request_t
));
460 snmp_rq
->buf
= (u_char
*) buf
;
462 snmp_rq
->sock
= sock
;
463 snmp_rq
->outbuf
= (unsigned char *)xmalloc(snmp_rq
->outlen
= SNMP_REQUEST_SIZE
);
464 snmp_rq
->from
= from
;
465 snmpDecodePacket(snmp_rq
);
466 xfree(snmp_rq
->outbuf
);
469 debugs(49, 1, "snmpHandleUdp: FD " << sock
<< " recvfrom: " << xstrerror());
474 * Turn SNMP packet into a PDU, check available ACL's
477 snmpDecodePacket(snmp_request_t
* rq
)
479 struct snmp_pdu
*PDU
;
481 u_char
*buf
= rq
->buf
;
485 debugs(49, 5, HERE
<< "Called.");
486 PDU
= snmp_pdu_create(0);
487 /* Allways answer on SNMPv1 */
488 rq
->session
.Version
= SNMP_VERSION_1
;
489 Community
= snmp_parse(&rq
->session
, PDU
, buf
, len
);
491 /* Check if we have explicit permission to access SNMP data.
492 * default (set above) is to deny all */
493 if (Community
&& Config
.accessList
.snmp
) {
494 ACLFilledChecklist
checklist(Config
.accessList
.snmp
, NULL
, NULL
);
495 checklist
.src_addr
= rq
->from
;
496 checklist
.snmp_community
= (char *) Community
;
497 allow
= checklist
.fastCheck();
500 if ((snmp_coexist_V2toV1(PDU
)) && (Community
) && (allow
)) {
501 rq
->community
= Community
;
503 debugs(49, 5, "snmpAgentParse: reqid=[" << PDU
->reqid
<< "]");
504 snmpConstructReponse(rq
);
506 debugs(49, 1, HERE
<< "Failed SNMP agent query from : " << rq
->from
);
515 * Packet OK, ACL Check OK, Create reponse.
518 snmpConstructReponse(snmp_request_t
* rq
)
521 struct snmp_pdu
*RespPDU
;
523 debugs(49, 5, "snmpConstructReponse: Called.");
524 RespPDU
= snmpAgentResponse(rq
->PDU
);
525 snmp_free_pdu(rq
->PDU
);
527 if (RespPDU
!= NULL
) {
528 snmp_build(&rq
->session
, RespPDU
, rq
->outbuf
, &rq
->outlen
);
529 comm_udp_sendto(rq
->sock
, rq
->from
, rq
->outbuf
, rq
->outlen
);
530 snmp_free_pdu(RespPDU
);
535 * Decide how to respond to the request, construct a response and
536 * return the response to the requester.
539 static struct snmp_pdu
*
540 snmpAgentResponse(struct snmp_pdu
*PDU
) {
542 struct snmp_pdu
*Answer
= NULL
;
544 debugs(49, 5, "snmpAgentResponse: Called.");
546 if ((Answer
= snmp_pdu_create(SNMP_PDU_RESPONSE
))) {
547 Answer
->reqid
= PDU
->reqid
;
548 Answer
->errindex
= 0;
550 if (PDU
->command
== SNMP_PDU_GET
|| PDU
->command
== SNMP_PDU_GETNEXT
) {
552 int get_next
= (PDU
->command
== SNMP_PDU_GETNEXT
);
553 variable_list
*VarPtr_
;
554 variable_list
**RespVars
= &(Answer
->variables
);
555 oid_ParseFn
*ParseFn
;
557 /* Loop through all variables */
559 for (VarPtr_
= PDU
->variables
; VarPtr_
; VarPtr_
= VarPtr_
->next_variable
) {
560 variable_list
*VarPtr
= VarPtr_
;
561 variable_list
*VarNew
= NULL
;
562 oid
*NextOidName
= NULL
;
563 snint NextOidNameLen
= 0;
568 ParseFn
= snmpTreeNext(VarPtr
->name
, VarPtr
->name_length
, &NextOidName
, &NextOidNameLen
);
570 ParseFn
= snmpTreeGet(VarPtr
->name
, VarPtr
->name_length
);
572 if (ParseFn
== NULL
) {
573 Answer
->errstat
= SNMP_ERR_NOSUCHNAME
;
574 debugs(49, 5, "snmpAgentResponse: No such oid. ");
577 VarPtr
= snmp_var_new(NextOidName
, NextOidNameLen
);
581 int * errstatTmp
= &(Answer
->errstat
);
583 VarNew
= (*ParseFn
) (VarPtr
, (snint
*) errstatTmp
);
586 snmp_var_free(VarPtr
);
589 if ((Answer
->errstat
!= SNMP_ERR_NOERROR
) || (VarNew
== NULL
)) {
590 Answer
->errindex
= index
;
591 debugs(49, 5, "snmpAgentResponse: error.");
594 snmp_var_free(VarNew
);
596 while ((VarPtr
= Answer
->variables
) != NULL
) {
597 Answer
->variables
= VarPtr
->next_variable
;
598 snmp_var_free(VarPtr
);
601 /* Steal the original PDU list of variables for the error response */
602 Answer
->variables
= PDU
->variables
;
604 PDU
->variables
= NULL
;
609 /* No error. Insert this var at the end, and move on to the next.
613 RespVars
= &(VarNew
->next_variable
);
622 snmpTreeGet(oid
* Current
, snint CurrentLen
)
624 oid_ParseFn
*Fn
= NULL
;
625 mib_tree_entry
*mibTreeEntry
= NULL
;
628 debugs(49, 5, "snmpTreeGet: Called");
631 debugs(49, 6, "snmpTreeGet: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
) );
633 mibTreeEntry
= mib_tree_head
;
635 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
638 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
639 mibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
644 if (mibTreeEntry
&& mibTreeEntry
->parsefunction
)
645 Fn
= mibTreeEntry
->parsefunction
;
647 debugs(49, 5, "snmpTreeGet: return");
653 snmpTreeNext(oid
* Current
, snint CurrentLen
, oid
** Next
, snint
* NextLen
)
655 oid_ParseFn
*Fn
= NULL
;
656 mib_tree_entry
*mibTreeEntry
= NULL
, *nextoid
= NULL
;
659 debugs(49, 5, "snmpTreeNext: Called");
662 debugs(49, 6, "snmpTreeNext: Current : " << snmpDebugOid(Current
, CurrentLen
, tmp
));
664 mibTreeEntry
= mib_tree_head
;
666 if (Current
[count
] == mibTreeEntry
->name
[count
]) {
669 while ((mibTreeEntry
) && (count
< CurrentLen
) && (!mibTreeEntry
->parsefunction
)) {
670 mib_tree_entry
*nextmibTreeEntry
= snmpTreeEntry(Current
[count
], count
, mibTreeEntry
);
672 if (!nextmibTreeEntry
)
675 mibTreeEntry
= nextmibTreeEntry
;
679 debugs(49, 5, "snmpTreeNext: Recursed down to requested object");
684 if (mibTreeEntry
== mib_tree_last
)
688 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
689 *NextLen
= CurrentLen
;
690 *Next
= (*mibTreeEntry
->instancefunction
) (Current
, NextLen
, mibTreeEntry
, &Fn
);
693 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
698 if ((mibTreeEntry
) && (mibTreeEntry
->parsefunction
)) {
700 nextoid
= snmpTreeSiblingEntry(Current
[count
], count
, mibTreeEntry
->parent
);
702 debugs(49, 5, "snmpTreeNext: Next OID found for sibling" << nextoid
);
703 mibTreeEntry
= nextoid
;
706 debugs(49, 5, "snmpTreeNext: Attempting to recurse up for next object");
711 if (mibTreeEntry
->parent
->parent
) {
712 nextoid
= mibTreeEntry
->parent
;
713 mibTreeEntry
= snmpTreeEntry(Current
[count
] + 1, count
, nextoid
->parent
);
716 mibTreeEntry
= nextoid
;
720 nextoid
= mibTreeEntry
;
726 while ((mibTreeEntry
) && (!mibTreeEntry
->parsefunction
)) {
727 mibTreeEntry
= mibTreeEntry
->leaves
[0];
731 *NextLen
= mibTreeEntry
->len
;
732 *Next
= (*mibTreeEntry
->instancefunction
) (mibTreeEntry
->name
, NextLen
, mibTreeEntry
, &Fn
);
737 debugs(49, 6, "snmpTreeNext: Next : " << snmpDebugOid(*Next
, *NextLen
, tmp
));
744 static_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
746 oid
*instance
= NULL
;
747 if (*len
<= current
->len
) {
748 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ 1));
749 xmemcpy(instance
, name
, (sizeof(name
) * *len
));
753 *Fn
= current
->parsefunction
;
758 time_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
760 oid
*instance
= NULL
;
761 int identifier
= 0, loop
= 0;
762 int index
[TIME_INDEX_LEN
] = {TIME_INDEX
};
764 if (*len
<= current
->len
) {
765 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ 1));
766 xmemcpy(instance
, name
, (sizeof(name
) * *len
));
767 instance
[*len
] = *index
;
770 identifier
= name
[*len
- 1];
772 while ((loop
< TIME_INDEX_LEN
) && (identifier
!= index
[loop
]))
775 if (loop
< (TIME_INDEX_LEN
- 1)) {
776 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
));
777 xmemcpy(instance
, name
, (sizeof(name
) * *len
));
778 instance
[*len
- 1] = index
[++loop
];
782 *Fn
= current
->parsefunction
;
788 peer_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
790 oid
*instance
= NULL
;
791 peer
*peers
= Config
.peers
;
794 debugs(49, 6, "snmp peer_Inst: No Peers.");
795 current
= current
->parent
->parent
->parent
->leaves
[1];
796 while ((current
) && (!current
->parsefunction
))
797 current
= current
->leaves
[0];
799 instance
= client_Inst(current
->name
, len
, current
, Fn
);
800 } else if (*len
<= current
->len
) {
801 debugs(49, 6, "snmp peer_Inst: *len <= current->len ???");
802 instance
= (oid
*)xmalloc(sizeof(name
) * ( *len
+ 1));
803 xmemcpy(instance
, name
, (sizeof(name
) * *len
));
807 int no
= name
[current
->len
] ;
809 // Note: This works because the Config.peers keeps its index according to its position.
810 for ( i
=0 ; peers
&& (i
< no
) ; peers
= peers
->next
, i
++ ) ;
813 debugs(49, 6, "snmp peer_Inst: Encode peer #" << i
);
814 instance
= (oid
*)xmalloc(sizeof(name
) * (current
->len
+ 1 ));
815 xmemcpy(instance
, name
, (sizeof(name
) * current
->len
));
816 instance
[current
->len
] = no
+ 1 ; // i.e. the next index on cache_peeer table.
818 debugs(49, 6, "snmp peer_Inst: We have " << i
<< " peers. Can't find #" << no
);
822 *Fn
= current
->parsefunction
;
827 client_Inst(oid
* name
, snint
* len
, mib_tree_entry
* current
, oid_ParseFn
** Fn
)
829 oid
*instance
= NULL
;
835 if (*len
<= current
->len
) {
836 aux
= client_entry(NULL
);
843 size
= sizeof(in_addr
);
846 size
= sizeof(in6_addr
);
849 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", size=" << size
);
851 instance
= (oid
*)xmalloc(sizeof(name
) * (*len
+ size
));
852 xmemcpy(instance
, name
, (sizeof(name
) * (*len
)));
854 if ( !laddr
.IsAnyAddr() ) {
855 addr2oid(laddr
, &instance
[ *len
]); // the addr
859 int shift
= *len
- current
->len
; // i.e 4 or 16
860 oid2addr(&name
[*len
- shift
], laddr
,shift
);
861 aux
= client_entry(&laddr
);
867 if (!laddr
.IsAnyAddr()) {
869 newshift
= sizeof(in_addr
);
872 newshift
= sizeof(in6_addr
);
875 debugs(49, 6, HERE
<< "len" << *len
<< ", current-len" << current
->len
<< ", addr=" << laddr
<< ", newshift=" << newshift
);
877 instance
= (oid
*)xmalloc(sizeof(name
) * (current
->len
+ newshift
));
878 xmemcpy(instance
, name
, (sizeof(name
) * (current
->len
)));
879 addr2oid(laddr
, &instance
[current
->len
]); // the addr.
880 *len
= current
->len
+ newshift
;
884 *Fn
= current
->parsefunction
;
893 * Tree utility functions.
897 * Returns a sibling object for the requested child object or NULL
898 * if it does not exit
900 static mib_tree_entry
*
901 snmpTreeSiblingEntry(oid entry
, snint len
, mib_tree_entry
* current
)
903 mib_tree_entry
*next
= NULL
;
906 while ((!next
) && (count
< current
->children
)) {
907 if (current
->leaves
[count
]->name
[len
] == entry
) {
908 next
= current
->leaves
[count
];
914 /* Exactly the sibling on right */
915 if (count
< current
->children
) {
916 next
= current
->leaves
[count
];
925 * Returns the requested child object or NULL if it does not exist
927 static mib_tree_entry
*
928 snmpTreeEntry(oid entry
, snint len
, mib_tree_entry
* current
)
930 mib_tree_entry
*next
= NULL
;
933 while ((!next
) && current
&& (count
< current
->children
)) {
934 if (current
->leaves
[count
]->name
[len
] == entry
) {
935 next
= current
->leaves
[count
];
945 snmpAddNodeChild(mib_tree_entry
*entry
, mib_tree_entry
*child
)
947 debugs(49, 5, "snmpAddNodeChild: assigning " << child
<< " to parent " << entry
);
948 entry
->leaves
= (mib_tree_entry
**)xrealloc(entry
->leaves
, sizeof(mib_tree_entry
*) * (entry
->children
+ 1));
949 entry
->leaves
[entry
->children
] = child
;
950 entry
->leaves
[entry
->children
]->parent
= entry
;
955 snmpLookupNodeStr(mib_tree_entry
*root
, const char *str
)
966 if (! snmpCreateOidFromStr(str
, &name
, &namelen
))
969 /* I wish there were some kind of sensible existing tree traversal
970 * routine to use. I'll worry about that later */
973 return e
; /* XXX it should only be this? */
977 while (r
< namelen
) {
979 /* Find the child node which matches this */
980 for (i
= 0; i
< e
->children
&& e
->leaves
[i
]->name
[r
] != name
[r
]; i
++) ; // seek-loop
982 /* Are we pointing to that node? */
983 if (i
>= e
->children
)
985 assert(e
->leaves
[i
]->name
[r
] == name
[r
]);
987 /* Skip to that node! */
997 snmpCreateOidFromStr(const char *str
, oid
**name
, int *nl
)
999 char const *delim
= ".";
1004 char *s
= xstrdup(str
);
1007 /* Parse the OID string into oid bits */
1008 while ( (p
= strsep(&s_
, delim
)) != NULL
) {
1009 *name
= (oid
*)xrealloc(*name
, sizeof(oid
) * ((*nl
) + 1));
1010 (*name
)[*nl
] = atoi(p
);
1019 * Create an entry. Return a pointer to the newly created node, or NULL
1022 static mib_tree_entry
*
1023 snmpAddNodeStr(const char *base_str
, int o
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
)
1025 mib_tree_entry
*m
, *b
;
1030 /* Find base node */
1031 b
= snmpLookupNodeStr(mib_tree_head
, base_str
);
1034 debugs(49, 5, "snmpAddNodeStr: " << base_str
<< ": -> " << b
);
1036 /* Create OID string for new entry */
1037 snprintf(s
, 1024, "%s.%d", base_str
, o
);
1038 if (! snmpCreateOidFromStr(s
, &n
, &nl
))
1042 m
= snmpAddNode(n
, nl
, parsefunction
, instancefunction
, 0);
1044 /* Link it into the existing tree */
1045 snmpAddNodeChild(b
, m
);
1047 /* Return the node */
1053 * Adds a node to the MIB tree structure and adds the appropriate children
1055 static mib_tree_entry
*
1056 snmpAddNode(oid
* name
, int len
, oid_ParseFn
* parsefunction
, instance_Fn
* instancefunction
, int children
,...)
1060 mib_tree_entry
*entry
= NULL
;
1061 va_start(args
, children
);
1064 debugs(49, 6, "snmpAddNode: Children : " << children
<< ", Oid : " << snmpDebugOid(name
, len
, tmp
));
1066 va_start(args
, children
);
1067 entry
= (mib_tree_entry
*)xmalloc(sizeof(mib_tree_entry
));
1070 entry
->parsefunction
= parsefunction
;
1071 entry
->instancefunction
= instancefunction
;
1072 entry
->children
= children
;
1073 entry
->leaves
= NULL
;
1076 entry
->leaves
= (mib_tree_entry
**)xmalloc(sizeof(mib_tree_entry
*) * children
);
1078 for (loop
= 0; loop
< children
; loop
++) {
1079 entry
->leaves
[loop
] = va_arg(args
, mib_tree_entry
*);
1080 entry
->leaves
[loop
]->parent
= entry
;
1086 /* End of tree utility functions */
1089 * Returns the list of parameters in an oid
1092 snmpCreateOid(int length
,...)
1097 va_start(args
, length
);
1099 new_oid
= (oid
*)xmalloc(sizeof(oid
) * length
);
1102 for (loop
= 0; loop
< length
; loop
++) {
1103 new_oid
[loop
] = va_arg(args
, int);
1111 * Debug calls, prints out the OID for debugging purposes.
1114 snmpDebugOid(oid
* Name
, snint Len
, MemBuf
&outbuf
)
1118 if (outbuf
.isNull())
1119 outbuf
.init(16, MAX_IPSTRLEN
);
1121 for (x
= 0; x
< Len
; x
++) {
1122 size_t bytes
= snprintf(mbuf
, sizeof(mbuf
), ".%u", (unsigned int) Name
[x
]);
1123 outbuf
.append(mbuf
, bytes
);
1125 return outbuf
.content();
1129 snmpSnmplibDebug(int lvl
, char *buf
)
1131 debugs(49, lvl
, buf
);
1137 IPv4 address: 10.10.0.9 ==>
1139 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01 ==>
1140 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1
1143 addr2oid(Ip::Address
&addr
, oid
* Dest
)
1147 struct in_addr iaddr
;
1149 struct in6_addr i6addr
;
1150 oid code
= addr
.IsIPv6()? INETADDRESSTYPE_IPV6
: INETADDRESSTYPE_IPV4
;
1151 u_int size
= (code
== INETADDRESSTYPE_IPV4
) ? sizeof(struct in_addr
):sizeof(struct in6_addr
);
1153 oid code
= INETADDRESSTYPE_IPV4
;
1154 u_int size
= sizeof(struct in_addr
) ;
1155 #endif /* USE_IPV6 */
1157 if ( code
== INETADDRESSTYPE_IPV4
) {
1158 addr
.GetInAddr(iaddr
);
1159 cp
= (u_char
*) &(iaddr
.s_addr
);
1163 addr
.GetInAddr(i6addr
);
1164 cp
= (u_char
*) &i6addr
;
1167 for ( i
=0 ; i
< size
; i
++) {
1168 // OID's are in network order
1172 debugs(49, 7, "addr2oid: Dest : " << snmpDebugOid(Dest
, size
, tmp
));
1176 oid == 10.10.0.9 ==>
1177 IPv4 address: 10.10.0.9
1178 oid == 32.1.50.239.162.33.251.20.50.0.0.0.0.0.0.0.0.0.1 ==>
1179 IPv6 adress : 20:01:32:ef:a2:21:fb:32:00:00:00:00:00:00:00:00:OO:01
1182 oid2addr(oid
* id
, Ip::Address
&addr
, u_int size
)
1184 struct in_addr iaddr
;
1188 struct in6_addr i6addr
;
1189 if ( size
== sizeof(struct in_addr
) )
1190 #endif /* USE_IPV6 */
1191 cp
= (u_char
*) &(iaddr
.s_addr
);
1194 cp
= (u_char
*) &(i6addr
);
1195 #endif /* USE_IPV6 */
1197 debugs(49, 7, "oid2addr: id : " << snmpDebugOid(id
, size
, tmp
) );
1198 for (i
=0 ; i
<size
; i
++) {
1202 if ( size
== sizeof(struct in_addr
) )
1212 /* SNMP checklists */
1213 #include "acl/Strategy.h"
1214 #include "acl/Strategised.h"
1215 #include "acl/StringData.h"
1217 class ACLSNMPCommunityStrategy
: public ACLStrategy
<char const *>
1221 virtual int match (ACLData
<MatchType
> * &, ACLFilledChecklist
*);
1222 static ACLSNMPCommunityStrategy
*Instance();
1223 /* Not implemented to prevent copies of the instance. */
1224 /* Not private to prevent brain dead g+++ warnings about
1225 * private constructors with no friends */
1226 ACLSNMPCommunityStrategy(ACLSNMPCommunityStrategy
const &);
1229 static ACLSNMPCommunityStrategy Instance_
;
1230 ACLSNMPCommunityStrategy() {}
1232 ACLSNMPCommunityStrategy
&operator=(ACLSNMPCommunityStrategy
const &);
1235 class ACLSNMPCommunity
1239 static ACL::Prototype RegistryProtoype
;
1240 static ACLStrategised
<char const *> RegistryEntry_
;
1243 ACL::Prototype
ACLSNMPCommunity::RegistryProtoype(&ACLSNMPCommunity::RegistryEntry_
, "snmp_community");
1244 ACLStrategised
<char const *> ACLSNMPCommunity::RegistryEntry_(new ACLStringData
, ACLSNMPCommunityStrategy::Instance(), "snmp_community");
1247 ACLSNMPCommunityStrategy::match (ACLData
<MatchType
> * &data
, ACLFilledChecklist
*checklist
)
1249 return data
->match (checklist
->snmp_community
);
1252 ACLSNMPCommunityStrategy
*
1253 ACLSNMPCommunityStrategy::Instance()
1258 ACLSNMPCommunityStrategy
ACLSNMPCommunityStrategy::Instance_
;