20#include "mongo/config.h"
26#include <sys/socket.h>
37#include <boost/scoped_ptr.hpp>
42#include "mongo/base/disallow_copying.h"
43#include "mongo/logger/log_severity.h"
44#include "mongo/platform/compiler.h"
45#include "mongo/platform/cstdint.h"
46#include "mongo/util/assert_util.h"
51 class SSLManagerInterface;
55 extern const int portSendFlags;
56 extern const int portRecvFlags;
58 const int SOCK_FAMILY_UNKNOWN_ERROR=13078;
60 void disableNagle(
int sock);
62 void shutdownNetworking();
66 typedef short sa_family_t;
67 typedef int socklen_t;
77 inline void closesocket(
int s) { close(s); }
78 const int INVALID_SOCKET = -1;
85 std::string hostbyname(
const char *hostname);
87 void enableIPv6(
bool state=
true);
89 void setSockTimeouts(
int sock,
double secs);
99 template <
typename T> T& as() {
return *(T*)(&sa); }
100 template <
typename T>
const T& as()
const {
return *(
const T*)(&sa); }
102 std::string toString(
bool includePort=
true)
const;
104 bool isValid()
const {
return _isValid; }
111 unsigned getPort()
const;
113 std::string getAddr()
const;
115 bool isLocalHost()
const;
117 bool operator==(
const SockAddr& r)
const;
119 bool operator!=(
const SockAddr& r)
const;
121 bool operator<(
const SockAddr& r)
const;
123 const sockaddr* raw()
const {
return (sockaddr*)&sa;}
124 sockaddr* raw() {
return (sockaddr*)&sa;}
126 socklen_t addressSize;
128 struct sockaddr_storage sa;
132 extern SockAddr unknownAddress;
142 const enum Type { CLOSED , RECV_ERROR , SEND_ERROR, RECV_TIMEOUT, SEND_TIMEOUT, FAILED_STATE, CONNECT_ERROR } _type;
144 SocketException( Type t ,
const std::string& server ,
int code = 9001 ,
const std::string& extra=
"" )
145 :
DBException( std::string(
"socket exception [") + _getStringType( t ) +
"] for " + server, code ),
153 bool shouldPrint()
const {
return _type != CLOSED; }
154 virtual std::string toString()
const;
155 virtual const std::string* server()
const {
return &_server; }
159 static std::string _getStringType( Type t ){
161 case CLOSED:
return "CLOSED";
162 case RECV_ERROR:
return "RECV_ERROR";
163 case SEND_ERROR:
return "SEND_ERROR";
164 case RECV_TIMEOUT:
return "RECV_TIMEOUT";
165 case SEND_TIMEOUT:
return "SEND_TIMEOUT";
166 case FAILED_STATE:
return "FAILED_STATE";
167 case CONNECT_ERROR:
return "CONNECT_ERROR";
168 default:
return "UNKNOWN";
182 MONGO_DISALLOW_COPYING(
Socket);
185 static const int errorPollIntervalSecs;
195 Socket(
double so_timeout = 0, logger::LogSeverity logLevel = logger::LogSeverity::Log() );
210 void send(
const char * data ,
int len,
const char *context );
211 void send(
const std::vector< std::pair< char *, int > > &data,
const char *context );
214 void recv(
char * data ,
int len );
215 int unsafe_recv(
char *buf,
int max );
217 logger::LogSeverity getLogLevel()
const {
return _logLevel; }
218 void setLogLevel( logger::LogSeverity ll ) { _logLevel = ll; }
220 SockAddr remoteAddr()
const {
return _remote; }
221 std::string remoteString()
const {
return _remote.toString(); }
222 unsigned remotePort()
const {
return _remote.getPort(); }
224 SockAddr localAddr()
const {
return _local; }
226 void clearCounters() { _bytesIn = 0; _bytesOut = 0; }
227 long long getBytesIn()
const {
return _bytesIn; }
228 long long getBytesOut()
const {
return _bytesOut; }
229 int rawFD()
const {
return _fd; }
231 void setTimeout(
double secs );
232 bool isStillConnected();
234 void setHandshakeReceived() {
235 _awaitingHandshake =
false;
238 bool isAwaitingHandshake() {
239 return _awaitingHandshake;
247 bool secure( SSLManagerInterface* ssl,
const std::string& remoteHost);
249 void secureAccepted( SSLManagerInterface* ssl );
269 return _fdCreationMicroSec;
272 void handleRecvError(
int ret,
int len);
273 MONGO_COMPILER_NORETURN
void handleSendError(
int ret,
const char* context);
279 void _send(
const std::vector< std::pair< char *, int > > &data,
const char *context );
282 int _send(
const char * data ,
int len ,
const char * context );
285 int _recv(
char * buf ,
int max );
288 uint64_t _fdCreationMicroSec;
295 time_t _lastValidityCheckAtSecs;
298 boost::scoped_ptr<SSLConnection> _sslConnection;
299 SSLManagerInterface* _sslManager;
301 logger::LogSeverity _logLevel;
304 bool _awaitingHandshake;
Most mongo exceptions inherit from this; this is commonly caught in most threads.
Definition assert_util.h:81
thrown by Socket and SockAddr
Definition sock.h:140
thin wrapped around file descriptor and system calls todo: ssl
Definition sock.h:181
uint64_t getSockCreationMicroSec() const
Definition sock.h:268
bool connect(SockAddr &farEnd)
The correct way to initialize and connect to a socket is as follows: (1) construct the SockAddr,...
std::string doSSLHandshake(const char *firstBytes=NULL, int len=0)
This function calls SSL_accept() if SSL-encrypted sockets are desired.
Socket(double so_timeout=0, logger::LogSeverity logLevel=logger::LogSeverity::Log())
In some cases the timeout will actually be 2x this value - eg we do a partial send,...
the main MongoDB namespace
Definition bulk_operation_builder.h:24
std::string getHostName()
this is not cache and does a syscall
wrapped around os representation of network address
Definition sock.h:94
sa_family_t getType() const