/src/wpantund/src/util/TunnelIPv6Interface.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2016 Nest Labs, Inc. |
4 | | * All rights reserved. |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | * |
18 | | * Description: |
19 | | * This file contains the interface for a C++ wrapper around the |
20 | | * `tunnel.c`/`tunnel.h` interface. |
21 | | * |
22 | | */ |
23 | | |
24 | | #ifndef __wpantund__TunnelInterface__ |
25 | | #define __wpantund__TunnelInterface__ |
26 | | |
27 | | #include "tunnel.h" |
28 | | #include "netif-mgmt.h" |
29 | | #include <cstdio> |
30 | | #include <string> |
31 | | #include <errno.h> |
32 | | #include <netinet/in.h> |
33 | | #include <net/if.h> |
34 | | #include "UnixSocket.h" |
35 | | #include <set> |
36 | | #include "IPv6Helpers.h" |
37 | | #include <boost/signals2/signal.hpp> |
38 | | |
39 | | class TunnelIPv6Interface : public nl::UnixSocket |
40 | | { |
41 | | |
42 | | public: |
43 | | TunnelIPv6Interface(const std::string& interface_name = "", int mtu = 1280); |
44 | | |
45 | | virtual ~TunnelIPv6Interface(); |
46 | | |
47 | | const std::string& get_interface_name(void); |
48 | | |
49 | | int get_last_error(void); |
50 | | |
51 | | bool is_up(void); |
52 | | int set_up(bool isUp); |
53 | | |
54 | | bool is_running(void); |
55 | | int set_running(bool isRunning); |
56 | | |
57 | | // This "online" is a bit of a mashup of "up" and "running". |
58 | | // This is going to be phased out. |
59 | | bool is_online(void); |
60 | | int set_online(bool isOnline); |
61 | | |
62 | | const struct in6_addr& get_realm_local_address()const; |
63 | | |
64 | | bool add_address(const struct in6_addr *addr, int prefixlen = 64); |
65 | | bool remove_address(const struct in6_addr *addr, int prefixlen = 64); |
66 | | |
67 | | bool add_route(const struct in6_addr *route, int prefixlen, uint32_t metric); |
68 | | bool remove_route(const struct in6_addr *route, int prefixlen, uint32_t metric); |
69 | | |
70 | | bool join_multicast_address(const struct in6_addr *addr); |
71 | | bool leave_multicast_address(const struct in6_addr *addr); |
72 | | |
73 | | virtual void reset(void); |
74 | | virtual ssize_t write(const void* data, size_t len); |
75 | | virtual ssize_t read(void* data, size_t len); |
76 | | |
77 | | virtual int process(void); |
78 | | virtual int update_fd_set(fd_set *read_fd_set, fd_set *write_fd_set, fd_set *error_fd_set, int *max_fd, cms_t *timeout); |
79 | | |
80 | | public: // Signals |
81 | | |
82 | | boost::signals2::signal<void(const struct in6_addr&, uint8_t)> mUnicastAddressWasAdded; |
83 | | boost::signals2::signal<void(const struct in6_addr&, uint8_t)> mUnicastAddressWasRemoved; |
84 | | boost::signals2::signal<void(const struct in6_addr&)> mMulticastAddressWasJoined; |
85 | | boost::signals2::signal<void(const struct in6_addr&)> mMulticastAddressWasLeft; |
86 | | |
87 | | // void linkStateChanged(isUp, isRunning); |
88 | | boost::signals2::signal<void(bool, bool)> mLinkStateChanged; |
89 | | |
90 | | private: |
91 | | void setup_signals(void); |
92 | | void setup_mld_listener(void); |
93 | | |
94 | | void processNetlinkFD(void); |
95 | | void processMLDMonitorFD(void); |
96 | | |
97 | | void on_link_state_changed(bool isUp, bool isRunning); |
98 | | void on_address_added(const struct in6_addr &address, uint8_t prefix_len); |
99 | | void on_multicast_address_joined(const struct in6_addr &address); |
100 | | void on_address_removed(const struct in6_addr &address, uint8_t prefix_len); |
101 | | void on_multicast_address_left(const struct in6_addr &address); |
102 | | |
103 | | private: |
104 | | std::string mInterfaceName; |
105 | | int mLastError; |
106 | | |
107 | | int mNetlinkFD; |
108 | | int mNetifMgmtFD; |
109 | | int mMLDMonitorFD; |
110 | | |
111 | | bool mIsRunning; |
112 | | bool mIsUp; |
113 | | |
114 | | struct Entry { |
115 | | int mPrefixLen; |
116 | | enum State { |
117 | | kWaitingToAdd, // Waiting to add the address on interface when it becomes online |
118 | | kWaitingForAddConfirm, // Address was added, waiting for callback to confirm the address add |
119 | | } mState; |
120 | | |
121 | | Entry(State state = kWaitingToAdd, int prefix_len = 64) : |
122 | 53.1k | mPrefixLen(prefix_len), |
123 | 53.1k | mState(state) { } |
124 | | }; |
125 | | |
126 | | std::map<struct in6_addr, Entry> mUnicastAddresses; |
127 | | std::map<struct in6_addr, Entry> mPendingMulticastAddresses; |
128 | | |
129 | | enum { |
130 | | kICMPv6MLDv2Type = 143, |
131 | | kICMPv6MLDv2RecordChangeToExcludeType = 3, |
132 | | kICMPv6MLDv2RecordChangeToIncludeType = 4, |
133 | | }; |
134 | | }; |
135 | | #endif /* defined(__wpantund__TunnelInterface__) */ |