mirror of https://gitee.com/bigwinds/arangodb
202 lines
6.1 KiB
C
202 lines
6.1 KiB
C
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief collection of socket functions
|
|
///
|
|
/// @file
|
|
///
|
|
/// DISCLAIMER
|
|
///
|
|
/// Copyright 2004-2012 triagens GmbH, Cologne, Germany
|
|
///
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
/// you may not use this file except in compliance with the License.
|
|
/// You may obtain a copy of the License at
|
|
///
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
///
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
/// See the License for the specific language governing permissions and
|
|
/// limitations under the License.
|
|
///
|
|
/// Copyright holder is triAGENS GmbH, Cologne, Germany
|
|
///
|
|
/// @author Dr. Frank Celler
|
|
/// @author Copyright 2008-2012, triAGENS GmbH, Cologne, Germany
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "socket-utils.h"
|
|
|
|
#ifdef TRI_HAVE_LINUX_SOCKETS
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <sys/resource.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#endif
|
|
|
|
#include "BasicsC/logging.h"
|
|
#include "BasicsC/locks.h"
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- private variables
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Sockets
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief gethostbyname lock
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
TRI_mutex_t GethostbynameLock;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public functions
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Sockets
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief thread save gethostbyname
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
char* TRI_GetHostByName (char const* hostname, size_t* length) {
|
|
char* netaddress;
|
|
struct hostent * sheep;
|
|
|
|
TRI_LockMutex(&GethostbynameLock);
|
|
|
|
sheep = gethostbyname(hostname);
|
|
|
|
if (sheep == 0) {
|
|
LOG_WARNING("cannot resolve hostname '%s'", hostname);
|
|
|
|
TRI_UnlockMutex(&GethostbynameLock);
|
|
return 0;
|
|
}
|
|
|
|
*length = sheep->h_length;
|
|
netaddress = TRI_Allocate(*length);
|
|
memcpy(netaddress, sheep->h_addr_list[0], *length);
|
|
|
|
TRI_UnlockMutex(&GethostbynameLock);
|
|
return netaddress;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief sets non-blocking mode for a socket
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifdef TRI_HAVE_WIN32_NON_BLOCKING
|
|
|
|
bool TRI_SetNonBlockingSocket (socket_t fd) {
|
|
DWORD ul = 1;
|
|
|
|
return ioctlsocket(fd, FIONBIO, &ul) == SOCKET_ERROR ? false : true;
|
|
}
|
|
|
|
#else
|
|
|
|
bool TRI_SetNonBlockingSocket (socket_t fd) {
|
|
long flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (flags < 0) {
|
|
return false;
|
|
}
|
|
|
|
flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
if (flags < 0) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#endif
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief sets close-on-exit for a socket
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifdef TRI_HAVE_WIN32_CLOSE_ON_EXEC
|
|
|
|
bool TRI_SetCloseOnExecSocket (socket_t fd) {
|
|
return true;
|
|
}
|
|
|
|
#else
|
|
|
|
bool TRI_SetCloseOnExecSocket (socket_t fd) {
|
|
long flags = fcntl(fd, F_GETFD, 0);
|
|
|
|
if (flags < 0) {
|
|
return false;
|
|
}
|
|
|
|
flags = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
|
|
|
|
if (flags < 0) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#endif
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- MODULE
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public functions
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Sockets
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief initialises the sockets components
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void TRI_InitialiseSockets (void) {
|
|
TRI_InitMutex(&GethostbynameLock);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief shut downs the sockets components
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void TRI_ShutdownSockets (void) {
|
|
TRI_DestroyMutex(&GethostbynameLock);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Local Variables:
|
|
// mode: outline-minor
|
|
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
|
|
// End:
|