softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::NetworkGatewayFunctions Class Reference

Sets up and manages network access and routing to the container. More...

#include <networkgatewayfunctions.h>

Public Member Functions

uint32_t generateIP (const uint32_t netmask, const std::string gatewayIP, const int32_t containerID)
 Generate IP address for the container. More...
 

Detailed Description

Sets up and manages network access and routing to the container.

The responsibility of NetworkGateway is to setup network connection as specified by given configuration. This configuration is described in detail in the user documentation, but a short summary of it is how to handle incoming and outgoing network packages using the three targets: ACCEPT, DROP and REJECT.

Definition at line 35 of file networkgatewayfunctions.h.

Member Function Documentation

uint32_t softwarecontainer::NetworkGatewayFunctions::generateIP ( const uint32_t  netmask,
const std::string  gatewayIP,
const int32_t  containerID 
)

Generate IP address for the container.

Generates an IP address and returns it

Note that a file on the system acts as a placeholder for the DHCP server. The file keeps track of the highest used IP address.

Parameters
netmask: representation available bits
gatewayIP: representation of the gateway IP
containerIDrepresentation of the containerID
Returns
uint32_t indicates assigned IP address

Definition at line 28 of file networkgatewayfunctions.cpp.

Referenced by softwarecontainer::NetworkGateway::activateGateway().

31 {
32  log_debug() << "Generating ip-address";
33  // IP generation is designed for Ipv4 in case of transition to IPv6 it should be revised
34  uint32_t internetAddress;
35 
36  /*
37  * Netmask is used for determining range of IP address assignment. maskBits variable represents
38  * bit-count for creating IP range starting from least significant bit of m_gateway. Since an
39  * ipv4 address consist of 32 bits, maskBits shall not be greater than 32. Since bit 0 and
40  * bit 31 cannot give a range but a single exact value, those will not be accepted as a maskBits.
41  */
42  if (netmask > 31 || netmask < 1) {
43  throw IPAllocationError("inappropriate netmask : " + netmask);
44  }
45 
46  // value of m_netmask interprets maskBits to a mask integer to calculate range.
47  uint32_t maskBits = (1L << (32 - netmask)) - 1;
48 
49  if ( 1 != inet_pton(AF_INET, gatewayIP.c_str(), &internetAddress)) {
50  throw IPAllocationError(gatewayIP + " does not represent a valid network address");
51  }
52  internetAddress = ntohl(internetAddress);
53 
54  //Check if generated IP address is in range of available IP addresses
55  if ((internetAddress | maskBits) < (internetAddress + containerID + 1)) {
56  throw IPAllocationError("There is no suitable IP address for this container.");
57  }
58 
59  //IP address is generated by adding 1 to container ID for provide uniqueness
60  internetAddress += (containerID + 1);
61  internetAddress = htonl(internetAddress);
62  return internetAddress;
63 }

Here is the caller graph for this function:


The documentation for this class was generated from the following files: