1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef BOINC_NETWORK_H
19 #define BOINC_NETWORK_H
20 
21 #include <string.h>
22 #ifdef _WIN32
23 #include "boinc_win.h"
24 // WxWidgets can't deal with modern network code (winsock2.h)
25 // so use old code on Win
26 #define sockaddr_storage sockaddr_in
27 #else
28 #include <sys/select.h>
29 #include <sys/socket.h>
30 #include <unistd.h>
31 #endif
32 
33 struct FDSET_GROUP {
34     fd_set read_fds;
35     fd_set write_fds;
36     fd_set exc_fds;
37     int max_fd;
38 
zeroFDSET_GROUP39     void zero() {
40         FD_ZERO(&read_fds);
41         FD_ZERO(&write_fds);
42         FD_ZERO(&exc_fds);
43         max_fd = -1;
44     }
45 };
46 
47 extern bool is_localhost(sockaddr_storage& s);
48 extern bool same_ip_addr(sockaddr_storage& s1, sockaddr_storage& s2);
49 extern int resolve_hostname(const char* hostname, sockaddr_storage& ip_addr);
50 extern int resolve_hostname_or_ip_addr(const char* hostname, sockaddr_storage& ip_addr);
51 extern int boinc_socket(int& sock, int protocol=AF_INET);
52     // create a stream-mode socket of the given family
53 extern int boinc_socket_asynch(int sock, bool asynch);
54     // make an existing socket asynchronous or synchronous
55 extern void boinc_close_socket(int sock);
56 extern int get_socket_error(int fd);
57 extern const char* socket_error_str();
58 extern void reset_dns();
59 extern int boinc_get_port(bool is_remote, int& port);
60 
61 #if defined(_WIN32) && !defined(__CYGWIN32__)
62 typedef int BOINC_SOCKLEN_T;
63 #else
64 typedef socklen_t BOINC_SOCKLEN_T;
65 #endif
66 
67 #if defined(_WIN32) && defined(USE_WINSOCK)
68 extern int WinsockInitialize();
69 extern int WinsockCleanup();
70 #endif
71 #endif
72