Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,407 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio 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
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-
22
-/*!
23
- * \file
24
- * \brief Kamailio core ::
25
- * \ingroup core
26
- * Module: \ref core
27
- */
28
-
29
-
30
-#include <string.h>
31
-#include <stdlib.h>
32
-#include <sys/time.h>
33
-
34
-#include "receive.h"
35
-#include "globals.h"
36
-#include "dprint.h"
37
-#include "route.h"
38
-#include "parser/msg_parser.h"
39
-#include "forward.h"
40
-#include "action.h"
41
-#include "mem/mem.h"
42
-#include "stats.h"
43
-#include "ip_addr.h"
44
-#include "script_cb.h"
45
-#include "nonsip_hooks.h"
46
-#include "dset.h"
47
-#include "usr_avp.h"
48
-#ifdef WITH_XAVP
49
-#include "xavp.h"
50
-#endif
51
-#include "select_buf.h"
52
-
53
-#include "tcp_server.h" /* for tcpconn_add_alias */
54
-#include "tcp_options.h" /* for access to tcp_accept_aliases*/
55
-#include "cfg/cfg.h"
56
-#include "core_stats.h"
57
-#include "kemi.h"
58
-
59
-#ifdef DEBUG_DMALLOC
60
-#include <mem/dmalloc.h>
61
-#endif
62
-
63
-unsigned int msg_no=0;
64
-/* address preset vars */
65
-str default_global_address={0,0};
66
-str default_global_port={0,0};
67
-str default_via_address={0,0};
68
-str default_via_port={0,0};
69
-
70
-/**
71
- * increment msg_no and return the new value
72
- */
73
-unsigned int inc_msg_no(void)
74
-{
75
-	return ++msg_no;
76
-}
77
-
78
-/**
79
- *
80
- */
81
-int sip_check_fline(char* buf, unsigned int len)
82
-{
83
-	char *p;
84
-	int m;
85
-
86
-	m = 0;
87
-	for(p=buf; p<buf+len; p++) {
88
-		/* first check if is a reply - starts with SIP/2.0 */
89
-		if(m==0) {
90
-			if(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n') continue;
91
-			if(buf+len-p<10) return -1;
92
-			if(strncmp(p, "SIP/2.0 ", 8)==0) {
93
-				LM_DBG("first line indicates a SIP reply\n");
94
-				return 0;
95
-			}
96
-			m=1;
97
-		} else {
98
-			/* check if a request - before end of first line is SIP/2.0 */
99
-			if(*p!='\r' && *p!='\n') continue;
100
-			if(p-10>=buf) {
101
-				if(strncmp(p-8, " SIP/2.0", 8)==0) {
102
-					LM_DBG("first line indicates a SIP request\n");
103
-					return 0;
104
-				}
105
-			}
106
-			return -1;
107
-		}
108
-	}
109
-	return -1;
110
-}
111
-
112
-/** Receive message
113
- *  WARNING: buf must be 0 terminated (buf[len]=0) or some things might
114
- * break (e.g.: modules/textops)
115
- */
116
-int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
117
-{
118
-	struct sip_msg* msg;
119
-	struct run_act_ctx ctx;
120
-	struct run_act_ctx *bctx;
121
-	int ret;
122
-#ifdef STATS
123
-	int skipped = 1;
124
-	int stats_on = 1;
125
-#else
126
-	int stats_on = 0;
127
-#endif
128
-	struct timeval tvb, tve;
129
-	struct timezone tz;
130
-	unsigned int diff = 0;
131
-	str inb;
132
-	sr_net_info_t netinfo;
133
-	sr_kemi_eng_t *keng = NULL;
134
-
135
-	if(sr_event_enabled(SREV_NET_DATA_RECV)) {
136
-		if(sip_check_fline(buf, len)==0) {
137
-			memset(&netinfo, 0, sizeof(sr_net_info_t));
138
-			netinfo.data.s = buf;
139
-			netinfo.data.len = len;
140
-			netinfo.rcv = rcv_info;
141
-			sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo);
142
-		}
143
-	}
144
-
145
-	inb.s = buf;
146
-	inb.len = len;
147
-	sr_event_exec(SREV_NET_DATA_IN, (void*)&inb);
148
-	len = inb.len;
149
-
150
-	msg=pkg_malloc(sizeof(struct sip_msg));
151
-	if (msg==0) {
152
-		LM_ERR("no mem for sip_msg\n");
153
-		goto error00;
154
-	}
155
-	msg_no++;
156
-	/* number of vias parsed -- good for diagnostic info in replies */
157
-	via_cnt=0;
158
-
159
-	memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
160
-	/* fill in msg */
161
-	msg->buf=buf;
162
-	msg->len=len;
163
-	/* zero termination (termination of orig message bellow not that
164
-	 * useful as most of the work is done with scratch-pad; -jiri  */
165
-	/* buf[len]=0; */ /* WARNING: zero term removed! */
166
-	msg->rcv=*rcv_info;
167
-	msg->id=msg_no;
168
-	msg->pid=my_pid();
169
-	msg->set_global_address=default_global_address;
170
-	msg->set_global_port=default_global_port;
171
-
172
-	if(likely(sr_msg_time==1)) msg_set_time(msg);
173
-
174
-	if (parse_msg(buf,len, msg)!=0){
175
-		if((ret=sr_event_exec(SREV_RCV_NOSIP, (void*)msg))<NONSIP_MSG_DROP) {
176
-			LOG(cfg_get(core, core_cfg, corelog),
177
-				"core parsing of SIP message failed (%s:%d/%d)\n",
178
-				ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
179
-				(int)msg->rcv.proto);
180
-			sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
181
-		}
182
-		else if(ret == NONSIP_MSG_DROP) goto error02;
183
-	}
184
-
185
-	parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F, 0);
186
-	LM_DBG("--- received sip message - %s - call-id: [%.*s] - cseq: [%.*s]\n",
187
-			(msg->first_line.type==SIP_REQUEST)?"request":"reply",
188
-			(msg->callid && msg->callid->body.s)?msg->callid->body.len:0,
189
-			(msg->callid && msg->callid->body.s)?msg->callid->body.s:"",
190
-			(msg->cseq && msg->cseq->body.s)?msg->cseq->body.len:0,
191
-			(msg->cseq && msg->cseq->body.s)?msg->cseq->body.s:"");
192
-
193
-	/* set log prefix */
194
-	log_prefix_set(msg);
195
-
196
-	/* ... clear branches from previous message */
197
-	clear_branches();
198
-
199
-	if (msg->first_line.type==SIP_REQUEST){
200
-		ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
201
-		if (!IS_SIP(msg)){
202
-			if ((ret=nonsip_msg_run_hooks(msg))!=NONSIP_MSG_ACCEPT){
203
-				if (unlikely(ret==NONSIP_MSG_ERROR))
204
-					goto error03;
205
-				goto end; /* drop the message */
206
-			}
207
-		}
208
-		/* sanity checks */
209
-		if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
210
-			/* no via, send back error ? */
211
-			LM_ERR("no via found in request\n");
212
-			STATS_BAD_MSG();
213
-			goto error02;
214
-		}
215
-		/* check if necessary to add receive?->moved to forward_req */
216
-		/* check for the alias stuff */
217
-#ifdef USE_TCP
218
-		if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) &&
219
-				(((rcv_info->proto==PROTO_TCP) && !tcp_disable)
220
-#ifdef USE_TLS
221
-					|| ((rcv_info->proto==PROTO_TLS) && !tls_disable)
222
-#endif
223
-				)
224
-			){
225
-			if (tcpconn_add_alias(rcv_info->proto_reserved1, msg->via1->port,
226
-									rcv_info->proto)!=0){
227
-				LM_ERR("tcp alias failed\n");
228
-				/* continue */
229
-			}
230
-		}
231
-#endif
232
-
233
-	/*	skip: */
234
-		LM_DBG("preparing to run routing scripts...\n");
235
-		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
236
-				|| stats_on==1) {
237
-			gettimeofday( & tvb, &tz );
238
-		}
239
-		/* execute pre-script callbacks, if any; -jiri */
240
-		/* if some of the callbacks said not to continue with
241
-		 * script processing, don't do so
242
-		 * if we are here basic sanity checks are already done
243
-		 * (like presence of at least one via), so you can count
244
-		 * on via1 being parsed in a pre-script callback --andrei
245
-		*/
246
-		if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
247
-		{
248
-			STATS_REQ_FWD_DROP();
249
-			goto end; /* drop the request */
250
-		}
251
-
252
-		set_route_type(REQUEST_ROUTE);
253
-		/* exec the routing script */
254
-		if(unlikely(main_rt.rlist[DEFAULT_RT]==NULL)) {
255
-			keng = sr_kemi_eng_get();
256
-			if(keng==NULL) {
257
-				LM_ERR("no config routing engine registered\n");
258
-				goto error_req;
259
-			}
260
-			if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL)<0) {
261
-				LM_NOTICE("negative return code from engine function\n");
262
-			}
263
-		} else {
264
-			if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){
265
-				LM_WARN("error while trying script\n");
266
-				goto error_req;
267
-			}
268
-		}
269
-
270
-		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
271
-				|| stats_on==1) {
272
-			gettimeofday( & tve, &tz );
273
-			diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
274
-			LOG(cfg_get(core, core_cfg, latency_cfg_log),
275
-					"request-route executed in: %d usec\n", diff);
276
-#ifdef STATS
277
-			stats->processed_requests++;
278
-			stats->acc_req_time += diff;
279
-			STATS_RX_REQUEST( msg->first_line.u.request.method_value );
280
-#endif
281
-		}
282
-
283
-		/* execute post request-script callbacks */
284
-		exec_post_script_cb(msg, REQUEST_CB_TYPE);
285
-	}else if (msg->first_line.type==SIP_REPLY){
286
-		/* sanity checks */
287
-		if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
288
-			/* no via, send back error ? */
289
-			LM_ERR("no via found in reply\n");
290
-			STATS_BAD_RPL();
291
-			goto error02;
292
-		}
293
-
294
-		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
295
-				|| stats_on==1) {
296
-			gettimeofday( & tvb, &tz );
297
-		}
298
-#ifdef STATS
299
-		STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
300
-#endif
301
-
302
-		/* execute pre-script callbacks, if any; -jiri */
303
-		/* if some of the callbacks said not to continue with
304
-		 * script processing, don't do so
305
-		 * if we are here basic sanity checks are already done
306
-		 * (like presence of at least one via), so you can count
307
-		 * on via1 being parsed in a pre-script callback --andrei
308
-		*/
309
-		if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
310
-		{
311
-			STATS_RPL_FWD_DROP();
312
-			goto end; /* drop the reply */
313
-		}
314
-
315
-		/* exec the onreply routing script */
316
-		keng = sr_kemi_eng_get();
317
-		if (onreply_rt.rlist[DEFAULT_RT]!=NULL || keng!=NULL){
318
-			set_route_type(CORE_ONREPLY_ROUTE);
319
-			ret = 1;
320
-			if(unlikely(keng!=NULL)) {
321
-				bctx = sr_kemi_act_ctx_get();
322
-				init_run_actions_ctx(&ctx);
323
-				sr_kemi_act_ctx_set(&ctx);
324
-				ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
325
-				sr_kemi_act_ctx_set(bctx);
326
-			} else {
327
-				ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
328
-			}
329
-#ifndef NO_ONREPLY_ROUTE_ERROR
330
-			if (unlikely(ret<0)){
331
-				LM_WARN("error while trying onreply script\n");
332
-				goto error_rpl;
333
-			}else
334
-#endif /* NO_ONREPLY_ROUTE_ERROR */
335
-			if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){
336
-				STATS_RPL_FWD_DROP();
337
-				goto skip_send_reply; /* drop the message, no error */
338
-			}
339
-		}
340
-		/* send the msg */
341
-		forward_reply(msg);
342
-	skip_send_reply:
343
-		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
344
-				|| stats_on==1) {
345
-			gettimeofday( & tve, &tz );
346
-			diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
347
-			LOG(cfg_get(core, core_cfg, latency_cfg_log),
348
-					"reply-route executed in: %d usec\n", diff);
349
-#ifdef STATS
350
-			stats->processed_responses++;
351
-			stats->acc_res_time+=diff;
352
-#endif
353
-		}
354
-
355
-		/* execute post reply-script callbacks */
356
-		exec_post_script_cb(msg, ONREPLY_CB_TYPE);
357
-	}
358
-
359
-end:
360
-#ifdef STATS
361
-	skipped = 0;
362
-#endif
363
-	/* free possible loaded avps -bogdan */
364
-	reset_avps();
365
-#ifdef WITH_XAVP
366
-	xavp_reset_list();
367
-#endif
368
-	LM_DBG("cleaning up\n");
369
-	free_sip_msg(msg);
370
-	pkg_free(msg);
371
-#ifdef STATS
372
-	if (skipped) STATS_RX_DROPS;
373
-#endif
374
-	/* reset log prefix */
375
-	log_prefix_set(NULL);
376
-	return 0;
377
-
378
-#ifndef NO_ONREPLY_ROUTE_ERROR
379
-error_rpl:
380
-	/* execute post reply-script callbacks */
381
-	exec_post_script_cb(msg, ONREPLY_CB_TYPE);
382
-	reset_avps();
383
-#ifdef WITH_XAVP
384
-	xavp_reset_list();
385
-#endif
386
-	goto error02;
387
-#endif /* NO_ONREPLY_ROUTE_ERROR */
388
-error_req:
389
-	LM_DBG("error:...\n");
390
-	/* execute post request-script callbacks */
391
-	exec_post_script_cb(msg, REQUEST_CB_TYPE);
392
-error03:
393
-	/* free possible loaded avps -bogdan */
394
-	reset_avps();
395
-#ifdef WITH_XAVP
396
-	xavp_reset_list();
397
-#endif
398
-error02:
399
-	free_sip_msg(msg);
400
-	pkg_free(msg);
401
-error00:
402
-	STATS_RX_DROPS;
403
-	/* reset log prefix */
404
-	log_prefix_set(NULL);
405
-	return -1;
406
-}
407
-
Browse code

core: updates to kemi callbacks for core routing blocks

Daniel-Constantin Mierla authored on 14/11/2016 09:30:41
Showing 1 changed files
... ...
@@ -257,7 +257,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
257 257
 				LM_ERR("no config routing engine registered\n");
258 258
 				goto error_req;
259 259
 			}
260
-			if(keng->froute(msg, REQUEST_ROUTE, NULL)<0) {
260
+			if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL)<0) {
261 261
 				LM_NOTICE("negative return code from engine function\n");
262 262
 			}
263 263
 		} else {
... ...
@@ -321,7 +321,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
321 321
 				bctx = sr_kemi_act_ctx_get();
322 322
 				init_run_actions_ctx(&ctx);
323 323
 				sr_kemi_act_ctx_set(&ctx);
324
-				ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL);
324
+				ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
325 325
 				sr_kemi_act_ctx_set(bctx);
326 326
 			} else {
327 327
 				ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
Browse code

core: from/to/callid/cseq headers are parsed before logging prefix is used

Daniel-Constantin Mierla authored on 28/09/2016 13:31:33
Showing 1 changed files
... ...
@@ -181,6 +181,8 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
181 181
 		}
182 182
 		else if(ret == NONSIP_MSG_DROP) goto error02;
183 183
 	}
184
+
185
+	parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F, 0);
184 186
 	LM_DBG("--- received sip message - %s - call-id: [%.*s] - cseq: [%.*s]\n",
185 187
 			(msg->first_line.type==SIP_REQUEST)?"request":"reply",
186 188
 			(msg->callid && msg->callid->body.s)?msg->callid->body.len:0,
Browse code

core: debug message with details of callid and cseq when a sip message is received

Daniel-Constantin Mierla authored on 28/09/2016 07:57:50
Showing 1 changed files
... ...
@@ -178,10 +178,15 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
178 178
 				ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
179 179
 				(int)msg->rcv.proto);
180 180
 			sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
181
-		}		
181
+		}
182 182
 		else if(ret == NONSIP_MSG_DROP) goto error02;
183 183
 	}
184
-	LM_DBG("After parse_msg...\n");
184
+	LM_DBG("--- received sip message - %s - call-id: [%.*s] - cseq: [%.*s]\n",
185
+			(msg->first_line.type==SIP_REQUEST)?"request":"reply",
186
+			(msg->callid && msg->callid->body.s)?msg->callid->body.len:0,
187
+			(msg->callid && msg->callid->body.s)?msg->callid->body.s:"",
188
+			(msg->cseq && msg->cseq->body.s)?msg->cseq->body.len:0,
189
+			(msg->cseq && msg->cseq->body.s)?msg->cseq->body.s:"");
185 190
 
186 191
 	/* set log prefix */
187 192
 	log_prefix_set(msg);
Browse code

core: added more flexible processing for nonsip_hook messages

Alexandr Dubovikov authored on 01/08/2016 15:39:03
Showing 1 changed files
... ...
@@ -172,14 +172,14 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
172 172
 	if(likely(sr_msg_time==1)) msg_set_time(msg);
173 173
 
174 174
 	if (parse_msg(buf,len, msg)!=0){
175
-		if(sr_event_exec(SREV_RCV_NOSIP, (void*)msg)!=0) {
175
+		if((ret=sr_event_exec(SREV_RCV_NOSIP, (void*)msg))<NONSIP_MSG_DROP) {
176 176
 			LOG(cfg_get(core, core_cfg, corelog),
177 177
 				"core parsing of SIP message failed (%s:%d/%d)\n",
178 178
 				ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
179 179
 				(int)msg->rcv.proto);
180 180
 			sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
181
-		}
182
-		goto error02;
181
+		}		
182
+		else if(ret == NONSIP_MSG_DROP) goto error02;
183 183
 	}
184 184
 	LM_DBG("After parse_msg...\n");
185 185
 
Browse code

core: new core parameter latency_cfg_log

- if set to a log level less or equal than debug parameter, a log
message with the duration of executing request route or reply route is
printed to syslog
- default value is L_DBG

Daniel-Constantin Mierla authored on 25/04/2016 14:57:19
Showing 1 changed files
... ...
@@ -121,10 +121,13 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
121 121
 	int ret;
122 122
 #ifdef STATS
123 123
 	int skipped = 1;
124
+	int stats_on = 1;
125
+#else
126
+	int stats_on = 0;
127
+#endif
124 128
 	struct timeval tvb, tve;
125 129
 	struct timezone tz;
126
-	unsigned int diff;
127
-#endif
130
+	unsigned int diff = 0;
128 131
 	str inb;
129 132
 	sr_net_info_t netinfo;
130 133
 	sr_kemi_eng_t *keng = NULL;
... ...
@@ -222,9 +225,10 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
222 225
 
223 226
 	/*	skip: */
224 227
 		LM_DBG("preparing to run routing scripts...\n");
225
-#ifdef  STATS
226
-		gettimeofday( & tvb, &tz );
227
-#endif
228
+		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
229
+				|| stats_on==1) {
230
+			gettimeofday( & tvb, &tz );
231
+		}
228 232
 		/* execute pre-script callbacks, if any; -jiri */
229 233
 		/* if some of the callbacks said not to continue with
230 234
 		 * script processing, don't do so
... ...
@@ -256,14 +260,18 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
256 260
 			}
257 261
 		}
258 262
 
263
+		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
264
+				|| stats_on==1) {
265
+			gettimeofday( & tve, &tz );
266
+			diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
267
+			LOG(cfg_get(core, core_cfg, latency_cfg_log),
268
+					"request-route executed in: %d usec\n", diff);
259 269
 #ifdef STATS
260
-		gettimeofday( & tve, &tz );
261
-		diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
262
-		stats->processed_requests++;
263
-		stats->acc_req_time += diff;
264
-		LM_DBG("successfully ran routing scripts...(%d usec)\n", diff);
265
-		STATS_RX_REQUEST( msg->first_line.u.request.method_value );
270
+			stats->processed_requests++;
271
+			stats->acc_req_time += diff;
272
+			STATS_RX_REQUEST( msg->first_line.u.request.method_value );
266 273
 #endif
274
+		}
267 275
 
268 276
 		/* execute post request-script callbacks */
269 277
 		exec_post_script_cb(msg, REQUEST_CB_TYPE);
... ...
@@ -276,8 +284,11 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
276 284
 			goto error02;
277 285
 		}
278 286
 
287
+		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
288
+				|| stats_on==1) {
289
+			gettimeofday( & tvb, &tz );
290
+		}
279 291
 #ifdef STATS
280
-		gettimeofday( & tvb, &tz );
281 292
 		STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
282 293
 #endif
283 294
 
... ...
@@ -322,13 +333,17 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
322 333
 		/* send the msg */
323 334
 		forward_reply(msg);
324 335
 	skip_send_reply:
336
+		if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
337
+				|| stats_on==1) {
338
+			gettimeofday( & tve, &tz );
339
+			diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
340
+			LOG(cfg_get(core, core_cfg, latency_cfg_log),
341
+					"reply-route executed in: %d usec\n", diff);
325 342
 #ifdef STATS
326
-		gettimeofday( & tve, &tz );
327
-		diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
328
-		stats->processed_responses++;
329
-		stats->acc_res_time+=diff;
330
-		LM_DBG("successfully ran reply processing...(%d usec)\n", diff);
343
+			stats->processed_responses++;
344
+			stats->acc_res_time+=diff;
331 345
 #endif
346
+		}
332 347
 
333 348
 		/* execute post reply-script callbacks */
334 349
 		exec_post_script_cb(msg, ONREPLY_CB_TYPE);
Browse code

core: set actions context for running kemi reply_route callback

Daniel-Constantin Mierla authored on 14/04/2016 19:23:52
Showing 1 changed files
... ...
@@ -117,6 +117,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
117 117
 {
118 118
 	struct sip_msg* msg;
119 119
 	struct run_act_ctx ctx;
120
+	struct run_act_ctx *bctx;
120 121
 	int ret;
121 122
 #ifdef STATS
122 123
 	int skipped = 1;
... ...
@@ -294,14 +295,16 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
294 295
 		}
295 296
 
296 297
 		/* exec the onreply routing script */
297
-		if (onreply_rt.rlist[DEFAULT_RT]){
298
+		keng = sr_kemi_eng_get();
299
+		if (onreply_rt.rlist[DEFAULT_RT]!=NULL || keng!=NULL){
298 300
 			set_route_type(CORE_ONREPLY_ROUTE);
299 301
 			ret = 1;
300
-			if(unlikely(main_rt.rlist[DEFAULT_RT]==NULL)) {
301
-				keng = sr_kemi_eng_get();
302
-				if(keng!=NULL) {
303
-					ret = keng->froute(msg, REQUEST_ROUTE, NULL);
304
-				}
302
+			if(unlikely(keng!=NULL)) {
303
+				bctx = sr_kemi_act_ctx_get();
304
+				init_run_actions_ctx(&ctx);
305
+				sr_kemi_act_ctx_set(&ctx);
306
+				ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL);
307
+				sr_kemi_act_ctx_set(bctx);
305 308
 			} else {
306 309
 				ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
307 310
 			}
Browse code

core: ability to run alternative request_route and reply_route handlers

- they can be offered by the embedded interpreters
- at this moment app_lua can execute functions from the script loaded
with param 'load':
- instead of request_route it executes ksr_request_route()
- instead of reply_route it executes ksr_reply_route()

Daniel-Constantin Mierla authored on 12/04/2016 12:14:15
Showing 1 changed files
... ...
@@ -54,6 +54,7 @@
54 54
 #include "tcp_options.h" /* for access to tcp_accept_aliases*/
55 55
 #include "cfg/cfg.h"
56 56
 #include "core_stats.h"
57
+#include "kemi.h"
57 58
 
58 59
 #ifdef DEBUG_DMALLOC
59 60
 #include <mem/dmalloc.h>
... ...
@@ -125,6 +126,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
125 126
 #endif
126 127
 	str inb;
127 128
 	sr_net_info_t netinfo;
129
+	sr_kemi_eng_t *keng = NULL;
128 130
 
129 131
 	if(sr_event_enabled(SREV_NET_DATA_RECV)) {
130 132
 		if(sip_check_fline(buf, len)==0) {
... ...
@@ -237,9 +239,20 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
237 239
 
238 240
 		set_route_type(REQUEST_ROUTE);
239 241
 		/* exec the routing script */
240
-		if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){
241
-			LM_WARN("error while trying script\n");
242
-			goto error_req;
242
+		if(unlikely(main_rt.rlist[DEFAULT_RT]==NULL)) {
243
+			keng = sr_kemi_eng_get();
244
+			if(keng==NULL) {
245
+				LM_ERR("no config routing engine registered\n");
246
+				goto error_req;
247
+			}
248
+			if(keng->froute(msg, REQUEST_ROUTE, NULL)<0) {
249
+				LM_NOTICE("negative return code from engine function\n");
250
+			}
251
+		} else {
252
+			if (run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0)<0){
253
+				LM_WARN("error while trying script\n");
254
+				goto error_req;
255
+			}
243 256
 		}
244 257
 
245 258
 #ifdef STATS
... ...
@@ -283,7 +296,15 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
283 296
 		/* exec the onreply routing script */
284 297
 		if (onreply_rt.rlist[DEFAULT_RT]){
285 298
 			set_route_type(CORE_ONREPLY_ROUTE);
286
-			ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
299
+			ret = 1;
300
+			if(unlikely(main_rt.rlist[DEFAULT_RT]==NULL)) {
301
+				keng = sr_kemi_eng_get();
302
+				if(keng!=NULL) {
303
+					ret = keng->froute(msg, REQUEST_ROUTE, NULL);
304
+				}
305
+			} else {
306
+				ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
307
+			}
287 308
 #ifndef NO_ONREPLY_ROUTE_ERROR
288 309
 			if (unlikely(ret<0)){
289 310
 				LM_WARN("error while trying onreply script\n");
Browse code

core: receive.c coherent indentation

Daniel-Constantin Mierla authored on 12/04/2016 11:48:22
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-/* 
1
+/*
2 2
  * Copyright (C) 2001-2003 FhG Fokus
3 3
  *
4 4
  * This file is part of Kamailio, a free SIP server.
... ...
@@ -13,15 +13,15 @@
13 13
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
  * GNU General Public License for more details.
15 15
  *
16
- * You should have received a copy of the GNU General Public License 
17
- * along with this program; if not, write to the Free Software 
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 19
  *
20 20
  */
21 21
 
22 22
 /*!
23 23
  * \file
24
- * \brief Kamailio core :: 
24
+ * \brief Kamailio core ::
25 25
  * \ingroup core
26 26
  * Module: \ref core
27 27
  */
... ...
@@ -109,17 +109,17 @@ int sip_check_fline(char* buf, unsigned int len)
109 109
 }
110 110
 
111 111
 /** Receive message
112
- *  WARNING: buf must be 0 terminated (buf[len]=0) or some things might 
112
+ *  WARNING: buf must be 0 terminated (buf[len]=0) or some things might
113 113
  * break (e.g.: modules/textops)
114 114
  */
115
-int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) 
115
+int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
116 116
 {
117 117
 	struct sip_msg* msg;
118 118
 	struct run_act_ctx ctx;
119 119
 	int ret;
120 120
 #ifdef STATS
121 121
 	int skipped = 1;
122
-	struct timeval tvb, tve;	
122
+	struct timeval tvb, tve;
123 123
 	struct timezone tz;
124 124
 	unsigned int diff;
125 125
 #endif
... ...
@@ -155,14 +155,14 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
155 155
 	msg->buf=buf;
156 156
 	msg->len=len;
157 157
 	/* zero termination (termination of orig message bellow not that
158
-	   useful as most of the work is done with scratch-pad; -jiri  */
158
+	 * useful as most of the work is done with scratch-pad; -jiri  */
159 159
 	/* buf[len]=0; */ /* WARNING: zero term removed! */
160 160
 	msg->rcv=*rcv_info;
161 161
 	msg->id=msg_no;
162 162
 	msg->pid=my_pid();
163 163
 	msg->set_global_address=default_global_address;
164 164
 	msg->set_global_port=default_global_port;
165
-	
165
+
166 166
 	if(likely(sr_msg_time==1)) msg_set_time(msg);
167 167
 
168 168
 	if (parse_msg(buf,len, msg)!=0){
... ...
@@ -202,7 +202,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
202 202
 		/* check if necessary to add receive?->moved to forward_req */
203 203
 		/* check for the alias stuff */
204 204
 #ifdef USE_TCP
205
-		if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) && 
205
+		if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) &&
206 206
 				(((rcv_info->proto==PROTO_TCP) && !tcp_disable)
207 207
 #ifdef USE_TLS
208 208
 					|| ((rcv_info->proto==PROTO_TLS) && !tls_disable)
... ...
@@ -224,10 +224,10 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
224 224
 #endif
225 225
 		/* execute pre-script callbacks, if any; -jiri */
226 226
 		/* if some of the callbacks said not to continue with
227
-		   script processing, don't do so
228
-		   if we are here basic sanity checks are already done
229
-		   (like presence of at least one via), so you can count
230
-		   on via1 being parsed in a pre-script callback --andrei
227
+		 * script processing, don't do so
228
+		 * if we are here basic sanity checks are already done
229
+		 * (like presence of at least one via), so you can count
230
+		 * on via1 being parsed in a pre-script callback --andrei
231 231
 		*/
232 232
 		if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
233 233
 		{
... ...
@@ -266,13 +266,13 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
266 266
 		gettimeofday( & tvb, &tz );
267 267
 		STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
268 268
 #endif
269
-		
269
+
270 270
 		/* execute pre-script callbacks, if any; -jiri */
271 271
 		/* if some of the callbacks said not to continue with
272
-		   script processing, don't do so
273
-		   if we are here basic sanity checks are already done
274
-		   (like presence of at least one via), so you can count
275
-		   on via1 being parsed in a pre-script callback --andrei
272
+		 * script processing, don't do so
273
+		 * if we are here basic sanity checks are already done
274
+		 * (like presence of at least one via), so you can count
275
+		 * on via1 being parsed in a pre-script callback --andrei
276 276
 		*/
277 277
 		if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
278 278
 		{
Browse code

core: earlier execution of SREV_NET_DATA_RECV event

- allow capturing the traffic before topoh decodes it

Daniel-Constantin Mierla authored on 12/01/2016 13:09:54
Showing 1 changed files
... ...
@@ -74,6 +74,39 @@ unsigned int inc_msg_no(void)
74 74
 	return ++msg_no;
75 75
 }
76 76
 
77
+/**
78
+ *
79
+ */
80
+int sip_check_fline(char* buf, unsigned int len)
81
+{
82
+	char *p;
83
+	int m;
84
+
85
+	m = 0;
86
+	for(p=buf; p<buf+len; p++) {
87
+		/* first check if is a reply - starts with SIP/2.0 */
88
+		if(m==0) {
89
+			if(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n') continue;
90
+			if(buf+len-p<10) return -1;
91
+			if(strncmp(p, "SIP/2.0 ", 8)==0) {
92
+				LM_DBG("first line indicates a SIP reply\n");
93
+				return 0;
94
+			}
95
+			m=1;
96
+		} else {
97
+			/* check if a request - before end of first line is SIP/2.0 */
98
+			if(*p!='\r' && *p!='\n') continue;
99
+			if(p-10>=buf) {
100
+				if(strncmp(p-8, " SIP/2.0", 8)==0) {
101
+					LM_DBG("first line indicates a SIP request\n");
102
+					return 0;
103
+				}
104
+			}
105
+			return -1;
106
+		}
107
+	}
108
+	return -1;
109
+}
77 110
 
78 111
 /** Receive message
79 112
  *  WARNING: buf must be 0 terminated (buf[len]=0) or some things might 
... ...
@@ -93,6 +126,16 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
93 126
 	str inb;
94 127
 	sr_net_info_t netinfo;
95 128
 
129
+	if(sr_event_enabled(SREV_NET_DATA_RECV)) {
130
+		if(sip_check_fline(buf, len)==0) {
131
+			memset(&netinfo, 0, sizeof(sr_net_info_t));
132
+			netinfo.data.s = buf;
133
+			netinfo.data.len = len;
134
+			netinfo.rcv = rcv_info;
135
+			sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo);
136
+		}
137
+	}
138
+
96 139
 	inb.s = buf;
97 140
 	inb.len = len;
98 141
 	sr_event_exec(SREV_NET_DATA_IN, (void*)&inb);
... ...
@@ -140,14 +183,6 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
140 183
 	/* ... clear branches from previous message */
141 184
 	clear_branches();
142 185
 
143
-	if(sr_event_enabled(SREV_NET_DATA_RECV)) {
144
-		memset(&netinfo, 0, sizeof(sr_net_info_t));
145
-		netinfo.data.s = msg->buf;
146
-		netinfo.data.len = msg->len;
147
-		netinfo.rcv = rcv_info;
148
-		sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo);
149
-	}
150
-
151 186
 	if (msg->first_line.type==SIP_REQUEST){
152 187
 		ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
153 188
 		if (!IS_SIP(msg)){
Browse code

core: execute SREV_NET_DATA_RECV event

Daniel-Constantin Mierla authored on 11/01/2016 21:23:26
Showing 1 changed files
... ...
@@ -91,6 +91,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
91 91
 	unsigned int diff;
92 92
 #endif
93 93
 	str inb;
94
+	sr_net_info_t netinfo;
94 95
 
95 96
 	inb.s = buf;
96 97
 	inb.len = len;
... ...
@@ -139,6 +140,14 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
139 140
 	/* ... clear branches from previous message */
140 141
 	clear_branches();
141 142
 
143
+	if(sr_event_enabled(SREV_NET_DATA_RECV)) {
144
+		memset(&netinfo, 0, sizeof(sr_net_info_t));
145
+		netinfo.data.s = msg->buf;
146
+		netinfo.data.len = msg->len;
147
+		netinfo.rcv = rcv_info;
148
+		sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo);
149
+	}
150
+
142 151
 	if (msg->first_line.type==SIP_REQUEST){
143 152
 		ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
144 153
 		if (!IS_SIP(msg)){
Browse code

receive.c: logging: DBG -> LM_DBG

Ovidiu Sas authored on 13/01/2015 16:45:39
Showing 1 changed files
... ...
@@ -131,7 +131,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
131 131
 		}
132 132
 		goto error02;
133 133
 	}
134
-	DBG("After parse_msg...\n");
134
+	LM_DBG("After parse_msg...\n");
135 135
 
136 136
 	/* set log prefix */
137 137
 	log_prefix_set(msg);
... ...
@@ -174,7 +174,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
174 174
 #endif
175 175
 
176 176
 	/*	skip: */
177
-		DBG("preparing to run routing scripts...\n");
177
+		LM_DBG("preparing to run routing scripts...\n");
178 178
 #ifdef  STATS
179 179
 		gettimeofday( & tvb, &tz );
180 180
 #endif
... ...
@@ -203,7 +203,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
203 203
 		diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
204 204
 		stats->processed_requests++;
205 205
 		stats->acc_req_time += diff;
206
-		DBG("successfully ran routing scripts...(%d usec)\n", diff);
206
+		LM_DBG("successfully ran routing scripts...(%d usec)\n", diff);
207 207
 		STATS_RX_REQUEST( msg->first_line.u.request.method_value );
208 208
 #endif
209 209
 
... ...
@@ -259,7 +259,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
259 259
 		diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
260 260
 		stats->processed_responses++;
261 261
 		stats->acc_res_time+=diff;
262
-		DBG("successfully ran reply processing...(%d usec)\n", diff);
262
+		LM_DBG("successfully ran reply processing...(%d usec)\n", diff);
263 263
 #endif
264 264
 
265 265
 		/* execute post reply-script callbacks */
... ...
@@ -275,7 +275,7 @@ end:
275 275
 #ifdef WITH_XAVP
276 276
 	xavp_reset_list();
277 277
 #endif
278
-	DBG("receive_msg: cleaning up\n");
278
+	LM_DBG("cleaning up\n");
279 279
 	free_sip_msg(msg);
280 280
 	pkg_free(msg);
281 281
 #ifdef STATS
... ...
@@ -296,7 +296,7 @@ error_rpl:
296 296
 	goto error02;
297 297
 #endif /* NO_ONREPLY_ROUTE_ERROR */
298 298
 error_req:
299
-	DBG("receive_msg: error:...\n");