summaryrefslogtreecommitdiff
path: root/analyzers/src/json/ip_endpoint.cpp
blob: 85f5d81ca8db012c5e4892d4b766925a0b1a6fc9 (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
//------------------------------------------------------------------------------
// Author: Ilya Storozhilov
// Description: TCP-endpoint class definition
// Copyright (c) 2013-2014 EPAM Systems
//------------------------------------------------------------------------------
/*
    This file is part of Nfstrace.

    Nfstrace is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 2 of the License.

    Nfstrace 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Nfstrace.  If not, see <http://www.gnu.org/licenses/>.
*/
//------------------------------------------------------------------------------
#include <cstring>
#include <stdexcept>

#include <sys/socket.h>

#include "ip_endpoint.h"
//------------------------------------------------------------------------------

IpEndpoint::IpEndpoint(const std::string& host, int port, bool hostAsAddress)
    : _addrinfo{}
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof hints);
    hints.ai_family   = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags |= AI_NUMERICSERV;
    std::string serviceStr{std::to_string(port)};
    if(host == WildcardAddress)
    {
        hints.ai_flags |= AI_PASSIVE;
    }
    if((host != WildcardAddress) && (host != LoopbackAddress))
    {
        hints.ai_flags |= AI_CANONNAME;
        if(hostAsAddress)
        {
            hints.ai_flags |= AI_NUMERICHOST;
        }
    }
    int status = getaddrinfo((host == LoopbackAddress) || (host == WildcardAddress) ? nullptr : host.c_str(),
                             serviceStr.c_str(), &hints, &_addrinfo);
    if(status != 0)
    {
        throw std::runtime_error{gai_strerror(status)};
    }
}

IpEndpoint::~IpEndpoint()
{
    freeaddrinfo(_addrinfo);
}

//------------------------------------------------------------------------------