File: uuid_mod.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (178 lines) | stat: -rw-r--r-- 3,764 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <uuid/uuid.h>

#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "../../pvar.h"

MODULE_VERSION

static int  mod_init(void);
static int  child_init(int);
static void mod_destroy(void);

static uuid_t _k_uuid_val;
static char   _k_uuid_str[40];

int pv_get_uuid(sip_msg_t *msg, pv_param_t *param,
		pv_value_t *res);
int pv_parse_uuid_name(pv_spec_p sp, str *in);

static pv_export_t mod_pvs[] = {
	{ {"uuid", (sizeof("uuid")-1)}, PVT_OTHER, pv_get_uuid,
		0, pv_parse_uuid_name, 0, 0, 0},

	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};

static cmd_export_t cmds[]={
	{0, 0, 0, 0, 0, 0}
};

static param_export_t params[]={
	{0, 0, 0}
};

struct module_exports exports = {
	"uuid",
	DEFAULT_DLFLAGS, /* dlopen flags */
	cmds,
	params,
	0,
	0,              /* exported MI functions */
	mod_pvs,        /* exported pseudo-variables */
	0,              /* extra processes */
	mod_init,       /* module initialization function */
	0,              /* response function */
	mod_destroy,    /* destroy function */
	child_init      /* per child init function */
};



/**
 * init module function
 */
static int mod_init(void)
{
	uuid_generate(_k_uuid_val);

	_k_uuid_str[0] = '\0';
	uuid_unparse_lower(_k_uuid_val, _k_uuid_str);
	LM_DBG("uuid initialized - probing value [%s]\n", _k_uuid_str);
	uuid_clear(_k_uuid_val);
	_k_uuid_str[0] = '\0';

	return 0;
}

/**
 * @brief Initialize async module children
 */
static int child_init(int rank)
{
	if (rank!=PROC_MAIN)
		return 0;

	return 0;
}
/**
 * destroy module function
 */
static void mod_destroy(void)
{
	uuid_generate(_k_uuid_val);
	uuid_clear(_k_uuid_val);
}

/**
 * parse the name of the $uuid(name)
 */
int pv_parse_uuid_name(pv_spec_p sp, str *in)
{
	if(sp==NULL || in==NULL || in->len<=0)
		return -1;
	switch(in->s[0])
	{
		case 'g':
		case 'G':
			sp->pvp.pvn.u.isname.name.n = 0;
			break;
		case 'r':
		case 'R':
			sp->pvp.pvn.u.isname.name.n = 1;
			break;
		case 't':
		case 'T':
			sp->pvp.pvn.u.isname.name.n = 2;
			break;
		case 's':
		case 'S':
			sp->pvp.pvn.u.isname.name.n = 3;
			break;
		default:
			sp->pvp.pvn.u.isname.name.n = 0;
	}
	sp->pvp.pvn.type = PV_NAME_INTSTR;
	sp->pvp.pvn.u.isname.type = 0;

	return 0;
}

/**
 * return the value of $uuid(name)
 */
int pv_get_uuid(sip_msg_t *msg, pv_param_t *param,
		pv_value_t *res)
{
	if(param==NULL)
		return -1;
	switch(param->pvn.u.isname.name.n)
	{
		case 1:
			uuid_generate_random(_k_uuid_val);
		break;
		case 2:
			uuid_generate_time(_k_uuid_val);
		break;
		case 3:
#ifndef __OS_darwin
			if(uuid_generate_time_safe(_k_uuid_val))
				return pv_get_null(msg, param, res);
#else
			uuid_generate_time(_k_uuid_val);
#endif
		break;
		default:
			uuid_generate(_k_uuid_val);
	}
	uuid_unparse_lower(_k_uuid_val, _k_uuid_str);
	return pv_get_strzval(msg, param, res, _k_uuid_str);
}