Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
gw_uuid.h File Reference
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

Go to the source code of this file.

Macros

#define UUID_STR_LEN   36
 
#define UUID_VARIANT_NCS   0
 
#define UUID_VARIANT_DCE   1
 
#define UUID_VARIANT_MICROSOFT   2
 
#define UUID_VARIANT_OTHER   3
 

Typedefs

typedef unsigned char uuid_t[16]
 

Functions

void uuid_init (void)
 
void uuid_shutdown (void)
 
void uuid_clear (uuid_t uu)
 
int uuid_compare (const uuid_t uu1, const uuid_t uu2)
 
void uuid_copy (uuid_t dst, const uuid_t src)
 
void uuid_generate (uuid_t out)
 
void uuid_generate_random (uuid_t out)
 
void uuid_generate_time (uuid_t out)
 
int uuid_is_null (const uuid_t uu)
 
int uuid_parse (const char *in, uuid_t uu)
 
void uuid_unparse (const uuid_t uu, char *out)
 
time_t uuid_time (const uuid_t uu, struct timeval *ret_tv)
 
int uuid_type (const uuid_t uu)
 
int uuid_variant (const uuid_t uu)
 

Macro Definition Documentation

◆ UUID_STR_LEN

◆ UUID_VARIANT_DCE

#define UUID_VARIANT_DCE   1

Definition at line 38 of file gw_uuid.h.

Referenced by uuid_variant().

◆ UUID_VARIANT_MICROSOFT

#define UUID_VARIANT_MICROSOFT   2

Definition at line 39 of file gw_uuid.h.

Referenced by uuid_variant().

◆ UUID_VARIANT_NCS

#define UUID_VARIANT_NCS   0

Definition at line 37 of file gw_uuid.h.

Referenced by uuid_variant().

◆ UUID_VARIANT_OTHER

#define UUID_VARIANT_OTHER   3

Definition at line 40 of file gw_uuid.h.

Referenced by uuid_variant().

Typedef Documentation

◆ uuid_t

typedef unsigned char uuid_t[16]

Definition at line 32 of file gw_uuid.h.

Function Documentation

◆ uuid_clear()

void uuid_clear ( uuid_t  uu)

Definition at line 105 of file gw_uuid.c.

Referenced by main(), and msg_cb().

106 {
107  memset(uu, 0, 16);
108 }

◆ uuid_compare()

int uuid_compare ( const uuid_t  uu1,
const uuid_t  uu2 
)

Definition at line 126 of file gw_uuid.c.

References uuid::clock_seq, uuid::node, uuid::time_hi_and_version, uuid::time_low, uuid::time_mid, UUCMP, and uuid_unpack().

127 {
128  struct uuid uuid1, uuid2;
129 
130  uuid_unpack(uu1, &uuid1);
131  uuid_unpack(uu2, &uuid2);
132 
133  UUCMP(uuid1.time_low, uuid2.time_low);
134  UUCMP(uuid1.time_mid, uuid2.time_mid);
135  UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
136  UUCMP(uuid1.clock_seq, uuid2.clock_seq);
137  return memcmp(uuid1.node, uuid2.node, 6);
138 }
static void uuid_unpack(const uuid_t in, struct uuid *uu)
Definition: gw_uuid.c:526
Definition: gw_uuid.c:62
#define UUCMP(u1, u2)
Definition: gw_uuid.c:124

◆ uuid_copy()

void uuid_copy ( uuid_t  dst,
const uuid_t  src 
)

Definition at line 150 of file gw_uuid.c.

Referenced by bearerbox_to_smpp(), bearerbox_to_sql(), boxc_receiver(), deliver_sms_to_queue(), obey_request_thread(), store_file_save_ack(), and store_spool_save_ack().

151 {
152  unsigned char *cp1;
153  const unsigned char *cp2;
154  int i;
155 
156  for (i=0, cp1 = dst, cp2 = src; i < 16; i++)
157  *cp1++ = *cp2++;
158 }

◆ uuid_generate()

void uuid_generate ( uuid_t  out)

Definition at line 393 of file gw_uuid.c.

References get_random_fd(), uuid_generate_random(), and uuid_generate_time().

Referenced by catenate_msg(), concat_handling_check_and_handle(), main(), msg_cb(), sms_split(), soap_send_loop(), store_spool_save(), store_to_dict(), and url_pattern().

394 {
395  if (get_random_fd() >= 0) {
397  }
398  else
399  uuid_generate_time(out);
400 }
void uuid_generate_time(uuid_t out)
Definition: gw_uuid.c:347
static int get_random_fd(void)
Definition: gw_uuid.c:171
void uuid_generate_random(uuid_t out)
Definition: gw_uuid.c:374

◆ uuid_generate_random()

void uuid_generate_random ( uuid_t  out)

Definition at line 374 of file gw_uuid.c.

References uuid::clock_seq, get_random_bytes(), uuid::time_hi_and_version, uuid_pack(), and uuid_unpack().

Referenced by uuid_generate().

375 {
376  uuid_t buf;
377  struct uuid uu;
378 
379  get_random_bytes(buf, sizeof(buf));
380  uuid_unpack(buf, &uu);
381 
382  uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
383  uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
384  uuid_pack(&uu, out);
385 }
static void uuid_unpack(const uuid_t in, struct uuid *uu)
Definition: gw_uuid.c:526
Definition: gw_uuid.c:62
static void get_random_bytes(void *buf, int nbytes)
Definition: gw_uuid.c:197
static void uuid_pack(const struct uuid *uu, uuid_t ptr)
Definition: gw_uuid.c:434
unsigned char uuid_t[16]
Definition: gw_uuid.h:32

◆ uuid_generate_time()

void uuid_generate_time ( uuid_t  out)

Definition at line 347 of file gw_uuid.c.

References uuid::clock_seq, get_clock(), get_node_id(), get_random_bytes(), uuid::node, uuid::time_hi_and_version, uuid::time_low, uuid::time_mid, and uuid_pack().

Referenced by uuid_generate().

348 {
349  static unsigned char node_id[6];
350  static int has_init = 0;
351  struct uuid uu;
352  __u32 clock_mid;
353 
354  if (!has_init) {
355  if (get_node_id(node_id) <= 0) {
356  get_random_bytes(node_id, 6);
357  /*
358  * Set multicast bit, to prevent conflicts
359  * with IEEE 802 addresses obtained from
360  * network cards
361  */
362  node_id[0] |= 0x80;
363  }
364  has_init = 1;
365  }
366  get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
367  uu.clock_seq |= 0x8000;
368  uu.time_mid = (__u16) clock_mid;
369  uu.time_hi_and_version = (clock_mid >> 16) | 0x1000;
370  memcpy(uu.node, node_id, 6);
371  uuid_pack(&uu, out);
372 }
static int get_clock(__u32 *clock_high, __u32 *clock_low, __u16 *ret_clock_seq)
Definition: gw_uuid.c:305
static int get_node_id(unsigned char *node_id)
Definition: gw_uuid.c:229
Definition: gw_uuid.c:62
static void get_random_bytes(void *buf, int nbytes)
Definition: gw_uuid.c:197
static void uuid_pack(const struct uuid *uu, uuid_t ptr)
Definition: gw_uuid.c:434

◆ uuid_init()

void uuid_init ( void  )

Definition at line 86 of file gw_uuid.c.

References get_random_fd().

Referenced by gwlib_init().

87 {
88  /*
89  * open random device if any.
90  * We should do it here because otherwise it's
91  * possible that we open device twice.
92  */
93  get_random_fd();
94 }
static int get_random_fd(void)
Definition: gw_uuid.c:171

◆ uuid_is_null()

int uuid_is_null ( const uuid_t  uu)

Definition at line 413 of file gw_uuid.c.

Referenced by get_pattern(), msg_cb(), soap_send_loop(), store_spool_save(), store_to_dict(), and urltrans_fill_escape_codes().

414 {
415  const unsigned char *cp;
416  int i;
417 
418  for (i=0, cp = uu; i < 16; i++)
419  if (*cp++)
420  return 0;
421  return 1;
422 }

◆ uuid_parse()

int uuid_parse ( const char *  in,
uuid_t  uu 
)

Definition at line 476 of file gw_uuid.c.

References uuid::clock_seq, uuid::node, uuid::time_hi_and_version, uuid::time_low, uuid::time_mid, and uuid_pack().

Referenced by bb_remove_message(), and parse_uuid().

477 {
478  struct uuid uuid;
479  int i;
480  const char *cp;
481  char buf[3];
482 
483  if (strlen(in) != 36)
484  return -1;
485  for (i=0, cp = in; i <= 36; i++,cp++) {
486  if ((i == 8) || (i == 13) || (i == 18) ||
487  (i == 23)) {
488  if (*cp == '-')
489  continue;
490  else
491  return -1;
492  }
493  if (i== 36)
494  if (*cp == 0)
495  continue;
496  if (!isxdigit(*cp))
497  return -1;
498  }
499  uuid.time_low = strtoul(in, NULL, 16);
500  uuid.time_mid = strtoul(in+9, NULL, 16);
501  uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
502  uuid.clock_seq = strtoul(in+19, NULL, 16);
503  cp = in+24;
504  buf[2] = 0;
505  for (i=0; i < 6; i++) {
506  buf[0] = *cp++;
507  buf[1] = *cp++;
508  uuid.node[i] = strtoul(buf, NULL, 16);
509  }
510 
511  uuid_pack(&uuid, uu);
512  return 0;
513 }
__u16 clock_seq
Definition: gw_uuid.c:66
__u16 time_mid
Definition: gw_uuid.c:64
__u8 node[6]
Definition: gw_uuid.c:67
__u16 time_hi_and_version
Definition: gw_uuid.c:65
__u32 time_low
Definition: gw_uuid.c:63
Definition: gw_uuid.c:62
static void uuid_pack(const struct uuid *uu, uuid_t ptr)
Definition: gw_uuid.c:434

◆ uuid_shutdown()

void uuid_shutdown ( void  )

Definition at line 97 of file gw_uuid.c.

References get_random_fd().

Referenced by gwlib_shutdown().

98 {
99  int fd = get_random_fd();
100 
101  if (fd > 0)
102  close(fd);
103 }
static int get_random_fd(void)
Definition: gw_uuid.c:171

◆ uuid_time()

time_t uuid_time ( const uuid_t  uu,
struct timeval *  ret_tv 
)

Definition at line 587 of file gw_uuid.c.

References uuid::time_hi_and_version, uuid::time_low, uuid::time_mid, and uuid_unpack().

588 {
589  struct uuid uuid;
590  __u32 high;
591  struct timeval tv;
592  unsigned long long clock_reg;
593 
594  uuid_unpack(uu, &uuid);
595 
596  high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
597  clock_reg = uuid.time_low | ((unsigned long long) high << 32);
598 
599  clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
600  tv.tv_sec = clock_reg / 10000000;
601  tv.tv_usec = (clock_reg % 10000000) / 10;
602 
603  if (ret_tv)
604  *ret_tv = tv;
605 
606  return tv.tv_sec;
607 }
__u16 time_mid
Definition: gw_uuid.c:64
__u16 time_hi_and_version
Definition: gw_uuid.c:65
static void uuid_unpack(const uuid_t in, struct uuid *uu)
Definition: gw_uuid.c:526
__u32 time_low
Definition: gw_uuid.c:63
Definition: gw_uuid.c:62

◆ uuid_type()

int uuid_type ( const uuid_t  uu)

Definition at line 609 of file gw_uuid.c.

References uuid::time_hi_and_version, and uuid_unpack().

610 {
611  struct uuid uuid;
612 
613  uuid_unpack(uu, &uuid);
614  return ((uuid.time_hi_and_version >> 12) & 0xF);
615 }
__u16 time_hi_and_version
Definition: gw_uuid.c:65
static void uuid_unpack(const uuid_t in, struct uuid *uu)
Definition: gw_uuid.c:526
Definition: gw_uuid.c:62

◆ uuid_unparse()

void uuid_unparse ( const uuid_t  uu,
char *  out 
)

Definition at line 562 of file gw_uuid.c.

References uuid::clock_seq, uuid::node, uuid::time_hi_and_version, uuid::time_low, uuid::time_mid, and uuid_unpack().

Referenced by add_msg_cb(), append_uuid(), bearerbox_to_smpp(), boxc_sent_pop(), boxc_sent_push(), brunet_send_sms(), clickatell_send_sms(), delayed_http_reply(), dispatch_into_queue(), generate_smppid(), generic_parse_reply(), get_pattern(), handle_pdu(), kannel_parse_reply(), kannel_send_sms(), main(), main_connection_loop(), msg_cb(), status_cb(), store_spool_save(), store_to_dict(), store_uuid(), url_pattern(), and urltrans_fill_escape_codes().

563 {
564  struct uuid uuid;
565 
566  uuid_unpack(uu, &uuid);
567  sprintf(out,
568  "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
570  uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
571  uuid.node[0], uuid.node[1], uuid.node[2],
572  uuid.node[3], uuid.node[4], uuid.node[5]);
573 }
__u16 clock_seq
Definition: gw_uuid.c:66
__u16 time_mid
Definition: gw_uuid.c:64
__u8 node[6]
Definition: gw_uuid.c:67
__u16 time_hi_and_version
Definition: gw_uuid.c:65
static void uuid_unpack(const uuid_t in, struct uuid *uu)
Definition: gw_uuid.c:526
__u32 time_low
Definition: gw_uuid.c:63
Definition: gw_uuid.c:62

◆ uuid_variant()

int uuid_variant ( const uuid_t  uu)

Definition at line 617 of file gw_uuid.c.

References uuid::clock_seq, uuid_unpack(), UUID_VARIANT_DCE, UUID_VARIANT_MICROSOFT, UUID_VARIANT_NCS, and UUID_VARIANT_OTHER.

618 {
619  struct uuid uuid;
620  int var;
621 
622  uuid_unpack(uu, &uuid);
623  var = uuid.clock_seq;
624 
625  if ((var & 0x8000) == 0)
626  return UUID_VARIANT_NCS;
627  if ((var & 0x4000) == 0)
628  return UUID_VARIANT_DCE;
629  if ((var & 0x2000) == 0)
630  return UUID_VARIANT_MICROSOFT;
631  return UUID_VARIANT_OTHER;
632 }
__u16 clock_seq
Definition: gw_uuid.c:66
static void uuid_unpack(const uuid_t in, struct uuid *uu)
Definition: gw_uuid.c:526
Definition: gw_uuid.c:62
#define UUID_VARIANT_DCE
Definition: gw_uuid.h:38
#define UUID_VARIANT_MICROSOFT
Definition: gw_uuid.h:39
#define UUID_VARIANT_NCS
Definition: gw_uuid.h:37
#define UUID_VARIANT_OTHER
Definition: gw_uuid.h:40
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.