2 * keepalive module - remote destinations probing
4 * Copyright (C) 2017 Guillaume Bour <guillaume@bour.cc>
6 * This file is part of Kamailio, a free SIP server.
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
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.
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
25 * \brief Keepalive :: Send keepalives
28 /*! \defgroup keepalive Keepalive :: Probing remote gateways by sending keepalives
34 #include <sys/types.h>
37 #include "../../core/fmsg.h"
38 #include "../tm/tm_load.h"
40 #include "keepalive.h"
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);
49 extern str ka_ping_from;
51 * Callback run from timer, for probing a destination
53 * This timer is regularly fired.
55 ticks_t ka_check_timer(ticks_t ticks, struct timer_ln* tl, void* param)
58 str ka_ping_method = str_init("OPTIONS");
59 str ka_outbound_proxy = {0, 0};
62 ka_dest = (ka_dest_t *)param;
64 LM_DBG("dest: %.*s\n", ka_dest->uri.len, ka_dest->uri.s);
66 if(ka_counter_del > 0 && ka_dest->counter > ka_counter_del) {
67 return (ticks_t)(0); /* stops the timer */
70 /* Send ping using TM-Module.
71 * int request(str* m, str* ruri, str* to, str* from, str* h,
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);
77 if(tmb.t_request(&uac_r, &ka_dest->uri, &ka_dest->uri, &ka_ping_from,
80 LM_ERR("unable to ping [%.*s]\n", ka_dest->uri.len, ka_dest->uri.s);
83 ka_dest->last_checked = time(NULL);
85 return ka_dest->ping_interval; /* periodical, but based on dest->ping_interval, not on initial_timeout */
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, ...)
93 static void ka_options_callback(
94 struct cell *t, int type, struct tmcb_params *ps)
97 sip_msg_t *msg = NULL;
100 char *state_routes[] = {"", "keepalive:dst-up", "keepalive:dst-down"};
102 //NOTE: how to be sure destination is still allocated ?
103 ka_dest_t *ka_dest = (ka_dest_t *)(*ps->param);
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);
111 // accepting 2XX return codes
112 if(ps->code >= 200 && ps->code <= 299) {
114 ka_dest->last_down = time(NULL);
117 state = KA_STATE_DOWN;
118 ka_dest->last_up = time(NULL);
122 if(state != ka_dest->state) {
123 ka_run_route(msg, &uri, state_routes[state]);
125 if(ka_dest->statechanged_clb != NULL) {
126 ka_dest->statechanged_clb(&ka_dest->uri, state, ka_dest->user_attr);
129 LM_DBG("new state is: %d\n", state);
130 ka_dest->state = state;
132 if(ka_dest->response_clb != NULL) {
133 ka_dest->response_clb(&ka_dest->uri, ps, ka_dest->user_attr);
138 * Execute kamailio script event routes
141 static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
144 struct run_act_ctx ctx;
148 LM_ERR("bad route\n");
152 LM_DBG("run event_route[%s]\n", route);
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);
162 if(faked_msg_init() < 0) {
163 LM_ERR("faked_msg_init() failed\n");
166 fmsg = faked_msg_next();
167 fmsg->parsed_orig_ruri_ok = 0;
168 fmsg->new_uri = *uri;
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);
180 * copy str into dynamically allocated shm memory
182 int ka_str_copy(str *src, str *dest, char *prefix)
184 int lp = prefix ? strlen(prefix) : 0;
186 dest->s = (char *)shm_malloc((src->len + 1 + lp) * sizeof(char));
187 if(dest->s == NULL) {
188 LM_ERR("no more memory!\n");
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;