20 #include "mongo/config.h"
26 #include <sys/socket.h>
27 #include <sys/types.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) {
80 const int INVALID_SOCKET = -1;
87 std::string hostbyname(
const char* hostname);
89 void enableIPv6(
bool state =
true);
91 void setSockTimeouts(
int sock,
double secs);
98 explicit SockAddr(
int sourcePort);
103 template <
typename T>
107 template <
typename T>
108 const T& as()
const {
109 return *(
const T*)(&sa);
112 std::string toString(
bool includePort =
true)
const;
114 bool isValid()
const {
121 sa_family_t getType()
const;
123 unsigned getPort()
const;
125 std::string getAddr()
const;
127 bool isLocalHost()
const;
129 bool operator==(
const SockAddr& r)
const;
131 bool operator!=(
const SockAddr& r)
const;
133 bool operator<(
const SockAddr& r)
const;
135 const sockaddr* raw()
const {
136 return (sockaddr*)&sa;
139 return (sockaddr*)&sa;
142 socklen_t addressSize;
145 struct sockaddr_storage sa;
170 const std::string& server,
172 const std::string& extra =
"")
173 :
DBException(std::string(
"socket exception [") + _getStringType(t) +
"] for " + server,
181 bool shouldPrint()
const {
182 return _type != CLOSED;
184 virtual std::string toString()
const;
185 virtual const std::string* server()
const {
191 static std::string _getStringType(Type t) {
200 return "RECV_TIMEOUT";
202 return "SEND_TIMEOUT";
204 return "FAILED_STATE";
206 return "CONNECT_ERROR";
222 MONGO_DISALLOW_COPYING(
Socket);
225 static const int errorPollIntervalSecs;
250 void send(
const char* data,
int len,
const char* context);
251 void send(
const std::vector<std::pair<char*, int> >& data,
const char* context);
254 void recv(
char* data,
int len);
255 int unsafe_recv(
char* buf,
int max);
267 std::string remoteString()
const {
268 return _remote.toString();
270 unsigned remotePort()
const {
271 return _remote.getPort();
278 void clearCounters() {
282 long long getBytesIn()
const {
285 long long getBytesOut()
const {
292 void setTimeout(
double secs);
293 bool isStillConnected();
295 void setHandshakeReceived() {
296 _awaitingHandshake =
false;
299 bool isAwaitingHandshake() {
300 return _awaitingHandshake;
308 bool secure(SSLManagerInterface* ssl,
const std::string& remoteHost);
310 void secureAccepted(SSLManagerInterface* ssl);
324 std::string doSSLHandshake(
const char* firstBytes = NULL,
int len = 0);
330 return _fdCreationMicroSec;
333 void handleRecvError(
int ret,
int len);
334 MONGO_COMPILER_NORETURN
void handleSendError(
int ret,
const char* context);
340 void _send(
const std::vector<std::pair<char*, int> >& data,
const char* context);
343 int _send(
const char* data,
int len,
const char* context);
346 int _recv(
char* buf,
int max);
349 uint64_t _fdCreationMicroSec;
356 time_t _lastValidityCheckAtSecs;
359 boost::scoped_ptr<SSLConnection> _sslConnection;
360 SSLManagerInterface* _sslManager;
365 bool _awaitingHandshake;
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
thrown by Socket and SockAddr
Definition: sock.h:157
wrapped around os representation of network address
Definition: sock.h:96
uint64_t getSockCreationMicroSec() const
Definition: sock.h:329
std::string getHostName()
this is not cache and does a syscall
thin wrapped around file descriptor and system calls todo: ssl
Definition: sock.h:221
Most mongo exceptions inherit from this; this is commonly caught in most threads. ...
Definition: assert_util.h:87
Representation of the severity / priority of a log message.
Definition: log_severity.h:33