3 * Author : Chris Koeritz
4 * Copyright (c) 1991-$now By Author. This program is free software; you can *
5 * redistribute it and/or modify it under the terms of the GNU General Public *
6 * License as published by the Free Software Foundation; either version 2 of *
7 * the License or (at your option) any later version. This is online at: *
8 * http://www.fsf.org/copyleft/gpl.html *
9 * Please send any updates to: fred@gruntose.com *
10 \*****************************************************************************/
12 #include "internet_address.h"
13 #include "machine_uid.h"
14 #include "raw_socket.h"
15 #include "tcpip_stack.h"
17 #include <basis/functions.h>
18 #include <loggers/critical_events.h>
19 #include <loggers/program_wide_logger.h>
20 #include <structures/string_array.h>
23 #include <arpa/inet.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
35 using namespace basis;
36 using namespace loggers;
37 using namespace structures;
41 //#define DEBUG_TCPIP_STACK
42 // turn on for clamor.
45 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
50 const WORD WINSOCK_VERSION_REQUIRED = 0x0101;
51 // 1.1 version is used by this version of tcp/ip transport.
56 const char *communication_commons::outcome_name(const outcome &to_name)
58 switch (to_name.value()) {
59 case NO_CONNECTION: return "NO_CONNECTION";
60 case NO_SERVER: return "NO_SERVER";
61 case NO_ANSWER: return "NO_ANSWER";
62 case SHUTDOWN: return "SHUTDOWN";
63 case ALREADY_SETUP: return "ALREADY_SETUP";
64 case MEDIUM_ERROR: return "MEDIUM_ERROR";
65 case BAD_MODE: return "BAD_MODE";
66 case ALREADY_CONNECTED: return "ALREADY_CONNECTED";
67 case WRONG_ENTITY: return "WRONG_ENTITY";
68 case IPC_ERROR: return "IPC_ERROR";
69 case TOO_NOISY: return "TOO_NOISY";
70 case COMM_ERROR: return "COMM_ERROR";
71 default: return common::outcome_name(to_name);
77 tcpip_stack::tcpip_stack()
78 : _healthy(initialize_tcpip())
81 tcpip_stack::~tcpip_stack()
87 bool tcpip_stack::initialize_tcpip()
90 FUNCDEF("initialize_tcpip");
91 // make sure we have the right version of WinSock available.
92 WORD desired_winsock = WINSOCK_VERSION_REQUIRED;
94 int error = WSAStartup(desired_winsock, &startup_data);
96 LOG(astring("startup error: ") + tcpip_error_name(critical_events::system_error()));
103 void tcpip_stack::deinitialize_tcpip()
110 astring tcpip_stack::hostname() const
115 // gethostname() finds the name for our tcp/ip host.
116 if (negative(gethostname(hostname, 255))) {
117 LOG(astring(astring::SPRINTF, "gethostname error %s.",
118 tcpip_error_name(critical_events::system_error()).s()));
124 astring tcpip_stack::tcpip_error_name(int error_value)
126 switch (error_value) {
128 case SOCK_EINTR: return "EINTR";
129 case SOCK_EBADF: return "EBADF";
130 case SOCK_EACCES: return "EACCES";
131 case SOCK_EFAULT: return "EFAULT";
132 case SOCK_EINVAL: return "EINVAL";
133 case SOCK_EMFILE: return "EMFILE";
134 case SOCK_EWOULDBLOCK: return "EWOULDBLOCK";
135 case SOCK_EINPROGRESS: return "EINPROGRESS";
136 case SOCK_EALREADY: return "EALREADY";
137 case SOCK_ENOTSOCK: return "ENOTSOCK";
138 case SOCK_EDESTADDRREQ: return "EDESTADDRREQ";
139 case SOCK_EMSGSIZE: return "EMSGSIZE";
140 case SOCK_EPROTOTYPE: return "EPROTOTYPE";
141 case SOCK_ENOPROTOOPT: return "ENOPROTOOPT";
142 case SOCK_EPROTONOSUPPORT: return "EPROTONOSUPPORT";
143 case SOCK_ESOCKTNOSUPPORT: return "ESOCKTNOSUPPORT";
144 case SOCK_EOPNOTSUPP: return "EOPNOTSUPP";
145 case SOCK_EPFNOSUPPORT: return "EPFNOSUPPORT";
146 case SOCK_EAFNOSUPPORT: return "EAFNOSUPPORT";
147 case SOCK_EADDRINUSE: return "EADDRINUSE";
148 case SOCK_EADDRNOTAVAIL: return "EADDRNOTAVAIL";
149 case SOCK_ENETDOWN: return "ENETDOWN";
150 case SOCK_ENETUNREACH: return "ENETUNREACH";
151 case SOCK_ENETRESET: return "ENETRESET";
152 case SOCK_ECONNABORTED: return "ECONNABORTED";
153 case SOCK_ECONNRESET: return "ECONNRESET";
154 case SOCK_ENOBUFS: return "ENOBUFS";
155 case SOCK_EISCONN: return "EISCONN";
156 case SOCK_ENOTCONN: return "ENOTCONN";
157 case SOCK_ESHUTDOWN: return "ESHUTDOWN";
158 case SOCK_ETOOMANYREFS: return "ETOOMANYREFS";
159 case SOCK_ETIMEDOUT: return "ETIMEDOUT";
160 case SOCK_ECONNREFUSED: return "ECONNREFUSED";
161 case SOCK_ELOOP: return "ELOOP";
162 case SOCK_ENAMETOOLONG: return "ENAMETOOLONG";
163 case SOCK_EHOSTDOWN: return "EHOSTDOWN";
164 case SOCK_EHOSTUNREACH: return "EHOSTUNREACH";
165 case SOCK_ENOTEMPTY: return "ENOTEMPTY";
166 case SOCK_EUSERS: return "EUSERS";
167 case SOCK_EDQUOT: return "EDQUOT";
168 case SOCK_ESTALE: return "ESTALE";
169 case SOCK_EREMOTE: return "EREMOTE";
171 case SOCK_EPROCLIM: return "EPROCLIM";
172 case SOCK_SYSNOTREADY: return "SYSNOTREADY";
173 case SOCK_VERNOTSUPPORTED: return "VERNOTSUPPORTED";
174 case SOCK_HOST_NOT_FOUND: return "HOST_NOT_FOUND";
175 case SOCK_TRY_AGAIN: return "TRY_AGAIN";
176 case SOCK_NO_RECOVERY: return "NO_RECOVERY";
177 case SOCK_NO_DATA: return "NO_DATA"; // or NO_ADDRESS.
178 case SOCK_NOTINITIALISED: return "NOTINITIALISED";
182 // return a standard OS error...
183 return critical_events::system_error_text(error_value);
188 bool tcpip_stack::enumerate_adapters(string_array &ip_addresses,
189 bool add_local) const
191 #ifdef DEBUG_TCPIP_STACK
192 FUNCDEF("enumerate_adapters");
194 ip_addresses.reset();
195 // see if they want to put the local adapter in there.
197 ip_addresses += "127.0.0.1";
198 astring this_host = hostname();
199 #ifdef DEBUG_TCPIP_STACK
200 LOG(astring("hostname is \"") + this_host + astring("\"."));
203 #ifdef DEBUG_TCPIP_STACK
204 LOG("failed to get the hostname for this machine!");
209 hostent *host_entry = gethostbyname(this_host.s());
211 #ifdef DEBUG_TCPIP_STACK
212 LOG(astring("failed to get host entry for \"") + this_host + "\".");
216 for (int adapter_num = 0; /* check is inside loop */; adapter_num++) {
217 in_addr *current_entry = (in_addr *)host_entry->h_addr_list[adapter_num];
218 if (!current_entry) break;
219 char *ip_address = inet_ntoa(*current_entry);
220 #ifdef DEBUG_TCPIP_STACK
221 LOG(astring("current is: ") + astring(ip_address));
223 ip_addresses += ip_address;
226 #ifdef DEBUG_TCPIP_STACK
227 LOG(astring("read addresses:") + parser_bits::platform_eol_to_chars()
228 + ip_addresses.text_form());
231 return !!ip_addresses.length();
234 bool tcpip_stack::enumerate_adapters(machine_uid_array &ip_addresses,
235 bool add_local) const
237 ip_addresses.reset();
238 string_array text_list;
239 if (!enumerate_adapters(text_list, add_local))
241 for (int i = 0; i < text_list.length(); i++) {
243 internet_address addr_form = fill_and_resolve(text_list[i], 0, worked);
245 hostname().stuff(addr_form.hostname, addr_form.MAXIMUM_HOSTNAME_LENGTH);
246 ip_addresses += addr_form.convert();
249 return !!ip_addresses.elements();
252 sockaddr tcpip_stack::convert(const internet_address &make_from)
254 FUNCDEF("convert [to sockaddr]");
255 sockaddr_in new_socket; // our socket.
256 memset(&new_socket, 0, sizeof(new_socket)); // clear it out.
257 new_socket.sin_family = AF_INET;
258 byte_array ip(internet_address::ADDRESS_SIZE, make_from.ip_address);
259 ip.stuff(internet_address::ADDRESS_SIZE, (abyte *)&new_socket.sin_addr);
260 new_socket.sin_port = htons(basis::un_short(make_from.port));
261 // possibly unportable conversion to short above.
262 // now we need to return the more generic form of socket address.
264 memset(&to_return, 0, sizeof(to_return));
265 memcpy(&to_return, (sockaddr *)&new_socket, sizeof(new_socket));
266 // sockaddr_in guaranteed to be smaller or equal to sockaddr.
270 internet_address tcpip_stack::convert(const sockaddr &make_from_o)
272 const sockaddr_in *make_from = (const sockaddr_in *)&make_from_o;
273 byte_array ip(internet_address::ADDRESS_SIZE, (abyte *)&make_from->sin_addr);
274 internet_address to_return;
275 to_return.fill(ip, "", ntohs(make_from->sin_port));
279 byte_array tcpip_stack::full_resolve(const astring &hostname,
280 astring &full_hostname) const
282 FUNCDEF("full_resolve");
283 if (!hostname) return byte_array(); // blank hostnames go nowhere.
284 full_hostname.reset();
285 // check first for local host equivalents.
286 if ( hostname.iequals("local") || hostname.iequals("localhost") ) {
287 byte_array to_return = internet_address::localhost();
288 full_hostname = "localhost";
290 } else if (hostname.iequals("inaddr_any")
291 || hostname.iequals("any-address")) {
292 byte_array to_return = internet_address::nil_address();
293 full_hostname = "inaddr_any";
296 // gethostbyname() fills in details about the host, such as its IP address
297 // and full hostname.
298 hostent *machine = gethostbyname(hostname.observe());
300 LOG(astring(astring::SPRINTF, "gethostbyname error %s.",
301 tcpip_error_name(critical_events::system_error()).s()));
304 full_hostname = astring(machine->h_name);
305 return byte_array(machine->h_length, (abyte *)machine->h_addr);
308 bool tcpip_stack::resolve_any(const astring &hostname,
309 internet_address &to_return) const
311 FUNCDEF("resolve_any");
312 to_return = internet_address();
313 if (!hostname) return false; // blank hostnames go nowhere.
315 byte_array ip = full_resolve(hostname, full_host);
316 if (!ip.length()) return false;
317 // success then. fill out the address object.
318 full_host.stuff(to_return.hostname,
319 internet_address::MAXIMUM_HOSTNAME_LENGTH);
320 // copy the ip address into our structure.
321 ip.stuff(internet_address::ADDRESS_SIZE, (abyte *)&to_return.ip_address);
325 astring tcpip_stack::dns_resolve(const astring &hostname) const
327 FUNCDEF("dns_resolve");
328 if (!hostname) return ""; // blank hostnames go nowhere.
329 if (hostname.iequals("local") || hostname.iequals("localhost")) {
331 } else if (hostname.iequals("inaddr_any")
332 || hostname.iequals("any-address")) {
335 // gethostbyname() fills out details about the host, such as its IP address
336 // and full hostname.
337 hostent *machine = gethostbyname(hostname.observe());
341 byte_array ip(machine->h_length, (abyte *)machine->h_addr);
342 // copy the ip address into an array for easier manipulation.
344 for (int i = 0; i < ip.length(); i++) {
345 to_return += astring(astring::SPRINTF, "%d", int(ip[i]));
346 if (i != ip.length() - 1) to_return += ".";
353 //decide if this should be kept or not.
355 internet_address tcpip_stack::deprecated_lookup
356 (const byte_array &ip_address)
358 FUNCDEF("deprecated_lookup");
359 // lookup the client's hostname through DNS.
360 hostent *entry = gethostbyaddr((char *)ip_address.observe(),
361 ip_address.length(), PF_INET);
362 astring hostname = "unknown_host";
363 if (entry) hostname = entry->h_name;
365 internet_address to_return;
366 to_return.fill(ip_address, hostname, 0);
367 // the zero is above because we don't know the port here.
372 machine_uid tcpip_stack::this_host(int location_type) const
374 switch (location_type) {
375 case machine_uid::TCPIP_LOCATION: {
376 astring host = hostname();
378 byte_array ip = full_resolve(host, full_host);
379 if (!ip.length()) return machine_uid(); // failure.
380 return internet_machine_uid(full_host, ip);
382 case machine_uid::IPX_LOCATION: {
384 return machine_uid(); // return junk.
387 case machine_uid::NETBIOS_LOCATION: {
389 return machine_uid(); // return junk.
394 return machine_uid(); // return junk.
398 internet_address tcpip_stack::fill_and_resolve(const astring &machine_in,
399 int port, bool &worked) const
401 internet_address to_return;
402 astring machine = machine_in;
404 if (!machine) machine = "local"; // assume they mean this machine.
405 bool resolved = false; // true if we know the name.
406 byte_array ip_addr; // the ip address we're guessing/decoding/looking up.
407 bool is_ip = false; // set to true if we have the IP address in ip_addr.
408 if (machine.iequals("local") || machine.iequals("localhost")) {
409 // handle our special flag and the normal localhost machine type.
410 machine = "localhost";
411 ip_addr = internet_address::localhost();
412 is_ip = true; // we have the address already.
413 resolved = true; // we know the name also.
414 } else if (machine.iequals("inaddr_any")
415 || machine.iequals("any-address")) {
416 // handle the any address case.
417 machine = "inaddr_any";
418 ip_addr = internet_address::nil_address();
419 is_ip = true; // we have the address already.
420 resolved = true; // we know the name also.
422 // well, we now are just going to guess about it being an IP address.
424 is_ip = internet_address::is_valid_internet_address(machine, ip_addr,
428 // we try to fill in the hostname if given ip only.
429 //hmmm: use reverse dns to get the machine name for real!
430 // for cases where we really had an ip address, using the machine there
431 // would be a mistake.
432 if (resolved) to_return.fill(ip_addr, machine, port);
433 else to_return.fill(ip_addr, astring::empty_string(), port);
434 resolved = true; // claim we are happy with it now.
436 // we try to fill in the ip address for the host.
438 ip_addr = full_resolve(machine, full_host);
439 if (ip_addr.length()) {
440 to_return.fill(ip_addr, machine, port);