20 #include "mongo/pch.h"
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
39 #include <openssl/ssl.h>
42 #include "mongo/platform/compiler.h"
46 class SSLManagerInterface;
48 const int SOCK_FAMILY_UNKNOWN_ERROR=13078;
50 void disableNagle(
int sock);
54 typedef short sa_family_t;
55 typedef int socklen_t;
65 inline void closesocket(
int s) { close(s); }
66 const int INVALID_SOCKET = -1;
71 string makeUnixSockPath(
int port);
75 string hostbyname(
const char *hostname);
77 void enableIPv6(
bool state=
true);
79 void setSockTimeouts(
int sock,
double secs);
86 addressSize =
sizeof(sa);
87 memset(&sa, 0,
sizeof(sa));
88 sa.ss_family = AF_UNSPEC;
93 template <
typename T> T& as() {
return *(T*)(&sa); }
94 template <
typename T>
const T& as()
const {
return *(
const T*)(&sa); }
96 string toString(
bool includePort=
true)
const;
103 unsigned getPort()
const;
105 string getAddr()
const;
107 bool isLocalHost()
const;
109 bool operator==(
const SockAddr& r)
const;
111 bool operator!=(
const SockAddr& r)
const;
113 bool operator<(
const SockAddr& r)
const;
115 const sockaddr* raw()
const {
return (sockaddr*)&sa;}
116 sockaddr* raw() {
return (sockaddr*)&sa;}
118 socklen_t addressSize;
120 struct sockaddr_storage sa;
132 string prettyHostName();
139 const enum Type { CLOSED , RECV_ERROR , SEND_ERROR, RECV_TIMEOUT, SEND_TIMEOUT, FAILED_STATE, CONNECT_ERROR } _type;
141 SocketException( Type t ,
const std::string& server ,
int code = 9001 ,
const std::string& extra=
"" )
142 :
DBException( (
string)
"socket exception [" + _getStringType( t ) +
"] for " + server, code ),
150 bool shouldPrint()
const {
return _type != CLOSED; }
151 virtual string toString()
const;
152 virtual const std::string* server()
const {
return &_server; }
156 static string _getStringType( Type t ){
158 case CLOSED:
return "CLOSED";
159 case RECV_ERROR:
return "RECV_ERROR";
160 case SEND_ERROR:
return "SEND_ERROR";
161 case RECV_TIMEOUT:
return "RECV_TIMEOUT";
162 case SEND_TIMEOUT:
return "SEND_TIMEOUT";
163 case FAILED_STATE:
return "FAILED_STATE";
164 case CONNECT_ERROR:
return "CONNECT_ERROR";
165 default:
return "UNKNOWN";
181 static const int errorPollIntervalSecs;
191 Socket(
double so_timeout = 0,
int logLevel = 0 );
198 void send(
const char * data ,
int len,
const char *context );
199 void send(
const vector< pair< char *, int > > &data,
const char *context );
202 void recv(
char * data ,
int len );
203 int unsafe_recv(
char *buf,
int max );
205 int getLogLevel()
const {
return _logLevel; }
206 void setLogLevel(
int ll ) { _logLevel = ll; }
208 SockAddr remoteAddr()
const {
return _remote; }
209 string remoteString()
const {
return _remote.toString(); }
210 unsigned remotePort()
const {
return _remote.getPort(); }
212 void clearCounters() { _bytesIn = 0; _bytesOut = 0; }
213 long long getBytesIn()
const {
return _bytesIn; }
214 long long getBytesOut()
const {
return _bytesOut; }
216 void setTimeout(
double secs );
217 bool isStillConnected();
221 void secure( SSLManagerInterface* ssl );
223 void secureAccepted( SSLManagerInterface* ssl );
238 return _fdCreationMicroSec;
245 void _send(
const vector< pair< char *, int > > &data,
const char *context );
248 int _send(
const char * data ,
int len );
251 int _recv(
char * buf ,
int max );
253 void _handleRecvError(
int ret,
int len,
int* retries);
254 MONGO_COMPILER_NORETURN
void _handleSendError(
int ret,
const char* context);
257 uint64_t _fdCreationMicroSec;
263 time_t _lastValidityCheckAtSecs;
267 SSLManagerInterface* _sslManager;