Handles various network operations over netlink.
More...
#include <netlink.h>
|
typedef std::pair< rtattr, void * > | AttributeInfo |
|
typedef std::vector< AttributeInfo > | AttributeList |
|
typedef std::pair< ifinfomsg, AttributeList > | LinkInfo |
|
typedef std::pair< ifaddrmsg, AttributeList > | AddressInfo |
|
typedef std::pair< rtmsg, AttributeList > | RouteInfo |
|
|
| LOG_DECLARE_CLASS_CONTEXT ("NETL","Netlink") |
|
| Netlink () |
| Construct a new Netlink object. More...
|
|
virtual | ~Netlink () |
| release all resources held by the Netlink object More...
|
|
bool | getKernelDump () |
| get a dump of all links, addresses and routes from the kernel. More...
|
|
virtual bool | checkKernelDump () |
| check for a kernel dump, and if not present, try to get one More...
|
|
bool | setDefaultGateway (const char *gatewayAddress) |
| Sets an ip address as the default gateway. More...
|
|
bool | linkUp (const int ifaceIndex) |
| Bring the given interface up. More...
|
|
bool | linkDown (const int ifaceIndex) |
| Bring a given interface down. More...
|
|
bool | setIP (const int ifaceIndex, const in_addr ip, const unsigned char netmask) |
| Sets an IP address for a network link. More...
|
|
bool | findLink (const char *ifaceName, LinkInfo &linkInfo) |
| Check that the device given is a network bridge. More...
|
|
bool | findAddresses (const unsigned int interfaceIndex, std::vector< AddressInfo > &result) |
| Get all addresses associated with the given interface index. More...
|
|
bool | hasAddress (const std::vector< AddressInfo > &haystack, const int addressFamily, const char *needle) |
| checks if an address is present in the given list More...
|
|
Handles various network operations over netlink.
Some of the code in this class is based on code shown here, written by Jean Lorchat http://iijean.blogspot.se/2010/03/howto-get-list-of-network-interfaces-in.html
Definition at line 37 of file netlink.h.
softwarecontainer::Netlink::Netlink |
( |
| ) |
|
Construct a new Netlink object.
This will do a setup of the sockets needed to talk netlink to the kernel, and will also call getKernelDump()
Definition at line 30 of file netlink.cpp.
References getKernelDump().
33 m_hasKernelDump =
false;
34 m_netlinkInitialized =
false;
36 if (!setupNetlink()) {
37 fprintf(stderr,
"Failed to setup netlink\n");
41 fprintf(stderr,
"Failed to initialize cache\n");
bool getKernelDump()
get a dump of all links, addresses and routes from the kernel.
softwarecontainer::Netlink::~Netlink |
( |
| ) |
|
|
virtual |
release all resources held by the Netlink object
Since we allocate everything on the stack, except for the netlink attributes which we save as void pointers, we basically go through all data structures and free those pointers.
Also shuts down the communication with the kernel.
Definition at line 45 of file netlink.cpp.
47 if (m_netlinkInitialized) {
48 shutdown(m_fd, SHUT_RDWR);
52 if (m_hasKernelDump) {
bool softwarecontainer::Netlink::getKernelDump |
( |
| ) |
|
get a dump of all links, addresses and routes from the kernel.
To get a dump of all links/addresses/routes we only need to use the generic netlink family message type, and for that we only need to set family (AF_PACKET).
This is done by sending RTM_GETLINK, RTM_GETADDR and RTM_GETROUTE as a general message (rtgenmsg) to the kernel.
After running this successfully, there will be a local cache of these network objects, which is needed to run some of the other functions.
- Returns
- true on success, false otherwise
- See also
- clearCache()
Definition at line 458 of file netlink.cpp.
Referenced by checkKernelDump(), and Netlink().
460 m_hasKernelDump =
false;
462 netlink_request<rtgenmsg> link_msg = createMessage<rtgenmsg>(RTM_GETLINK, NLM_F_DUMP);
463 link_msg.pay.rtgen_family = AF_PACKET;
464 if (!sendMessage(link_msg)) {
465 std::cerr <<
"Could not send link message" << std::endl;
469 netlink_request<rtgenmsg> addr_msg = createMessage<rtgenmsg>(RTM_GETADDR, NLM_F_DUMP);
470 addr_msg.pay.rtgen_family = AF_PACKET;
471 if (!sendMessage(addr_msg)) {
472 std::cerr <<
"Could not send address message" << std::endl;
476 netlink_request<rtgenmsg> route_msg = createMessage<rtgenmsg>(RTM_GETROUTE, NLM_F_DUMP);
477 route_msg.pay.rtgen_family = AF_PACKET;
478 if (!sendMessage(route_msg)) {
479 std::cerr <<
"Could not send route message" << std::endl;
483 m_hasKernelDump =
true;
bool softwarecontainer::Netlink::checkKernelDump |
( |
| ) |
|
|
virtual |
check for a kernel dump, and if not present, try to get one
- Returns
- true if there a kernel dump was found or could be fetched
-
false otherwise
Definition at line 487 of file netlink.cpp.
References getKernelDump().
Referenced by findAddresses(), findLink(), and linkUp().
490 std::cerr <<
"Could not get cache dump from kernel" << std::endl;
bool getKernelDump()
get a dump of all links, addresses and routes from the kernel.
bool softwarecontainer::Netlink::setDefaultGateway |
( |
const char * |
gatewayAddress | ) |
|
Sets an ip address as the default gateway.
- Parameters
-
gatewayAddress | the address to set |
- Returns
- true on success
-
false otherwise
Definition at line 175 of file netlink.cpp.
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
177 netlink_request<rtmsg> set_gw = createMessage<rtmsg>(RTM_NEWROUTE, NLM_F_CREATE | NLM_F_REPLACE);
178 set_gw.pay.rtm_family = AF_INET;
179 set_gw.pay.rtm_table = RT_TABLE_MAIN;
180 set_gw.pay.rtm_protocol = RTPROT_STATIC;
181 set_gw.pay.rtm_scope = RT_SCOPE_UNIVERSE;
182 set_gw.pay.rtm_type = RTN_UNICAST;
184 struct in_addr gw_addr;
185 if (inet_aton(gatewayAddress, &gw_addr) == 0) {
188 addAttribute(set_gw, RTA_GATEWAY,
sizeof(gw_addr), &gw_addr);
190 return sendMessage(set_gw);
bool softwarecontainer::Netlink::linkUp |
( |
const int |
ifaceIndex | ) |
|
Bring the given interface up.
Sets the UP flag for the given interface, if it exists.
- Parameters
-
ifaceIndex | the index for the interface to bring up |
- Returns
- true if the interface was found and brought up
-
false otherwise
Definition at line 194 of file netlink.cpp.
References checkKernelDump().
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
200 for (LinkInfo link : m_links) {
201 ifinfomsg ifinfo = link.first;
202 if (ifinfo.ifi_index != ifaceIndex) {
207 netlink_request<ifinfomsg> msg_up = createMessage<ifinfomsg>(RTM_NEWLINK, NLM_F_CREATE);
208 msg_up.pay.ifi_family = AF_UNSPEC;
209 msg_up.pay.ifi_flags = ifinfo.ifi_flags | IFF_UP;
210 msg_up.pay.ifi_change |= IFF_UP;
211 msg_up.pay.ifi_index = ifinfo.ifi_index;
212 if (!sendMessage(msg_up)) {
213 fprintf(stderr,
"Failed to bring device %i up\n", ifinfo.ifi_index);
virtual bool checkKernelDump()
check for a kernel dump, and if not present, try to get one
bool softwarecontainer::Netlink::linkDown |
( |
const int |
ifaceIndex | ) |
|
Bring a given interface down.
- Parameters
-
ifaceIndex | the index for the interface to bring down |
- Returns
- true if interface was found and brought down
-
false otherwise
Definition at line 261 of file netlink.cpp.
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
263 for (LinkInfo link : m_links) {
264 ifinfomsg ifinfo = link.first;
265 if (ifinfo.ifi_type == ARPHRD_LOOPBACK) {
269 if (ifinfo.ifi_index != ifaceIndex) {
273 netlink_request<ifinfomsg> down_msg = createMessage<ifinfomsg>(RTM_NEWLINK, 0);
274 down_msg.pay.ifi_family = AF_UNSPEC;
275 down_msg.pay.ifi_index = ifinfo.ifi_index;
276 down_msg.pay.ifi_flags = ~IFF_UP;
277 down_msg.pay.ifi_change = IFF_UP;
279 if (!sendMessage(down_msg)) {
bool softwarecontainer::Netlink::setIP |
( |
const int |
ifaceIndex, |
|
|
const in_addr |
ip, |
|
|
const unsigned char |
netmask |
|
) |
| |
Sets an IP address for a network link.
- Parameters
-
ifaceIndex | the index for the interface to set ip for |
ip | the ipv4 address to set |
netmask | the netmask in CIDR format (for example 24) |
- Returns
- true if interface was found and IP was set
-
false otherwise
Definition at line 223 of file netlink.cpp.
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
225 for (LinkInfo link : m_links) {
226 ifinfomsg ifinfo = link.first;
227 if (ifinfo.ifi_index != ifaceIndex) {
232 netlink_request<ifaddrmsg> msg_setip = createMessage<ifaddrmsg>(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE);
233 msg_setip.pay.ifa_family = AF_INET;
234 msg_setip.pay.ifa_prefixlen = netmask;
235 msg_setip.pay.ifa_scope = RT_SCOPE_UNIVERSE;
236 msg_setip.pay.ifa_index = ifinfo.ifi_index;
240 in_addr_t inet_netmask = (1 << netmask) -1;
241 in_addr_t inet_bcast = ip.s_addr | (~inet_netmask);
242 in_addr bcast_addr = { inet_bcast };
244 addAttribute(msg_setip, IFA_LOCAL,
sizeof(ip), &ip);
245 addAttribute(msg_setip, IFA_BROADCAST,
sizeof(bcast_addr), &bcast_addr);
248 if (!sendMessage(msg_setip)) {
250 fprintf(stderr,
"Failed to set ip on link %i\n", ifinfo.ifi_index);
bool softwarecontainer::Netlink::findLink |
( |
const char * |
ifaceName, |
|
|
LinkInfo & |
linkInfo |
|
) |
| |
Check that the device given is a network bridge.
- Parameters
-
ifaceName | the name of the interface |
ifaceIndex | the index of the interface (out parameter) |
- Returns
- true if interface with matching name was found.
-
false otherwise.
Definition at line 289 of file netlink.cpp.
References checkKernelDump().
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
295 for (LinkInfo link : m_links) {
296 AttributeList attributes = link.second;
297 for (AttributeInfo attrinfo : attributes) {
298 rtattr attr = attrinfo.first;
299 void *data = attrinfo.second;
301 if (attr.rta_type == IFLA_IFNAME) {
302 char *ifname = (
char *) data;
303 if (strcmp(ifname, ifaceName) == 0) {
virtual bool checkKernelDump()
check for a kernel dump, and if not present, try to get one
bool softwarecontainer::Netlink::findAddresses |
( |
const unsigned int |
interfaceIndex, |
|
|
std::vector< AddressInfo > & |
result |
|
) |
| |
Get all addresses associated with the given interface index.
- Parameters
-
interfaceIndex | the interface address to get addresses for |
result | the vector to place all addresses in (out parameter) |
- Returns
- true on success
-
false otherwise
Definition at line 314 of file netlink.cpp.
References checkKernelDump().
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
320 for (AddressInfo addressInfo : m_addresses) {
321 ifaddrmsg addrmsg = addressInfo.first;
322 if (addrmsg.ifa_index == interfaceIndex) {
323 result.push_back(addressInfo);
virtual bool checkKernelDump()
check for a kernel dump, and if not present, try to get one
bool softwarecontainer::Netlink::hasAddress |
( |
const std::vector< AddressInfo > & |
haystack, |
|
|
const int |
addressFamily, |
|
|
const char * |
needle |
|
) |
| |
checks if an address is present in the given list
- Parameters
-
haystack | the list to search in |
addressFamily | the address family, AF_INET or AF_INET6 |
needle | the ip address to search for, in dotted notation |
- Returns
- true if the address is in the haystack
-
false otherwise
Definition at line 331 of file netlink.cpp.
Referenced by softwarecontainer::NetworkGateway::teardownGateway().
335 for (AddressInfo addressInfo : haystack) {
336 AttributeList attributes = addressInfo.second;
337 for (AttributeInfo attrPair : attributes) {
340 rtattr attr = attrPair.first;
341 if (attr.rta_type != IFA_ADDRESS && attr.rta_type != IFA_LOCAL) {
345 void *data = attrPair.second;
346 char out[INET6_ADDRSTRLEN];
349 if (inet_ntop(addressFamily, data, out,
sizeof(out)) && strcmp(out, needle) == 0) {
355 std::cout <<
"Netlink does not have address" << std::endl;
The documentation for this class was generated from the following files:
- libsoftwarecontainer/src/gateway/network/netlink.h
- libsoftwarecontainer/src/gateway/network/netlink.cpp