softwarecontainer  0.18.0-739e8d7 2017-05-04
networkgateway.h
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #pragma once
21 
22 #include "gateway/gateway.h"
23 #include "jansson.h"
24 #include "netlink.h"
25 #include "iptableentry.h"
26 #include "softwarecontainererror.h"
27 #include "networkgatewayfunctions.h"
28 
29 namespace softwarecontainer {
30 
40  public Gateway
41 {
42  LOG_DECLARE_CLASS_CONTEXT("NETG", "Network gateway");
43 
44 public:
45  static constexpr const char *ID = "network";
46 
47  // Exit codes used when running inside container
48  static const constexpr int FAILURE = -1;
49  static const constexpr int SUCCESS = 0;
50  static const constexpr int NO_LINK = 1; // The desired link was not found
51  static const constexpr int BAD_LINKUP = 2; // Could not bring link up
52  static const constexpr int BAD_LINKDOWN = 3; // Could not bring link up
53 
59  NetworkGateway(const int32_t id,
60  const std::string bridgeDevice,
61  const std::string gateway,
62  const uint8_t maskBits,
63  std::shared_ptr<ContainerAbstractInterface> container);
64 
65  virtual ~NetworkGateway();
66 
67  bool readConfigElement(const json_t *element) override;
68 
72  bool activateGateway() override;
73 
77  bool teardownGateway() override;
78 private:
90  bool setDefaultGateway();
91 
103  bool up();
104 
113  bool down();
114 
123  virtual bool isBridgeAvailable();
124 
125  struct in_addr m_ip;
126  uint32_t m_netmask;
127  std::string m_gateway;
128  std::string m_bridgeDevice;
129 
130  std::vector<IPTableEntry> m_entries;
131 
132  bool m_interfaceInitialized;
133 
134  int32_t m_containerID;
135  Netlink m_netlinkHost;
136 
137  NetworkGatewayFunctions m_functions;
138 };
139 
141 {
142 public:
144  m_message("NetworkGateway exception")
145  {
146  }
147 
148  NetworkGatewayError(const std::string &message):
149  m_message(message)
150  {
151  }
152 
153  virtual const char *what() const throw()
154  {
155  return m_message.c_str();
156  }
157 
158 protected:
159  std::string m_message;
160 };
161 
163 {
164 public:
166  NetworkGatewayError("An error is occurred when trying to generate IP address")
167  {
168  }
169 
170  IPAllocationError(const std::string &message):
171  NetworkGatewayError(message)
172  {
173  }
174 };
175 
176 } // namespace softwarecontainer
Sets up and manages network access and routing to the container.
bool activateGateway() override
Implements Gateway::activateGateway.
Gateway base class for SoftwareContainer.
Definition: gateway.h:61
Sets up and manages network access and routing to the container.
bool teardownGateway() override
Implements Gateway::teardownGateway.
NetworkGateway(const int32_t id, const std::string bridgeDevice, const std::string gateway, const uint8_t maskBits, std::shared_ptr< ContainerAbstractInterface > container)
Creates a network gateway.
Developers guide to adding a config item:
bool readConfigElement(const json_t *element) override
Gateway specific parsing of config elements.