keepalive: ignore delete_counter if it is set to 0
[kamailio] / src / modules / keepalive / keepalive_core.c
1 /**
2  * keepalive module - remote destinations probing
3  *
4  * Copyright (C) 2017 Guillaume Bour <guillaume@bour.cc>
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 /*! \file
24  * \ingroup keepalive
25  * \brief Keepalive :: Send keepalives
26  */
27
28 /*! \defgroup keepalive Keepalive :: Probing remote gateways by sending keepalives
29  */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include "../../core/fmsg.h"
38 #include "../tm/tm_load.h"
39
40 #include "keepalive.h"
41 #include "api.h"
42
43 struct tm_binds tmb;
44
45 static void ka_run_route(sip_msg_t *msg, str *uri, char *route);
46 static void ka_options_callback(struct cell *t, int type,
47                 struct tmcb_params *ps);
48
49 extern str ka_ping_from;
50 /*! \brief
51  * Timer for checking probing destinations
52  *
53  * This timer is regularly fired.
54  */
55 void ka_check_timer(unsigned int ticks, void *param)
56 {
57         ka_dest_t *ka_dest;
58         str ka_ping_method = str_init("OPTIONS");
59         str ka_outbound_proxy = {0, 0};
60         uac_req_t uac_r;
61
62         LM_DBG("ka check timer\n");
63
64         ka_lock_destination_list();
65
66         for(ka_dest = ka_destinations_list->first; ka_dest != NULL;
67                         ka_dest = ka_dest->next) {
68                 LM_DBG("ka_check_timer dest:%.*s\n", ka_dest->uri.len, ka_dest->uri.s);
69
70                 if(ka_counter_del > 0 && ka_dest->counter > ka_counter_del) {
71                         continue;
72                 }
73
74                 /* Send ping using TM-Module.
75                  * int request(str* m, str* ruri, str* to, str* from, str* h,
76                  *              str* b, str *oburi,
77                  *              transaction_cb cb, void* cbp); */
78                 set_uac_req(&uac_r, &ka_ping_method, 0, 0, 0, TMCB_LOCAL_COMPLETED,
79                                 ka_options_callback, (void *)ka_dest);
80
81                 if(tmb.t_request(&uac_r, &ka_dest->uri, &ka_dest->uri, &ka_ping_from,
82                                    &ka_outbound_proxy)
83                                 < 0) {
84                         LM_ERR("unable to ping [%.*s]\n", ka_dest->uri.len, ka_dest->uri.s);
85                 }
86
87                 ka_dest->last_checked = time(NULL);
88         }
89         ka_unlock_destination_list();
90
91         return;
92 }
93
94 /*! \brief
95  * Callback-Function for the OPTIONS-Request
96  * This Function is called, as soon as the Transaction is finished
97  * (e. g. a Response came in, the timeout was hit, ...)
98  */
99 static void ka_options_callback(
100                 struct cell *t, int type, struct tmcb_params *ps)
101 {
102         str uri = {0, 0};
103         sip_msg_t *msg = NULL;
104         ka_state state;
105
106         char *state_routes[] = {"", "keepalive:dst-up", "keepalive:dst-down"};
107
108         //NOTE: how to be sure destination is still allocated ?
109         ka_dest_t *ka_dest = (ka_dest_t *)(*ps->param);
110
111         uri.s = t->to.s + 5;
112         uri.len = t->to.len - 8;
113         LM_DBG("OPTIONS-Request was finished with code %d (to %.*s)\n", ps->code,
114                         ka_dest->uri.len, ka_dest->uri.s); //uri.len, uri.s);
115
116
117         // accepting 2XX return codes
118         if(ps->code >= 200 && ps->code <= 299) {
119                 state = KA_STATE_UP;
120                 ka_dest->last_down = time(NULL);
121                 ka_dest->counter=0;
122         } else {
123                 state = KA_STATE_DOWN;
124                 ka_dest->last_up = time(NULL);
125                 ka_dest->counter++;
126         }
127
128         LM_DBG("new state is: %d\n", state);
129         if(state != ka_dest->state) {
130                 ka_run_route(msg, &uri, state_routes[state]);
131
132                 if(ka_dest->statechanged_clb != NULL) {
133                         ka_dest->statechanged_clb(&ka_dest->uri, state, ka_dest->user_attr);
134                 }
135
136                 ka_dest->state = state;
137         }
138 }
139
140 /*
141  * Execute kamailio script event routes
142  *
143  */
144 static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
145 {
146         int rt, backup_rt;
147         struct run_act_ctx ctx;
148         sip_msg_t *fmsg;
149
150         if(route == NULL) {
151                 LM_ERR("bad route\n");
152                 return;
153         }
154
155         LM_DBG("ka_run_route event_route[%s]\n", route);
156
157         rt = route_get(&event_rt, route);
158         if(rt < 0 || event_rt.rlist[rt] == NULL) {
159                 LM_DBG("route *%s* does not exist", route);
160                 return;
161         }
162
163         fmsg = msg;
164         if(fmsg == NULL) {
165                 if(faked_msg_init() < 0) {
166                         LM_ERR("faked_msg_init() failed\n");
167                         return;
168                 }
169                 fmsg = faked_msg_next();
170                 fmsg->parsed_orig_ruri_ok = 0;
171                 fmsg->new_uri = *uri;
172         }
173
174         backup_rt = get_route_type();
175         set_route_type(REQUEST_ROUTE);
176         init_run_actions_ctx(&ctx);
177         run_top_route(event_rt.rlist[rt], fmsg, 0);
178         set_route_type(backup_rt);
179 }
180
181
182 /*
183  * copy str into dynamically allocated shm memory
184  */
185 int ka_str_copy(str *src, str *dest, char *prefix)
186 {
187         int lp = prefix ? strlen(prefix) : 0;
188
189         dest->s = (char *)shm_malloc((src->len + 1 + lp) * sizeof(char));
190         if(dest->s == NULL) {
191                 LM_ERR("no more memory!\n");
192                 return -1;
193         }
194
195         if(prefix)
196                 strncpy(dest->s, prefix, lp);
197         strncpy(dest->s + lp, src->s, src->len);
198         dest->s[src->len + lp] = '\0';
199         dest->len = src->len + lp;
200
201         return 0;
202 }