src/modules/keepalive/keepalive_core.c
cf07d3fe
 /**
  * keepalive module - remote destinations probing
  *
  * Copyright (C) 2017 Guillaume Bour <guillaume@bour.cc>
  *
  * This file is part of Kamailio, a free SIP server.
  *
  * Kamailio is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * Kamailio is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 /*! \file
  * \ingroup keepalive
  * \brief Keepalive :: Send keepalives
  */
 
 /*! \defgroup keepalive Keepalive :: Probing remote gateways by sending keepalives
  */
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include "../../core/fmsg.h"
 #include "../tm/tm_load.h"
 
 #include "keepalive.h"
 #include "api.h"
 
 struct tm_binds tmb;
 
 static void ka_run_route(sip_msg_t *msg, str *uri, char *route);
338e5df6
 static void ka_options_callback(struct cell *t, int type,
 		struct tmcb_params *ps);
cf07d3fe
 
e8bd86d7
 extern str ka_ping_from;
cf07d3fe
 /*! \brief
4cebb16b
  * Callback run from timer,  for probing a destination
cf07d3fe
  *
  * This timer is regularly fired.
  */
4cebb16b
 ticks_t ka_check_timer(ticks_t ticks, struct timer_ln* tl, void* param)
cf07d3fe
 {
 	ka_dest_t *ka_dest;
 	str ka_ping_method = str_init("OPTIONS");
 	str ka_outbound_proxy = {0, 0};
 	uac_req_t uac_r;
 
4cebb16b
 	ka_dest = (ka_dest_t *)param;
cf07d3fe
 
17d6a880
     LM_DBG("dest: %.*s\n", ka_dest->uri.len, ka_dest->uri.s);
e8bd86d7
 
4cebb16b
     if(ka_counter_del > 0 && ka_dest->counter > ka_counter_del) {
         return (ticks_t)(0); /* stops the timer */
     }
cf07d3fe
 
9a3a8f15
 	str *uuid = shm_malloc(sizeof(str));
 	ka_str_copy(&(ka_dest->uuid), uuid, NULL);
4cebb16b
     /* Send ping using TM-Module.
      * int request(str* m, str* ruri, str* to, str* from, str* h,
      *		str* b, str *oburi,
      *		transaction_cb cb, void* cbp); */
     set_uac_req(&uac_r, &ka_ping_method, 0, 0, 0, TMCB_LOCAL_COMPLETED,
9a3a8f15
             ka_options_callback, (void *)uuid);
6f52da52
 
4cebb16b
     if(tmb.t_request(&uac_r, &ka_dest->uri, &ka_dest->uri, &ka_ping_from,
                &ka_outbound_proxy)
             < 0) {
         LM_ERR("unable to ping [%.*s]\n", ka_dest->uri.len, ka_dest->uri.s);
     }
338e5df6
 
4cebb16b
     ka_dest->last_checked = time(NULL);
cf07d3fe
 
f6b5f782
 	return ka_dest->ping_interval; /* periodical, but based on dest->ping_interval, not on initial_timeout */
cf07d3fe
 }
 
 /*! \brief
  * Callback-Function for the OPTIONS-Request
  * This Function is called, as soon as the Transaction is finished
  * (e. g. a Response came in, the timeout was hit, ...)
  */
338e5df6
 static void ka_options_callback(
 		struct cell *t, int type, struct tmcb_params *ps)
cf07d3fe
 {
 	str uri = {0, 0};
 	sip_msg_t *msg = NULL;
 	ka_state state;
 
 	char *state_routes[] = {"", "keepalive:dst-up", "keepalive:dst-down"};
 
9a3a8f15
 	str *uuid = (str *)(*ps->param);
 
 	// Retrieve ka_dest by uuid from destination list
 	ka_dest_t *ka_dest=0,*hollow=0;
 	if (!ka_find_destination_by_uuid(*uuid, &ka_dest, &hollow)) {
 		LM_ERR("Couldn't find destination \r\n");
 		return;
 	}
 	shm_free(uuid);
cf07d3fe
 
338e5df6
 	uri.s = t->to.s + 5;
cf07d3fe
 	uri.len = t->to.len - 8;
17d6a880
 	LM_DBG("OPTIONS request was finished with code %d (to %.*s)\n", ps->code,
338e5df6
 			ka_dest->uri.len, ka_dest->uri.s); //uri.len, uri.s);
cf07d3fe
 
 
 	// accepting 2XX return codes
338e5df6
 	if(ps->code >= 200 && ps->code <= 299) {
 		state = KA_STATE_UP;
cf07d3fe
 		ka_dest->last_down = time(NULL);
e8bd86d7
 		ka_dest->counter=0;
cf07d3fe
 	} else {
338e5df6
 		state = KA_STATE_DOWN;
 		ka_dest->last_up = time(NULL);
e8bd86d7
 		ka_dest->counter++;
cf07d3fe
 	}
 
338e5df6
 	if(state != ka_dest->state) {
cf07d3fe
 		ka_run_route(msg, &uri, state_routes[state]);
 
 		if(ka_dest->statechanged_clb != NULL) {
211dc1be
 			ka_dest->statechanged_clb(&ka_dest->uri, state, ka_dest->user_attr);
cf07d3fe
 		}
 
5649a249
 		LM_DBG("new state is: %d\n", state);
cf07d3fe
 		ka_dest->state = state;
 	}
9700a8da
 	if(ka_dest->response_clb != NULL) {
 		ka_dest->response_clb(&ka_dest->uri, ps, ka_dest->user_attr);
 	}
cf07d3fe
 }
 
 /*
  * Execute kamailio script event routes
  *
  */
 static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
 {
 	int rt, backup_rt;
 	struct run_act_ctx ctx;
 	sip_msg_t *fmsg;
 
338e5df6
 	if(route == NULL) {
cf07d3fe
 		LM_ERR("bad route\n");
 		return;
 	}
 
17d6a880
 	LM_DBG("run event_route[%s]\n", route);
cf07d3fe
 
 	rt = route_get(&event_rt, route);
338e5df6
 	if(rt < 0 || event_rt.rlist[rt] == NULL) {
cf07d3fe
 		LM_DBG("route *%s* does not exist", route);
 		return;
 	}
 
 	fmsg = msg;
338e5df6
 	if(fmsg == NULL) {
 		if(faked_msg_init() < 0) {
cf07d3fe
 			LM_ERR("faked_msg_init() failed\n");
 			return;
 		}
 		fmsg = faked_msg_next();
 		fmsg->parsed_orig_ruri_ok = 0;
 		fmsg->new_uri = *uri;
 	}
 
 	backup_rt = get_route_type();
 	set_route_type(REQUEST_ROUTE);
 	init_run_actions_ctx(&ctx);
 	run_top_route(event_rt.rlist[rt], fmsg, 0);
 	set_route_type(backup_rt);
 }
 
 
 /*
  * copy str into dynamically allocated shm memory
  */
211dc1be
 int ka_str_copy(str *src, str *dest, char *prefix)
338e5df6
 {
 	int lp = prefix ? strlen(prefix) : 0;
cf07d3fe
 
211dc1be
 	dest->s = (char *)shm_malloc((src->len + 1 + lp) * sizeof(char));
338e5df6
 	if(dest->s == NULL) {
cf07d3fe
 		LM_ERR("no more memory!\n");
 		return -1;
 	}
 
 	if(prefix)
 		strncpy(dest->s, prefix, lp);
211dc1be
 	strncpy(dest->s + lp, src->s, src->len);
 	dest->s[src->len + lp] = '\0';
 	dest->len = src->len + lp;
cf07d3fe
 
 	return 0;
 }