AgentXcpp  Revision:0.1
Internals Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends
/home/tanjeff/projekte/agentxcpp/src/varbind.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011-2012 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
00003  *
00004  * This file is part of the agentXcpp library.
00005  *
00006  * AgentXcpp is free software: you can redistribute it and/or modify
00007  * it under the terms of the AgentXcpp library license, version 1, which 
00008  * consists of the GNU General Public License and some additional 
00009  * permissions.
00010  *
00011  * AgentXcpp is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * See the AgentXcpp library license in the LICENSE file of this package 
00017  * for more details.
00018  */
00019 #include "varbind.hpp"
00020 #include "Octet_String.hpp"
00021 #include "Integer.hpp"
00022 #include "Counter32.hpp"
00023 #include "Counter64.hpp"
00024 #include "Gauge32.hpp"
00025 #include "TimeTicks.hpp"
00026 #include "Opaque.hpp"
00027 #include "IpAddress.hpp"
00028 #include "helper.hpp"
00029 
00030 using namespace agentxcpp;
00031 
00032 
00033 data_t varbind::serialize() const
00034 {
00035     data_t serialized;
00036 
00037     // encode type
00038     serialized.push_back( type << 8 & 0xff );
00039     serialized.push_back( type << 0 & 0xff );
00040     
00041     // reserved field
00042     serialized.push_back( 0 );  // reserved
00043     serialized.push_back( 0 );  // reserved
00044     
00045     // encode name
00046     serialized += name.serialize();
00047 
00048     // encode data if needed
00049     if (var.get()) serialized += var->serialize();
00050 
00051     return serialized;
00052 }
00053 
00054 
00055 varbind::varbind(const oid& o, boost::shared_ptr<variable> v)
00056 {
00057     name = o;
00058     var = v;
00059 
00060     // Determine type of variable and fill type field.
00061     if( dynamic_cast<Integer*>(var.get()) ) type = 2;
00062     else if( dynamic_cast<Octet_String*>(var.get()) ) type = 4;
00063     else if( dynamic_cast<oid*>(var.get()) ) type = 6;
00064     else if( dynamic_cast<IpAddress*>(var.get()) ) type = 64;
00065     else if( dynamic_cast<Counter32*>(var.get()) ) type = 65;
00066     else if( dynamic_cast<Gauge32*>(var.get()) ) type = 66;
00067     else if( dynamic_cast<TimeTicks*>(var.get()) ) type = 67;
00068     else if( dynamic_cast<Opaque*>(var.get()) ) type = 68;
00069     else if( dynamic_cast<Counter64*>(var.get()) ) type = 70;
00070     else
00071     {
00072         // Type could not be determined -> invalid parameter.
00073         throw inval_param();
00074     }
00075 }
00076 
00077 
00078 varbind::varbind(const oid& o, type_t t)
00079 {
00080     name = o;
00081 
00082     // Check t
00083     switch(t)
00084     {
00085         case noSuchObject:
00086         case noSuchInstance:
00087         case Null:
00088         case endOfMibView:
00089             // OK, store value
00090             type = t;
00091             break;
00092         default:
00093             // invalid type: Throw exception
00094             throw inval_param();
00095     }
00096 }
00097 
00098 varbind::varbind(data_t::const_iterator& pos,
00099                  const data_t::const_iterator& end,
00100                  bool big_endian)
00101 {
00102     uint16_t type;
00103     
00104     // Type and reserved field
00105     if(end - pos < 4)
00106     {
00107         throw(parse_error());
00108     }
00109     
00110     // Get type
00111     type = read16(pos, big_endian);
00112 
00113     // skip reserved field
00114     pos += 2;
00115     
00116     // read OID: no exceptions are catched; they are forwarded to the caller
00117     name = oid(pos, end, big_endian); 
00118 
00119     // Get data: no exceptions are catched; they are forwarded to the caller
00120     switch(type)
00121     {
00122         case 2:
00123             var.reset(new Integer(pos, end, big_endian));
00124             break;
00125         case 4:
00126             var.reset(new Octet_String(pos, end, big_endian));
00127             break;
00128         case 6:
00129             var.reset(new oid(pos, end, big_endian));
00130             break;
00131         case 64:
00132             var.reset(new IpAddress(pos, end, big_endian));
00133             break;
00134         case 65:
00135             var.reset(new Counter32(pos, end, big_endian));
00136             break;
00137         case 66:
00138             var.reset(new Gauge32(pos, end, big_endian));
00139             break;
00140         case 67:
00141             var.reset(new TimeTicks(pos, end, big_endian));
00142             break;
00143         case 68:
00144             var.reset(new Opaque(pos, end, big_endian));
00145             break;
00146         case 70:
00147             var.reset(new Counter64(pos, end, big_endian));
00148             break;
00149         case 5:     // Null
00150         case 128:   // noSuchObject
00151         case 129:   // noSuchInstance
00152         case 130:   // endOfMibView
00153             var.reset();
00154             break;
00155         default:
00156             // invalid type
00157             throw(parse_error());
00158     }
00159 }