Coverage Report

Created: 2024-02-25 06:34

/src/kamailio/src/core/onsend.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2005 iptelorg GmbH
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
 * \file
22
 * \brief Kamailio core :: IP address handling
23
 * \author andrei
24
 * \ingroup core
25
 * Module: \ref core
26
 */
27
28
29
#include "onsend.h"
30
31
onsend_info_t *p_onsend = 0; /* onsend route send info */
32
33
34
/*
35
 * returns: 0 drop the message, >= ok, <0 error (but forward the message)
36
 * it also migh change dst->send_flags!
37
 * WARNING: buf must be 0 terminated (to allow regex matches on it) */
38
int run_onsend(sip_msg_t *orig_msg, dest_info_t *dst, char *buf, int len)
39
0
{
40
0
  onsend_info_t onsnd_info = {0};
41
0
  int ret;
42
0
  run_act_ctx_t ra_ctx;
43
0
  run_act_ctx_t *bctx;
44
0
  int backup_route_type;
45
0
  snd_flags_t fwd_snd_flags_bak;
46
0
  snd_flags_t rpl_snd_flags_bak;
47
0
  sr_kemi_eng_t *keng = NULL;
48
49
0
  if(orig_msg == NULL || dst == NULL || buf == NULL) {
50
0
    LM_DBG("required parameters are not available - ignoring\n");
51
0
    return 1;
52
0
  }
53
0
  ret = 1;
54
  // do if onsend_route{} or cfgengine exists
55
0
  if(kemi_onsend_route_callback.len > 0) {
56
0
    keng = sr_kemi_eng_get();
57
0
  }
58
0
  if(onsend_rt.rlist[DEFAULT_RT] || keng) {
59
0
    onsnd_info.to = &dst->to;
60
0
    onsnd_info.send_sock = dst->send_sock;
61
0
    onsnd_info.buf = buf;
62
0
    onsnd_info.len = len;
63
0
    onsnd_info.msg = orig_msg;
64
0
    p_onsend = &onsnd_info;
65
0
    backup_route_type = get_route_type();
66
0
    set_route_type(ONSEND_ROUTE);
67
0
    if(exec_pre_script_cb(orig_msg, ONSEND_CB_TYPE) > 0) {
68
      /* backup orig_msg send flags */
69
0
      fwd_snd_flags_bak = orig_msg->fwd_send_flags;
70
0
      rpl_snd_flags_bak = orig_msg->rpl_send_flags;
71
0
      orig_msg->fwd_send_flags = dst->send_flags; /* initial value */
72
0
      init_run_actions_ctx(&ra_ctx);
73
74
0
      if(keng) {
75
0
        bctx = sr_kemi_act_ctx_get();
76
0
        sr_kemi_act_ctx_set(&ra_ctx);
77
0
        ret = sr_kemi_route(keng, orig_msg, ONSEND_ROUTE, NULL, NULL);
78
0
        sr_kemi_act_ctx_set(bctx);
79
0
      } else {
80
0
        ret = run_actions(
81
0
            &ra_ctx, onsend_rt.rlist[DEFAULT_RT], orig_msg);
82
0
      }
83
84
      /* update dst send_flags */
85
0
      dst->send_flags = orig_msg->fwd_send_flags;
86
      /* restore orig_msg flags */
87
0
      orig_msg->fwd_send_flags = fwd_snd_flags_bak;
88
0
      orig_msg->rpl_send_flags = rpl_snd_flags_bak;
89
0
      exec_post_script_cb(orig_msg, ONSEND_CB_TYPE);
90
      /* - handle drop action with flag DROP_R_F
91
       * KEMI case (python,lua,jsdt)
92
       * ret = 1 always
93
       * Native case:
94
       * ret = 1 success
95
       * ret = 0 drop the message
96
       */
97
0
      if(ra_ctx.run_flags & DROP_R_F) {
98
0
        ret = 0;
99
0
      } else if(ret == 0) {
100
        /* native case where run_actions return 0
101
         * but DROP_R_F is not set */
102
0
        ret = 1;
103
0
      }
104
0
    } else {
105
0
      ret = 0; /* drop the message */
106
0
    }
107
0
    set_route_type(backup_route_type);
108
0
    p_onsend = 0; /* reset it */
109
0
  }
110
0
  return ret;
111
0
}
112
113
/**
114
 * return: 0 drop the message
115
 *    >= ok
116
 *    <0 error (but forward the message)
117
 * it also migh change sndinfo dst->send_flags!
118
 * WARNING: sndinfo buf must be 0 terminated (to allow regex matches on it)
119
 */
120
int run_onsend_evroute(onsend_info_t *sndinfo, int evrt, str *evcb, str *evname)
121
0
{
122
0
  int ret;
123
0
  run_act_ctx_t ra_ctx;
124
0
  run_act_ctx_t *bctx;
125
0
  int backup_route_type;
126
0
  snd_flags_t fwd_snd_flags_bak;
127
0
  snd_flags_t rpl_snd_flags_bak;
128
0
  sr_kemi_eng_t *keng = NULL;
129
130
0
  if(sndinfo == NULL || sndinfo->dst == NULL || sndinfo->msg == NULL
131
0
      || sndinfo->buf == NULL) {
132
0
    LM_DBG("required parameters are not available - ignoring\n");
133
0
    return 1;
134
0
  }
135
0
  ret = 1;
136
  // do if onsend_route{} or cfgengine exists
137
0
  if(evcb != NULL && evcb->len > 0) {
138
0
    keng = sr_kemi_eng_get();
139
0
  }
140
0
  if(evrt < 0 && keng == NULL) {
141
0
    return 1;
142
0
  }
143
144
0
  p_onsend = sndinfo;
145
0
  backup_route_type = get_route_type();
146
0
  set_route_type(EVENT_ROUTE);
147
  /* backup orig_msg send flags */
148
0
  fwd_snd_flags_bak = sndinfo->msg->fwd_send_flags;
149
0
  rpl_snd_flags_bak = sndinfo->msg->rpl_send_flags;
150
0
  sndinfo->msg->fwd_send_flags = sndinfo->dst->send_flags; /* initial value */
151
0
  init_run_actions_ctx(&ra_ctx);
152
153
0
  if(keng) {
154
0
    bctx = sr_kemi_act_ctx_get();
155
0
    sr_kemi_act_ctx_set(&ra_ctx);
156
0
    ret = sr_kemi_route(keng, sndinfo->msg, EVENT_ROUTE, evcb, evname);
157
0
    sr_kemi_act_ctx_set(bctx);
158
0
  } else {
159
0
    ret = run_actions(&ra_ctx, event_rt.rlist[evrt], sndinfo->msg);
160
0
  }
161
162
  /* update dst send_flags */
163
0
  sndinfo->dst->send_flags = sndinfo->msg->fwd_send_flags;
164
  /* restore orig_msg flags */
165
0
  sndinfo->msg->fwd_send_flags = fwd_snd_flags_bak;
166
0
  sndinfo->msg->rpl_send_flags = rpl_snd_flags_bak;
167
0
  if((ret == 0) && !(ra_ctx.run_flags & DROP_R_F)) {
168
0
    ret = 1;
169
0
  }
170
0
  set_route_type(backup_route_type);
171
0
  p_onsend = 0; /* reset it */
172
173
0
  return ret;
174
0
}