Add SERVAL_ prefix to UUID symbols to avoid collisions on OS-X

This commit is contained in:
Andrew Bettison 2017-10-09 11:41:44 +10:30
parent 33a17a01fe
commit 159c731e9e
7 changed files with 47 additions and 45 deletions

View File

@ -30,7 +30,7 @@ HDRS= fifo.h \
rotbuf.h \
mem.h \
os.h \
uuid.h \
serval_uuid.h \
sighandlers.h \
strbuf.h \
strbuf_helpers.h \

View File

@ -28,7 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "overlay_packet.h"
#include "fdqueue.h"
#include "os.h"
#include "uuid.h"
#include "serval_uuid.h"
#include "trigger.h"
#ifndef __RHIZOME_INLINE

View File

@ -238,7 +238,7 @@ void verify_bundles()
int rhizome_opendb()
{
if (rhizome_db) {
assert(uuid_is_valid(&rhizome_db_uuid));
assert(serval_uuid_is_valid(&rhizome_db_uuid));
return 0;
}
@ -388,21 +388,21 @@ int rhizome_opendb()
The above schema can be assumed to exist, no matter which version we upgraded from.
All changes should attempt to preserve all existing interesting data */
char buf[UUID_STRLEN + 1];
char buf[SERVAL_UUID_STRLEN + 1];
int r = sqlite_exec_strbuf_retry(&retry, strbuf_local_buf(buf), "SELECT uuid from IDENTITY LIMIT 1;", END);
if (r == -1)
RETURN(-1);
if (r) {
if (!str_to_uuid(buf, &rhizome_db_uuid, NULL)) {
if (!str_to_serval_uuid(buf, &rhizome_db_uuid, NULL)) {
WHYF("IDENTITY table contains malformed UUID %s -- overwriting", alloca_str_toprint(buf));
if (uuid_generate_random(&rhizome_db_uuid) == -1)
if (serval_uuid_generate_random(&rhizome_db_uuid) == -1)
RETURN(WHY("Cannot generate new UUID for Rhizome database"));
if (sqlite_exec_void_retry(&retry, "UPDATE IDENTITY SET uuid = ? LIMIT 1;", SERVAL_UUID_T, &rhizome_db_uuid, END) == -1)
RETURN(WHY("Failed to update new UUID in Rhizome database"));
DEBUGF(rhizome, "Updated Rhizome database UUID to %s", alloca_uuid_str(rhizome_db_uuid));
}
} else if (r == 0) {
if (uuid_generate_random(&rhizome_db_uuid) == -1)
if (serval_uuid_generate_random(&rhizome_db_uuid) == -1)
RETURN(WHY("Cannot generate UUID for Rhizome database"));
if (sqlite_exec_void_retry(&retry, "INSERT INTO IDENTITY (uuid) VALUES (?);", SERVAL_UUID_T, &rhizome_db_uuid, END) == -1)
RETURN(WHY("Failed to insert UUID into Rhizome database"));
@ -891,10 +891,10 @@ int _sqlite_vbind(struct __sourceloc __whence, int log_level, sqlite_retry_state
if (uuidp == NULL) {
BIND_NULL(SERVAL_UUID_T);
} else {
char uuid_str[UUID_STRLEN + 1];
uuid_to_str(uuidp, uuid_str);
BIND_DEBUG(SERVAL_UUID_T, sqlite3_bind_text, "%s,%u,SQLITE_TRANSIENT", uuid_str, UUID_STRLEN);
BIND_RETRY(sqlite3_bind_text, uuid_str, UUID_STRLEN, SQLITE_TRANSIENT);
char uuid_str[SERVAL_UUID_STRLEN + 1];
serval_uuid_to_str(uuidp, uuid_str);
BIND_DEBUG(SERVAL_UUID_T, sqlite3_bind_text, "%s,%u,SQLITE_TRANSIENT", uuid_str, SERVAL_UUID_STRLEN);
BIND_RETRY(sqlite3_bind_text, uuid_str, SERVAL_UUID_STRLEN, SQLITE_TRANSIENT);
}
}
break;

View File

@ -79,7 +79,7 @@ static int strn_to_list_token(const char *str, uint64_t *rowidp, const char **af
{
unsigned char token[sizeof rhizome_db_uuid.u.binary + sizeof *rowidp];
if (base64url_decode(token, sizeof token, str, 0, afterp, 0, NULL) == sizeof token
&& cmp_uuid_t(&rhizome_db_uuid, (serval_uuid_t *) &token) == 0
&& cmp_serval_uuid_t(&rhizome_db_uuid, (serval_uuid_t *) &token) == 0
&& **afterp=='/'){
memcpy(rowidp, token + sizeof rhizome_db_uuid.u.binary, sizeof *rowidp);
(*afterp)++;

View File

@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "lang.h" // for FALLTHROUGH
#define __SERVAL_DNA__UUID_H_INLINE
#include "uuid.h"
#include "serval_uuid.h"
#include "os.h"
#include "str.h"
#include <sodium.h>
@ -33,9 +33,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# include <arpa/inet.h>
#endif
enum uuid_version uuid_get_version(const serval_uuid_t *uuid)
enum serval_uuid_version serval_uuid_get_version(const serval_uuid_t *uuid)
{
assert(uuid_is_valid(uuid));
assert(serval_uuid_is_valid(uuid));
switch (ntohs(uuid->u.record.time_hi_and_version) & 0xf000) {
case 0x1000: return UUID_VERSION_TIME_BASED;
case 0x2000: return UUID_VERSION_DCE_SECURITY;
@ -46,7 +46,7 @@ enum uuid_version uuid_get_version(const serval_uuid_t *uuid)
return UUID_VERSION_UNSUPPORTED;
}
void uuid_set_version(serval_uuid_t *uuid, enum uuid_version version)
void serval_uuid_set_version(serval_uuid_t *uuid, enum serval_uuid_version version)
{
uint16_t version_bits;
switch (version) {
@ -57,23 +57,23 @@ void uuid_set_version(serval_uuid_t *uuid, enum uuid_version version)
case UUID_VERSION_NAME_SHA1: version_bits = 0x5000; break;
default: abort();
}
assert(uuid_is_valid(uuid));
assert(serval_uuid_is_valid(uuid));
uuid->u.record.time_hi_and_version = htons((ntohs(uuid->u.record.time_hi_and_version) & 0xfff) | version_bits);
}
int uuid_generate_random(serval_uuid_t *uuid)
int serval_uuid_generate_random(serval_uuid_t *uuid)
{
randombytes_buf(uuid->u.binary, sizeof uuid->u.binary);
// The following discards 6 random bits.
uuid->u.record.clock_seq_hi_and_reserved &= 0x3f;
uuid->u.record.clock_seq_hi_and_reserved |= 0x80;
uuid_set_version(uuid, UUID_VERSION_RANDOM);
serval_uuid_set_version(uuid, UUID_VERSION_RANDOM);
return 0;
}
strbuf strbuf_uuid(strbuf sb, const serval_uuid_t *uuid)
{
assert(uuid_is_valid(uuid));
assert(serval_uuid_is_valid(uuid));
unsigned i;
for (i = 0; i != sizeof uuid->u.binary; ++i) {
switch (i) {
@ -88,15 +88,15 @@ strbuf strbuf_uuid(strbuf sb, const serval_uuid_t *uuid)
return sb;
}
char *uuid_to_str(const serval_uuid_t *uuid, char *const dst)
char *serval_uuid_to_str(const serval_uuid_t *uuid, char *const dst)
{
strbuf b = strbuf_local(dst, UUID_STRLEN + 1);
strbuf b = strbuf_local(dst, SERVAL_UUID_STRLEN + 1);
strbuf_uuid(b, uuid);
assert(!strbuf_overrun(b));
return dst;
}
int str_to_uuid(const char *const str, serval_uuid_t *uuid, const char **afterp)
int str_to_serval_uuid(const char *const str, serval_uuid_t *uuid, const char **afterp)
{
const char *end = str;
int ret = 0;
@ -110,8 +110,8 @@ int str_to_uuid(const char *const str, serval_uuid_t *uuid, const char **afterp)
&& *end == '-'
&& strn_fromhex(uuid->u.binary + 10, 6, end + 1, &end) == 6
) {
assert(end == str + UUID_STRLEN);
ret = uuid_is_valid(uuid);
assert(end == str + SERVAL_UUID_STRLEN);
ret = serval_uuid_is_valid(uuid);
}
if (afterp)
*afterp = end;

View File

@ -17,19 +17,19 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __SERVAL_DNA__UUID_H
#define __SERVAL_DNA__UUID_H
#ifndef __SERVAL_DNA__SERVAL_UUID_H
#define __SERVAL_DNA__SERVAL_UUID_H
#include <stdint.h>
#include <alloca.h>
#include <string.h>
#include "strbuf.h"
#ifndef __SERVAL_DNA__UUID_H_INLINE
#ifndef __SERVAL_DNA__SERVAL_UUID_H_INLINE
# if __GNUC__ && !__GNUC_STDC_INLINE__
# define __SERVAL_DNA__UUID_H_INLINE extern inline
# define __SERVAL_DNA__SERVAL_UUID_H_INLINE extern inline
# else
# define __SERVAL_DNA__UUID_H_INLINE inline
# define __SERVAL_DNA__SERVAL_UUID_H_INLINE inline
# endif
#endif
@ -49,7 +49,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* so code wishing to make use of the record structure must use ntohl(3) and
* ntohs(3) to read values, and htonl(3) and htons(3) to assign values.
*
* OS-X already defines a "uuid_t" typedef, so we use a different symbol.
* OS-X already defines a "uuid_t" typedef and has a "uuid_generate_random()"
* function, so all symbols defined in this file are prefixed with
* serval_uuid_ to avoid potential collisions on OS-X and other platforms.
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
@ -67,7 +69,7 @@ typedef struct serval_uuid {
} u;
} serval_uuid_t;
enum uuid_version {
enum serval_uuid_version {
UUID_VERSION_UNSUPPORTED = 0,
UUID_VERSION_TIME_BASED = 1,
UUID_VERSION_DCE_SECURITY = 2,
@ -76,20 +78,20 @@ enum uuid_version {
UUID_VERSION_NAME_SHA1 = 5
};
__SERVAL_DNA__UUID_H_INLINE int cmp_uuid_t(const serval_uuid_t *a, const serval_uuid_t *b) {
__SERVAL_DNA__SERVAL_UUID_H_INLINE int cmp_serval_uuid_t(const serval_uuid_t *a, const serval_uuid_t *b) {
return memcmp(a->u.binary, b->u.binary, sizeof a->u.binary);
}
__SERVAL_DNA__UUID_H_INLINE int uuid_is_valid(const serval_uuid_t *any_uuid) {
__SERVAL_DNA__SERVAL_UUID_H_INLINE int serval_uuid_is_valid(const serval_uuid_t *any_uuid) {
return (any_uuid->u.record.clock_seq_hi_and_reserved & 0xc0) == 0x80;
}
enum uuid_version uuid_get_version(const serval_uuid_t *valid_uuid);
void uuid_set_version(serval_uuid_t *valid_uuid, enum uuid_version);
enum serval_uuid_version serval_uuid_get_version(const serval_uuid_t *valid_uuid);
void serval_uuid_set_version(serval_uuid_t *valid_uuid, enum serval_uuid_version);
/* Returns -1 if error (eg, cannot open /dev/urandom), 0 if successful.
*/
int uuid_generate_random(serval_uuid_t *dest_uuid);
int serval_uuid_generate_random(serval_uuid_t *dest_uuid);
/* Formats the given valid UUID in its canonical string representation:
* XXXXXXXX-VXXX-MXXX-XXXXXXXXXXXX
@ -106,15 +108,15 @@ int uuid_generate_random(serval_uuid_t *dest_uuid);
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
char *uuid_to_str(const serval_uuid_t *valid_uuid, char *dst);
#define UUID_STRLEN 36
#define alloca_uuid_str(uuid) uuid_to_str(&(uuid), alloca(UUID_STRLEN + 1))
char *serval_uuid_to_str(const serval_uuid_t *valid_uuid, char *dst);
#define SERVAL_UUID_STRLEN 36
#define alloca_uuid_str(uuid) serval_uuid_to_str(&(uuid), alloca(SERVAL_UUID_STRLEN + 1))
/* Append a UUID to the given strbuf, formatted as per the uuid_to_str() function.
/* Append a UUID to the given strbuf, formatted as per the serval_uuid_to_str() function.
*/
strbuf strbuf_uuid(strbuf, const serval_uuid_t *valid_uuid);
/* Parse a canonical UUID string (as generated by uuid_to_str()) into a valid
/* Parse a canonical UUID string (as generated by serval_uuid_to_str()) into a valid
* UUID, which may or not be supported.
*
* Returns 1 if a valid UUID is parsed, storing the value in *result (unless result is NULL) and
@ -125,6 +127,6 @@ strbuf strbuf_uuid(strbuf, const serval_uuid_t *valid_uuid);
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
int str_to_uuid(const char *str, serval_uuid_t *result, const char **afterp);
int str_to_serval_uuid(const char *str, serval_uuid_t *result, const char **afterp);
#endif //__SERVAL_DNA__OS_H
#endif //__SERVAL_DNA__SERVAL_UUID_H

View File

@ -34,7 +34,7 @@ SERVAL_CLIENT_SOURCES = \
strlcpy.c \
test_cli.c \
uri.c \
uuid.c \
serval_uuid.c \
version_cli.c \
whence.c \
xprintf.c