Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions | Variables
uuid.c File Reference

Universally unique identifier support. More...

#include "asterisk.h"
#include <uuid/uuid.h>
#include <fcntl.h>
#include "asterisk/uuid.h"
#include "asterisk/utils.h"
#include "asterisk/strings.h"
#include "asterisk/logger.h"
#include "asterisk/lock.h"

Go to the source code of this file.

Data Structures

struct  ast_uuid
 

Functions

struct ast_uuidast_str_to_uuid (char *str)
 Convert a string to a UUID. More...
 
void ast_uuid_clear (struct ast_uuid *uuid)
 Clear a UUID by setting it to be a nil UUID (all 0s) More...
 
int ast_uuid_compare (struct ast_uuid *left, struct ast_uuid *right)
 Compare two UUIDs. More...
 
struct ast_uuidast_uuid_copy (struct ast_uuid *src)
 Make a copy of a UUID. More...
 
struct ast_uuidast_uuid_generate (void)
 Generate a UUID. More...
 
char * ast_uuid_generate_str (char *buf, size_t size)
 Generate a UUID string. More...
 
void ast_uuid_init (void)
 Initialize the UUID system.
 
int ast_uuid_is_nil (struct ast_uuid *uuid)
 Check if a UUID is a nil UUID (all 0s) More...
 
char * ast_uuid_to_str (struct ast_uuid *uuid, char *buf, size_t size)
 Convert a UUID to a string. More...
 
static void generate_uuid (struct ast_uuid *uuid)
 

Variables

static int has_dev_urandom
 
static ast_mutex_t uuid_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 

Detailed Description

Universally unique identifier support.

ExtRef:
Depends on libuuid, a component of the e2fsprogs package - http://e2fsprogs.sourceforge.net/

Definition in file uuid.c.

Function Documentation

struct ast_uuid* ast_str_to_uuid ( char *  str)

Convert a string to a UUID.

This function allocates memory on the heap. The returned pointer must be freed using ast_free()

Parameters
strThe string to convert to a UUID
Return values
NULLFailed to convert
non-NULLThe heap-allocated converted UUID

Definition at line 149 of file uuid.c.

References ast_malloc.

150 {
151  struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
152  int res;
153 
154  if (!uuid) {
155  return NULL;
156  }
157  res = uuid_parse(str, uuid->uu);
158  if (res) {
159  ast_log(LOG_WARNING, "Unable to convert string %s into a UUID\n", str);
160  ast_free(uuid);
161  return NULL;
162  }
163  return uuid;
164 }
Definition: uuid.c:39
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
void ast_uuid_clear ( struct ast_uuid uuid)

Clear a UUID by setting it to be a nil UUID (all 0s)

Parameters
uuidUUID to clear

Definition at line 182 of file uuid.c.

183 {
184  uuid_clear(uuid->uu);
185 }
int ast_uuid_compare ( struct ast_uuid left,
struct ast_uuid right 
)

Compare two UUIDs.

Parameters
leftFirst UUID to compare
rightSecond UUID to compare
Return values
<0left is lexicographically less than right
0left and right are the same
>0left is lexicographically greater than right

Definition at line 177 of file uuid.c.

178 {
179  return uuid_compare(left->uu, right->uu);
180 }
struct ast_uuid* ast_uuid_copy ( struct ast_uuid src)

Make a copy of a UUID.

This function allocates memory on the heap. The returned pointer must be freed using ast_free()

Parameters
srcThe source UUID to copy
Return values
NULLFailed to copy
non-NULLThe heap-allocated duplicate UUID

Definition at line 166 of file uuid.c.

References ast_malloc.

167 {
168  struct ast_uuid *dst = ast_malloc(sizeof(*dst));
169 
170  if (!dst) {
171  return NULL;
172  }
173  uuid_copy(dst->uu, src->uu);
174  return dst;
175 }
Definition: uuid.c:39
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
struct ast_uuid* ast_uuid_generate ( void  )

Generate a UUID.

This function allocates memory on the heap. The returned pointer must be freed using ast_free()

Return values
NULLGeneration failed
non-NULLheap-allocated UUID

Definition at line 123 of file uuid.c.

References ast_malloc.

124 {
125  struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
126 
127  if (!uuid) {
128  return NULL;
129  }
130  generate_uuid(uuid);
131  return uuid;
132 }
Definition: uuid.c:39
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
char* ast_uuid_generate_str ( char *  buf,
size_t  size 
)

Generate a UUID string.

Since
12.0.0
Parameters
bufThe buffer where the UUID string will be stored
sizeThe size of the buffer. Must be at least AST_UUID_STR_LEN.
Returns
The UUID string (a pointer to buf)

Definition at line 141 of file uuid.c.

References ast_uuid_to_str().

Referenced by ast_aeap_message_id_generate(), ast_datastores_alloc_datastore(), ast_rtp_new(), ast_sorcery_alloc(), and bridge_base_init().

142 {
143  struct ast_uuid uuid;
144 
145  generate_uuid(&uuid);
146  return ast_uuid_to_str(&uuid, buf, size);
147 }
Definition: uuid.c:39
char * ast_uuid_to_str(struct ast_uuid *uuid, char *buf, size_t size)
Convert a UUID to a string.
Definition: uuid.c:134
int ast_uuid_is_nil ( struct ast_uuid uuid)

Check if a UUID is a nil UUID (all 0s)

Parameters
uuidUUID to check
Return values
0The UUID is not nil
non-zeroThe UUID is nil

Definition at line 187 of file uuid.c.

188 {
189  return uuid_is_null(uuid->uu);
190 }
char* ast_uuid_to_str ( struct ast_uuid uuid,
char *  buf,
size_t  size 
)

Convert a UUID to a string.

Parameters
uuidThe UUID to convert to a string
[out]bufThe buffer where the UUID string will be stored
sizeThe size of the buffer. Must be at least AST_UUID_STR_LEN.
Returns
The UUID string (a pointer to buf)

Definition at line 134 of file uuid.c.

References ast_str_to_lower().

Referenced by ast_uuid_generate_str().

135 {
136  ast_assert(size >= AST_UUID_STR_LEN);
137  uuid_unparse(uuid->uu, buf);
138  return ast_str_to_lower(buf);
139 }
static force_inline char * ast_str_to_lower(char *str)
Convert a string to all lower-case.
Definition: strings.h:1321