AgentXcpp  Revision:0.1.1
Internals Documentation
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Pages
varbind.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2012 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
3  *
4  * This file is part of the agentXcpp library.
5  *
6  * AgentXcpp is free software: you can redistribute it and/or modify
7  * it under the terms of the AgentXcpp library license, version 1, which
8  * consists of the GNU General Public License and some additional
9  * permissions.
10  *
11  * AgentXcpp is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See the AgentXcpp library license in the LICENSE file of this package
17  * for more details.
18  */
19 #include <boost/cstdint.hpp>
20 
21 #include "varbind.hpp"
22 #include "Octet_String.hpp"
23 #include "Integer.hpp"
24 #include "Counter32.hpp"
25 #include "Counter64.hpp"
26 #include "Gauge32.hpp"
27 #include "TimeTicks.hpp"
28 #include "Opaque.hpp"
29 #include "IpAddress.hpp"
30 #include "util.hpp"
31 
32 using namespace agentxcpp;
33 using boost::uint16_t;
34 
35 
37 {
38  binary serialized;
39 
40  // encode type
41  serialized.push_back( type << 8 & 0xff );
42  serialized.push_back( type << 0 & 0xff );
43 
44  // reserved field
45  serialized.push_back( 0 ); // reserved
46  serialized.push_back( 0 ); // reserved
47 
48  // encode name
49  serialized += name.serialize();
50 
51  // encode data if needed
52  if (var.get()) serialized += var->serialize();
53 
54  return serialized;
55 }
56 
57 
58 varbind::varbind(const oid& o, boost::shared_ptr<variable> v)
59 {
60  name = o;
61  var = v;
62 
63  // Determine type of variable and fill type field.
64  if( dynamic_cast<Integer*>(var.get()) ) type = 2;
65  else if( dynamic_cast<Octet_String*>(var.get()) ) type = 4;
66  else if( dynamic_cast<oid*>(var.get()) ) type = 6;
67  else if( dynamic_cast<IpAddress*>(var.get()) ) type = 64;
68  else if( dynamic_cast<Counter32*>(var.get()) ) type = 65;
69  else if( dynamic_cast<Gauge32*>(var.get()) ) type = 66;
70  else if( dynamic_cast<TimeTicks*>(var.get()) ) type = 67;
71  else if( dynamic_cast<Opaque*>(var.get()) ) type = 68;
72  else if( dynamic_cast<Counter64*>(var.get()) ) type = 70;
73  else
74  {
75  // Type could not be determined -> invalid parameter.
76  throw inval_param();
77  }
78 }
79 
80 
82 {
83  name = o;
84 
85  // Check t
86  switch(t)
87  {
88  case noSuchObject:
89  case noSuchInstance:
90  case Null:
91  case endOfMibView:
92  // OK, store value
93  type = t;
94  break;
95  default:
96  // invalid type: Throw exception
97  throw inval_param();
98  }
99 }
100 
101 varbind::varbind(binary::const_iterator& pos,
102  const binary::const_iterator& end,
103  bool big_endian)
104 {
105  uint16_t type;
106 
107  // Type and reserved field
108  if(end - pos < 4)
109  {
110  throw(parse_error());
111  }
112 
113  // Get type
114  type = read16(pos, big_endian);
115 
116  // skip reserved field
117  pos += 2;
118 
119  // read OID: no exceptions are catched; they are forwarded to the caller
120  name = oid(pos, end, big_endian);
121 
122  // Get data: no exceptions are catched; they are forwarded to the caller
123  switch(type)
124  {
125  case 2:
126  var.reset(new Integer(pos, end, big_endian));
127  break;
128  case 4:
129  var.reset(new Octet_String(pos, end, big_endian));
130  break;
131  case 6:
132  var.reset(new oid(pos, end, big_endian));
133  break;
134  case 64:
135  var.reset(new IpAddress(pos, end, big_endian));
136  break;
137  case 65:
138  var.reset(new Counter32(pos, end, big_endian));
139  break;
140  case 66:
141  var.reset(new Gauge32(pos, end, big_endian));
142  break;
143  case 67:
144  var.reset(new TimeTicks(pos, end, big_endian));
145  break;
146  case 68:
147  var.reset(new Opaque(pos, end, big_endian));
148  break;
149  case 70:
150  var.reset(new Counter64(pos, end, big_endian));
151  break;
152  case 5: // Null
153  case 128: // noSuchObject
154  case 129: // noSuchInstance
155  case 130: // endOfMibView
156  var.reset();
157  break;
158  default:
159  // invalid type
160  throw(parse_error());
161  }
162 }