summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/socket_utils.cpp
blob: 11fec0298ac7511a981f90f8a6f289da3f031565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kNetwork

#include "mongo/platform/basic.h"

#include "mongo/util/net/socket_utils.h"

#if !defined(_WIN32)
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#if defined(__OpenBSD__)
#include <sys/uio.h>
#endif
#else
#include <mstcpip.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include "mongo/db/server_options.h"
#include "mongo/logv2/log.h"
#include "mongo/util/concurrency/value.h"
#include "mongo/util/errno_util.h"
#include "mongo/util/exit_code.h"
#include "mongo/util/net/sockaddr.h"
#include "mongo/util/quick_exit.h"
#include "mongo/util/str.h"
#include "mongo/util/winutil.h"

namespace mongo {

#if defined(_WIN32)
const struct WinsockInit {
    WinsockInit() {
        WSADATA d;
        if (WSAStartup(MAKEWORD(2, 2), &d) != 0) {
            LOGV2(23201,
                  "ERROR: wsastartup failed {errnoWithDescription}",
                  "ERROR: wsastartup failed",
                  "errnoWithDescription"_attr = errnoWithDescription());
            quickExit(EXIT_NTSERVICE_ERROR);
        }
    }
} winsock_init;
#endif

static bool ipv6 = false;
void enableIPv6(bool state) {
    ipv6 = state;
}
bool IPv6Enabled() {
    return ipv6;
}

#ifdef _WIN32
#ifdef _UNICODE
#define X_STR_CONST(str) (L##str)
#else
#define X_STR_CONST(str) (str)
#endif
const CString kKeepAliveGroup(
    X_STR_CONST("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"));
const CString kKeepAliveTime(X_STR_CONST("KeepAliveTime"));
const CString kKeepAliveInterval(X_STR_CONST("KeepAliveInterval"));
#undef X_STR_CONST
#endif

void setSocketKeepAliveParams(int sock,
                              unsigned int maxKeepIdleSecs,
                              unsigned int maxKeepIntvlSecs) {
#ifdef _WIN32
    // Defaults per MSDN when registry key does not exist.
    // Expressed in seconds here to be consistent with posix,
    // though Windows uses milliseconds.
    const DWORD kWindowsKeepAliveTimeSecsDefault = 2 * 60 * 60;
    const DWORD kWindowsKeepAliveIntervalSecsDefault = 1;

    const auto getKey = [](const CString& key, DWORD default_value) {
        auto withval = windows::getDWORDRegistryKey(kKeepAliveGroup, key);
        if (withval.isOK()) {
            auto val = withval.getValue();
            // Return seconds
            return val ? (val.get() / 1000) : default_value;
        }
        LOGV2_ERROR(23203,
                    "can't get KeepAlive parameter: {status}",
                    "can't get KeepAlive parameter",
                    "status"_attr = withval.getStatus());
        return default_value;
    };

    const auto keepIdleSecs = getKey(kKeepAliveTime, kWindowsKeepAliveTimeSecsDefault);
    const auto keepIntvlSecs = getKey(kKeepAliveInterval, kWindowsKeepAliveIntervalSecsDefault);

    if ((keepIdleSecs > maxKeepIdleSecs) || (keepIntvlSecs > maxKeepIntvlSecs)) {
        DWORD sent = 0;
        struct tcp_keepalive keepalive;
        keepalive.onoff = TRUE;
        keepalive.keepalivetime = std::min<DWORD>(keepIdleSecs, maxKeepIdleSecs) * 1000;
        keepalive.keepaliveinterval = std::min<DWORD>(keepIntvlSecs, maxKeepIntvlSecs) * 1000;
        if (WSAIoctl(sock,
                     SIO_KEEPALIVE_VALS,
                     &keepalive,
                     sizeof(keepalive),
                     nullptr,
                     0,
                     &sent,
                     nullptr,
                     nullptr)) {
            LOGV2_ERROR(23204,
                        "failed setting keepalive values: {error}",
                        "failed setting keepalive values",
                        "error"_attr = WSAGetLastError());
        }
    }
#elif defined(__APPLE__) || defined(__linux__)
    const auto updateSockOpt =
        [sock](int level, int optnum, unsigned int maxval, StringData optname) {
            unsigned int optval = 1;
            socklen_t len = sizeof(optval);

            if (getsockopt(sock, level, optnum, (char*)&optval, &len)) {
                LOGV2_ERROR(23205,
                            "can't get {optname}: {errnoWithDescription}",
                            "optname"_attr = optname,
                            "errnoWithDescription"_attr = errnoWithDescription());
            }

            if (optval > maxval) {
                optval = maxval;
                if (setsockopt(sock, level, optnum, (char*)&optval, sizeof(optval))) {
                    LOGV2_ERROR(23206,
                                "can't set {optname}: {errnoWithDescription}",
                                "optname"_attr = optname,
                                "errnoWithDescription"_attr = errnoWithDescription());
                }
            }
        };

#ifdef __APPLE__
    updateSockOpt(IPPROTO_TCP, TCP_KEEPALIVE, maxKeepIdleSecs, "TCP_KEEPALIVE");
#endif

#ifdef __linux__
#ifdef SOL_TCP
    const int level = SOL_TCP;
#else
    const int level = SOL_SOCKET;
#endif
    updateSockOpt(level, TCP_KEEPIDLE, maxKeepIdleSecs, "TCP_KEEPIDLE");
    updateSockOpt(level, TCP_KEEPINTVL, maxKeepIntvlSecs, "TCP_KEEPINTVL");
#endif

#endif
}

std::string makeUnixSockPath(int port) {
    return str::stream() << serverGlobalParams.socket << "/mongodb-" << port << ".sock";
}

// If an ip address is passed in, just return that.  If a hostname is passed
// in, look up its ip and return that.  Returns "" on failure.
std::string hostbyname(const char* hostname) {
    SockAddr sockAddr(hostname, 0, IPv6Enabled() ? AF_UNSPEC : AF_INET);
    if (!sockAddr.isValid() || sockAddr.getAddr() == "0.0.0.0")
        return "";
    else
        return sockAddr.getAddr();
}

//  --- my --

DiagStr& _hostNameCached = *(new DiagStr);  // this is also written to from commands/cloud.cpp

std::string getHostName() {
    char buf[256];
    int ec = gethostname(buf, 127);
    if (ec || *buf == 0) {
        LOGV2(23202,
              "can't get this server's hostname {errnoWithDescription}",
              "can't get this server's hostname",
              "errnoWithDescription"_attr = errnoWithDescription());
        return "";
    }
    return buf;
}

/** we store our host name once */
std::string getHostNameCached() {
    std::string temp = _hostNameCached.get();
    if (_hostNameCached.empty()) {
        temp = getHostName();
        _hostNameCached = temp;
    }
    return temp;
}

std::string getHostNameCachedAndPort() {
    return str::stream() << getHostNameCached() << ':' << serverGlobalParams.port;
}

std::string prettyHostName() {
    return (serverGlobalParams.port == ServerGlobalParams::DefaultDBPort
                ? getHostNameCached()
                : getHostNameCachedAndPort());
}

}  // namespace mongo