#include "acl/FilledChecklist.h"
#include "cache_snmp.h"
#include "comm.h"
+#include "ipc/StartListening.h"
#include "compat/strsep.h"
#include "ip/Address.h"
#define SNMP_REQUEST_SIZE 4096
#define MAX_PROTOSTAT 5
+/// dials snmpConnectionOpened call
+class SnmpListeningStartedDialer: public CallDialer,
+ public Ipc::StartListeningCb
+{
+public:
+ typedef void (*Handler)(int fd, int errNo);
+ SnmpListeningStartedDialer(Handler aHandler): handler(aHandler) {}
+
+ virtual void print(std::ostream &os) const { startPrint(os) << ')'; }
+
+ virtual bool canDial(AsyncCall &) const { return true; }
+ virtual void dial(AsyncCall &) { (handler)(fd, errNo); }
+
+public:
+ Handler handler;
+};
+
+
Ip::Address theOutSNMPAddr;
typedef struct _mib_tree_entry mib_tree_entry;
mib_tree_entry *mib_tree_head;
mib_tree_entry *mib_tree_last;
+static void snmpIncomingConnectionOpened(int fd, int errNo);
+static void snmpOutgoingConnectionOpened(int fd, int errNo);
+
static mib_tree_entry * snmpAddNodeStr(const char *base_str, int o, oid_ParseFn * parsefunction, instance_Fn * instancefunction);
static mib_tree_entry *snmpAddNode(oid * name, int len, oid_ParseFn * parsefunction, instance_Fn * instancefunction, int children,...);
static oid *snmpCreateOid(int length,...);
void
snmpConnectionOpen(void)
{
- struct addrinfo *xaddr = NULL;
- int x;
-
debugs(49, 5, "snmpConnectionOpen: Called");
if (Config.Port.snmp > 0) {
Config.Addrs.snmp_incoming.SetPort(Config.Port.snmp);
- enter_suid();
- theInSnmpConnection = comm_open_listener(SOCK_DGRAM,
- IPPROTO_UDP,
- Config.Addrs.snmp_incoming,
- COMM_NONBLOCKING,
- "SNMP Port");
- leave_suid();
- if (theInSnmpConnection < 0)
- fatal("Cannot open SNMP Port");
+ AsyncCall::Pointer call = asyncCall(49, 2,
+ "snmpIncomingConnectionOpened",
+ SnmpListeningStartedDialer(&snmpIncomingConnectionOpened));
- commSetSelect(theInSnmpConnection, COMM_SELECT_READ, snmpHandleUdp, NULL, 0);
-
- debugs(1, 1, "Accepting SNMP messages on " << Config.Addrs.snmp_incoming << ", FD " << theInSnmpConnection << ".");
+ Ipc::StartListening(SOCK_DGRAM,
+ IPPROTO_UDP,
+ Config.Addrs.snmp_incoming,
+ COMM_NONBLOCKING,
+ Ipc::fdnInSnmpSocket, call);
if (!Config.Addrs.snmp_outgoing.IsNoAddr()) {
Config.Addrs.snmp_outgoing.SetPort(Config.Port.snmp);
- enter_suid();
- theOutSnmpConnection = comm_open_listener(SOCK_DGRAM,
- IPPROTO_UDP,
- Config.Addrs.snmp_outgoing,
- COMM_NONBLOCKING,
- "SNMP Port");
- leave_suid();
- if (theOutSnmpConnection < 0)
- fatal("Cannot open Outgoing SNMP Port");
+ AsyncCall::Pointer call = asyncCall(49, 2,
+ "snmpOutgoingConnectionOpened",
+ SnmpListeningStartedDialer(&snmpOutgoingConnectionOpened));
+
+ Ipc::StartListening(SOCK_DGRAM,
+ IPPROTO_UDP,
+ Config.Addrs.snmp_outgoing,
+ COMM_NONBLOCKING,
+ Ipc::fdnOutSnmpSocket, call);
+ }
+ }
+}
+
+static void
+snmpIncomingConnectionOpened(int fd, int errNo)
+{
+ theInSnmpConnection = fd;
+ if (theInSnmpConnection < 0)
+ fatal("Cannot open Incoming SNMP Port");
- commSetSelect(theOutSnmpConnection,
- COMM_SELECT_READ,
- snmpHandleUdp,
- NULL, 0);
+ commSetSelect(theInSnmpConnection, COMM_SELECT_READ, snmpHandleUdp, NULL,
+ 0);
- debugs(1, 1, "Outgoing SNMP messages on " << Config.Addrs.snmp_outgoing << ", FD " << theOutSnmpConnection << ".");
+ debugs(1, 1, "Accepting SNMP messages on " << Config.Addrs.snmp_incoming <<
+ ", FD " << theInSnmpConnection << ".");
- fd_note(theOutSnmpConnection, "Outgoing SNMP socket");
+ if (Config.Addrs.snmp_outgoing.IsNoAddr())
+ theOutSnmpConnection = theInSnmpConnection;
+}
+
+static void
+snmpOutgoingConnectionOpened(int fd, int errNo)
+{
+ theOutSnmpConnection = fd;
+ if (theOutSnmpConnection < 0)
+ fatal("Cannot open Outgoing SNMP Port");
+
+ commSetSelect(theOutSnmpConnection, COMM_SELECT_READ, snmpHandleUdp, NULL,
+ 0);
+
+ debugs(1, 1, "Outgoing SNMP messages on " << Config.Addrs.snmp_outgoing <<
+ ", FD " << theOutSnmpConnection << ".");
+
+ {
+ struct addrinfo *xaddr = NULL;
+ int x;
- fd_note(theInSnmpConnection, "Incoming SNMP socket");
- } else {
- theOutSnmpConnection = theInSnmpConnection;
- }
theOutSNMPAddr.SetEmpty();
}
int i, r = 1;
- while (r <= namelen) {
+ while (r < namelen) {
/* Find the child node which matches this */
for (i = 0; i < e->children && e->leaves[i]->name[r] != name[r]; i++) ; // seek-loop
*name = NULL;
*nl = 0;
char *s = xstrdup(str);
+ char *s_ = s;
/* Parse the OID string into oid bits */
- while ( (p = strsep(&s, delim)) != NULL) {
+ while ( (p = strsep(&s_, delim)) != NULL) {
*name = (oid*)xrealloc(*name, sizeof(oid) * ((*nl) + 1));
(*name)[*nl] = atoi(p);
(*nl)++;