33 using namespace basis;
47 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
85 #define CAST_UP(type) \
86 const type *temp = dynamic_cast<const type *>(&compare_in); \
87 if (!temp) return false; \
88 const type &to_compare = *temp;
92 internet_address::internet_address() { fill(
byte_array(),
"", 0); }
95 const astring &host,
int port_in)
96 { fill(ip, host, port_in); }
105 bool internet_address::ip_appropriate_number(
const astring &to_check,
int indy,
108 FUNCDEF(
"ip_appropriate_number");
110 for (
int i = indy; (i < indy + 3) && (i < to_check.
length()); i++) {
112 if (!parser_bits::is_numeric(to_check[i]) || (to_check[i] ==
'-') ) {
115 if (i == indy)
return false;
118 accum += to_check[i];
120 if (!accum.
length())
return false;
127 bool internet_address::has_ip_address(
const astring &to_check,
133 for (
int i = 0; i < to_check.
length(); i++) {
137 if (!ip_appropriate_number(to_check, i, num_found)) {
144 if (nums_seen >= 4) {
145 ip_found += num_found;
149 int period_indy = to_check.
find(
'.', i);
150 if (
negative(period_indy) || (period_indy > i + 3) ) hosed =
true;
152 for (
int x = i; x < period_indy; x++) {
153 if (!parser_bits::is_numeric(to_check[x]) || (to_check[x] ==
'-')) {
159 ip_found += to_check.
substring(i, period_indy);
176 bool internet_address::is_localhost()
const
185 for (
int i = 0; i < ADDRESS_SIZE; i++) {
186 if (ip_address[i] != localhost().get(i))
193 astring internet_address::normalize_host()
const
197 if (remote.
t())
return remote;
200 remote = ip_address_text_form(ip_form);
206 return sizeof(port) +
207 +
sizeof(
int) + ADDRESS_SIZE
208 +
sizeof(int) + MAXIMUM_HOSTNAME_LENGTH;
213 attach(packed_form, port);
214 packed_form +=
byte_array(ADDRESS_SIZE, ip_address);
221 if (packed_form.
length() <
int(
sizeof(port)) + ADDRESS_SIZE
222 + MAXIMUM_HOSTNAME_LENGTH)
224 if (!
detach(packed_form, port))
return false;
225 packed_form.
stuff(ADDRESS_SIZE, ip_address);
226 packed_form.
zap(0, ADDRESS_SIZE - 1);
227 packed_form.
stuff(MAXIMUM_HOSTNAME_LENGTH, (
abyte *)hostname);
228 packed_form.
zap(0, MAXIMUM_HOSTNAME_LENGTH - 1);
237 for (
int i = 0; i < mini; i++) ip_address[i] = ip[i];
238 for (
int j = mini; j < ADDRESS_SIZE; j++) ip_address[j] = 0;
240 host.
stuff(hostname, MAXIMUM_HOSTNAME_LENGTH - 1);
254 to_print += hostname;
260 to_print +=
"ip_addr=";
261 for (
int i = 0; i < ADDRESS_SIZE; i++) {
262 to_print +=
a_sprintf(
"%d",
int(ip_address[i]));
263 if (i != ADDRESS_SIZE - 1) to_print +=
".";
271 bool internet_address::is_nil_address(
const address_array &ip_address)
273 for (
int i = 0; i < ADDRESS_SIZE; i++)
if (ip_address[i])
return false;
281 bool internet_address::is_nil_address()
const
282 {
return is_nil_address(ip_address); }
284 bool internet_address::same_host(
const base_address &compare_in)
const
292 if ( (is_nil_address(ip_address) && is_nil_address(to_compare.ip_address))
295 if ( (is_nil_address(ip_address) && is_nil_address(to_compare.ip_address))
296 && (
astring(hostname).iequals(to_compare.hostname)) )
298 if (is_nil_address(ip_address))
return false;
299 if (is_nil_address(to_compare.ip_address))
return false;
302 for (
int i = 0; i < ADDRESS_SIZE; i++)
303 if (ip_address[i] != to_compare.ip_address[i])
318 bool internet_address::same_port(
const base_address &compare_in)
const
321 return port == to_compare.port;
324 bool internet_address::shareable(
const base_address &)
const
327 bool internet_address::detokenize(
const astring &info)
335 FIND(
"address", addr);
344 for (
int i = 0; i < ADDRESS_SIZE; i++) {
348 int current_byte = addr.convert(
int(0));
349 ip_found +=
abyte(current_byte);
353 int indy = addr.find(
'.');
359 GRAB(
"port", port_t);
360 int port = port_t.convert(0);
361 fill(ip_found, host, port);
363 LOG(
astring(
"tcp/ip address found::: ") + text_form());
378 if (
astring(hostname).t())
ADD(
"host", hostname);
379 bool print_ip =
false;
380 for (
int i = 0; i < ADDRESS_SIZE; i++) {
381 if (ip_address[i]) print_ip =
true;
385 for (
int i = 0; i < ADDRESS_SIZE; i++)
387 ip_addr.
zap(ip_addr.
end(), ip_addr.
end());
388 ADD(
"address", ip_addr);
397 bool internet_address::appropriate_for_ip(
const astring &to_check)
399 for (
int i = 0; i < to_check.
length(); i++) {
400 char curr = to_check[i];
401 if (curr ==
'.')
continue;
402 if ( (curr >=
'0') && (curr <=
'9') )
continue;
412 bool internet_address::is_valid_internet_address(
const astring &to_check,
420 if (!appropriate_for_ip(to_check))
return false;
423 if ( (to_check[0] ==
'.') || (to_check[to_check.
end()] ==
'.') )
427 if (to_check.
contains(
".."))
return false;
430 char *p = strtok(tmpstr.
s(),
".");
433 while (p && (index < ADDRESS_SIZE)) {
437 if ( (nTemp < 0) || (nTemp > 255) )
return false;
440 ip_form += (
abyte)nTemp;
449 for (
int i = 0; i < ip_form.
length(); i++)
450 if (ip_form[i]) { all_zeros =
false;
break; }
452 return ip_form.
length() == ADDRESS_SIZE;
455 bool internet_address::valid_address(
const astring &to_check)
459 return is_valid_internet_address(to_check, addr, all_zeros);
464 return a_sprintf(
"%d.%d.%d.%d",
int(ip_address[0]),
465 int(ip_address[1]),
int(ip_address[2]),
int(ip_address[3]));
#define GRAB(name, value)
#define FIND(name, value)
a_sprintf is a specialization of astring that provides printf style support.
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
int length() const
Returns the current reported length of the allocated C array.
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
outcome stuff(int length, contents *to_stuff) const
Copies at most "length" elements from this into the array "to_stuff".
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
astring lower() const
like to_lower(), but returns a new string rather than modifying this.
bool t() const
t() is a shortcut for the string being "true", as in non-empty.
int convert(int default_value) const
Converts the string into a corresponding integer.
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
bool substring(astring &target, int start, int end) const
a version that stores the substring in an existing "target" string.
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
void stuff(char *to_stuff, int count) const
a synonym for copy().
void reset()
clears out the contents string.
bool equal_to(const char *that) const
returns true if "that" is equal to this.
int end() const
returns the index of the last (non-null) character in the string.
int length() const
Returns the current length of the string.
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
bool contains(const astring &to_find) const
Returns true if "to_find" is contained in this string or false if not.
void to_lower()
to_lower modifies "this" by replacing capitals with lower-case.
A very common template for a dynamic array of bytes.
this type of address describes a destination out on the internet.
#define NULL_POINTER
The value representing a pointer to nothing.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
The guards collection helps in testing preconditions and reporting errors.
unsigned char abyte
A fairly important unit which is seldom defined...
void attach(byte_array &packed_form, const char *to_attach)
Packs a character string "to_attach" into "packed_form".
bool detach(byte_array &packed_form, astring &to_detach)
Unpacks a character string "to_attach" from "packed_form".
type minimum(type a, type b)
maximum returns the greater of two values.
bool negative(const type &a)
negative returns true if "a" is less than zero.
A logger that sends to the console screen using the standard output device.
Provides access to the operating system's socket methods.
const abyte localhosts_bytes[]
const abyte nil_address_bytes[]
A dynamic container class that holds any kind of object via pointers.
bool unpack(basis::byte_array &packed_form, set< contents > &to_unpack)
provides a way to unpack any set that stores packable objects.
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.
int packed_size(const byte_array &packed_form)
Reports the size required to pack a byte array into a byte array.
time_locus convert(time_number seconds, time_number useconds, const tm &cal_values)
#define SAFE_STATIC_CONST(type, func_name, parms)
this version returns a constant object instead.