-
Notifications
You must be signed in to change notification settings - Fork 0
/
sipluafunc.c
148 lines (131 loc) · 3.85 KB
/
sipluafunc.c
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
/*
*
* Copyright (c) 2008, 2009
* Eric Gouyer <[email protected]>
* Copyright (c) 2008, 2009, 2010, 2011
* Arnaud Chong <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <unistd.h>
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
/* #include "../../data_lump.h" */
/* #include "../../parser/parse_param.h" */
/* #include "../../parser/msg_parser.h" */
/* #include "../../dprint.h" */
/* #include "../../action.h" */
/* #include "../../config.h" */
#include "../../parser/parse_uri.h"
#include "../sl/sl_api.h"
#include "siplua.h"
#include "sipluafunc.h"
#include "sipapi.h"
#include "sipstate.h"
#ifndef LM_GEN1
# define LM_GEN1 LOG /* 1.3.x backward compatibility */
#endif /* !LM_GEN1 */
void siplua_log(int lev, const char *format, ...)
{
va_list ap;
char *ret;
int priority;
if (!format)
return;
if (!(is_printable(lev) | lua_user_debug))
return;
va_start(ap, format);
vasprintf(&ret, format, ap);
va_end(ap);
LM_GEN1(lev, "siplua: %s", ret);
if (lua_user_debug)
{
switch (lev)
{
case L_ALERT: priority = LOG_ALERT; break;
case L_CRIT: priority = LOG_CRIT; break;
case L_ERR: priority = LOG_ERR; break;
case L_WARN: priority = LOG_WARNING; break;
case L_NOTICE: priority = LOG_NOTICE; break;
case L_INFO: priority = LOG_INFO; break;
case L_DBG: priority = LOG_DEBUG; break;
default: /* should not happen, no execution path permits it */
priority = LOG_ERR;
}
syslog(LOG_USER | priority, "siplua: %s", ret);
}
free(ret);
}
void siplua_notice(int local, const char *format, ...)
{
va_list ap;
if (!(local >= 0 && local <= 7))
return;
va_start(ap, format);
vsyslog((LOG_LOCAL0 + local) | LOG_NOTICE, format, ap);
va_end(ap);
}
static int siplua_exec(struct sip_msg* _msg, const char *fnc, const char *mystr)
{
str reason;
if ((_msg->first_line).type != SIP_INVALID)
parse_headers(_msg, ~0, 0);
switch ((_msg->first_line).type) {
case SIP_REQUEST:
if (parse_sip_msg_uri(_msg) < 0) {
LM_ERR("failed to parse Request-URI\n");
reason.s = "Bad Request-URI";
reason.len = sizeof("Bad Request-URI")-1;
if (slb.reply(_msg, 400, &reason) == -1) {
LM_ERR("failed to send reply\n");
}
return -1;
}
break;
case SIP_REPLY:
break;
default:
LM_ERR("invalid firstline");
return -1;
}
return sipstate_call(_msg, fnc, mystr);
}
int siplua_exec1(struct sip_msg* _msg, char *fnc, char *str)
{
/* siplua_log(L_DBG, "exec1(,,%s)", str); */
int ret;
ret = siplua_exec(_msg, fnc, NULL);
return (ret>=0)?1:-1;
}
int siplua_exec2(struct sip_msg* _msg, char *fnc, char *str)
{
int ret;
/* siplua_log(L_DBG, "exec2(,,%s)", str); */
ret = siplua_exec(_msg, fnc, str);
/* siplua_log(L_DBG, "exec2 RETURN %d", n); */
return (ret>=0)?1:-1;
}
int siplua_meminfo(struct sip_msg *msg)
{
struct mem_info info;
shm_info(&info);
siplua_log(L_INFO, "free/%d used/%d real_used/%d max_used/%d min_frag/%d total_frags/%d",
info.free, info.used, info.real_used, info.max_used, info.min_frag, info.total_frags);
return -1;
}