aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/if_addrs.cpp
diff options
context:
space:
mode:
authorBrent Stapleton <brent.stapleton@ettus.com>2019-01-18 09:45:34 -0800
committerBrent Stapleton <brent.stapleton@ettus.com>2019-01-18 17:51:35 -0800
commita53130679944ddd179593259eb953b89ab1a7a38 (patch)
tree5d8274750bed0b21aa133bc93d97d75bbce0ecd9 /host/lib/transport/if_addrs.cpp
parent2a44d6836ca08b6b67b83b63487b838e138ac379 (diff)
downloaduhd-a53130679944ddd179593259eb953b89ab1a7a38.tar.gz
uhd-a53130679944ddd179593259eb953b89ab1a7a38.tar.bz2
uhd-a53130679944ddd179593259eb953b89ab1a7a38.zip
lib: transport: apply clang-format
This is a continuation of 967be2a4. $ find host/lib/transport -iname *.hpp -o -iname *.cpp |\ xargs clang-format -i -style=file Skipping host/lib/transport/nirio/ because of build errors. $ git checkout host/lib/transport/nirio
Diffstat (limited to 'host/lib/transport/if_addrs.cpp')
-rw-r--r--host/lib/transport/if_addrs.cpp98
1 files changed, 58 insertions, 40 deletions
diff --git a/host/lib/transport/if_addrs.cpp b/host/lib/transport/if_addrs.cpp
index 109619601..a1cb6909f 100644
--- a/host/lib/transport/if_addrs.cpp
+++ b/host/lib/transport/if_addrs.cpp
@@ -6,47 +6,54 @@
//
#include <uhd/transport/if_addrs.hpp>
-#include <boost/asio/ip/address_v4.hpp>
#include <stdint.h>
+#include <boost/asio/ip/address_v4.hpp>
#include <iostream>
/***********************************************************************
* Interface address discovery through ifaddrs api
**********************************************************************/
#ifdef HAVE_GETIFADDRS
-#include <ifaddrs.h>
+# include <ifaddrs.h>
-static boost::asio::ip::address_v4 sockaddr_to_ip_addr(sockaddr *addr){
- return boost::asio::ip::address_v4(ntohl(
- reinterpret_cast<sockaddr_in*>(addr)->sin_addr.s_addr
- ));
+static boost::asio::ip::address_v4 sockaddr_to_ip_addr(sockaddr* addr)
+{
+ return boost::asio::ip::address_v4(
+ ntohl(reinterpret_cast<sockaddr_in*>(addr)->sin_addr.s_addr));
}
-std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void){
+std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void)
+{
std::vector<if_addrs_t> if_addrs;
- struct ifaddrs *ifap;
- if (getifaddrs(&ifap) == 0){
- for (struct ifaddrs *iter = ifap; iter != nullptr; iter = iter->ifa_next){
- //ensure that the entries are valid
- if (iter->ifa_addr == nullptr) continue;
- if (iter->ifa_addr->sa_family != AF_INET) continue;
- if (iter->ifa_netmask->sa_family != AF_INET) continue;
- if (iter->ifa_broadaddr->sa_family != AF_INET) continue;
-
- //append a new set of interface addresses
+ struct ifaddrs* ifap;
+ if (getifaddrs(&ifap) == 0) {
+ for (struct ifaddrs* iter = ifap; iter != nullptr; iter = iter->ifa_next) {
+ // ensure that the entries are valid
+ if (iter->ifa_addr == nullptr)
+ continue;
+ if (iter->ifa_addr->sa_family != AF_INET)
+ continue;
+ if (iter->ifa_netmask->sa_family != AF_INET)
+ continue;
+ if (iter->ifa_broadaddr->sa_family != AF_INET)
+ continue;
+
+ // append a new set of interface addresses
if_addrs_t if_addr;
- if_addr.inet = sockaddr_to_ip_addr(iter->ifa_addr).to_string();
- if_addr.mask = sockaddr_to_ip_addr(iter->ifa_netmask).to_string();
+ if_addr.inet = sockaddr_to_ip_addr(iter->ifa_addr).to_string();
+ if_addr.mask = sockaddr_to_ip_addr(iter->ifa_netmask).to_string();
if_addr.bcast = sockaddr_to_ip_addr(iter->ifa_broadaddr).to_string();
- //correct the bcast address when its same as the gateway
- if (if_addr.inet == if_addr.bcast or sockaddr_to_ip_addr(iter->ifa_broadaddr) == boost::asio::ip::address_v4(0)){
- //manually calculate broadcast address
- //https://svn.boost.org/trac/boost/ticket/5198
- const uint32_t addr = sockaddr_to_ip_addr(iter->ifa_addr).to_ulong();
- const uint32_t mask = sockaddr_to_ip_addr(iter->ifa_netmask).to_ulong();
+ // correct the bcast address when its same as the gateway
+ if (if_addr.inet == if_addr.bcast
+ or sockaddr_to_ip_addr(iter->ifa_broadaddr)
+ == boost::asio::ip::address_v4(0)) {
+ // manually calculate broadcast address
+ // https://svn.boost.org/trac/boost/ticket/5198
+ const uint32_t addr = sockaddr_to_ip_addr(iter->ifa_addr).to_ulong();
+ const uint32_t mask = sockaddr_to_ip_addr(iter->ifa_netmask).to_ulong();
const uint32_t bcast = (addr & mask) | ~mask;
- if_addr.bcast = boost::asio::ip::address_v4(bcast).to_string();
+ if_addr.bcast = boost::asio::ip::address_v4(bcast).to_string();
}
if_addrs.push_back(if_addr);
@@ -62,34 +69,44 @@ std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void){
* Interface address discovery through windows api
**********************************************************************/
#ifdef HAVE_SIO_GET_INTERFACE_LIST
-#include <winsock2.h>
+# include <winsock2.h>
-std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void){
+std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void)
+{
std::vector<if_addrs_t> if_addrs;
SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
if (sd == SOCKET_ERROR) {
- std::cerr << "Failed to get a socket. Error " << WSAGetLastError() <<
- std::endl; return if_addrs;
+ std::cerr << "Failed to get a socket. Error " << WSAGetLastError() << std::endl;
+ return if_addrs;
}
INTERFACE_INFO InterfaceList[20];
unsigned long nBytesReturned;
- if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
- sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) {
- std::cerr << "Failed calling WSAIoctl: error " << WSAGetLastError() <<
- std::endl;
- return if_addrs;
+ if (WSAIoctl(sd,
+ SIO_GET_INTERFACE_LIST,
+ 0,
+ 0,
+ &InterfaceList,
+ sizeof(InterfaceList),
+ &nBytesReturned,
+ 0,
+ 0)
+ == SOCKET_ERROR) {
+ std::cerr << "Failed calling WSAIoctl: error " << WSAGetLastError() << std::endl;
+ return if_addrs;
}
int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
for (int i = 0; i < nNumInterfaces; ++i) {
- uint32_t iiAddress = ntohl(reinterpret_cast<sockaddr_in&>(InterfaceList[i].iiAddress).sin_addr.s_addr);
- uint32_t iiNetmask = ntohl(reinterpret_cast<sockaddr_in&>(InterfaceList[i].iiNetmask).sin_addr.s_addr);
+ uint32_t iiAddress = ntohl(
+ reinterpret_cast<sockaddr_in&>(InterfaceList[i].iiAddress).sin_addr.s_addr);
+ uint32_t iiNetmask = ntohl(
+ reinterpret_cast<sockaddr_in&>(InterfaceList[i].iiNetmask).sin_addr.s_addr);
uint32_t iiBroadcastAddress = (iiAddress & iiNetmask) | ~iiNetmask;
if_addrs_t if_addr;
- if_addr.inet = boost::asio::ip::address_v4(iiAddress).to_string();
- if_addr.mask = boost::asio::ip::address_v4(iiNetmask).to_string();
+ if_addr.inet = boost::asio::ip::address_v4(iiAddress).to_string();
+ if_addr.mask = boost::asio::ip::address_v4(iiNetmask).to_string();
if_addr.bcast = boost::asio::ip::address_v4(iiBroadcastAddress).to_string();
if_addrs.push_back(if_addr);
}
@@ -104,7 +121,8 @@ std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void){
**********************************************************************/
#ifdef HAVE_IF_ADDRS_DUMMY
-std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void){
+std::vector<uhd::transport::if_addrs_t> uhd::transport::get_if_addrs(void)
+{
return std::vector<if_addrs_t>();
}