summaryrefslogtreecommitdiff
path: root/ASNMP/asnmp/sagent.cpp
blob: 489975afbc14048b4e5e4a3a6a7e66e820935b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// $Id$

// ============================================================================
//
// = LIBRARY
//    asnmp
//
// = FILENAME
//    sagent.cpp
//
// = DESCRIPTION
//   SNMP agent class definition. The sagent class provides an object oriented
//   approach for creating SNMP Agents. The sagent class is an encapsulation 
//   of SNMP sessions, gets, sets, etc. 
//
// = AUTHOR
//    Michael R. MacFaden 
//
// ============================================================================
#include "ace/Reactor.h"
#include "ace/SOCK_Dgram.h"

#include "asnmp/oid.h"                // snmp++ oid class
#include "asnmp/vb.h"                 // snbmp++ vb class
#include "asnmp/target.h"             // snmp++ target class
#include "asnmp/pdu.h"                // snmp++ pdu class
#include "asnmp/snmperrs.h"           // error macros and strings 
#include "asnmp/address.h"            // snmp++ address class defs
#include "asnmp/snmp.h"               // manager snmp interface 
#include "asnmp/sagent.h"             // agent interface
#include "asnmp/transaction.h"	      // convert from wire to API

ACE_RCSID(asnmp, sagent, "$Id$")

sagent::sagent(unsigned short port): Snmp(port)
{
  ACE_TRACE("sagent::sagent(short)");
}

sagent::~sagent()
{
  ACE_TRACE("sagent::~sagent");
}

int sagent::handle_input(ACE_HANDLE fd)
{
  ACE_TRACE("sagent::handle_input");

  transaction tr(iv_snmp_session_); // this section needs a better design
  tr.handle_input(fd);
  char rcv_com_str[MAX_COMM_STR_LEN];
  if (tr.result(pdu_, rcv_com_str) < 0)
     return 0;
  OctetStr community(rcv_com_str);
  const ACE_INET_Addr &ta = tr.get_from_addr();
  char buf_tmp[MAXHOSTNAMELEN + 1];
  ta.addr_to_string (buf_tmp, MAXHOSTNAMELEN);
  UdpAddress ra(buf_tmp);
  tgt_.set_address(ra);


  // process msg here by calling subclass's implementation
  switch (pdu_.get_type()){
  case sNMP_PDU_GET:
    tgt_.set_read_community(community);
    this->handle_get(pdu_, tgt_);
    break;

  case sNMP_PDU_GETNEXT:
    tgt_.set_read_community(community);
    this->handle_get_next(pdu_, tgt_);
    break;

  case sNMP_PDU_SET:
    tgt_.set_write_community(community);
    this->handle_set(pdu_, tgt_);
    break;

  default:
    ACE_ASSERT(0);
  }
  return 0;
}

ACE_HANDLE sagent::get_handle() const
{
  ACE_TRACE("sagent::get_handle");
  return iv_snmp_session_.get_handle();
}

int sagent::respond(Pdu& pdu,UdpTarget& tgt)
{
  pdu.set_type(sNMP_PDU_RESPONSE);
  transaction tr(pdu, tgt, iv_snmp_session_);
  tr.send();
  return 0;
}