<commit_before>/*************************************************************************
 * Copyright (c) 2014 eProsima. All rights reserved.
 *
 * This copy of eProsima Fast RTPS is licensed to you under the terms described in the
 * FASTRTPS_LIBRARY_LICENSE file included in this distribution.
 *
 *************************************************************************/

/**
 * @file IPFinder.cpp
 *
 */

#include <fastrtps/utils/IPFinder.h>

#if defined(_WIN32)
#include <stdio.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <assert.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#endif



namespace eprosima {
namespace fastrtps{

IPFinder::IPFinder() {


}

IPFinder::~IPFinder() {

}

#if defined(_WIN32)


bool IPFinder::getIPs(std::vector<info_IP>* vec_name)
{
	DWORD rv, size;
	PIP_ADAPTER_ADDRESSES adapter_addresses, aa;
	PIP_ADAPTER_UNICAST_ADDRESS ua;

	rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &size);


	if (rv != ERROR_BUFFER_OVERFLOW) {
		fprintf(stderr, "GetAdaptersAddresses() failed...");
		return false;
	}
	adapter_addresses = (PIP_ADAPTER_ADDRESSES)malloc(size);

	rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addresses, &size);
	if (rv != ERROR_SUCCESS) {
		fprintf(stderr, "GetAdaptersAddresses() failed...");
		free(adapter_addresses);
		return false;
	}
	//cout << "IP INFORMATION " << endl;
	for (aa = adapter_addresses; aa != NULL; aa = aa->Next) {
		if (aa->OperStatus == 1) //is ENABLED
		{
			for (ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) {
				char buf[BUFSIZ];

				int family = ua->Address.lpSockaddr->sa_family;

				if (family == AF_INET || family == AF_INET6) //IP4
				{
					//printf("\t%s ",  family == AF_INET ? "IPv4":"IPv6");
					memset(buf, 0, BUFSIZ);
					getnameinfo(ua->Address.lpSockaddr, ua->Address.iSockaddrLength, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
					info_IP info;
					info.type = family == AF_INET ? IP4 : IP6;
					info.name = std::string(buf);
					if (info.type == IP4 && !parseIP4(info.name, &info.locator))
						info.type = IP4_LOCAL;
					else if (info.type == IP6 && !parseIP6(info.name, &info.locator))
						info.type = IP6_LOCAL;
					if (info.type == IP6)
					{
						sockaddr_in6* so = (sockaddr_in6*)ua->Address.lpSockaddr;
						info.scope_id = so->sin6_scope_id;
					}
					vec_name->push_back(info);
					//printf("Buffer: %s\n", buf);
				}
			}
		}
	}

	free(adapter_addresses);
	return true;
}

#else

bool IPFinder::getIPs(std::vector<info_IP>* vec_name )
{
	struct ifaddrs *ifaddr, *ifa;
	int family, s;
	char host[NI_MAXHOST];

	if (getifaddrs(&ifaddr) == -1) {
		perror("getifaddrs");
		exit(EXIT_FAILURE);
	}

	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
	{
		family = ifa->ifa_addr->sa_family;

		if (family == AF_INET)
		{
			s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
					host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
			if (s != 0) {
				printf("getnameinfo() failed: %s\n", gai_strerror(s));
                freeifaddrs(ifaddr);
				exit(EXIT_FAILURE);
			}
			info_IP info;
			info.type = IP4;
			info.name = std::string(host);
			if(!parseIP4(info.name,&info.locator))
				info.type = IP4_LOCAL;
			vec_name->push_back(info);
			//printf("<Interface>: %s \t <Address> %s\n", ifa->ifa_name, host);

		}
		else if(family == AF_INET6)
		{
			s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),<commit_msg>refs #1698. Null network interface fix<commit_after>/*************************************************************************
 * Copyright (c) 2014 eProsima. All rights reserved.
 *
 * This copy of eProsima Fast RTPS is licensed to you under the terms described in the
 * FASTRTPS_LIBRARY_LICENSE file included in this distribution.
 *
 *************************************************************************/

/**
 * @file IPFinder.cpp
 *
 */

#include <fastrtps/utils/IPFinder.h>

#if defined(_WIN32)
#include <stdio.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <assert.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#endif



namespace eprosima {
namespace fastrtps{

IPFinder::IPFinder() {


}

IPFinder::~IPFinder() {

}

#if defined(_WIN32)


bool IPFinder::getIPs(std::vector<info_IP>* vec_name)
{
	DWORD rv, size;
	PIP_ADAPTER_ADDRESSES adapter_addresses, aa;
	PIP_ADAPTER_UNICAST_ADDRESS ua;

	rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &size);


	if (rv != ERROR_BUFFER_OVERFLOW) {
		fprintf(stderr, "GetAdaptersAddresses() failed...");
		return false;
	}
	adapter_addresses = (PIP_ADAPTER_ADDRESSES)malloc(size);

	rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addresses, &size);
	if (rv != ERROR_SUCCESS) {
		fprintf(stderr, "GetAdaptersAddresses() failed...");
		free(adapter_addresses);
		return false;
	}
	//cout << "IP INFORMATION " << endl;
	for (aa = adapter_addresses; aa != NULL; aa = aa->Next) {
		if (aa->OperStatus == 1) //is ENABLED
		{
			for (ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) {
				char buf[BUFSIZ];

				int family = ua->Address.lpSockaddr->sa_family;

				if (family == AF_INET || family == AF_INET6) //IP4
				{
					//printf("\t%s ",  family == AF_INET ? "IPv4":"IPv6");
					memset(buf, 0, BUFSIZ);
					getnameinfo(ua->Address.lpSockaddr, ua->Address.iSockaddrLength, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
					info_IP info;
					info.type = family == AF_INET ? IP4 : IP6;
					info.name = std::string(buf);
					if (info.type == IP4 && !parseIP4(info.name, &info.locator))
						info.type = IP4_LOCAL;
					else if (info.type == IP6 && !parseIP6(info.name, &info.locator))
						info.type = IP6_LOCAL;
					if (info.type == IP6)
					{
						sockaddr_in6* so = (sockaddr_in6*)ua->Address.lpSockaddr;
						info.scope_id = so->sin6_scope_id;
					}
					vec_name->push_back(info);
					//printf("Buffer: %s\n", buf);
				}
			}
		}
	}

	free(adapter_addresses);
	return true;
}

#else

bool IPFinder::getIPs(std::vector<info_IP>* vec_name )
{
	struct ifaddrs *ifaddr, *ifa;
	int family, s;
	char host[NI_MAXHOST];

	if (getifaddrs(&ifaddr) == -1) {
		perror("getifaddrs");
		exit(EXIT_FAILURE);
	}

	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
	{
      if (ifa->ifa_addr == NULL)
         continue;

		family = ifa->ifa_addr->sa_family;

		if (family == AF_INET)
		{
			s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
					host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
			if (s != 0) {
				printf("getnameinfo() failed: %s\n", gai_strerror(s));
                freeifaddrs(ifaddr);
				exit(EXIT_FAILURE);
			}
			info_IP info;
			info.type = IP4;
			info.name = std::string(host);
			if(!parseIP4(info.name,&info.locator))
				info.type = IP4_LOCAL;
			vec_name->push_back(info);
			//printf("<Interface>: %s \t <Address> %s\n", ifa->ifa_name, host);

		}
		else if(family == AF_INET6)
		{
			s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
{'fraction_non_alphanumeric': 0.19190384084001105, 'fraction_numerical': 0.010776457584968223, 'mean_word_length': 3.92784206943499, 'pattern_counts': {'":': 2, '<': 47, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6244283', 'n_tokens_mistral': 3149, 'n_tokens_neox': 2758, 'n_words': 554}