Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
network.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License version 2.1 as published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
24 #ifndef LXCPP_NETWORK_HPP
25 #define LXCPP_NETWORK_HPP
26 
27 #include "config.hpp"
28 #include <cargo/fields.hpp>
29 
30 #include <cstring>
31 #include <string>
32 #include <vector>
33 #include <array>
34 #include <ostream>
35 
36 #include <arpa/inet.h>
37 
38 namespace lxcpp {
39 
40 std::string toString(const in_addr& addr);
41 std::string toString(const in6_addr& addr);
42 void fromString(const std::string& s, in_addr& addr);
43 void fromString(const std::string& s, in6_addr& addr);
44 
48 enum class InetAddrType {
49  IPV4,
50  IPV6
51 };
52 
56 class InetAddr {
57 public:
58  InetAddr() : InetAddr("", 0) {}
59  InetAddr(const std::string& addr, unsigned prefix, uint32_t flags=0);
60 
61  template<typename T>
62  T& getAddr() {
63  std::uint8_t *v = addr.data();
64  return *(reinterpret_cast<T*>(v));
65  }
66  template<typename T>
67  const T& getAddr() const {
68  const std::uint8_t *v = addr.data();
69  return *(reinterpret_cast<const T*>(v));
70  }
71 
73  unsigned prefix;
74  uint32_t flags;
75 
77  (
78  type,
79  flags,
80  prefix,
81  addr
82  )
83 
84 private:
85  std::array<std::uint8_t,sizeof(in6_addr)> addr;
86 };
87 
88 static inline bool operator==(const in_addr& a, const in_addr& b)
89 {
90  return ::memcmp(&a, &b, sizeof(a)) == 0;
91 }
92 
93 static inline bool operator==(const in6_addr& a, const in6_addr& b)
94 {
95  return ::memcmp(&a, &b, sizeof(a)) == 0;
96 }
97 
98 static inline bool operator==(const InetAddr& a, const InetAddr& b)
99 {
100  if (a.type == b.type && a.prefix == b.prefix) {
101  if (a.type == InetAddrType::IPV6)
102  return a.getAddr<in6_addr>() == b.getAddr<in6_addr>();
103  else
104  return a.getAddr<in_addr>() == b.getAddr<in_addr>();
105  }
106  return false;
107 }
108 
109 std::string toString(const InetAddr& a);
110 
111 enum class RoutingTable {
112  UNSPEC, // also means 'any'
113  COMPAT,
114  DEFAULT,
115  MAIN,
116  LOCAL,
117  USER,
118 };
119 
120 inline std::string toString(const RoutingTable rt)
121 {
122  switch (rt) {
124  return "unspec";
126  return "compat";
128  return "default";
129  case RoutingTable::MAIN:
130  return "main";
131  case RoutingTable::LOCAL:
132  return "local";
133  default:
134  return "user";
135  }
136 }
137 
138 struct Route {
141  unsigned metric;
142  std::string ifname;
144 };
145 
146 enum class AttrName {
147  MAC,
148  FLAGS,
149  CHANGE,
150  TYPE,
151  MTU,
152  LINK,
153  TXQLEN,
154 };
155 
156 inline std::ostream& operator<<(std::ostream& os, const AttrName& a) {
157  switch (a) {
158  case AttrName::MAC: os << "mac"; break;
159  case AttrName::FLAGS: os << "flags"; break;
160  case AttrName::CHANGE: os << "change"; break;
161  case AttrName::TYPE: os << "type"; break;
162  case AttrName::MTU: os << "mtu"; break;
163  case AttrName::LINK: os << "link"; break;
164  case AttrName::TXQLEN: os << "txq"; break;
165  }
166  return os;
167 }
168 
169 struct Attr {
171  std::string value;
172 };
173 
174 typedef std::vector<Attr> Attrs;
175 
179 enum class InterfaceType : int {
180  VETH,
181  BRIDGE,
182  MACVLAN
183 };
184 
188 enum class MacVLanMode {
189  PRIVATE,
190  VEPA,
191  BRIDGE,
192  PASSTHRU
193 };
194 
195 enum class NetStatus {
196  DOWN,
197  UP
198 };
199 
200 std::string toString(const InetAddr& a);
201 
207  //TODO: remove pid from NetworkInterface (and from netlink)
208 public:
212  NetworkInterface(const std::string& ifname, pid_t pid = 0) :
213  mIfname(ifname),
214  mContainerPid(pid)
215  {
216  }
217 
218  const std::string& getName() const { return mIfname; }
219 
220  bool exists() const noexcept;
221 
225  NetStatus status() const;
226 
241  void create(InterfaceType type, const std::string& peerif = "", MacVLanMode mode = MacVLanMode::PRIVATE);
242 
247  void destroy();
248 
253  void moveToContainer(pid_t pid);
254 
259  void renameFrom(const std::string& oldif);
260 
265  void addToBridge(const std::string& bridge);
266 
271  void delFromBridge();
272 
277  void setAttrs(const Attrs& attrs);
278  Attrs getAttrs() const;
279 
284  void addInetAddr(const InetAddr& addr);
285 
290  void delInetAddr(const InetAddr& addr);
291 
296  std::vector<InetAddr> getInetAddressList() const;
297 
302  void addRoute(const Route& route, const RoutingTable rt = RoutingTable::MAIN);
303 
308  void delRoute(const Route& route, const RoutingTable rt = RoutingTable::MAIN);
309 
314  std::vector<Route> getRoutes(const RoutingTable rt = RoutingTable::MAIN) const;
315 
320  void up();
321 
326  void down();
327 
338  void setMACAddress(const std::string& macaddr);
339 
344  void setMTU(int mtu);
345 
350  void setTxLength(int txlen);
351 
356  static std::vector<std::string> getInterfaces(pid_t initpid);
357 
362  static std::vector<Route> getRoutes(pid_t initpid, const RoutingTable rt = RoutingTable::MAIN);
363 
364 private:
365  void createVeth(const std::string& peerif);
366  void createBridge();
367  void createMacVLan(const std::string& masterif, MacVLanMode mode);
368 
369  void modifyRoute(int cmd, const InetAddr& src, const InetAddr& dst);
370 
371  const std::string mIfname;
372  pid_t mContainerPid;
373 };
374 
375 } // namespace lxcpp
376 
377 #endif // LXCPP_NETWORK_HPP
std::string toString(const in_addr &addr)
Definition: network.cpp:220
int cmd
Definition: initctl.cpp:38
RoutingTable
Definition: network.hpp:111
AttrName name
Definition: network.hpp:170
static bool operator==(const in_addr &a, const in_addr &b)
Definition: network.hpp:88
AttrName
Definition: network.hpp:146
InetAddr src
Definition: network.hpp:140
void createVeth(const pid_t &nsPid, const std::string &nsDev, const std::string &hostDev)
Definition: netdev.cpp:375
Attrs getAttrs(const pid_t nsPid, const std::string &netdev)
Definition: netdev.cpp:476
const T & getAddr() const
Definition: network.hpp:67
Unified ip address.
Definition: network.hpp:56
InetAddr dst
Definition: network.hpp:139
Configuration file for the code.
Network interface information structure.
Definition: vasum-client-impl.hpp:69
InetAddrType
Suported address types.
Definition: network.hpp:48
T & getAddr()
Definition: network.hpp:62
Definition: network.hpp:138
std::string ifname
Definition: network.hpp:142
std::string value
Definition: network.hpp:171
#define CARGO_REGISTER(...)
Registers cargo fields within class.
Definition: fields.hpp:74
std::ostream & operator<<(std::ostream &os, const AttrName &a)
Definition: network.hpp:156
const std::string & getName() const
Definition: network.hpp:218
std::vector< Attr > Attrs
Definition: network.hpp:174
InetAddrType type
Definition: network.hpp:72
NetStatus
Definition: network.hpp:195
InetAddr()
Definition: network.hpp:58
RoutingTable table
Definition: network.hpp:143
void setAttrs(const pid_t nsPid, const std::string &netdev, const Attrs &attrs)
Definition: netdev.cpp:540
unsigned prefix
Definition: network.hpp:73
Definition: network.hpp:169
MacVLanMode
Suported MacVLan modes.
Definition: network.hpp:188
uint32_t flags
Definition: network.hpp:74
unsigned metric
Definition: network.hpp:141
void createBridge(const std::string &netdev)
Create bridge.
Definition: netdev.cpp:454
NetworkInterface(const std::string &ifname, pid_t pid=0)
Create network interface object for the ifname in the container (network namespace) ...
Definition: network.hpp:212
InterfaceType
Created interface type.
Definition: network.hpp:179
void fromString(const std::string &s, in_addr &addr)
Definition: network.cpp:244
Network operations to be performed on given container and interface operates on netlink device...
Definition: network.hpp:206