Generate an UUID if no one was set

pull/23/head
Gerard Wagener 2018-12-03 12:00:34 +01:00
parent 423261613b
commit 6843c61ba0
3 changed files with 44 additions and 2 deletions

View File

@ -1,2 +1,2 @@
d4: d4.c
gcc -Wall -o d4 d4.c
gcc -Wall -o d4 d4.c -luuid

View File

@ -9,8 +9,47 @@
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <uuid/uuid.h>
#include "d4.h"
//
/*
* Generate a uuid if no one was set
*/
void d4_update_uuid(d4_t* d4)
{
uuid_t uuid;
int fd,ret;
char* filename;
char* uuid_text;
if (d4->conf[UUID][0] == 0){
uuid_generate(uuid);
memcpy(&(d4->header.uuid), uuid, SZUUID);
filename = calloc(1,2*FILENAME_MAX);
uuid_text = calloc(1, SZUUID_TEXT);
if ((filename != NULL) && (uuid != NULL)) {
snprintf(filename, 2*FILENAME_MAX, "%s/%s",d4->confdir, d4params[UUID]);
fd = open(filename, O_CREAT | O_WRONLY, S_IRUSR |S_IWUSR);
if (fd > 0) {
uuid_unparse(uuid, uuid_text);
ret = write(fd, uuid_text, SZUUID_TEXT-1);
if (ret < 0) {
d4->errno_copy = errno;
}
close(fd);
} else {
// Cannot open file
d4->errno_copy = errno;
}
}
/* If there is an error the uuid is not stored and a new one is
* generated for the next boot
*/
}
}
int d4_check_config(d4_t* d4)
{
// TODO implement other sources, file, fifo, unix_socket ...
@ -27,6 +66,9 @@ int d4_check_config(d4_t* d4)
}
}
d4->snaplen = atoi(d4->conf[SNAPLEN]);
d4_update_uuid(d4);
if ((d4->snaplen < 0) || (d4->snaplen > MAXSNAPLEN)) {
d4->snaplen = 0;
}

View File

@ -10,7 +10,7 @@
#define STDOUT "stdout"
#define MAXSNAPLEN 65535
#define SZUUID 128
#define SZUUID_TEXT 37
#define INSERT_ERROR(...) do { \
if (d4->err_idx < NERRORS) \
snprintf(d4->errors[d4->err_idx],SZERRVALUE,__VA_ARGS__); \