File: xhttp_pi.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 (390 lines) | stat: -rw-r--r-- 9,443 bytes parent folder | download | duplicates (2)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Copyright (C) 2012 VoIP Embedded, Inc.
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio 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
 *
 * Kamailio 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
 *
 * 2012-10-18  initial version (osas)
 */

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

#include "../../trim.h"
#include "../../sr_module.h"
#include "../../nonsip_hooks.h"
#include "../../modules/xhttp/api.h"
#include "../../rpc_lookup.h"
#include "xhttp_pi.h"
#include "xhttp_pi_fnc.h"
#include "http_db_handler.h"

/** @addtogroup xhttp_pi
 * @ingroup modules
 * @{
 *
 * <h1>Overview of Operation</h1>
 * This module provides a web interface for DB provisioning web interface.
 * It is built on top of the xhttp API module.
 */

/** @file
 *
 * This is the main file of xhttp_pi module which contains all the functions
 * related to http processing, as well as the module interface.
 */

MODULE_VERSION

str XHTTP_PI_REASON_OK = str_init("OK");
str XHTTP_PI_CONTENT_TYPE_TEXT_HTML = str_init("text/html");


extern ph_framework_t *ph_framework_data;


static int mod_init(void);
static int destroy(void);
static int xhttp_pi_dispatch(sip_msg_t* msg, char* s1, char* s2);


/** The context of the xhttp_pi request being processed.
 *
 * This is a global variable that records the context of the xhttp_pi request
 * being currently processed.
 * @sa pi_ctx
 */
static pi_ctx_t ctx;

static xhttp_api_t xhttp_api;

gen_lock_t* ph_lock;


str xhttp_pi_root = str_init("pi");
str filename = STR_NULL;

int buf_size = 0;
char error_buf[ERROR_REASON_BUF_LEN];

static cmd_export_t cmds[] = {
	{"dispatch_xhttp_pi",(cmd_function)xhttp_pi_dispatch,0,0,0,REQUEST_ROUTE},
	{0, 0, 0, 0, 0, 0}
};

static param_export_t params[] = {
	{"xhttp_pi_root",	PARAM_STR,	&xhttp_pi_root},
	{"xhttp_pi_buf_size",	INT_PARAM,	&buf_size},
	{"framework",	PARAM_STR,	&filename},
	{0, 0, 0}
};

static rpc_export_t rpc_methods[];

/** module exports */
struct module_exports exports= {
	"xhttp_pi",
	DEFAULT_DLFLAGS, /* dlopen flags */
	cmds,
	params,
	0,		/* exported statistics */
	0,		/* exported MI functions */
	0,		/* exported pseudo-variables */
	0,		/* extra processes */
	mod_init,	/* module initialization function */
	0,
	(destroy_function) destroy,	/* destroy function */
	NULL	/* per-child init function */
};



/** Implementation of pi_fault function required by the management API.
 *
 * This function will be called whenever a management function
 * indicates that an error ocurred while it was processing the request. The
 * function takes the reply code and reason phrase as parameters, these will
 * be put in the body of the reply.
 *
 * @param ctx A pointer to the context structure of the request being
 *            processed.
 * @param code Reason code.
 * @param fmt Formatting string used to build the reason phrase.
 */
static void pi_fault(pi_ctx_t* ctx, int code, char* fmt, ...)
{
	va_list ap;
	struct xhttp_pi_reply *reply = &ctx->reply;

	reply->code = code;
	va_start(ap, fmt);
	vsnprintf(error_buf, ERROR_REASON_BUF_LEN, fmt, ap);
	va_end(ap);
	reply->reason.len = strlen(error_buf);
	reply->reason.s = error_buf;
	/* reset body so we can print the error */
	reply->body.len = 0;

	return;
}


static int pi_send(pi_ctx_t* ctx)
{
	struct xhttp_pi_reply* reply;

	if (ctx->reply_sent) return 1;

	reply = &ctx->reply;

	if (0!=ph_run_pi_cmd(ctx)){
		LM_DBG("pi_fault(500,\"Internal Server Error\"\n");
		pi_fault(ctx, 500, "Internal Server Error");
	}

	ctx->reply_sent = 1;
	if (reply->body.len) {
		xhttp_api.reply(ctx->msg, reply->code, &reply->reason,
			&XHTTP_PI_CONTENT_TYPE_TEXT_HTML, &reply->body);
	}
	else {
		LM_DBG("xhttp_api.reply(%p, %d, %.*s, %.*s, %.*s)\n",
			ctx->msg, reply->code,
			(&reply->reason)->len, (&reply->reason)->s,
			(&XHTTP_PI_CONTENT_TYPE_TEXT_HTML)->len,
			(&XHTTP_PI_CONTENT_TYPE_TEXT_HTML)->s,
			(&reply->reason)->len, (&reply->reason)->s);
		xhttp_api.reply(ctx->msg, reply->code, &reply->reason,
			&XHTTP_PI_CONTENT_TYPE_TEXT_HTML, &reply->reason);
	}

	if (reply->buf.s) {
		pkg_free(reply->buf.s);
		reply->buf.s = NULL;
		reply->buf.len = 0;
	}
	if (ctx->arg.s) {
        pkg_free(ctx->arg.s);
        ctx->arg.s = NULL;
        ctx->arg.len = 0;
    }

	return 0;
}

/** Initialize xhttp_pi reply data structure.
 *
 * This function initializes the data structure that contains all data related
 * to the xhttp_pi reply being created. The function must be called before any
 * other function that adds data to the reply.
 * @param ctx pi_ctx_t structure to be initialized.
 * @return 0 on success, a negative number on error.
 */
static int init_xhttp_pi_reply(pi_ctx_t *ctx)
{
	struct xhttp_pi_reply *reply = &ctx->reply;

	reply->code = 200;
	reply->reason = XHTTP_PI_REASON_OK;
	reply->buf.s = pkg_malloc(buf_size);
	if (!reply->buf.s) {
		LM_ERR("oom\n");
		pi_fault(ctx, 500, "Internal Server Error (No memory left)");
		return -1;
	}
	reply->buf.len = buf_size;
	reply->body.s = reply->buf.s;
	reply->body.len = 0;
	return 0;
}



int ph_init_async_lock(void)
{
	ph_lock = lock_alloc();
		if (ph_lock==NULL) {
		LM_ERR("failed to create lock\n");
		return -1;
	}
	if (lock_init(ph_lock)==NULL) {
		LM_ERR("failed to init lock\n");
		return -1;
	}
	return 0;
}


void ph_destroy_async_lock(void)
{
	if (ph_lock) {
		lock_destroy(ph_lock);
		lock_dealloc(ph_lock);
	}
}

static int mod_init(void)
{
	int i;

	if (rpc_register_array(rpc_methods)!=0) {
		LM_ERR("failed to register RPC commands\n");
		return -1;
	}

	/* bind the XHTTP API */
	if (xhttp_load_api(&xhttp_api) < 0) {
		LM_ERR("cannot bind to XHTTP API\n");
		return -1;
	}

	/* Check xhttp_pi_buf_size param */
	if (buf_size == 0)
		buf_size = pkg_mem_size/3;

	/* Check xhttp_pi_root param */
	for(i=0;i<xhttp_pi_root.len;i++){
		if ( !isalnum(xhttp_pi_root.s[i]) && xhttp_pi_root.s[i]!='_') {
			LM_ERR("bad xhttp_pi_root param [%.*s], char [%c] "
				"- use only alphanumerical chars\n",
				xhttp_pi_root.len, xhttp_pi_root.s,
				xhttp_pi_root.s[i]);
			return -1;
		}
	}

	/* Check framework param */
	if (!filename.s || filename.len<=0) {
		LM_ERR("missing framework\n");
		return -1;
	}

		/* building a cache of pi module commands */
		if (0!=ph_init_cmds(&ph_framework_data, filename.s))
			return -1;
		for(i=0;i<ph_framework_data->ph_db_urls_size;i++){
			LM_DBG("initializing db[%d] [%s]\n",
				i, ph_framework_data->ph_db_urls[i].db_url.s);
			if (init_http_db(ph_framework_data, i)!=0) {
				LM_ERR("failed to initialize the DB support\n");
				return -1;
			}
			if (connect_http_db(ph_framework_data, i)) {
				LM_ERR("failed to connect to database\n");
				return -1;
			}
		}

	/* Build async lock */
	if (ph_init_async_lock() != 0) return -1;

	return 0;
}


int destroy(void)
{
	destroy_http_db(ph_framework_data);
	ph_destroy_async_lock();
	return 0;
}


static int xhttp_pi_dispatch(sip_msg_t* msg, char* s1, char* s2)
{
	str arg = {NULL, 0};
	int ret = 0;
	int i;

	if(!IS_HTTP(msg)) {
		LM_DBG("Got non HTTP msg\n");
		return NONSIP_MSG_PASS;
	}

	/* Init xhttp_pi context */
	if (ctx.reply.buf.s) LM_ERR("Unexpected buf value [%p][%d]\n",
				ctx.reply.buf.s, ctx.reply.buf.len);
	memset(&ctx, 0, sizeof(pi_ctx_t));
	ctx.msg = msg;
	ctx.mod = ctx.cmd = -1;
	if (init_xhttp_pi_reply(&ctx) < 0) goto send_reply;

	lock_get(ph_lock);
	LM_DBG("ph_framework_data: [%p]->[%p]\n", &ph_framework_data, ph_framework_data);
	/* Extract arguments from url */
	if (0!=ph_parse_url(&msg->first_line.u.request.uri,
				&ctx.mod, &ctx.cmd, &arg)){
		pi_fault(&ctx, 500, "Bad URL");
		goto send_reply;
	}

	if (arg.s) {
		if (arg.len) {
			/* Unescape args */
			ctx.arg.s = pkg_malloc((arg.len)*sizeof(char));
			if (ctx.arg.s==NULL){
				LM_ERR("oom\n");
				pi_fault(&ctx, 500, "Internal Server Error (oom)");
				goto send_reply;
			}
			for(i=0;i<arg.len;i++) if (arg.s[i]=='+') arg.s[i]=' ';
			if (0>un_escape(&arg, &ctx.arg)) {
				LM_ERR("unable to escape [%.*s]\n", arg.len, arg.s);
				pi_fault(&ctx, 500, "Bad arg in URL");
				goto send_reply;
			}
		}
		ctx.arg_received = 1;
	} else {
		LM_DBG("Got no arg\n");
		goto send_reply;
	}
	
send_reply:
	LM_DBG("ph_framework_data: [%p]->[%p]\n", &ph_framework_data, ph_framework_data);
	lock_release(ph_lock);
	if (!ctx.reply_sent) {
		ret = pi_send(&ctx);
	}
	if (ret < 0) return -1;
	return 0;
}


/* rpc function documentation */
static const char *rpc_reload_doc[2] = {
	"Reload the xml framework", 0
};

/* rpc function implementations */
static void rpc_reload(rpc_t *rpc, void *c) {
	lock_get(ph_lock);
	if (0!=ph_init_cmds(&ph_framework_data, filename.s)) {
		rpc->rpl_printf(c, "Reload failed");
	} else {
		rpc->rpl_printf(c, "Reload OK");
	}
	lock_release(ph_lock);
	return;
}

static rpc_export_t rpc_methods[] = {
	{"xhttp_pi.reload", rpc_reload, rpc_reload_doc, 0},
	{0, 0, 0, 0}
};