<commit_before>/*************************************************************************
*    UrBackup - Client/Server backup system
*    Copyright (C) 2011-2016 <NAME>
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Affero General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#pragma warning ( disable:4005 )
#pragma warning ( disable:4996 )

#ifdef _WIN32
#include <winsock2.h>
#endif

#include "../vld.h"
#include "../Interface/Server.h"
#include "CUDPThread.h"
#include "settings.h"
#include "packet_ids.h"
#include "log.h"
#include "FileServ.h"
#include "../stringtools.h"
#include <memory.h>
#ifdef _WIN32
#include <ws2tcpip.h>
#endif

#ifndef IPV6_ADD_MEMBERSHIP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif

namespace
{
	const char* multicast_group = "fc00:e968:6179::de52:7100:d:dd00:ef91";
}

#ifdef __APPLE__
std::string mac_get_serial()
{
	char buf[4096];
	FILE* fd = popen("/usr/sbin/system_profiler SPHardwareDataType | grep \"Serial Number\"", "r");
	std::string serial;
	if (fd != NULL)
	{
		if (fgets(buf, sizeof(buf), fd) != NULL)
		{
			serial = trim(getafter(":", buf));
		}
		pclose(fd);
	}

	if (!serial.empty())
	{
		return serial;
	}

	int64 tt = Server->getTimeSeconds() / 60;
	tt = big_endian(tt);
	char* p = reinterpret_cast<char*>(&tt);
	Server->randomFill(p, sizeof(int64)-3);
	serial = bytesToHex(reinterpret_cast<unsigned char*>(p), sizeof(int64));

	return serial;
}
#endif

std::string getSystemServerName(bool use_fqdn)
{
#ifdef __APPLE__
	//TODO: Fix FQDN for Apple
	while (true)
	{
		char hostname_appl[MAX_PATH + 15];
		FILE* fd = popen("/usr/sbin/system_profiler SPSoftwareDataType | grep \"Computer Name: \"", "r");
		if (fd != NULL)
		{
			if (fgets(hostname_appl, sizeof(hostname_appl), fd) != NULL)
			{
				std::string chostname = getafter("Computer Name: ", trim(hostname_appl));
				if (chostname.empty())
				{
					Server->wait(100);
					continue;
				}
				pclose(fd);

				const std::string mac_add_fn = "urbackup/mac_computername_add.txt";
				std::string mac_add = getStreamFile(mac_add_fn);
				if (!mac_add.empty())
				{
					return chostname + "-" + mac_add;
				}

				mac_add = mac_get_serial();

				writestring(mac_add, mac_add_fn);

				return chostname + "-" + mac_add;
			}
			else<commit_msg>Code review for last commit<commit_after>/*************************************************************************
*    UrBackup - Client/Server backup system
*    Copyright (C) 2011-2016 <NAME>
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Affero General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#pragma warning ( disable:4005 )
#pragma warning ( disable:4996 )

#ifdef _WIN32
#include <winsock2.h>
#endif

#include "../vld.h"
#include "../Interface/Server.h"
#include "CUDPThread.h"
#include "settings.h"
#include "packet_ids.h"
#include "log.h"
#include "FileServ.h"
#include "../stringtools.h"
#include <memory.h>
#ifdef _WIN32
#include <ws2tcpip.h>
#endif

#ifndef IPV6_ADD_MEMBERSHIP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif

namespace
{
	const char* multicast_group = "fc00:e968:6179::de52:7100:d:dd00:ef91";
}

#ifdef __APPLE__
std::string mac_get_serial()
{
	char buf[4096];
	FILE* fd = popen("/usr/sbin/system_profiler SPHardwareDataType | grep \"Serial Number\"", "r");
	std::string serial;
	if (fd != NULL)
	{
		if (fgets(buf, sizeof(buf), fd) != NULL)
		{
			serial = trim(getafter(":", buf));
		}
		pclose(fd);
	}

	if (!serial.empty())
	{
		return serial;
	}

	int64 tt = Server->getTimeSeconds() / 60;
	tt = big_endian(tt);
	char* p = reinterpret_cast<char*>(&tt);
	Server->randomFill(p, sizeof(int64)-3);
	serial = bytesToHex(reinterpret_cast<unsigned char*>(p), sizeof(int64));

	return serial;
}
#endif

std::string getSystemServerName(bool use_fqdn)
{
#ifdef __APPLE__
	//TODO: Fix FQDN for Apple
	while (true)
	{
		char hostname_appl[MAX_PATH + 15];
		FILE* fd = popen("/usr/sbin/system_profiler SPSoftwareDataType | grep \"Computer Name: \"", "r");
		if (fd != NULL)
		{
			if (fgets(hostname_appl, sizeof(hostname_appl), fd) != NULL)
			{
				pclose(fd);
				std::string chostname = getafter("Computer Name: ", trim(hostname_appl));
				if (chostname.empty())
				{
					Server->wait(100);
					continue;
				}

				const std::string mac_add_fn = "urbackup/mac_computername_add.txt";
				std::string mac_add = getStreamFile(mac_add_fn);
				if (!mac_add.empty())
				{
					return chostname + "-" + mac_add;
				}

				mac_add = mac_get_serial();

				writestring(mac_add, mac_add_fn);

				return chostname + "-" + mac_add;
			}
			else
{'fraction_non_alphanumeric': 0.17669172932330826, 'fraction_numerical': 0.0215311004784689, 'mean_word_length': 4.138718173836699, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1437794', 'n_tokens_mistral': 2211, 'n_tokens_neox': 1978, 'n_words': 537}