Merge pull request #1054 from jchavanton/dmq_usrloc_multi_sync
[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
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_ping_from = str_init("sip:dispatcher@localhost");
60         str ka_outbound_proxy = {0, 0};
61         uac_req_t uac_r;
62
63         LM_DBG("ka check timer\n");
64
65         for(ka_dest = ka_destinations_list->first; ka_dest != NULL;
66                         ka_dest = ka_dest->next) {
67                 LM_DBG("ka_check_timer dest:%.*s\n", ka_dest->uri.len, ka_dest->uri.s);
68
69                 /* Send ping using TM-Module.
70                  * int request(str* m, str* ruri, str* to, str* from, str* h,
71                  *              str* b, str *oburi,
72                  *              transaction_cb cb, void* cbp); */
73                 set_uac_req(&uac_r, &ka_ping_method, 0, 0, 0, TMCB_LOCAL_COMPLETED,
74                                 ka_options_callback, (void *)ka_dest);
75
76                 if(tmb.t_request(&uac_r, &ka_dest->uri, &ka_dest->uri, &ka_ping_from,
77                                    &ka_outbound_proxy)
78                                 < 0) {
79                         LM_ERR("unable to ping [%.*s]\n", ka_dest->uri.len, ka_dest->uri.s);
80                 }
81
82                 ka_dest->last_checked = time(NULL);
83         }
84
85         return;
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         } else {
116                 state = KA_STATE_DOWN;
117                 ka_dest->last_up = time(NULL);
118         }
119
120         LM_DBG("new state is: %d\n", state);
121         if(state != ka_dest->state) {
122                 ka_run_route(msg, &uri, state_routes[state]);
123
124                 if(ka_dest->statechanged_clb != NULL) {
125                         ka_dest->statechanged_clb(ka_dest->uri, state, ka_dest->user_attr);
126                 }
127
128                 ka_dest->state = state;
129         }
130 }
131
132 /*
133  * Execute kamailio script event routes
134  *
135  */
136 static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
137 {
138         int rt, backup_rt;
139         struct run_act_ctx ctx;
140         sip_msg_t *fmsg;
141
142         if(route == NULL) {
143                 LM_ERR("bad route\n");
144                 return;
145         }
146
147         LM_DBG("ka_run_route event_route[%s]\n", route);
148
149         rt = route_get(&event_rt, route);
150         if(rt < 0 || event_rt.rlist[rt] == NULL) {
151                 LM_DBG("route *%s* does not exist", route);
152                 return;
153         }
154
155         fmsg = msg;
156         if(fmsg == NULL) {
157                 if(faked_msg_init() < 0) {
158                         LM_ERR("faked_msg_init() failed\n");
159                         return;
160                 }
161                 fmsg = faked_msg_next();
162                 fmsg->parsed_orig_ruri_ok = 0;
163                 fmsg->new_uri = *uri;
164         }
165
166         backup_rt = get_route_type();
167         set_route_type(REQUEST_ROUTE);
168         init_run_actions_ctx(&ctx);
169         run_top_route(event_rt.rlist[rt], fmsg, 0);
170         set_route_type(backup_rt);
171 }
172
173
174 /*
175  * copy str into dynamically allocated shm memory
176  */
177 int ka_str_copy(str src, str *dest, char *prefix)
178 {
179         int lp = prefix ? strlen(prefix) : 0;
180
181         dest->s = (char *)shm_malloc((src.len + 1 + lp) * sizeof(char));
182         if(dest->s == NULL) {
183                 LM_ERR("no more memory!\n");
184                 return -1;
185         }
186
187         if(prefix)
188                 strncpy(dest->s, prefix, lp);
189         strncpy(dest->s + lp, src.s, src.len);
190         dest->s[src.len + lp] = '\0';
191         dest->len = src.len + lp;
192
193         return 0;
194 }