Modified the BOINC_GETSOCKOPT_TYPE macro so it determines the proper type of

parameter 5 to getsockopt() by using the compiler.  The macro was in danger of
becoming an ever expanding list of case statements, since some platforms use
socklen_t, others use size_t, and still others use int.  Some aren't even
consistent from OS rev to OS rev.

A macro BOINC_SOCKLEN_T is set in config.h to indicate the proper type.  This
is typedef to be boinc_socklen_t in lib/network.h.  The special cases for
__APPLE__ and WIN32 can probably be removed at a later time.  Places where
socklen_t was used have been changed to boinc_socklen_t.

Also added double inclusion protection to network.h

svn path=/trunk/boinc/; revision=6055
This commit is contained in:
Eric J. Korpela 2005-05-06 01:54:00 +00:00
parent 5fa308a43d
commit 90d49a7d91
5 changed files with 78 additions and 27 deletions

View File

@ -702,10 +702,10 @@ int GUI_RPC_CONN_SET::init() {
int one = 1;
setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, (char*)&one, 4);
retval = bind(lsock, (const sockaddr*)(&addr), (socklen_t)sizeof(addr));
retval = bind(lsock, (const sockaddr*)(&addr), (boinc_socklen_t)sizeof(addr));
if (retval) {
addr.sin_port = htons(GUI_RPC_PORT_ALT);
retval = bind(lsock, (const sockaddr*)(&addr), (socklen_t)sizeof(addr));
retval = bind(lsock, (const sockaddr*)(&addr), (boinc_socklen_t)sizeof(addr));
if (retval) {
msg_printf(NULL, MSG_ERROR, "GUI RPC bind failed: %d\n", retval);
boinc_close_socket(lsock);
@ -778,7 +778,7 @@ bool GUI_RPC_CONN_SET::poll(double) {
if (FD_ISSET(lsock, &read_fds)) {
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
boinc_socklen_t addr_len = sizeof(addr);
sock = accept(lsock, (struct sockaddr*)&addr, &addr_len);
int peer_ip = (int) ntohl(addr.sin_addr.s_addr);

View File

@ -77,13 +77,11 @@
#include "client_msgs.h"
#if defined(_WIN32)
typedef int socklen_t;
typedef int boinc_socklen_t;
#elif defined ( __APPLE__)
typedef int32_t socklen_t;
#elif !GETSOCKOPT_SOCKLEN_T
#ifndef socklen_t
typedef size_t socklen_t;
#endif
typedef int32_t boinc_socklen_t;
#else
typedef BOINC_SOCKLEN_T boinc_socklen_t;
#endif
// if an active transfer doesn't get any activity
@ -91,7 +89,7 @@ typedef size_t socklen_t;
#define NET_XFER_TIMEOUT 600
int get_socket_error(int fd) {
socklen_t intsize = sizeof(int);
boinc_socklen_t intsize = sizeof(int);
int n;
#ifdef WIN32
getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&n, &intsize);

View File

@ -162,7 +162,7 @@ void boinc_close_socket(int sock) {
}
int get_socket_error(int fd) {
socklen_t intsize = sizeof(int);
boinc_socklen_t intsize = sizeof(int);
int n;
#ifdef WIN32
getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&n, &intsize);

View File

@ -16,6 +16,8 @@
// 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
#ifndef _BOINC_NETWORK_H_
#define _BOINC_NETWORK_H_
extern int resolve_hostname(char* hostname, int& ip_addr, char* msg);
extern int boinc_socket(int& sock);
@ -24,12 +26,12 @@ extern void boinc_close_socket(int sock);
extern int get_socket_error(int fd);
#if defined(_WIN32)
typedef int socklen_t;
typedef int boinc_socklen_t;
#define SHUT_WR SD_SEND
#elif defined( __APPLE__)
typedef int32_t socklen_t;
#elif !defined(GETSOCKOPT_SOCKLEN_T) && !defined(_SOCKLEN_T_DECLARED) && !defined(socklen_t)
typedef size_t socklen_t;
typedef int32_t boinc_socklen_t;
#else
typedef BOINC_SOCKLEN_T boinc_socklen_t;
#endif
#define CONNECTED_STATE_NOT_CONNECTED 0
@ -45,3 +47,4 @@ extern int NetOpen();
extern void NetClose();
extern void NetCheck(bool hangup_if_dialed);
#endif
#endif

View File

@ -1,16 +1,66 @@
AC_DEFUN([BOINC_GETSOCKOPT_TYPE],[
dnl TODO: use compiler to test these; there probably exists an autoconf macro already!
AC_MSG_CHECKING(type of getsockopt() parameter four)
case "$target" in
*-linux* | *solaris* | *openbsd* )
AC_DEFINE(GETSOCKOPT_SOCKLEN_T, 1, [getsockopt uses socklen_t])
AC_MSG_RESULT(socklen_t)
;;
*sysv5OpenUNIX8* | *hpux* | i*86*cygwin* )
AC_DEFINE(GETSOCKOPT_SIZE_T, 1, [getsockopt uses size_t])
AC_MSG_RESULT(size_t)
;;
esac
AC_LANG_PUSH(C)
AC_CHECK_HEADERS([sys/socket.h])
if test "${ac_cv_header_sys_socket_h}" = "yes" ; then
ac_includes_default="${ac_includes_default}
#include <sys/socket.h>
"
fi
AC_CHECK_TYPES([socklen_t])
B_SV_CFLAGS="${CFLAGS}"
if test "${ac_cv_c_compiler_gnu}" = "yes" ; then
CFLAGS="${CFLAGS} -Werror -pedantic"
fi
AC_CACHE_CHECK([type of getsockopt() parameter five],
[boinc_cv_getsockopt_type],
[
if test "${ac_cv_type_socklen_t}" = "yes" ; then
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([[
#define CONFIG_TEST
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
]],
[
socklen_t l;
return getsockopt(0,0,0,(void *)0,&l);
])],
[
boinc_cv_getsockopt_type="socklen_t"
]
)
fi
if test -z "${boinc_cv_getsockopt_type}"; then
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([[
#define CONFIG_TEST
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
]],
[
size_t l;
return getsockopt(0,0,0,(void *)0,&l);
])],
[
boinc_cv_getsockopt_type="size_t"
],
[
boinc_cv_getsockopt_type="int"
]
)
fi
])
CFLAGS="${B_SV_CFLAGS}"
AC_DEFINE_UNQUOTED([BOINC_SOCKLEN_T],[${boinc_cv_getsockopt_type}],[Define to the type pointed to by the 5th parameter of getsockopt])
AC_LANG_POP(C)
])