machine_uid.h

Go to the documentation of this file.
00001 #ifndef MACHINE_UID_CLASS
00002 #define MACHINE_UID_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : machine_uid                                                       *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *  Purpose:                                                                   *
00010 *                                                                             *
00011 *    This object identifies a machine uniquely within a particular network    *
00012 *  configuration.  It is not world-unique, since some location names are not  *
00013 *  either.  For example, a TCP/IP address like 10.2.13.9 might be used many   *
00014 *  times around the world, since it is reserved for private networks.  But    *
00015 *  within one valid network configuration, there should not be more than one  *
00016 *  of these addresses.  If there are, then be aware that multiple machines    *
00017 *  might answer to a particular address and invalidate some assumptions of    *
00018 *  uniqueness.                                                                *
00019 *    The machine_uid is most useful when a program wishes to ensure that it   *
00020 *  treats a machine only once when it is performing some form of processing   *
00021 *  for that address.  For example, the id could be contained within a 'set'   *
00022 *  to ensure that a message is only sent once to each machine of interest.    *
00023 *                                                                             *
00024 *******************************************************************************
00025 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00026 * redistribute it and/or modify it under the terms of the GNU General Public  *
00027 * License as published by the Free Software Foundation; either version 2 of   *
00028 * the License or (at your option) any later version.  This is online at:      *
00029 *     http://www.fsf.org/copyleft/gpl.html                                    *
00030 * Please send any updates to: fred@gruntose.com                               *
00031 \*****************************************************************************/
00032 
00033 #include "sockets_dll.h"
00034 
00035 #include <basis/object_base.h>
00036 #include <basis/packable.h>
00037 
00038 // forward.
00039 class internal_machine_uid_array;
00040 
00041 class SOCKETS_CLASS_STYLE machine_uid : public packable
00042 {
00043 public:
00044   enum known_location_types {
00045     INVALID_LOCATION,            // this id has not been initialized.
00046     TCPIP_LOCATION,              // a location on the internet.
00047     IPX_LOCATION,                // a host on an IPX/SPX network.
00048     NETBIOS_LOCATION             // a machine reachable by SMB protocol.
00049   };
00050   static const istring &type_name(known_location_types type);
00051     // returns the text form for the "type" specified.
00052 
00053   machine_uid();  // constructs an invalid id.
00054 
00055   machine_uid(known_location_types type, const byte_array &address);
00056     // sets up an id for the "type" specified given the serialized "address".
00057     // the format used here is that bytes follow in the natural listing order
00058     // for the address "type".
00059 
00060   machine_uid(const machine_uid &to_copy);  // copy constructor.
00061 
00062   virtual ~machine_uid();
00063 
00064   known_location_types type() const;
00065     // returns the type currently held.
00066 
00067   bool valid() const { return type() != INVALID_LOCATION; }
00068     // returns true if this id seems possibly valid.  an id for which this
00069     // returns true could still have protocol specific issues that make it
00070     // invalid, but as far as this class can tell, it looks okay.
00071 
00072   machine_uid &operator =(const machine_uid &to_copy);
00073 
00074   void reset(known_location_types type, const byte_array &address);
00075     // reconstructs the machine_uid with new parameters.
00076 
00077   istring text_form() const;
00078     // returns a string that describes the address held here.
00079 
00080   istring compact_form() const;
00081     // returns a non-readable string form of the id that is more efficient.
00082   static machine_uid expand(const istring &compacted);
00083     // gets the original machine_uid out of the "compacted" form.
00084 
00085   byte_array native() const;
00086     // returns the identifier for the machine in the native format.  for
00087     // internet1, this is 4 bytes of IP address.
00088 
00089   // equality means: same protocol type, same key length, and same contents.
00090   bool operator == (const machine_uid &to_compare) const;
00091   bool operator != (const machine_uid &to_compare) const
00092          { return !(*this == to_compare); }
00093 
00094   // meets the requirements of packable.
00095   virtual void pack(byte_array &packed_form) const;
00096   virtual bool unpack(byte_array &packed_form);
00097 
00098   const byte_array &raw() const;
00099     // returns the raw internal representation for the machine_uid.
00100 
00101 private:
00102   byte_array *_contents;
00103 };
00104 
00106 
00107 class internet_machine_uid : public machine_uid
00108 {
00109 public:
00110   internet_machine_uid(const istring &hostname, const byte_array &ip_address);
00111     // constructs a machine uid that almost always uniquely identifies a
00112     // machine on the internet.  if all ip addresses in a system are unique,
00113     // then IMU is always unique.  however, if all ip addresses in a system
00114     // are not unique, then the IMU can discriminate the hostname as long as
00115     // no two hostnames resolve to the same checksum.
00116 };
00117 
00119 
00120 // this object contains a list of unique machine identifiers.  this is
00121 // intentionally just an array (and not a set) because some usages of the
00122 // list of machine_uids need a notion of ordering (such as for a list of
00123 // machines that have touched a packet).
00124 
00125 class SOCKETS_CLASS_STYLE machine_uid_array : public virtual object_base
00126 {
00127 public:
00128   machine_uid_array();
00129   machine_uid_array(const machine_uid_array &to_copy);
00130   ~machine_uid_array();
00131   IMPLEMENT_CLASS_NAME("machine_uid_array");
00132 
00133   static const machine_uid_array &blank_array();
00134 
00135   machine_uid_array &operator =(const machine_uid_array &to_copy);
00136 
00137   bool operator += (const machine_uid &to_add);
00138 
00139   istring text_form() const;
00140 
00141   int elements() const;
00142 
00143   void reset();
00144 
00145   machine_uid &operator [] (int index);
00146   const machine_uid &operator [] (int index) const;
00147 
00148   bool member(const machine_uid &to_test) const;
00149     // returns true if the id "to_test" is a member of the list of machine
00150     // ids held here.  if the machine_uid's are of different sizes, then a
00151     // prefix compare is used.  this allows an id without the host
00152     // discriminator portion to match the full version.
00153 
00154 private:
00155   internal_machine_uid_array *_uids;  // the underlying machine_uid list.
00156 };
00157 
00158 #endif
00159 

Generated on Fri Nov 28 04:28:56 2008 for HOOPLE Libraries by  doxygen 1.5.1