summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/sockaddr.cpp
blob: db6adefc62f782d3b387681376a9d37df184f219 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
 *    Copyright (C) 2016 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    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 GNU Affero General 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 <iterator>
#include <set>
#include <vector>

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

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

#include "mongo/bson/util/builder.h"
#include "mongo/util/itoa.h"
#include "mongo/util/log.h"

namespace mongo {
namespace {
constexpr int SOCK_FAMILY_UNKNOWN_ERROR = 13078;

using AddrInfo = std::unique_ptr<addrinfo, decltype(&freeaddrinfo)>;

std::pair<int, AddrInfo> resolveAddrInfo(const std::string& hostOrIp,
                                         int port,
                                         sa_family_t familyHint) {
    addrinfo hints;
    memset(&hints, 0, sizeof(addrinfo));
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags |= AI_NUMERICHOST;  // first pass tries w/o DNS lookup
    hints.ai_family = familyHint;
    addrinfo* addrs = nullptr;

    ItoA portStr(port);
    int ret = getaddrinfo(hostOrIp.c_str(), StringData(portStr).rawData(), &hints, &addrs);

// old C compilers on IPv6-capable hosts return EAI_NODATA error
#ifdef EAI_NODATA
    int nodata = (ret == EAI_NODATA);
#else
    int nodata = false;
#endif
    if ((ret == EAI_NONAME) || nodata) {
        // iporhost isn't an IP address, allow DNS lookup
        hints.ai_flags &= ~AI_NUMERICHOST;
        ret = getaddrinfo(hostOrIp.c_str(), StringData(portStr).rawData(), &hints, &addrs);
    }

    return {ret, AddrInfo(addrs, &freeaddrinfo)};
}

}  // namespace

std::string getAddrInfoStrError(int code) {
#if !defined(_WIN32)
    return gai_strerror(code);
#else
    /* gai_strerrorA is not threadsafe on windows. don't use it. */
    return errnoWithDescription(code);
#endif
}

SockAddr::SockAddr() {
    addressSize = sizeof(sa);
    memset(&sa, 0, sizeof(sa));
    sa.ss_family = AF_UNSPEC;
    _isValid = true;
}

SockAddr::SockAddr(int sourcePort) {
    memset(as<sockaddr_in>().sin_zero, 0, sizeof(as<sockaddr_in>().sin_zero));
    as<sockaddr_in>().sin_family = AF_INET;
    as<sockaddr_in>().sin_port = htons(sourcePort);
    as<sockaddr_in>().sin_addr.s_addr = htonl(INADDR_ANY);
    addressSize = sizeof(sockaddr_in);
    _isValid = true;
}

void SockAddr::initUnixDomainSocket(const std::string& path, int port) {
#ifdef _WIN32
    uassert(13080, "no unix socket support on windows", false);
#endif
    uassert(
        13079, "path to unix socket too long", path.size() < sizeof(as<sockaddr_un>().sun_path));
    as<sockaddr_un>().sun_family = AF_UNIX;
    strcpy(as<sockaddr_un>().sun_path, path.c_str());
    addressSize = sizeof(sockaddr_un);
    _isValid = true;
}

SockAddr::SockAddr(StringData target, int port, sa_family_t familyHint)
    : _hostOrIp(target.toString()) {
    if (_hostOrIp == "localhost") {
        _hostOrIp = "127.0.0.1";
    }

    if (mongoutils::str::contains(_hostOrIp, '/') || familyHint == AF_UNIX) {
        initUnixDomainSocket(_hostOrIp, port);
        return;
    }

    auto addrErr = resolveAddrInfo(_hostOrIp, port, familyHint);

    if (addrErr.first) {
        // we were unsuccessful
        if (_hostOrIp != "0.0.0.0") {  // don't log if this as it is a
                                       // CRT construction and log() may not work yet.
            log() << "getaddrinfo(\"" << _hostOrIp
                  << "\") failed: " << getAddrInfoStrError(addrErr.first);
            _isValid = false;
            return;
        }
        *this = SockAddr(port);
        return;
    }

    // This throws away all but the first address.
    // Use SockAddr::createAll() to get all addresses.
    const auto* addrs = addrErr.second.get();
    fassert(16501, static_cast<size_t>(addrs->ai_addrlen) <= sizeof(sa));
    memcpy(&sa, addrs->ai_addr, addrs->ai_addrlen);
    addressSize = addrs->ai_addrlen;
    _isValid = true;
}

std::vector<SockAddr> SockAddr::createAll(StringData target, int port, sa_family_t familyHint) {
    std::string hostOrIp = target.toString();
    if (mongoutils::str::contains(hostOrIp, '/')) {
        std::vector<SockAddr> ret = {SockAddr()};
        ret[0].initUnixDomainSocket(hostOrIp, port);
        // Currently, this is always valid since initUnixDomainSocket()
        // will uassert() on failure. Be defensive against future changes.
        return ret[0].isValid() ? ret : std::vector<SockAddr>();
    }

    auto addrErr = resolveAddrInfo(hostOrIp, port, familyHint);
    if (addrErr.first) {
        log() << "getaddrinfo(\"" << hostOrIp
              << "\") failed: " << getAddrInfoStrError(addrErr.first);
        return {};
    }

    std::set<SockAddr> ret;
    struct sockaddr_storage storage;
    memset(&storage, 0, sizeof(storage));
    for (const auto* addrs = addrErr.second.get(); addrs; addrs = addrs->ai_next) {
        fassert(40594, static_cast<size_t>(addrs->ai_addrlen) <= sizeof(struct sockaddr_storage));
        // Make a temp copy in a local sockaddr_storage so that the
        // SockAddr constructor below can copy the entire buffer
        // without over-running addrinfo's storage
        memcpy(&storage, addrs->ai_addr, addrs->ai_addrlen);
        ret.emplace(storage, addrs->ai_addrlen);
    }
    return std::vector<SockAddr>(ret.begin(), ret.end());
}

SockAddr::SockAddr(const sockaddr_storage& other, socklen_t size)
    : addressSize(size), _hostOrIp(), sa(other), _isValid(true) {
    _hostOrIp = toString(true);
}

bool SockAddr::isIP() const {
    return (getType() == AF_INET) || (getType() == AF_INET6);
}

bool SockAddr::isLocalHost() const {
    switch (getType()) {
        case AF_INET:
            return getAddr() == "127.0.0.1";
        case AF_INET6:
            return getAddr() == "::1";
        case AF_UNIX:
            return true;
        default:
            return false;
    }
    fassert(16502, false);
    return false;
}

bool SockAddr::isDefaultRoute() const {
    using std::begin;
    using std::end;
    switch (getType()) {
        case AF_INET:
            return as<sockaddr_in>().sin_addr.s_addr == 0;
        case AF_INET6: {
            const auto& addr6 = as<sockaddr_in6>().sin6_addr;
            return std::all_of(
                begin(addr6.s6_addr), end(addr6.s6_addr), [](const auto c) { return c == 0; });
        }
        default:
            return false;
    }
}

bool SockAddr::isAnonymousUNIXSocket() const {
    return ((getType() == AF_UNIX) && (as<sockaddr_un>().sun_path[0] == '\0'));
}

std::string SockAddr::toString(bool includePort) const {
    if (includePort && (getType() != AF_UNIX) && (getType() != AF_UNSPEC)) {
        StringBuilder ss;

        if (getType() == AF_INET6) {
            ss << '[' << getAddr() << "]:" << getPort();
        } else {
            ss << getAddr() << ':' << getPort();
        }

        return ss.str();
    } else {
        return getAddr();
    }
}

sa_family_t SockAddr::getType() const {
    return sa.ss_family;
}

unsigned SockAddr::getPort() const {
    switch (getType()) {
        case AF_INET:
            return ntohs(as<sockaddr_in>().sin_port);
        case AF_INET6:
            return ntohs(as<sockaddr_in6>().sin6_port);
        case AF_UNIX:
            return 0;
        case AF_UNSPEC:
            return 0;
        default:
            massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
            return 0;
    }
}

std::string SockAddr::getAddr() const {
    switch (getType()) {
        case AF_INET:
        case AF_INET6: {
            const int buflen = 128;
            char buffer[buflen];
            int ret = getnameinfo(raw(), addressSize, buffer, buflen, NULL, 0, NI_NUMERICHOST);
            massert(13082,
                    mongoutils::str::stream() << "getnameinfo error " << getAddrInfoStrError(ret),
                    ret == 0);
            return buffer;
        }

        case AF_UNIX:
            return (!isAnonymousUNIXSocket() ? as<sockaddr_un>().sun_path
                                             : "anonymous unix socket");
        case AF_UNSPEC:
            return "(NONE)";
        default:
            massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
            return "";
    }
}

bool SockAddr::operator==(const SockAddr& r) const {
    if (getType() != r.getType())
        return false;

    if (getPort() != r.getPort())
        return false;

    switch (getType()) {
        case AF_INET:
            return as<sockaddr_in>().sin_addr.s_addr == r.as<sockaddr_in>().sin_addr.s_addr;
        case AF_INET6:
            return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr,
                          r.as<sockaddr_in6>().sin6_addr.s6_addr,
                          sizeof(in6_addr)) == 0;
        case AF_UNIX:
            return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) == 0;
        case AF_UNSPEC:
            return true;  // assume all unspecified addresses are the same
        default:
            massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
    }
    return false;
}

bool SockAddr::operator!=(const SockAddr& r) const {
    return !(*this == r);
}

bool SockAddr::operator<(const SockAddr& r) const {
    // Address family first
    if (getType() < r.getType()) {
        return true;
    }
    if (getType() > r.getType()) {
        return false;
    }

    // Address second
    int cmp;
    switch (getType()) {
        case AF_INET: {
            const auto laddr = ntohl(as<sockaddr_in>().sin_addr.s_addr);
            const auto raddr = ntohl(r.as<sockaddr_in>().sin_addr.s_addr);
            cmp = (laddr < raddr) ? -1 : (laddr > raddr) ? 1 : 0;
            break;
        }
        case AF_INET6:
            cmp = memcmp(as<sockaddr_in6>().sin6_addr.s6_addr,
                         r.as<sockaddr_in6>().sin6_addr.s6_addr,
                         sizeof(in6_addr));
            break;
        case AF_UNIX:
            cmp = strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path);
            break;
        default:
            massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
    }
    if (cmp < 0) {
        return true;
    }
    if (cmp > 0) {
        return false;
    }

    // All else equal, compare port
    return getPort() < r.getPort();
}

}  // namespace mongo