<commit_before>#include "configuration.h"

/* Plugin includes */
#include "pud.h"
#include "netTools.h"
#include "nmeaTools.h"
#include "nodeIdConversion.h"
#include "networkInterfaces.h"

/* OLSR includes */
#include "olsr_types.h"
#include "olsr_cfg.h"

/* System includes */
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>

/*
 * Utility functions
 */

/**
 Read an unsigned long long number from a value string

 @param valueName
 the name of the value
 @param value
 the string to convert to a number
 @param valueNumber
 a pointer to the location where to store the number upon successful conversion

 @return
 - true on success
 - false otherwise
 */
static bool readULL(const char * valueName, const char * value,
		unsigned long long * valueNumber) {
	char * endPtr = NULL;
	unsigned long long valueNew;

	errno = 0;
	valueNew = strtoull(value, &endPtr, 10);

	if (!((endPtr != value) && (*value != '\0') && (*endPtr == '\0'))) {
		/* invalid conversion */
		pudError(true, "Configured %s (%s) could not be converted to a number",
				valueName, value);
		return false;
	}

	*valueNumber = valueNew;

	return true;
}

/**
 Read a double number from a value string

 @param valueName
 the name of the value
 @param value
 the string to convert to a number
 @param valueNumber
 a pointer to the location where to store the number upon successful conversion

 @return
 - true on success
 - false otherwise
 */
static bool readDouble(const char * valueName, const char * value,
		double * valueNumber) {
	char * endPtr = NULL;
	double valueNew;

	errno = 0;
	valueNew = strtod(value, &endPtr);

	if (!((endPtr != value) && (*value != '\0') && (*endPtr == '\0'))) {
		/* invalid conversion */
		pudError(true, "Configured %s (%s) could not be converted to a number",
				valueName, value);
		return false;
	}

	*valueNumber = valueNew;

	return true;
}

/*
 * nodeIdType
 */

/** The nodeIdType */
static NodeIdType nodeIdType = PUD_NODE_ID_TYPE_DEFAULT;

/**
 @return
 The node ID type
 */
NodeIdType getNodeIdTypeNumber(void) {
	return nodeIdType;
}

/**
 Set the node ID type.

 @param value
 The value of the node ID type to set (a number in string representation)
 @param data
 Unused
 @param addon
 Unused

 @return
 - true when an error is detected
 - false otherwise
 */
int setNodeIdType(const char *value, void *data __attribute__ ((unused)),
		set_plugin_parameter_addon addon __attribute__ ((unused))) {
	static const char * valueName = PUD_NODE_ID_TYPE_NAME;
	unsigned long long nodeIdTypeNew;

	assert (value != NULL);

	if (!readULL(valueName, value, &nodeIdTypeNew)) {
		return true;
	}

	if (nodeIdTypeNew > PUD_NODE_ID_TYPE_MAX) {
		pudError(false, "Configured %s (%llu) is out of range 0-%u", valueName,
				nodeIdTypeNew, PUD_NODE_ID_TYPE_MAX);
		return true;
	}

	switch (nodeIdTypeNew) {
		case PUD_NODEIDTYPE_MAC:
		case PUD_NODEIDTYPE_MSISDN:
		case PUD_NODEIDTYPE_TETRA:
		case PUD_NODEIDTYPE_DNS:
		case PUD_NODEIDTYPE_IPV4:
		case PUD_NODEIDTYPE_IPV6:
		case PUD_NODEIDTYPE_192:
		case PUD_NODEIDTYPE_193:
		case PUD_NODEIDTYPE_194:
			break;

		default:
			pudError(false, "Configured %s (%llu) is reserved", valueName,
					nodeIdTypeNew);
			return true;
	}

	nodeIdType = nodeIdTypeNew;

	return false;
}

/*
 * nodeId
 */

/** The maximum length of a nodeId */
#define PUD_NODEIDMAXLENGTH 255

/** The nodeId buffer */
static unsigned char nodeId[PUD_NODEIDMAXLENGTH + 1];

/** The length of the string in the nodeId buffer */
static size_t nodeIdLength = 0;

/** True when the nodeId is set */
static bool nodeIdSet = false;

/** The nodeId as a nuber */
static unsigned long long nodeIdNumber = 0;

/** True when the nodeIdNumber is set */
static bool nodeIdNumberSet = false;

/**
 @return
 The node ID
 */
unsigned char * getNodeId(void) {
	return getNodeIdWithLength(NULL);
}

/**
 @param nodeId
 A pointer to the node ID number
 @return
 - true on success
 - false otherwise
 */
bool getNodeIdAsNumber(unsigned long long * value) {
	if (!nodeIdNumberSet) {
		if (!readULL(PUD_NODE_ID_NAME, (char *) &nodeId[0], &nodeIdNumber)) {
			return false;
		}
		nodeIdNumberSet = true;
	}
	*value = nodeIdNumber;
	return true;
}

/**
 Get the nodeId and its length

 @param length
 a pointer to the variable in which to store the nodeId length (allowed to be
 NULL, in which case the length is not stored)

 @return
 The node ID
 */
unsigned char * getNodeIdWithLength(size_t *length) {
	if (!nodeIdSet) {<commit_msg>PUD: minor comment fix<commit_after>#include "configuration.h"

/* Plugin includes */
#include "pud.h"
#include "netTools.h"
#include "nmeaTools.h"
#include "nodeIdConversion.h"
#include "networkInterfaces.h"

/* OLSR includes */
#include "olsr_types.h"
#include "olsr_cfg.h"

/* System includes */
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>

/*
 * Utility functions
 */

/**
 Read an unsigned long long number from a value string

 @param valueName
 the name of the value
 @param value
 the string to convert to a number
 @param valueNumber
 a pointer to the location where to store the number upon successful conversion

 @return
 - true on success
 - false otherwise
 */
static bool readULL(const char * valueName, const char * value,
		unsigned long long * valueNumber) {
	char * endPtr = NULL;
	unsigned long long valueNew;

	errno = 0;
	valueNew = strtoull(value, &endPtr, 10);

	if (!((endPtr != value) && (*value != '\0') && (*endPtr == '\0'))) {
		/* invalid conversion */
		pudError(true, "Configured %s (%s) could not be converted to a number",
				valueName, value);
		return false;
	}

	*valueNumber = valueNew;

	return true;
}

/**
 Read a double number from a value string

 @param valueName
 the name of the value
 @param value
 the string to convert to a number
 @param valueNumber
 a pointer to the location where to store the number upon successful conversion

 @return
 - true on success
 - false otherwise
 */
static bool readDouble(const char * valueName, const char * value,
		double * valueNumber) {
	char * endPtr = NULL;
	double valueNew;

	errno = 0;
	valueNew = strtod(value, &endPtr);

	if (!((endPtr != value) && (*value != '\0') && (*endPtr == '\0'))) {
		/* invalid conversion */
		pudError(true, "Configured %s (%s) could not be converted to a number",
				valueName, value);
		return false;
	}

	*valueNumber = valueNew;

	return true;
}

/*
 * nodeIdType
 */

/** The nodeIdType */
static NodeIdType nodeIdType = PUD_NODE_ID_TYPE_DEFAULT;

/**
 @return
 The node ID type
 */
NodeIdType getNodeIdTypeNumber(void) {
	return nodeIdType;
}

/**
 Set the node ID type.

 @param value
 The value of the node ID type to set (a number in string representation)
 @param data
 Unused
 @param addon
 Unused

 @return
 - true when an error is detected
 - false otherwise
 */
int setNodeIdType(const char *value, void *data __attribute__ ((unused)),
		set_plugin_parameter_addon addon __attribute__ ((unused))) {
	static const char * valueName = PUD_NODE_ID_TYPE_NAME;
	unsigned long long nodeIdTypeNew;

	assert (value != NULL);

	if (!readULL(valueName, value, &nodeIdTypeNew)) {
		return true;
	}

	if (nodeIdTypeNew > PUD_NODE_ID_TYPE_MAX) {
		pudError(false, "Configured %s (%llu) is out of range 0-%u", valueName,
				nodeIdTypeNew, PUD_NODE_ID_TYPE_MAX);
		return true;
	}

	switch (nodeIdTypeNew) {
		case PUD_NODEIDTYPE_MAC:
		case PUD_NODEIDTYPE_MSISDN:
		case PUD_NODEIDTYPE_TETRA:
		case PUD_NODEIDTYPE_DNS:
		case PUD_NODEIDTYPE_IPV4:
		case PUD_NODEIDTYPE_IPV6:
		case PUD_NODEIDTYPE_192:
		case PUD_NODEIDTYPE_193:
		case PUD_NODEIDTYPE_194:
			break;

		default:
			pudError(false, "Configured %s (%llu) is reserved", valueName,
					nodeIdTypeNew);
			return true;
	}

	nodeIdType = nodeIdTypeNew;

	return false;
}

/*
 * nodeId
 */

/** The maximum length of a nodeId */
#define PUD_NODEIDMAXLENGTH 255

/** The nodeId buffer */
static unsigned char nodeId[PUD_NODEIDMAXLENGTH + 1];

/** The length of the string in the nodeId buffer */
static size_t nodeIdLength = 0;

/** True when the nodeId is set */
static bool nodeIdSet = false;

/** The nodeId as a nuber */
static unsigned long long nodeIdNumber = 0;

/** True when the nodeIdNumber is set */
static bool nodeIdNumberSet = false;

/**
 @return
 The node ID
 */
unsigned char * getNodeId(void) {
	return getNodeIdWithLength(NULL);
}

/**
 @param value
 A pointer to the node ID number
 @return
 - true on success
 - false otherwise
 */
bool getNodeIdAsNumber(unsigned long long * value) {
	if (!nodeIdNumberSet) {
		if (!readULL(PUD_NODE_ID_NAME, (char *) &nodeId[0], &nodeIdNumber)) {
			return false;
		}
		nodeIdNumberSet = true;
	}
	*value = nodeIdNumber;
	return true;
}

/**
 Get the nodeId and its length

 @param length
 a pointer to the variable in which to store the nodeId length (allowed to be
 NULL, in which case the length is not stored)

 @return
 The node ID
 */
unsigned char * getNodeIdWithLength(size_t *length) {
	if (!nodeIdSet) {
{'fraction_non_alphanumeric': 0.11088139281828074, 'fraction_numerical': 0.0058759521218716, 'mean_word_length': 3.8992537313432836, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '5871343', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3068, 'n_words': 1039}