address.h

Go to the documentation of this file.
00001 #ifndef NETWORK_ADDRESS_CLASS
00002 #define NETWORK_ADDRESS_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : network_address                                                   *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *  Purpose:                                                                   *
00010 *                                                                             *
00011 *    Provides a way to describe the destination for messages that will        *
00012 *  be sent through the different kinds of transport mechanisms; a             *
00013 *  network_address represents a location on some communications medium.       *
00014 *                                                                             *
00015 *******************************************************************************
00016 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00017 * redistribute it and/or modify it under the terms of the GNU General Public  *
00018 * License as published by the Free Software Foundation; either version 2 of   *
00019 * the License or (at your option) any later version.  This is online at:      *
00020 *     http://www.fsf.org/copyleft/gpl.html                                    *
00021 * Please send any updates to: fred@gruntose.com                               *
00022 \*****************************************************************************/
00023 
00024 #include "address_base.h"
00025 
00026 // forward.
00027 class configurator;
00028 class machine_uid;
00029 
00030 // The ideal goal here is to not include any network specific header files:
00031 // The address should be stated in terms of the simpler characteristics of the
00032 // underlying kind of transport address and should include all relevant
00033 // constants.  Only the actual derived classes should include details
00034 // from the communications-medium dependent interfaces.
00035 
00037 
00038 class SOCKETS_CLASS_STYLE internet_address : public address_base
00039 {
00040 public:
00041   enum internet_address_constraints {
00042     ADDRESS_SIZE = 4,
00043     MAXIMUM_HOSTNAME_LENGTH = 128
00044   };
00045 
00046   typedef byte address_array[ADDRESS_SIZE];
00047   address_array ip_address;
00048   int port;
00049 
00050   char hostname[MAXIMUM_HOSTNAME_LENGTH];
00051     // can be resolved to an ip_address if a domain name server
00052     // is available.
00053 
00054   internet_address();
00055   internet_address(const byte_array &ip_address, const istring &host,
00056           int port);
00057 
00058   IMPLEMENT_CLASS_NAME("internet_address");
00059 
00060   machine_uid convert() const;
00061     // returns the address in the uniquifying format.
00062 
00063   void fill(const byte_array &ip_address, const istring &host, int port);
00064 
00065   bool same_host(const address_base &to_compare) const;
00066   bool same_port(const address_base &to_compare) const;
00067   bool shareable(const address_base &to_compare) const;
00068 
00069   inline bool operator == (const internet_address &to_compare) const {
00070     return same_host(to_compare) && same_port(to_compare);
00071   }
00072 
00073   istring text_form() const;
00074 
00075   istring tokenize() const;
00076   bool detokenize(const istring &info);
00077 
00078   istring normalize_host() const;
00079     // returns a normal form for the hostname or address.  this will come from
00080     // the hostname member first, if it's set.  next it will come from the
00081     // string form of the IP address.
00082 
00083   static const byte_array &nil_address();
00084     // returns the address that is all zeros, otherwise known as INADDR_ANY.
00085 
00086   bool is_nil_address() const;
00087     // returns true if this object's address_array is all zeros.
00088 
00089   static bool is_nil_address(const address_array &ip_address);
00090     // returns true if the array "ip_address" is all zero.
00091 
00092   static bool valid_address(const istring &to_check);
00093     // returns true if the address "to_check" seems well-formed for IP.  note
00094     // that this will accept 0.0.0.0 as valid.
00095 
00096   static bool is_valid_internet_address(const istring &to_check,
00097           byte_array &ip_form, bool &all_zeros);
00098     // this function checks the string "to_check" to see if it is a valid
00099     // internet address (e.g., 143.203.39.222).  if it is a valid address,
00100     // then the address "ip_form" is filled in with four bytes that are each
00101     // in the range (0..255).  if the "ip_form" is 0.0.0.0, then all_zeros
00102     // is set to true.
00103 
00104   static bool ip_appropriate_number(const istring &to_check, int indy,
00105           istring &accum);
00107 
00116   static bool has_ip_address(const istring &to_check, istring &ip_found);
00118 
00121   static istring ip_address_text_form(const byte_array &ip_address);
00122     // returns a string containing the textual form of the "ip_address".  the
00123     // "ip_address" is expected to have the proper length (four bytes).  if it
00124     // does not, then an empty string is returned.
00125 
00126   static const byte_array &localhost();
00127     // provides the array that indicates localhost, rather than a NIC.
00128     // this is commonly known to be 127.0.0.1.
00129 
00130   bool is_localhost() const;
00131     // returns true if the address in question is the same as the localhost,
00132     // either through the name matching "local" or "localhost", or through
00133     // the address matching 127.0.0.1.  note that the word "local" here is
00134     // an "enhancement" and would not normally be considered the same as
00135     // localhost.  beware a networked computer that's actually named "local".
00136     // also note that an address where the name disagrees with the address is
00137     // basically broken, but we are not checking that here; either condition
00138     // of the hostname or the address matching causes this to return true.
00139 
00140   address_base *create_copy() const;
00141 
00142   void pack(byte_array &packed_form) const;
00143   bool unpack(byte_array &packed_form);
00144 
00145   virtual int packed_size() const;
00146 };
00147 
00148 #ifndef EMBEDDED_BUILD
00149 
00151 
00152 class SOCKETS_CLASS_STYLE serial_port_address : public address_base
00153 {
00154 public:
00155   enum parity_type
00156       { NONE = 'N', EVEN = 'E', ODD = 'O', MARK = 'M', SPACE = 'S' };
00157 
00158   enum stop_bits_type {
00159     ZERO_SB = 0,
00160     ONE_SB = 1,
00161     ONE_POINT_FIVE_SB = 9,
00162     TWO_SB = 2
00163   };
00164 
00165   int port;   // range from 1 .. n for COM1 .. COMn.
00166   int baud_rate;
00167   byte data_bits;
00168   char parity;  // use the parity_type above.
00169   byte stop_bits;  // use the stop_bits_type above.
00170 
00171   serial_port_address();
00172   serial_port_address(int port, int baud_rate, byte data_bits, char parity,
00173       byte stop_bits);
00174 
00175   void fill(int port, int baud_rate, byte data_bits, char parity,
00176       byte stop_bits);
00177 
00178   IMPLEMENT_CLASS_NAME("serial_port_address");
00179 
00180   bool same_host(const address_base &to_compare) const;
00181   bool same_port(const address_base &to_compare) const;
00182   bool shareable(const address_base &to_compare) const;
00183 
00184   istring text_form() const;
00185 
00186   istring tokenize() const;
00187   bool detokenize(const istring &info);
00188 
00189   address_base *create_copy() const;
00190 
00191   void pack(byte_array &packed_form) const;
00192   bool unpack(byte_array &packed_form);
00193 
00194   virtual machine_uid convert() const;
00195 
00196   virtual int packed_size() const;
00197 
00198   // commands understood by serial_transports.  note that "now" is relative to
00199   // when the command is processed.  if the transport is in streaming mode,
00200   // then the command will hit after any messages that were scheduled before
00201   // the command.
00202   static istring GET_DSR();  // the DSR state as of "now" is returned.
00203   static istring GET_CTS();  // the CTS state ....
00204   static istring SET_DTR();  // sets the DTR state to value found in command.
00205   static istring SET_RTS();  // sets the RTS state ....
00206   static istring USE_MARK_SPACE();
00207     // invokes the special mark-space parity protocol for the message to be
00208     // transferred after this one; the first byte is transmitted with mark
00209     // parity and the rest are transmitted with space parity.
00210 };
00211 
00213 
00214 class SOCKETS_CLASS_STYLE ipc_address : public address_base
00215 {
00216 public:
00217   enum ipc_address_constraints {
00218     MAXIMUM_IPC_NAME_LENGTH = 42
00219   };
00220 
00221   char service[MAXIMUM_IPC_NAME_LENGTH];
00222   char topic[MAXIMUM_IPC_NAME_LENGTH];
00223 
00224   ipc_address();
00225   ipc_address(const istring &service, const istring &topic);
00226 
00227   void fill(const istring &service, const istring &topic);
00228 
00229   bool same_host(const address_base &to_compare) const;
00230   bool same_port(const address_base &to_compare) const;
00231   bool shareable(const address_base &to_compare) const;
00232 
00233   istring text_form() const;
00234 
00235   IMPLEMENT_CLASS_NAME("ipc_address");
00236 
00237   istring tokenize() const;
00238   bool detokenize(const istring &info);
00239 
00240   virtual machine_uid convert() const;
00241 
00242   address_base *create_copy() const;
00243 
00244   void pack(byte_array &packed_form) const;
00245   bool unpack(byte_array &packed_form);
00246 
00247   virtual int packed_size() const;
00248 };
00249 
00250 #endif // embedded
00251 
00253 
00254 // the unifying class: network_address pulls together the other types..
00255 
00256 class SOCKETS_CLASS_STYLE network_address : public packable
00257 {
00258 public:
00259   network_address();
00260   network_address(const network_address &to_copy);
00261 
00262   network_address(const internet_address &address);
00263 #ifndef EMBEDDED_BUILD
00264   network_address(const serial_port_address &address);
00265   network_address(const ipc_address &address);
00266 #endif
00267 
00268   ~network_address();
00269 
00270   IMPLEMENT_CLASS_NAME("network_address");
00271 
00272   network_address &operator =(const network_address &to_copy);
00273 
00274   // the address_type represents the networks and devices over which
00275   // communication is possible.
00276   enum address_type {
00277     ADDRESS_SENTINEL_BEGIN = 0,
00278     UNINITIALIZED_ADDRESS = 0,
00279     INVALID_ADDRESS = UNINITIALIZED_ADDRESS,
00280     IPC_ADDRESS,
00281     INTERNET_ADDRESS,
00282     SERIAL_PORT_ADDRESS,
00283     ADDRESS_SENTINEL_END
00284   };
00285 
00286   bool valid() const;
00287     // returns true if the type for this address is not UNINITIALIZED_ADDRESS.
00288 
00289   inline const char *type_name() const {
00290     return (_type == UNINITIALIZED_ADDRESS)? "UninitializedAddress"
00291         : _addr->class_name();
00292   }
00293 
00294   address_type type() const { return address_type(_type); }
00295   static istring text_for_type(address_type type);
00296   static address_type type_from_text(const istring &text);
00297 
00298   // the following accessor functions will return NIL if the type of
00299   // address requested doesn't match the type held internally.  these are
00300   // pointers to the internal contents and MUST NOT be destroyed.
00301   internet_address *internet();
00302   const internet_address *internet() const;
00303 #ifndef EMBEDDED_BUILD
00304   serial_port_address *serial();
00305   const serial_port_address *serial() const;
00306   ipc_address *ipc();
00307   const ipc_address *ipc() const;
00308 #endif
00309 
00310   bool comparable(address_type a, address_type b) const;
00311     // returns true if it is meaningful to compare two addresses.  for example,
00312     // it is meaningful to compare serial with modem, but not ipc or tcpip.
00313   bool same_host(const network_address &to_compare) const;
00314     // returns true if the hosts of this and "to_compare" are identical within
00315     // the networking scheme.
00316   bool same_port(const network_address &to_compare) const;
00317     // returns true if the posts of this and "to_compare" are identical within
00318     // the networking scheme.
00319   bool operator == (const network_address &to_compare) const;
00320     // returns true if the address "to_compare" is effectively identical to
00321     // this (that is, if it's host and port are the same).
00322   bool operator != (const network_address &to_compare) const
00323         { return !(*this == to_compare); }
00324   bool shareable(const network_address &to_compare) const;
00325     // returns true if the two transports can be shared.  this is false if the
00326     // two addresses are not == to each other.  it also fails if for some
00327     // reason the two addresses are not shareable due to communications medium
00328     // constraints.
00329 
00330   istring text_form(bool show_type = true) const;
00331     // returns a printed form of the address.  if "show_type" is true, then the
00332     // address_type is included.  otherwise, just the low level address is
00333     // printed.
00334 
00335   istring tokenize() const;
00336   bool detokenize(const istring &info);
00337     // converts the address to and from a specific text format.  if tokenize
00338     // fails, an empty string is returned.  if detokenize fails, false is
00339     // returned.
00340 
00341   static network_address load(configurator &config, const istring &section,
00342         const istring &name, const network_address &default_address);
00343     // retrieves the network address from "config" that is stored under the
00344     // "section" for the "name" specified.  the returned address will be
00345     // the same as "default_address" if no entry was found, and the "config"
00346     // will be updated to that default.  if a badly formed address is found
00347     // under the "name", then the return value's type() is uninitialized.
00348   static bool store(configurator &config, const istring &section,
00349         const istring &name, const network_address &to_store);
00350     // writes a network_address "to_store" to the "config" under the
00351     // "section" with the specified "name".
00352 
00353   // expensive packing using strings.
00354   void parse_pack(byte_array &packed_form) const;
00355   bool parse_unpack(byte_array &packed_form);
00356 
00357   machine_uid convert() const;
00358     // convert to our lower-level unique machine identifier.
00359 
00360   istring unified_hostname() const;
00361     // returns a string that represents the host's name uniquely.  if the
00362     // member is not set, the address is substituted.
00363 
00364   virtual int packed_size() const;
00365 
00366   // cheap packing by the ints and bytes.
00367   void pack(byte_array &packed_form) const;
00368   bool unpack(byte_array &packed_form);
00369 
00370 private:
00371   int _type;  // what kind of address this is.
00372   address_base *_addr;  // our real data for the address.
00373 };
00374 
00375 #endif
00376 

Generated on Thu Nov 20 04:28:48 2008 for HOOPLE Libraries by  doxygen 1.5.1