summaryrefslogtreecommitdiff
path: root/ASNMP/agent/snmp_agent.cpp
blob: 43a5342a3ecd736eb04904dbe780cd42b14761b6 (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
// $Id$

// This server daemon processes SNMPv1 requests over MIB II System group 

#include "snmp_agent.h"
#include "agent_impl.h"  // actual implementation
#include "ace/Get_Opt.h"
#include "ace/Reactor.h"

ACE_RCSID(agent, snmp_agent, "$Id$")

// module globals 
#define SNMP_AGENT_PORT (161)
#define WR_COM ("private")
#define RD_COM ("public")

snmp_agent::snmp_agent() : agent_(0)
{
  ACE_TRACE("snmp_agent::snmp_agent");
}

snmp_agent::~snmp_agent()
{
  ACE_TRACE("snmp_agent::~snmp_agent");
  delete agent_;
}


int snmp_agent::valid() const
{
  ACE_TRACE("snmp_agent::valid");
  return (agent_ != 0);
}

int snmp_agent::set_args(int argc, char *argv[])
{
  ACE_TRACE("snmp_agent::set_args");
  unsigned short port = SNMP_AGENT_PORT;
  char *rd = RD_COM, *wr = WR_COM;

  ACE_Get_Opt get_opt (argc, argv, "p:w:r:hv");
  for (int c; (c = get_opt ()) != -1; ) {
     switch (c)
       {
       case 'p':
	 port = (ACE_OS::atoi (get_opt.optarg));
	 break;
       case 'w': // write community string
	 wr = get_opt.optarg;
	 break;
       case 'r': // read community string
	 rd = get_opt.optarg;
	 break;
        
       case 'h': // help & version info  
       case 'v': 
        ACE_DEBUG ((LM_DEBUG,
	      "(%P|%t) Example SNMP Version 1 Agent - ASNMP/ACE\n"));
         return -1;
         break;

       default:
	 return -1; // invalid arg
       }
  }

   
   agent_ = new agent_impl(port, rd, wr); // opens OS IO port
   return 0;
}

int snmp_agent::run()
{
  ACE_TRACE("snmp_agent::run");
  ACE_ASSERT(agent_);

  // open service port
#if 0
  if (agent_->open_port()) {
    ACE_DEBUG((LM_DEBUG, "(%P|%t) open port %d failed\n", agent_->get_port()));
    return -1;
  } 
#endif

  // Run forever, processing SNMP requests.

  ACE_DEBUG ((LM_DEBUG,
	      "(%P|%t) starting up snmp agent (server) daemon\n"));

  agent_->process_requests();

  ACE_DEBUG ((LM_DEBUG,
	      "(%P|%t) shutting down snmp agent (server) daemon\n"));
  return 0;
}