1 // implement a prototype SNMP Agent using ASNMP and ACE
3 #include <ace/Reactor.h>
4 #include <ace/SOCK_Dgram.h>
5 #include <ace/INET_Addr.h>
6 #include <ace/Signal.h>
8 #include "agent_impl.h"
10 agent_impl::agent_impl(unsigned short port
, const char *rd
, const char *wr
) :
13 ACE_TRACE("agent_impl::agent_impl");
14 tgt_
.set_read_community(rd
);
15 tgt_
.set_write_community(wr
);
19 agent_impl::~agent_impl()
21 ACE_TRACE("agent_impl::~agent_impl");
24 // callback : have received a Pdu from the target host with given read comm str
25 // this is really simplistic, but gives the general idea
26 int agent_impl::handle_get( Pdu
&pdu
, UdpTarget
&target
)
28 ACE_TRACE("agent_impl::handle_get");
29 OctetStr mgr_rd_str
, agent_rd_str
;
30 target
.get_read_community(mgr_rd_str
); // requster's read community string
31 tgt_
.get_read_community(agent_rd_str
); // this agent's read community string
33 // 1. verify we have a valid read string else drop pdu (no response to caller)
34 if (mgr_rd_str
!= agent_rd_str
) {
35 ACE_DEBUG((LM_DEBUG
, "agent_impl::handle_get: invalid read community recvd\n"));
39 // 2. iterate over each varbind in the pdu, filling providing responses
41 for (int i
= 0; (i
< pdu
.get_vb_count()) && !fdone
; i
++) {
44 if (get_response(vb
)) { // set a value for the oid if we can else
45 set_error_status(&pdu
, SNMP_ERROR_NO_SUCH_NAME
); // these ought to be member
46 set_error_index(&pdu
, i
); // functions but are not yet...
47 fdone
++; // trigger flag to exit loop early
49 else // failed, return noSuch error
53 // 3. lastly, return the pkt to the caller
54 return respond(pdu
, target
);
57 // this routine makes up the brains of the agent
58 // it knows only the MIB II system group set of variables for a get operation
59 int agent_impl::get_response(Vb
& vb
)
61 // these objects represent the MIB II system group per RFC 1213
62 static Oid
sysDescr("1.3.6.1.2.1.1.1.0"),
63 sysObjectID("1.3.6.1.2.1.1.2.0"), sysUpTime("1.3.6.1.2.1.1.3.0"),
64 sysContact("1.3.6.1.2.1.1.4.0"), sysName("1.3.6.1.2.1.1.5.0"),
65 sysLocation("1.3.6.1.2.1.1.6.0"), sysServices("1.3.6.1.2.1.1.7.0");
69 if (oid
== sysDescr
) {
70 OctetStr
desc("ASNMP Prototype Agent 1.0");
73 else if (oid
== sysObjectID
) { // the IANA gives assigns Enterprise Numbers
74 // see ftp://ftp.isi.edu/in-notes/iana/assignments/enterprise-numbers
75 // for the official list of enterprise numbers. Then under this tree
76 // assign a unique subtree to identify this agent
77 Oid
id("1.3.6.1.4.1.2533.9.1");
80 else if (oid
== sysUpTime
) {
82 agent_clock_
.elapsed_time (tv
);
83 TimeTicks
tt(tv
.msec());
86 else if (oid
== sysContact
) {
87 OctetStr
contact("mrm@acm.org");
88 vb
.set_value(contact
);
90 else if (oid
== sysName
) {
91 OctetStr
fqdn("foo.org"); // extract this from the gethostbyname() TODO
94 else if (oid
== sysLocation
) {
98 else if (oid
== sysServices
) {
103 return 1; // noSuchName
108 int agent_impl::handle_get_next( Pdu
&, UdpTarget
&)
110 ACE_TRACE("agent_impl::handle_get_next -NI");
114 int agent_impl::handle_set( Pdu
&, UdpTarget
&)
116 ACE_TRACE("agent_impl::handle_set -NI");
120 // stuff used by process_requests
122 // called when SIGINT
123 static sig_atomic_t finished
= 0;
128 ACE_TRACE("::sig_handler");
132 int agent_impl::process_requests()
134 ACE_TRACE("agent_impl::process_requests");
137 ACE_Sig_Action
sa ((ACE_SignalHandler
) sig_handler
, SIGINT
);
140 // Read data from other side.
141 if (reactor
.register_handler (this, ACE_Event_Handler::READ_MASK
) == -1)
142 ACE_ERROR_RETURN ((LM_ERROR
, "ACE_Reactor::register_handler"), -1);
144 // TODO: register signal handler to shut down gracefully here
148 reactor
.handle_events ();
149 ACE_DEBUG ((LM_DEBUG
, "return from handle events\n"));
152 ACE_DEBUG ((LM_DEBUG
, "return from handle events - normal shut down\n"));