ifaddrs.c revision 55c76589fd19e5f04697dcfd0084039cd77ef304
/*
Copyright (c) 2013, Kenneth MacKay
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ifaddrs.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <linux/rtnetlink.h>
typedef struct NetlinkList
{
struct NetlinkList *m_next;
unsigned int m_size;
} NetlinkList;
static int netlink_socket(void)
{
if(l_socket < 0)
{
return -1;
}
struct sockaddr_nl l_addr;
{
return -1;
}
return l_socket;
}
{
struct sockaddr_nl l_addr;
}
{
struct sockaddr_nl l_addr;
for(;;)
{
l_msg.msg_controllen = 0;
if(l_result < 0)
{
{
continue;
}
return -2;
}
{ // buffer was too small
return -1;
}
return l_result;
}
}
{
for(;;)
{
if(l_read == -2)
{
return NULL;
}
if(l_read >= 0)
{
for(l_hdr = (struct nlmsghdr *)l_buffer; NLMSG_OK(l_hdr, (unsigned int)l_read); l_hdr = (struct nlmsghdr *)NLMSG_NEXT(l_hdr, l_read))
{
{
continue;
}
{
*p_done = 1;
break;
}
{
return NULL;
}
}
return l_buffer;
}
l_size *= 2;
}
}
{
return l_item;
}
{
while(p_list)
{
}
}
{
{
return NULL;
}
int l_size;
int l_done = 0;
while(!l_done)
{
if(!l_hdr)
{ // error
return NULL;
}
if(!l_list)
{
}
else
{
}
}
return l_list;
}
{
return (a > b ? a : b);
}
{
switch(p_family)
{
case AF_INET:
return sizeof(struct sockaddr_in);
case AF_INET6:
return sizeof(struct sockaddr_in6);
case AF_PACKET:
default:
}
}
static void makeSockaddr(sa_family_t p_family, struct sockaddr *p_dest, void *p_data, size_t p_size)
{
switch(p_family)
{
case AF_INET:
break;
case AF_INET6:
break;
case AF_PACKET:
break;
default:
break;
}
}
{
if(!*p_resultList)
{
*p_resultList = p_entry;
}
else
{
{
}
}
}
static void interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_links, struct ifaddrs **p_resultList)
{
size_t l_nameSize = 0;
size_t l_addrSize = 0;
size_t l_dataSize = 0;
for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifinfomsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
{
case IFLA_ADDRESS:
case IFLA_BROADCAST:
break;
case IFLA_IFNAME:
break;
case IFLA_STATS:
break;
default:
break;
}
}
for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifinfomsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
{
case IFLA_ADDRESS:
case IFLA_BROADCAST:
{
{
}
else
{
}
break;
}
case IFLA_IFNAME:
break;
case IFLA_STATS:
break;
default:
break;
}
}
}
static void interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_links, struct ifaddrs **p_resultList)
{
size_t l_nameSize = 0;
size_t l_addrSize = 0;
int l_addedNetmask = 0;
for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
{
continue;
}
{
case IFA_ADDRESS:
case IFA_LOCAL:
{ // make room for netmask
l_addedNetmask = 1;
}
case IFA_BROADCAST:
break;
case IFA_LABEL:
break;
default:
break;
}
}
for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
{
case IFA_ADDRESS:
case IFA_BROADCAST:
case IFA_LOCAL:
{
{
if(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
{
}
}
{ // apparently in a point-to-point network IFA_ADDRESS contains the dest address and IFA_LOCAL contains the local address
{
}
else
{
}
}
{
{
}
}
else
{
}
break;
}
case IFA_LABEL:
break;
default:
break;
}
}
if(l_entry->ifa_addr && (l_entry->ifa_addr->sa_family == AF_INET || l_entry->ifa_addr->sa_family == AF_INET6))
{
char l_mask[16] = {0};
unsigned i;
for(i=0; i<(l_prefix/8); ++i)
{
l_mask[i] = 0xff;
}
}
}
static void interpret(int p_socket, NetlinkList *p_netlinkList, struct ifaddrs **p_links, struct ifaddrs **p_resultList)
{
{
{
{
continue;
}
{
break;
}
{
}
{
}
}
}
}
{
unsigned l_links = 0;
{
{
{
continue;
}
{
break;
}
{
++l_links;
}
}
}
return l_links;
}
{
if(!ifap)
{
return -1;
}
int l_socket = netlink_socket();
if(l_socket < 0)
{
return -1;
}
if(!l_linkResults)
{
return -1;
}
if(!l_addrResults)
{
return -1;
}
return 0;
}
{
while(ifa)
{
}
}