Move getifaddrs to libbnetapi

* Since it uses BNetworkAddress, it can't be in libnetwork.
This commit is contained in:
Adrien Destugues 2015-01-16 13:26:35 +01:00
parent e2fc7cd3c7
commit 46c267cf18
3 changed files with 89 additions and 71 deletions

View File

@ -1,10 +1,9 @@
/*
* Copyright 2006-2015, Haiku, Inc. All Rights Reserved.
* Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, axeld@pinc-software.de
* Adrien Destugues, pulkomandy@pulkomandy.tk
*/
@ -17,9 +16,6 @@
#include <unistd.h>
#include <AutoDeleter.h>
#include <NetworkAddress.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include "compatibility/bsd/ifaddrs.h"
@ -147,69 +143,3 @@ if_freenameindex(struct if_nameindex *interfaceArray)
}
free(interfaceArray);
}
int getifaddrs(struct ifaddrs **ifap)
{
if (ifap == NULL) {
errno = B_BAD_VALUE;
return -1;
}
BNetworkRoster& roster = BNetworkRoster::Default();
uint32 cookie;
struct ifaddrs* previous = NULL;
struct ifaddrs* current = NULL;
BNetworkInterface* interface = new BNetworkInterface();
while (roster.GetNextInterface(&cookie, *interface) == B_OK) {
BNetworkInterfaceAddress address;
int32 i = 0;
while (interface->GetAddressAt(i++, address) == B_OK) {
current = new ifaddrs();
// Chain this interface with the next one
current->ifa_next = previous;
previous = current;
current->ifa_data = interface;
current->ifa_name = interface->Name();
// Points to the name in the BNetworkInterface instance, which
// is added as ifa_data so freeifaddrs can release it.
current->ifa_flags = address.Flags();
current->ifa_addr = new sockaddr(address.Address().SockAddr());
current->ifa_netmask = new sockaddr(address.Mask().SockAddr());
current->ifa_dstaddr = new sockaddr(address.Destination().SockAddr());
}
interface = new BNetworkInterface();
}
delete interface;
*ifap = current;
return 0;
}
void freeifaddrs(struct ifaddrs *ifa)
{
struct ifaddrs* next;
BNetworkInterface* interface = NULL;
while (ifa != NULL) {
if (ifa->ifa_data != interface) {
interface = (BNetworkInterface*)ifa->ifa_data;
delete interface;
}
delete ifa->ifa_addr;
delete ifa->ifa_netmask;
delete ifa->ifa_dstaddr;
next = ifa->ifa_next;
delete ifa;
ifa = next;
}
}

View File

@ -79,6 +79,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
HttpResult.cpp
HttpTime.cpp
getifaddrs.cpp
notifications.cpp
$(md5Sources)

View File

@ -0,0 +1,87 @@
/*
* Copyright 2015, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Adrien Destugues, pulkomandy@pulkomandy.tk
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <AutoDeleter.h>
#include <NetworkAddress.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include "compatibility/bsd/ifaddrs.h"
int getifaddrs(struct ifaddrs **ifap)
{
if (ifap == NULL) {
errno = B_BAD_VALUE;
return -1;
}
BNetworkRoster& roster = BNetworkRoster::Default();
uint32 cookie;
struct ifaddrs* previous = NULL;
struct ifaddrs* current = NULL;
BNetworkInterface* interface = new BNetworkInterface();
while (roster.GetNextInterface(&cookie, *interface) == B_OK) {
BNetworkInterfaceAddress address;
int32 i = 0;
while (interface->GetAddressAt(i++, address) == B_OK) {
current = new ifaddrs();
// Chain this interface with the next one
current->ifa_next = previous;
previous = current;
current->ifa_data = interface;
current->ifa_name = interface->Name();
// Points to the name in the BNetworkInterface instance, which
// is added as ifa_data so freeifaddrs can release it.
current->ifa_flags = address.Flags();
current->ifa_addr = new sockaddr(address.Address().SockAddr());
current->ifa_netmask = new sockaddr(address.Mask().SockAddr());
current->ifa_dstaddr = new sockaddr(address.Destination().SockAddr());
}
interface = new BNetworkInterface();
}
delete interface;
*ifap = current;
return 0;
}
void freeifaddrs(struct ifaddrs *ifa)
{
struct ifaddrs* next;
BNetworkInterface* interface = NULL;
while (ifa != NULL) {
if (ifa->ifa_data != interface) {
interface = (BNetworkInterface*)ifa->ifa_data;
delete interface;
}
delete ifa->ifa_addr;
delete ifa->ifa_netmask;
delete ifa->ifa_dstaddr;
next = ifa->ifa_next;
delete ifa;
ifa = next;
}
}