//=========================================================================== // @(#) $Name: $ // @(#) $Id: ArtsPrimitive.hh,v 1.1.1.1 2000/03/29 22:53:56 kkeys Exp $ //=========================================================================== // CAIDA Copyright Notice // // By accessing this software, arts++, you are duly informed // of and agree to be bound by the conditions described below in this // notice: // // This software product, arts++, is developed by Daniel W. McRobb, and // copyrighted(C) 1998 by the University of California, San Diego // (UCSD), with all rights reserved. UCSD administers the CAIDA grant, // NCR-9711092, under which part of this code was developed. // // There is no charge for arts++ software. You can redistribute it // and/or modify it under the terms of the GNU General Public License, // v. 2 dated June 1991 which is incorporated by reference herein. // arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use // of it will not infringe on any third party's intellectual property // rights. // // You should have received a copy of the GNU GPL along with arts++. // Copies can also be obtained from: // // http://www.gnu.org/copyleft/gpl.html // // or by writing to: // // University of California, San Diego // // SDSC/CAIDA // 9500 Gilman Dr., MS-0505 // La Jolla, CA 92093 - 0505 USA // // Or contact: // // info@caida.org //=========================================================================== #ifndef _ARTSPRIMITIVE_HH_ #define _ARTSPRIMITIVE_HH_ #include extern "C" { #include "caida_t.h" } //--------------------------------------------------------------------------- // class ArtsPrimitive //--------------------------------------------------------------------------- // This class provides read/write routines for multi-byte integer data, // floats and doubles. It also provides generic robust read/write // routines for sockets (FdWrite() and FdRead()). All of the routines // except FdWrite() and FdRead() assume that contents of an iostream or // file descriptor are always in network byte order. Hence the Write* // member functions for integer types convert to network byte order // from host byte order before outputting to an ostream or file // descriptor and the Read* member functions for integer types convert // from network byte order to host byte order after reading from an // istream or file descriptor. // // The routines for floats and doubles use XDR under the hood. Hence // the on-the-wire (or disk) format for these types is always 4 bytes // for floats, 8 bytes for doubles (in IEEE format, see RFC 1832 and/or // ANSI/IEEE Standard 754-1985). For the multi-byte integer types, the // routines will handle run length encoded (RLE) values, but the caller // is responsible for passing in the correct length (and // reading/writing the length itself). //--------------------------------------------------------------------------- class ArtsPrimitive { public: //------------------------------------------------------------------------- // int FdWrite(int fd, const void *ptr, int numBytes) const //......................................................................... // Writes numBytes of the data pointed to by ptr to the file // descriptor fd. This is really just a wrapper around the UNIX // write() system call, but we try to write all of numBytes until // an error occurs (instead of just returning a short byte count). // This is mostly useful when dealing with sockets (where fd is // a socket file descriptor), but is used internally as well (to // unify socket and file handling). //------------------------------------------------------------------------- int FdWrite(int fd, const void *ptr, int numBytes) const; //------------------------------------------------------------------------- // int FdRead(int fd, void *ptr, int numBytes) const //......................................................................... // Reads numBytes from fd into ptr. This is really just a wrapper // around the UNIX read() system call, but we try to read all of // numBytes until an error occurs (instead of just returning a short // byte count). This is mostly useful when dealing with sockets // (where fd is a socket file descriptor), but is used internally // as well (to unify socket and file handling). //------------------------------------------------------------------------- int FdRead(int fd, void *ptr, int numBytes) const; //------------------------------------------------------------------------ // WriteUint16(ostream & os, const uint16_t & value, uint8_t len) const //........................................................................ // Writes len (1 or 2) bytes of an uint16_t value to an ostream in // network byte order. The caller's passed-in value is assumed to be // in host byte order. The len field is useful for cases where an // uint16_t value can be stored in a single byte and both reader and // writer know this ahead of time. Legal len values are 1 and 2. // Returns the ostream. //------------------------------------------------------------------------ ostream & WriteUint16(ostream & os, const uint16_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int WriteUint16(int fd, const uint16_t & value, uint8_t len) const //......................................................................... // Writes len (1 or 2) bytes of an uint16_t value to a file descriptor // in network byte order. The caller's passed-in value is assumed to be // in host byte order. The len field is useful for cases where an // uint16_t value can be stored in a single byte and both reader and // writer know this ahead of time. Legal len values are 1 and 2. // Returns the number of bytes written on success, -1 on failure. //------------------------------------------------------------------------- int WriteUint16(int fd, const uint16_t & value, uint8_t len) const; //------------------------------------------------------------------------ // ReadUint16(istream & is, uint16_t & value, uint8_t len) const //........................................................................ // Reads len (1 or 2) bytes from an ostream and converts the contents // from network byte order to host byte order, storing the result in // value. Returns the istream. //------------------------------------------------------------------------ istream & ReadUint16(istream & is, uint16_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int ReadUint16(int fd, uint16_t & value, uint8_t len) const //......................................................................... // Reads len (1 or 2) bytes from a file descriptor and converts the // contents from network byte order to host byte order, storing the // result in value. Returns the number of bytes read on success, -1 // on failure. //------------------------------------------------------------------------- int ReadUint16(int fd, uint16_t & value, uint8_t len) const; //------------------------------------------------------------------------ // WriteUint32(ostream & os, const uint32_t & value, uint8_t len) const //........................................................................ // Writes len (1, 2 or 4) bytes of an uint32_t to an ostream in network // byte order. The caller's passed-in value is assumed to be in host // byte order. The len field is useful for cases where an uint32_t // value can be stored in less than 4 bytes and both reader and writer // know this ahead of time. Returns the ostream. //------------------------------------------------------------------------ ostream & WriteUint32(ostream & os, const uint32_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int WriteUint32(int fd, const uint32_t & value, uint8_t len) const //......................................................................... // Writes len (1, 2 or 4) bytes of an uint32_t to a file descriptor // in network byte order. The caller's passed-in value is assumed to // be in host byte order. The len field is useful for cases where an // uint32_t value can be stored in less than 4 bytes and both reader // and writer know this ahead of time. Returns the number of bytes // written on success, -1 on failure. //------------------------------------------------------------------------- int WriteUint32(int fd, const uint32_t & value, uint8_t len) const; //------------------------------------------------------------------------ // ReadUint32(istream & is, uint32_t & value, uint8_t len) const //........................................................................ // Reads len (1, 2 or 4) bytes from an ostream and converts the contents // from network byte order to host byte order, storing the result in // value. Returns the istream. //------------------------------------------------------------------------ istream & ReadUint32(istream & is, uint32_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int ReadUint32(int fd, uint32_t & value, uint8_t len) const //......................................................................... // Reads len (1, 2 or 4) bytes from a file descriptor and converts // the contents from network byte order to host byte order, storing // the result in value. Returns the number of bytes read on success, // -1 on failure. //------------------------------------------------------------------------- int ReadUint32(int fd, uint32_t & value, uint8_t len) const; //------------------------------------------------------------------------ // WriteUint64(ostream & os, const uint64_t & value, uint8_t len) const //........................................................................ // Writes len (1, 2, 4 or 8) bytes of an uint32_t to an ostream in // network byte order. The caller's passed-in value is assumed to be // in host byte order. The len field is useful for cases where an // uint64_t value can be stored in less than 8 bytes and both reader // and writer know this ahead of time. Returns the ostream. //------------------------------------------------------------------------ ostream & WriteUint64(ostream & os, const uint64_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int WriteUint64(int fd, const uint64_t & value, uint8_t len) const //......................................................................... // Writes len (1, 2, 4 or 8) bytes of an uint32_t to a file // descriptor in network byte order. The caller's passed-in value is // assumed to be in host byte order. The len field is useful for // cases where an uint64_t value can be stored in less than 8 bytes // and both reader and writer know this ahead of time. Returns the // number of bytes written on success, -1 on failure. //------------------------------------------------------------------------- int WriteUint64(int fd, const uint64_t & value, uint8_t len) const; //------------------------------------------------------------------------ // ReadUint64(istream & is, uint64_t & value, uint8_t len) const //........................................................................ // Reads len (1, 2, 4 or 8) bytes from an ostream and converts the // contents from network byte order to host byte order, storing the // result in value. Returns the istream. //------------------------------------------------------------------------ istream & ReadUint64(istream & is, uint64_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int ReadUint64(int fd, uint64_t & value, uint8_t len) const //......................................................................... // Reads len (1, 2, 4 or 8) bytes from a file descriptor and converts // the contents from network byte order to host byte order, storing // the result in value. Returns the number of bytes read on success, // -1 on failure. //------------------------------------------------------------------------- int ReadUint64(int fd, uint64_t & value, uint8_t len) const; //------------------------------------------------------------------------- // istream & ReadIpv4Network(istream & is, ipv4addr_t & value, // uint8_t len) const //......................................................................... // Reads len (1, 2, 3 or 4) bytes from an istream and stores the // results in value. This function permits reading network addresses // in shortened form; a class B network, for example, can be stored // in only 2 bytes on disk. //------------------------------------------------------------------------- istream & ReadIpv4Network(istream & is, ipv4addr_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int ReadIpv4Network(int fd, ipv4addr_t & value, uint8_t len) const //......................................................................... // Reads len (1, 2, 3 or 4) bytes from a file descriptor and stores the // results in value. This function permits reading network addresses // in shortened form; a class B network, for example, can be stored in // only 2 bytes on disk. //------------------------------------------------------------------------- int ReadIpv4Network(int fd, ipv4addr_t & value, uint8_t len) const; //------------------------------------------------------------------------- // ostream & WriteIpv4Network(ostream & os, const ipv4addr_t & value, // uint8_t len) const //......................................................................... // Writes len bytes of value to an ostream. When using this function, // the idea is that you're storing a byte-counted IP address, which // means you can store a network address with the minimum required // space (a class B network in 2 bytes, for example). Returns the // number of bytes written. Always writes in network byte order. //------------------------------------------------------------------------- ostream & WriteIpv4Network(ostream & os, const ipv4addr_t & value, uint8_t len) const; //------------------------------------------------------------------------- // int WriteIpv4Network(int fd, const ipv4addr_t & value, // uint8_t len) const //......................................................................... // Writes len bytes of value to a file descriptor. When using this // function, the idea is that you're storing a byte-counted IP address, // which means you can store a network address with the minimum // required space (a class B network in 2 bytes, for example). Returns // the number of bytes written. Always writes in network byte order. //------------------------------------------------------------------------- int WriteIpv4Network(int fd, const ipv4addr_t & value, uint8_t len) const; //------------------------------------------------------------------------- // istream & ReadFloat(istream & is, float & value) const //......................................................................... // UNTESTED // Reads a float from an istream, using XDR. Returns the istream. //------------------------------------------------------------------------- istream & ReadFloat(istream & is, float & value) const; //------------------------------------------------------------------------- // int ReadFloat(int fd, float & value) const //......................................................................... // UNTESTED // Reads a float from a file descriptor, using XDR. Returns the // number of bytes read on success (should be 4), -1 on error. //------------------------------------------------------------------------- int ReadFloat(int fd, float & value) const; //------------------------------------------------------------------------- // ostream & WriteFloat(ostream & os, float value) const //......................................................................... // UNTESTED // Writes a float to an ostream, using XDR. Returns the ostream. //------------------------------------------------------------------------- ostream & WriteFloat(ostream & os, float value) const; //------------------------------------------------------------------------- // int WriteFloat(int fd, float value) const //......................................................................... // UNTESTED // Writes a float to a file descriptor, using XDR. Returns the // number of bytes written on success (should be 4), -1 on error. //------------------------------------------------------------------------- int WriteFloat(int fd, float value) const; //------------------------------------------------------------------------- // istream & ReadDouble(istream & is, double & value) const //......................................................................... // UNTESTED // Reads a souble from an istream, using XDR. Returns the istream. //------------------------------------------------------------------------- istream & ReadDouble(istream & is, double & value) const; //------------------------------------------------------------------------- // int ReadDouble(int fd, double & value) const; //......................................................................... // UNTESTED // Reads a double from a file descriptor, using XDR. Returns the // number of bytes read on success (should be 8), -1 on failure. //------------------------------------------------------------------------- int ReadDouble(int fd, double & value) const; //------------------------------------------------------------------------- // ostream & WriteDouble(ostream & os, double value) const //......................................................................... // UNTESTED // Writes a double to an ostream, using XDR. Returns the ostream. //------------------------------------------------------------------------- ostream & WriteDouble(ostream & os, double value) const; //------------------------------------------------------------------------- // int WriteDouble(int fd, double value) const //......................................................................... // UNTESTED // Writes a double to a file descriptor, using XDR. Returns the // number of bytes written on success (should be 8), -1 on error. //------------------------------------------------------------------------- int WriteDouble(int fd, double value) const; }; #endif /* _ARTSPRIMITIVE_HH_ */