Browse code

siputils: added hdr_date_check(tdiff)

- returns true if sip message has Date header and its value is lower
than NOW() - tdiff

Daniel-Constantin Mierla authored on 16/06/2021 10:03:38
Showing 1 changed files
... ...
@@ -66,6 +66,7 @@
66 66
 #include "../../core/kemi.h"
67 67
 #include "../../core/parser/parse_option_tags.h"
68 68
 #include "../../core/parser/parse_uri.h"
69
+#include "../../core/parser/parse_date.h"
69 70
 
70 71
 #include "ring.h"
71 72
 #include "options.h"
... ...
@@ -113,6 +114,8 @@ static int w_contact_param_decode(sip_msg_t *msg, char *pnparam, char *p2);
113 114
 static int w_contact_param_decode_ruri(sip_msg_t *msg, char *pnparam, char *p2);
114 115
 static int w_contact_param_rm(sip_msg_t *msg, char *pnparam, char *p2);
115 116
 
117
+static int w_hdr_date_check(sip_msg_t *msg, char *ptdiff, char *p2);
118
+
116 119
 /* Fixup functions to be defined later */
117 120
 static int fixup_set_uri(void** param, int param_no);
118 121
 static int fixup_free_set_uri(void** param, int param_no);
... ...
@@ -205,6 +208,8 @@ static cmd_export_t cmds[]={
205 208
 		fixup_spve_null, fixup_free_spve_null, REQUEST_ROUTE},
206 209
 	{"contact_param_rm",      (cmd_function)w_contact_param_rm,    1,
207 210
 		fixup_spve_null, fixup_free_spve_null, REQUEST_ROUTE|ONREPLY_ROUTE},
211
+	{"hdr_date_check",  (cmd_function)w_hdr_date_check,      1, fixup_igp_null,
212
+		fixup_free_igp_null, ANY_ROUTE},
208 213
 
209 214
 	{"bind_siputils",       (cmd_function)bind_siputils,           1, 0,
210 215
 		0, 0},
... ...
@@ -548,6 +553,66 @@ static int ki_is_uri(sip_msg_t* msg, str* suri)
548 553
 	return 1;
549 554
 }
550 555
 
556
+/*
557
+ * Check date header value with time difference
558
+ */
559
+static int ki_hdr_date_check(sip_msg_t* msg, int tdiff)
560
+{
561
+	time_t tnow, tmsg;
562
+
563
+	if ((!msg->date) && (parse_headers(msg, HDR_DATE_F, 0) == -1)) {
564
+		LM_ERR("failed parsing Date header\n");
565
+		return -1;
566
+	}
567
+	if (!msg->date) {
568
+		LM_ERR("Date header field is not found\n");
569
+		return -1;
570
+	}
571
+	if ((!(msg->date)->parsed) && (parse_date_header(msg) < 0)) {
572
+		LM_ERR("failed parsing DATE body\n");
573
+		return -1;
574
+	}
575
+
576
+#ifdef HAVE_TIMEGM
577
+	tmsg=timegm(&get_date(msg)->date);
578
+#else
579
+	tmsg=_timegm(&get_date(msg)->date);
580
+#endif
581
+	if (tmsg < 0) {
582
+		LM_ERR("timegm error\n");
583
+		return -2;
584
+	}
585
+
586
+	if ((tnow=time(0)) < 0) {
587
+		LM_ERR("time error %s\n", strerror(errno));
588
+		return -3;
589
+	}
590
+
591
+	if (tnow > tmsg + tdiff) {
592
+		LM_ERR("autdated date header value (%ld sec)\n", tnow - tmsg + tdiff);
593
+		return -4;
594
+	} else {
595
+		LM_ERR("Date header value OK\n");
596
+	}
597
+
598
+	return 1;
599
+
600
+}
601
+
602
+/**
603
+ *
604
+ */
605
+static int w_hdr_date_check(sip_msg_t *msg, char *ptdiff, char *p2)
606
+{
607
+	int tdiff = 0;
608
+
609
+	if(fixup_get_ivalue(msg, (gparam_t*)ptdiff, &tdiff)<0) {
610
+		LM_ERR("failed to get time diff parameter\n");
611
+		return -1;
612
+	}
613
+	return ki_hdr_date_check(msg, tdiff);
614
+}
615
+
551 616
 /**
552 617
  *
553 618
  */
... ...
@@ -668,6 +733,11 @@ static sr_kemi_t sr_kemi_siputils_exports[] = {
668 733
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
669 734
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
670 735
 	},
736
+	{ str_init("siputils"), str_init("hdr_date_check"),
737
+		SR_KEMIP_INT, ki_hdr_date_check,
738
+		{ SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
739
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
740
+	},
671 741
 
672 742
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
673 743
 };