Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
agentx.hpp
1 #pragma once
2 
3 #include "context.hpp"
4 #include "exception.hpp"
5 #include "numericoid.hpp"
6 #include "variant.hpp"
7 
8 #include <condition_variable>
9 #include <map>
10 #include <mutex>
11 #include <string>
12 #include <thread>
13 
14 namespace dangerous { namespace snmp {
15 
16 class AgentxPacket;
17 class Transport;
18 
19 
37 class AgentX {
38 public:
41  static const uint8_t DEFAULT_PRIORITY;
42 
58  public:
66  typedef std::function<bool(const NumericOid& oid, Variant& value)> GetFunction;
67 
76  typedef std::function<bool(const NumericOid& oid, NumericOid& nextOid, Variant& value)> GetNextFunction;
77 
81  enum Type {
88  };
89  public:
94 
95  public:
99  uint8_t priority;
100 
103 
106 
109  };
110 
111 public:
117  AgentX( const Context& context );
118 
124  virtual ~AgentX();
125 
143  void uri( const std::string& uri ) throw( ParseErrorException, UnknownTransportException, Exception );
144 
151  void identifier( const NumericOid& oid ) { _identifier = oid; }
152 
157  NumericOid identifier() const { return _identifier; }
158 
164  void description( const std::string& text ) { _description = text; }
165 
171  std::string description() const { return _description; }
172 
173  /*
174  * Raw protocol operations.
175  */
176 
185  void raw_addAgentCapabilities( const NumericOid& oid, const std::string& description );
186 
194  void raw_removeAgentCapabilities( const NumericOid& oid );
195 
204  void raw_registerTree( const NumericOid& oid, uint8_t priority = DEFAULT_PRIORITY );
205 
215  void raw_registerOid( const NumericOid& oid, uint8_t priority = DEFAULT_PRIORITY );
216 
217 
218  /*
219  * Logical protocol operations.
220  */
221 
230  void addAgentCapabilities( const NumericOid& oid, const std::string& description ) throw( Exception );
231 
240  void removeAgentCapabilities( const NumericOid& oid ) throw( Exception );
241 
249  void registerOid( const NumericOid& oid, const OidRegistrationEntry& entry ) throw ( Exception );
250 
251  // TODO: registerSimpleOid( const NumericOid& oid, const Variant& value );
252  // TODO: registerSimpleTree( const NumericOid& oid, OidRegistrationEntry::BeforeGetFunction beforeGetFunction );
253 
254 protected:
260  class ValueEntry {
261  public:
264 
266  uint32_t id;
267  };
268 
269 
271  std::weak_ptr<Context::PrivateData> context;
272 
278  std::string _description;
279 
283  std::unique_ptr<Transport> transport;
284 
287  uint32_t packetNumber;
288 
290  uint32_t sessionId;
291 
292  /*
293  * Mailbox variables.
294  */
295 
298  std::mutex readMutex;
301  std::condition_variable readConditionVariable;
302 
305  std::mutex writeMutex;
306 
309  std::map< uint32_t, std::unique_ptr<AgentxPacket> > inbox;
310 
313  bool isStopped;
315  std::thread mailboxThread;
316 
317  /*
318  * Capabilities.
319  */
320 
324  typedef std::map< NumericOid, std::string > CapabilityMap;
325 
329 
330  /*
331  * OID registration.
332  */
333 
337  typedef std::map< NumericOid, OidRegistrationEntry > OidRegistrationMap;
338 
344 
358  OidRegistrationMap::const_iterator registrationThatCovers( const NumericOid& oid );
359 
365  void rawRead( AgentxPacket& packet );
366 
372  void rawWrite( AgentxPacket& packet );
373 
382  void open();
383 
387  void close();
388 
389 
390  /*
391  * Mailbox functions.
392  */
393 
397  void startMailboxThread();
398 
402  void stopMailboxThread();
403 
417  void mailboxRead( AgentxPacket& packet, uint32_t packetId ) throw( Exception );
418 
422  void mailboxMain();
423 };
424 
425 } }
426 
This registration is for an instance of a table-based OID.
Definition: agentx.hpp:85
void uri(const std::string &uri)
This sets the URI for the client.
GetFunction getFunction
This function will be called when the value of the OID is requested.
Definition: agentx.hpp:105
OidRegistrationEntry()
Constructor.
Definition: agentx.hpp:93
void removeAgentCapabilities(const NumericOid &oid)
This removes a previously registered capability.
std::thread mailboxThread
This is the thread that runs the main loop for handling the mailbox.
Definition: agentx.hpp:315
static const uint8_t DEFAULT_PRIORITY
This is the default value for the "priority" of an OID registered with the "Register" PDU...
Definition: agentx.hpp:41
A Variant defines an arbitrary value (within the scope of SNMP).
Definition: variant.hpp:78
An AgentX represents an AgentX sub-agent.
Definition: agentx.hpp:37
uint32_t sessionId
This is the session ID, as given by the master agent.
Definition: agentx.hpp:290
void registerOid(const NumericOid &oid, const OidRegistrationEntry &entry)
This registers a new OID.
void raw_removeAgentCapabilities(const NumericOid &oid)
This sends a "RemoveAgentCaps" PDU to the master agent.
A Dangerous SNMP "Context" is the backing for the rest of the Dangerous SNMP libraries.
Definition: context.hpp:18
std::map< NumericOid, OidRegistrationEntry > OidRegistrationMap
An OidRegistrationMap maps an OID to its registration entry.
Definition: agentx.hpp:337
void addAgentCapabilities(const NumericOid &oid, const std::string &description)
This adds a new capability for the agent.
void mailboxMain()
This is the main loop for the mailbox thread.
GetNextFunction getNextFunction
This function will be called when the next OID is requested.
Definition: agentx.hpp:108
OidRegistrationMap oidRegistrationMap
This is the mapping of an OID to its registration entry.
Definition: agentx.hpp:343
void open()
This connects the agent to the master agent.
std::string description() const
This returns the description for the agent.
Definition: agentx.hpp:171
std::unique_ptr< Transport > transport
This is a pointer to the "Transport" instance that will be used to communicate with the AgentX "maste...
Definition: agentx.hpp:283
void raw_addAgentCapabilities(const NumericOid &oid, const std::string &description)
This sends an "AddAgentCaps" PDU to the master agent.
A NumericOid represents an "true" OID; that is, one identified by a series of integers (as opposed to...
Definition: numericoid.hpp:22
AgentX(const Context &context)
Constructor.
std::function< bool(const NumericOid &oid, NumericOid &nextOid, Variant &value)> GetNextFunction
The GetNextFunction is called when the next OID is requested.
Definition: agentx.hpp:76
uint32_t id
TODO: THIS IS NOT REALLY USED ANYWHERE.
Definition: agentx.hpp:266
void startMailboxThread()
This starts the mailbox thread, if it has not already been started.
uint8_t priority
This is the priority of the OID.
Definition: agentx.hpp:93
void rawRead(AgentxPacket &packet)
This reads an AgentxPacket from the transport.
void identifier(const NumericOid &oid)
This sets the identifier OID for the agent.
Definition: agentx.hpp:151
std::mutex readMutex
This is the read mutex used for guaranteeing that only a single thread is working with the inbox for ...
Definition: agentx.hpp:298
NumericOid _identifier
This is the OID that identifies the Agent; each Agent should have a unique identifying OID...
Definition: agentx.hpp:276
void raw_registerTree(const NumericOid &oid, uint8_t priority=DEFAULT_PRIORITY)
This sends a "Register" PDU to the master agent.
virtual ~AgentX()
Destructor.
std::function< bool(const NumericOid &oid, Variant &value)> GetFunction
The GetFunction is called when the value of an OID is requested.
Definition: agentx.hpp:66
Type
This represents the kind of OID that is being registered.
Definition: agentx.hpp:81
std::string _description
This is the description of the Agent.
Definition: agentx.hpp:278
This defines the base exception class for Dangerous SNMP.
Definition: exception.hpp:10
std::condition_variable readConditionVariable
This is used to notify any waiting threads when a new packet is available from the inbox...
Definition: agentx.hpp:301
Type type
This is the type of registration TODO TODO TODO.
Definition: agentx.hpp:102
std::map< uint32_t, std::unique_ptr< AgentxPacket > > inbox
This represents an "inbox" of packets received from the master agent.
Definition: agentx.hpp:309
NumericOid identifier() const
This returns the identifier OID for the agent.
Definition: agentx.hpp:157
This registration is for an individual OID.
Definition: agentx.hpp:83
std::weak_ptr< Context::PrivateData > context
This is a weak pointer to the Context.
Definition: agentx.hpp:271
void rawWrite(AgentxPacket &packet)
This writes an AgentxPacket to the transport.
uint32_t packetNumber
This is a counter that is incremented for each packet sent by this agent.
Definition: agentx.hpp:287
This represents a parse-error event.
Definition: exception.hpp:135
std::mutex writeMutex
This is the write mutex used for guaranteeing that only a single thread is speaking for this agent at...
Definition: agentx.hpp:305
void stopMailboxThread()
This stops the mailbox thread, if it has not already been stopped.
This represents an unknown transport event.
Definition: exception.hpp:173
An OidRegistrationEntry represents an OID that has been registered.
Definition: agentx.hpp:57
void close()
This disconnects the agent from the master agent.
void description(const std::string &text)
This sets the description for the agent.
Definition: agentx.hpp:164
This registration is for a tree; it will handle all OIDs under the given one.
Definition: agentx.hpp:87
CapabilityMap capabilityMap
This is the mapping of capability OID to its description; these are created by adding a new "capabili...
Definition: agentx.hpp:328
bool isStopped
This is whether or not the mailbox thread is "stopped"; if the thread has not been started...
Definition: agentx.hpp:313
void raw_registerOid(const NumericOid &oid, uint8_t priority=DEFAULT_PRIORITY)
This sends a "Register" PDU to the master agent with a flag signifying "instance" registration...
OidRegistrationMap::const_iterator registrationThatCovers(const NumericOid &oid)
This returns the "oidRegistrationMap" iterator whose entry would be responsible for the OID given...
std::map< NumericOid, std::string > CapabilityMap
A CapabilityMap maps an OID to a string.
Definition: agentx.hpp:324
void mailboxRead(AgentxPacket &packet, uint32_t packetId)
This attempts to obtain the packet, with the specified ID, from the mailbox.
A ValueEntry represents an OID's value.
Definition: agentx.hpp:260
Variant value
This is the value of the OID.
Definition: agentx.hpp:263