2 * Usrloc module - keepalive
4 * Copyright (C) 2020 Asipto.com
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
30 #include "../../core/dprint.h"
31 #include "../../core/ut.h"
32 #include "../../core/resolve.h"
33 #include "../../core/forward.h"
34 #include "../../core/globals.h"
35 #include "../../core/pvar.h"
36 #include "../../core/parser/parse_uri.h"
37 #include "../../core/parser/parse_from.h"
38 #include "../../core/parser/parse_to.h"
39 #include "../../core/parser/parse_rr.h"
41 #include "ul_keepalive.h"
43 static int ul_ka_send(str *kamsg, dest_info_t *kadst);
47 _KAMETHOD_ _URI_ SIP/2.0\r\n
48 Via: SIP/2.0/_PROTO_ _IP_:_PORT_;branch=z9hG4bKx._GCNT_._BCNT_.0\r\n
50 From: <_KAFROM_>;tag=_RUID_-_AORHASH_-_TIMESEC_-_TIMEUSEC_-_GCNT_._BCNT_\r\n
52 Call-ID: _KACALLID_\r\n
53 CSeq: 1 _KAMETHOD_\r\n
54 Content-Length: 0\r\n\r\n"
58 #define ULKA_CALLID_PREFIX "ksrulka-"
59 #define ULKA_CALLID_PREFIX_LEN (sizeof(ULKA_CALLID_PREFIX) - 1)
61 #define ULKA_MSG "%.*s %.*s SIP/2.0\r\n" \
62 "Via: SIP/2.0/%.*s %.*s:%.*s;branch=z9hG4bKx.%u.%u.0\r\n" \
64 "From: <%.*s>;tag=%.*s-%x-%lx-%lx-%x.%x\r\n" \
65 "To: <sip:%.*s%s%.*s>\r\n" \
66 "Call-ID: " ULKA_CALLID_PREFIX "%u.%u\r\n" \
68 "Content-Length: 0\r\n\r\n"
70 extern str ul_ka_from;
71 extern str ul_ka_domain;
72 extern str ul_ka_method;
73 extern int ul_ka_mode;
74 extern int ul_ka_filter;
75 extern int ul_ka_loglevel;
76 extern pv_elem_t *ul_ka_logfmt;
78 extern unsigned int ul_nat_bflag;
80 static unsigned int _ul_ka_counter = 0;
85 int ul_ka_urecord(urecord_t *ur)
88 #define ULKA_BUF_SIZE 2048
89 char kabuf[ULKA_BUF_SIZE];
95 str sproto = STR_NULL;
101 unsigned int bcnt = 0;
106 if (ul_ka_mode == ULKA_NONE) {
109 LM_DBG("keepalive for aor: %.*s\n", ur->aor.len, ur->aor.s);
111 for(i=0; i<ur->aor.len; i++) {
112 if(ur->aor.s[i] == '@') {
118 for (uc = ur->contacts; uc != NULL; uc = uc->next) {
119 if (uc->c.len <= 0) {
122 if((ul_ka_filter&GAU_OPT_SERVER_ID) && (uc->server_id != server_id)) {
125 if(ul_ka_mode & ULKA_NAT) {
126 /* keepalive for natted contacts only */
127 if (ul_nat_bflag == 0) {
130 if ((uc->cflags & ul_nat_bflag) != ul_nat_bflag) {
134 if(uc->received.len > 0) {
137 if (uc->path.len > 0) {
138 if(get_path_dst_uri(&uc->path, &sdst) < 0) {
139 LM_ERR("failed to get first uri for path\n");
146 if(parse_uri(sdst.s, sdst.len, &duri) < 0) {
147 LM_ERR("cannot parse next hop uri\n");
151 if(duri.port_no == 0) {
152 duri.port_no = SIP_PORT;
155 he = sip_resolvehost(&duri.host, &duri.port_no, &dproto);
157 LM_ERR("cannot resolve destination\n");
160 if(ul_ka_mode & ULKA_UDP) {
161 if(dproto != PROTO_UDP) {
162 LM_DBG("skipping non-udp contact - proto %d\n", (int)dproto);
166 init_dest_info(&idst);
167 hostent2su(&idst.to, he, 0, duri.port_no);
170 ssock = get_send_socket(0, &idst.to, dproto);
173 LM_ERR("cannot get sending socket\n");
177 idst.send_sock = ssock;
179 if(ssock->useinfo.name.len > 0) {
180 vaddr = ssock->useinfo.name;
182 vaddr = ssock->address_str;
184 if(ssock->useinfo.port_no > 0) {
185 vport = ssock->useinfo.port_no_str;
187 vport = ssock->port_no_str;
189 get_valid_proto_string(dproto, 1, 1, &sproto);
192 gettimeofday(&tv, NULL);
193 kabuf_len = snprintf(kabuf, ULKA_BUF_SIZE - 1, ULKA_MSG,
194 ul_ka_method.len, ul_ka_method.s,
196 sproto.len, sproto.s,
199 _ul_ka_counter, bcnt,
200 (uc->path.len>0)?"Route: ":"",
201 (uc->path.len>0)?uc->path.len:0,
202 (uc->path.len>0)?uc->path.s:"",
203 (uc->path.len>0)?2:0,
204 (uc->path.len>0)?"\r\n":"",
205 ul_ka_from.len, ul_ka_from.s,
206 uc->ruid.len, uc->ruid.s, ur->aorhash,
207 (unsigned long)tv.tv_sec, (unsigned long)tv.tv_usec,
208 _ul_ka_counter, bcnt,
209 ur->aor.len, ur->aor.s,
211 (aortype==1)?0:ul_ka_domain.len, (aortype==1)?"":ul_ka_domain.s,
212 _ul_ka_counter, bcnt,
213 ul_ka_method.len, ul_ka_method.s);
214 if(kabuf_len<=0 || kabuf_len>=ULKA_BUF_SIZE) {
215 LM_ERR("failed to print the keepalive request\n");
217 LM_DBG("keepalive request (len: %d) [[\n%.*s]]\n",
218 kabuf_len, kabuf_len, kabuf);
220 kamsg.len = kabuf_len;
221 ul_ka_send(&kamsg, &idst);
231 static int ul_ka_send(str *kamsg, dest_info_t *kadst)
233 if (kadst->proto == PROTO_UDP) {
234 return udp_send(kadst, kamsg->s, kamsg->len);
238 else if(kadst->proto == PROTO_TCP) {
241 return tcp_send(kadst, 0, kamsg->s, kamsg->len);
245 else if(kadst->proto == PROTO_TLS) {
248 return tcp_send(kadst, 0, kamsg->s, kamsg->len);
252 else if(kadst->proto == PROTO_SCTP) {
254 return sctp_core_msg_send(kadst, kamsg->s, kamsg->len);
258 LM_ERR("unknown proto [%d] for sending keepalive\n",
267 unsigned long ul_ka_fromhex(str *shex, int *err)
273 for (i=0; i<shex->len; i++) {
275 if (b >= '0' && b <= '9') b = b - '0';
276 else if (b >= 'a' && b <='f') b = b - 'a' + 10;
277 else if (b >= 'A' && b <='F') b = b - 'A' + 10;
278 else { *err = 1; return 0; };
279 v = (v << 4) | (b & 0xF);
287 int ul_ka_reply_received(sip_msg_t *msg)
293 unsigned int aorhash;
299 if(msg->cseq == NULL) {
300 if((parse_headers(msg, HDR_CSEQ_F, 0) == -1) || (msg->cseq == NULL)) {
301 LM_ERR("invalid CSeq header\n");
306 if(get_cseq(msg)->method.len != ul_ka_method.len) {
309 if(strncmp(get_cseq(msg)->method.s, ul_ka_method.s, ul_ka_method.len) != 0) {
313 /* there must be no second via */
314 if(!(parse_headers(msg, HDR_VIA2_F, 0) == -1 || (msg->via2 == 0)
315 || (msg->via2->error != PARSE_OK))) {
320 if((parse_from_header(msg)) < 0) {
321 LM_ERR("cannot parse From header\n");
326 if(fb->uri.len != ul_ka_from.len
327 || strncmp(fb->uri.s, ul_ka_from.s, ul_ka_from.len) != 0) {
331 /* from-tag is: ruid-aorhash-tmsec-tmusec-counter */
332 if(fb->tag_value.len <= 8) {
336 LM_DBG("checking keepalive reply [%.*s]\n", fb->tag_value.len,
339 /* todo: macro/function to tokenize */
341 p = q_memrchr(fb->tag_value.s, '-', fb->tag_value.len);
343 LM_DBG("from tag format mismatch [%.*s]\n", fb->tag_value.len,
349 tok.len = p - fb->tag_value.s;
350 p = q_memrchr(fb->tag_value.s, '-', tok.len);
352 LM_DBG("from tag format mismatch [%.*s]\n", fb->tag_value.len,
357 tok.len = fb->tag_value.s + tok.len - tok.s;
359 LM_DBG("empty token\n");
362 LM_DBG("tv usec string is [%.*s] (%d)\n", tok.len, tok.s, tok.len);
363 tvm.tv_usec = ul_ka_fromhex(&tok, &err);
365 LM_DBG("invalid tv usec value\n");
368 LM_DBG("tv usec is [%lu]\n", (unsigned long)tvm.tv_usec);
371 tok.len = p - fb->tag_value.s;
372 p = q_memrchr(fb->tag_value.s, '-', tok.len);
374 LM_DBG("from tag format mismatch [%.*s]\n", fb->tag_value.len,
379 tok.len = fb->tag_value.s + tok.len - tok.s;
381 LM_DBG("empty token\n");
384 LM_DBG("tv sec string is [%.*s] (%d)\n", tok.len, tok.s, tok.len);
385 tvm.tv_sec = ul_ka_fromhex(&tok, &err);
387 LM_DBG("invalid tv sec value\n");
390 LM_DBG("tv sec is [%lu]\n", (unsigned long)tvm.tv_sec);
393 tok.len = p - fb->tag_value.s;
394 p = q_memrchr(fb->tag_value.s, '-', tok.len);
396 LM_DBG("from tag format mismatch [%.*s]\n", fb->tag_value.len,
401 tok.len = fb->tag_value.s + tok.len - tok.s;
403 LM_DBG("empty token\n");
406 LM_DBG("aor hash string is [%.*s] (%d)\n", tok.len, tok.s, tok.len);
407 aorhash = ul_ka_fromhex(&tok, &err);
409 LM_DBG("invalid aor hash value\n");
412 LM_DBG("aor hash is [%u]\n", aorhash);
414 ruid.s = fb->tag_value.s;
415 ruid.len = tok.s - ruid.s - 1;
418 LM_DBG("cannot get ruid in [%.*s]\n", fb->tag_value.len,
423 gettimeofday(&tvn, NULL);
424 tvdiff = (tvn.tv_sec - tvm.tv_sec) * 1000000
425 + (tvn.tv_usec - tvm.tv_usec);
426 ul_update_keepalive(aorhash, &ruid, tvn.tv_sec, tvdiff);
428 if(ul_ka_loglevel != 255 && ul_ka_logfmt != NULL) {
429 if (pv_printf_s(msg, ul_ka_logfmt, &tok) == 0) {
430 LOG(ul_ka_loglevel, "keepalive roundtrip: %u.%06u sec - ruid [%.*s]%.*s\n",
431 tvdiff/1000000, tvdiff%1000000, ruid.len, ruid.s,
437 LM_DBG("response of keepalive for ruid [%.*s] aorhash [%u] roundtrip: %u.%06u secs\n",
438 ruid.len, ruid.s, aorhash, tvdiff/1000000, tvdiff%1000000);