<commit_before>/*
 * Copyright (C) 2013, Pelagicore AB <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "generators.h"

const static char *iface_counter_file = "/tmp/pelc_ifc";

char *gen_net_iface_name (char *ip_addr_net)
{
	struct  ifaddrs *ifaddr, *ifa;
	char   *iface     = NULL;
	int     collision = 0;

	iface = malloc (sizeof (char) * 16);
	if (!iface) {
		printf ("Failed to malloc iface\n");
		return NULL;
	}

	do {
		snprintf (iface, 20, "veth-%d", ip_addr_net, (rand() % 253) + 1);

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

		/* Iterate through the device list */
		for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
			if (ifa->ifa_name == NULL)
				continue;
			else {
				if (strcmp (ifa->ifa_name, iface) == 0) {
					collision = 1;
					break;
				}
			}
		}
	}
	while (collision);

	return iface;
}

char *gen_ip_addr (char *ip_addr_net)
{
	char *ip    = malloc (sizeof (char) * 20);
	char *buf   = calloc(sizeof (char), 4);
	int fd      = open (iface_counter_file, O_CREAT | O_RDWR);
	int counter = 0;

	if (fd == -1) {
		printf ("Unable to lock interface counter\n");
		return NULL;
	}
	flock (fd, LOCK_EX);

	if (read (fd, buf, 3) == 0)
		counter = 1;
	else
		counter = atoi (buf);

	/* We reserve the first IP for gateway .. */
	snprintf(buf, 4, "%03d", (counter % 254) + 2);

	lseek (fd, 0, SEEK_SET);
	write (fd, buf, 3);

	flock(fd, LOCK_UN);
	close (fd);

	free (buf);
	snprintf (ip, 20, "%s%d", ip_addr_net, counter);
	return ip;
}

char *gen_lxc_config (struct lxc_params *params)
{
	char cmd[1024];
	FILE *cfg;
	char *iface_line = malloc (sizeof (char) * 150);
	size_t status;

	debug ("Generating config to %s for IP %s\n",
	       params->lxc_cfg_file,
	       params->ip_addr);

	/* copy system config to temporary location */
	snprintf (cmd, 1024, "cp %s %s", params->lxc_system_cfg,
	                                 params->lxc_cfg_file);
	system (cmd);

	/* Add ipv4 config to config */
	snprintf (iface_line, 150, "lxc.network.veth.pair = %s\n"
	                           "lxc.network.ipv4 = %s/24\n"
	                           "lxc.network.ipv4.gateway = %s\n",
	                           params->net_iface_name,
	                           params->ip_addr,
	                           params->gw_addr);

	cfg = fopen (params->lxc_cfg_file, "a+");
	status = fwrite (iface_line,
	                 sizeof (char) * strlen (iface_line),
	                 1,
	                 cfg);
	fclose (cfg);

	free (iface_line);
}

char *gen_ct_name ()
{
	struct timeval time;
	gettimeofday(&time,NULL);
	char *name = malloc (sizeof (char) * 10);
	int   i    = 0;
	srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
	static const char alphanum[] =
        "abcdefghijklmnopqrstuvwxyz";

	for (i = 0; i < 10; i++) {
		name[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
	}

	name[9] = 0;
	return name;
}
<commit_msg>Cleaned up sources<commit_after>/*
 * Copyright (C) 2013, Pelagicore AB <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "generators.h"

const static char *iface_counter_file = "/tmp/pelc_ifc";

char *gen_net_iface_name (char *ip_addr_net)
{
	struct  ifaddrs *ifaddr, *ifa;
	char   *iface     = NULL;
	int     collision = 0;

	iface = malloc (sizeof (char) * 16);
	if (!iface) {
		printf ("Failed to malloc iface\n");
		return NULL;
	}

	do {
		snprintf (iface, 20, "veth-%d", (rand() % 1024));

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

		/* Iterate through the device list */
		for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
			if (ifa->ifa_name == NULL)
				continue;
			else {
				if (strcmp (ifa->ifa_name, iface) == 0) {
					collision = 1;
					break;
				}
			}
		}
	}
	while (collision);

	return iface;
}

char *gen_ip_addr (char *ip_addr_net)
{
	char *ip      = malloc (sizeof (char) * 20);
	char *buf     = calloc (sizeof (char), 4);
	int   fd      = open (iface_counter_file, O_CREAT | O_RDWR);
	int   counter = 0;

	if (fd == -1) {
		printf ("Unable to lock interface counter\n");
		return NULL;
	}
	flock (fd, LOCK_EX);

	if (read (fd, buf, 3) == 0)
		counter = 1;
	else
		counter = atoi (buf);

	/* We reserve the first IP for gateway .. */
	snprintf(buf, 4, "%03d", (counter % 254) + 2);

	/* Overwrite the first three bytes */
	lseek (fd, 0, SEEK_SET);
	write (fd, buf, 3);

	flock(fd, LOCK_UN);
	close (fd);

	free (buf);
	snprintf (ip, 20, "%s%d", ip_addr_net, counter);
	return ip;
}

char *gen_lxc_config (struct lxc_params *params)
{
	FILE  *cfg;
	char  *iface_line = malloc (sizeof (char) * 150);
	char   cmd[1024];
	size_t status;

	debug ("Generating config to %s for IP %s\n",
	       params->lxc_cfg_file,
	       params->ip_addr);

	/* copy system config to temporary location */
	snprintf (cmd, 1024, "cp %s %s", params->lxc_system_cfg,
	                                 params->lxc_cfg_file);
	system (cmd);

	/* Add ipv4 config to config */
	snprintf (iface_line, 150, "lxc.network.veth.pair = %s\n"
	                           "lxc.network.ipv4 = %s/24\n"
	                           "lxc.network.ipv4.gateway = %s\n",
	                           params->net_iface_name,
	                           params->ip_addr,
	                           params->gw_addr);

	cfg    = fopen (params->lxc_cfg_file, "a+");
	status = fwrite (iface_line,
	                 sizeof (char) * strlen (iface_line),
	                 1,
	                 cfg);
	fclose (cfg);

	free (iface_line);
}

char *gen_ct_name ()
{
	struct timeval time;
	gettimeofday(&time,NULL);
	char *name = malloc (sizeof (char) * 10);
	int   i    = 0;
	srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
	static const char alphanum[] =
        "abcdefghijklmnopqrstuvwxyz";

	for (i = 0; i < 10; i++) {
		name[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
	}

	name[9] = 0;
	return name;
}

{'fraction_non_alphanumeric': 0.13351648351648351, 'fraction_numerical': 0.023351648351648352, 'mean_word_length': 2.625996015936255, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3661190', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2576, 'n_words': 808}