summaryrefslogtreecommitdiff
path: root/implementation/endpoints/include/netlink_connector.hpp
blob: 0a7c94effbf5a3391fc19c38574c79acc27d6919 (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
// Copyright (C) 2014-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef VSOMEIP_V3_NETLINK_CONNECTOR_HPP_
#define VSOMEIP_V3_NETLINK_CONNECTOR_HPP_

#if defined(__linux__) || defined(ANDROID)

#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <map>
#include <mutex>

#include <boost/asio/basic_raw_socket.hpp>
#include <boost/asio/ip/address.hpp>

#include "../../endpoints/include/buffer.hpp"

namespace vsomeip_v3 {

template <typename Protocol>
class nl_endpoint {
public:
    /// The protocol type associated with the endpoint.
    using protocol_type = Protocol;
    using data_type = boost::asio::detail::socket_addr_type;

    /// Default constructor.
    nl_endpoint()
    {
        sockaddr.nl_family = PF_NETLINK;
        sockaddr.nl_groups = 0;
        sockaddr.nl_pid = 0; // Let the kernel do the assignment
    }

    /// Construct an endpoint using the specified path name.
    nl_endpoint(int group)
    {
        sockaddr.nl_family = PF_NETLINK;
        sockaddr.nl_groups = static_cast<unsigned int>(group);
        sockaddr.nl_pid = 0;
    }

    /// Copy constructor.
    nl_endpoint(const nl_endpoint& other)
    {
        sockaddr = other.sockaddr;
    }

    /// Assign from another endpoint.
    nl_endpoint& operator=(const nl_endpoint& other)
    {
        sockaddr = other.sockaddr;
        return *this;
    }

    /// The protocol associated with the endpoint.
    protocol_type protocol() const
    {
        return protocol_type();
    }

    /// Get the underlying endpoint in the native type.
    data_type* data()
    {
        return reinterpret_cast<struct sockaddr*>(&sockaddr);
    }

    /// Get the underlying endpoint in the native type.
    const data_type* data() const
    {
        return reinterpret_cast<const struct sockaddr*>(&sockaddr);
    }

    /// Get the underlying size of the endpoint in the native type.
    std::size_t size() const
    {
        return sizeof(sockaddr);
    }

    /// Set the underlying size of the endpoint in the native type.
    void resize(std::size_t size)
    {
        (void)size;
    /* nothing we can do here */
    }

    /// Get the capacity of the endpoint in the native type.
    std::size_t capacity() const
    {
        return sizeof(sockaddr);
    }

private:
    sockaddr_nl sockaddr;
};

class nl_protocol {
public:
    nl_protocol() {
        proto = 0;
    }
    nl_protocol(int proto) {
        this->proto = proto;
    }
    /// Obtain an identifier for the type of the protocol.
    int type() const
    {
        return SOCK_RAW;
    }
    /// Obtain an identifier for the protocol.
    int protocol() const
    {
        return proto;
    }
    /// Obtain an identifier for the protocol family.
    int family() const
    {
        return PF_NETLINK;
    }

    using endpoint = nl_endpoint<nl_protocol>;
    using socket = boost::asio::basic_raw_socket<nl_protocol>;

private:
    int proto;
};

using net_if_changed_handler_t = std::function< void (
    bool,        // true = is interface, false = is route
    std::string, // interface name
    bool)        // available?
>;

class netlink_connector : public std::enable_shared_from_this<netlink_connector> {
public:
    netlink_connector(boost::asio::io_context &_io, const boost::asio::ip::address &_address,
                        const boost::asio::ip::address &_multicast_address,
                        bool _is_requiring_link = true):
        net_if_index_for_address_(0),
        handler_(nullptr),
        socket_(_io),
        recv_buffer_(recv_buffer_size, 0),
        address_(_address),
        multicast_address_(_multicast_address),
        is_requiring_link_(_is_requiring_link) {
    }
    ~netlink_connector() {}

    void register_net_if_changes_handler(const net_if_changed_handler_t& _handler);
    void unregister_net_if_changes_handler();

    void start();
    void stop();

private:
    bool has_address(struct ifaddrmsg * ifa_struct,
            size_t length,
            const unsigned int address);
    void send_ifa_request();
    void send_ifi_request();
    void send_rt_request();

    void receive_cbk(boost::system::error_code const &_error, std::size_t _bytes);
    void send_cbk(boost::system::error_code const &_error, std::size_t _bytes);

    bool check_sd_multicast_route_match(struct rtmsg* _routemsg,
                                        size_t _length,
                                        std::string* _routename) const;

    std::map<int, unsigned int> net_if_flags_;
    int net_if_index_for_address_;

    net_if_changed_handler_t handler_;

    std::mutex socket_mutex_;
    boost::asio::basic_raw_socket<nl_protocol> socket_;

    const size_t recv_buffer_size = 16384;
    message_buffer_t recv_buffer_;

    boost::asio::ip::address address_;
    boost::asio::ip::address multicast_address_;
    bool is_requiring_link_;
};

} // namespace vsomeip_v3

#endif // __linux__ || ANDROID

#endif // VSOMEIP_V3_NETLINK_CONNECTOR_HPP_