Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
client.hpp
1 #pragma once
2 
3 #include <chrono>
4 #include <list>
5 #include <memory>
6 #include <string>
7 #include <stdexcept>
8 
9 #include "context.hpp"
10 #include "exception.hpp"
11 #include "rawresponse.hpp"
12 #include "response.hpp"
13 
14 #include "securitymodel.hpp"
15 #include "securitymodel/usm.hpp"
16 #include "types.hpp"
17 
18 namespace dangerous { namespace snmp {
19 
20 class ByteStream;
21 class MessageProcessor;
22 class Transport;
23 
24 
60 class Client {
61 public:
68  Client( const Context& context );
69 
73  virtual ~Client();
74 
82  std::string uri() const;
95  void uri( const std::string& uri ) throw( ParseErrorException, UnknownTransportException, Exception );
96 
102  std::chrono::milliseconds timeout() const { return _timeout; }
108  void timeout( const std::chrono::milliseconds& timeout ) { _timeout = timeout; }
109 
115  void useVersion1( const std::string& communityString );
116 
122  void useVersion2c( const std::string& communityString );
123 
130 
136  bool isAuthenticated();
137 
141  void authenticate();
142 
143  /*
144  * Raw protocol operations.
145  *
146  * These can be used to speak strict SNMP without the extra "magic" that we
147  * would otherwise do (such as retrying requests).
148  *
149  * All of these functions begin with "raw_".
150  */
151 
164  std::unique_ptr<RawResponse> raw_get( PDU& requestPdu ) throw( Exception );
165 
178  std::unique_ptr<RawResponse> raw_getnext( PDU& requestPdu ) throw( Exception );
179 
192  std::unique_ptr<RawResponse> raw_getbulk( PDU& requestPdu ) throw( Exception );
193 
206  std::unique_ptr<RawResponse> raw_set( PDU& requestPdu ) throw( Exception );
207 
208  /*
209  * Logical operations.
210  *
211  * These operations do what is usually _expected_ of SNMP; they may re-issue
212  * a request, or they may break a PDU into multiple parts (for example), in
213  * order to get the best results.
214  */
215 
222  std::unique_ptr<Response> get( const std::list<NumericOid>& oidList ) throw( Exception );
223 
230  std::unique_ptr<Response> getNext( const std::list<NumericOid>& oidList );
231 
240  std::unique_ptr<Response> getBulk( const std::list<NumericOid>& singleRequestOids, unsigned int multipleRequestLimit, const std::list<NumericOid>& multipleRequestOids );
241 
252  std::unique_ptr<Response> set( const VarBindList& varbinds ) throw( Exception );
253 
274  std::unique_ptr<Response> walk( const NumericOid& oid, unsigned int limit = 0, const NumericOid& startingOid = NumericOid() );
275 
276 protected:
286  template<typename PduEncoder>
287  void lowLevelSend( SecurityModel* outgoingSecurityModel, PDU& request ) throw( Exception );
288 
298  std::unique_ptr<ByteStream> lowLevelReceive( std::unique_ptr<SecurityModel>& incomingSecurityModel ) throw( Exception );
299 
311  template<typename PduEncoder>
312  std::unique_ptr<RawResponse> basicRequest( PDU& request, SecurityModel* desiredSecurityModel = nullptr ) throw( Exception );
313 
314 protected:
316  std::weak_ptr<Context::PrivateData> context;
317 
321  std::unique_ptr<Transport> transport;
322 
327  std::unique_ptr<MessageProcessor> messageProcessor;
328 
332  std::unique_ptr<SecurityModel> securityModel;
333 
336  int32_t requestId;
337 
339  std::chrono::milliseconds _timeout;
340 
342  std::string _uri;
343 };
344 
345 } }
346 
std::weak_ptr< Context::PrivateData > context
This is a weak pointer to the Context.
Definition: client.hpp:316
std::unique_ptr< RawResponse > raw_getnext(PDU &requestPdu)
This issues a "getnext" request.
int32_t requestId
This is the current request ID.
Definition: client.hpp:336
bool isAuthenticated()
TODO: ?????????
A PDU represents an SNMP Protocol Data Unit (PDU).
Definition: pdu.hpp:56
std::unique_ptr< RawResponse > raw_set(PDU &requestPdu)
This issues a "set" request.
void authenticate()
TODO: ?????????
std::unique_ptr< RawResponse > raw_get(PDU &requestPdu)
This issues a "get" request.
void lowLevelSend(SecurityModel *outgoingSecurityModel, PDU &request)
Internal use only.
std::unique_ptr< Response > set(const VarBindList &varbinds)
This issues a "set" request.
A Dangerous SNMP "Context" is the backing for the rest of the Dangerous SNMP libraries.
Definition: context.hpp:18
std::unique_ptr< Response > getNext(const std::list< NumericOid > &oidList)
This issues a simple "getnext" request.
std::unique_ptr< MessageProcessor > messageProcessor
This is a pointer to the "MessageProcessor" instance that will be handling the SNMP version envelope...
Definition: client.hpp:327
void useVersion2c(const std::string &communityString)
This configures the client for SNMPv2c.
The User-based Security Model (USM) associates a user (by "user name") with security information...
Definition: usm.hpp:36
std::unique_ptr< ByteStream > lowLevelReceive(std::unique_ptr< SecurityModel > &incomingSecurityModel)
Internal use only.
A NumericOid represents an "true" OID; that is, one identified by a series of integers (as opposed to...
Definition: numericoid.hpp:22
std::chrono::milliseconds timeout() const
This returns the timeout for the connection.
Definition: client.hpp:102
std::chrono::milliseconds _timeout
This is the timeout for the connection.
Definition: client.hpp:339
void useVersion3(const securitymodel::USM &securityModel)
This configures the client for SNMPv3/USM.
std::unique_ptr< Transport > transport
This is a pointer to the "Transport" instance that will be used to communicate with the SNMP agent...
Definition: client.hpp:321
This defines the base exception class for Dangerous SNMP.
Definition: exception.hpp:10
virtual ~Client()
Destructor.
std::unique_ptr< RawResponse > raw_getbulk(PDU &requestPdu)
This issues a "getbulk" request.
std::unique_ptr< SecurityModel > securityModel
This is the kind of security model that is being used.
Definition: client.hpp:332
Client(const Context &context)
This constructs a new Client.
This represents a parse-error event.
Definition: exception.hpp:135
This represents an unknown transport event.
Definition: exception.hpp:173
std::string _uri
This is the URI that was given for the connection.
Definition: client.hpp:342
void timeout(const std::chrono::milliseconds &timeout)
This sets the timeout for the connection.
Definition: client.hpp:108
std::string uri() const
This returns the URI for the connection.
This class is the base class for all security models.
Definition: securitymodel.hpp:29
std::unique_ptr< Response > getBulk(const std::list< NumericOid > &singleRequestOids, unsigned int multipleRequestLimit, const std::list< NumericOid > &multipleRequestOids)
This issues a "getbulk" request.
void useVersion1(const std::string &communityString)
This configures the client for SNMPv1.
std::unique_ptr< Response > walk(const NumericOid &oid, unsigned int limit=0, const NumericOid &startingOid=NumericOid())
This performs a "walk" operation.
std::vector< std::unique_ptr< VarBind > > VarBindList
This defines a list of varbinds.
Definition: types.hpp:116
A Client instance is used to communicate with an SNMP agent.
Definition: client.hpp:60
std::unique_ptr< RawResponse > basicRequest(PDU &request, SecurityModel *desiredSecurityModel=nullptr)
Internal use only.