2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2004-06-16 19:21:11 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#include "cpp.h"
|
|
|
|
|
2004-06-16 19:21:11 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2005-12-16 03:45:01 +00:00
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2004-06-16 19:21:11 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-04-08 11:00:45 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif
|
|
|
|
|
2007-02-21 16:26:51 +00:00
|
|
|
#include "str_util.h"
|
2009-06-16 20:54:44 +00:00
|
|
|
#include "str_replace.h"
|
2004-06-16 19:21:11 +00:00
|
|
|
#include "parse.h"
|
2008-11-05 20:11:45 +00:00
|
|
|
#include "util.h"
|
2004-06-17 17:00:14 +00:00
|
|
|
#include "file_names.h"
|
2004-06-16 19:21:11 +00:00
|
|
|
#include "client_msgs.h"
|
|
|
|
#include "error_numbers.h"
|
|
|
|
|
|
|
|
#include "hostinfo.h"
|
|
|
|
|
2004-06-17 17:00:14 +00:00
|
|
|
// get domain name and IP address of this host
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2006-06-23 20:41:47 +00:00
|
|
|
int HOST_INFO::get_local_network_info() {
|
2010-09-15 17:41:25 +00:00
|
|
|
struct sockaddr_storage s;
|
2006-04-08 11:00:45 +00:00
|
|
|
|
2007-08-11 03:34:38 +00:00
|
|
|
strcpy(domain_name, "");
|
|
|
|
strcpy(ip_addr, "");
|
2007-12-27 21:40:20 +00:00
|
|
|
|
|
|
|
// it seems like we should use getdomainname() instead of gethostname(),
|
|
|
|
// but on FC6 it returns "(none)".
|
|
|
|
//
|
2010-09-15 17:41:25 +00:00
|
|
|
if (gethostname(domain_name, 256)) {
|
2004-06-16 19:21:11 +00:00
|
|
|
return ERR_GETHOSTBYNAME;
|
|
|
|
}
|
2010-09-15 17:41:25 +00:00
|
|
|
int retval = resolve_hostname(domain_name, s);
|
|
|
|
if (retval) return retval;
|
2010-09-15 20:33:44 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)&s;
|
|
|
|
strlcpy(ip_addr, inet_ntoa(sin->sin_addr), sizeof(ip_addr));
|
|
|
|
#else
|
2010-09-15 17:41:25 +00:00
|
|
|
if (s.ss_family == AF_INET) {
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)&s;
|
|
|
|
inet_ntop(AF_INET, (void*)(&sin->sin_addr), ip_addr, 256);
|
|
|
|
} else {
|
|
|
|
sockaddr_in6* sin = (sockaddr_in6*)&s;
|
|
|
|
inet_ntop(AF_INET6, (void*)(&sin->sin6_addr), ip_addr, 256);
|
|
|
|
}
|
2010-09-15 20:33:44 +00:00
|
|
|
#endif
|
2004-06-16 19:21:11 +00:00
|
|
|
return 0;
|
2004-06-17 17:54:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 00:08:40 +00:00
|
|
|
// make a random string using time of day and host info.
|
2008-11-05 20:11:45 +00:00
|
|
|
// Not recommended for password generation;
|
|
|
|
// use as a last resort if more secure methods fail
|
|
|
|
//
|
|
|
|
void HOST_INFO::make_random_string(const char* salt, char* out) {
|
|
|
|
char buf[1024];
|
|
|
|
|
2012-02-16 00:08:40 +00:00
|
|
|
sprintf(buf, "%.15e%s%s%f%s", dtime(), domain_name, ip_addr, d_free, salt);
|
2008-11-05 20:11:45 +00:00
|
|
|
md5_block((const unsigned char*) buf, (int)strlen(buf), out);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a host cross-project ID.
|
|
|
|
// Should be unique across hosts with very high probability
|
|
|
|
//
|
|
|
|
void HOST_INFO::generate_host_cpid() {
|
|
|
|
make_random_string("", host_cpid);
|
|
|
|
}
|
|
|
|
|