23 #include <arpa/inet.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
35 using namespace basis;
46 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
59 const char *communication_commons::outcome_name(
const outcome &to_name)
61 switch (to_name.
value()) {
62 case NO_CONNECTION:
return "NO_CONNECTION";
63 case NO_SERVER:
return "NO_SERVER";
64 case NO_ANSWER:
return "NO_ANSWER";
65 case SHUTDOWN:
return "SHUTDOWN";
66 case ALREADY_SETUP:
return "ALREADY_SETUP";
67 case MEDIUM_ERROR:
return "MEDIUM_ERROR";
68 case BAD_MODE:
return "BAD_MODE";
69 case ALREADY_CONNECTED:
return "ALREADY_CONNECTED";
70 case WRONG_ENTITY:
return "WRONG_ENTITY";
71 case IPC_ERROR:
return "IPC_ERROR";
72 case TOO_NOISY:
return "TOO_NOISY";
73 case COMM_ERROR:
return "COMM_ERROR";
80 tcpip_stack::tcpip_stack()
81 : _healthy(initialize_tcpip())
90 bool tcpip_stack::initialize_tcpip()
108 void tcpip_stack::deinitialize_tcpip()
122 LOG(
astring(astring::SPRINTF,
"gethostname error %s.",
131 switch (error_value) {
189 return critical_events::system_error_text(error_value);
195 bool add_local)
const
197 #ifdef DEBUG_TCPIP_STACK
200 ip_addresses.
reset();
203 ip_addresses +=
"127.0.0.1";
205 #ifdef DEBUG_TCPIP_STACK
209 #ifdef DEBUG_TCPIP_STACK
210 LOG(
"failed to get the hostname for this machine!");
215 hostent *host_entry = gethostbyname(
this_host.s());
217 #ifdef DEBUG_TCPIP_STACK
222 for (
int adapter_num = 0; ; adapter_num++) {
223 in_addr *current_entry = (in_addr *)host_entry->h_addr_list[adapter_num];
224 if (!current_entry)
break;
225 char *ip_address = inet_ntoa(*current_entry);
226 #ifdef DEBUG_TCPIP_STACK
229 ip_addresses += ip_address;
232 #ifdef DEBUG_TCPIP_STACK
233 LOG(
astring(
"read addresses:") + parser_bits::platform_eol_to_chars()
237 return !!ip_addresses.
length();
241 bool add_local)
const
243 ip_addresses.
reset();
247 for (
int i = 0; i < text_list.
length(); i++) {
252 ip_addresses += addr_form.
convert();
260 FUNCDEF(
"convert [to sockaddr]");
261 sockaddr_in new_socket;
262 memset(&new_socket, 0,
sizeof(new_socket));
263 new_socket.sin_family = AF_INET;
270 memset(&to_return, 0,
sizeof(to_return));
271 memcpy(&to_return, (sockaddr *)&new_socket,
sizeof(new_socket));
278 const sockaddr_in *make_from = (
const sockaddr_in *)&make_from_o;
281 to_return.
fill(ip,
"", ntohs(make_from->sin_port));
290 full_hostname.
reset();
294 full_hostname =
"localhost";
299 full_hostname =
"inaddr_any";
306 LOG(
astring(astring::SPRINTF,
"gethostbyname error %s.",
310 full_hostname =
astring(machine->h_name);
322 if (!ip.
length())
return false;
350 for (
int i = 0; i < ip.
length(); i++) {
351 to_return +=
astring(astring::SPRINTF,
"%d",
int(ip[i]));
352 if (i != ip.
length() - 1) to_return +=
".";
380 switch (location_type) {
405 int port,
bool &worked)
const
410 if (!machine) machine =
"local";
411 bool resolved =
false;
416 machine =
"localhost";
420 }
else if (machine.
iequals(
"inaddr_any")
421 || machine.
iequals(
"any-address")) {
423 machine =
"inaddr_any";
438 if (resolved) to_return.
fill(ip_addr, machine, port);
439 else to_return.
fill(ip_addr, astring::empty_string(), port);
446 to_return.
fill(ip_addr, machine, port);
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 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.
bool iequals(const astring &that) const
returns true if this is case-insensitively equal to "that".
void stuff(char *to_stuff, int count) const
a synonym for copy().
void reset()
clears out the contents string.
virtual const char * observe() const
observes the underlying pointer to the zero-terminated string.
void to_lower()
to_lower modifies "this" by replacing capitals with lower-case.
A very common template for a dynamic array of bytes.
static const char * outcome_name(const outcome &to_name)
Returns a string representation of the outcome "to_name".
Outcomes describe the state of completion for an operation.
this type of address describes a destination out on the internet.
machine_uid convert() const
static const basis::byte_array & localhost()
void fill(const basis::byte_array &ip_address, const basis::astring &host, int port)
static const basis::byte_array & nil_address()
char hostname[MAXIMUM_HOSTNAME_LENGTH]
static bool is_valid_internet_address(const basis::astring &to_check, basis::byte_array &ip_form, bool &all_zeros)
@ MAXIMUM_HOSTNAME_LENGTH
static basis::astring tcpip_error_name(int error_value)
internet_address fill_and_resolve(const basis::astring &machine, int port, bool &worked) const
basis::byte_array full_resolve(const basis::astring &hostname, basis::astring &full_host) const
basis::astring hostname() const
machine_uid this_host(int location_type) const
bool resolve_any(const basis::astring &name, internet_address &resolved) const
basis::astring dns_resolve(const basis::astring &hostname) const
bool enumerate_adapters(structures::string_array &ip_addresses, bool add_local=false) const
static sockaddr convert(const internet_address &to_convert)
An array of strings with some additional helpful methods.
basis::astring text_form() const
A synonym for the text_format() method.
#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...
unsigned short un_short
Abbreviated name for unsigned short integers.
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.
A dynamic container class that holds any kind of object via pointers.
#define SOCK_EDESTADDRREQ
#define SOCK_EPFNOSUPPORT
#define SOCK_ETOOMANYREFS
#define SOCK_ENAMETOOLONG
#define SOCK_ESOCKTNOSUPPORT
#define SOCK_EAFNOSUPPORT
#define SOCK_EADDRNOTAVAIL
#define SOCK_EHOSTUNREACH
#define SOCK_ECONNABORTED
#define SOCK_EPROTONOSUPPORT
#define SOCK_ECONNREFUSED