uuid_mod.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. *
  12. * This file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <uuid/uuid.h>
  27. #include "../../sr_module.h"
  28. #include "../../dprint.h"
  29. #include "../../ut.h"
  30. #include "../../pvar.h"
  31. MODULE_VERSION
  32. static int mod_init(void);
  33. static int child_init(int);
  34. static void mod_destroy(void);
  35. static uuid_t _k_uuid_val;
  36. static char _k_uuid_str[40];
  37. int pv_get_uuid(sip_msg_t *msg, pv_param_t *param,
  38. pv_value_t *res);
  39. int pv_parse_uuid_name(pv_spec_p sp, str *in);
  40. static pv_export_t mod_pvs[] = {
  41. { {"uuid", (sizeof("uuid")-1)}, PVT_OTHER, pv_get_uuid,
  42. 0, pv_parse_uuid_name, 0, 0, 0},
  43. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  44. };
  45. static cmd_export_t cmds[]={
  46. {0, 0, 0, 0, 0, 0}
  47. };
  48. static param_export_t params[]={
  49. {0, 0, 0}
  50. };
  51. struct module_exports exports = {
  52. "uuid",
  53. DEFAULT_DLFLAGS, /* dlopen flags */
  54. cmds,
  55. params,
  56. 0,
  57. 0, /* exported MI functions */
  58. mod_pvs, /* exported pseudo-variables */
  59. 0, /* extra processes */
  60. mod_init, /* module initialization function */
  61. 0, /* response function */
  62. mod_destroy, /* destroy function */
  63. child_init /* per child init function */
  64. };
  65. /**
  66. * init module function
  67. */
  68. static int mod_init(void)
  69. {
  70. uuid_generate(_k_uuid_val);
  71. _k_uuid_str[0] = '\0';
  72. uuid_unparse_lower(_k_uuid_val, _k_uuid_str);
  73. LM_DBG("uuid initialized - probing value [%s]\n", _k_uuid_str);
  74. uuid_clear(_k_uuid_val);
  75. _k_uuid_str[0] = '\0';
  76. return 0;
  77. }
  78. /**
  79. * @brief Initialize async module children
  80. */
  81. static int child_init(int rank)
  82. {
  83. if (rank!=PROC_MAIN)
  84. return 0;
  85. return 0;
  86. }
  87. /**
  88. * destroy module function
  89. */
  90. static void mod_destroy(void)
  91. {
  92. uuid_generate(_k_uuid_val);
  93. uuid_clear(_k_uuid_val);
  94. }
  95. /**
  96. * parse the name of the $uuid(name)
  97. */
  98. int pv_parse_uuid_name(pv_spec_p sp, str *in)
  99. {
  100. if(sp==NULL || in==NULL || in->len<=0)
  101. return -1;
  102. switch(in->s[0])
  103. {
  104. case 'g':
  105. case 'G':
  106. sp->pvp.pvn.u.isname.name.n = 0;
  107. break;
  108. case 'r':
  109. case 'R':
  110. sp->pvp.pvn.u.isname.name.n = 1;
  111. break;
  112. case 't':
  113. case 'T':
  114. sp->pvp.pvn.u.isname.name.n = 2;
  115. break;
  116. case 's':
  117. case 'S':
  118. sp->pvp.pvn.u.isname.name.n = 3;
  119. break;
  120. default:
  121. sp->pvp.pvn.u.isname.name.n = 0;
  122. }
  123. sp->pvp.pvn.type = PV_NAME_INTSTR;
  124. sp->pvp.pvn.u.isname.type = 0;
  125. return 0;
  126. }
  127. /**
  128. * return the value of $uuid(name)
  129. */
  130. int pv_get_uuid(sip_msg_t *msg, pv_param_t *param,
  131. pv_value_t *res)
  132. {
  133. if(param==NULL)
  134. return -1;
  135. switch(param->pvn.u.isname.name.n)
  136. {
  137. case 1:
  138. uuid_generate_random(_k_uuid_val);
  139. break;
  140. case 2:
  141. uuid_generate_time(_k_uuid_val);
  142. break;
  143. case 3:
  144. #ifndef __OS_darwin
  145. if(uuid_generate_time_safe(_k_uuid_val))
  146. return pv_get_null(msg, param, res);
  147. #else
  148. uuid_generate_time(_k_uuid_val);
  149. #endif
  150. break;
  151. default:
  152. uuid_generate(_k_uuid_val);
  153. }
  154. uuid_unparse_lower(_k_uuid_val, _k_uuid_str);
  155. return pv_get_strzval(msg, param, res, _k_uuid_str);
  156. }