internet_address.h

Go to the documentation of this file.
00001 #ifndef INTERNET_ADDRESS_CLASS
00002 #define INTERNET_ADDRESS_CLASS
00003 
00005 // Name   : internet_address
00006 // Author : Chris Koeritz
00008 // Copyright (c) 1995-$now By Author.  This program is free software; you can
00009 // redistribute it and/or modify it under the terms of the GNU General Public
00010 // License as published by the Free Software Foundation:
00011 //     http://www.gnu.org/licenses/gpl.html
00012 // or under the terms of the GNU Library license:
00013 //     http://www.gnu.org/licenses/lgpl.html
00014 // at your preference.  Those licenses describe your legal rights to this
00015 // software, and no other rights or warranties apply.
00016 // Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
00018 
00019 #include "base_address.h"
00020 
00021 #include <configuration/configurator.h>
00022 
00023 namespace sockets {
00024 
00025 // forward.
00026 class machine_uid;
00027 
00029 
00030 class internet_address : public base_address
00031 {
00032 public:
00033   enum internet_address_constraints {
00034     ADDRESS_SIZE = 4,
00035     MAXIMUM_HOSTNAME_LENGTH = 128
00036   };
00037 
00038   typedef basis::abyte address_array[ADDRESS_SIZE];
00039   address_array ip_address;
00040   int port;
00041 
00042   char hostname[MAXIMUM_HOSTNAME_LENGTH];
00043     // can be resolved to an ip_address if a domain name server
00044     // is available.
00045 
00046   internet_address();
00047   internet_address(const basis::byte_array &ip_address, const basis::astring &host,
00048           int port);
00049 
00050   DEFINE_CLASS_NAME("internet_address");
00051 
00052   machine_uid convert() const;
00053     // returns the address in the uniquifying format.
00054 
00055   void fill(const basis::byte_array &ip_address, const basis::astring &host, int port);
00056 
00057   bool same_host(const base_address &to_compare) const;
00058   bool same_port(const base_address &to_compare) const;
00059   bool shareable(const base_address &to_compare) const;
00060 
00061   bool operator == (const internet_address &to_compare) const {
00062     return same_host(to_compare) && same_port(to_compare);
00063   }
00064 
00065   basis::astring text_form() const;
00066 
00067   basis::astring tokenize() const;
00068   bool detokenize(const basis::astring &info);
00069 
00070   basis::astring normalize_host() const;
00071     // returns a normal form for the hostname or address.  this will come from
00072     // the hostname member first, if it's set.  next it will come from the
00073     // string form of the IP address.
00074 
00075   static const basis::byte_array &nil_address();
00076     // returns the address that is all zeros, otherwise known as INADDR_ANY.
00077 
00078   bool is_nil_address() const;
00079     // returns true if this object's address_array is all zeros.
00080 
00081   static bool is_nil_address(const address_array &ip_address);
00082     // returns true if the array "ip_address" is all zero.
00083 
00084   static bool appropriate_for_ip(const basis::astring &to_check);
00085     // tests whether the string is possibly an ip address; it must have no
00086     // characters in it besides dots and numbers.
00087 
00088   static bool valid_address(const basis::astring &to_check);
00089     // returns true if the address "to_check" seems well-formed for IP.  note
00090     // that this will accept 0.0.0.0 as valid.
00091 
00092   static bool is_valid_internet_address(const basis::astring &to_check,
00093           basis::byte_array &ip_form, bool &all_zeros);
00094     // this function checks the string "to_check" to see if it is a valid
00095     // internet address (e.g., 143.203.39.222).  if it is a valid address,
00096     // then the address "ip_form" is filled in with four bytes that are each
00097     // in the range (0..255).  if the "ip_form" is 0.0.0.0, then all_zeros
00098     // is set to true.
00099 
00100   static bool ip_appropriate_number(const basis::astring &to_check, int indy,
00101           basis::astring &accum);
00103 
00112   static bool has_ip_address(const basis::astring &to_check, basis::astring &ip_found);
00114 
00117   static basis::astring ip_address_text_form(const basis::byte_array &ip_address);
00118     // returns a string containing the textual form of the "ip_address".  the
00119     // "ip_address" is expected to have the proper length (four bytes).  if it
00120     // does not, then an empty string is returned.
00121 
00122   static const basis::byte_array &localhost();
00123     // provides the array that indicates localhost, rather than a NIC.
00124     // this is commonly known to be 127.0.0.1.
00125 
00126   bool is_localhost() const;
00127     // returns true if the address in question is the same as the localhost,
00128     // either through the name matching "local" or "localhost", or through
00129     // the address matching 127.0.0.1.  note that the word "local" here is
00130     // an "enhancement" and would not normally be considered the same as
00131     // localhost.  beware a networked computer that's actually named "local".
00132     // also note that an address where the name disagrees with the address is
00133     // basically broken, but we are not checking that here; either condition
00134     // of the hostname or the address matching causes this to return true.
00135 
00136   base_address *create_copy() const;
00137 
00138   void pack(basis::byte_array &packed_form) const;
00139   bool unpack(basis::byte_array &packed_form);
00140 
00141   virtual int packed_size() const;
00142 };
00143 
00144 } //namespace.
00145 
00146 #endif
00147 
Generated on Sat Jan 28 04:22:43 2012 for hoople2 project by  doxygen 1.6.3