mirror of https://gitee.com/bigwinds/arangodb
678 lines
26 KiB
C++
678 lines
26 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief connection endpoint specification
|
|
///
|
|
/// @file
|
|
///
|
|
/// DISCLAIMER
|
|
///
|
|
/// Copyright 2010-2011 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 Jan Steemann
|
|
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef TRIAGENS_FYN_REST_ENDPOINT_SPECIFICATION_H
|
|
#define TRIAGENS_FYN_REST_ENDPOINT_SPECIFICATION_H 1
|
|
|
|
#include <Basics/Common.h>
|
|
#include <Basics/StringUtils.h>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
|
|
#ifdef TRI_HAVE_LINUX_SOCKETS
|
|
#include <sys/un.h>
|
|
#endif
|
|
|
|
#include <sys/socket.h>
|
|
#include <netdb.h>
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- EndpointSpecification
|
|
// -----------------------------------------------------------------------------
|
|
|
|
namespace triagens {
|
|
namespace rest {
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief endpoint types
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
enum EndpointType {
|
|
ENDPOINT_SERVER,
|
|
ENDPOINT_CLIENT
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief endpoint types
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
enum EndpointDomainType {
|
|
ENDPOINT_UNKNOWN = 0,
|
|
#ifdef TRI_HAVE_LINUX_SOCKETS
|
|
ENDPOINT_UNIX,
|
|
#endif
|
|
ENDPOINT_IPV4,
|
|
ENDPOINT_IPV6
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief endpoint specification
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class EndpointSpecification {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- constructors / destructors
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
protected:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointSpecification (EndpointType, EndpointDomainType, const string&);
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief destroys an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual ~EndpointSpecification ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public methods
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates a server endpoint from a string value
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static EndpointSpecification* serverFactory (const string&);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates a client endpoint from a string value
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static EndpointSpecification* clientFactory (const string&);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates an endpoint from a string value
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static EndpointSpecification* factory (const EndpointType type,
|
|
const string&);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief compare two endpoints
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool operator== (EndpointSpecification const &) const;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief connect the endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual int connect () = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief disconnect the endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual void disconnect () = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief init an incoming connection
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual bool initIncoming (socket_t) = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief initialise socket flags
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual bool setSocketFlags (socket_t);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief return whether the endpoint is connected
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool isConnected () const {
|
|
return _connected;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get the type of an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointType getType () const {
|
|
return _type;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get the original endpoint specification
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string getSpecification () const {
|
|
return _specification;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get endpoint domain
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual int getDomain () const = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get port number
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual int getPort () const = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get host name
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual string getHost () const = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get address
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual string getHostString () const = 0;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- protected variables
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
protected:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief default port number if none specified
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static const uint16_t _defaultPort;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief whether or not the endpoint is connected
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool _connected;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief the actual socket
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
socket_t _socket;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief endpoint type
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointType _type;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief endpoint domain type
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointDomainType _domainType;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief original endpoint specification
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string _specification;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
};
|
|
|
|
#ifdef TRI_HAVE_LINUX_SOCKETS
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- EndpointSpecificationUnix
|
|
// -----------------------------------------------------------------------------
|
|
|
|
class EndpointSpecificationUnix : public EndpointSpecification {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- constructors / destructors
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointSpecificationUnix (const EndpointType,
|
|
string const&,
|
|
string const&);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief destroys an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual ~EndpointSpecificationUnix ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public methods
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief connect the endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
socket_t connect ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief disconnect the endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual void disconnect ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief init an incoming connection
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual bool initIncoming (socket_t);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get endpoint domain
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int getDomain () const {
|
|
return AF_UNIX;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get port
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int getPort () const {
|
|
return 0;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get host name
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string getHost () const {
|
|
return "localhost";
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get host string for HTTP requests
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string getHostString () const {
|
|
return "localhost";
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- private variables
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
private:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief socket file
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string _path;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- EndpointSpecificationIp
|
|
// -----------------------------------------------------------------------------
|
|
|
|
class EndpointSpecificationIp : public EndpointSpecification {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- constructors / destructors
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
protected:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointSpecificationIp (const EndpointType,
|
|
const EndpointDomainType,
|
|
string const&,
|
|
string const&,
|
|
const uint16_t);
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief destroys an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
~EndpointSpecificationIp ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- private methods
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
private:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief connect the socket
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
socket_t connectSocket (const struct addrinfo*);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public methods
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief connect the endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
socket_t connect ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief disconnect the endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual void disconnect ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief init an incoming connection
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
virtual bool initIncoming (socket_t);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get port
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int getPort () const {
|
|
return _port;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get host
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string getHost () const {
|
|
return _host;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get host strin for HTTP requests
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string getHostString () const {
|
|
return _host + ':' + triagens::basics::StringUtils::itoa(_port);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- private variables
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
private:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief host name / address (IPv4 or IPv6)
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
string _host;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief port number
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
uint16_t _port;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- EndpointSpecificationIpV4
|
|
// -----------------------------------------------------------------------------
|
|
|
|
class EndpointSpecificationIpV4 : public EndpointSpecificationIp {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- constructors / destructors
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointSpecificationIpV4 (const EndpointType,
|
|
string const&,
|
|
string const&,
|
|
const uint16_t);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief destroys an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
~EndpointSpecificationIpV4 ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public methods
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get endpoint domain
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int getDomain () const {
|
|
return AF_INET;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- EndpointSpecificationIpV6
|
|
// -----------------------------------------------------------------------------
|
|
|
|
class EndpointSpecificationIpV6 : public EndpointSpecificationIp {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- constructors / destructors
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief creates an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
EndpointSpecificationIpV6 (const EndpointType,
|
|
string const&,
|
|
string const&,
|
|
const uint16_t);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief destroys an endpoint
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
~EndpointSpecificationIpV6 ();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- public methods
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup Rest
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get endpoint domain
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int getDomain () const {
|
|
return AF_INET6;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
};
|
|
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
// Local Variables:
|
|
// mode: outline-minor
|
|
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
|
|
// End:
|