first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / sockets / internet_address.h
1 #ifndef INTERNET_ADDRESS_CLASS
2 #define INTERNET_ADDRESS_CLASS
3
4 //////////////
5 // Name   : internet_address
6 // Author : Chris Koeritz
7 //////////////
8 // Copyright (c) 1995-$now By Author.  This program is free software; you can
9 // redistribute it and/or modify it under the terms of the GNU General Public
10 // License as published by the Free Software Foundation:
11 //     http://www.gnu.org/licenses/gpl.html
12 // or under the terms of the GNU Library license:
13 //     http://www.gnu.org/licenses/lgpl.html
14 // at your preference.  Those licenses describe your legal rights to this
15 // software, and no other rights or warranties apply.
16 // Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
17 //////////////
18
19 #include "base_address.h"
20
21 #include <configuration/configurator.h>
22
23 namespace sockets {
24
25 // forward.
26 class machine_uid;
27
28 //! this type of address describes a destination out on the internet.
29
30 class internet_address : public base_address
31 {
32 public:
33   enum internet_address_constraints {
34     ADDRESS_SIZE = 4,
35     MAXIMUM_HOSTNAME_LENGTH = 128
36   };
37
38   typedef basis::abyte address_array[ADDRESS_SIZE];
39   address_array ip_address;
40   int port;
41
42   char hostname[MAXIMUM_HOSTNAME_LENGTH];
43     // can be resolved to an ip_address if a domain name server
44     // is available.
45
46   internet_address();
47   internet_address(const basis::byte_array &ip_address, const basis::astring &host,
48           int port);
49
50   DEFINE_CLASS_NAME("internet_address");
51
52   machine_uid convert() const;
53     // returns the address in the uniquifying format.
54
55   void fill(const basis::byte_array &ip_address, const basis::astring &host, int port);
56
57   bool same_host(const base_address &to_compare) const;
58   bool same_port(const base_address &to_compare) const;
59   bool shareable(const base_address &to_compare) const;
60
61   bool operator == (const internet_address &to_compare) const {
62     return same_host(to_compare) && same_port(to_compare);
63   }
64
65   basis::astring text_form() const;
66
67   basis::astring tokenize() const;
68   bool detokenize(const basis::astring &info);
69
70   basis::astring normalize_host() const;
71     // returns a normal form for the hostname or address.  this will come from
72     // the hostname member first, if it's set.  next it will come from the
73     // string form of the IP address.
74
75   static const basis::byte_array &nil_address();
76     // returns the address that is all zeros, otherwise known as INADDR_ANY.
77
78   bool is_nil_address() const;
79     // returns true if this object's address_array is all zeros.
80
81   static bool is_nil_address(const address_array &ip_address);
82     // returns true if the array "ip_address" is all zero.
83
84   static bool appropriate_for_ip(const basis::astring &to_check);
85     // tests whether the string is possibly an ip address; it must have no
86     // characters in it besides dots and numbers.
87
88   static bool valid_address(const basis::astring &to_check);
89     // returns true if the address "to_check" seems well-formed for IP.  note
90     // that this will accept 0.0.0.0 as valid.
91
92   static bool is_valid_internet_address(const basis::astring &to_check,
93           basis::byte_array &ip_form, bool &all_zeros);
94     // this function checks the string "to_check" to see if it is a valid
95     // internet address (e.g., 143.203.39.222).  if it is a valid address,
96     // then the address "ip_form" is filled in with four bytes that are each
97     // in the range (0..255).  if the "ip_form" is 0.0.0.0, then all_zeros
98     // is set to true.
99
100   static bool ip_appropriate_number(const basis::astring &to_check, int indy,
101           basis::astring &accum);
102     //!< returns true if "to_check" has a number at "indy" that works in ipv4.
103     /*!< this reports if the string at the position specified could be part of
104     a valid internet address.  the characters starting at the "indy" must be
105     numeric.  up to three numbers will be checked, and when we get three or
106     less of the numbers together, we will check that they make an integer less
107     than 255.  the "accum" string will be filled with the number we found.
108     note that this doesn't care what the characters are after our 1-3 numbers;
109     it merely checks whether the portion of the string at "indy" *could* work
110     in an IP address. */
111
112   static bool has_ip_address(const basis::astring &to_check, basis::astring &ip_found);
113     //!< returns true if "to_check" has an IP address in it somewhere.
114     /*!< this looks across the whole string and returns the first IP address
115     it finds in "ip_found", if possible. */
116
117   static basis::astring ip_address_text_form(const basis::byte_array &ip_address);
118     // returns a string containing the textual form of the "ip_address".  the
119     // "ip_address" is expected to have the proper length (four bytes).  if it
120     // does not, then an empty string is returned.
121
122   static const basis::byte_array &localhost();
123     // provides the array that indicates localhost, rather than a NIC.
124     // this is commonly known to be 127.0.0.1.
125
126   bool is_localhost() const;
127     // returns true if the address in question is the same as the localhost,
128     // either through the name matching "local" or "localhost", or through
129     // the address matching 127.0.0.1.  note that the word "local" here is
130     // an "enhancement" and would not normally be considered the same as
131     // localhost.  beware a networked computer that's actually named "local".
132     // also note that an address where the name disagrees with the address is
133     // basically broken, but we are not checking that here; either condition
134     // of the hostname or the address matching causes this to return true.
135
136   base_address *create_copy() const;
137
138   void pack(basis::byte_array &packed_form) const;
139   bool unpack(basis::byte_array &packed_form);
140
141   virtual int packed_size() const;
142 };
143
144 } //namespace.
145
146 #endif
147