1 // message_port.h
2 
3 
4 /**
5  *    Copyright (C) 2018-present MongoDB, Inc.
6  *
7  *    This program is free software: you can redistribute it and/or modify
8  *    it under the terms of the Server Side Public License, version 1,
9  *    as published by MongoDB, Inc.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    Server Side Public License for more details.
15  *
16  *    You should have received a copy of the Server Side Public License
17  *    along with this program. If not, see
18  *    <http://www.mongodb.com/licensing/server-side-public-license>.
19  *
20  *    As a special exception, the copyright holders give permission to link the
21  *    code of portions of this program with the OpenSSL library under certain
22  *    conditions as described in each individual source file and distribute
23  *    linked combinations including the program with the OpenSSL library. You
24  *    must comply with the Server Side Public License in all respects for
25  *    all of the code used other than as permitted herein. If you modify file(s)
26  *    with this exception, you may extend this exception to your version of the
27  *    file(s), but you are not obligated to do so. If you do not wish to do so,
28  *    delete this exception statement from your version. If you delete this
29  *    exception statement from all source files in the program, then also delete
30  *    it in the license file.
31  */
32 
33 #pragma once
34 
35 #include <vector>
36 
37 #include "mongo/config.h"
38 #include "mongo/util/net/abstract_message_port.h"
39 #include "mongo/util/net/message.h"
40 #include "mongo/util/net/sock.h"
41 
42 namespace mongo {
43 
44 class MessagingPort;
45 
46 class MessagingPort final : public AbstractMessagingPort {
47 public:
48     MessagingPort(int fd, const SockAddr& remote);
49 
50     // in some cases the timeout will actually be 2x this value - eg we do a partial send,
51     // then the timeout fires, then we try to send again, then the timeout fires again with
52     // no data sent, then we detect that the other side is down
53     MessagingPort(double so_timeout = 0, logger::LogSeverity logLevel = logger::LogSeverity::Log());
54 
55     MessagingPort(std::shared_ptr<Socket> socket);
56 
57     ~MessagingPort() override;
58 
59     void setTimeout(Milliseconds millis) override;
60 
61     void shutdown() override;
62 
63     /* it's assumed if you reuse a message object, that it doesn't cross MessagingPort's.
64        also, the Message data will go out of scope on the subsequent recv call.
65     */
66     bool recv(Message& m) override;
67     bool call(const Message& toSend, Message& response) override;
68 
69     void say(const Message& toSend) override;
70 
remotePort()71     unsigned remotePort() const override {
72         return _psock->remotePort();
73     }
74     virtual HostAndPort remote() const override;
75     virtual SockAddr remoteAddr() const override;
76     virtual SockAddr localAddr() const override;
77 
send(const char * data,int len,const char * context)78     void send(const char* data, int len, const char* context) override {
79         _psock->send(data, len, context);
80     }
send(const std::vector<std::pair<char *,int>> & data,const char * context)81     void send(const std::vector<std::pair<char*, int>>& data, const char* context) override {
82         _psock->send(data, context);
83     }
connect(SockAddr & farEnd)84     bool connect(SockAddr& farEnd) override {
85         return _psock->connect(farEnd);
86     }
87 
setLogLevel(logger::LogSeverity ll)88     void setLogLevel(logger::LogSeverity ll) override {
89         _psock->setLogLevel(ll);
90     }
91 
clearCounters()92     void clearCounters() override {
93         _psock->clearCounters();
94     }
95 
getBytesIn()96     long long getBytesIn() const override {
97         return _psock->getBytesIn();
98     }
99 
getBytesOut()100     long long getBytesOut() const override {
101         return _psock->getBytesOut();
102     }
103 
104     void setX509PeerInfo(SSLPeerInfo x509PeerInfo) override;
105 
106     const SSLPeerInfo& getX509PeerInfo() const override;
107 
108     void setConnectionId(const long long connectionId) override;
109 
110     long long connectionId() const override;
111 
112     void setTag(const AbstractMessagingPort::Tag tag) override;
113 
114     AbstractMessagingPort::Tag getTag() const override;
115 
116     /**
117      * Initiates the TLS/SSL handshake on this MessagingPort.
118      * When this function returns, further communication on this
119      * MessagingPort will be encrypted.
120      * ssl - Pointer to the global SSLManager.
121      * remoteHost - The hostname of the remote server.
122      */
secure(SSLManagerInterface * ssl,const std::string & remoteHost)123     bool secure(SSLManagerInterface* ssl, const std::string& remoteHost) override {
124 #ifdef MONGO_CONFIG_SSL
125         return _psock->secure(ssl, remoteHost);
126 #else
127         return false;
128 #endif
129     }
130 
isStillConnected()131     bool isStillConnected() const override {
132         return _psock->isStillConnected();
133     }
134 
getSockCreationMicroSec()135     uint64_t getSockCreationMicroSec() const override {
136         return _psock->getSockCreationMicroSec();
137     }
138 
139 private:
140     // this is the parsed version of remote
141     HostAndPort _remoteParsed;
142     SSLPeerInfo _x509PeerInfo;
143     long long _connectionId;
144     AbstractMessagingPort::Tag _tag;
145     std::shared_ptr<Socket> _psock;
146 };
147 
148 }  // namespace mongo
149