2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2004-06-16 19:21:11 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-06-16 19:21:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
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
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "parse.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
|
|
|
//
|
2004-06-17 17:00:14 +00:00
|
|
|
int get_local_network_info(
|
|
|
|
char* domain_name, int domlen, char* ip_addr, int iplen
|
|
|
|
) {
|
2004-06-16 19:21:11 +00:00
|
|
|
char buf[256];
|
2004-06-17 17:09:03 +00:00
|
|
|
struct in_addr addr;
|
2004-06-16 19:21:11 +00:00
|
|
|
|
|
|
|
if (gethostname(buf, 256)) return ERR_GETHOSTBYNAME;
|
|
|
|
struct hostent* he = gethostbyname(buf);
|
|
|
|
if (!he || !he->h_addr_list[0]) {
|
2004-06-17 17:09:03 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "get_local_network_info(): gethostbyname failed\n");
|
2004-06-16 19:21:11 +00:00
|
|
|
return ERR_GETHOSTBYNAME;
|
|
|
|
}
|
2004-06-17 17:09:03 +00:00
|
|
|
safe_strncpy(domain_name, he->h_name, domlen);
|
2004-06-16 19:21:11 +00:00
|
|
|
memcpy(&addr, he->h_addr_list[0], sizeof(addr));
|
2004-06-17 17:00:14 +00:00
|
|
|
safe_strncpy(ip_addr, inet_ntoa(addr), iplen);
|
2004-06-16 19:21:11 +00:00
|
|
|
return 0;
|
2004-06-17 17:54:05 +00:00
|
|
|
}
|
|
|
|
|
2004-06-19 00:41:58 +00:00
|
|
|
#if 0
|
2004-06-16 19:21:11 +00:00
|
|
|
|
2004-06-18 19:08:42 +00:00
|
|
|
// NEW POLICY: on UNIX, get just the hostname.
|
|
|
|
// the rest (domain name, IP address) is a can of worms,
|
|
|
|
// and doesn't really matter anyway
|
|
|
|
|
|
|
|
int get_local_network_info(
|
|
|
|
char* domain_name, int domlen, char* ip_addr, int iplen
|
|
|
|
) {
|
|
|
|
char hostname[256];
|
|
|
|
char buf[256];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
strcpy(domain_name, "unknown");
|
|
|
|
strcpy(ip_addr, "unknown");
|
|
|
|
if (!gethostname(hostname, 256)) {
|
|
|
|
safe_strncpy(domain_name, hostname, domlen);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2004-06-17 17:00:14 +00:00
|
|
|
// gethostbyname() is a linkage nightmare on UNIX systems (go figure)
|
2004-06-18 05:08:57 +00:00
|
|
|
// so use a kludge instead: run ping and parse the output.
|
|
|
|
// The output should have a line like
|
|
|
|
// "9 bytes from a.b.c.d (1.0.1.0): icmp_seq=0"
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2004-06-17 17:54:05 +00:00
|
|
|
static int try_ping(
|
|
|
|
char* cmd, char* domain_name, int domlen, char* ip_addr, int iplen
|
|
|
|
) {
|
2004-06-18 05:08:57 +00:00
|
|
|
int retval,n;
|
2004-06-17 17:54:05 +00:00
|
|
|
char buf[256];
|
2004-06-18 05:08:57 +00:00
|
|
|
char *p, *q;
|
2004-06-16 19:21:11 +00:00
|
|
|
|
2004-06-17 21:08:27 +00:00
|
|
|
retval = system(cmd);
|
2004-06-16 19:21:11 +00:00
|
|
|
if (retval) return retval;
|
2004-06-17 17:00:14 +00:00
|
|
|
FILE* f = fopen(TEMP_FILE_NAME, "r");
|
|
|
|
if (!f) return ERR_FOPEN;
|
2004-06-18 05:08:57 +00:00
|
|
|
while (fgets(buf, 256, f)) {
|
|
|
|
p = strchr(buf, '(');
|
|
|
|
if (!p) continue;
|
|
|
|
q = strchr(p, ')');
|
|
|
|
if (!q) continue;
|
|
|
|
*q = 0;
|
|
|
|
if (!strchr(p, '.')) continue;
|
|
|
|
safe_strncpy(ip_addr, p+1, iplen);
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
p = strrchr(buf, ' ');
|
|
|
|
if (!p) continue;
|
|
|
|
*p = 0;
|
|
|
|
p = strrchr(buf, ' ');
|
|
|
|
if (!p) continue;
|
|
|
|
p++;
|
|
|
|
if (!strchr(p, '.')) continue;
|
2004-06-18 19:08:42 +00:00
|
|
|
safe_strncpy(domain_name, p, domlen);
|
2004-06-18 05:08:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ERR_NULL;
|
2004-06-16 19:21:11 +00:00
|
|
|
}
|
2004-06-17 17:54:05 +00:00
|
|
|
|
|
|
|
int get_local_network_info(
|
|
|
|
char* domain_name, int domlen, char* ip_addr, int iplen
|
|
|
|
) {
|
|
|
|
char hostname[256];
|
|
|
|
char buf[256];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (gethostname(hostname, 256)) return ERR_GETHOSTBYNAME;
|
2004-06-18 05:08:57 +00:00
|
|
|
|
2004-06-17 23:27:35 +00:00
|
|
|
sprintf(buf, "ping -c 1 -w 1 %s > %s 2>/dev/null", hostname, TEMP_FILE_NAME);
|
2004-06-17 17:54:05 +00:00
|
|
|
retval = try_ping(buf, domain_name, domlen, ip_addr, iplen);
|
2004-06-18 05:08:57 +00:00
|
|
|
if (!retval) return 0;
|
|
|
|
|
|
|
|
sprintf(buf, "ping -c 1 %s > %s 2>/dev/null", hostname, TEMP_FILE_NAME);
|
|
|
|
retval = try_ping(buf, domain_name, domlen, ip_addr, iplen);
|
|
|
|
if (!retval) return 0;
|
|
|
|
|
|
|
|
sprintf(buf, "/usr/sbin/ping -s %s 1 1 > %s 2>/dev/null", hostname, TEMP_FILE_NAME);
|
|
|
|
retval = try_ping(buf, domain_name, domlen, ip_addr, iplen);
|
|
|
|
if (!retval) return 0;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2004-06-18 05:08:57 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "Couldn't get hostname and IP address");
|
|
|
|
msg_printf(NULL, MSG_INFO, "Make sure 'ping' is in your search path");
|
|
|
|
|
|
|
|
strcpy(domain_name, "unknown");
|
|
|
|
strcpy(ip_addr, "0.0.0.0");
|
|
|
|
|
2004-06-17 17:54:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-06-18 19:08:42 +00:00
|
|
|
#endif
|
2004-06-17 17:54:05 +00:00
|
|
|
|
|
|
|
#endif
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_9275b20aa5 = "$Id$";
|