2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-03-07 06:09:04 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2005-03-07 06:09:04 +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.
|
2005-03-07 06:09:04 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-03-07 06:09:04 +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.
|
|
|
|
//
|
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/>.
|
2005-03-07 06:09:04 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2005-07-14 16:46:38 +00:00
|
|
|
#include "boinc_win.h"
|
2010-09-15 20:33:44 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2009-02-26 00:23:23 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2005-03-07 21:19:09 +00:00
|
|
|
#include <unistd.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#endif
|
2005-04-08 23:40:50 +00:00
|
|
|
#include <cstdio>
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2005-03-07 21:19:09 +00:00
|
|
|
#include <sys/socket.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2005-04-10 00:16:20 +00:00
|
|
|
#include <netinet/in.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_TCP_H
|
2005-04-10 00:16:20 +00:00
|
|
|
#include <netinet/tcp.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2005-04-10 00:16:20 +00:00
|
|
|
#include <arpa/inet.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#endif
|
2009-03-31 16:45:17 +00:00
|
|
|
#include <resolv.h>
|
2005-04-10 00:16:20 +00:00
|
|
|
#include <netdb.h>
|
2005-04-08 23:40:50 +00:00
|
|
|
#include <fcntl.h>
|
2005-12-16 03:35:15 +00:00
|
|
|
#include <errno.h>
|
2005-03-07 21:19:09 +00:00
|
|
|
#endif
|
|
|
|
|
2009-02-26 00:23:23 +00:00
|
|
|
using std::perror;
|
|
|
|
using std::sprintf;
|
|
|
|
|
2005-04-08 23:40:50 +00:00
|
|
|
#include "error_numbers.h"
|
2005-03-07 06:09:04 +00:00
|
|
|
#include "network.h"
|
|
|
|
|
2005-06-07 21:46:11 +00:00
|
|
|
const char* socket_error_str() {
|
2005-05-14 19:45:24 +00:00
|
|
|
static char buf[80];
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2005-05-14 19:45:24 +00:00
|
|
|
int e = WSAGetLastError();
|
|
|
|
switch (e) {
|
|
|
|
case WSANOTINITIALISED:
|
|
|
|
return "WSA not initialized";
|
|
|
|
case WSAENETDOWN:
|
|
|
|
return "the network subsystem has failed";
|
|
|
|
case WSAHOST_NOT_FOUND:
|
|
|
|
return "host name not found";
|
|
|
|
case WSATRY_AGAIN:
|
|
|
|
return "no response from server";
|
|
|
|
case WSANO_RECOVERY:
|
|
|
|
return "a nonrecoverable error occurred";
|
|
|
|
case WSANO_DATA:
|
|
|
|
return "valid name, no data record of requested type";
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
return "a blocking socket call in progress";
|
|
|
|
case WSAEFAULT:
|
|
|
|
return "invalid part of user address space";
|
|
|
|
case WSAEINTR:
|
|
|
|
return "a blocking socket call was canceled";
|
|
|
|
case WSAENOTSOCK:
|
|
|
|
return "not a socket";
|
|
|
|
}
|
|
|
|
sprintf(buf, "error %d", e);
|
|
|
|
return buf;
|
|
|
|
#else
|
|
|
|
switch (h_errno) {
|
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
return "host not found";
|
|
|
|
case NO_DATA:
|
|
|
|
return "valid name, no data record of requested type";
|
|
|
|
case NO_RECOVERY:
|
|
|
|
return "a nonrecoverable error occurred";
|
|
|
|
case TRY_AGAIN:
|
|
|
|
return "host not found or server failure";
|
2005-12-16 03:35:15 +00:00
|
|
|
#ifdef NETDB_INTERNAL
|
|
|
|
case NETDB_INTERNAL:
|
2010-05-31 21:51:09 +00:00
|
|
|
sprintf(buf,"network internal error %d",errno);
|
|
|
|
return buf;
|
2005-12-16 03:35:15 +00:00
|
|
|
#endif
|
2005-05-14 19:45:24 +00:00
|
|
|
}
|
|
|
|
sprintf(buf, "error %d", h_errno);
|
|
|
|
return buf;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-09-15 17:41:25 +00:00
|
|
|
bool is_localhost(sockaddr_storage& s) {
|
2010-09-15 20:33:44 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (ntohl(s.sin_addr.s_addr) == 0x7f000001) return true;
|
|
|
|
#else
|
2010-09-15 17:41:25 +00:00
|
|
|
switch (s.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)&s;
|
|
|
|
return (ntohl(sin->sin_addr.s_addr) == 0x7f000001);
|
|
|
|
}
|
2010-09-15 20:33:44 +00:00
|
|
|
|
2010-09-15 17:41:25 +00:00
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
sockaddr_in6* sin = (sockaddr_in6*)&s;
|
|
|
|
char buf[256];
|
|
|
|
inet_ntop(AF_INET6, (void*)(&sin->sin6_addr), buf, 256);
|
|
|
|
return (strcmp(buf, "::1") == 0);
|
|
|
|
}
|
2010-09-15 20:33:44 +00:00
|
|
|
|
2010-09-15 17:41:25 +00:00
|
|
|
}
|
2010-09-15 20:33:44 +00:00
|
|
|
#endif
|
2010-09-15 17:41:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool same_ip_addr(sockaddr_storage& s1, sockaddr_storage& s2) {
|
2010-09-15 20:33:44 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return (s1.sin_addr.s_addr == s2.sin_addr.s_addr);
|
|
|
|
#else
|
2010-09-15 17:41:25 +00:00
|
|
|
if (s1.ss_family != s2.ss_family) return false;
|
|
|
|
switch (s1.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
sockaddr_in* sin1 = (sockaddr_in*)&s1;
|
|
|
|
sockaddr_in* sin2 = (sockaddr_in*)&s2;
|
|
|
|
return (memcmp((void*)(&sin1->sin_addr), (void*)(&sin2->sin_addr), sizeof(in_addr)) == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
sockaddr_in6* sin1 = (sockaddr_in6*)&s1;
|
|
|
|
sockaddr_in6* sin2 = (sockaddr_in6*)&s2;
|
|
|
|
return (memcmp((void*)(&sin1->sin6_addr), (void*)(&sin2->sin6_addr), sizeof(in6_addr)) == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-09-15 20:33:44 +00:00
|
|
|
#endif
|
2010-09-15 17:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int resolve_hostname(const char* hostname, sockaddr_storage &ip_addr) {
|
2010-09-15 20:33:44 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
hostent* hep;
|
|
|
|
hep = gethostbyname(hostname);
|
|
|
|
if (!hep) {
|
|
|
|
return ERR_GETHOSTBYNAME;
|
|
|
|
}
|
|
|
|
ip_addr.sin_family = AF_INET;
|
|
|
|
ip_addr.sin_addr.s_addr = *(int*)hep->h_addr_list[0];
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
2010-09-15 17:41:25 +00:00
|
|
|
struct addrinfo *res, hints;
|
2005-04-10 00:16:20 +00:00
|
|
|
|
2010-09-15 17:41:25 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2010-10-07 18:08:20 +00:00
|
|
|
hints.ai_family = AF_INET;
|
2010-09-15 17:41:25 +00:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
int retval = getaddrinfo(hostname, NULL, &hints, &res);
|
|
|
|
if (retval) {
|
|
|
|
perror("getaddrinfo");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
memcpy(&ip_addr, res->ai_addr, res->ai_addrlen);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return 0;
|
2010-09-15 20:33:44 +00:00
|
|
|
#endif
|
2010-09-15 17:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int resolve_hostname_or_ip_addr(const char* hostname, sockaddr_storage &ip_addr) {
|
2010-09-15 20:33:44 +00:00
|
|
|
#ifdef _WIN32 // inet_pton() only on Vista or later!!
|
|
|
|
int x = inet_addr(hostname);
|
|
|
|
if (x != -1) {
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)&ip_addr;
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = x;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2010-09-15 17:41:25 +00:00
|
|
|
int retval;
|
|
|
|
// check for IPV4 and IPV6 notation
|
2005-04-10 00:16:20 +00:00
|
|
|
//
|
2010-09-15 17:41:25 +00:00
|
|
|
sockaddr_in* sin = (sockaddr_in*)&ip_addr;
|
|
|
|
retval = inet_pton(AF_INET, hostname, &sin->sin_addr);
|
|
|
|
if (retval > 0) {
|
|
|
|
ip_addr.ss_family = AF_INET;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sockaddr_in6* sin6 = (sockaddr_in6*)&ip_addr;
|
|
|
|
retval = inet_pton(AF_INET6, hostname, &sin6->sin6_addr);
|
|
|
|
if (retval > 0) {
|
|
|
|
ip_addr.ss_family = AF_INET6;
|
2005-04-10 00:16:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-09-15 20:33:44 +00:00
|
|
|
#endif
|
2005-04-10 00:16:20 +00:00
|
|
|
|
|
|
|
// else resolve the name
|
|
|
|
//
|
2010-09-15 17:41:25 +00:00
|
|
|
return resolve_hostname(hostname, ip_addr);
|
2005-04-10 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
2005-04-08 23:40:50 +00:00
|
|
|
int boinc_socket(int& fd) {
|
2005-07-23 07:24:51 +00:00
|
|
|
fd = (int)socket(AF_INET, SOCK_STREAM, 0);
|
2005-04-08 23:40:50 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return ERR_SOCKET;
|
|
|
|
}
|
2005-12-18 02:00:15 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
2005-04-08 23:40:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_socket_asynch(int fd, bool asynch) {
|
|
|
|
if (asynch) {
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2005-04-08 23:40:50 +00:00
|
|
|
unsigned long one = 1;
|
|
|
|
ioctlsocket(fd, FIONBIO, &one);
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (flags < 0) {
|
|
|
|
return ERR_FCNTL;
|
|
|
|
}
|
|
|
|
if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0 ) {
|
|
|
|
return ERR_FCNTL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2005-04-08 23:40:50 +00:00
|
|
|
unsigned long zero = 0;
|
|
|
|
ioctlsocket(fd, FIONBIO, &zero);
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (flags < 0) {
|
|
|
|
return ERR_FCNTL;
|
|
|
|
}
|
|
|
|
if (fcntl(fd, F_SETFL, flags&(~O_NONBLOCK)) < 0 ) {
|
|
|
|
return ERR_FCNTL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-07 06:09:04 +00:00
|
|
|
void boinc_close_socket(int sock) {
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2005-03-07 06:09:04 +00:00
|
|
|
closesocket(sock);
|
|
|
|
#else
|
|
|
|
close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_socket_error(int fd) {
|
|
|
|
int n;
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2009-06-16 21:58:38 +00:00
|
|
|
int intsize = sizeof(int);
|
2005-03-07 06:09:04 +00:00
|
|
|
getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&n, &intsize);
|
2005-05-05 18:59:55 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
// workaround for FreeBSD. I don't understand this.
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
socklen_t sinsz = sizeof(sin);
|
|
|
|
n = getpeername(fd, (struct sockaddr *)&sin, &sinsz);
|
2005-03-07 06:09:04 +00:00
|
|
|
#else
|
2009-06-16 21:58:38 +00:00
|
|
|
socklen_t intsize = sizeof(int);
|
2006-01-16 06:23:14 +00:00
|
|
|
getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, (socklen_t*)&intsize);
|
2005-03-07 06:09:04 +00:00
|
|
|
#endif
|
|
|
|
return n;
|
|
|
|
}
|
2005-03-29 23:56:50 +00:00
|
|
|
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && defined(USE_WINSOCK)
|
2005-03-29 23:56:50 +00:00
|
|
|
|
2005-06-22 10:51:34 +00:00
|
|
|
int WinsockInitialize() {
|
|
|
|
WSADATA wsdata;
|
2010-05-31 21:51:09 +00:00
|
|
|
return WSAStartup(MAKEWORD(2, 0), &wsdata);
|
2005-03-29 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
2005-06-22 10:51:34 +00:00
|
|
|
int WinsockCleanup() {
|
|
|
|
return WSACleanup();
|
2005-03-29 23:56:50 +00:00
|
|
|
}
|
2005-03-30 00:01:34 +00:00
|
|
|
|
2009-03-31 16:45:17 +00:00
|
|
|
#endif
|
2005-03-30 00:01:34 +00:00
|
|
|
|
2009-04-12 15:30:45 +00:00
|
|
|
void reset_dns() {
|
2009-05-07 13:54:51 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
|
|
|
// Windows doesn't have this, and it crashes Macs
|
2009-04-12 15:30:45 +00:00
|
|
|
res_init();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|