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;
38 using namespace textual;
42 //#define DEBUG_TCPIP_STACK
43 // turn on for clamor.
46 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
52 const WORD WINSOCK_VERSION_REQUIRED = 0x0101;
53 // 1.1 version is used by this version of tcp/ip transport.
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";
74 default: return common::outcome_name(to_name);
80 tcpip_stack::tcpip_stack()
81 : _healthy(initialize_tcpip())
84 tcpip_stack::~tcpip_stack()
90 bool tcpip_stack::initialize_tcpip()
94 FUNCDEF("initialize_tcpip");
95 // make sure we have the right version of WinSock available.
96 WORD desired_winsock = WINSOCK_VERSION_REQUIRED;
98 int error = WSAStartup(desired_winsock, &startup_data);
100 LOG(astring("startup error: ") + tcpip_error_name(critical_events::system_error()));
108 void tcpip_stack::deinitialize_tcpip()
115 astring tcpip_stack::hostname() const
120 // gethostname() finds the name for our tcp/ip host.
121 if (negative(gethostname(hostname, 255))) {
122 LOG(astring(astring::SPRINTF, "gethostname error %s.",
123 tcpip_error_name(critical_events::system_error()).s()));
129 astring tcpip_stack::tcpip_error_name(int error_value)
131 switch (error_value) {
133 case SOCK_EINTR: return "EINTR";
134 case SOCK_EBADF: return "EBADF";
135 case SOCK_EACCES: return "EACCES";
136 case SOCK_EFAULT: return "EFAULT";
137 case SOCK_EINVAL: return "EINVAL";
138 case SOCK_EMFILE: return "EMFILE";
139 case SOCK_EWOULDBLOCK: return "EWOULDBLOCK";
140 case SOCK_EINPROGRESS: return "EINPROGRESS";
141 case SOCK_EALREADY: return "EALREADY";
142 case SOCK_ENOTSOCK: return "ENOTSOCK";
143 case SOCK_EDESTADDRREQ: return "EDESTADDRREQ";
144 case SOCK_EMSGSIZE: return "EMSGSIZE";
145 case SOCK_EPROTOTYPE: return "EPROTOTYPE";
146 case SOCK_ENOPROTOOPT: return "ENOPROTOOPT";
147 case SOCK_EPROTONOSUPPORT: return "EPROTONOSUPPORT";
148 case SOCK_ESOCKTNOSUPPORT: return "ESOCKTNOSUPPORT";
149 case SOCK_EOPNOTSUPP: return "EOPNOTSUPP";
150 case SOCK_EPFNOSUPPORT: return "EPFNOSUPPORT";
151 case SOCK_EAFNOSUPPORT: return "EAFNOSUPPORT";
152 case SOCK_EADDRINUSE: return "EADDRINUSE";
153 case SOCK_EADDRNOTAVAIL: return "EADDRNOTAVAIL";
154 case SOCK_ENETDOWN: return "ENETDOWN";
155 case SOCK_ENETUNREACH: return "ENETUNREACH";
156 case SOCK_ENETRESET: return "ENETRESET";
157 case SOCK_ECONNABORTED: return "ECONNABORTED";
158 case SOCK_ECONNRESET: return "ECONNRESET";
159 case SOCK_ENOBUFS: return "ENOBUFS";
160 case SOCK_EISCONN: return "EISCONN";
161 case SOCK_ENOTCONN: return "ENOTCONN";
162 case SOCK_ESHUTDOWN: return "ESHUTDOWN";
163 case SOCK_ETOOMANYREFS: return "ETOOMANYREFS";
164 case SOCK_ETIMEDOUT: return "ETIMEDOUT";
165 case SOCK_ECONNREFUSED: return "ECONNREFUSED";
166 case SOCK_ELOOP: return "ELOOP";
167 case SOCK_ENAMETOOLONG: return "ENAMETOOLONG";
168 case SOCK_EHOSTDOWN: return "EHOSTDOWN";
169 case SOCK_EHOSTUNREACH: return "EHOSTUNREACH";
170 case SOCK_ENOTEMPTY: return "ENOTEMPTY";
171 case SOCK_EUSERS: return "EUSERS";
172 case SOCK_EDQUOT: return "EDQUOT";
173 case SOCK_ESTALE: return "ESTALE";
174 case SOCK_EREMOTE: return "EREMOTE";
176 case SOCK_EPROCLIM: return "EPROCLIM";
177 case SOCK_SYSNOTREADY: return "SYSNOTREADY";
178 case SOCK_VERNOTSUPPORTED: return "VERNOTSUPPORTED";
179 case SOCK_HOST_NOT_FOUND: return "HOST_NOT_FOUND";
180 case SOCK_TRY_AGAIN: return "TRY_AGAIN";
181 case SOCK_NO_RECOVERY: return "NO_RECOVERY";
182 case SOCK_NO_DATA: return "NO_DATA"; // or NO_ADDRESS.
183 case SOCK_NOTINITIALISED: return "NOTINITIALISED";
188 // return a standard OS error...
189 return critical_events::system_error_text(error_value);
194 bool tcpip_stack::enumerate_adapters(string_array &ip_addresses,
195 bool add_local) const
197 #ifdef DEBUG_TCPIP_STACK
198 FUNCDEF("enumerate_adapters");
200 ip_addresses.reset();
201 // see if they want to put the local adapter in there.
203 ip_addresses += "127.0.0.1";
204 astring this_host = hostname();
205 #ifdef DEBUG_TCPIP_STACK
206 LOG(astring("hostname is \"") + this_host + astring("\"."));
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
218 LOG(astring("failed to get host entry for \"") + this_host + "\".");
222 for (int adapter_num = 0; /* check is inside loop */; 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
227 LOG(astring("current is: ") + astring(ip_address));
229 ip_addresses += ip_address;
232 #ifdef DEBUG_TCPIP_STACK
233 LOG(astring("read addresses:") + parser_bits::platform_eol_to_chars()
234 + ip_addresses.text_form());
237 return !!ip_addresses.length();
240 bool tcpip_stack::enumerate_adapters(machine_uid_array &ip_addresses,
241 bool add_local) const
243 ip_addresses.reset();
244 string_array text_list;
245 if (!enumerate_adapters(text_list, add_local))
247 for (int i = 0; i < text_list.length(); i++) {
249 internet_address addr_form = fill_and_resolve(text_list[i], 0, worked);
251 hostname().stuff(addr_form.hostname, addr_form.MAXIMUM_HOSTNAME_LENGTH);
252 ip_addresses += addr_form.convert();
255 return !!ip_addresses.elements();
258 sockaddr tcpip_stack::convert(const internet_address &make_from)
260 FUNCDEF("convert [to sockaddr]");
261 sockaddr_in new_socket; // our socket.
262 memset(&new_socket, 0, sizeof(new_socket)); // clear it out.
263 new_socket.sin_family = AF_INET;
264 byte_array ip(internet_address::ADDRESS_SIZE, make_from.ip_address);
265 ip.stuff(internet_address::ADDRESS_SIZE, (abyte *)&new_socket.sin_addr);
266 new_socket.sin_port = htons(basis::un_short(make_from.port));
267 // possibly unportable conversion to short above.
268 // now we need to return the more generic form of socket address.
270 memset(&to_return, 0, sizeof(to_return));
271 memcpy(&to_return, (sockaddr *)&new_socket, sizeof(new_socket));
272 // sockaddr_in guaranteed to be smaller or equal to sockaddr.
276 internet_address tcpip_stack::convert(const sockaddr &make_from_o)
278 const sockaddr_in *make_from = (const sockaddr_in *)&make_from_o;
279 byte_array ip(internet_address::ADDRESS_SIZE, (abyte *)&make_from->sin_addr);
280 internet_address to_return;
281 to_return.fill(ip, "", ntohs(make_from->sin_port));
285 byte_array tcpip_stack::full_resolve(const astring &hostname,
286 astring &full_hostname) const
288 FUNCDEF("full_resolve");
289 if (!hostname) return byte_array(); // blank hostnames go nowhere.
290 full_hostname.reset();
291 // check first for local host equivalents.
292 if ( hostname.iequals("local") || hostname.iequals("localhost") ) {
293 byte_array to_return = internet_address::localhost();
294 full_hostname = "localhost";
296 } else if (hostname.iequals("inaddr_any")
297 || hostname.iequals("any-address")) {
298 byte_array to_return = internet_address::nil_address();
299 full_hostname = "inaddr_any";
302 // gethostbyname() fills in details about the host, such as its IP address
303 // and full hostname.
304 hostent *machine = gethostbyname(hostname.observe());
306 LOG(astring(astring::SPRINTF, "gethostbyname error %s.",
307 tcpip_error_name(critical_events::system_error()).s()));
310 full_hostname = astring(machine->h_name);
311 return byte_array(machine->h_length, (abyte *)machine->h_addr);
314 bool tcpip_stack::resolve_any(const astring &hostname,
315 internet_address &to_return) const
317 FUNCDEF("resolve_any");
318 to_return = internet_address();
319 if (!hostname) return false; // blank hostnames go nowhere.
321 byte_array ip = full_resolve(hostname, full_host);
322 if (!ip.length()) return false;
323 // success then. fill out the address object.
324 full_host.stuff(to_return.hostname,
325 internet_address::MAXIMUM_HOSTNAME_LENGTH);
326 // copy the ip address into our structure.
327 ip.stuff(internet_address::ADDRESS_SIZE, (abyte *)&to_return.ip_address);
331 astring tcpip_stack::dns_resolve(const astring &hostname) const
333 FUNCDEF("dns_resolve");
334 if (!hostname) return ""; // blank hostnames go nowhere.
335 if (hostname.iequals("local") || hostname.iequals("localhost")) {
337 } else if (hostname.iequals("inaddr_any")
338 || hostname.iequals("any-address")) {
341 // gethostbyname() fills out details about the host, such as its IP address
342 // and full hostname.
343 hostent *machine = gethostbyname(hostname.observe());
347 byte_array ip(machine->h_length, (abyte *)machine->h_addr);
348 // copy the ip address into an array for easier manipulation.
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 += ".";
359 //decide if this should be kept or not.
361 internet_address tcpip_stack::deprecated_lookup
362 (const byte_array &ip_address)
364 FUNCDEF("deprecated_lookup");
365 // lookup the client's hostname through DNS.
366 hostent *entry = gethostbyaddr((char *)ip_address.observe(),
367 ip_address.length(), PF_INET);
368 astring hostname = "unknown_host";
369 if (entry) hostname = entry->h_name;
371 internet_address to_return;
372 to_return.fill(ip_address, hostname, 0);
373 // the zero is above because we don't know the port here.
378 machine_uid tcpip_stack::this_host(int location_type) const
380 switch (location_type) {
381 case machine_uid::TCPIP_LOCATION: {
382 astring host = hostname();
384 byte_array ip = full_resolve(host, full_host);
385 if (!ip.length()) return machine_uid(); // failure.
386 return internet_machine_uid(full_host, ip);
388 case machine_uid::IPX_LOCATION: {
390 return machine_uid(); // return junk.
393 case machine_uid::NETBIOS_LOCATION: {
395 return machine_uid(); // return junk.
400 return machine_uid(); // return junk.
404 internet_address tcpip_stack::fill_and_resolve(const astring &machine_in,
405 int port, bool &worked) const
407 internet_address to_return;
408 astring machine = machine_in;
410 if (!machine) machine = "local"; // assume they mean this machine.
411 bool resolved = false; // true if we know the name.
412 byte_array ip_addr; // the ip address we're guessing/decoding/looking up.
413 bool is_ip = false; // set to true if we have the IP address in ip_addr.
414 if (machine.iequals("local") || machine.iequals("localhost")) {
415 // handle our special flag and the normal localhost machine type.
416 machine = "localhost";
417 ip_addr = internet_address::localhost();
418 is_ip = true; // we have the address already.
419 resolved = true; // we know the name also.
420 } else if (machine.iequals("inaddr_any")
421 || machine.iequals("any-address")) {
422 // handle the any address case.
423 machine = "inaddr_any";
424 ip_addr = internet_address::nil_address();
425 is_ip = true; // we have the address already.
426 resolved = true; // we know the name also.
428 // well, we now are just going to guess about it being an IP address.
430 is_ip = internet_address::is_valid_internet_address(machine, ip_addr,
434 // we try to fill in the hostname if given ip only.
435 //hmmm: use reverse dns to get the machine name for real!
436 // for cases where we really had an ip address, using the machine there
437 // would be a mistake.
438 if (resolved) to_return.fill(ip_addr, machine, port);
439 else to_return.fill(ip_addr, astring::empty_string(), port);
440 resolved = true; // claim we are happy with it now.
442 // we try to fill in the ip address for the host.
444 ip_addr = full_resolve(machine, full_host);
445 if (ip_addr.length()) {
446 to_return.fill(ip_addr, machine, port);