-
Notifications
You must be signed in to change notification settings - Fork 909
/
log_custom_mod.c
197 lines (162 loc) · 4.19 KB
/
log_custom_mod.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
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
/**
* Copyright (C) 2015 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 <stdarg.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../mod_fix.h"
#include "../../forward.h"
#include "../../resolve.h"
#include "../../udp_server.h"
MODULE_VERSION
static int _lc_log_udp = 0;
static struct dest_info _lc_udp_dst = {0};
static int mod_init(void);
static int child_init(int);
static void mod_destroy(void);
static int w_log_udp(struct sip_msg* msg, char* txt, char* p2);
void _lc_core_log_udp(int lpriority, const char *format, ...);
static cmd_export_t cmds[]={
{"log_udp", (cmd_function)w_log_udp, 1, fixup_spve_null,
0, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}
};
static param_export_t params[]={
{0, 0, 0}
};
struct module_exports exports = {
"log_custom",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds,
params,
0,
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
mod_init, /* module initialization function */
0, /* response function */
mod_destroy, /* destroy function */
child_init /* per child init function */
};
int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
str dest = {0};
int ret = 0;
struct sip_uri next_hop, *u;
char *p;
if(_km_log_engine_type==0 || _km_log_engine_data==0)
return 0;
if(strcasecmp(_km_log_engine_type, "udp")!=0)
return 0;
dest.s = _km_log_engine_data;
dest.len = strlen(dest.s);
init_dest_info(&_lc_udp_dst);
u = &next_hop;
u->port_no = 5060;
u->host = dest;
p = dest.s;
/* detect ipv6 */
p = memchr(p, ']', dest.len);
if (p) p++;
else p = dest.s;
p = memchr(p, ':', dest.len - (p - dest.s));
if (p) {
u->host.len = p - dest.s;
p++;
u->port_no = str2s(p, dest.len - (p - dest.s), NULL);
}
ret = sip_hostport2su(&_lc_udp_dst.to, &u->host, u->port_no,
&_lc_udp_dst.proto);
if(ret!=0) {
LM_ERR("failed to resolve [%.*s]\n", u->host.len,
ZSW(u->host.s));
return -1;;
}
return 0;
}
/**
* init module function
*/
static int mod_init(void)
{
return 0;
}
/**
* @brief Initialize module children
*/
static int child_init(int rank)
{
if(rank!=PROC_INIT)
return 0;
_lc_udp_dst.proto = PROTO_UDP;
_lc_udp_dst.send_sock=get_send_socket(NULL, &_lc_udp_dst.to, PROTO_UDP);
if (_lc_udp_dst.send_sock==0) {
_lc_udp_dst.send_sock = get_out_socket(&_lc_udp_dst.to, PROTO_UDP);
if (_lc_udp_dst.send_sock==0) {
LM_ERR("failed to get send socket\n");
return -1;
}
}
LM_DBG("setting udp-send custom logging function\n");
km_log_func_set(&_lc_core_log_udp);
_lc_log_udp = 1;
return 0;
}
/**
* destroy module function
*/
static void mod_destroy(void)
{
}
/**
*
*/
static int w_log_udp(struct sip_msg* msg, char* txt, char* p2)
{
str stxt;
int ret;
if(_lc_log_udp==0)
return 1;
if(fixup_get_svalue(msg, (gparam_t*)txt, &stxt)!=0) {
LM_ERR("unable to get text parameter\n");
return -1;
}
ret=udp_send(&_lc_udp_dst, stxt.s, stxt.len);
if(ret==0) return 1;
return ret;
}
#define LC_LOG_MSG_MAX_SIZE 16384
void _lc_core_log_udp(int lpriority, const char *format, ...)
{
va_list arglist;
char obuf[LC_LOG_MSG_MAX_SIZE];
int n;
va_start(arglist, format);
n = 0;
n += snprintf(obuf + n, LC_LOG_MSG_MAX_SIZE - n, "(%d) ", my_pid());
n += vsnprintf(obuf + n, LC_LOG_MSG_MAX_SIZE - n, format, arglist);
va_end(arglist);
udp_send(&_lc_udp_dst, obuf, n);
}