Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
uuid.h File Reference

Universally unique identifier support. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define AST_UUID_STR_LEN   (36 + 1)
 

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. More...
 
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...
 

Detailed Description

Universally unique identifier support.

Definition in file uuid.h.

Macro Definition Documentation

◆ AST_UUID_STR_LEN

#define AST_UUID_STR_LEN   (36 + 1)

Function Documentation

◆ ast_str_to_uuid()

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 151 of file uuid.c.

References ast_free, ast_log, ast_malloc, LOG_WARNING, NULL, and ast_uuid::uu.

Referenced by AST_TEST_DEFINE(), and is_valid_uuid().

152 {
153  struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
154  int res;
155 
156  if (!uuid) {
157  return NULL;
158  }
159  res = uuid_parse(str, uuid->uu);
160  if (res) {
161  ast_log(LOG_WARNING, "Unable to convert string %s into a UUID\n", str);
162  ast_free(uuid);
163  return NULL;
164  }
165  return uuid;
166 }
#define LOG_WARNING
Definition: logger.h:274
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
Definition: uuid.c:39
#define ast_log
Definition: astobj2.c:42
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
#define ast_free(a)
Definition: astmm.h:182
uuid_t uu
Definition: uuid.c:40

◆ ast_uuid_clear()

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 184 of file uuid.c.

References ast_uuid::uu.

Referenced by AST_TEST_DEFINE().

185 {
186  uuid_clear(uuid->uu);
187 }
uuid_t uu
Definition: uuid.c:40

◆ ast_uuid_compare()

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 179 of file uuid.c.

References ast_uuid::uu.

Referenced by AST_TEST_DEFINE().

180 {
181  return uuid_compare(left->uu, right->uu);
182 }
uuid_t uu
Definition: uuid.c:40

◆ ast_uuid_copy()

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 168 of file uuid.c.

References ast_malloc, NULL, and ast_uuid::uu.

Referenced by AST_TEST_DEFINE().

169 {
170  struct ast_uuid *dst = ast_malloc(sizeof(*dst));
171 
172  if (!dst) {
173  return NULL;
174  }
175  uuid_copy(dst->uu, src->uu);
176  return dst;
177 }
#define NULL
Definition: resample.c:96
Definition: uuid.c:39
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
uuid_t uu
Definition: uuid.c:40

◆ ast_uuid_generate()

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 125 of file uuid.c.

References ast_malloc, generate_uuid(), and NULL.

Referenced by AST_TEST_DEFINE().

126 {
127  struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
128 
129  if (!uuid) {
130  return NULL;
131  }
132  generate_uuid(uuid);
133  return uuid;
134 }
static void generate_uuid(struct ast_uuid *uuid)
Definition: uuid.c:52
#define NULL
Definition: resample.c:96
Definition: uuid.c:39
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193

◆ ast_uuid_generate_str()

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 143 of file uuid.c.

References ast_uuid_to_str(), and generate_uuid().

Referenced by add_msid_to_stream(), ast_datastores_alloc_datastore(), ast_rtp_new(), ast_sip_publish_client_alloc_datastore(), ast_sip_session_alloc_datastore(), ast_sorcery_alloc(), AST_TEST_DEFINE(), ast_websocket_uri_cb(), asterisk_daemon(), bridge_base_init(), build_entity_id(), generate_exchange_uuid(), mock_retrieve_multiple(), playback_create(), sorcery_config_open(), and stir_shaken_add_origid().

144 {
145  struct ast_uuid uuid;
146 
147  generate_uuid(&uuid);
148  return ast_uuid_to_str(&uuid, buf, size);
149 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static void generate_uuid(struct ast_uuid *uuid)
Definition: uuid.c:52
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:136

◆ ast_uuid_init()

void ast_uuid_init ( void  )

Initialize the UUID system.

Definition at line 194 of file uuid.c.

References ast_debug, ast_log, dev_urandom_fd, has_dev_urandom, LOG_WARNING, and ast_uuid::uu.

Referenced by asterisk_daemon().

195 {
196  /* This requires some explanation.
197  *
198  * libuuid generates UUIDs based on random number generation. This involves
199  * opening a handle to /dev/urandom or /dev/random in order to get random
200  * data for the UUIDs.
201  *
202  * This is thread-safe, to a point. The problem is that the first attempt
203  * to generate a UUID will result in opening the random number handle. Once
204  * the handle is opened, all further generation is thread safe. This
205  * first generation can be potentially risky if multiple threads attempt
206  * to generate a UUID at the same time, though, since there is no thread
207  * synchronization used within libuuid. To get around this potential
208  * issue, we go ahead and generate a UUID up front so that the underlying
209  * work is done before we start requesting UUIDs for real.
210  *
211  * Think of this along the same lines as initializing a singleton.
212  */
213  uuid_t uu;
214  int dev_urandom_fd;
215 
216  dev_urandom_fd = open("/dev/urandom", O_RDONLY);
217  if (dev_urandom_fd < 0) {
218  ast_log(LOG_WARNING, "It appears your system does not have /dev/urandom on it. This\n"
219  "means that UUID generation will use a pseudorandom number generator. Since\n"
220  "the thread-safety of your system's random number generator cannot\n"
221  "be guaranteed, we have to synchronize UUID generation. This may result\n"
222  "in decreased performance. It is highly recommended that you set up your\n"
223  "system to have /dev/urandom\n");
224  } else {
225  has_dev_urandom = 1;
226  close(dev_urandom_fd);
227  }
228  uuid_generate_random(uu);
229 
230  ast_debug(1, "UUID system initiated\n");
231 }
static int dev_urandom_fd
Definition: main/utils.c:792
#define LOG_WARNING
Definition: logger.h:274
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static int has_dev_urandom
Definition: uuid.c:37
uuid_t uu
Definition: uuid.c:40

◆ ast_uuid_is_nil()

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 189 of file uuid.c.

References ast_uuid::uu.

Referenced by AST_TEST_DEFINE().

190 {
191  return uuid_is_null(uuid->uu);
192 }
uuid_t uu
Definition: uuid.c:40

◆ ast_uuid_to_str()

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 136 of file uuid.c.

References ast_assert, ast_str_to_lower(), AST_UUID_STR_LEN, and ast_uuid::uu.

Referenced by AST_TEST_DEFINE(), and ast_uuid_generate_str().

137 {
138  ast_assert(size >= AST_UUID_STR_LEN);
139  uuid_unparse(uuid->uu, buf);
140  return ast_str_to_lower(buf);
141 }
#define AST_UUID_STR_LEN
Definition: uuid.h:27
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ast_assert(a)
Definition: utils.h:695
static force_inline char * ast_str_to_lower(char *str)
Convert a string to all lower-case.
Definition: strings.h:1268
uuid_t uu
Definition: uuid.c:40