xorp

tlv.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/tlv.hh,v 1.9 2008/10/02 21:57:36 bms Exp $
00023 
00024 #ifndef __LIBXORP_TLV_HH__
00025 #define __LIBXORP_TLV_HH__
00026 
00031 class Tlv {
00032  public:
00033     Tlv() : _fp(0)
00034     {}
00035 
00045     bool open(string& fname, bool read) {
00046     if ("-" == fname) {
00047         _fp = read ? stdin : stdout;
00048     } else {
00049         _fp = fopen(fname.c_str(), read ? "r" : "w");
00050     }
00051     if (0 == _fp)
00052         return false;
00053 
00054     return true;
00055     }
00056 
00065     bool read(uint32_t& type, vector<uint8_t>& data) {
00066     uint32_t n;
00067     if (1 != fread(&n, sizeof(n), 1, _fp))
00068         return false;
00069     
00070     type = ntohl(n);
00071 
00072     if (1 != fread(&n, sizeof(n), 1, _fp))
00073         return false;
00074 
00075     uint32_t len = ntohl(n);
00076 
00077     data.resize(len);
00078     if (len != fread(&data[0], 1, len, _fp))
00079         return false;
00080 
00081     return true;
00082     }
00083 
00092     bool write(uint32_t type, vector<uint8_t>& data) {
00093     uint32_t n = htonl(type);
00094     if (1 != fwrite(&n, sizeof(n), 1, _fp))
00095         return false;
00096     
00097     uint32_t len = data.size();
00098     n = htonl(len);
00099 
00100     if (1 != fwrite(&n, sizeof(n), 1, _fp))
00101         return false;
00102 
00103     if (len != fwrite(&data[0], 1, len, _fp))
00104         return false;
00105 
00106     return true;
00107     }
00108 
00114     bool close() {
00115     if (0 == _fp) {
00116         return false;
00117     }
00118     return 0 == fclose(_fp) ? true : false;
00119     }
00120 
00121     /*
00122      * Get a 32 bit unsigned int from the data array.
00123      *
00124      * @param data array containing the value.
00125      * @param offset in data to start reading.
00126      * @param u32 value read from array.
00127      *
00128      * @return true on success
00129      */
00130     bool get32(vector<uint8_t>& data, uint32_t offset, uint32_t& u32) {
00131 
00132     if (data.size() < offset + sizeof(u32))
00133         return false;
00134 
00135     u32 = data[offset + 0];
00136     u32 <<= 8;
00137     u32 |= data[offset + 1];
00138     u32 <<= 8;
00139     u32 |= data[offset + 2];
00140     u32 <<= 8;
00141     u32 |= data[offset + 3];
00142     
00143     return true;
00144     }
00145 
00146     /*
00147      * Put a 32 bit unsigned int into the data array.
00148      *
00149      * @param data array into which to place the value.
00150      * @param offset in data to start writing.
00151      * @param u32 value to put into the array.
00152      *
00153      * @return true on success
00154      */
00155     bool put32(vector<uint8_t>& data, uint32_t offset, uint32_t u32) {
00156 
00157     if (data.size() < offset + sizeof(u32))
00158         return false;
00159 
00160     data[offset + 0] = (u32 >> 24) & 0xff;
00161     data[offset + 1] = (u32 >> 16) & 0xff;
00162     data[offset + 2] = (u32 >> 8) & 0xff;
00163     data[offset + 3] = u32 & 0xff;
00164 
00165     return true;
00166     }
00167 
00168  private:
00169     FILE *_fp;
00170 };
00171 
00172 #endif // __LIBXORP_TLV_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations