andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * This file defines _PR_MapOptionName(). The purpose of putting andre@0: * _PR_MapOptionName() in a separate file is to work around a Winsock andre@0: * header file problem on Windows NT. andre@0: * andre@0: * On Windows NT, if we define _WIN32_WINNT to be 0x0400 (in order andre@0: * to use Service Pack 3 extensions), windows.h includes winsock2.h andre@0: * (instead of winsock.h), which doesn't define many socket options andre@0: * defined in winsock.h. andre@0: * andre@0: * We need the socket options defined in winsock.h. So this file andre@0: * includes winsock.h, with _WIN32_WINNT undefined. andre@0: */ andre@0: andre@0: #if defined(WINNT) || defined(__MINGW32__) andre@0: #include andre@0: #endif andre@0: andre@0: /* MinGW doesn't define these in its winsock.h. */ andre@0: #ifdef __MINGW32__ andre@0: #ifndef IP_TTL andre@0: #define IP_TTL 7 andre@0: #endif andre@0: #ifndef IP_TOS andre@0: #define IP_TOS 8 andre@0: #endif andre@0: #endif andre@0: andre@0: #include "primpl.h" andre@0: andre@0: #ifdef HAVE_NETINET_TCP_H andre@0: #include /* TCP_NODELAY, TCP_MAXSEG */ andre@0: #endif andre@0: andre@0: #ifndef _PR_PTHREADS andre@0: andre@0: PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionData *data) andre@0: { andre@0: PRStatus rv; andre@0: PRInt32 length; andre@0: PRInt32 level, name; andre@0: andre@0: /* andre@0: * PR_SockOpt_Nonblocking is a special case that does not andre@0: * translate to a getsockopt() call andre@0: */ andre@0: if (PR_SockOpt_Nonblocking == data->option) andre@0: { andre@0: data->value.non_blocking = fd->secret->nonblocking; andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: rv = _PR_MapOptionName(data->option, &level, &name); andre@0: if (PR_SUCCESS == rv) andre@0: { andre@0: switch (data->option) andre@0: { andre@0: case PR_SockOpt_Linger: andre@0: { andre@0: #if !defined(XP_BEOS) || defined(BONE_VERSION) andre@0: struct linger linger; andre@0: length = sizeof(linger); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char *) &linger, &length); andre@0: if (PR_SUCCESS == rv) andre@0: { andre@0: PR_ASSERT(sizeof(linger) == length); andre@0: data->value.linger.polarity = andre@0: (linger.l_onoff) ? PR_TRUE : PR_FALSE; andre@0: data->value.linger.linger = andre@0: PR_SecondsToInterval(linger.l_linger); andre@0: } andre@0: break; andre@0: #else andre@0: PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 ); andre@0: return PR_FAILURE; andre@0: #endif andre@0: } andre@0: case PR_SockOpt_Reuseaddr: andre@0: case PR_SockOpt_Keepalive: andre@0: case PR_SockOpt_NoDelay: andre@0: case PR_SockOpt_Broadcast: andre@0: { andre@0: #ifdef WIN32 /* Winsock */ andre@0: BOOL value; andre@0: #else andre@0: PRIntn value; andre@0: #endif andre@0: length = sizeof(value); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char*)&value, &length); andre@0: if (PR_SUCCESS == rv) andre@0: data->value.reuse_addr = (0 == value) ? PR_FALSE : PR_TRUE; andre@0: break; andre@0: } andre@0: case PR_SockOpt_McastLoopback: andre@0: { andre@0: #ifdef WIN32 /* Winsock */ andre@0: BOOL bool; andre@0: #else andre@0: PRUint8 bool; andre@0: #endif andre@0: length = sizeof(bool); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char*)&bool, &length); andre@0: if (PR_SUCCESS == rv) andre@0: data->value.mcast_loopback = (0 == bool) ? PR_FALSE : PR_TRUE; andre@0: break; andre@0: } andre@0: case PR_SockOpt_RecvBufferSize: andre@0: case PR_SockOpt_SendBufferSize: andre@0: case PR_SockOpt_MaxSegment: andre@0: { andre@0: PRIntn value; andre@0: length = sizeof(value); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char*)&value, &length); andre@0: if (PR_SUCCESS == rv) andre@0: data->value.recv_buffer_size = value; andre@0: break; andre@0: } andre@0: case PR_SockOpt_IpTimeToLive: andre@0: case PR_SockOpt_IpTypeOfService: andre@0: { andre@0: /* These options should really be an int (or PRIntn). */ andre@0: length = sizeof(PRUintn); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char*)&data->value.ip_ttl, &length); andre@0: break; andre@0: } andre@0: case PR_SockOpt_McastTimeToLive: andre@0: { andre@0: #ifdef WIN32 /* Winsock */ andre@0: int ttl; andre@0: #else andre@0: PRUint8 ttl; andre@0: #endif andre@0: length = sizeof(ttl); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char*)&ttl, &length); andre@0: if (PR_SUCCESS == rv) andre@0: data->value.mcast_ttl = ttl; andre@0: break; andre@0: } andre@0: #ifdef IP_ADD_MEMBERSHIP andre@0: case PR_SockOpt_AddMember: andre@0: case PR_SockOpt_DropMember: andre@0: { andre@0: struct ip_mreq mreq; andre@0: length = sizeof(mreq); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, (char*)&mreq, &length); andre@0: if (PR_SUCCESS == rv) andre@0: { andre@0: data->value.add_member.mcaddr.inet.ip = andre@0: mreq.imr_multiaddr.s_addr; andre@0: data->value.add_member.ifaddr.inet.ip = andre@0: mreq.imr_interface.s_addr; andre@0: } andre@0: break; andre@0: } andre@0: #endif /* IP_ADD_MEMBERSHIP */ andre@0: case PR_SockOpt_McastInterface: andre@0: { andre@0: /* This option is a struct in_addr. */ andre@0: length = sizeof(data->value.mcast_if.inet.ip); andre@0: rv = _PR_MD_GETSOCKOPT( andre@0: fd, level, name, andre@0: (char*)&data->value.mcast_if.inet.ip, &length); andre@0: break; andre@0: } andre@0: default: andre@0: PR_NOT_REACHED("Unknown socket option"); andre@0: break; andre@0: } andre@0: } andre@0: return rv; andre@0: } /* _PR_SocketGetSocketOption */ andre@0: andre@0: PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOptionData *data) andre@0: { andre@0: PRStatus rv; andre@0: PRInt32 level, name; andre@0: andre@0: /* andre@0: * PR_SockOpt_Nonblocking is a special case that does not andre@0: * translate to a setsockopt call. andre@0: */ andre@0: if (PR_SockOpt_Nonblocking == data->option) andre@0: { andre@0: #ifdef WINNT andre@0: PR_ASSERT((fd->secret->md.io_model_committed == PR_FALSE) andre@0: || (fd->secret->nonblocking == data->value.non_blocking)); andre@0: if (fd->secret->md.io_model_committed andre@0: && (fd->secret->nonblocking != data->value.non_blocking)) andre@0: { andre@0: /* andre@0: * On NT, once we have associated a socket with the io andre@0: * completion port, we can't disassociate it. So we andre@0: * can't change the nonblocking option of the socket andre@0: * afterwards. andre@0: */ andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: #endif andre@0: fd->secret->nonblocking = data->value.non_blocking; andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: rv = _PR_MapOptionName(data->option, &level, &name); andre@0: if (PR_SUCCESS == rv) andre@0: { andre@0: switch (data->option) andre@0: { andre@0: case PR_SockOpt_Linger: andre@0: { andre@0: #if !defined(XP_BEOS) || defined(BONE_VERSION) andre@0: struct linger linger; andre@0: linger.l_onoff = data->value.linger.polarity; andre@0: linger.l_linger = PR_IntervalToSeconds(data->value.linger.linger); andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&linger, sizeof(linger)); andre@0: break; andre@0: #else andre@0: PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 ); andre@0: return PR_FAILURE; andre@0: #endif andre@0: } andre@0: case PR_SockOpt_Reuseaddr: andre@0: case PR_SockOpt_Keepalive: andre@0: case PR_SockOpt_NoDelay: andre@0: case PR_SockOpt_Broadcast: andre@0: { andre@0: #ifdef WIN32 /* Winsock */ andre@0: BOOL value; andre@0: #else andre@0: PRIntn value; andre@0: #endif andre@0: value = (data->value.reuse_addr) ? 1 : 0; andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&value, sizeof(value)); andre@0: break; andre@0: } andre@0: case PR_SockOpt_McastLoopback: andre@0: { andre@0: #ifdef WIN32 /* Winsock */ andre@0: BOOL bool; andre@0: #else andre@0: PRUint8 bool; andre@0: #endif andre@0: bool = data->value.mcast_loopback ? 1 : 0; andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&bool, sizeof(bool)); andre@0: break; andre@0: } andre@0: case PR_SockOpt_RecvBufferSize: andre@0: case PR_SockOpt_SendBufferSize: andre@0: case PR_SockOpt_MaxSegment: andre@0: { andre@0: PRIntn value = data->value.recv_buffer_size; andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&value, sizeof(value)); andre@0: break; andre@0: } andre@0: case PR_SockOpt_IpTimeToLive: andre@0: case PR_SockOpt_IpTypeOfService: andre@0: { andre@0: /* These options should really be an int (or PRIntn). */ andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&data->value.ip_ttl, sizeof(PRUintn)); andre@0: break; andre@0: } andre@0: case PR_SockOpt_McastTimeToLive: andre@0: { andre@0: #ifdef WIN32 /* Winsock */ andre@0: int ttl; andre@0: #else andre@0: PRUint8 ttl; andre@0: #endif andre@0: ttl = data->value.mcast_ttl; andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&ttl, sizeof(ttl)); andre@0: break; andre@0: } andre@0: #ifdef IP_ADD_MEMBERSHIP andre@0: case PR_SockOpt_AddMember: andre@0: case PR_SockOpt_DropMember: andre@0: { andre@0: struct ip_mreq mreq; andre@0: mreq.imr_multiaddr.s_addr = andre@0: data->value.add_member.mcaddr.inet.ip; andre@0: mreq.imr_interface.s_addr = andre@0: data->value.add_member.ifaddr.inet.ip; andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&mreq, sizeof(mreq)); andre@0: break; andre@0: } andre@0: #endif /* IP_ADD_MEMBERSHIP */ andre@0: case PR_SockOpt_McastInterface: andre@0: { andre@0: /* This option is a struct in_addr. */ andre@0: rv = _PR_MD_SETSOCKOPT( andre@0: fd, level, name, (char*)&data->value.mcast_if.inet.ip, andre@0: sizeof(data->value.mcast_if.inet.ip)); andre@0: break; andre@0: } andre@0: default: andre@0: PR_NOT_REACHED("Unknown socket option"); andre@0: break; andre@0: } andre@0: } andre@0: return rv; andre@0: } /* _PR_SocketSetSocketOption */ andre@0: andre@0: #endif /* ! _PR_PTHREADS */ andre@0: andre@0: /* andre@0: ********************************************************************* andre@0: ********************************************************************* andre@0: ** andre@0: ** Make sure that the following is at the end of this file, andre@0: ** because we will be playing with macro redefines. andre@0: ** andre@0: ********************************************************************* andre@0: ********************************************************************* andre@0: */ andre@0: andre@0: /* andre@0: * Not every platform has all the socket options we want to andre@0: * support. Some older operating systems such as SunOS 4.1.3 andre@0: * don't have the IP multicast socket options. Win32 doesn't andre@0: * have TCP_MAXSEG. andre@0: * andre@0: * To deal with this problem, we define the missing socket andre@0: * options as _PR_NO_SUCH_SOCKOPT. _PR_MapOptionName() fails with andre@0: * PR_OPERATION_NOT_SUPPORTED_ERROR if a socket option not andre@0: * available on the platform is requested. andre@0: */ andre@0: andre@0: /* andre@0: * Sanity check. SO_LINGER and TCP_NODELAY should be available andre@0: * on all platforms. Just to make sure we have included the andre@0: * appropriate header files. Then any undefined socket options andre@0: * are really missing. andre@0: */ andre@0: andre@0: #if !defined(SO_LINGER) andre@0: #error "SO_LINGER is not defined" andre@0: #endif andre@0: andre@0: #if !defined(TCP_NODELAY) andre@0: #error "TCP_NODELAY is not defined" andre@0: #endif andre@0: andre@0: /* andre@0: * Make sure the value of _PR_NO_SUCH_SOCKOPT is not andre@0: * a valid socket option. andre@0: */ andre@0: #define _PR_NO_SUCH_SOCKOPT -1 andre@0: andre@0: #ifndef SO_KEEPALIVE andre@0: #define SO_KEEPALIVE _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef SO_SNDBUF andre@0: #define SO_SNDBUF _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef SO_RCVBUF andre@0: #define SO_RCVBUF _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_MULTICAST_IF /* set/get IP multicast interface */ andre@0: #define IP_MULTICAST_IF _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_MULTICAST_TTL /* set/get IP multicast timetolive */ andre@0: #define IP_MULTICAST_TTL _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_MULTICAST_LOOP /* set/get IP multicast loopback */ andre@0: #define IP_MULTICAST_LOOP _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_ADD_MEMBERSHIP /* add an IP group membership */ andre@0: #define IP_ADD_MEMBERSHIP _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_DROP_MEMBERSHIP /* drop an IP group membership */ andre@0: #define IP_DROP_MEMBERSHIP _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_TTL /* set/get IP Time To Live */ andre@0: #define IP_TTL _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef IP_TOS /* set/get IP Type Of Service */ andre@0: #define IP_TOS _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef TCP_NODELAY /* don't delay to coalesce data */ andre@0: #define TCP_NODELAY _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef TCP_MAXSEG /* maxumum segment size for tcp */ andre@0: #define TCP_MAXSEG _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: #ifndef SO_BROADCAST /* enable broadcast on udp sockets */ andre@0: #define SO_BROADCAST _PR_NO_SUCH_SOCKOPT andre@0: #endif andre@0: andre@0: PRStatus _PR_MapOptionName( andre@0: PRSockOption optname, PRInt32 *level, PRInt32 *name) andre@0: { andre@0: static PRInt32 socketOptions[PR_SockOpt_Last] = andre@0: { andre@0: 0, SO_LINGER, SO_REUSEADDR, SO_KEEPALIVE, SO_RCVBUF, SO_SNDBUF, andre@0: IP_TTL, IP_TOS, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, andre@0: IP_MULTICAST_IF, IP_MULTICAST_TTL, IP_MULTICAST_LOOP, andre@0: TCP_NODELAY, TCP_MAXSEG, SO_BROADCAST andre@0: }; andre@0: static PRInt32 socketLevels[PR_SockOpt_Last] = andre@0: { andre@0: 0, SOL_SOCKET, SOL_SOCKET, SOL_SOCKET, SOL_SOCKET, SOL_SOCKET, andre@0: IPPROTO_IP, IPPROTO_IP, IPPROTO_IP, IPPROTO_IP, andre@0: IPPROTO_IP, IPPROTO_IP, IPPROTO_IP, andre@0: IPPROTO_TCP, IPPROTO_TCP, SOL_SOCKET andre@0: }; andre@0: andre@0: if ((optname < PR_SockOpt_Linger) andre@0: || (optname >= PR_SockOpt_Last)) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: if (socketOptions[optname] == _PR_NO_SUCH_SOCKOPT) andre@0: { andre@0: PR_SetError(PR_OPERATION_NOT_SUPPORTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: *name = socketOptions[optname]; andre@0: *level = socketLevels[optname]; andre@0: return PR_SUCCESS; andre@0: } /* _PR_MapOptionName */