InterfaceAddress.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include "icl_comm/InterfaceAddress.h"
00024 
00025 #ifdef _SYSTEM_POSIX_
00026 # include <sys/types.h>
00027 # include <ifaddrs.h>
00028 # include <netinet/in.h>
00029 # include <arpa/inet.h>
00030 #else
00031 # error "No implementation available yet for non-POSIX systems!"
00032 #endif
00033 
00034 namespace icl_comm {
00035 
00036 InterfaceAddress::InterfaceAddress(const boost::asio::ip::address& interface_address,
00037                                    const boost::asio::ip::address& interface_netmask)
00038   : interface_address(interface_address),
00039     interface_netmask(interface_netmask)
00040 { }
00041 
00042 InterfaceAddressMap getInterfaceAddresses(bool ipv6_support)
00043 {
00044   using namespace boost::asio;
00045   InterfaceAddressMap result;
00046 
00047 #if defined(_SYSTEM_POSIX_)
00048   //--------------------------------------------------------------------
00049   // POSIX Implementation
00050   //--------------------------------------------------------------------
00051   struct ifaddrs *if_addresses = NULL;
00052 
00053   getifaddrs(&if_addresses);
00054   for (struct ifaddrs *iter = if_addresses; iter != NULL; iter = iter->ifa_next)
00055   {
00056     if (iter->ifa_addr->sa_family == AF_INET) // IPv4
00057     {
00058       ip::address_v4 addr(reinterpret_cast<const ip::address_v4::bytes_type&>(reinterpret_cast<struct sockaddr_in *>(iter->ifa_addr)->sin_addr.s_addr));
00059       ip::address_v4 netmask(reinterpret_cast<const ip::address_v4::bytes_type&>(reinterpret_cast<struct sockaddr_in *>(iter->ifa_netmask)->sin_addr.s_addr));
00060       result.insert(std::make_pair(icl_core::String(iter->ifa_name),
00061                                    InterfaceAddress(addr, netmask)));
00062     }
00063     else if (ipv6_support && iter->ifa_addr->sa_family == AF_INET6) // IPv6
00064     {
00065       ip::address_v6 addr(reinterpret_cast<const ip::address_v6::bytes_type&>(reinterpret_cast<struct sockaddr_in6 *>(iter->ifa_addr)->sin6_addr.s6_addr));
00066       ip::address_v6 netmask(reinterpret_cast<const ip::address_v6::bytes_type&>(reinterpret_cast<struct sockaddr_in6 *>(iter->ifa_netmask)->sin6_addr.s6_addr));
00067       result.insert(std::make_pair(icl_core::String(iter->ifa_name),
00068                                    InterfaceAddress(addr, netmask)));
00069     }
00070   }
00071   if (if_addresses)
00072   {
00073     freeifaddrs(if_addresses);
00074   }
00075   return result;
00076 #endif
00077 }
00078 
00079 }


fzi_icl_comm
Author(s):
autogenerated on Thu Jun 6 2019 21:31:31