Module: sip-router
Branch: master
Commit: 40a183b716853a08b523349b92a8d4ad0470e360
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=40a183b…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: Wed Jan 27 23:17:59 2010 +0100
pv(k): added new PV class to get timeval attributes
- $TV(s) - struct timeval tv_sec (cached per sip message)
- $TV(u) - struct timeval tv_usec (cached per sip message)
- $TV(sn) - struct timeval tv_sec (not cached)
- $TV(un) - struct timeval tv_usec (not cached)
---
modules_k/pv/pv.c | 2 +
modules_k/pv/pv_time.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++
modules_k/pv/pv_time.h | 3 ++
3 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/modules_k/pv/pv.c b/modules_k/pv/pv.c
index 54c293c..6455a93 100644
--- a/modules_k/pv/pv.c
+++ b/modules_k/pv/pv.c
@@ -364,6 +364,8 @@ static pv_export_t mod_pvs[] = {
pv_set_shvar, pv_parse_shvar_name, 0, 0, 0},
{ {"time", (sizeof("time")-1)}, PVT_CONTEXT, pv_get_time,
0, pv_parse_time_name, 0, 0, 0},
+ { {"TV", (sizeof("TV")-1)}, PVT_OTHER, pv_get_timeval,
+ 0, pv_parse_timeval_name, 0, 0, 0},
{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};
diff --git a/modules_k/pv/pv_time.c b/modules_k/pv/pv_time.c
index 9628463..0bc62a2 100644
--- a/modules_k/pv/pv_time.c
+++ b/modules_k/pv/pv_time.c
@@ -26,6 +26,7 @@
#include <string.h>
#include <stdlib.h>
#include <time.h>
+#include <sys/time.h>
#include "../../dprint.h"
#include "../../pvar.h"
@@ -197,3 +198,90 @@ int pv_get_timef(struct sip_msg *msg, pv_param_t *param,
return pv_get_strintval(msg, param, res, &s, (int)t);
}
+static struct timeval _timeval_ts;
+static unsigned int _timeval_msg_id = 0;
+
+int pv_get_timeval(struct sip_msg *msg, pv_param_t *param,
+ pv_value_t *res)
+{
+ struct timeval tv;
+
+ if(msg==NULL || param==NULL)
+ return -1;
+
+ switch(param->pvn.u.isname.name.n)
+ {
+ case 1:
+ if(_timeval_msg_id != msg->id)
+ {
+ if(gettimeofday(&_timeval_ts, NULL)!=0)
+ {
+ LM_ERR("unable to get time val attributes\n");
+ return -1;
+ }
+ _timeval_msg_id = msg->id;
+ }
+ return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_usec);
+ case 2:
+ if(gettimeofday(&tv, NULL)!=0)
+ {
+ LM_ERR("unable to get time val attributes\n");
+ return pv_get_null(msg, param, res);
+ }
+ return pv_get_uintval(msg, param, res, (unsigned int)tv.tv_sec);
+ case 3:
+ if(gettimeofday(&tv, NULL)!=0)
+ {
+ LM_ERR("unable to get time val attributes\n");
+ return pv_get_null(msg, param, res);
+ }
+ return pv_get_uintval(msg, param, res, (unsigned int)tv.tv_usec);
+ default:
+ if(_timeval_msg_id != msg->id)
+ {
+ if(gettimeofday(&_timeval_ts, NULL)!=0)
+ {
+ LM_ERR("unable to get time val attributes\n");
+ return -1;
+ }
+ _timeval_msg_id = msg->id;
+ }
+ return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_sec);
+ }
+}
+
+int pv_parse_timeval_name(pv_spec_p sp, str *in)
+{
+ if(sp==NULL || in==NULL || in->len<=0)
+ return -1;
+
+ switch(in->len)
+ {
+ case 1:
+ if(strncmp(in->s, "s", 1)==0)
+ sp->pvp.pvn.u.isname.name.n = 0;
+ else if(strncmp(in->s, "u", 1)==0)
+ sp->pvp.pvn.u.isname.name.n = 1;
+ else goto error;
+ break;
+ case 2:
+ if(strncmp(in->s, "sn", 2)==0)
+ sp->pvp.pvn.u.isname.name.n = 2;
+ else if(strncmp(in->s, "un", 2)==0)
+ sp->pvp.pvn.u.isname.name.n = 3;
+ else goto error;
+ break;
+ default:
+ goto error;
+ }
+ sp->pvp.pvn.type = PV_NAME_INTSTR;
+ sp->pvp.pvn.u.isname.type = 0;
+
+ return 0;
+
+error:
+ LM_ERR("unknown PV timeval name %.*s\n", in->len, in->s);
+ return -1;
+}
+
+
diff --git a/modules_k/pv/pv_time.h b/modules_k/pv/pv_time.h
index 05fc37d..4a6caf6 100644
--- a/modules_k/pv/pv_time.h
+++ b/modules_k/pv/pv_time.h
@@ -38,5 +38,8 @@ int pv_get_times(struct sip_msg *msg, pv_param_t *param,
int pv_get_timef(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res);
+int pv_parse_timeval_name(pv_spec_p sp, str *in);
+int pv_get_timeval(struct sip_msg *msg, pv_param_t *param,
+ pv_value_t *res);
#endif