Merge pull request #2319 from cruzccl/redis-sentinel-reconnection
[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  * Callback run from timer,  for probing a destination
52  *
53  * This timer is regularly fired.
54  */
55 ticks_t ka_check_timer(ticks_t ticks, struct timer_ln* tl, 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         ka_dest = (ka_dest_t *)param;
63
64     LM_DBG("ka_check_timer dest:%.*s\n", ka_dest->uri.len, ka_dest->uri.s);
65
66     if(ka_counter_del > 0 && ka_dest->counter > ka_counter_del) {
67         return (ticks_t)(0); /* stops the timer */
68     }
69
70     /* Send ping using TM-Module.
71      * int request(str* m, str* ruri, str* to, str* from, str* h,
72      *          str* b, str *oburi,
73      *          transaction_cb cb, void* cbp); */
74     set_uac_req(&uac_r, &ka_ping_method, 0, 0, 0, TMCB_LOCAL_COMPLETED,
75             ka_options_callback, (void *)ka_dest);
76
77     if(tmb.t_request(&uac_r, &ka_dest->uri, &ka_dest->uri, &ka_ping_from,
78                &ka_outbound_proxy)
79             < 0) {
80         LM_ERR("unable to ping [%.*s]\n", ka_dest->uri.len, ka_dest->uri.s);
81     }
82
83     ka_dest->last_checked = time(NULL);
84
85         return (ticks_t)(-1); /* periodical */
86 }
87
88 /*! \brief
89  * Callback-Function for the OPTIONS-Request
90  * This Function is called, as soon as the Transaction is finished
91  * (e. g. a Response came in, the timeout was hit, ...)
92  */
93 static void ka_options_callback(
94                 struct cell *t, int type, struct tmcb_params *ps)
95 {
96         str uri = {0, 0};
97         sip_msg_t *msg = NULL;
98         ka_state state;
99
100         char *state_routes[] = {"", "keepalive:dst-up", "keepalive:dst-down"};
101
102         //NOTE: how to be sure destination is still allocated ?
103         ka_dest_t *ka_dest = (ka_dest_t *)(*ps->param);
104
105         uri.s = t->to.s + 5;
106         uri.len = t->to.len - 8;
107         LM_DBG("OPTIONS-Request was finished with code %d (to %.*s)\n", ps->code,
108                         ka_dest->uri.len, ka_dest->uri.s); //uri.len, uri.s);
109
110
111         // accepting 2XX return codes
112         if(ps->code >= 200 && ps->code <= 299) {
113                 state = KA_STATE_UP;
114                 ka_dest->last_down = time(NULL);
115                 ka_dest->counter=0;
116         } else {
117                 state = KA_STATE_DOWN;
118                 ka_dest->last_up = time(NULL);
119                 ka_dest->counter++;
120         }
121
122         LM_DBG("new state is: %d\n", state);
123         if(state != ka_dest->state) {
124                 ka_run_route(msg, &uri, state_routes[state]);
125
126                 if(ka_dest->statechanged_clb != NULL) {
127                         ka_dest->statechanged_clb(&ka_dest->uri, state, ka_dest->user_attr);
128                 }
129
130                 ka_dest->state = state;
131         }
132         if(ka_dest->response_clb != NULL) {
133                 ka_dest->response_clb(&ka_dest->uri, ps, ka_dest->user_attr);
134         }
135 }
136
137 /*
138  * Execute kamailio script event routes
139  *
140  */
141 static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
142 {
143         int rt, backup_rt;
144         struct run_act_ctx ctx;
145         sip_msg_t *fmsg;
146
147         if(route == NULL) {
148                 LM_ERR("bad route\n");
149                 return;
150         }
151
152         LM_DBG("ka_run_route event_route[%s]\n", route);
153
154         rt = route_get(&event_rt, route);
155         if(rt < 0 || event_rt.rlist[rt] == NULL) {
156                 LM_DBG("route *%s* does not exist", route);
157                 return;
158         }
159
160         fmsg = msg;
161         if(fmsg == NULL) {
162                 if(faked_msg_init() < 0) {
163                         LM_ERR("faked_msg_init() failed\n");
164                         return;
165                 }
166                 fmsg = faked_msg_next();
167                 fmsg->parsed_orig_ruri_ok = 0;
168                 fmsg->new_uri = *uri;
169         }
170
171         backup_rt = get_route_type();
172         set_route_type(REQUEST_ROUTE);
173         init_run_actions_ctx(&ctx);
174         run_top_route(event_rt.rlist[rt], fmsg, 0);
175         set_route_type(backup_rt);
176 }
177
178
179 /*
180  * copy str into dynamically allocated shm memory
181  */
182 int ka_str_copy(str *src, str *dest, char *prefix)
183 {
184         int lp = prefix ? strlen(prefix) : 0;
185
186         dest->s = (char *)shm_malloc((src->len + 1 + lp) * sizeof(char));
187         if(dest->s == NULL) {
188                 LM_ERR("no more memory!\n");
189                 return -1;
190         }
191
192         if(prefix)
193                 strncpy(dest->s, prefix, lp);
194         strncpy(dest->s + lp, src->s, src->len);
195         dest->s[src->len + lp] = '\0';
196         dest->len = src->len + lp;
197
198         return 0;
199 }